diff --git a/v2rayN/v2rayN/Forms/MainForm.cs b/v2rayN/v2rayN/Forms/MainForm.cs index 11160913..e07386bf 100644 --- a/v2rayN/v2rayN/Forms/MainForm.cs +++ b/v2rayN/v2rayN/Forms/MainForm.cs @@ -30,6 +30,7 @@ namespace v2rayN.Forms this.WindowState = FormWindowState.Minimized; HideForm(); this.Text = Utils.GetVersion(); + Global.processJob = new Job(); Application.ApplicationExit += (sender, args) => { diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 3cdfadea..5953ef64 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -161,7 +161,7 @@ namespace v2rayN slow = 3000 } public const string StatisticLogOverall = "StatisticLogOverall.json"; - + public const string IEProxyExceptions = ";localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*"; #endregion @@ -216,6 +216,11 @@ namespace v2rayN get; set; } + public static Job processJob + { + get; set; + } + #endregion diff --git a/v2rayN/v2rayN/Handler/V2rayHandler.cs b/v2rayN/v2rayN/Handler/V2rayHandler.cs index a30f28df..aacaa51f 100644 --- a/v2rayN/v2rayN/Handler/V2rayHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayHandler.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Text; using v2rayN.Mode; namespace v2rayN.Handler @@ -147,6 +148,7 @@ namespace v2rayN.Handler Process p = new Process(); p.StartInfo.FileName = fileName; + p.StartInfo.WorkingDirectory = Utils.StartupPath(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; @@ -162,6 +164,8 @@ namespace v2rayN.Handler p.Start(); p.BeginOutputReadLine(); processId = p.Id; + + Global.processJob.AddProcess(p.Handle); } catch (Exception ex) { diff --git a/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs b/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs index e87cfcf4..a2e4e486 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs @@ -77,7 +77,7 @@ namespace v2rayN.HttpProxyHandler int localPort = config.GetLocalPort(Global.InboundSocks); if (localPort > 0) { - PrivoxyHandler.Instance.Start(localPort, config); + PrivoxyHandler.Instance.Restart(localPort, config); if (PrivoxyHandler.Instance.RunningPort > 0) { Global.sysAgent = true; diff --git a/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs b/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs index baa9fa94..2fa34824 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs @@ -22,18 +22,17 @@ namespace v2rayN.HttpProxyHandler private static int _uid; private static string _uniqueConfigFile; - private static Job _privoxyJob; private Process _process; + private static string _privoxyName = "v2ray_privoxy"; static PrivoxyHandler() { try { - _uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance. + _uid = Application.StartupPath.GetHashCode(); _uniqueConfigFile = string.Format("privoxy_{0}.conf", _uid); - _privoxyJob = new Job(); - FileManager.UncompressFile(Utils.GetTempPath("v2ray_privoxy.exe"), Resources.privoxy_exe); + FileManager.UncompressFile(Utils.GetTempPath($"{_privoxyName}.exe"), Resources.privoxy_exe); } catch (IOException ex) { @@ -41,11 +40,6 @@ namespace v2rayN.HttpProxyHandler } } - private PrivoxyHandler() - { - - } - /// /// 单例 /// @@ -66,17 +60,20 @@ namespace v2rayN.HttpProxyHandler get; set; } + public void Restart(int localPort, Config config) + { + Stop(); + Start(localPort, config); + } + + public void Start(int localPort, Config config) { try { if (_process == null) { - Process[] existingPrivoxy = Process.GetProcessesByName("v2ray_privoxy"); - foreach (Process p in existingPrivoxy.Where(IsChildProcess)) - { - KillProcess(p); - } + string privoxyConfig = Resources.privoxy_conf; RunningPort = config.GetLocalPort(Global.InboundHttp); privoxyConfig = privoxyConfig.Replace("__SOCKS_PORT__", localPort.ToString()); @@ -96,7 +93,7 @@ namespace v2rayN.HttpProxyHandler // Configure the process using the StartInfo properties. StartInfo = { - FileName = "v2ray_privoxy.exe", + FileName = $"{_privoxyName}.exe", Arguments = _uniqueConfigFile, WorkingDirectory = Utils.GetTempPath(), WindowStyle = ProcessWindowStyle.Hidden, @@ -110,12 +107,13 @@ namespace v2rayN.HttpProxyHandler * Add this process to job obj associated with this ss process, so that * when ss exit unexpectedly, this process will be forced killed by system. */ - _privoxyJob.AddProcess(_process.Handle); + Global.processJob.AddProcess(_process.Handle); } } catch (Exception ex) { + RunningPort = 0; Utils.SaveLog(ex.Message, ex); } } @@ -129,6 +127,14 @@ namespace v2rayN.HttpProxyHandler _process = null; RunningPort = 0; } + else + { + Process[] existingPrivoxy = Process.GetProcessesByName(_privoxyName); + foreach (Process p in existingPrivoxy.Where(IsChildProcess)) + { + KillProcess(p); + } + } } private static void KillProcess(Process p) @@ -168,7 +174,7 @@ namespace v2rayN.HttpProxyHandler */ var path = process.MainModule.FileName; - return Utils.GetTempPath("v2ray_privoxy.exe").Equals(path); + return Utils.GetTempPath($"{_privoxyName}.exe").Equals(path); } catch (Exception ex) diff --git a/v2rayN/v2rayN/Tool/Job.cs b/v2rayN/v2rayN/Tool/Job.cs index 64415589..daee62cf 100644 --- a/v2rayN/v2rayN/Tool/Job.cs +++ b/v2rayN/v2rayN/Tool/Job.cs @@ -54,7 +54,7 @@ namespace v2rayN if (!succ) { - //Logging.Error("Failed to call AssignProcessToJobObject! GetLastError=" + Marshal.GetLastWin32Error()); + Utils.SaveLog("Failed to call AssignProcessToJobObject! GetLastError=" + Marshal.GetLastWin32Error()); } return succ;