Skip to content

Commit

Permalink
Improve PAC
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Nov 15, 2024
1 parent ba5ad12 commit 1016dcb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
45 changes: 24 additions & 21 deletions v2rayN/PacLib/PacHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ public class PacHandler
private static int _httpPort;
private static int _pacPort;
private static TcpListener? _tcpListener;
private static string _pacText;
private static byte[] _writeContent;
private static bool _isRunning;
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);

_configPath = configPath;
_httpPort = httpPort;
_pacPort = pacPort;

InitText();
await InitText();

if (_needRestart)
{
Expand All @@ -32,15 +32,24 @@ public static void Start(string configPath, int httpPort, int pacPort)
}
}

private static void InitText()
private static async Task InitText()
{
var path = Path.Combine(_configPath, "pac.txt");
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()
Expand All @@ -60,21 +69,8 @@ private static void RunListener()
continue;
}

var client = _tcpListener.AcceptTcpClient();
await Task.Run(() =>
{
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();
});
var client = await _tcpListener.AcceptTcpClientAsync();
await Task.Run(() => { WriteContent(client); });
}
catch
{
Expand All @@ -84,6 +80,13 @@ await Task.Run(() =>
}, TaskCreationOptions.LongRunning);
}

private static void WriteContent(TcpClient client)
{
var stream = client.GetStream();
stream.Write(_writeContent, 0, _writeContent.Length);
stream.Flush();
}

public static void Stop()
{
if (_tcpListener == null) return;
Expand Down
39 changes: 16 additions & 23 deletions v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ public static async Task<bool> UpdateSysProxy(Config config, bool forceDisable)
await ProxySettingLinux.SetProxy(Global.Loopback, port);
break;

case ESysProxyType.ForcedChange:
{
if (Utils.IsOSX())
{
await ProxySettingOSX.SetProxy(Global.Loopback, port);
}
case ESysProxyType.ForcedChange when Utils.IsOSX():
await ProxySettingOSX.SetProxy(Global.Loopback, port);
break;

break;
}
case ESysProxyType.ForcedClear when Utils.IsWindows():
ProxySettingWindows.UnsetProxy();
break;
Expand All @@ -50,23 +45,13 @@ public static async Task<bool> UpdateSysProxy(Config config, bool forceDisable)
await ProxySettingLinux.UnsetProxy();
break;

case ESysProxyType.ForcedClear:
{
if (Utils.IsOSX())
{
await ProxySettingOSX.UnsetProxy();
}
case ESysProxyType.ForcedClear when Utils.IsOSX():
await ProxySettingOSX.UnsetProxy();
break;

break;
}
case ESysProxyType.Pac when Utils.IsWindows():
{
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;
}
await SetWindowsProxyPac(port);
break;
}

if (type != ESysProxyType.Pac && Utils.IsWindows())
Expand Down Expand Up @@ -102,5 +87,13 @@ private static void GetWindowsProxyString(Config config, int port, int portSocks
.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);
}
}
}

0 comments on commit 1016dcb

Please sign in to comment.