Browse Source

pass received url to controller

tags/4.2.0.0
Student Main database64128 5 years ago
parent
commit
e492bc02f4
2 changed files with 30 additions and 6 deletions
  1. +23
    -4
      shadowsocks-csharp/Controller/Service/PipeServer.cs
  2. +7
    -2
      shadowsocks-csharp/Program.cs

+ 23
- 4
shadowsocks-csharp/Controller/Service/PipeServer.cs View File

@@ -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<RequestAddUrlEventArgs> 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();
}
}


+ 7
- 2
shadowsocks-csharp/Program.cs View File

@@ -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();
}


Loading…
Cancel
Save