Browse Source

Merge pull request #1977 from celeron533/EnvironmentVariables

Fix #1969 #1818
tags/4.1.2
Allen Zhu GitHub 6 years ago
parent
commit
14559b5ef6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions
  1. +17
    -0
      shadowsocks-csharp/Controller/Service/Sip003Plugin.cs

+ 17
- 0
shadowsocks-csharp/Controller/Service/Sip003Plugin.cs View File

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


Loading…
Cancel
Save