Cleanup code

This commit is contained in:
Bruce Wayne
2020-01-23 20:44:17 +08:00
parent c5e46844da
commit 087f6e4eb9
33 changed files with 294 additions and 407 deletions

View File

@@ -1,5 +1,5 @@
using Microsoft.Win32;
using System;
using System;
using Microsoft.Win32;
namespace Netch.Controllers
{
@@ -22,14 +22,12 @@ namespace Netch.Controllers
{
if (server.Type == "Socks5")
{
if (!String.IsNullOrWhiteSpace(server.Username) && !String.IsNullOrWhiteSpace(server.Password))
if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password))
{
return false;
}
else
{
pPrivoxyController.Start(server, mode);
}
pPrivoxyController.Start(server, mode);
}
else
{

View File

@@ -9,7 +9,7 @@ namespace Netch.Controllers
public static Process GetProcess()
{
var process = new Process();
process.StartInfo.WorkingDirectory = String.Format("{0}\\bin", Directory.GetCurrentDirectory());
process.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory());
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
@@ -23,33 +23,33 @@ namespace Netch.Controllers
/// <summary>
/// SS 控制器
/// </summary>
public SSController pSSController = null;
public SSController pSSController;
/// <summary>
/// SSR 控制器
/// </summary>
public SSRController pSSRController = null;
public SSRController pSSRController;
/// <summary>
/// V2Ray 控制器
/// </summary>
public VMessController pVMessController = null;
public VMessController pVMessController;
/// <summary>
/// NF 控制器
/// </summary>
public NFController pNFController = null;
public NFController pNFController;
/// <summary>
/// HTTP 控制器
/// </summary>
public HTTPController pHTTPController = null;
public HTTPController pHTTPController;
/// <summary>
/// TUN/TAP 控制器
/// </summary>
public TUNTAPController pTUNTAPController = null;
public TUNTAPController pTUNTAPController;
/// <summary>
/// 启动
@@ -96,8 +96,6 @@ namespace Netch.Controllers
}
result = pVMessController.Start(server, mode);
break;
default:
break;
}
if (result)
@@ -189,9 +187,9 @@ namespace Netch.Controllers
}
public void KillProcess(String name) {
Process[] processes = Process.GetProcessesByName(name);
foreach (Process p in processes)
public void KillProcess(string name) {
var processes = Process.GetProcessesByName(name);
foreach (var p in processes)
{
if (IsChildProcess(p, name))
{
@@ -205,8 +203,8 @@ namespace Netch.Controllers
bool result;
try
{
string FileName = (Directory.GetCurrentDirectory() + "\\bin\\" + name + ".exe").ToLower();
string procFileName = process.MainModule.FileName.ToLower();
var FileName = (Directory.GetCurrentDirectory() + "\\bin\\" + name + ".exe").ToLower();
var procFileName = process.MainModule.FileName.ToLower();
result = FileName.Equals(procFileName, StringComparison.Ordinal);
}
catch (Exception e)

View File

@@ -19,7 +19,7 @@ namespace Netch.Controllers
/// </summary>
/// <param name="upload">上传</param>
/// <param name="download">下载</param>
public delegate void BandwidthUpdateHandler(long upload, Int64 download);
public delegate void BandwidthUpdateHandler(long upload, long download);
/// <summary>
/// 进程实例
@@ -45,7 +45,7 @@ namespace Netch.Controllers
}
// 生成驱动文件路径
var driver = String.Format("{0}\\drivers\\netfilter2.sys", Environment.SystemDirectory);
var driver = string.Format("{0}\\drivers\\netfilter2.sys", Environment.SystemDirectory);
// 检查驱动是否存在
if (!File.Exists(driver))
@@ -139,9 +139,9 @@ namespace Netch.Controllers
return false;
}
fallback = $"-r {result.ToString()}:{server.Port} -p \"{processes}\"";
fallback = $"-r {result}:{server.Port} -p \"{processes}\"";
if (!String.IsNullOrWhiteSpace(server.Username) && !String.IsNullOrWhiteSpace(server.Password))
if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password))
{
fallback += $" -username \"{server.Username}\" -password \"{server.Password}\"";
}
@@ -156,7 +156,7 @@ namespace Netch.Controllers
Instance.BeginErrorReadLine();
var IsFallback = false;
for (int i = 0; i < 1000; i++)
for (var i = 0; i < 1000; i++)
{
Thread.Sleep(10);
@@ -219,9 +219,9 @@ namespace Netch.Controllers
public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrWhiteSpace(e.Data))
if (!string.IsNullOrWhiteSpace(e.Data))
{
File.AppendAllText("logging\\redirector.log", String.Format("{0}\r\n", e.Data));
File.AppendAllText("logging\\redirector.log", string.Format("{0}\r\n", e.Data));
if (State == Models.State.Starting)
{
@@ -250,7 +250,7 @@ namespace Netch.Controllers
if (uploadSplited.Length == 2 && downloadSplited.Length == 2)
{
if (long.TryParse(uploadSplited[1], out long upload) && long.TryParse(downloadSplited[1], out long download))
if (long.TryParse(uploadSplited[1], out var upload) && long.TryParse(downloadSplited[1], out var download))
{
Task.Run(() => OnBandwidthUpdated(upload, download));
}

View File

@@ -46,13 +46,13 @@ namespace Netch.Controllers
}
Instance = new Process()
Instance = new Process
{
StartInfo =
{
FileName = String.Format("{0}\\bin\\Privoxy.exe", Directory.GetCurrentDirectory()),
FileName = string.Format("{0}\\bin\\Privoxy.exe", Directory.GetCurrentDirectory()),
Arguments = "..\\data\\privoxy.conf",
WorkingDirectory = String.Format("{0}\\bin", Directory.GetCurrentDirectory()),
WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory()),
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
CreateNoWindow = true

View File

@@ -33,7 +33,7 @@ namespace Netch.Controllers
Instance = MainController.GetProcess();
Instance.StartInfo.FileName = "bin\\Shadowsocks.exe";
if (!String.IsNullOrWhiteSpace(server.Plugin) && !String.IsNullOrWhiteSpace(server.PluginOption))
if (!string.IsNullOrWhiteSpace(server.Plugin) && !string.IsNullOrWhiteSpace(server.PluginOption))
{
Instance.StartInfo.Arguments = $"-s {server.Hostname} -p {server.Port} -b {Global.Settings.LocalAddress} -l {Global.Settings.Socks5LocalPort} -m {server.EncryptMethod} -k \"{server.Password}\" -u --plugin {server.Plugin} --plugin-opts \"{server.PluginOption}\"";
}
@@ -54,7 +54,7 @@ namespace Netch.Controllers
Instance.Start();
Instance.BeginOutputReadLine();
Instance.BeginErrorReadLine();
for (int i = 0; i < 1000; i++)
for (var i = 0; i < 1000; i++)
{
Thread.Sleep(10);
@@ -97,7 +97,7 @@ namespace Netch.Controllers
public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrWhiteSpace(e.Data))
if (!string.IsNullOrWhiteSpace(e.Data))
{
// File.AppendAllText("logging\\shadowsocks.log", $"{e.Data}\r\n");

View File

@@ -34,21 +34,21 @@ namespace Netch.Controllers
Instance.StartInfo.FileName = "bin\\ShadowsocksR.exe";
Instance.StartInfo.Arguments = $"-s {server.Hostname} -p {server.Port} -k \"{server.Password}\" -m {server.EncryptMethod}";
if (!String.IsNullOrEmpty(server.Protocol))
if (!string.IsNullOrEmpty(server.Protocol))
{
Instance.StartInfo.Arguments += $" -O {server.Protocol}";
if (!String.IsNullOrEmpty(server.ProtocolParam))
if (!string.IsNullOrEmpty(server.ProtocolParam))
{
Instance.StartInfo.Arguments += $" -G \"{server.ProtocolParam}\"";
}
}
if (!String.IsNullOrEmpty(server.OBFS))
if (!string.IsNullOrEmpty(server.OBFS))
{
Instance.StartInfo.Arguments += $" -o {server.OBFS}";
if (!String.IsNullOrEmpty(server.OBFSParam))
if (!string.IsNullOrEmpty(server.OBFSParam))
{
Instance.StartInfo.Arguments += $" -g \"{server.OBFSParam}\"";
}
@@ -68,7 +68,7 @@ namespace Netch.Controllers
Instance.Start();
Instance.BeginOutputReadLine();
Instance.BeginErrorReadLine();
for (int i = 0; i < 1000; i++)
for (var i = 0; i < 1000; i++)
{
Thread.Sleep(10);
@@ -111,7 +111,7 @@ namespace Netch.Controllers
public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrWhiteSpace(e.Data))
if (!string.IsNullOrWhiteSpace(e.Data))
{
File.AppendAllText("logging\\shadowsocksr.log", $"{e.Data}\r\n");

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
@@ -101,12 +101,12 @@ namespace Netch.Controllers
if (SavedMode.Type == 2) // 处理仅规则内走直连
{
// 将 TUN/TAP 网卡权重放到最高
var instance = new Process()
var instance = new Process
{
StartInfo =
{
FileName = String.Format("netsh"),
Arguments = String.Format("interface ip set interface {0} metric=0", Global.TUNTAP.Index),
FileName = "netsh",
Arguments = string.Format("interface ip set interface {0} metric=0", Global.TUNTAP.Index),
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
CreateNoWindow = true
@@ -166,7 +166,7 @@ namespace Netch.Controllers
{
if (SavedMode.Type == 2)
{
NativeMethods.DeleteRoute("0.0.0.0", 0, Global.Settings.TUNTAP.Gateway.ToString(), Global.TUNTAP.Index, 10);
NativeMethods.DeleteRoute("0.0.0.0", 0, Global.Settings.TUNTAP.Gateway, Global.TUNTAP.Index, 10);
foreach (var ip in SavedMode.Rule)
{
@@ -273,8 +273,8 @@ namespace Netch.Controllers
SetupBypass();
Instance = new Process();
Instance.StartInfo.WorkingDirectory = String.Format("{0}\\bin", Directory.GetCurrentDirectory());
Instance.StartInfo.FileName = String.Format("{0}\\bin\\tun2socks.exe", Directory.GetCurrentDirectory());
Instance.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory());
Instance.StartInfo.FileName = string.Format("{0}\\bin\\tun2socks.exe", Directory.GetCurrentDirectory());
string dns;
if (Global.Settings.TUNTAP.UseCustomDNS)
@@ -297,11 +297,11 @@ namespace Netch.Controllers
if (server.Type == "Socks5")
{
Instance.StartInfo.Arguments = String.Format("-proxyServer {0}:{1} -tunAddr {2} -tunMask {3} -tunGw {4} -tunDns {5} -tunName \"{6}\"", server.Hostname, server.Port, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name);
Instance.StartInfo.Arguments = string.Format("-proxyServer {0}:{1} -tunAddr {2} -tunMask {3} -tunGw {4} -tunDns {5} -tunName \"{6}\"", server.Hostname, server.Port, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name);
}
else
{
Instance.StartInfo.Arguments = String.Format("-proxyServer 127.0.0.1:{0} -tunAddr {1} -tunMask {2} -tunGw {3} -tunDns {4} -tunName \"{5}\"", Global.Settings.Socks5LocalPort, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name);
Instance.StartInfo.Arguments = string.Format("-proxyServer 127.0.0.1:{0} -tunAddr {1} -tunMask {2} -tunGw {3} -tunDns {4} -tunName \"{5}\"", Global.Settings.Socks5LocalPort, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name);
}
Instance.StartInfo.CreateNoWindow = true;
@@ -319,7 +319,7 @@ namespace Netch.Controllers
Instance.BeginOutputReadLine();
Instance.PriorityClass = ProcessPriorityClass.RealTime;
for (int i = 0; i < 1000; i++)
for (var i = 0; i < 1000; i++)
{
Thread.Sleep(10);
@@ -362,9 +362,9 @@ namespace Netch.Controllers
public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data))
if (!string.IsNullOrEmpty(e.Data))
{
File.AppendAllText("logging\\tun2socks.log", String.Format("{0}\r\n", e.Data.Trim()));
File.AppendAllText("logging\\tun2socks.log", string.Format("{0}\r\n", e.Data.Trim()));
if (State == Models.State.Starting)
{

View File

@@ -1,10 +1,10 @@
using Netch.Models.GitHubRelease;
using Newtonsoft.Json;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Netch.Models.GitHubRelease;
using Newtonsoft.Json;
namespace Netch.Controllers
{

View File

@@ -31,32 +31,32 @@ namespace Netch.Controllers
return false;
}
File.WriteAllText("data\\last.json", Newtonsoft.Json.JsonConvert.SerializeObject(new Models.Information.VMess.Config()
File.WriteAllText("data\\last.json", Newtonsoft.Json.JsonConvert.SerializeObject(new Models.Information.VMess.Config
{
inbounds = new List<Models.Information.VMess.Inbounds>()
inbounds = new List<Models.Information.VMess.Inbounds>
{
new Models.Information.VMess.Inbounds()
new Models.Information.VMess.Inbounds
{
settings = new Models.Information.VMess.InboundSettings(),
port = Global.Settings.Socks5LocalPort,
listen = Global.Settings.LocalAddress
}
},
outbounds = new List<Models.Information.VMess.Outbounds>()
outbounds = new List<Models.Information.VMess.Outbounds>
{
new Models.Information.VMess.Outbounds()
new Models.Information.VMess.Outbounds
{
settings = new Models.Information.VMess.OutboundSettings()
settings = new Models.Information.VMess.OutboundSettings
{
vnext = new List<Models.Information.VMess.VNext>()
vnext = new List<Models.Information.VMess.VNext>
{
new Models.Information.VMess.VNext()
new Models.Information.VMess.VNext
{
address = server.Hostname,
port = server.Port,
users = new List<Models.Information.VMess.User>
{
new Models.Information.VMess.User()
new Models.Information.VMess.User
{
id = server.UserID,
alterId = server.AlterID,
@@ -66,66 +66,66 @@ namespace Netch.Controllers
}
}
},
streamSettings = new Models.Information.VMess.StreamSettings()
streamSettings = new Models.Information.VMess.StreamSettings
{
network = server.TransferProtocol,
security = server.TLSSecure == true ? "tls" : "",
wsSettings = server.TransferProtocol == "ws" ? new Models.Information.VMess.WebSocketSettings()
security = server.TLSSecure ? "tls" : "",
wsSettings = server.TransferProtocol == "ws" ? new Models.Information.VMess.WebSocketSettings
{
path = server.Path == "" ? "/" : server.Path,
headers = new Models.Information.VMess.WSHeaders()
headers = new Models.Information.VMess.WSHeaders
{
Host = server.Host == "" ? server.Hostname : server.Host
}
} : null,
tcpSettings = server.FakeType == "http" ? new Models.Information.VMess.TCPSettings()
tcpSettings = server.FakeType == "http" ? new Models.Information.VMess.TCPSettings
{
header = new Models.Information.VMess.TCPHeaders()
header = new Models.Information.VMess.TCPHeaders
{
type = server.FakeType,
request = new Models.Information.VMess.TCPRequest()
request = new Models.Information.VMess.TCPRequest
{
path = server.Path == "" ? "/" : server.Path,
headers = new Models.Information.VMess.TCPRequestHeaders()
headers = new Models.Information.VMess.TCPRequestHeaders
{
Host = server.Host == "" ? server.Hostname : server.Host
}
}
}
} : null,
kcpSettings = server.TransferProtocol == "kcp" ? new Models.Information.VMess.KCPSettings()
kcpSettings = server.TransferProtocol == "kcp" ? new Models.Information.VMess.KCPSettings
{
header = new Models.Information.VMess.TCPHeaders()
header = new Models.Information.VMess.TCPHeaders
{
type = server.FakeType
}
} : null,
quicSettings = server.TransferProtocol == "quic" ? new Models.Information.VMess.QUICSettings()
quicSettings = server.TransferProtocol == "quic" ? new Models.Information.VMess.QUICSettings
{
security = server.QUICSecure,
key = server.QUICSecret,
header = new Models.Information.VMess.TCPHeaders()
header = new Models.Information.VMess.TCPHeaders
{
type = server.FakeType
}
} : null,
httpSettings = server.TransferProtocol == "h2" ? new Models.Information.VMess.HTTPSettings()
httpSettings = server.TransferProtocol == "h2" ? new Models.Information.VMess.HTTPSettings
{
host = server.Host == "" ? server.Hostname : server.Host,
path = server.Path == "" ? "/" : server.Path
} : null,
tlsSettings = new Models.Information.VMess.TLSSettings()
tlsSettings = new Models.Information.VMess.TLSSettings
{
allowInsecure = true,
serverName = server.Host == "" ? server.Hostname : server.Host
}
},
mux = new Models.Information.VMess.OutboundMux()
mux = new Models.Information.VMess.OutboundMux
{
enabled = server.UseMux
}
},
new Models.Information.VMess.Outbounds()
new Models.Information.VMess.Outbounds
{
tag = "direct",
protocol = "freedom",
@@ -134,11 +134,11 @@ namespace Netch.Controllers
mux = null
}
},
routing = new Models.Information.VMess.Routing()
routing = new Models.Information.VMess.Routing
{
rules = new List<Models.Information.VMess.RoutingRules>()
rules = new List<Models.Information.VMess.RoutingRules>
{
mode.BypassChina == true ? new Models.Information.VMess.RoutingRules()
mode.BypassChina ? new Models.Information.VMess.RoutingRules
{
type = "field",
ip = new List<string>
@@ -152,7 +152,7 @@ namespace Netch.Controllers
"geosite:cn"
},
outboundTag = "direct"
} : new Models.Information.VMess.RoutingRules()
} : new Models.Information.VMess.RoutingRules
{
type = "field",
ip = new List<string>
@@ -185,7 +185,7 @@ namespace Netch.Controllers
Instance.Start();
Instance.BeginOutputReadLine();
Instance.BeginErrorReadLine();
for (int i = 0; i < 1000; i++)
for (var i = 0; i < 1000; i++)
{
Thread.Sleep(10);
@@ -232,7 +232,7 @@ namespace Netch.Controllers
public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrWhiteSpace(e.Data))
if (!string.IsNullOrWhiteSpace(e.Data))
{
File.AppendAllText("logging\\v2ray.log", $"{e.Data}\r\n");