mirror of
https://github.com/netchx/netch.git
synced 2026-05-11 23:45:06 +08:00
Make Task Analyzer Happy
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Netch.Interfaces;
|
||||
using static Netch.Interops.AioDNS;
|
||||
|
||||
@@ -9,9 +10,9 @@ namespace Netch.Controllers
|
||||
{
|
||||
public string Name => "DNS Service";
|
||||
|
||||
public void Stop()
|
||||
public async Task StopAsync()
|
||||
{
|
||||
Free();
|
||||
await FreeAsync();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
|
||||
@@ -4,8 +4,8 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.Threading;
|
||||
using Netch.Models;
|
||||
using Netch.Utils;
|
||||
using Serilog;
|
||||
@@ -67,7 +67,7 @@ namespace Netch.Controllers
|
||||
Instance.Dispose();
|
||||
}
|
||||
|
||||
protected void StartGuard(string argument, ProcessPriorityClass priority = ProcessPriorityClass.Normal)
|
||||
protected async Task StartGuardAsync(string argument, ProcessPriorityClass priority = ProcessPriorityClass.Normal)
|
||||
{
|
||||
State = State.Starting;
|
||||
|
||||
@@ -82,8 +82,8 @@ namespace Netch.Controllers
|
||||
|
||||
if (RedirectOutput)
|
||||
{
|
||||
Task.Run(() => ReadOutput(Instance.StandardOutput));
|
||||
Task.Run(() => ReadOutput(Instance.StandardError));
|
||||
Task.Run(() => ReadOutput(Instance.StandardOutput)).Forget();
|
||||
Task.Run(() => ReadOutput(Instance.StandardError)).Forget();
|
||||
|
||||
if (!StartedKeywords.Any())
|
||||
{
|
||||
@@ -95,20 +95,20 @@ namespace Netch.Controllers
|
||||
// wait ReadOutput change State
|
||||
for (var i = 0; i < 1000; i++)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
await Task.Delay(50);
|
||||
switch (State)
|
||||
{
|
||||
case State.Started:
|
||||
OnStarted();
|
||||
return;
|
||||
case State.Stopped:
|
||||
StopGuard();
|
||||
await StopGuardAsync();
|
||||
OnStartFailed();
|
||||
throw new MessageException($"{Name} 控制器启动失败");
|
||||
}
|
||||
}
|
||||
|
||||
StopGuard();
|
||||
await StopGuardAsync();
|
||||
throw new MessageException($"{Name} 控制器启动超时");
|
||||
}
|
||||
}
|
||||
@@ -136,12 +136,12 @@ namespace Netch.Controllers
|
||||
State = State.Stopped;
|
||||
}
|
||||
|
||||
public virtual void Stop()
|
||||
public virtual async Task StopAsync()
|
||||
{
|
||||
StopGuard();
|
||||
await StopGuardAsync();
|
||||
}
|
||||
|
||||
protected void StopGuard()
|
||||
protected async Task StopGuardAsync()
|
||||
{
|
||||
_logStreamWriter?.Close();
|
||||
_logFileStream?.Close();
|
||||
@@ -151,7 +151,7 @@ namespace Netch.Controllers
|
||||
if (Instance is { HasExited: false })
|
||||
{
|
||||
Instance.Kill();
|
||||
Instance.WaitForExit();
|
||||
await Instance.WaitForExitAsync();
|
||||
}
|
||||
}
|
||||
catch (Win32Exception e)
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Netch.Controllers
|
||||
{
|
||||
Log.Information("启动主控制器: {Server} {Mode}", $"{server.Type}", $"[{(int) mode.Type}]{mode.Remark}");
|
||||
|
||||
if (DnsUtils.Lookup(server.Hostname) == null)
|
||||
if (await DnsUtils.LookupAsync(server.Hostname) == null)
|
||||
throw new MessageException(i18N.Translate("Lookup Server hostname failed"));
|
||||
|
||||
Mode = mode;
|
||||
@@ -45,9 +45,9 @@ namespace Netch.Controllers
|
||||
try
|
||||
{
|
||||
if (!ModeHelper.SkipServerController(server, mode))
|
||||
server = await Task.Run(() => StartServer(server));
|
||||
server = await StartServerAsync(server);
|
||||
|
||||
await Task.Run(() => StartMode(server, mode));
|
||||
await StartModeAsync(server, mode);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -67,7 +67,7 @@ namespace Netch.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
private static Server StartServer(Server server)
|
||||
private static async Task<Server> StartServerAsync(Server server)
|
||||
{
|
||||
ServerController = ServerHelper.GetUtilByTypeName(server.Type).GetController();
|
||||
|
||||
@@ -76,7 +76,7 @@ namespace Netch.Controllers
|
||||
Global.MainForm.StatusText(i18N.TranslateFormat("Starting {0}", ServerController.Name));
|
||||
|
||||
Log.Debug("Server Information: {Data}", $"{server.Type} {server.MaskedData()}");
|
||||
var socks5 = ServerController.Start(server);
|
||||
var socks5 = await ServerController.StartAsync(server);
|
||||
|
||||
StatusPortInfoText.Socks5Port = socks5.Port;
|
||||
StatusPortInfoText.UpdateShareLan();
|
||||
@@ -84,7 +84,7 @@ namespace Netch.Controllers
|
||||
return socks5;
|
||||
}
|
||||
|
||||
private static void StartMode(Server server, Mode mode)
|
||||
private static async Task StartModeAsync(Server server, Mode mode)
|
||||
{
|
||||
ModeController = ModeHelper.GetModeControllerByType(mode.Type, out var port, out var portName);
|
||||
|
||||
@@ -93,7 +93,7 @@ namespace Netch.Controllers
|
||||
|
||||
Global.MainForm.StatusText(i18N.TranslateFormat("Starting {0}", ModeController.Name));
|
||||
|
||||
ModeController.Start(server, mode);
|
||||
await ModeController.StartAsync(server, mode);
|
||||
}
|
||||
|
||||
public static async Task StopAsync()
|
||||
@@ -103,12 +103,12 @@ namespace Netch.Controllers
|
||||
|
||||
StatusPortInfoText.Reset();
|
||||
|
||||
Task.Run(() => NTTController.Stop()).Forget();
|
||||
Task.Run(() => NTTController.StopAsync()).Forget();
|
||||
|
||||
var tasks = new[]
|
||||
{
|
||||
Task.Run(() => ServerController?.Stop()),
|
||||
Task.Run(() => ModeController?.Stop())
|
||||
Task.Run(() => ServerController?.StopAsync()),
|
||||
Task.Run(() => ModeController?.StopAsync())
|
||||
};
|
||||
|
||||
try
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.ServiceProcess;
|
||||
using System.Threading.Tasks;
|
||||
using Netch.Interfaces;
|
||||
using Netch.Interops;
|
||||
using Netch.Models;
|
||||
@@ -27,7 +28,7 @@ namespace Netch.Controllers
|
||||
|
||||
public string Name => "Redirector";
|
||||
|
||||
public void Start(Server server, Mode mode)
|
||||
public async Task StartAsync(Server server, Mode mode)
|
||||
{
|
||||
_server = server;
|
||||
_mode = mode;
|
||||
@@ -43,7 +44,7 @@ namespace Netch.Controllers
|
||||
// Server
|
||||
Dial(NameList.TYPE_FILTERUDP, _rdrConfig.FilterProtocol.HasFlag(PortType.UDP).ToString().ToLower());
|
||||
Dial(NameList.TYPE_FILTERTCP, _rdrConfig.FilterProtocol.HasFlag(PortType.TCP).ToString().ToLower());
|
||||
dial_Server(_rdrConfig.FilterProtocol, _server);
|
||||
await DialServerAsync(_rdrConfig.FilterProtocol, _server);
|
||||
|
||||
// Mode Rule
|
||||
dial_Name(_mode);
|
||||
@@ -51,13 +52,13 @@ namespace Netch.Controllers
|
||||
// Features
|
||||
Dial(NameList.TYPE_DNSHOST, _rdrConfig.DNSHijack ? _rdrConfig.DNSHijackHost : "");
|
||||
|
||||
if (!Init())
|
||||
if (!await InitAsync())
|
||||
throw new MessageException("Redirector start failed.");
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
public async Task StopAsync()
|
||||
{
|
||||
Free();
|
||||
await FreeAsync();
|
||||
}
|
||||
|
||||
#region CheckRule
|
||||
@@ -102,12 +103,12 @@ namespace Netch.Controllers
|
||||
|
||||
#endregion
|
||||
|
||||
private void dial_Server(PortType portType, in Server server)
|
||||
private async Task DialServerAsync(PortType portType, Server server)
|
||||
{
|
||||
if (portType == PortType.Both)
|
||||
{
|
||||
dial_Server(PortType.TCP, server);
|
||||
dial_Server(PortType.UDP, server);
|
||||
await DialServerAsync(PortType.TCP, server);
|
||||
await DialServerAsync(PortType.UDP, server);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ namespace Netch.Controllers
|
||||
if (server is Socks5 socks5)
|
||||
{
|
||||
Dial(NameList.TYPE_TCPTYPE + offset, "Socks5");
|
||||
Dial(NameList.TYPE_TCPHOST + offset, $"{socks5.AutoResolveHostname()}:{socks5.Port}");
|
||||
Dial(NameList.TYPE_TCPHOST + offset, $"{await socks5.AutoResolveHostnameAsync()}:{socks5.Port}");
|
||||
Dial(NameList.TYPE_TCPUSER + offset, socks5.Username ?? string.Empty);
|
||||
Dial(NameList.TYPE_TCPPASS + offset, socks5.Password ?? string.Empty);
|
||||
Dial(NameList.TYPE_TCPMETH + offset, string.Empty);
|
||||
@@ -124,7 +125,7 @@ namespace Netch.Controllers
|
||||
else if (server is Shadowsocks shadowsocks && !shadowsocks.HasPlugin() && _rdrConfig.RedirectorSS)
|
||||
{
|
||||
Dial(NameList.TYPE_TCPTYPE + offset, "Shadowsocks");
|
||||
Dial(NameList.TYPE_TCPHOST + offset, $"{shadowsocks.AutoResolveHostname()}:{shadowsocks.Port}");
|
||||
Dial(NameList.TYPE_TCPHOST + offset, $"{await shadowsocks.AutoResolveHostnameAsync()}:{shadowsocks.Port}");
|
||||
Dial(NameList.TYPE_TCPMETH + offset, shadowsocks.EncryptMethod);
|
||||
Dial(NameList.TYPE_TCPPASS + offset, shadowsocks.Password);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Netch.Controllers
|
||||
/// 启动 NatTypeTester
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<(string? result, string? localEnd, string? publicEnd)> Start()
|
||||
public async Task<(string? result, string? localEnd, string? publicEnd)> StartAsync()
|
||||
{
|
||||
string? localEnd = null, publicEnd = null, result = null, bindingTest = null;
|
||||
|
||||
@@ -87,7 +87,7 @@ namespace Netch.Controllers
|
||||
Log.Error(e, "{Name} 控制器启动异常", Name);
|
||||
try
|
||||
{
|
||||
Stop();
|
||||
await StopAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.Threading;
|
||||
using Netch.Forms;
|
||||
using Netch.Interfaces;
|
||||
using Netch.Models;
|
||||
@@ -29,7 +30,7 @@ namespace Netch.Controllers
|
||||
|
||||
public override string Name => "pcap2socks";
|
||||
|
||||
public void Start(Server server, Mode mode)
|
||||
public async Task StartAsync(Server server, Mode mode)
|
||||
{
|
||||
_server = server;
|
||||
_mode = mode;
|
||||
@@ -38,18 +39,18 @@ namespace Netch.Controllers
|
||||
|
||||
var argument = new StringBuilder($@"-i \Device\NPF_{outboundNetworkInterface.Id}");
|
||||
if (_server is Socks5 socks5 && !socks5.Auth())
|
||||
argument.Append($" --destination {socks5.AutoResolveHostname()}:{socks5.Port}");
|
||||
argument.Append($" --destination {await socks5.AutoResolveHostnameAsync()}:{socks5.Port}");
|
||||
else
|
||||
argument.Append($" --destination 127.0.0.1:{Global.Settings.Socks5LocalPort}");
|
||||
|
||||
argument.Append($" {_mode.GetRules().FirstOrDefault() ?? "-P n"}");
|
||||
StartGuard(argument.ToString());
|
||||
await StartGuardAsync(argument.ToString());
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
public override async Task StopAsync()
|
||||
{
|
||||
Global.MainForm.Invoke(new Action(() => { _form.Close(); }));
|
||||
StopGuard();
|
||||
await StopGuardAsync();
|
||||
}
|
||||
|
||||
~PcapController()
|
||||
@@ -79,7 +80,7 @@ namespace Netch.Controllers
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
Utils.Utils.Open("https://github.com/zhxie/pcap2socks#dependencies");
|
||||
});
|
||||
}).Forget();
|
||||
|
||||
throw new MessageException("Pleases install pcap2socks's dependency");
|
||||
}
|
||||
|
||||
@@ -30,13 +30,13 @@ namespace Netch.Controllers
|
||||
|
||||
public string Name => "tun2socks";
|
||||
|
||||
public void Start(Server server, Mode mode)
|
||||
public async Task StartAsync(Server server, Mode mode)
|
||||
{
|
||||
_mode = mode;
|
||||
_tunConfig = Global.Settings.TUNTAP;
|
||||
|
||||
if (server is Socks5Bridge socks5Bridge)
|
||||
_serverRemoteAddress = DnsUtils.Lookup(socks5Bridge.RemoteHostname);
|
||||
_serverRemoteAddress = await DnsUtils.LookupAsync(socks5Bridge.RemoteHostname);
|
||||
|
||||
if (_serverRemoteAddress != null && IPAddress.IsLoopback(_serverRemoteAddress))
|
||||
_serverRemoteAddress = null;
|
||||
@@ -58,9 +58,9 @@ namespace Netch.Controllers
|
||||
|
||||
if (server is Socks5 socks5)
|
||||
{
|
||||
Dial(NameList.TYPE_TCPHOST, $"{socks5.AutoResolveHostname()}:{socks5.Port}");
|
||||
Dial(NameList.TYPE_TCPHOST, $"{await socks5.AutoResolveHostnameAsync()}:{socks5.Port}");
|
||||
|
||||
Dial(NameList.TYPE_UDPHOST, $"{socks5.AutoResolveHostname()}:{socks5.Port}");
|
||||
Dial(NameList.TYPE_UDPHOST, $"{await socks5.AutoResolveHostnameAsync()}:{socks5.Port}");
|
||||
|
||||
if (socks5.Auth())
|
||||
{
|
||||
@@ -106,16 +106,16 @@ namespace Netch.Controllers
|
||||
SetupRouteTable();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
public async Task StopAsync()
|
||||
{
|
||||
var tasks = new[]
|
||||
{
|
||||
Task.Run(Free),
|
||||
FreeAsync(),
|
||||
Task.Run(ClearRouteTable),
|
||||
Task.Run(_aioDnsController.Stop)
|
||||
_aioDnsController.StopAsync()
|
||||
};
|
||||
|
||||
Task.WaitAll(tasks);
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
private void CheckDriver()
|
||||
|
||||
@@ -37,14 +37,14 @@ namespace Netch.Controllers
|
||||
|
||||
public static event EventHandler? NewVersionNotFound;
|
||||
|
||||
public static async Task Check(bool isPreRelease)
|
||||
public static async Task CheckAsync(bool isPreRelease)
|
||||
{
|
||||
try
|
||||
{
|
||||
var updater = new GitHubRelease(Owner, Repo);
|
||||
var url = updater.AllReleaseUrl;
|
||||
|
||||
var json = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(url));
|
||||
var (_, json) = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(url));
|
||||
|
||||
var releases = JsonSerializer.Deserialize<List<Release>>(json)!;
|
||||
LatestRelease = GetLatestRelease(releases, isPreRelease);
|
||||
@@ -52,12 +52,12 @@ namespace Netch.Controllers
|
||||
if (VersionUtil.CompareVersion(LatestRelease.tag_name, Version) > 0)
|
||||
{
|
||||
Log.Information("发现新版本");
|
||||
NewVersionFound?.Invoke(null, new EventArgs());
|
||||
NewVersionFound?.Invoke(null, EventArgs.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Information("目前是最新版本");
|
||||
NewVersionNotFound?.Invoke(null, new EventArgs());
|
||||
NewVersionNotFound?.Invoke(null, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
Reference in New Issue
Block a user