@@ -6,8 +6,20 @@ using System.Text; | |||||
namespace Shadowsocks.Controller | namespace Shadowsocks.Controller | ||||
{ | { | ||||
class RequestAddUrlEventArgs : EventArgs | |||||
{ | |||||
public readonly string Url; | |||||
public RequestAddUrlEventArgs(string url) | |||||
{ | |||||
this.Url = url; | |||||
} | |||||
} | |||||
internal class PipeServer | internal class PipeServer | ||||
{ | { | ||||
public event EventHandler<RequestAddUrlEventArgs> AddUrlRequested; | |||||
public async void Run(string path) | public async void Run(string path) | ||||
{ | { | ||||
byte[] buf = new byte[4096]; | byte[] buf = new byte[4096]; | ||||
@@ -17,10 +29,17 @@ namespace Shadowsocks.Controller | |||||
{ | { | ||||
stream.WaitForConnection(); | stream.WaitForConnection(); | ||||
await stream.ReadAsync(buf, 0, 4); | 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(); | stream.Close(); | ||||
} | } | ||||
} | } | ||||
@@ -84,7 +84,9 @@ namespace Shadowsocks | |||||
if (!pipeExist) return; | if (!pipeExist) return; | ||||
byte[] b = Encoding.UTF8.GetBytes(Args[urlidx]); | byte[] b = Encoding.UTF8.GetBytes(Args[urlidx]); | ||||
byte[] opAddUrl = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1)); | |||||
byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length)); | byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length)); | ||||
pipe.Write(opAddUrl, 0, 4); // opcode addurl | |||||
pipe.Write(blen, 0, 4); | pipe.Write(blen, 0, 4); | ||||
pipe.Write(b, 0, b.Length); | pipe.Write(b, 0, b.Length); | ||||
pipe.Close(); | pipe.Close(); | ||||
@@ -105,8 +107,6 @@ namespace Shadowsocks | |||||
} | } | ||||
} | } | ||||
Task.Run(() => new PipeServer().Run(pipename)); | |||||
Utils.ReleaseMemory(true); | Utils.ReleaseMemory(true); | ||||
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); | Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); | ||||
@@ -133,6 +133,11 @@ namespace Shadowsocks | |||||
HotKeys.Init(MainController); | HotKeys.Init(MainController); | ||||
MainController.Start(); | MainController.Start(); | ||||
PipeServer pipeServer = new PipeServer(); | |||||
Task.Run(() => pipeServer.Run(pipename)); | |||||
pipeServer.AddUrlRequested += (_1, e) => MainController.AddServerBySSURL(e.Url); | |||||
Application.Run(); | Application.Run(); | ||||
} | } | ||||