前段时间,因为客户有个项目要求跨局域网进行远程控制桌面,想知道能不能实现。于是查询了许多资料,了解到需要有公网服务器作为中介才能够实现,但是公司又没有公网服务器,于是只有利用花生壳、natapp服务器实现内网穿透服务。但是这些都要收费,免费的域名和端口是随机变动的,对于测试相当不方便。于是就想设计一款工具能够自动获取natapp产生的动态域名与端口。申请natapp的账号及隧道这里就不用介绍,网上资料一大把,官网也有:https://natapp.cn/article/natapp_newbie可以申请到三个免费不同协议的隧道WEB,TCP,UDP。
配置好cogfig.ini文件后,Natapp.exe运行起来界面是这样的,我们需要的是Forwarding后面的值, 刚开始想利用图像处理的
方式进行OCR文字识别,但是Tesseract的识别率太低,训练可以提高准确率,但是也比较麻烦,于是半途还是放弃了。功夫不负有心人,摸索半天被我寻找到了其他的方法。根据config.ini文件介绍,尝试使用了Log的功能,想看看log里面都有些啥。原来在Debug日志里面,记录了得到的所有消息。这样就有办法进行自动获取了。
下面看具体执行步骤吧:
1.首先将官网下载的Natapp.exe文件放置到一目录下
2.然后打开本工具,选取文件所在地址,如下图:
3.填入natapp提供的authtoken,点击解析,经过2s左右即可获取域名端口
WEB协议:
TCP协议:
UDP:协议
以上,是测试步骤,调用了本人封装好的DLL,在自己的项目中引用时,只需要提供authtoken,协议类型以及natapp文件的地址即可获取。后台会自动打开natapp服务,没有任何窗体展示。
下面开始重点了,DLL源码放送:
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace NatappToolDll
{
/// <summary>
/// 通道协议
/// </summary>
public enum TunnelingProtocol
{
WEB = 0,
TCP = 1,
UDP = 2
}
public class NatappResolve
{
Process process;
static string logPath = "log.txt";
/// <summary>
/// 开始解析
/// </summary>
/// <param name="natappDir">natapp.exe目录</param>
/// <param name="authtoken">authtoken码</param>
/// <param name="domainNameAndPort">域名和端口号</param>
/// <returns></returns>
public bool StartResolve(string natappDir, string authtoken, TunnelingProtocol tunnelingProtocol, out ResolveResult resolveResult)
{
resolveResult = new ResolveResult();
try
{
//【1】创建配置文件
string natappFilePath = natappDir + "\natapp.exe";
if (File.Exists(natappFilePath) && authtoken != "")
{
CreatConfigureFile(natappDir, authtoken);
}
else
return false;
//【2】打开natapp.exe
StartNatapp(natappFilePath);
Thread.Sleep(1000);
//【3】分析日志
using (FileStream fs = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default))
{
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
sb.AppendLine(sr.ReadLine() + "<br>");
}
string str = sb.ToString();
int x = str.IndexOf("Url"); //定位位置
string validData = str.Substring(x, 100);
string[] strTmps = validData.Split('"');
string[] serverStr = strTmps[2].Split(':');
resolveResult.serverUrl = serverStr[0] + ":" + serverStr[1];
if (tunnelingProtocol == TunnelingProtocol.TCP || tunnelingProtocol == TunnelingProtocol.UDP)
resolveResult.serverPort = int.Parse(serverStr[2]);
resolveResult.localIP = strTmps[10].Split(':')[0];
resolveResult.localPort = int.Parse(strTmps[10].Split(':')[1]);
return true;
}
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 删除日志文件
/// </summary>
/// <param name="directoryPath"> 文件夹路径 </param>
/// <param name="fileName"> 文件名称 </param>
public static void DeleteFolder()
{
DirectoryInfo di = new DirectoryInfo(Application.StartupPath);
FileInfo[] fis = di.GetFiles();
foreach (FileInfo fi in fis)
{
try
{
if (fi.Name.Contains(logPath))
{
fi.Delete();
}
}
catch (Exception ex)
{
}
}
}
/// <summary>
/// 创建natapp的配置文件config.ini
/// </summary>
/// <param name="dirPath"></param>
/// <param name="authtoken"></param>
/// <param name="clienttoken"></param>
/// <param name="logFilePath"></param>
/// <param name="loglevel"></param>
/// <param name="httpProxy"></param>
private void CreatConfigureFile(string dirPath, string authtoken, string clienttoken = null)
{
string filePath = dirPath + "\config.ini";
try
{
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
StringBuilder sb = new StringBuilder();
sb.AppendLine("[default]");
sb.AppendLine("authtoken=" + authtoken);//对应一条隧道的authtoken
sb.AppendLine("clienttoken=" + clienttoken);//对应客户端的clienttoken,将会忽略authtoken,若无请留空
sb.AppendLine("log=" +logPath);//log 日志文件,可指定本地文件, none=不做记录,stdout=直接屏幕输出 ,默认为none
sb.AppendLine("loglevel=DEBUG");//日志等级 DEBUG, INFO, WARNING, ERROR 默认为 DEBUG
sb.AppendLine("http_proxy=");//代理设置 如 http://10.123.10.10:3128 非代理上网用户请务必留空
string str = sb.ToString(); ;
byte[] data = System.Text.Encoding.Default.GetBytes(str);
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
catch(Exception ex)
{
}
}
/// <summary>
/// 打开natapp.exe 并嵌套到本程序窗体内
/// </summary>
private void StartNatapp(string appPath)
{
KillProcess("natapp");
DeleteFolder();
process = null;
process = new Process();
process.StartInfo.FileName = appPath; // 需要启动的程序
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // 为了美观,启动的时候最小化程序
process.Start();
Thread.Sleep(1000); // 这里必须等待,否则启动程序的句柄还没有创建,不能控制程序
}
/// <summary>
/// 关闭进程
/// </summary>
/// <param name="processName">进程名</param>
private void KillProcess(string processName)
{
Process[] myproc = Process.GetProcesses();
foreach (Process item in myproc)
{
if (item.ProcessName == processName)
{
item.Kill();
}
}
}
/// <summary>
/// 关闭natapp服务
/// </summary>
public void CloseNatappServer()
{
KillProcess("natapp");
}
}
}
域名端口类:
namespace NatappToolDll
{
public class ResolveResult
{
/// <summary>
/// 服务器域名
/// </summary>
public string serverUrl { get; set; }
/// <summary>
/// 服务器端口
/// </summary>
public int? serverPort { get; set; }
/// <summary>
/// 本地地址
/// </summary>
public string localIP { get; set; }
/// <summary>
/// 本地端口
/// </summary>
public int localPort { get; set; }
}
}
调用方法:引用NatappToolDll.dll,执行以下方法即可获取:
private void btnStartWeb_Click(object sender, EventArgs e)
{
string _natappPath = tbNatappAddress.Text;
string _authtoken = tbAuthtokenWeb.Text;
tbProtocol.Text = "WEB";
StartResolve(_natappPath, NatappToolDll.TunnelingProtocol.WEB,_authtoken);
}
/// <summary>
/// 开始解析
/// </summary>
/// <param name="natappPath"></param>
/// <param name="authtoken"></param>
public void StartResolve(string natappPath, NatappToolDll.TunnelingProtocol tunnelingProtocol, string authtoken)
{
NatappToolDll.ResolveResult rr = new NatappToolDll.ResolveResult();
bool res = natappResolve.StartResolve(natappPath, authtoken, tunnelingProtocol, out rr);
tbServerUrl.Text = rr.serverUrl;
tbServerPort.Text = rr.serverPort.ToString();
tbLocalIP.Text = rr.localIP;
tbLocalPort.Text = rr.localPort.ToString();
}
源码地址:https://github.com/FreshBreezes/NatappAutoGetUrl.git
使用本工具可方便个人学习,避免每次开启程序需要手动添加域名和端口的麻烦,如果觉得好用,请多多鼓励!