-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
246 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.