diff --git a/v2rayN/PacLib/PacHandler.cs b/v2rayN/PacLib/PacHandler.cs deleted file mode 100644 index 6801c69b..00000000 --- a/v2rayN/PacLib/PacHandler.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System.IO; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; - -namespace PacLib; - -public class PacHandler -{ - private static string _configPath; - private static int _httpPort; - private static int _pacPort; - private static TcpListener? _tcpListener; - private static byte[] _writeContent; - private static bool _isRunning; - private static bool _needRestart = true; - - 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; - - await InitText(); - - if (_needRestart) - { - Stop(); - RunListener(); - } - } - - private static async Task InitText() - { - var path = Path.Combine(_configPath, "pac.txt"); - if (!File.Exists(path)) - { - await File.AppendAllTextAsync(path, Resources.ResourceManager.GetString("pac")); - } - - 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() - { - _tcpListener = TcpListener.Create(_pacPort); - _isRunning = true; - _tcpListener.Start(); - Task.Factory.StartNew(async () => - { - while (_isRunning) - { - try - { - if (!_tcpListener.Pending()) - { - await Task.Delay(10); - continue; - } - - var client = await _tcpListener.AcceptTcpClientAsync(); - await Task.Run(() => { WriteContent(client); }); - } - catch - { - // ignored - } - } - }, 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; - try - { - _isRunning = false; - _tcpListener.Stop(); - _tcpListener = null; - } - catch - { - // ignored - } - } -} \ No newline at end of file diff --git a/v2rayN/PacLib/PacLib.csproj b/v2rayN/PacLib/PacLib.csproj deleted file mode 100644 index feb6e55d..00000000 --- a/v2rayN/PacLib/PacLib.csproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - net8.0 - enable - - - - - True - True - Resources.resx - - - ResXFileCodeGenerator - Resources.Designer.cs - - - - diff --git a/v2rayN/PacLib/Resources.Designer.cs b/v2rayN/PacLib/Resources.Designer.cs deleted file mode 100644 index e7bfd02c..00000000 --- a/v2rayN/PacLib/Resources.Designer.cs +++ /dev/null @@ -1,95 +0,0 @@ -//------------------------------------------------------------------------------ -// -// 此代码由工具生成。 -// 运行时版本:4.0.30319.42000 -// -// 对此文件的更改可能会导致不正确的行为,并且如果 -// 重新生成代码,这些更改将会丢失。 -// -//------------------------------------------------------------------------------ - -namespace PacLib { - using System; - - - /// - /// 一个强类型的资源类,用于查找本地化的字符串等。 - /// - // 此类是由 StronglyTypedResourceBuilder - // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 - // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen - // (以 /str 作为命令选项),或重新生成 VS 项目。 - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - /// - /// 返回此类使用的缓存的 ResourceManager 实例。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("PacLib.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// 重写当前线程的 CurrentUICulture 属性,对 - /// 使用此强类型资源类的所有资源查找执行重写。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// 查找类似 var proxy = '__PROXY__'; - ///var rules = [ - /// [ - /// [], - /// [] - /// ], - /// [ - /// [ - /// "aftygh.gov.tw", - /// "aide.gov.tw", - /// "aliyun.com", - /// "arte.gov.tw", - /// "baidu.com", - /// "chinaso.com", - /// "chinaz.com", - /// "chukuang.gov.tw", - /// "cycab.gov.tw", - /// "dbnsa.gov.tw", - /// "df.gov.tw", - /// "eastcoast-nsa.gov.tw", - /// "erv-nsa.gov.tw", - /// "grb.gov.tw", - /// "haosou.com", - /// [字符串的其余部分被截断]"; 的本地化字符串。 - /// - internal static string pac { - get { - return ResourceManager.GetString("pac", resourceCulture); - } - } - } -} diff --git a/v2rayN/PacLib/Resources.resx b/v2rayN/PacLib/Resources.resx deleted file mode 100644 index 1fffd5af..00000000 --- a/v2rayN/PacLib/Resources.resx +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - - Resources\pac.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;gb2312 - - \ No newline at end of file diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 4e93b66a..4a4e89d9 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -47,6 +47,7 @@ public const string ClashMixinYaml = NamespaceSample + "clash_mixin_yaml"; public const string ClashTunYaml = NamespaceSample + "clash_tun_yaml"; public const string LinuxAutostartConfig = NamespaceSample + "linux_autostart_config"; + public const string PacFileName = NamespaceSample + "pac"; public const string DefaultSecurity = "auto"; public const string DefaultNetwork = "tcp"; diff --git a/v2rayN/ServiceLib/Handler/PacHandler.cs b/v2rayN/ServiceLib/Handler/PacHandler.cs new file mode 100644 index 00000000..f6bf0e28 --- /dev/null +++ b/v2rayN/ServiceLib/Handler/PacHandler.cs @@ -0,0 +1,105 @@ +using System.Net.Sockets; +using System.Text; + +namespace ServiceLib.Handler +{ + public class PacHandler + { + private static string _configPath; + private static int _httpPort; + private static int _pacPort; + private static TcpListener? _tcpListener; + private static byte[] _writeContent; + private static bool _isRunning; + private static bool _needRestart = true; + + 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; + + await InitText(); + + if (_needRestart) + { + Stop(); + RunListener(); + } + } + + private static async Task InitText() + { + var path = Path.Combine(_configPath, "pac.txt"); + if (!File.Exists(path)) + { + var pac = Utils.GetEmbedText(Global.PacFileName); + await File.AppendAllTextAsync(path, pac); + } + + 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() + { + _tcpListener = TcpListener.Create(_pacPort); + _isRunning = true; + _tcpListener.Start(); + Task.Factory.StartNew(async () => + { + while (_isRunning) + { + try + { + if (!_tcpListener.Pending()) + { + await Task.Delay(10); + continue; + } + + var client = await _tcpListener.AcceptTcpClientAsync(); + await Task.Run(() => { WriteContent(client); }); + } + catch + { + // ignored + } + } + }, 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; + try + { + _isRunning = false; + _tcpListener.Stop(); + _tcpListener = null; + } + catch + { + // ignored + } + } + } +} \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs b/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs index de2ac9db..54b6ab3b 100644 --- a/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs @@ -1,6 +1,4 @@ -using PacLib; - -namespace ServiceLib.Handler.SysProxy +namespace ServiceLib.Handler.SysProxy { public static class SysProxyHandler { diff --git a/v2rayN/PacLib/Resources/pac.txt b/v2rayN/ServiceLib/Sample/pac similarity index 100% rename from v2rayN/PacLib/Resources/pac.txt rename to v2rayN/ServiceLib/Sample/pac diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index db26f8c6..d1ee0aa2 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -33,6 +33,7 @@ + @@ -46,11 +47,6 @@ - - - - - ResUI.resx diff --git a/v2rayN/v2rayN.sln b/v2rayN/v2rayN.sln index ea0fd985..22a2bcce 100644 --- a/v2rayN/v2rayN.sln +++ b/v2rayN/v2rayN.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 17.3.32811.315 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "v2rayN", "v2rayN\v2rayN.csproj", "{6DE127CA-1763-4236-B297-D2EF9CB2EC9B}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PacLib", "PacLib\PacLib.csproj", "{EE4E6CD8-8353-446B-8F29-A841A02AE5EC}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceLib", "ServiceLib\ServiceLib.csproj", "{1B6456C4-FFAA-4298-80F6-7B689A6E9243}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "v2rayN.Desktop", "v2rayN.Desktop\v2rayN.Desktop.csproj", "{5D16541A-F971-4C17-9315-BB8955E3F984}" @@ -23,10 +21,6 @@ Global {6DE127CA-1763-4236-B297-D2EF9CB2EC9B}.Debug|Any CPU.Build.0 = Debug|Any CPU {6DE127CA-1763-4236-B297-D2EF9CB2EC9B}.Release|Any CPU.ActiveCfg = Release|Any CPU {6DE127CA-1763-4236-B297-D2EF9CB2EC9B}.Release|Any CPU.Build.0 = Release|Any CPU - {EE4E6CD8-8353-446B-8F29-A841A02AE5EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EE4E6CD8-8353-446B-8F29-A841A02AE5EC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EE4E6CD8-8353-446B-8F29-A841A02AE5EC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EE4E6CD8-8353-446B-8F29-A841A02AE5EC}.Release|Any CPU.Build.0 = Release|Any CPU {1B6456C4-FFAA-4298-80F6-7B689A6E9243}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1B6456C4-FFAA-4298-80F6-7B689A6E9243}.Debug|Any CPU.Build.0 = Debug|Any CPU {1B6456C4-FFAA-4298-80F6-7B689A6E9243}.Release|Any CPU.ActiveCfg = Release|Any CPU