Browse Source

named pipe for url ipc

tags/4.2.0.0
Student Main database64128 5 years ago
parent
commit
5846c9dc93
3 changed files with 92 additions and 26 deletions
  1. +29
    -0
      shadowsocks-csharp/Controller/Service/PipeServer.cs
  2. +62
    -26
      shadowsocks-csharp/Program.cs
  3. +1
    -0
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 29
- 0
shadowsocks-csharp/Controller/Service/PipeServer.cs View File

@@ -0,0 +1,29 @@
using System;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Text;

namespace Shadowsocks.Controller
{
internal class PipeServer
{
public async void Run(string path)
{
byte[] buf = new byte[4096];
while (true)
{
using (NamedPipeServerStream stream = new NamedPipeServerStream(path))
{
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);
stream.Close();
}
}
}
}
}

+ 62
- 26
shadowsocks-csharp/Program.cs View File

@@ -10,6 +10,12 @@ using Shadowsocks.Controller;
using Shadowsocks.Controller.Hotkeys;
using Shadowsocks.Util;
using Shadowsocks.View;
using System.Linq;
using System.IO.Pipes;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Shadowsocks
{
@@ -55,22 +61,36 @@ namespace Shadowsocks
}
return;
}
string pipename = $"Shadowsocks\\{Application.StartupPath.GetHashCode()}";
Utils.ReleaseMemory(true);
using (Mutex mutex = new Mutex(false, $"Global\\Shadowsocks_{Application.StartupPath.GetHashCode()}"))
using (NamedPipeClientStream pipe = new NamedPipeClientStream(pipename))
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// handle UI exceptions
Application.ThreadException += Application_ThreadException;
// handle non-UI exceptions
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ApplicationExit += Application_ApplicationExit;
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AutoStartup.RegisterForRestart(true);
if (!mutex.WaitOne(0, false))
bool pipeExist = false;
try
{
pipe.Connect(10);
pipeExist = true;
}
catch (TimeoutException)
{
pipeExist = false;
}

var alist = Args.ToList();
int urlidx = alist.IndexOf("--open-url") + 1;
if (urlidx > 0)
{
if (Args.Length <= urlidx) return;
if (!pipeExist) return;

byte[] b = Encoding.UTF8.GetBytes(Args[urlidx]);
byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length));
pipe.Write(blen, 0, 4);
pipe.Write(b, 0, b.Length);
pipe.Close();
return;
}
else if (pipeExist)
{
Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks");
if (oldProcesses.Length > 0)
@@ -83,21 +103,37 @@ namespace Shadowsocks
I18N.GetString("Shadowsocks is already running."));
return;
}
Directory.SetCurrentDirectory(Application.StartupPath);
}

Task.Run(() => new PipeServer().Run(pipename));

Utils.ReleaseMemory(true);

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// handle UI exceptions
Application.ThreadException += Application_ThreadException;
// handle non-UI exceptions
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ApplicationExit += Application_ApplicationExit;
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AutoStartup.RegisterForRestart(true);

Directory.SetCurrentDirectory(Application.StartupPath);

#if DEBUG
// truncate privoxy log file while debugging
string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
if (File.Exists(privoxyLogFilename))
using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
// truncate privoxy log file while debugging
string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
if (File.Exists(privoxyLogFilename))
using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
#endif
MainController = new ShadowsocksController();
MenuController = new MenuViewController(MainController);
MainController = new ShadowsocksController();
MenuController = new MenuViewController(MainController);
HotKeys.Init(MainController);
MainController.Start();
Application.Run();
}
HotKeys.Init(MainController);
MainController.Start();
Application.Run();
}
private static int exited = 0;


+ 1
- 0
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -113,6 +113,7 @@
<Compile Include="Controller\HotkeyReg.cs" />
<Compile Include="Controller\LoggerExtension.cs" />
<Compile Include="Controller\Service\PACDaemon.cs" />
<Compile Include="Controller\Service\PipeServer.cs" />
<Compile Include="Controller\System\Hotkeys\HotkeyCallbacks.cs" />
<Compile Include="Encryption\AEAD\AEADEncryptor.cs" />
<Compile Include="Encryption\AEAD\AEADMbedTLSEncryptor.cs" />


Loading…
Cancel
Save