From 70584aff9d69f86685e84c094321e871b0de992f Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 9 Mar 2024 10:12:45 +0800 Subject: [PATCH] Refactor --- v2rayN/v2rayN/Global.cs | 4 ---- v2rayN/v2rayN/Handler/ConfigHandler.cs | 4 ++-- v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 22 +++++++++---------- v2rayN/v2rayN/Handler/CoreConfigV2ray.cs | 20 ++++++++--------- .../v2rayN/ViewModels/MainWindowViewModel.cs | 8 +++---- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 690d2985..e987cc9f 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -56,10 +56,6 @@ namespace v2rayN public const string BlockTag = "block"; public const string StreamSecurity = "tls"; public const string StreamSecurityReality = "reality"; - public const string InboundSocks = "socks"; - public const string InboundHttp = "http"; - public const string InboundSocks2 = "socks2"; - public const string InboundHttp2 = "http2"; public const string Loopback = "127.0.0.1"; public const string InboundAPITagName = "api"; public const string InboundAPIProtocol = "dokodemo-door"; diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 76148041..88e8dfa6 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -61,7 +61,7 @@ namespace v2rayN.Handler config.inbound = new List(); InItem inItem = new() { - protocol = Global.InboundSocks, + protocol = EInboundProtocol.socks.ToString(), localPort = 10808, udpEnabled = true, sniffingEnabled = true, @@ -75,7 +75,7 @@ namespace v2rayN.Handler { if (config.inbound.Count > 0) { - config.inbound[0].protocol = Global.InboundSocks; + config.inbound[0].protocol = EInboundProtocol.socks.ToString(); } } if (config.routingBasicItem == null) diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index f726e754..f59a0204 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -115,8 +115,8 @@ namespace v2rayN.Handler { var inbound = new Inbound4Sbox() { - type = Global.InboundSocks, - tag = Global.InboundSocks, + type = EInboundProtocol.socks.ToString(), + tag = EInboundProtocol.socks.ToString(), listen = Global.Loopback, }; singboxConfig.inbounds.Add(inbound); @@ -136,18 +136,18 @@ namespace v2rayN.Handler } //http - var inbound2 = GetInbound(inbound, Global.InboundHttp, 1, false); + var inbound2 = GetInbound(inbound, EInboundProtocol.http, false); singboxConfig.inbounds.Add(inbound2); if (_config.inbound[0].allowLANConn) { if (_config.inbound[0].newPort4LAN) { - var inbound3 = GetInbound(inbound, Global.InboundSocks2, 2, true); + var inbound3 = GetInbound(inbound, EInboundProtocol.socks2, true); inbound3.listen = "::"; singboxConfig.inbounds.Add(inbound3); - var inbound4 = GetInbound(inbound, Global.InboundHttp2, 3, false); + var inbound4 = GetInbound(inbound, EInboundProtocol.http2, false); inbound4.listen = "::"; singboxConfig.inbounds.Add(inbound4); @@ -198,12 +198,12 @@ namespace v2rayN.Handler return 0; } - private Inbound4Sbox GetInbound(Inbound4Sbox inItem, string tag, int offset, bool bSocks) + private Inbound4Sbox GetInbound(Inbound4Sbox inItem, EInboundProtocol protocol, bool bSocks) { var inbound = JsonUtile.DeepCopy(inItem); - inbound.tag = tag; - inbound.listen_port = inItem.listen_port + offset; - inbound.type = bSocks ? Global.InboundSocks : Global.InboundHttp; + inbound.tag = protocol.ToString(); + inbound.listen_port = inItem.listen_port + (int)protocol; + inbound.type = bSocks ? EInboundProtocol.socks.ToString() : EInboundProtocol.http.ToString(); return inbound; } @@ -933,9 +933,9 @@ namespace v2rayN.Handler { listen = Global.Loopback, listen_port = port, - type = Global.InboundHttp + type = EInboundProtocol.http.ToString(), }; - inbound.tag = Global.InboundHttp + inbound.listen_port.ToString(); + inbound.tag = inbound.type + inbound.listen_port.ToString(); singboxConfig.inbounds.Add(inbound); //outbound diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 6d457798..1a572754 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -100,22 +100,22 @@ namespace v2rayN.Handler { v2rayConfig.inbounds = new List(); - Inbounds4Ray? inbound = GetInbound(_config.inbound[0], Global.InboundSocks, 0, true); + Inbounds4Ray? inbound = GetInbound(_config.inbound[0], EInboundProtocol.socks, true); v2rayConfig.inbounds.Add(inbound); //http - Inbounds4Ray? inbound2 = GetInbound(_config.inbound[0], Global.InboundHttp, 1, false); + Inbounds4Ray? inbound2 = GetInbound(_config.inbound[0], EInboundProtocol.http, false); v2rayConfig.inbounds.Add(inbound2); if (_config.inbound[0].allowLANConn) { if (_config.inbound[0].newPort4LAN) { - var inbound3 = GetInbound(_config.inbound[0], Global.InboundSocks2, 2, true); + var inbound3 = GetInbound(_config.inbound[0], EInboundProtocol.socks2, true); inbound3.listen = "0.0.0.0"; v2rayConfig.inbounds.Add(inbound3); - var inbound4 = GetInbound(_config.inbound[0], Global.InboundHttp2, 3, false); + var inbound4 = GetInbound(_config.inbound[0], EInboundProtocol.http2, false); inbound4.listen = "0.0.0.0"; v2rayConfig.inbounds.Add(inbound4); @@ -143,7 +143,7 @@ namespace v2rayN.Handler return 0; } - private Inbounds4Ray? GetInbound(InItem inItem, string tag, int offset, bool bSocks) + private Inbounds4Ray? GetInbound(InItem inItem, EInboundProtocol protocol, bool bSocks) { string result = Utile.GetEmbedText(Global.V2raySampleInbound); if (Utile.IsNullOrEmpty(result)) @@ -156,9 +156,9 @@ namespace v2rayN.Handler { return null; } - inbound.tag = tag; - inbound.port = inItem.localPort + offset; - inbound.protocol = bSocks ? Global.InboundSocks : Global.InboundHttp; + inbound.tag = protocol.ToString(); + inbound.port = inItem.localPort + (int)protocol; + inbound.protocol = bSocks ? EInboundProtocol.socks.ToString() : EInboundProtocol.http.ToString(); inbound.settings.udp = inItem.udpEnabled; inbound.sniffing.enabled = inItem.sniffingEnabled; inbound.sniffing.routeOnly = inItem.routeOnly; @@ -975,9 +975,9 @@ namespace v2rayN.Handler { listen = Global.Loopback, port = port, - protocol = Global.InboundHttp + protocol = EInboundProtocol.http.ToString(), }; - inbound.tag = Global.InboundHttp + inbound.port.ToString(); + inbound.tag = inbound.protocol + inbound.port.ToString(); v2rayConfig.inbounds.Add(inbound); //outbound diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index a17079c1..cb2714da 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(EInboundProtocol.socks)}]"); + sb.Append($"[{EInboundProtocol.socks}:{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(EInboundProtocol.http)}]"); + sb.Append($"[{EInboundProtocol.http}:{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(EInboundProtocol.socks2)}]"); + sb2.Append($"[{EInboundProtocol.socks}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks2)}]"); sb2.Append(" | "); - sb2.Append($"[{Global.InboundHttp}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.http2)}]"); + sb2.Append($"[{EInboundProtocol.http}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.http2)}]"); InboundLanDisplay = $"{ResUI.LabLAN}:{sb2}"; } else