Browse Source

Improve PAC

pull/6080/head
2dust 1 week ago
parent
commit
1016dcb3d1
  1. 45
      v2rayN/PacLib/PacHandler.cs
  2. 33
      v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs

45
v2rayN/PacLib/PacHandler.cs

@ -11,11 +11,11 @@ public class PacHandler
private static int _httpPort; private static int _httpPort;
private static int _pacPort; private static int _pacPort;
private static TcpListener? _tcpListener; private static TcpListener? _tcpListener;
private static string _pacText; private static byte[] _writeContent;
private static bool _isRunning; private static bool _isRunning;
private static bool _needRestart = true; private static bool _needRestart = true;
public static void Start(string configPath, int httpPort, int pacPort) public static async Task Start(string configPath, int httpPort, int pacPort)
{ {
_needRestart = (configPath != _configPath || httpPort != _httpPort || pacPort != _pacPort || !_isRunning); _needRestart = (configPath != _configPath || httpPort != _httpPort || pacPort != _pacPort || !_isRunning);
@ -23,7 +23,7 @@ public class PacHandler
_httpPort = httpPort; _httpPort = httpPort;
_pacPort = pacPort; _pacPort = pacPort;
InitText(); await InitText();
if (_needRestart) if (_needRestart)
{ {
@ -32,15 +32,24 @@ public class PacHandler
} }
} }
private static void InitText() private static async Task InitText()
{ {
var path = Path.Combine(_configPath, "pac.txt"); var path = Path.Combine(_configPath, "pac.txt");
if (!File.Exists(path)) if (!File.Exists(path))
{ {
File.AppendAllText(path, Resources.ResourceManager.GetString("pac")); await File.AppendAllTextAsync(path, Resources.ResourceManager.GetString("pac"));
} }
_pacText = File.ReadAllText(path).Replace("__PROXY__", $"PROXY 127.0.0.1:{_httpPort};DIRECT;"); var pacText = (await File.ReadAllTextAsync(path)).Replace("__PROXY__", $"PROXY 127.0.0.1:{_httpPort};DIRECT;");
var sb = new StringBuilder();
sb.AppendLine("HTTP/1.0 200 OK");
sb.AppendLine("Content-type:application/x-ns-proxy-autoconfig");
sb.AppendLine("Connection:close");
sb.AppendLine("Content-Length:" + Encoding.UTF8.GetByteCount(pacText));
sb.AppendLine();
sb.Append(pacText);
_writeContent = Encoding.UTF8.GetBytes(sb.ToString());
} }
private static void RunListener() private static void RunListener()
@ -60,21 +69,8 @@ public class PacHandler
continue; continue;
} }
var client = _tcpListener.AcceptTcpClient(); var client = await _tcpListener.AcceptTcpClientAsync();
await Task.Run(() => await Task.Run(() => { WriteContent(client); });
{
var stream = client.GetStream();
var sb = new StringBuilder();
sb.AppendLine("HTTP/1.0 200 OK");
sb.AppendLine("Content-type:application/x-ns-proxy-autoconfig");
sb.AppendLine("Connection:close");
sb.AppendLine("Content-Length:" + Encoding.UTF8.GetByteCount(_pacText));
sb.AppendLine();
sb.Append(_pacText);
var content = Encoding.UTF8.GetBytes(sb.ToString());
stream.Write(content, 0, content.Length);
stream.Flush();
});
} }
catch catch
{ {
@ -84,6 +80,13 @@ public class PacHandler
}, TaskCreationOptions.LongRunning); }, TaskCreationOptions.LongRunning);
} }
private static void WriteContent(TcpClient client)
{
var stream = client.GetStream();
stream.Write(_writeContent, 0, _writeContent.Length);
stream.Flush();
}
public static void Stop() public static void Stop()
{ {
if (_tcpListener == null) return; if (_tcpListener == null) return;

33
v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs

@ -33,15 +33,10 @@ namespace ServiceLib.Handler.SysProxy
await ProxySettingLinux.SetProxy(Global.Loopback, port); await ProxySettingLinux.SetProxy(Global.Loopback, port);
break; break;
case ESysProxyType.ForcedChange: case ESysProxyType.ForcedChange when Utils.IsOSX():
{
if (Utils.IsOSX())
{
await ProxySettingOSX.SetProxy(Global.Loopback, port); await ProxySettingOSX.SetProxy(Global.Loopback, port);
}
break; break;
}
case ESysProxyType.ForcedClear when Utils.IsWindows(): case ESysProxyType.ForcedClear when Utils.IsWindows():
ProxySettingWindows.UnsetProxy(); ProxySettingWindows.UnsetProxy();
break; break;
@ -50,24 +45,14 @@ namespace ServiceLib.Handler.SysProxy
await ProxySettingLinux.UnsetProxy(); await ProxySettingLinux.UnsetProxy();
break; break;
case ESysProxyType.ForcedClear: case ESysProxyType.ForcedClear when Utils.IsOSX():
{
if (Utils.IsOSX())
{
await ProxySettingOSX.UnsetProxy(); await ProxySettingOSX.UnsetProxy();
}
break; break;
}
case ESysProxyType.Pac when Utils.IsWindows(): case ESysProxyType.Pac when Utils.IsWindows():
{ await SetWindowsProxyPac(port);
var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
PacHandler.Start(Utils.GetConfigPath(), port, portPac);
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
ProxySettingWindows.SetProxy(strProxy, "", 4);
break; break;
} }
}
if (type != ESysProxyType.Pac && Utils.IsWindows()) if (type != ESysProxyType.Pac && Utils.IsWindows())
{ {
@ -102,5 +87,13 @@ namespace ServiceLib.Handler.SysProxy
.Replace("{socks_port}", portSocks.ToString()); .Replace("{socks_port}", portSocks.ToString());
} }
} }
private static async Task SetWindowsProxyPac(int port)
{
var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
await PacHandler.Start(Utils.GetConfigPath(), port, portPac);
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
ProxySettingWindows.SetProxy(strProxy, "", 4);
}
} }
} }
Loading…
Cancel
Save