Skip to content

Commit

Permalink
✨ 添加NAT检测功能,特别感谢@HMBSbige大佬的轮子
Browse files Browse the repository at this point in the history
  • Loading branch information
AmazingDM committed Mar 5, 2020
1 parent 5985f6c commit 5e1f25a
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 25 deletions.
58 changes: 40 additions & 18 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,30 @@ public static Process GetProcess()
/// </summary>
public TUNTAPController pTUNTAPController;

/// <summary>
/// NTT 控制器
/// </summary>
public NTTController pNTTController;

/// <summary>
/// 启动
/// </summary>
/// <param name="server">服务器</param>
/// <param name="mode">模式</param>
/// <returns>是否启动成功</returns>
public bool Start(Models.Server server, Models.Mode mode)
public (bool, string) Start(Models.Server server, Models.Mode mode)
{
var result = false;
var result = (false, "");
switch (server.Type)
{
case "Socks5":
if (mode.Type == 4)
{
result = false;
result = (false, null);
}
else
{
result = true;
result = (true, null);
}
break;
case "SS":
Expand All @@ -78,54 +83,66 @@ public bool Start(Models.Server server, Models.Mode mode)
{
pSSController = new SSController();
}
result = pSSController.Start(server, mode);
result = (pSSController.Start(server, mode), null);
break;
case "SSR":
KillProcess("ShadowsocksR");
if (pSSRController == null)
{
pSSRController = new SSRController();
}
result = pSSRController.Start(server, mode);
result = (pSSRController.Start(server, mode), null);
break;
case "VMess":
KillProcess("v2ray");
if (pVMessController == null)
{
pVMessController = new VMessController();
}
result = pVMessController.Start(server, mode);
result = (pVMessController.Start(server, mode), null);
break;
}

if (result)
if (result.Item1)
{
if (mode.Type == 0)
{
if (pNFController == null)
{
pNFController = new NFController();
}
if (pNTTController == null)
{
pNTTController = new NTTController();
}
// 进程代理模式,启动 NF 控制器
result = pNFController.Start(server, mode);
result = (pNFController.Start(server, mode), pNTTController.Start().Item2);
}
else if (mode.Type == 1)
{
if (pTUNTAPController == null)
{
pTUNTAPController = new TUNTAPController();
}
if (pNTTController == null)
{
pNTTController = new NTTController();
}
// TUN/TAP 黑名单代理模式,启动 TUN/TAP 控制器
result = pTUNTAPController.Start(server, mode);
result = (pTUNTAPController.Start(server, mode), pNTTController.Start().Item2);
}
else if (mode.Type == 2)
{
if (pTUNTAPController == null)
{
pTUNTAPController = new TUNTAPController();
}
if (pNTTController == null)
{
pNTTController = new NTTController();
}
// TUN/TAP 白名单代理模式,启动 TUN/TAP 控制器
result = pTUNTAPController.Start(server, mode);
result = (pTUNTAPController.Start(server, mode), pNTTController.Start().Item2);
}
else if (mode.Type == 3 || mode.Type == 5)
{
Expand All @@ -134,19 +151,19 @@ public bool Start(Models.Server server, Models.Mode mode)
pHTTPController = new HTTPController();
}
// HTTP 系统代理和 Socks5 和 HTTP 代理模式,启动 HTTP 控制器
result = pHTTPController.Start(server, mode);
result = (pHTTPController.Start(server, mode), null);
}
else if (mode.Type == 4)
{
// Socks5 代理模式,不需要启动额外的控制器
}
else
{
result = false;
result = (false, null);
}
}

if (!result)
if (!result.Item1)
{
Stop();
}
Expand All @@ -171,7 +188,7 @@ public void Stop()
{
pVMessController.Stop();
}

if (pNFController != null)
{
pNFController.Stop();
Expand All @@ -184,10 +201,15 @@ public void Stop()
{
pHTTPController.Stop();
}


if (pNTTController != null)
{
pNTTController.Stop();
}
}

public void KillProcess(string name) {
public void KillProcess(string name)
{
var processes = Process.GetProcessesByName(name);
foreach (var p in processes)
{
Expand All @@ -198,7 +220,7 @@ public void KillProcess(string name) {
}
}

private static bool IsChildProcess(Process process,string name)
private static bool IsChildProcess(Process process, string name)
{
bool result;
try
Expand Down
2 changes: 2 additions & 0 deletions Netch/Controllers/NFController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public bool Start(Models.Server server, Models.Mode mode)
}

var processes = "";

mode.Rule.Add("NTT.exe");
foreach (var proc in mode.Rule)
{
processes += proc;
Expand Down
93 changes: 93 additions & 0 deletions Netch/Controllers/NTTController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Netch.Forms;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Netch.Controllers
{
public class NTTController
{
/// <summary>
/// 进程实例
/// </summary>
public Process Instance;

/// <summary>
/// 当前状态
/// </summary>
public Models.State State = Models.State.Waiting;

/// <summary>
/// 启动NatTypeTester
/// </summary>
/// <returns></returns>
public (bool, string, string, string) Start()
{
try
{
if (!File.Exists("bin\\NTT.exe"))
{
return (false, null, null, null);
}

Instance = MainController.GetProcess();
Instance.StartInfo.FileName = "bin\\NTT.exe";

Instance.StartInfo.Arguments = $" {Global.Settings.STUN_Server} {Global.Settings.STUN_Server_Port}";

Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;

State = Models.State.Starting;
Instance.Start();
Instance.BeginOutputReadLine();
Instance.BeginErrorReadLine();
Instance.WaitForExit();

string[] result = File.ReadAllText("logging\\NTT.log").ToString().Split('#');
var natType = result[0];
var localEnd = result[1];
var publicEnd = result[2];

return (true, natType, localEnd, publicEnd);
}
catch (Exception)
{
Utils.Logging.Info("NTT 进程出错");
Stop();
return (false, null, null, null);
}
}

/// <summary>
/// 停止
/// </summary>
public void Stop()
{
try
{
if (Instance != null && !Instance.HasExited)
{
Instance.Kill();
}
}
catch (Exception e)
{
Utils.Logging.Info(e.ToString());
}
}

public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(e.Data))
{
if (File.Exists("logging\\NTT.log"))
{
File.Delete("logging\\NTT.log");
}
File.AppendAllText("logging\\NTT.log", $"{e.Data}\r\n");
}
}
}
}
11 changes: 10 additions & 1 deletion Netch/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion Netch/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sende
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
ControlButton.Text = Utils.i18N.Translate("Start");
MainController.Stop();
NatTypeStatusLabel.Text = "";
}
Utils.Configuration.Save();
}).ContinueWith(task =>
Expand Down Expand Up @@ -786,7 +787,18 @@ private void ControlButton_Click(object sender, EventArgs e)
var mode = ModeComboBox.SelectedItem as Models.Mode;
MainController = new MainController();
if (MainController.Start(server, mode))
var startResult = MainController.Start(server, mode);//item1是否启动成功,item2nat类型
if (startResult.Item2 != null)
{
NatTypeStatusLabel.Text = "NatType:" + startResult.Item2;
}
else
{
NatTypeStatusLabel.Text = "";
}
if (startResult.Item1)
{
// UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true;
// MainController.pNFController.OnBandwidthUpdated += OnBandwidthUpdated;
Expand Down Expand Up @@ -883,6 +895,7 @@ private void ControlButton_Click(object sender, EventArgs e)
var mode = ModeComboBox.SelectedItem as Models.Mode;
MainController.Stop();
NatTypeStatusLabel.Text = "";
// LastUploadBandwidth = 0;
// LastDownloadBandwidth = 0;
Expand Down
Loading

0 comments on commit 5e1f25a

Please sign in to comment.