From f5deb8e168639a1edd2ec3d1dcb4d2ff8aaa4dd1 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 19 Dec 2023 18:09:11 +0800 Subject: [PATCH] Code clean --- v2rayN/v2rayN/Global.cs | 52 ++++++++++++---------- v2rayN/v2rayN/Handler/ConfigHandler.cs | 2 +- v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 14 +++--- v2rayN/v2rayN/Handler/CoreConfigV2ray.cs | 12 ++--- v2rayN/v2rayN/Handler/ShareHandler.cs | 34 +++++++------- v2rayN/v2rayN/Handler/StatisticsV2ray.cs | 2 +- v2rayN/v2rayN/Handler/SysProxyHandle.cs | 2 +- v2rayN/v2rayN/Handler/UpdateHandle.cs | 2 +- 8 files changed, 63 insertions(+), 57 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index de7404d7..923cdea0 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -1,4 +1,6 @@ -namespace v2rayN +using v2rayN.Mode; + +namespace v2rayN { internal class Global { @@ -30,18 +32,15 @@ public const string ConfigDB = "guiNDB.db"; public const string CoreConfigFileName = "config.json"; public const string CorePreConfigFileName = "configPre.json"; - public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig"; public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig"; public const string V2raySampleHttprequestFileName = "v2rayN.Sample.SampleHttprequest"; public const string V2raySampleHttpresponseFileName = "v2rayN.Sample.SampleHttpresponse"; public const string V2raySampleInbound = "v2rayN.Sample.SampleInbound"; public const string CustomRoutingFileName = "v2rayN.Sample.custom_routing_"; - public const string TunSingboxDNSFileName = "v2rayN.Sample.tun_singbox_dns"; public const string TunSingboxInboundFileName = "v2rayN.Sample.tun_singbox_inbound"; public const string TunSingboxRulesFileName = "v2rayN.Sample.tun_singbox_rules"; - public const string DNSV2rayNormalFileName = "v2rayN.Sample.dns_v2ray_normal"; public const string DNSSingboxNormalFileName = "v2rayN.Sample.dns_singbox_normal"; @@ -61,24 +60,8 @@ public const string Loopback = "127.0.0.1"; public const string InboundAPITagName = "api"; public const string InboundAPIProtocal = "dokodemo-door"; - - public const string vmessProtocol = "vmess://"; - public const string vmessProtocolLite = "vmess"; - public const string ssProtocol = "ss://"; - public const string ssProtocolLite = "shadowsocks"; - public const string socksProtocol = "socks://"; - public const string socksProtocolLite = "socks"; - public const string httpProtocol = "http://"; - public const string httpsProtocol = "https://"; - public const string vlessProtocol = "vless://"; - public const string vlessProtocolLite = "vless"; - public const string trojanProtocol = "trojan://"; - public const string trojanProtocolLite = "trojan"; - public const string hysteria2Protocol = "hysteria2://"; - public const string hysteria2Protocol2 = "hy2://"; - public const string hysteria2ProtocolLite = "hysteria2"; - public const string tuicProtocol = "tuic://"; - public const string tuicProtocolLite = "tuic"; + public const string HttpProtocol = "http://"; + public const string HttpsProtocol = "https://"; public const string UserEMail = "t@t.tt"; public const string MyRegPath = "Software\\v2rayNGUI"; @@ -132,7 +115,7 @@ @"http://cachefly.cachefly.net/10mb.test" }; - public static readonly Dictionary UserAgentTxt = new() + public static readonly Dictionary UserAgentTxts = new() { {"chrome","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" }, {"firefox","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0" }, @@ -141,6 +124,29 @@ {"none",""} }; + public const string Hysteria2ProtocolShare = "hy2://"; + public static readonly Dictionary ProtocolShares = new() + { + {EConfigType.VMess,"vmess://"}, + {EConfigType.Shadowsocks,"ss://"}, + {EConfigType.Socks,"socks://"}, + {EConfigType.VLESS,"vless://"}, + {EConfigType.Trojan,"trojan://"}, + {EConfigType.Hysteria2,"hysteria2://"}, + {EConfigType.Tuic,"tuic://"} + }; + + public static readonly Dictionary ProtocolTypes = new() + { + {EConfigType.VMess,"vmess"}, + {EConfigType.Shadowsocks,"shadowsocks"}, + {EConfigType.Socks,"socks"}, + {EConfigType.VLESS,"vless"}, + {EConfigType.Trojan,"trojan"}, + {EConfigType.Hysteria2,"hysteria2"}, + {EConfigType.Tuic,"tuic"} + }; + public static readonly List VmessSecuritys = new() { "aes-128-gcm", "chacha20-poly1305", "auto", "none", "zero" }; public static readonly List SsSecuritys = new() { "aes-256-gcm", "aes-128-gcm", "chacha20-poly1305", "chacha20-ietf-poly1305", "none", "plain" }; public static readonly List SsSecuritysInSagerNet = new() { "none", "2022-blake3-aes-128-gcm", "2022-blake3-aes-256-gcm", "2022-blake3-chacha20-poly1305", "aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305", "rc4", "rc4-md5", "aes-128-ctr", "aes-192-ctr", "aes-256-ctr", "aes-128-cfb", "aes-192-cfb", "aes-256-cfb", "aes-128-cfb8", "aes-192-cfb8", "aes-256-cfb8", "aes-128-ofb", "aes-192-ofb", "aes-256-ofb", "bf-cfb", "cast5-cfb", "des-cfb", "idea-cfb", "rc2-cfb", "seed-cfb", "camellia-128-cfb", "camellia-192-cfb", "camellia-256-cfb", "camellia-128-cfb8", "camellia-192-cfb8", "camellia-256-cfb8", "salsa20", "chacha20", "chacha20-ietf", "xchacha20" }; diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 37117951..eccc8263 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -1051,7 +1051,7 @@ namespace v2rayN.Handler foreach (string str in arrData) { //maybe sub - if (!isSub && (str.StartsWith(Global.httpsProtocol) || str.StartsWith(Global.httpProtocol))) + if (!isSub && (str.StartsWith(Global.HttpsProtocol) || str.StartsWith(Global.HttpProtocol))) { if (AddSubItem(ref config, str) == 0) { diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 4f549e6f..dcc1dea2 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -222,7 +222,7 @@ namespace v2rayN.Handler if (node.configType == EConfigType.VMess) { - outbound.type = Global.vmessProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.VMess]; outbound.uuid = node.id; outbound.alter_id = node.alterId; @@ -239,7 +239,7 @@ namespace v2rayN.Handler } else if (node.configType == EConfigType.Shadowsocks) { - outbound.type = Global.ssProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.Shadowsocks]; outbound.method = LazyConfig.Instance.GetShadowsocksSecuritys(node).Contains(node.security) ? node.security : "none"; outbound.password = node.id; @@ -248,7 +248,7 @@ namespace v2rayN.Handler } else if (node.configType == EConfigType.Socks) { - outbound.type = Global.socksProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.Socks]; outbound.version = "5"; if (!Utils.IsNullOrEmpty(node.security) @@ -260,7 +260,7 @@ namespace v2rayN.Handler } else if (node.configType == EConfigType.VLESS) { - outbound.type = Global.vlessProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.VLESS]; outbound.uuid = node.id; @@ -277,7 +277,7 @@ namespace v2rayN.Handler } else if (node.configType == EConfigType.Trojan) { - outbound.type = Global.trojanProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.Trojan]; outbound.password = node.id; @@ -285,7 +285,7 @@ namespace v2rayN.Handler } else if (node.configType == EConfigType.Hysteria2) { - outbound.type = Global.hysteria2ProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.Hysteria2]; outbound.password = node.id; @@ -296,7 +296,7 @@ namespace v2rayN.Handler } else if (node.configType == EConfigType.Tuic) { - outbound.type = Global.tuicProtocolLite; + outbound.type = Global.ProtocolTypes[EConfigType.Tuic]; outbound.uuid = node.id; outbound.password = node.security; diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 1c0bd4a3..65083081 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -336,7 +336,7 @@ namespace v2rayN.Handler outboundMux(node, outbound, _config.coreBasicItem.muxEnabled); - outbound.protocol = Global.vmessProtocolLite; + outbound.protocol = Global.ProtocolTypes[EConfigType.VMess]; outbound.settings.servers = null; } else if (node.configType == EConfigType.Shadowsocks) @@ -361,7 +361,7 @@ namespace v2rayN.Handler outboundMux(node, outbound, false); - outbound.protocol = Global.ssProtocolLite; + outbound.protocol = Global.ProtocolTypes[EConfigType.Shadowsocks]; outbound.settings.vnext = null; } else if (node.configType == EConfigType.Socks) @@ -396,7 +396,7 @@ namespace v2rayN.Handler outboundMux(node, outbound, false); - outbound.protocol = Global.socksProtocolLite; + outbound.protocol = Global.ProtocolTypes[EConfigType.Socks]; outbound.settings.vnext = null; } else if (node.configType == EConfigType.VLESS) @@ -445,7 +445,7 @@ namespace v2rayN.Handler outboundMux(node, outbound, _config.coreBasicItem.muxEnabled); } - outbound.protocol = Global.vlessProtocolLite; + outbound.protocol = Global.ProtocolTypes[EConfigType.VLESS]; outbound.settings.servers = null; } else if (node.configType == EConfigType.Trojan) @@ -469,7 +469,7 @@ namespace v2rayN.Handler outboundMux(node, outbound, false); - outbound.protocol = Global.trojanProtocolLite; + outbound.protocol = Global.ProtocolTypes[EConfigType.Trojan]; outbound.settings.vnext = null; } boundStreamSettings(node, outbound.streamSettings); @@ -515,7 +515,7 @@ namespace v2rayN.Handler { try { - useragent = Global.UserAgentTxt[_config.coreBasicItem.defUserAgent]; + useragent = Global.UserAgentTxts[_config.coreBasicItem.defUserAgent]; } catch (KeyNotFoundException) { diff --git a/v2rayN/v2rayN/Handler/ShareHandler.cs b/v2rayN/v2rayN/Handler/ShareHandler.cs index 920b498f..9ac2ae05 100644 --- a/v2rayN/v2rayN/Handler/ShareHandler.cs +++ b/v2rayN/v2rayN/Handler/ShareHandler.cs @@ -68,7 +68,7 @@ namespace v2rayN.Handler url = Utils.ToJson(vmessQRCode); url = Utils.Base64Encode(url); - url = $"{Global.vmessProtocol}{url}"; + url = $"{Global.ProtocolShares[EConfigType.VMess]}{url}"; return url; } @@ -91,7 +91,7 @@ namespace v2rayN.Handler //new Sip002 var pw = Utils.Base64Encode($"{item.security}:{item.id}"); url = $"{pw}@{GetIpv6(item.address)}:{item.port}"; - url = $"{Global.ssProtocol}{url}{remark}"; + url = $"{Global.ProtocolShares[EConfigType.Shadowsocks]}{url}{remark}"; return url; } @@ -112,7 +112,7 @@ namespace v2rayN.Handler //new var pw = Utils.Base64Encode($"{item.security}:{item.id}"); url = $"{pw}@{GetIpv6(item.address)}:{item.port}"; - url = $"{Global.socksProtocol}{url}{remark}"; + url = $"{Global.ProtocolShares[EConfigType.Socks]}{url}{remark}"; return url; } @@ -132,7 +132,7 @@ namespace v2rayN.Handler item.id, GetIpv6(item.address), item.port); - url = $"{Global.trojanProtocol}{url}{query}{remark}"; + url = $"{Global.ProtocolShares[EConfigType.Trojan]}{url}{query}{remark}"; return url; } @@ -160,7 +160,7 @@ namespace v2rayN.Handler item.id, GetIpv6(item.address), item.port); - url = $"{Global.vlessProtocol}{url}{query}{remark}"; + url = $"{Global.ProtocolShares[EConfigType.VLESS]}{url}{query}{remark}"; return url; } @@ -189,7 +189,7 @@ namespace v2rayN.Handler item.id, GetIpv6(item.address), item.port); - url = $"{Global.hysteria2Protocol}{url}{query}{remark}"; + url = $"{Global.ProtocolShares[EConfigType.Hysteria2]}{url}{query}{remark}"; return url; } @@ -218,7 +218,7 @@ namespace v2rayN.Handler $"{item.id}:{item.security}", GetIpv6(item.address), item.port); - url = $"{Global.tuicProtocol}{url}{query}{remark}"; + url = $"{Global.ProtocolShares[EConfigType.Tuic]}{url}{query}{remark}"; return url; } @@ -359,7 +359,7 @@ namespace v2rayN.Handler return null; } - if (result.StartsWith(Global.vmessProtocol)) + if (result.StartsWith(Global.ProtocolShares[EConfigType.VMess])) { int indexSplit = result.IndexOf("?"); if (indexSplit > 0) @@ -371,7 +371,7 @@ namespace v2rayN.Handler profileItem = ResolveVmess(result, out msg); } } - else if (result.StartsWith(Global.ssProtocol)) + else if (result.StartsWith(Global.ProtocolShares[EConfigType.Shadowsocks])) { msg = ResUI.ConfigurationFormatIncorrect; @@ -387,7 +387,7 @@ namespace v2rayN.Handler profileItem.configType = EConfigType.Shadowsocks; } - else if (result.StartsWith(Global.socksProtocol)) + else if (result.StartsWith(Global.ProtocolShares[EConfigType.Socks])) { msg = ResUI.ConfigurationFormatIncorrect; @@ -403,23 +403,23 @@ namespace v2rayN.Handler profileItem.configType = EConfigType.Socks; } - else if (result.StartsWith(Global.trojanProtocol)) + else if (result.StartsWith(Global.ProtocolShares[EConfigType.Trojan])) { msg = ResUI.ConfigurationFormatIncorrect; profileItem = ResolveTrojan(result); } - else if (result.StartsWith(Global.vlessProtocol)) + else if (result.StartsWith(Global.ProtocolShares[EConfigType.VLESS])) { profileItem = ResolveStdVLESS(result); } - else if (result.StartsWith(Global.hysteria2Protocol) || result.StartsWith(Global.hysteria2Protocol2)) + else if (result.StartsWith(Global.ProtocolShares[EConfigType.Hysteria2]) || result.StartsWith(Global.Hysteria2ProtocolShare)) { msg = ResUI.ConfigurationFormatIncorrect; profileItem = ResolveHysteria2(result); } - else if (result.StartsWith(Global.tuicProtocol)) + else if (result.StartsWith(Global.ProtocolShares[EConfigType.Tuic])) { profileItem = ResolveTuic(result); } @@ -447,7 +447,7 @@ namespace v2rayN.Handler configType = EConfigType.VMess }; - result = result[Global.vmessProtocol.Length..]; + result = result[Global.ProtocolShares[EConfigType.VMess].Length..]; result = Utils.Base64Decode(result); //转成Json @@ -495,7 +495,7 @@ namespace v2rayN.Handler { configType = EConfigType.VMess }; - result = result[Global.vmessProtocol.Length..]; + result = result[Global.ProtocolShares[EConfigType.VMess].Length..]; int indexSplit = result.IndexOf("?"); if (indexSplit > 0) { @@ -712,7 +712,7 @@ namespace v2rayN.Handler { configType = EConfigType.Socks }; - result = result[Global.socksProtocol.Length..]; + result = result[Global.ProtocolShares[EConfigType.Socks].Length..]; //remark int indexRemark = result.IndexOf("#"); if (indexRemark > 0) diff --git a/v2rayN/v2rayN/Handler/StatisticsV2ray.cs b/v2rayN/v2rayN/Handler/StatisticsV2ray.cs index 788ba45a..d2af9f2b 100644 --- a/v2rayN/v2rayN/Handler/StatisticsV2ray.cs +++ b/v2rayN/v2rayN/Handler/StatisticsV2ray.cs @@ -28,7 +28,7 @@ namespace v2rayN.Handler { if (_channel == null) { - _channel = GrpcChannel.ForAddress($"{Global.httpProtocol}{Global.Loopback}:{Global.StatePort}"); + _channel = GrpcChannel.ForAddress($"{Global.HttpProtocol}{Global.Loopback}:{Global.StatePort}"); _client = new StatsService.StatsServiceClient(_channel); } } diff --git a/v2rayN/v2rayN/Handler/SysProxyHandle.cs b/v2rayN/v2rayN/Handler/SysProxyHandle.cs index fcf09db7..ae1b66cf 100644 --- a/v2rayN/v2rayN/Handler/SysProxyHandle.cs +++ b/v2rayN/v2rayN/Handler/SysProxyHandle.cs @@ -76,7 +76,7 @@ namespace v2rayN.Handler else if (type == ESysProxyType.Pac) { PacHandler.Start(Utils.GetConfigPath(), port, portPac); - var strProxy = $"{Global.httpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; + var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; ProxySetting.SetProxy(strProxy, "", 4); // use pac script url for auto-config proxy } diff --git a/v2rayN/v2rayN/Handler/UpdateHandle.cs b/v2rayN/v2rayN/Handler/UpdateHandle.cs index a3ff722c..2ec8b5cb 100644 --- a/v2rayN/v2rayN/Handler/UpdateHandle.cs +++ b/v2rayN/v2rayN/Handler/UpdateHandle.cs @@ -180,7 +180,7 @@ namespace v2rayN.Handler //_updateFunc(false, $"{hashCode}{ResUI.MsgNoValidSubscription}"); continue; } - if (!url.StartsWith(Global.httpsProtocol) && !url.StartsWith(Global.httpProtocol)) + if (!url.StartsWith(Global.HttpsProtocol) && !url.StartsWith(Global.HttpProtocol)) { continue; }