diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index c8bc583e..76148041 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -69,13 +69,7 @@ namespace v2rayN.Handler }; config.inbound.Add(inItem); - - //inItem = new InItem(); - //inItem.protocol = "http"; - //inItem.localPort = 1081; - //inItem.udpEnabled = true; - - //config.inbound.Add(inItem); + } else { diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 4d418e1b..21374738 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -132,8 +132,8 @@ namespace v2rayN.Handler fileContent.RemoveAt(indexPort); } - fileContent.Add($"port: {LazyConfig.Instance.GetLocalPort(Global.InboundHttp)}"); - fileContent.Add($"socks-port: {LazyConfig.Instance.GetLocalPort(Global.InboundSocks)}"); + fileContent.Add($"port: {LazyConfig.Instance.GetLocalPort(EInboundProtocol.http)}"); + fileContent.Add($"socks-port: {LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)}"); break; } File.WriteAllLines(fileName, fileContent); diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 6feb2f55..f726e754 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -121,7 +121,7 @@ namespace v2rayN.Handler }; singboxConfig.inbounds.Add(inbound); - inbound.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundSocks); + inbound.listen_port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks); inbound.sniff = _config.inbound[0].sniffingEnabled; inbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled; inbound.domain_strategy = Utile.IsNullOrEmpty(_config.routingBasicItem.domainStrategy4Singbox) ? null : _config.routingBasicItem.domainStrategy4Singbox; @@ -405,13 +405,13 @@ namespace v2rayN.Handler switch (node.GetNetwork()) { - case "h2": - transport.type = "http"; + case nameof(ETransport.h2): + transport.type = nameof(ETransport.http); transport.host = Utile.IsNullOrEmpty(node.requestHost) ? null : Utile.String2List(node.requestHost); transport.path = Utile.IsNullOrEmpty(node.path) ? null : node.path; break; - case "tcp": //http + case nameof(ETransport.tcp): //http if (node.headerType == Global.TcpHeaderHttp) { if (node.configType == EConfigType.Shadowsocks) @@ -421,7 +421,7 @@ namespace v2rayN.Handler } else { - transport.type = "http"; + transport.type = nameof(ETransport.http); transport.host = Utile.IsNullOrEmpty(node.requestHost) ? null : Utile.String2List(node.requestHost); transport.path = Utile.IsNullOrEmpty(node.path) ? null : node.path; } @@ -433,8 +433,8 @@ namespace v2rayN.Handler break; - case "ws": - transport.type = "ws"; + case nameof(ETransport.ws): + transport.type = nameof(ETransport.ws); transport.path = Utile.IsNullOrEmpty(node.path) ? null : node.path; if (!Utile.IsNullOrEmpty(node.requestHost)) { @@ -445,12 +445,12 @@ namespace v2rayN.Handler } break; - case "quic": - transport.type = "quic"; + case nameof(ETransport.quic): + transport.type = nameof(ETransport.quic); break; - case "grpc": - transport.type = "grpc"; + case nameof(ETransport.grpc): + transport.type = nameof(ETransport.grpc); transport.service_name = node.path; transport.idle_timeout = _config.grpcItem.idle_timeout.ToString("##s"); transport.ping_timeout = _config.grpcItem.health_check_timeout.ToString("##s"); @@ -881,7 +881,7 @@ namespace v2rayN.Handler singboxConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts. singboxConfig.outbounds.RemoveAt(0); - int httpPort = LazyConfig.Instance.GetLocalPort("speedtest"); + int httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.speedtest); foreach (var it in selecteds) { diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 54f1a270..6d457798 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -111,11 +111,11 @@ namespace v2rayN.Handler { if (_config.inbound[0].newPort4LAN) { - Inbounds4Ray inbound3 = GetInbound(_config.inbound[0], Global.InboundSocks2, 2, true); + var inbound3 = GetInbound(_config.inbound[0], Global.InboundSocks2, 2, true); inbound3.listen = "0.0.0.0"; v2rayConfig.inbounds.Add(inbound3); - Inbounds4Ray inbound4 = GetInbound(_config.inbound[0], Global.InboundHttp2, 3, false); + var inbound4 = GetInbound(_config.inbound[0], Global.InboundHttp2, 3, false); inbound4.listen = "0.0.0.0"; v2rayConfig.inbounds.Add(inbound4); @@ -564,7 +564,7 @@ namespace v2rayN.Handler //streamSettings switch (node.GetNetwork()) { - case "kcp": + case nameof(ETransport.kcp): KcpSettings4Ray kcpSettings = new() { mtu = _config.kcpItem.mtu, @@ -588,7 +588,7 @@ namespace v2rayN.Handler streamSettings.kcpSettings = kcpSettings; break; //ws - case "ws": + case nameof(ETransport.ws): WsSettings4Ray wsSettings = new(); wsSettings.headers = new Headers4Ray(); string path = node.path; @@ -608,7 +608,7 @@ namespace v2rayN.Handler break; //h2 - case "h2": + case nameof(ETransport.h2): HttpSettings4Ray httpSettings = new(); if (!string.IsNullOrWhiteSpace(host)) @@ -621,7 +621,7 @@ namespace v2rayN.Handler break; //quic - case "quic": + case nameof(ETransport.quic): QuicSettings4Ray quicsettings = new() { security = host, @@ -645,7 +645,7 @@ namespace v2rayN.Handler } break; - case "grpc": + case nameof(ETransport.grpc): GrpcSettings4Ray grpcSettings = new() { serviceName = node.path, @@ -923,7 +923,7 @@ namespace v2rayN.Handler v2rayConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts. v2rayConfig.outbounds.RemoveAt(0); - int httpPort = LazyConfig.Instance.GetLocalPort("speedtest"); + int httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.speedtest); foreach (var it in selecteds) { diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index 67000d65..9162c86c 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -4,6 +4,7 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Sockets; +using v2rayN.Model; using v2rayN.Resx; namespace v2rayN.Handler @@ -311,7 +312,7 @@ namespace v2rayN.Handler { return null; } - var httpPort = LazyConfig.Instance.GetLocalPort(Global.InboundHttp); + var httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.http); if (!SocketCheck(Global.Loopback, httpPort)) { return null; diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index 2cc46fac..1b13a035 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(GetLocalPort(Global.InboundAPITagName)); + _statePort = Utile.GetFreePort(GetLocalPort(EInboundProtocol.api)); } return _statePort.Value; @@ -50,20 +50,10 @@ namespace v2rayN.Handler return _config; } - public int GetLocalPort(string protocol) + public int GetLocalPort(EInboundProtocol protocol) { - 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, - }; + var localPort = _config.inbound.FirstOrDefault(t => t.protocol == nameof(EInboundProtocol.socks))?.localPort ?? 10808; + return localPort + (int)protocol; } public void AddProcess(IntPtr processHandle) diff --git a/v2rayN/v2rayN/Handler/ShareHandler.cs b/v2rayN/v2rayN/Handler/ShareHandler.cs index 3704cce6..4a240d11 100644 --- a/v2rayN/v2rayN/Handler/ShareHandler.cs +++ b/v2rayN/v2rayN/Handler/ShareHandler.cs @@ -310,11 +310,11 @@ namespace v2rayN.Handler dicQuery.Add("spx", Utile.UrlEncode(item.spiderX)); } - dicQuery.Add("type", !Utile.IsNullOrEmpty(item.network) ? item.network : "tcp"); + dicQuery.Add("type", !Utile.IsNullOrEmpty(item.network) ? item.network : nameof(ETransport.tcp)); switch (item.network) { - case "tcp": + case nameof(ETransport.tcp): dicQuery.Add("headerType", !Utile.IsNullOrEmpty(item.headerType) ? item.headerType : Global.None); if (!Utile.IsNullOrEmpty(item.requestHost)) { @@ -322,7 +322,7 @@ namespace v2rayN.Handler } break; - case "kcp": + case nameof(ETransport.kcp): dicQuery.Add("headerType", !Utile.IsNullOrEmpty(item.headerType) ? item.headerType : Global.None); if (!Utile.IsNullOrEmpty(item.path)) { @@ -330,7 +330,7 @@ namespace v2rayN.Handler } break; - case "ws": + case nameof(ETransport.ws): if (!Utile.IsNullOrEmpty(item.requestHost)) { dicQuery.Add("host", Utile.UrlEncode(item.requestHost)); @@ -341,9 +341,9 @@ namespace v2rayN.Handler } break; - case "http": - case "h2": - dicQuery["type"] = "http"; + case nameof(ETransport.http): + case nameof(ETransport.h2): + dicQuery["type"] = nameof(ETransport.http); if (!Utile.IsNullOrEmpty(item.requestHost)) { dicQuery.Add("host", Utile.UrlEncode(item.requestHost)); @@ -354,13 +354,13 @@ namespace v2rayN.Handler } break; - case "quic": + case nameof(ETransport.quic): dicQuery.Add("headerType", !Utile.IsNullOrEmpty(item.headerType) ? item.headerType : Global.None); dicQuery.Add("quicSecurity", Utile.UrlEncode(item.requestHost)); dicQuery.Add("key", Utile.UrlEncode(item.path)); break; - case "grpc": + case nameof(ETransport.grpc): if (!Utile.IsNullOrEmpty(item.path)) { dicQuery.Add("serviceName", Utile.UrlEncode(item.path)); @@ -599,32 +599,32 @@ namespace v2rayN.Handler i.network = m.Groups["network"].Value; switch (i.network) { - case "tcp": + case nameof(ETransport.tcp): string t1 = query["type"] ?? Global.None; i.headerType = t1; break; - case "kcp": + case nameof(ETransport.kcp): i.headerType = query["type"] ?? Global.None; break; - case "ws": + case nameof(ETransport.ws): string p1 = query["path"] ?? "/"; string h1 = query["host"] ?? ""; i.requestHost = Utile.UrlDecode(h1); i.path = p1; break; - case "http": - case "h2": - i.network = "h2"; + case nameof(ETransport.http): + case nameof(ETransport.h2): + i.network = nameof(ETransport.h2); string p2 = query["path"] ?? "/"; string h2 = query["host"] ?? ""; i.requestHost = Utile.UrlDecode(h2); i.path = p2; break; - case "quic": + case nameof(ETransport.quic): string s = query["security"] ?? Global.None; string k = query["key"] ?? ""; string t3 = query["type"] ?? Global.None; @@ -945,39 +945,39 @@ namespace v2rayN.Handler item.shortId = Utile.UrlDecode(query["sid"] ?? ""); item.spiderX = Utile.UrlDecode(query["spx"] ?? ""); - item.network = query["type"] ?? "tcp"; + item.network = query["type"] ?? nameof(ETransport.tcp); switch (item.network) { - case "tcp": + case nameof(ETransport.tcp): item.headerType = query["headerType"] ?? Global.None; item.requestHost = Utile.UrlDecode(query["host"] ?? ""); break; - case "kcp": + case nameof(ETransport.kcp): item.headerType = query["headerType"] ?? Global.None; item.path = Utile.UrlDecode(query["seed"] ?? ""); break; - case "ws": + case nameof(ETransport.ws): item.requestHost = Utile.UrlDecode(query["host"] ?? ""); item.path = Utile.UrlDecode(query["path"] ?? "/"); break; - case "http": - case "h2": - item.network = "h2"; + case nameof(ETransport.http): + case nameof(ETransport.h2): + item.network = nameof(ETransport.h2); item.requestHost = Utile.UrlDecode(query["host"] ?? ""); item.path = Utile.UrlDecode(query["path"] ?? "/"); break; - case "quic": + case nameof(ETransport.quic): item.headerType = query["headerType"] ?? Global.None; item.requestHost = query["quicSecurity"] ?? Global.None; item.path = Utile.UrlDecode(query["key"] ?? ""); break; - case "grpc": + case nameof(ETransport.grpc): item.path = Utile.UrlDecode(query["serviceName"] ?? ""); item.headerType = Utile.UrlDecode(query["mode"] ?? Global.GrpcGunMode); break; diff --git a/v2rayN/v2rayN/Handler/SysProxyHandle.cs b/v2rayN/v2rayN/Handler/SysProxyHandle.cs index 5b2c2507..f71b0f68 100644 --- a/v2rayN/v2rayN/Handler/SysProxyHandle.cs +++ b/v2rayN/v2rayN/Handler/SysProxyHandle.cs @@ -41,9 +41,9 @@ namespace v2rayN.Handler try { - int port = LazyConfig.Instance.GetLocalPort(Global.InboundHttp); - int portSocks = LazyConfig.Instance.GetLocalPort(Global.InboundSocks); - int portPac = LazyConfig.Instance.GetLocalPort(ESysProxyType.Pac.ToString()); + int port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.http); + int portSocks = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks); + int portPac = LazyConfig.Instance.GetLocalPort(EInboundProtocol.pac); if (port <= 0) { return false; diff --git a/v2rayN/v2rayN/Model/EInboundProtocol.cs b/v2rayN/v2rayN/Model/EInboundProtocol.cs new file mode 100644 index 00000000..7e322b8e --- /dev/null +++ b/v2rayN/v2rayN/Model/EInboundProtocol.cs @@ -0,0 +1,13 @@ +namespace v2rayN.Model +{ + public enum EInboundProtocol + { + socks = 0, + http, + socks2, + http2, + pac, + api = 11, + speedtest = 21 + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN/Model/ETransport.cs b/v2rayN/v2rayN/Model/ETransport.cs new file mode 100644 index 00000000..0351aa52 --- /dev/null +++ b/v2rayN/v2rayN/Model/ETransport.cs @@ -0,0 +1,13 @@ +namespace v2rayN.Model +{ + public enum ETransport + { + tcp, + kcp, + ws, + h2, + http, + quic, + grpc + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index ea6d2e04..a17079c1 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -1811,7 +1811,7 @@ namespace v2rayN.ViewModels public void InboundDisplayStaus() { StringBuilder sb = new(); - sb.Append($"[{Global.InboundSocks}:{LazyConfig.Instance.GetLocalPort(Global.InboundSocks)}]"); + sb.Append($"[{Global.InboundSocks}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)}]"); sb.Append(" | "); //if (_config.sysProxyType == ESysProxyType.ForcedChange) //{ @@ -1819,7 +1819,7 @@ namespace v2rayN.ViewModels //} //else //{ - sb.Append($"[{Global.InboundHttp}:{LazyConfig.Instance.GetLocalPort(Global.InboundHttp)}]"); + sb.Append($"[{Global.InboundHttp}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.http)}]"); //} InboundDisplay = $"{ResUI.LabLocal}:{sb}"; @@ -1828,9 +1828,9 @@ namespace v2rayN.ViewModels if (_config.inbound[0].newPort4LAN) { StringBuilder sb2 = new(); - sb2.Append($"[{Global.InboundSocks}:{LazyConfig.Instance.GetLocalPort(Global.InboundSocks2)}]"); + sb2.Append($"[{Global.InboundSocks}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks2)}]"); sb2.Append(" | "); - sb2.Append($"[{Global.InboundHttp}:{LazyConfig.Instance.GetLocalPort(Global.InboundHttp2)}]"); + sb2.Append($"[{Global.InboundHttp}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.http2)}]"); InboundLanDisplay = $"{ResUI.LabLAN}:{sb2}"; } else diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs index 07107a0a..c9c5c8ed 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs @@ -284,7 +284,7 @@ namespace v2rayN.Views cmbHeaderType.Items.Add(Global.None); cmbHeaderType.Items.Add(Global.TcpHeaderHttp); } - else if (network is "kcp" or "quic") + else if (network is nameof(ETransport.kcp) or nameof(ETransport.quic)) { cmbHeaderType.Items.Add(Global.None); Global.KcpHeaderTypes.ForEach(it => @@ -292,7 +292,7 @@ namespace v2rayN.Views cmbHeaderType.Items.Add(it); }); } - else if (network == "grpc") + else if (network == nameof(ETransport.grpc)) { cmbHeaderType.Items.Add(Global.GrpcGunMode); cmbHeaderType.Items.Add(Global.GrpcMultiMode); @@ -318,33 +318,33 @@ namespace v2rayN.Views switch (network) { - case Global.DefaultNetwork: + case nameof(ETransport.tcp): tipRequestHost.Text = ResUI.TransportRequestHostTip1; tipHeaderType.Text = ResUI.TransportHeaderTypeTip1; break; - case "kcp": + case nameof(ETransport.kcp): tipHeaderType.Text = ResUI.TransportHeaderTypeTip2; tipPath.Text = ResUI.TransportPathTip5; break; - case "ws": + case nameof(ETransport.ws): tipRequestHost.Text = ResUI.TransportRequestHostTip2; tipPath.Text = ResUI.TransportPathTip1; break; - case "h2": + case nameof(ETransport.h2): tipRequestHost.Text = ResUI.TransportRequestHostTip3; tipPath.Text = ResUI.TransportPathTip2; break; - case "quic": + case nameof(ETransport.quic): tipRequestHost.Text = ResUI.TransportRequestHostTip4; tipPath.Text = ResUI.TransportPathTip3; tipHeaderType.Text = ResUI.TransportHeaderTypeTip3; break; - case "grpc": + case nameof(ETransport.grpc): tipPath.Text = ResUI.TransportPathTip4; tipHeaderType.Text = ResUI.TransportHeaderTypeTip4; labHeaderType.Visibility = Visibility.Hidden;