diff --git a/v2rayN/v2rayN/Forms/MainForm.cs b/v2rayN/v2rayN/Forms/MainForm.cs index 575f05f5..a2fec808 100644 --- a/v2rayN/v2rayN/Forms/MainForm.cs +++ b/v2rayN/v2rayN/Forms/MainForm.cs @@ -1,17 +1,14 @@ using System; using System.Collections.Generic; -using System.IO; +using System.Diagnostics; +using System.Drawing; using System.Text; +using System.Text.RegularExpressions; using System.Windows.Forms; +using v2rayN.Base; using v2rayN.Handler; -using v2rayN.HttpProxyHandler; using v2rayN.Mode; -using v2rayN.Base; using v2rayN.Tool; -using System.Diagnostics; -using System.Drawing; -using System.Net; -using System.Text.RegularExpressions; namespace v2rayN.Forms { @@ -125,11 +122,11 @@ namespace v2rayN.Forms //HttpProxyHandle.CloseHttpAgent(config); if (blWindowsShutDown) { - HttpProxyHandle.ResetIEProxy4WindowsShutDown(); + SysProxyHandle.ResetIEProxy4WindowsShutDown(); } else { - HttpProxyHandle.UpdateSysProxy(config, true); + SysProxyHandle.UpdateSysProxy(config, true); } ConfigHandler.SaveConfig(ref config); @@ -1214,7 +1211,7 @@ namespace v2rayN.Forms private void ChangePACButtonStatus(ESysProxyType type) { - HttpProxyHandle.UpdateSysProxy(config, false); + SysProxyHandle.UpdateSysProxy(config, false); for (int k = 0; k < menuSysAgentMode.DropDownItems.Count; k++) { diff --git a/v2rayN/v2rayN/Forms/OptionSettingForm.cs b/v2rayN/v2rayN/Forms/OptionSettingForm.cs index 97a3aa56..7a8c0525 100644 --- a/v2rayN/v2rayN/Forms/OptionSettingForm.cs +++ b/v2rayN/v2rayN/Forms/OptionSettingForm.cs @@ -1,11 +1,9 @@ using System; -using System.Collections.Generic; +using System.Diagnostics; using System.Windows.Forms; -using v2rayN.Handler; using v2rayN.Base; -using v2rayN.HttpProxyHandler; +using v2rayN.Handler; using v2rayN.Mode; -using System.Diagnostics; namespace v2rayN.Forms { diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 0fd71c75..ef587c0a 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -183,6 +183,7 @@ namespace v2rayN.Handler } } + LazyConfig.Instance.SetConfig(ref config); return 0; } diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs new file mode 100644 index 00000000..976c8970 --- /dev/null +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -0,0 +1,24 @@ +using System; +using v2rayN.Mode; + +namespace v2rayN.Handler +{ + public sealed class LazyConfig + { + private static readonly Lazy _instance = new Lazy(() => new LazyConfig()); + private Config _config; + + public static LazyConfig Instance + { + get { return _instance.Value; } + } + public void SetConfig(ref Config config) + { + _config = config; + } + public Config GetConfig() + { + return _config; + } + } +} diff --git a/v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs b/v2rayN/v2rayN/Handler/ProxySetting.cs similarity index 99% rename from v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs rename to v2rayN/v2rayN/Handler/ProxySetting.cs index 2ee71072..4e900eee 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs +++ b/v2rayN/v2rayN/Handler/ProxySetting.cs @@ -2,7 +2,7 @@ using System; using System.Runtime.InteropServices; -namespace v2rayN.HttpProxyHandler +namespace v2rayN.Handler { class ProxySetting { diff --git a/v2rayN/v2rayN/HttpProxyHandler/SysProxyHandle.cs b/v2rayN/v2rayN/Handler/SysProxyHandle.cs similarity index 81% rename from v2rayN/v2rayN/HttpProxyHandler/SysProxyHandle.cs rename to v2rayN/v2rayN/Handler/SysProxyHandle.cs index 2d6f96e0..0dc19e72 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/SysProxyHandle.cs +++ b/v2rayN/v2rayN/Handler/SysProxyHandle.cs @@ -8,7 +8,7 @@ using v2rayN.Mode; using v2rayN.Properties; using v2rayN.Tool; -namespace v2rayN.HttpProxyHandler +namespace v2rayN.Handler { public static class SysProxyHandle { @@ -47,6 +47,56 @@ namespace v2rayN.HttpProxyHandler } } + + public static bool UpdateSysProxy(Config config, bool forceDisable) + { + var type = config.sysProxyType; + + if (forceDisable && type == ESysProxyType.ForcedChange) + { + type = ESysProxyType.ForcedClear; + } + + try + { + Global.httpPort = config.GetLocalPort(Global.InboundHttp); + int port = Global.httpPort; + if (port <= 0) + { + return false; + } + if (type == ESysProxyType.ForcedChange) + { + var strExceptions = $"{config.constItem.defIEProxyExceptions};{config.systemProxyExceptions}"; + SetIEProxy(true, $"{Global.Loopback}:{port}", strExceptions); + } + else if (type == ESysProxyType.ForcedClear) + { + ResetIEProxy(); + } + else if (type == ESysProxyType.Unchanged) + { + } + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + } + return true; + } + + public static void ResetIEProxy4WindowsShutDown() + { + try + { + //TODO To be verified + Utils.RegWriteValue(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", 0); + } + catch + { + } + } + public static void SetIEProxy(bool enable, bool global, string strProxy) { //Read(); diff --git a/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs b/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs deleted file mode 100644 index d4f14af0..00000000 --- a/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs +++ /dev/null @@ -1,199 +0,0 @@ -using System; -using v2rayN.Mode; - -namespace v2rayN.HttpProxyHandler -{ - /// - /// 系统代理(http)模式 - /// - public enum ListenerType - { - noHttpProxy = 0, - GlobalHttp = 1, - HttpOpenAndClear = 2, - HttpOpenOnly = 3, - } - /// - /// 系统代理(http)总处理 - /// 启动privoxy提供http协议 - /// 设置IE系统代理 - /// - class HttpProxyHandle - { - private static bool Update(Config config, bool forceDisable) - { - // ListenerType type = config.listenerType; - var type = ListenerType.noHttpProxy; - if (forceDisable) - { - type = ListenerType.noHttpProxy; - } - - try - { - if (type != ListenerType.noHttpProxy) - { - int port = Global.httpPort; - if (port <= 0) - { - return false; - } - if (type == ListenerType.GlobalHttp) - { - //ProxySetting.SetProxy($"{Global.Loopback}:{port}", Global.IEProxyExceptions, 2); - SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}"); - } - else if (type == ListenerType.HttpOpenAndClear) - { - SysProxyHandle.ResetIEProxy(); - } - else if (type == ListenerType.HttpOpenOnly) - { - //SysProxyHandle.ResetIEProxy(); - } - } - else - { - SysProxyHandle.ResetIEProxy(); - } - } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - } - return true; - } - - /// - /// 启用系统代理(http) - /// - /// - private static void StartHttpAgent(Config config) - { - try - { - int localPort = config.GetLocalPort(Global.InboundSocks); - if (localPort > 0) - { - PrivoxyHandler.Instance.Restart(localPort, config); - if (PrivoxyHandler.Instance.RunningPort > 0) - { - Global.sysAgent = true; - Global.socksPort = localPort; - Global.httpPort = PrivoxyHandler.Instance.RunningPort; - } - } - } - catch - { - } - } - - /// - /// 关闭系统代理 - /// - /// - public static void CloseHttpAgent(Config config) - { - try - { - //if (config.listenerType != ListenerType.HttpOpenOnly) - //{ - // Update(config, true); - //} - - PrivoxyHandler.Instance.Stop(); - - Global.sysAgent = false; - Global.socksPort = 0; - Global.httpPort = 0; - } - catch - { - } - } - - /// - /// 重启系统代理(http) - /// - /// - /// - public static void RestartHttpAgent(Config config, bool forced) - { - bool isRestart = false; - //if (config.listenerType == ListenerType.noHttpProxy) - //{ - // // 关闭http proxy时,直接返回 - // return; - //} - //强制重启或者socks端口变化 - if (forced) - { - isRestart = true; - } - else - { - int localPort = config.GetLocalPort(Global.InboundSocks); - if (localPort != Global.socksPort) - { - isRestart = true; - } - } - if (isRestart) - { - CloseHttpAgent(config); - StartHttpAgent(config); - } - Update(config, false); - } - - public static bool UpdateSysProxy(Config config, bool forceDisable) - { - var type = config.sysProxyType; - - if (forceDisable && type == ESysProxyType.ForcedChange) - { - type = ESysProxyType.ForcedClear; - } - - try - { - Global.httpPort = config.GetLocalPort(Global.InboundHttp); - int port = Global.httpPort; - if (port <= 0) - { - return false; - } - if (type == ESysProxyType.ForcedChange) - { - var strExceptions = $"{config.constItem.defIEProxyExceptions};{config.systemProxyExceptions}"; - SysProxyHandle.SetIEProxy(true, $"{Global.Loopback}:{port}", strExceptions); - } - else if (type == ESysProxyType.ForcedClear) - { - SysProxyHandle.ResetIEProxy(); - } - else if (type == ESysProxyType.Unchanged) - { - } - } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - } - return true; - } - - public static void ResetIEProxy4WindowsShutDown() - { - try - { - //TODO To be verified - Utils.RegWriteValue(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", 0); - } - catch - { - } - } - } -} diff --git a/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs b/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs deleted file mode 100644 index a49e2d04..00000000 --- a/v2rayN/v2rayN/HttpProxyHandler/PrivoxyHandler.cs +++ /dev/null @@ -1,194 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Text; -using System.Windows.Forms; -using v2rayN.Mode; -using v2rayN.Properties; -using v2rayN.Tool; - -namespace v2rayN.HttpProxyHandler -{ - /// - /// Privoxy处理类,提供http协议代理 - /// - class PrivoxyHandler - { - /// - /// 单例 - /// - private static PrivoxyHandler instance; - - private static int _uid; - private static string _uniqueConfigFile; - private Process _process; - private static string _privoxyName = "v2ray_privoxy"; - - static PrivoxyHandler() - { - try - { - _uid = Application.StartupPath.GetHashCode(); - _uniqueConfigFile = string.Format("privoxy_{0}.conf", _uid); - - FileManager.UncompressFile(Utils.GetTempPath($"{_privoxyName}.exe"), Resources.privoxy_exe); - } - catch (IOException ex) - { - Utils.SaveLog(ex.Message, ex); - } - } - - /// - /// 单例 - /// - public static PrivoxyHandler Instance - { - get - { - if (instance == null) - { - instance = new PrivoxyHandler(); - } - return instance; - } - } - - public int RunningPort - { - get; set; - } - - public void Restart(int localPort, Config config) - { - Stop(); - Start(localPort, config); - } - - - public void Start(int localPort, Config config) - { - try - { - if (_process == null) - { - - string privoxyConfig = "";//Resources.privoxy_conf; - RunningPort = config.GetLocalPort(Global.InboundHttp); - privoxyConfig = privoxyConfig.Replace("__SOCKS_PORT__", localPort.ToString()); - privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_PORT__", RunningPort.ToString()); - if (config.allowLANConn) - { - privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_IP__", "0.0.0.0"); - } - else - { - privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_IP__", Global.Loopback); - } - FileManager.ByteArrayToFile(Utils.GetTempPath(_uniqueConfigFile), Encoding.UTF8.GetBytes(privoxyConfig)); - - _process = new Process - { - // Configure the process using the StartInfo properties. - StartInfo = - { - FileName = $"{_privoxyName}.exe", - Arguments = _uniqueConfigFile, - WorkingDirectory = Utils.GetTempPath(), - WindowStyle = ProcessWindowStyle.Hidden, - UseShellExecute = true, - CreateNoWindow = true - } - }; - _process.Start(); - - /* - * 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. - */ - - Global.processJob.AddProcess(_process.Handle); - } - } - catch (Exception ex) - { - RunningPort = 0; - Utils.SaveLog(ex.Message, ex); - } - } - - public void Stop() - { - if (_process != null) - { - KillProcess(_process); - _process.Dispose(); - _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) - { - try - { - p.CloseMainWindow(); - p.WaitForExit(100); - if (!p.HasExited) - { - p.Kill(); - p.WaitForExit(100); - } - } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - } - } - - /* - * We won't like to kill other ss instances' v2ray_privoxy.exe. - * This function will check whether the given process is created - * by this process by checking the module path or command line. - * - * Since it's required to put ss in different dirs to run muti instances, - * different instance will create their unique "privoxy_UID.conf" where - * UID is hash of ss's location. - */ - - private static bool IsChildProcess(Process process) - { - try - { - /* - * Under PortableMode, we could identify it by the path of v2ray_privoxy.exe. - */ - string path = process.MainModule.FileName; - - return Utils.GetTempPath($"{_privoxyName}.exe").Equals(path); - - } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - /* - * Sometimes Process.GetProcessesByName will return some processes that - * are already dead, and that will cause exceptions here. - * We could simply ignore those exceptions. - */ - //Logging.LogUsefulException(ex); - return false; - } - } - - } -} diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index 505a6101..f2ca1c04 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using v2rayN.Base; -using v2rayN.HttpProxyHandler; namespace v2rayN.Mode diff --git a/v2rayN/v2rayN/Properties/Resources.Designer.cs b/v2rayN/v2rayN/Properties/Resources.Designer.cs index 672c250f..a1c0e06f 100644 --- a/v2rayN/v2rayN/Properties/Resources.Designer.cs +++ b/v2rayN/v2rayN/Properties/Resources.Designer.cs @@ -47,8 +47,8 @@ namespace v2rayN.Properties { } /// - /// 重写当前线程的 CurrentUICulture 属性 - /// 重写当前线程的 CurrentUICulture 属性。 + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { @@ -120,16 +120,6 @@ namespace v2rayN.Properties { } } - /// - /// 查找 System.Byte[] 类型的本地化资源。 - /// - internal static byte[] privoxy_exe { - get { - object obj = ResourceManager.GetObject("privoxy_exe", resourceCulture); - return ((byte[])(obj)); - } - } - /// /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// diff --git a/v2rayN/v2rayN/Properties/Resources.resx b/v2rayN/v2rayN/Properties/Resources.resx index 2ffcd3a9..c5d8319b 100644 --- a/v2rayN/v2rayN/Properties/Resources.resx +++ b/v2rayN/v2rayN/Properties/Resources.resx @@ -118,9 +118,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\resources\privoxy.exe.gz;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - ..\Resources\about.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/v2rayN/v2rayN/Resources/privoxy.exe.gz b/v2rayN/v2rayN/Resources/privoxy.exe.gz deleted file mode 100644 index bf58503c..00000000 Binary files a/v2rayN/v2rayN/Resources/privoxy.exe.gz and /dev/null differ diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index d86e904a..202d3e56 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -149,6 +149,7 @@ ServerTransportControl.cs + @@ -198,13 +199,11 @@ - - - + Component - + @@ -466,7 +465,6 @@ -