From e492bc02f482ca96fafff154d1725cbcb1816d24 Mon Sep 17 00:00:00 2001 From: Student Main Date: Sat, 18 Apr 2020 01:50:57 +0800 Subject: [PATCH] pass received url to controller --- .../Controller/Service/PipeServer.cs | 27 ++++++++++++++++--- shadowsocks-csharp/Program.cs | 9 +++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/PipeServer.cs b/shadowsocks-csharp/Controller/Service/PipeServer.cs index fb2e1e0d..fd535b48 100644 --- a/shadowsocks-csharp/Controller/Service/PipeServer.cs +++ b/shadowsocks-csharp/Controller/Service/PipeServer.cs @@ -6,8 +6,20 @@ using System.Text; namespace Shadowsocks.Controller { + class RequestAddUrlEventArgs : EventArgs + { + public readonly string Url; + + public RequestAddUrlEventArgs(string url) + { + this.Url = url; + } + } + + internal class PipeServer { + public event EventHandler AddUrlRequested; public async void Run(string path) { byte[] buf = new byte[4096]; @@ -17,10 +29,17 @@ namespace Shadowsocks.Controller { stream.WaitForConnection(); await stream.ReadAsync(buf, 0, 4); - int strlen = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buf, 0)); - await stream.ReadAsync(buf, 0, strlen); - string url = Encoding.UTF8.GetString(buf, 0, strlen); - Console.WriteLine(url); + int opcode = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buf, 0)); + if (opcode == 1) + { + await stream.ReadAsync(buf, 0, 4); + int strlen = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buf, 0)); + + await stream.ReadAsync(buf, 0, strlen); + string url = Encoding.UTF8.GetString(buf, 0, strlen); + + AddUrlRequested?.Invoke(this, new RequestAddUrlEventArgs(url)); + } stream.Close(); } } diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 8fe8646a..cc4c1ea5 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -84,7 +84,9 @@ namespace Shadowsocks if (!pipeExist) 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(); @@ -105,8 +107,6 @@ namespace Shadowsocks } } - Task.Run(() => new PipeServer().Run(pipename)); - Utils.ReleaseMemory(true); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); @@ -133,6 +133,11 @@ namespace Shadowsocks HotKeys.Init(MainController); MainController.Start(); + + PipeServer pipeServer = new PipeServer(); + Task.Run(() => pipeServer.Run(pipename)); + pipeServer.AddUrlRequested += (_1, e) => MainController.AddServerBySSURL(e.Url); + Application.Run(); }