From 1b373fd145ceda8dc82fe66015d111c0c4ee77ce Mon Sep 17 00:00:00 2001 From: celeron533 Date: Sun, 19 Aug 2018 23:42:26 +0800 Subject: [PATCH] Fix #1969 #1818 Cause: process's additional environment variables are not expanded by correctly. Innovated by studentmain (StudentEx ) --- .../Controller/Service/Sip003Plugin.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs b/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs index 131f0923..0618d3d6 100644 --- a/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs +++ b/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Net; @@ -92,6 +93,7 @@ namespace Shadowsocks.Controller.Service _pluginProcess.StartInfo.Environment["SS_LOCAL_HOST"] = LocalEndPoint.Address.ToString(); _pluginProcess.StartInfo.Environment["SS_LOCAL_PORT"] = LocalEndPoint.Port.ToString(); + _pluginProcess.StartInfo.Arguments = ExpandEnvironmentVariables(_pluginProcess.StartInfo.Arguments, _pluginProcess.StartInfo.EnvironmentVariables); _pluginProcess.Start(); _pluginJob.AddProcess(_pluginProcess.Handle); _started = true; @@ -100,6 +102,21 @@ namespace Shadowsocks.Controller.Service return true; } + public string ExpandEnvironmentVariables(string name, StringDictionary environmentVariables = null) + { + // Expand the environment variables from the new process itself + if (environmentVariables != null) + { + foreach(string key in environmentVariables.Keys) + { + name = name.Replace($"%{key}%", environmentVariables[key], StringComparison.OrdinalIgnoreCase); + } + } + // Also expand the environment variables from current main process (system) + name = Environment.ExpandEnvironmentVariables(name); + return name; + } + static int GetNextFreeTcpPort() { var l = new TcpListener(IPAddress.Loopback, 0);