From 4b2b45979b34b96756df03f6e9506f1d46538e82 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:00:28 +0800 Subject: [PATCH] Return api port=socks port +11 --- v2rayN/v2rayN/Common/Utile.cs | 5 ++- v2rayN/v2rayN/Handler/LazyConfig.cs | 40 +++++++--------------- v2rayN/v2rayN/Handler/StatisticsHandler.cs | 24 +------------ 3 files changed, 16 insertions(+), 53 deletions(-) diff --git a/v2rayN/v2rayN/Common/Utile.cs b/v2rayN/v2rayN/Common/Utile.cs index c25a1818..30d4a6dd 100644 --- a/v2rayN/v2rayN/Common/Utile.cs +++ b/v2rayN/v2rayN/Common/Utile.cs @@ -579,11 +579,10 @@ namespace v2rayN return inUse; } - public static int GetFreePort() + public static int GetFreePort(int defaultPort = 9090) { try { - int defaultPort = 9090; if (!Utile.PortInUse(defaultPort)) { return defaultPort; @@ -598,7 +597,7 @@ namespace v2rayN catch { } - return 69090; + return 59090; } #endregion 测速 diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index 09428e5f..2cc46fac 100644 --- a/v2rayN/v2rayN/Handler/LazyConfig.cs +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -19,7 +19,7 @@ namespace v2rayN.Handler { if (_statePort is null) { - _statePort = Utile.GetFreePort(); + _statePort = Utile.GetFreePort(GetLocalPort(Global.InboundAPITagName)); } return _statePort.Value; @@ -52,32 +52,18 @@ namespace v2rayN.Handler public int GetLocalPort(string protocol) { - int localPort = _config.inbound.FirstOrDefault(t => t.protocol == Global.InboundSocks).localPort; - if (protocol == Global.InboundSocks) - { - return localPort; - } - else if (protocol == Global.InboundHttp) - { - return localPort + 1; - } - else if (protocol == Global.InboundSocks2) - { - return localPort + 2; - } - else if (protocol == Global.InboundHttp2) - { - return localPort + 3; - } - else if (protocol == ESysProxyType.Pac.ToString()) - { - return localPort + 4; - } - else if (protocol == "speedtest") - { - return localPort + 103; - } - return localPort; + var localPort = _config.inbound.FirstOrDefault(t => t.protocol == Global.InboundSocks)?.localPort ?? 10808; + return protocol.ToLower() switch + { + Global.InboundSocks => localPort, + Global.InboundHttp => localPort + 1, + Global.InboundSocks2 => localPort + 2, + Global.InboundHttp2 => localPort + 3, + "pac" => localPort + 4, + Global.InboundAPITagName => localPort + 11, + "speedtest" => localPort + 21, + _ => localPort, + }; } public void AddProcess(IntPtr processHandle) diff --git a/v2rayN/v2rayN/Handler/StatisticsHandler.cs b/v2rayN/v2rayN/Handler/StatisticsHandler.cs index 2e4697a8..abc86f2d 100644 --- a/v2rayN/v2rayN/Handler/StatisticsHandler.cs +++ b/v2rayN/v2rayN/Handler/StatisticsHandler.cs @@ -132,28 +132,6 @@ namespace v2rayN.Handler _serverStatItem.todayDown = 0; _serverStatItem.dateNow = ticks; } - } - - private int GetFreePort() - { - try - { - int defaultPort = 9090; - if (!Utile.PortInUse(defaultPort)) - { - return defaultPort; - } - - TcpListener l = new(IPAddress.Loopback, 0); - l.Start(); - int port = ((IPEndPoint)l.LocalEndpoint).Port; - l.Stop(); - return port; - } - catch - { - } - return 69090; - } + } } } \ No newline at end of file