From 8140fba21e9de10c50bbdb511a3b61c263c4ecf1 Mon Sep 17 00:00:00 2001 From: Amazing_DM Date: Tue, 5 May 2020 09:27:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=96=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Controllers/SSController.cs | 39 +++++++++++++++++++++++++++-- Netch/Controllers/SSRController.cs | 3 +++ Netch/Forms/SettingForm.Designer.cs | 29 +++++++++++++++------ Netch/Forms/SettingForm.cs | 5 +++- Netch/Models/Setting.cs | 4 +++ Netch/NativeMethods.cs | 10 ++++++++ Netch/Resources/zh-CN | 1 + binaries | 2 +- 8 files changed, 81 insertions(+), 12 deletions(-) diff --git a/Netch/Controllers/SSController.cs b/Netch/Controllers/SSController.cs index 3d2faa93..2358eb4f 100644 --- a/Netch/Controllers/SSController.cs +++ b/Netch/Controllers/SSController.cs @@ -1,7 +1,10 @@ using Netch.Forms; +using Netch.Utils; using System; using System.Diagnostics; using System.IO; +using System.Runtime.InteropServices; +using System.Text; using System.Threading; namespace Netch.Controllers @@ -27,11 +30,41 @@ namespace Netch.Controllers public bool Start(Models.Server server, Models.Mode mode) { MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting Shadowsocks")}"); + + File.Delete("logging\\shadowsocks.log"); + //从DLL启动Shaowsocks + if (Global.Settings.BootShadowsocksFromDLL) + { + State = Models.State.Starting; + var client = Encoding.UTF8.GetBytes($"0.0.0.0:{Global.Settings.Socks5LocalPort}"); + var remote = Encoding.UTF8.GetBytes($"{server.Hostname}:{server.Port}"); + var passwd = Encoding.UTF8.GetBytes($"{server.Password}"); + var method = Encoding.UTF8.GetBytes($"{server.EncryptMethod}"); + if (!NativeMethods.Shadowsocks.Info(client, remote, passwd, method)) + { + State = Models.State.Stopped; + Logging.Info("DllSS_Info设置失败!"); + return false; + } + + Logging.Info("DllSS_Info设置成功!"); + + if (!NativeMethods.Shadowsocks.Start()) + { + State = Models.State.Stopped; + Logging.Info("DllSS_Start 启动失败!"); + return false; + } + + Logging.Info("DllSS_Start 启动成功!"); + State = Models.State.Started; + return true; + } + if (!File.Exists("bin\\Shadowsocks.exe")) { return false; } - Instance = MainController.GetProcess(); Instance.StartInfo.FileName = "bin\\Shadowsocks.exe"; @@ -90,6 +123,8 @@ namespace Netch.Controllers { Instance.Kill(); } + if (Global.Settings.BootShadowsocksFromDLL) + NativeMethods.Shadowsocks.Stop(); } catch (Exception e) { @@ -101,7 +136,7 @@ namespace Netch.Controllers { if (!string.IsNullOrWhiteSpace(e.Data)) { - // File.AppendAllText("logging\\shadowsocks.log", $"{e.Data}\r\n"); + File.AppendAllText("logging\\shadowsocks.log", $"{e.Data}\r\n"); if (State == Models.State.Starting) { diff --git a/Netch/Controllers/SSRController.cs b/Netch/Controllers/SSRController.cs index 32e748f0..c4daf2a1 100644 --- a/Netch/Controllers/SSRController.cs +++ b/Netch/Controllers/SSRController.cs @@ -27,6 +27,9 @@ namespace Netch.Controllers public bool Start(Models.Server server, Models.Mode mode) { MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting ShadowsocksR")}"); + + File.Delete("logging\\shadowsocksr.log"); + if (!File.Exists("bin\\ShadowsocksR.exe")) { return false; diff --git a/Netch/Forms/SettingForm.Designer.cs b/Netch/Forms/SettingForm.Designer.cs index a5acbda3..99e27d25 100644 --- a/Netch/Forms/SettingForm.Designer.cs +++ b/Netch/Forms/SettingForm.Designer.cs @@ -52,6 +52,7 @@ this.ControlButton = new System.Windows.Forms.Button(); this.GlobalBypassIPsButton = new System.Windows.Forms.Button(); this.BehaviorGroupBox = new System.Windows.Forms.GroupBox(); + this.BootShadowsocksFromDLLCheckBox = new System.Windows.Forms.CheckBox(); this.AclAddr = new System.Windows.Forms.TextBox(); this.AclLabel = new System.Windows.Forms.Label(); this.DetectionInterval_Label = new System.Windows.Forms.Label(); @@ -278,7 +279,7 @@ // ControlButton // this.ControlButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ControlButton.Location = new System.Drawing.Point(781, 356); + this.ControlButton.Location = new System.Drawing.Point(804, 356); this.ControlButton.Name = "ControlButton"; this.ControlButton.Size = new System.Drawing.Size(75, 23); this.ControlButton.TabIndex = 11; @@ -299,6 +300,7 @@ // // BehaviorGroupBox // + this.BehaviorGroupBox.Controls.Add(this.BootShadowsocksFromDLLCheckBox); this.BehaviorGroupBox.Controls.Add(this.AclAddr); this.BehaviorGroupBox.Controls.Add(this.AclLabel); this.BehaviorGroupBox.Controls.Add(this.DetectionInterval_Label); @@ -322,16 +324,26 @@ this.BehaviorGroupBox.Controls.Add(this.ExitWhenClosedCheckBox); this.BehaviorGroupBox.Location = new System.Drawing.Point(438, 12); this.BehaviorGroupBox.Name = "BehaviorGroupBox"; - this.BehaviorGroupBox.Size = new System.Drawing.Size(420, 333); + this.BehaviorGroupBox.Size = new System.Drawing.Size(441, 333); this.BehaviorGroupBox.TabIndex = 8; this.BehaviorGroupBox.TabStop = false; this.BehaviorGroupBox.Text = "Behavior"; // + // BootShadowsocksFromDLLCheckBox + // + this.BootShadowsocksFromDLLCheckBox.AutoSize = true; + this.BootShadowsocksFromDLLCheckBox.Location = new System.Drawing.Point(206, 102); + this.BootShadowsocksFromDLLCheckBox.Name = "BootShadowsocksFromDLLCheckBox"; + this.BootShadowsocksFromDLLCheckBox.Size = new System.Drawing.Size(195, 21); + this.BootShadowsocksFromDLLCheckBox.TabIndex = 21; + this.BootShadowsocksFromDLLCheckBox.Text = "Boot Shadowsocks from DLL"; + this.BootShadowsocksFromDLLCheckBox.UseVisualStyleBackColor = true; + // // AclAddr // this.AclAddr.Location = new System.Drawing.Point(120, 258); this.AclAddr.Name = "AclAddr"; - this.AclAddr.Size = new System.Drawing.Size(294, 23); + this.AclAddr.Size = new System.Drawing.Size(315, 23); this.AclAddr.TabIndex = 19; this.AclAddr.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // @@ -357,7 +369,7 @@ // this.DetectionInterval_TextBox.Location = new System.Drawing.Point(367, 174); this.DetectionInterval_TextBox.Name = "DetectionInterval_TextBox"; - this.DetectionInterval_TextBox.Size = new System.Drawing.Size(47, 23); + this.DetectionInterval_TextBox.Size = new System.Drawing.Size(68, 23); this.DetectionInterval_TextBox.TabIndex = 17; this.DetectionInterval_TextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // @@ -414,7 +426,7 @@ // this.STUN_ServerPortTextBox.Location = new System.Drawing.Point(120, 229); this.STUN_ServerPortTextBox.Name = "STUN_ServerPortTextBox"; - this.STUN_ServerPortTextBox.Size = new System.Drawing.Size(294, 23); + this.STUN_ServerPortTextBox.Size = new System.Drawing.Size(315, 23); this.STUN_ServerPortTextBox.TabIndex = 8; this.STUN_ServerPortTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // @@ -450,7 +462,7 @@ // this.STUN_ServerTextBox.Location = new System.Drawing.Point(120, 200); this.STUN_ServerTextBox.Name = "STUN_ServerTextBox"; - this.STUN_ServerTextBox.Size = new System.Drawing.Size(294, 23); + this.STUN_ServerTextBox.Size = new System.Drawing.Size(315, 23); this.STUN_ServerTextBox.TabIndex = 11; this.STUN_ServerTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // @@ -477,7 +489,7 @@ // this.ProfileCount_TextBox.Location = new System.Drawing.Point(209, 149); this.ProfileCount_TextBox.Name = "ProfileCount_TextBox"; - this.ProfileCount_TextBox.Size = new System.Drawing.Size(208, 23); + this.ProfileCount_TextBox.Size = new System.Drawing.Size(226, 23); this.ProfileCount_TextBox.TabIndex = 9; this.ProfileCount_TextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // @@ -529,7 +541,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; - this.ClientSize = new System.Drawing.Size(868, 390); + this.ClientSize = new System.Drawing.Size(891, 390); this.Controls.Add(this.BehaviorGroupBox); this.Controls.Add(this.PortGroupBox); this.Controls.Add(this.GlobalBypassIPsButton); @@ -601,5 +613,6 @@ private System.Windows.Forms.TextBox AclAddr; private System.Windows.Forms.Label AclLabel; private System.Windows.Forms.CheckBox UseFakeDNSCheckBox; + private System.Windows.Forms.CheckBox BootShadowsocksFromDLLCheckBox; } } \ No newline at end of file diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 5fa4a130..6f58a798 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -56,6 +56,7 @@ namespace Netch.Forms GlobalBypassIPsButton.Text = Utils.i18N.Translate(GlobalBypassIPsButton.Text); ControlButton.Text = Utils.i18N.Translate(ControlButton.Text); BypassModeCheckBox.Text = Utils.i18N.Translate(BypassModeCheckBox.Text); + BootShadowsocksFromDLLCheckBox.Text = Utils.i18N.Translate(BootShadowsocksFromDLLCheckBox.Text); ExitWhenClosedCheckBox.Checked = Global.Settings.ExitWhenClosed; StopWhenExitedCheckBox.Checked = Global.Settings.StopWhenExited; @@ -67,6 +68,7 @@ namespace Netch.Forms BypassModeCheckBox.Checked = Global.Settings.ProcessBypassMode; EnableStartedTcping_CheckBox.Checked = Global.Settings.StartedTcping; DetectionInterval_TextBox.Text = Global.Settings.StartedTcping_Interval.ToString(); + BootShadowsocksFromDLLCheckBox.Checked = Global.Settings.BootShadowsocksFromDLL; Socks5PortTextBox.Text = Global.Settings.Socks5LocalPort.ToString(); HTTPPortTextBox.Text = Global.Settings.HTTPLocalPort.ToString(); @@ -161,7 +163,8 @@ namespace Netch.Forms Global.Settings.RunAtStartup = RunAtStartup.Checked; Global.Settings.UseRedirector2 = Redirector2checkBox.Checked; Global.Settings.ProcessBypassMode = BypassModeCheckBox.Checked; - Global.Settings.StartedTcping = EnableStartedTcping_CheckBox.Checked; + Global.Settings.BootShadowsocksFromDLL = BootShadowsocksFromDLLCheckBox.Checked; + // 开机自启判断 TaskSchedulerClass scheduler = new TaskSchedulerClass(); diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index c83184ab..7a3f1a24 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -184,5 +184,9 @@ namespace Netch.Models /// public string ACL = "https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/banAD.acl"; + /// + /// 是否使用DLL启动Shadowsocks + /// + public bool BootShadowsocksFromDLL = true; } } diff --git a/Netch/NativeMethods.cs b/Netch/NativeMethods.cs index 0fbfd77d..e754fde9 100644 --- a/Netch/NativeMethods.cs +++ b/Netch/NativeMethods.cs @@ -63,5 +63,15 @@ namespace Netch /// 是否成功 [DllImport("bin\\sysproxy", CallingConvention = CallingConvention.Cdecl)] public static extern bool SetURL([MarshalAs(UnmanagedType.LPTStr)] string remote); + + public class Shadowsocks + { + [DllImport("bin\\shadowsocks-windows-dynamic", CallingConvention = CallingConvention.Cdecl)] + public static extern bool Info(byte[] client, byte[] remote, byte[] passwd, byte[] method); + [DllImport("bin\\shadowsocks-windows-dynamic", CallingConvention = CallingConvention.Cdecl)] + public static extern bool Start(); + [DllImport("bin\\shadowsocks-windows-dynamic", CallingConvention = CallingConvention.Cdecl)] + public static extern void Stop(); + } } } diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index 74495f7f..bdf0f006 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -173,6 +173,7 @@ "Detection interval(/s)": "检测间隔(/秒)", "Custom ACL": "自定义 ACL 规则", "Process whitelist mode": "进程白名单模式", + "Boot Shadowsocks from DLL": "从DLL启动SS(快速启动,推荐使用)", "Profile": "配置名", "Profiles": "配置", diff --git a/binaries b/binaries index b51fd1ce..52ba0a29 160000 --- a/binaries +++ b/binaries @@ -1 +1 @@ -Subproject commit b51fd1cefbc37932c6610432f54e24af8b135f4d +Subproject commit 52ba0a29eafc603cd9708af43efb40410138dfdc