Skip to content

Commit

Permalink
Merge pull request #1977 from celeron533/EnvironmentVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 authored Aug 20, 2018
2 parents 465355a + 1b373fd commit 14559b5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions shadowsocks-csharp/Controller/Service/Sip003Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -92,6 +93,7 @@ public bool StartIfNeeded()

_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;
Expand All @@ -100,6 +102,21 @@ public bool StartIfNeeded()
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);
Expand Down

0 comments on commit 14559b5

Please sign in to comment.