From 1e92b30fe5f30808a39c1696d9f2d5493000b46e Mon Sep 17 00:00:00 2001 From: Student Main Date: Sat, 18 Apr 2020 12:12:47 +0800 Subject: [PATCH] add from url without another instance also with some i18n update --- .../Controller/ShadowsocksController.cs | 10 +++ shadowsocks-csharp/Program.cs | 81 +++++++++++-------- shadowsocks-csharp/View/MenuViewController.cs | 4 +- 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index f4ea1294..ccf08858 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -218,6 +218,16 @@ namespace Shadowsocks.Controller StatisticsStrategyConfiguration.Save(configuration); } + public void AskAddServerBySSURL(string ssURL) + { + var dr = MessageBox.Show(I18N.GetString("Open url: {0} ?", ssURL), I18N.GetString("Shadowsocks"), MessageBoxButtons.YesNo); + if (dr == DialogResult.Yes) + { + AddServerBySSURL(ssURL); + } + } + + public bool AddServerBySSURL(string ssURL) { try diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 10c6d9fb..653ee448 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -1,27 +1,26 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Threading; -using System.Windows.Forms; +using Microsoft.Win32; using NLog; -using Microsoft.Win32; - using Shadowsocks.Controller; using Shadowsocks.Controller.Hotkeys; using Shadowsocks.Util; using Shadowsocks.View; -using System.Linq; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.IO.Pipes; -using System.Text; +using System.Linq; using System.Net; +using System.Text; +using System.Threading; using System.Threading.Tasks; -using System.Collections.Generic; +using System.Windows.Forms; namespace Shadowsocks { - static class Program + internal static class Program { - private static Logger logger = LogManager.GetCurrentClassLogger(); + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); public static ShadowsocksController MainController { get; private set; } public static MenuViewController MenuController { get; private set; } public static string[] Args { get; private set; } @@ -30,15 +29,15 @@ namespace Shadowsocks /// /// [STAThread] - static void Main(string[] args) + private static void Main(string[] args) { Directory.SetCurrentDirectory(Application.StartupPath); // todo: initialize the NLog configuartion Model.NLogConfig.TouchAndApplyNLogConfig(); // .NET Framework 4.7.2 on Win7 compatibility - System.Net.ServicePointManager.SecurityProtocol |= - System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12; + ServicePointManager.SecurityProtocol |= + SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // store args for further use Args = args; @@ -56,13 +55,14 @@ namespace Shadowsocks if (DialogResult.OK == MessageBox.Show(I18N.GetString("Unsupported .NET Framework, please update to {0} or later.", "4.7.2"), "Shadowsocks Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)) { - //Process.Start("https://www.microsoft.com/download/details.aspx?id=53344"); // 4.6.2 Process.Start("https://dotnet.microsoft.com/download/dotnet-framework/net472"); } return; } string pipename = $"Shadowsocks\\{Application.StartupPath.GetHashCode()}"; + string addedUrl = null; + using (NamedPipeClientStream pipe = new NamedPipeClientStream(pipename)) { bool pipeExist = false; @@ -76,22 +76,36 @@ namespace Shadowsocks pipeExist = false; } - var alist = Args.ToList(); + // TODO: switch to better argv parser when it's getting complicate + List alist = Args.ToList(); + // check --open-url param int urlidx = alist.IndexOf("--open-url") + 1; if (urlidx > 0) { - if (Args.Length <= urlidx) return; - if (!pipeExist) return; + if (Args.Length <= urlidx) + { + return; + } - byte[] b = Encoding.UTF8.GetBytes(Args[urlidx]); - byte[] opAddUrl = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1)); - byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length)); - pipe.Write(opAddUrl, 0, 4); // opcode addurl - pipe.Write(blen, 0, 4); - pipe.Write(b, 0, b.Length); - pipe.Close(); - return; + // --open-url exist, and no other instance, add it later + if (!pipeExist) + { + addedUrl = Args[urlidx]; + } + // has other instance, send url via pipe then exit + else + { + byte[] b = Encoding.UTF8.GetBytes(Args[urlidx]); + byte[] opAddUrl = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1)); + byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length)); + pipe.Write(opAddUrl, 0, 4); // opcode addurl + pipe.Write(blen, 0, 4); + pipe.Write(b, 0, b.Length); + pipe.Close(); + return; + } } + // has another instance, and no need to communicate with it return else if (pipeExist) { Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks"); @@ -136,14 +150,11 @@ namespace Shadowsocks PipeServer pipeServer = new PipeServer(); Task.Run(() => pipeServer.Run(pipename)); - pipeServer.AddUrlRequested += (_1, e) => + pipeServer.AddUrlRequested += (_1, e) => MainController.AskAddServerBySSURL(e.Url); + if (!addedUrl.IsNullOrEmpty()) { - var dr = MessageBox.Show($"Open url: {e.Url} ?", "Shadowsocks", MessageBoxButtons.YesNo); - if (dr == DialogResult.Yes) - { - MainController.AddServerBySSURL(e.Url); - } - }; + MainController.AskAddServerBySSURL(addedUrl); + } Application.Run(); } @@ -183,7 +194,7 @@ namespace Shadowsocks logger.Info("os wake up"); if (MainController != null) { - System.Threading.Tasks.Task.Factory.StartNew(() => + Task.Factory.StartNew(() => { Thread.Sleep(10 * 1000); try diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index 72147693..5014d392 100644 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -316,7 +316,7 @@ namespace Shadowsocks.View this.proxyItem = CreateMenuItem("Forward Proxy...", new EventHandler(this.proxyItem_Click)), new MenuItem("-"), this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)), - this.ProtocolHandlerItem = CreateMenuItem("Start on Boot", new EventHandler(this.ProtocolHandlerItem_Click)), + this.ProtocolHandlerItem = CreateMenuItem("Register ss:// protocol", new EventHandler(this.ProtocolHandlerItem_Click)), this.ShareOverLANItem = CreateMenuItem("Allow other Devices to connect", new EventHandler(this.ShareOverLANItem_Click)), new MenuItem("-"), this.hotKeyItem = CreateMenuItem("Edit Hotkeys...", new EventHandler(this.hotKeyItem_Click)), @@ -855,7 +855,7 @@ namespace Shadowsocks.View ProtocolHandlerItem.Checked = !ProtocolHandlerItem.Checked; if (!ProtocolHandler.Set(ProtocolHandlerItem.Checked)) { - MessageBox.Show(I18N.GetString("Failed to update registry")); + MessageBox.Show(I18N.GetString("Failed to update registry, try run as Admin")); } LoadCurrentConfiguration(); }