v2rayN/v2rayN/v2rayN/HttpProxyHandler/HttpProxyHandle.cs

187 lines
5.5 KiB
C#
Raw Normal View History

2020-03-13 06:37:18 +00:00
using System;
using v2rayN.Mode;
namespace v2rayN.HttpProxyHandler
{
2020-03-18 16:56:25 +00:00
/// <summary>
/// 系统代理(http)模式
/// </summary>
public enum ListenerType
{
noHttpProxy = 0,
GlobalHttp = 1,
2020-12-25 12:43:28 +00:00
HttpOpenAndClear = 2,
HttpOpenOnly = 3,
2020-03-18 16:56:25 +00:00
}
2020-03-13 06:37:18 +00:00
/// <summary>
/// 系统代理(http)总处理
/// 启动privoxy提供http协议
2020-12-25 12:43:28 +00:00
/// 设置IE系统代理
2020-03-13 06:37:18 +00:00
/// </summary>
class HttpProxyHandle
{
private static bool Update(Config config, bool forceDisable)
{
2020-12-25 12:43:28 +00:00
// ListenerType type = config.listenerType;
var type = ListenerType.noHttpProxy;
2020-03-13 06:37:18 +00:00
if (forceDisable)
{
2020-03-18 16:56:25 +00:00
type = ListenerType.noHttpProxy;
2020-03-13 06:37:18 +00:00
}
try
{
2020-03-18 16:56:25 +00:00
if (type != ListenerType.noHttpProxy)
2020-03-13 06:37:18 +00:00
{
2020-03-14 19:25:40 +00:00
int port = Global.httpPort;
2020-03-13 06:37:18 +00:00
if (port <= 0)
{
return false;
}
2020-03-18 16:56:25 +00:00
if (type == ListenerType.GlobalHttp)
2020-03-13 06:37:18 +00:00
{
//ProxySetting.SetProxy($"{Global.Loopback}:{port}", Global.IEProxyExceptions, 2);
SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}");
}
2020-03-18 16:56:25 +00:00
else if (type == ListenerType.HttpOpenAndClear)
2020-03-13 06:37:18 +00:00
{
SysProxyHandle.ResetIEProxy();
}
2020-03-18 16:56:25 +00:00
else if (type == ListenerType.HttpOpenOnly)
2020-03-13 06:37:18 +00:00
{
//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
{
2020-12-25 12:43:28 +00:00
//if (config.listenerType != ListenerType.HttpOpenOnly)
//{
// Update(config, true);
//}
2020-03-13 06:37:18 +00:00
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)
2020-03-13 06:37:18 +00:00
{
bool isRestart = false;
2020-12-25 12:43:28 +00:00
//if (config.listenerType == ListenerType.noHttpProxy)
//{
// // 关闭http proxy时直接返回
// return;
//}
//强制重启或者socks端口变化
if (forced)
{
isRestart = true;
}
else
{
int localPort = config.GetLocalPort(Global.InboundSocks);
if (localPort != Global.socksPort)
2020-03-13 06:37:18 +00:00
{
isRestart = true;
}
}
if (isRestart)
{
CloseHttpAgent(config);
StartHttpAgent(config);
}
Update(config, false);
2020-03-13 06:37:18 +00:00
}
2020-12-25 12:43:28 +00:00
public static bool UpdateSysProxy(Config config, bool forceDisable)
2020-03-13 06:37:18 +00:00
{
2020-12-25 12:43:28 +00:00
var type = config.sysProxyType;
if (forceDisable)
{
type = ESysProxyType.ForcedClear;
}
try
{
Global.httpPort = config.GetLocalPort(Global.InboundHttp);
int port = Global.httpPort;
if (port <= 0)
{
return false;
}
if (type == ESysProxyType.ForcedChange)
{
SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}");
}
else if (type == ESysProxyType.ForcedClear)
{
SysProxyHandle.ResetIEProxy();
}
else if (type == ESysProxyType.Unchanged)
{
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
return true;
2020-03-13 06:37:18 +00:00
}
}
}