mirror of https://github.com/2dust/v2rayN
Refactor some code
parent
b2d538d02a
commit
03ad07733f
|
@ -1,17 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
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;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
using v2rayN.Base;
|
||||
using v2rayN.Handler;
|
||||
using v2rayN.Mode;
|
||||
using v2rayN.Tool;
|
||||
|
||||
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++)
|
||||
{
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
using v2rayN.Handler;
|
||||
using v2rayN.Base;
|
||||
using v2rayN.HttpProxyHandler;
|
||||
using v2rayN.Mode;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Forms;
|
||||
using v2rayN.Base;
|
||||
using v2rayN.Handler;
|
||||
using v2rayN.Mode;
|
||||
|
||||
namespace v2rayN.Forms
|
||||
{
|
||||
|
|
|
@ -183,6 +183,7 @@ namespace v2rayN.Handler
|
|||
}
|
||||
}
|
||||
|
||||
LazyConfig.Instance.SetConfig(ref config);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using v2rayN.Mode;
|
||||
|
||||
namespace v2rayN.Handler
|
||||
{
|
||||
public sealed class LazyConfig
|
||||
{
|
||||
private static readonly Lazy<LazyConfig> _instance = new Lazy<LazyConfig>(() => 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace v2rayN.HttpProxyHandler
|
||||
namespace v2rayN.Handler
|
||||
{
|
||||
class ProxySetting
|
||||
{
|
|
@ -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();
|
|
@ -1,199 +0,0 @@
|
|||
using System;
|
||||
using v2rayN.Mode;
|
||||
|
||||
namespace v2rayN.HttpProxyHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 系统代理(http)模式
|
||||
/// </summary>
|
||||
public enum ListenerType
|
||||
{
|
||||
noHttpProxy = 0,
|
||||
GlobalHttp = 1,
|
||||
HttpOpenAndClear = 2,
|
||||
HttpOpenOnly = 3,
|
||||
}
|
||||
/// <summary>
|
||||
/// 系统代理(http)总处理
|
||||
/// 启动privoxy提供http协议
|
||||
/// 设置IE系统代理
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启用系统代理(http)
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭系统代理
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重启系统代理(http)
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="forced"></param>
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Privoxy处理类,提供http协议代理
|
||||
/// </summary>
|
||||
class PrivoxyHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 单例
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单例
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using v2rayN.Base;
|
||||
using v2rayN.HttpProxyHandler;
|
||||
|
||||
|
||||
namespace v2rayN.Mode
|
||||
|
|
|
@ -47,8 +47,8 @@ namespace v2rayN.Properties {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重写当前线程的 CurrentUICulture 属性
|
||||
/// 重写当前线程的 CurrentUICulture 属性。
|
||||
/// 重写当前线程的 CurrentUICulture 属性,对
|
||||
/// 使用此强类型资源类的所有资源查找执行重写。
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
|
@ -120,16 +120,6 @@ namespace v2rayN.Properties {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找 System.Byte[] 类型的本地化资源。
|
||||
/// </summary>
|
||||
internal static byte[] privoxy_exe {
|
||||
get {
|
||||
object obj = ResourceManager.GetObject("privoxy_exe", resourceCulture);
|
||||
return ((byte[])(obj));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
|
||||
/// </summary>
|
||||
|
|
|
@ -118,9 +118,6 @@
|
|||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
|
||||
<data name="privoxy_exe" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>..\resources\privoxy.exe.gz;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</data>
|
||||
<data name="about" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>..\Resources\about.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
|
||||
</data>
|
||||
|
|
Binary file not shown.
|
@ -149,6 +149,7 @@
|
|||
<Compile Include="Forms\ServerTransportControl.Designer.cs">
|
||||
<DependentUpon>ServerTransportControl.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Handler\LazyConfig.cs" />
|
||||
<Compile Include="Handler\ShareHandler.cs" />
|
||||
<Compile Include="Handler\UpdateHandle.cs" />
|
||||
<Compile Include="Mode\ComboItem.cs" />
|
||||
|
@ -198,13 +199,11 @@
|
|||
<Compile Include="Handler\SpeedtestHandler.cs" />
|
||||
<Compile Include="Handler\StatisticsHandler.cs" />
|
||||
<Compile Include="Handler\DownloadHandle.cs" />
|
||||
<Compile Include="HttpProxyHandler\PrivoxyHandler.cs" />
|
||||
<Compile Include="HttpProxyHandler\ProxySetting.cs" />
|
||||
<Compile Include="HttpProxyHandler\HttpProxyHandle.cs" />
|
||||
<Compile Include="Handler\ProxySetting.cs" />
|
||||
<Compile Include="Base\WebClientEx.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="HttpProxyHandler\SysProxyHandle.cs" />
|
||||
<Compile Include="Handler\SysProxyHandle.cs" />
|
||||
<Compile Include="Mode\ECoreType.cs" />
|
||||
<Compile Include="Mode\ESysProxyType.cs" />
|
||||
<Compile Include="Mode\EMove.cs" />
|
||||
|
@ -466,7 +465,6 @@
|
|||
<EmbeddedResource Include="Sample\SampleServerConfig.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\privoxy.exe.gz" />
|
||||
<None Include="Resources\restart.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
Loading…
Reference in New Issue