From 37734d2de90ba16a8e6f707cb448f3b568f58e17 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Wed, 21 Jan 2026 21:04:00 +0800 Subject: [PATCH] Refactor, use record instead of class --- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 86 +++++++------------ v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs | 5 +- .../ServiceLib/Handler/Fmt/ShadowsocksFmt.cs | 17 ++-- v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs | 13 +-- v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs | 9 +- v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs | 4 +- v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs | 17 ++-- v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs | 3 +- v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs | 30 +++---- v2rayN/ServiceLib/Manager/AppManager.cs | 37 +++++--- v2rayN/ServiceLib/Models/ProfileItem.cs | 6 +- v2rayN/ServiceLib/Models/ProtocolExtraItem.cs | 46 +++++----- .../ViewModels/AddGroupServerViewModel.cs | 33 ++++--- .../ViewModels/AddServerViewModel.cs | 33 +++---- 14 files changed, 151 insertions(+), 188 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index d2cda968..a8b4bf13 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -286,18 +286,19 @@ public static class ConfigHandler { profileItem.ConfigType = EConfigType.VMess; - var protocolExtra = profileItem.GetProtocolExtra(); - profileItem.Address = profileItem.Address.TrimEx(); profileItem.Password = profileItem.Password.TrimEx(); - protocolExtra.VmessSecurity = protocolExtra.VmessSecurity?.TrimEx(); + profileItem.SetProtocolExtra(profileItem.GetProtocolExtra() with + { + VmessSecurity = profileItem.GetProtocolExtra().VmessSecurity?.TrimEx() + }); profileItem.Network = profileItem.Network.TrimEx(); profileItem.HeaderType = profileItem.HeaderType.TrimEx(); profileItem.RequestHost = profileItem.RequestHost.TrimEx(); profileItem.Path = profileItem.Path.TrimEx(); profileItem.StreamSecurity = profileItem.StreamSecurity.TrimEx(); - if (!Global.VmessSecurities.Contains(protocolExtra.VmessSecurity)) + if (!Global.VmessSecurities.Contains(profileItem.GetProtocolExtra().VmessSecurity)) { return -1; } @@ -306,8 +307,6 @@ public static class ConfigHandler return -1; } - profileItem.SetProtocolExtra(protocolExtra); - await AddServerCommon(config, profileItem, toFile); return 0; @@ -605,13 +604,14 @@ public static class ConfigHandler { profileItem.ConfigType = EConfigType.Shadowsocks; - var protocolExtra = profileItem.GetProtocolExtra(); - profileItem.Address = profileItem.Address.TrimEx(); profileItem.Password = profileItem.Password.TrimEx(); - protocolExtra.SsMethod = protocolExtra.SsMethod.TrimEx(); + profileItem.SetProtocolExtra(profileItem.GetProtocolExtra() with + { + SsMethod = profileItem.GetProtocolExtra().SsMethod?.TrimEx() + }); - if (!AppManager.Instance.GetShadowsocksSecurities(profileItem).Contains(protocolExtra.SsMethod)) + if (!AppManager.Instance.GetShadowsocksSecurities(profileItem).Contains(profileItem.GetProtocolExtra().SsMethod)) { return -1; } @@ -620,8 +620,6 @@ public static class ConfigHandler return -1; } - profileItem.SetProtocolExtra(protocolExtra); - await AddServerCommon(config, profileItem, toFile); return 0; @@ -707,8 +705,6 @@ public static class ConfigHandler profileItem.ConfigType = EConfigType.Hysteria2; //profileItem.CoreType = ECoreType.sing_box; - var protocolExtra = profileItem.GetProtocolExtra(); - profileItem.Address = profileItem.Address.TrimEx(); profileItem.Password = profileItem.Password.TrimEx(); profileItem.Path = profileItem.Path.TrimEx(); @@ -722,20 +718,12 @@ public static class ConfigHandler { return -1; } - if (protocolExtra.UpMbps is null or < 0) + profileItem.SetProtocolExtra(profileItem.GetProtocolExtra() with { - protocolExtra.UpMbps = config.HysteriaItem.UpMbps; - } - if (protocolExtra.DownMbps is null or < 0) - { - protocolExtra.DownMbps = config.HysteriaItem.DownMbps; - } - if (protocolExtra.HopInterval is null or <= 5) - { - protocolExtra.HopInterval = Global.Hysteria2DefaultHopInt; - } - - profileItem.SetProtocolExtra(protocolExtra); + UpMbps = profileItem.GetProtocolExtra().UpMbps is null or < 0 ? config.HysteriaItem.UpMbps : profileItem.GetProtocolExtra().UpMbps, + DownMbps = profileItem.GetProtocolExtra().DownMbps is null or < 0 ? config.HysteriaItem.DownMbps : profileItem.GetProtocolExtra().DownMbps, + HopInterval = profileItem.GetProtocolExtra().HopInterval is null or <= 5 ? Global.Hysteria2DefaultHopInt : profileItem.GetProtocolExtra().HopInterval, + }); await AddServerCommon(config, profileItem, toFile); @@ -756,11 +744,12 @@ public static class ConfigHandler profileItem.ConfigType = EConfigType.TUIC; profileItem.CoreType = ECoreType.sing_box; - var protocolExtra = profileItem.GetProtocolExtra(); - profileItem.Address = profileItem.Address.TrimEx(); profileItem.Password = profileItem.Password.TrimEx(); - protocolExtra.Username = protocolExtra.Username?.TrimEx(); + profileItem.SetProtocolExtra(profileItem.GetProtocolExtra() with + { + Username = profileItem.GetProtocolExtra().Username?.TrimEx() + }); profileItem.Network = string.Empty; if (!Global.TuicCongestionControls.Contains(profileItem.HeaderType)) @@ -798,26 +787,22 @@ public static class ConfigHandler { profileItem.ConfigType = EConfigType.WireGuard; - var protocolExtra = profileItem.GetProtocolExtra(); - profileItem.Address = profileItem.Address.TrimEx(); profileItem.Password = profileItem.Password.TrimEx(); - protocolExtra.WgPublicKey = protocolExtra.WgPublicKey?.TrimEx(); - protocolExtra.WgPresharedKey = protocolExtra.WgPresharedKey?.TrimEx(); - protocolExtra.WgInterfaceAddress = protocolExtra.WgInterfaceAddress?.TrimEx(); - protocolExtra.WgReserved = protocolExtra.WgReserved?.TrimEx(); - if (protocolExtra.WgMtu <= 0) + profileItem.SetProtocolExtra(profileItem.GetProtocolExtra() with { - protocolExtra.WgMtu = Global.TunMtus.First(); - } + WgPublicKey = profileItem.GetProtocolExtra().WgPublicKey?.TrimEx(), + WgPresharedKey = profileItem.GetProtocolExtra().WgPresharedKey?.TrimEx(), + WgInterfaceAddress = profileItem.GetProtocolExtra().WgInterfaceAddress?.TrimEx(), + WgReserved = profileItem.GetProtocolExtra().WgReserved?.TrimEx(), + WgMtu = profileItem.GetProtocolExtra().WgMtu is null or <= 0 ? Global.TunMtus.First() : profileItem.GetProtocolExtra().WgMtu, + }); if (profileItem.Password.IsNullOrEmpty()) { return -1; } - profileItem.SetProtocolExtra(protocolExtra); - await AddServerCommon(config, profileItem, toFile); return 0; @@ -979,8 +964,6 @@ public static class ConfigHandler { profileItem.ConfigType = EConfigType.VLESS; - var protocolExtra = profileItem.GetProtocolExtra(); - profileItem.Address = profileItem.Address.TrimEx(); profileItem.Password = profileItem.Password.TrimEx(); profileItem.Network = profileItem.Network.TrimEx(); @@ -988,22 +971,19 @@ public static class ConfigHandler profileItem.RequestHost = profileItem.RequestHost.TrimEx(); profileItem.Path = profileItem.Path.TrimEx(); profileItem.StreamSecurity = profileItem.StreamSecurity.TrimEx(); - protocolExtra.VlessEncryption = protocolExtra.VlessEncryption?.TrimEx(); - if (!Global.Flows.Contains(protocolExtra.Flow ?? string.Empty)) + var vlessEncryption = profileItem.GetProtocolExtra().VlessEncryption?.TrimEx(); + var flow = profileItem.GetProtocolExtra().Flow?.TrimEx() ?? string.Empty; + profileItem.SetProtocolExtra(profileItem.GetProtocolExtra() with { - protocolExtra.Flow = Global.Flows.First(); - } + VlessEncryption = vlessEncryption.IsNullOrEmpty() ? Global.None : vlessEncryption, + Flow = Global.Flows.Contains(flow) ? flow : Global.Flows.First(), + }); + if (profileItem.Password.IsNullOrEmpty()) { return -1; } - if (protocolExtra.VlessEncryption.IsNullOrEmpty()) - { - protocolExtra.VlessEncryption = Global.None; - } - - profileItem.SetProtocolExtra(protocolExtra); await AddServerCommon(config, profileItem, toFile); diff --git a/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs b/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs index 759cf3c6..59833e5d 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs @@ -59,10 +59,9 @@ public class Hysteria2Fmt : BaseFmt dicQuery.Add("obfs", "salamander"); dicQuery.Add("obfs-password", Utils.UrlEncode(item.Path)); } - var protocolExtra = item.GetProtocolExtra(); - if (protocolExtra?.Ports?.IsNotEmpty() ?? false) + if (item.GetProtocolExtra()?.Ports?.IsNotEmpty() ?? false) { - dicQuery.Add("mport", Utils.UrlEncode(protocolExtra.Ports.Replace(':', '-'))); + dicQuery.Add("mport", Utils.UrlEncode(item.GetProtocolExtra().Ports.Replace(':', '-'))); } if (!item.CertSha.IsNullOrEmpty()) { diff --git a/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs index c613a296..5b30fa46 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs @@ -13,8 +13,7 @@ public class ShadowsocksFmt : BaseFmt return null; } - var protocolExtra = item.GetProtocolExtra(); - if (item.Address.Length == 0 || item.Port == 0 || protocolExtra.SsMethod.IsNullOrEmpty() || item.Password.Length == 0) + if (item.Address.Length == 0 || item.Port == 0 || item.GetProtocolExtra().SsMethod.IsNullOrEmpty() || item.Password.Length == 0) { return null; } @@ -42,8 +41,7 @@ public class ShadowsocksFmt : BaseFmt // item.port); //url = Utile.Base64Encode(url); //new Sip002 - var protocolExtra = item.GetProtocolExtra(); - var pw = Utils.Base64Encode($"{protocolExtra.SsMethod}:{item.Password}", true); + var pw = Utils.Base64Encode($"{item.GetProtocolExtra().SsMethod}:{item.Password}", true); // plugin var plugin = string.Empty; @@ -139,12 +137,10 @@ public class ShadowsocksFmt : BaseFmt { return null; } - var protocolExtra = item.GetProtocolExtra(); - protocolExtra.SsMethod = details.Groups["method"].Value; + item.SetProtocolExtra(item.GetProtocolExtra() with { SsMethod = details.Groups["method"].Value }); item.Password = details.Groups["password"].Value; item.Address = details.Groups["hostname"].Value; item.Port = details.Groups["port"].Value.ToInt(); - item.SetProtocolExtra(protocolExtra); return item; } @@ -162,7 +158,6 @@ public class ShadowsocksFmt : BaseFmt Address = parsedUrl.IdnHost, Port = parsedUrl.Port, }; - var protocolExtra = item.GetProtocolExtra(); var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo); //2022-blake3 if (rawUserInfo.Contains(':')) @@ -172,7 +167,7 @@ public class ShadowsocksFmt : BaseFmt { return null; } - protocolExtra.SsMethod = userInfoParts.First(); + item.SetProtocolExtra(item.GetProtocolExtra() with { SsMethod = userInfoParts.First() }); item.Password = Utils.UrlDecode(userInfoParts.Last()); } else @@ -184,7 +179,7 @@ public class ShadowsocksFmt : BaseFmt { return null; } - protocolExtra.SsMethod = userInfoParts.First(); + item.SetProtocolExtra(item.GetProtocolExtra() with { SsMethod = userInfoParts.First() }); item.Password = userInfoParts.Last(); } @@ -281,8 +276,6 @@ public class ShadowsocksFmt : BaseFmt } } } - - item.SetProtocolExtra(protocolExtra); return item; } diff --git a/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs index 35f10bd3..be996aba 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs @@ -33,8 +33,7 @@ public class SocksFmt : BaseFmt remark = "#" + Utils.UrlEncode(item.Remarks); } //new - var protocolExtra = item.GetProtocolExtra(); - var pw = Utils.Base64Encode($"{protocolExtra.Username}:{item.Password}", true); + var pw = Utils.Base64Encode($"{item.GetProtocolExtra().Username}:{item.Password}", true); return ToUri(EConfigType.SOCKS, item.Address, item.Port, pw, null, remark); } @@ -44,7 +43,6 @@ public class SocksFmt : BaseFmt { ConfigType = EConfigType.SOCKS }; - var protocolExtra = item.GetProtocolExtra(); result = result[Global.ProtocolShares[EConfigType.SOCKS].Length..]; //remark var indexRemark = result.IndexOf('#'); @@ -80,10 +78,8 @@ public class SocksFmt : BaseFmt } item.Address = arr1[1][..indexPort]; item.Port = arr1[1][(indexPort + 1)..].ToInt(); - protocolExtra.Username = arr21.First(); + item.SetProtocolExtra(item.GetProtocolExtra() with { Username = arr21.First() }); item.Password = arr21[1]; - - item.SetProtocolExtra(protocolExtra); return item; } @@ -101,19 +97,16 @@ public class SocksFmt : BaseFmt Address = parsedUrl.IdnHost, Port = parsedUrl.Port, }; - var protocolExtra = item.GetProtocolExtra(); - // parse base64 UserInfo var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo); var userInfo = Utils.Base64Decode(rawUserInfo); var userInfoParts = userInfo.Split([':'], 2); if (userInfoParts.Length == 2) { - protocolExtra.Username = userInfoParts.First(); + item.SetProtocolExtra(item.GetProtocolExtra() with { Username = userInfoParts.First() }); item.Password = userInfoParts[1]; } - item.SetProtocolExtra(protocolExtra); return item; } } diff --git a/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs index 5180a883..630dead1 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs @@ -10,7 +10,6 @@ public class TrojanFmt : BaseFmt { ConfigType = EConfigType.Trojan }; - var protocolExtra = item.GetProtocolExtra(); var url = Utils.TryUri(str); if (url == null) @@ -24,10 +23,9 @@ public class TrojanFmt : BaseFmt item.Password = Utils.UrlDecode(url.UserInfo); var query = Utils.ParseQueryString(url.Query); - protocolExtra.Flow = GetQueryValue(query, "flow"); + item.SetProtocolExtra(item.GetProtocolExtra() with { Flow = GetQueryValue(query, "flow") }); ResolveUriQuery(query, ref item); - item.SetProtocolExtra(protocolExtra); return item; } @@ -37,16 +35,15 @@ public class TrojanFmt : BaseFmt { return null; } - var protocolExtra = item.GetProtocolExtra(); var remark = string.Empty; if (item.Remarks.IsNotEmpty()) { remark = "#" + Utils.UrlEncode(item.Remarks); } var dicQuery = new Dictionary(); - if (!protocolExtra.Flow.IsNullOrEmpty()) + if (!item.GetProtocolExtra().Flow.IsNullOrEmpty()) { - dicQuery.Add("flow", protocolExtra.Flow); + dicQuery.Add("flow", item.GetProtocolExtra().Flow); } ToUriQuery(item, null, ref dicQuery); diff --git a/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs index 3f8be224..b17b1ef2 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs @@ -10,7 +10,6 @@ public class TuicFmt : BaseFmt { ConfigType = EConfigType.TUIC }; - var protocolExtra = item.GetProtocolExtra(); var url = Utils.TryUri(str); if (url == null) @@ -26,14 +25,13 @@ public class TuicFmt : BaseFmt if (userInfoParts.Length == 2) { item.Password = userInfoParts.First(); - protocolExtra.Username = userInfoParts.Last(); + item.SetProtocolExtra(item.GetProtocolExtra() with { Username = userInfoParts.Last() }); } var query = Utils.ParseQueryString(url.Query); ResolveUriQuery(query, ref item); item.HeaderType = GetQueryValue(query, "congestion_control"); - item.SetProtocolExtra(protocolExtra); return item; } diff --git a/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs index a48c3c05..a3463bcd 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs @@ -10,7 +10,6 @@ public class VLESSFmt : BaseFmt { ConfigType = EConfigType.VLESS, }; - var protocolExtra = item.GetProtocolExtra(); var url = Utils.TryUri(str); if (url == null) @@ -24,12 +23,14 @@ public class VLESSFmt : BaseFmt item.Password = Utils.UrlDecode(url.UserInfo); var query = Utils.ParseQueryString(url.Query); - protocolExtra.VlessEncryption = GetQueryValue(query, "encryption", Global.None); - protocolExtra.Flow = GetQueryValue(query, "flow"); + item.SetProtocolExtra(item.GetProtocolExtra() with + { + VlessEncryption = GetQueryValue(query, "encryption", Global.None), + Flow = GetQueryValue(query, "flow") + }); item.StreamSecurity = GetQueryValue(query, "security"); ResolveUriQuery(query, ref item); - item.SetProtocolExtra(protocolExtra); return item; } @@ -40,8 +41,6 @@ public class VLESSFmt : BaseFmt return null; } - var protocolExtra = item.GetProtocolExtra(); - var remark = string.Empty; if (item.Remarks.IsNotEmpty()) { @@ -49,10 +48,10 @@ public class VLESSFmt : BaseFmt } var dicQuery = new Dictionary(); dicQuery.Add("encryption", - !protocolExtra.VlessEncryption.IsNullOrEmpty() ? protocolExtra.VlessEncryption : Global.None); - if (!protocolExtra.Flow.IsNullOrEmpty()) + !item.GetProtocolExtra().VlessEncryption.IsNullOrEmpty() ? item.GetProtocolExtra().VlessEncryption : Global.None); + if (!item.GetProtocolExtra().Flow.IsNullOrEmpty()) { - dicQuery.Add("flow", protocolExtra.Flow); + dicQuery.Add("flow", item.GetProtocolExtra().Flow); } ToUriQuery(item, Global.None, ref dicQuery); diff --git a/v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs index 58874b6d..b8760a40 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs @@ -24,7 +24,6 @@ public class VmessFmt : BaseFmt return null; } - var protocolExtra = item?.GetProtocolExtra(); var vmessQRCode = new VmessQRCode { v = 2, @@ -32,7 +31,7 @@ public class VmessFmt : BaseFmt add = item.Address, port = item.Port, id = item.Password, - aid = int.TryParse(protocolExtra?.AlterId, out var result) ? result : 0, + aid = int.TryParse(item.GetProtocolExtra()?.AlterId, out var result) ? result : 0, scy = item.GetProtocolExtra().VmessSecurity ?? "", net = item.Network, type = item.HeaderType, diff --git a/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs index c003a96b..73cb6a85 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/WireguardFmt.cs @@ -17,8 +17,6 @@ public class WireguardFmt : BaseFmt return null; } - var protocolExtra = item.GetProtocolExtra(); - item.Address = url.IdnHost; item.Port = url.Port; item.Remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped); @@ -26,11 +24,13 @@ public class WireguardFmt : BaseFmt var query = Utils.ParseQueryString(url.Query); - protocolExtra.WgPublicKey = GetQueryDecoded(query, "publickey"); - protocolExtra.WgReserved = GetQueryDecoded(query, "reserved"); - protocolExtra.WgInterfaceAddress = GetQueryDecoded(query, "address"); - protocolExtra.WgMtu = int.TryParse(GetQueryDecoded(query, "mtu"), out var mtu) ? mtu : 1280; - protocolExtra.WgPresharedKey = GetQueryDecoded(query, "presharedKey"); + item.SetProtocolExtra(item.GetProtocolExtra() with + { + WgPublicKey = GetQueryDecoded(query, "publickey"), + WgReserved = GetQueryDecoded(query, "reserved"), + WgInterfaceAddress = GetQueryDecoded(query, "address"), + WgMtu = int.TryParse(GetQueryDecoded(query, "mtu"), out var mtuVal) ? mtuVal : 1280, + }); return item; } @@ -42,8 +42,6 @@ public class WireguardFmt : BaseFmt return null; } - var protocolExtra = item.GetProtocolExtra(); - var remark = string.Empty; if (item.Remarks.IsNotEmpty()) { @@ -51,19 +49,19 @@ public class WireguardFmt : BaseFmt } var dicQuery = new Dictionary(); - if (!protocolExtra.WgPublicKey.IsNullOrEmpty()) + if (!item.GetProtocolExtra().WgPublicKey.IsNullOrEmpty()) { - dicQuery.Add("publickey", Utils.UrlEncode(protocolExtra.WgPublicKey)); + dicQuery.Add("publickey", Utils.UrlEncode(item.GetProtocolExtra().WgPublicKey)); } - if (!protocolExtra.WgReserved.IsNullOrEmpty()) + if (!item.GetProtocolExtra().WgReserved.IsNullOrEmpty()) { - dicQuery.Add("reserved", Utils.UrlEncode(protocolExtra.WgReserved)); + dicQuery.Add("reserved", Utils.UrlEncode(item.GetProtocolExtra().WgReserved)); } - if (!protocolExtra.WgInterfaceAddress.IsNullOrEmpty()) + if (!item.GetProtocolExtra().WgInterfaceAddress.IsNullOrEmpty()) { - dicQuery.Add("address", Utils.UrlEncode(protocolExtra.WgInterfaceAddress)); + dicQuery.Add("address", Utils.UrlEncode(item.GetProtocolExtra().WgInterfaceAddress)); } - dicQuery.Add("mtu", Utils.UrlEncode(protocolExtra.WgMtu > 0 ? protocolExtra.WgMtu.ToString() : "1280")); + dicQuery.Add("mtu", Utils.UrlEncode(item.GetProtocolExtra().WgMtu > 0 ? item.GetProtocolExtra().WgMtu.ToString() : "1280")); return ToUri(EConfigType.WireGuard, item.Address, item.Port, item.Password, dicQuery, remark); } } diff --git a/v2rayN/ServiceLib/Manager/AppManager.cs b/v2rayN/ServiceLib/Manager/AppManager.cs index 3838626e..7753cac8 100644 --- a/v2rayN/ServiceLib/Manager/AppManager.cs +++ b/v2rayN/ServiceLib/Manager/AppManager.cs @@ -288,34 +288,43 @@ public sealed class AppManager if (item.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) { - extra.GroupType = nameof(item.ConfigType); + extra = extra with { GroupType = nameof(item.ConfigType) }; groupItems.TryGetValue(item.IndexId, out var groupItem); if (groupItem != null && !groupItem.NotHasChild()) { - extra.ChildItems = groupItem.ChildItems; - extra.SubChildItems = groupItem.SubChildItems; - extra.Filter = groupItem.Filter; - extra.MultipleLoad = groupItem.MultipleLoad; + extra = extra with + { + ChildItems = groupItem.ChildItems, + SubChildItems = groupItem.SubChildItems, + Filter = groupItem.Filter, + MultipleLoad = groupItem.MultipleLoad, + }; } switch (item.ConfigType) { case EConfigType.Shadowsocks: - extra.SsMethod = item.Security.IsNotEmpty() ? item.Security : null; + extra = extra with {SsMethod = item.Security.IsNotEmpty() ? item.Security : null}; break; case EConfigType.VMess: - extra.VmessSecurity = item.Security.IsNotEmpty() ? item.Security : null; + extra = extra with {VmessSecurity = item.Security.IsNotEmpty() ? item.Security : null}; break; case EConfigType.Hysteria2: - extra.UpMbps = _config.HysteriaItem.UpMbps; - extra.DownMbps = _config.HysteriaItem.DownMbps; - extra.HopInterval = _config.HysteriaItem.HopInterval; + extra = extra with + { + UpMbps = _config.HysteriaItem.UpMbps, + DownMbps = _config.HysteriaItem.DownMbps, + HopInterval = _config.HysteriaItem.HopInterval + }; break; case EConfigType.WireGuard: - extra.WgPublicKey = item.PublicKey.IsNotEmpty() ? item.PublicKey : null; - extra.WgInterfaceAddress = item.RequestHost.IsNotEmpty() ? item.RequestHost : null; - extra.WgReserved = item.Path.IsNotEmpty() ? item.Path : null; - extra.WgMtu = int.TryParse(item.ShortId, out var mtu) ? mtu : 1280; + extra = extra with + { + WgPublicKey = item.PublicKey.IsNotEmpty() ? item.PublicKey : null, + WgInterfaceAddress = item.RequestHost.IsNotEmpty() ? item.RequestHost : null, + WgReserved = item.Path.IsNotEmpty() ? item.Path : null, + WgMtu = int.TryParse(item.ShortId, out var mtu) ? mtu : 1280 + }; break; } } diff --git a/v2rayN/ServiceLib/Models/ProfileItem.cs b/v2rayN/ServiceLib/Models/ProfileItem.cs index 60cbe9b2..f8be1cfe 100644 --- a/v2rayN/ServiceLib/Models/ProfileItem.cs +++ b/v2rayN/ServiceLib/Models/ProfileItem.cs @@ -106,10 +106,8 @@ public class ProfileItem : ReactiveObject return false; } - var protocolExtra = GetProtocolExtra(); - - if (string.IsNullOrEmpty(protocolExtra.SsMethod) - || !Global.SsSecuritiesInSingbox.Contains(protocolExtra.SsMethod)) + if (string.IsNullOrEmpty(GetProtocolExtra().SsMethod) + || !Global.SsSecuritiesInSingbox.Contains(GetProtocolExtra().SsMethod)) { return false; } diff --git a/v2rayN/ServiceLib/Models/ProtocolExtraItem.cs b/v2rayN/ServiceLib/Models/ProtocolExtraItem.cs index eea4698c..0233b542 100644 --- a/v2rayN/ServiceLib/Models/ProtocolExtraItem.cs +++ b/v2rayN/ServiceLib/Models/ProtocolExtraItem.cs @@ -1,40 +1,40 @@ namespace ServiceLib.Models; -public class ProtocolExtraItem +public record ProtocolExtraItem { // vmess - public string? AlterId { get; set; } - public string? VmessSecurity { get; set; } + public string? AlterId { get; init; } + public string? VmessSecurity { get; init; } // vless - public string? Flow { get; set; } - public string? VlessEncryption { get; set; } - //public string? VisionSeed { get; set; } + public string? Flow { get; init; } + public string? VlessEncryption { get; init; } + //public string? VisionSeed { get; init; } // shadowsocks - //public string? PluginArgs { get; set; } - public string? SsMethod { get; set; } + //public string? PluginArgs { get; init; } + public string? SsMethod { get; init; } // socks and http - public string? Username { get; set; } + public string? Username { get; init; } // wireguard - public string? WgPublicKey { get; set; } - public string? WgPresharedKey { get; set; } - public string? WgInterfaceAddress { get; set; } - public string? WgReserved { get; set; } - public int? WgMtu { get; set; } + public string? WgPublicKey { get; init; } + public string? WgPresharedKey { get; init; } + public string? WgInterfaceAddress { get; init; } + public string? WgReserved { get; init; } + public int? WgMtu { get; init; } // hysteria2 - public int? UpMbps { get; set; } - public int? DownMbps { get; set; } - public string? Ports { get; set; } - public int? HopInterval { get; set; } + public int? UpMbps { get; init; } + public int? DownMbps { get; init; } + public string? Ports { get; init; } + public int? HopInterval { get; init; } // group profile - public string? GroupType { get; set; } - public string? ChildItems { get; set; } - public string? SubChildItems { get; set; } - public string? Filter { get; set; } - public EMultipleLoad? MultipleLoad { get; set; } + public string? GroupType { get; init; } + public string? ChildItems { get; init; } + public string? SubChildItems { get; init; } + public string? Filter { get; init; } + public EMultipleLoad? MultipleLoad { get; init; } } diff --git a/v2rayN/ServiceLib/ViewModels/AddGroupServerViewModel.cs b/v2rayN/ServiceLib/ViewModels/AddGroupServerViewModel.cs index 0bedeac5..c19d218b 100644 --- a/v2rayN/ServiceLib/ViewModels/AddGroupServerViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/AddGroupServerViewModel.cs @@ -202,31 +202,30 @@ public class AddGroupServerViewModel : MyReactiveObject return; } - var protocolExtra = SelectedSource.GetProtocolExtra(); - protocolExtra.ChildItems = - Utils.List2String(ChildItemsObs.Where(s => !s.IndexId.IsNullOrEmpty()).Select(s => s.IndexId).ToList()); - protocolExtra.MultipleLoad = PolicyGroupType switch + SelectedSource.SetProtocolExtra(SelectedSource.GetProtocolExtra() with { - var s when s == ResUI.TbLeastPing => EMultipleLoad.LeastPing, - var s when s == ResUI.TbFallback => EMultipleLoad.Fallback, - var s when s == ResUI.TbRandom => EMultipleLoad.Random, - var s when s == ResUI.TbRoundRobin => EMultipleLoad.RoundRobin, - var s when s == ResUI.TbLeastLoad => EMultipleLoad.LeastLoad, - _ => EMultipleLoad.LeastPing, - }; + ChildItems = + Utils.List2String(ChildItemsObs.Where(s => !s.IndexId.IsNullOrEmpty()).Select(s => s.IndexId).ToList()), + MultipleLoad = PolicyGroupType switch + { + var s when s == ResUI.TbLeastPing => EMultipleLoad.LeastPing, + var s when s == ResUI.TbFallback => EMultipleLoad.Fallback, + var s when s == ResUI.TbRandom => EMultipleLoad.Random, + var s when s == ResUI.TbRoundRobin => EMultipleLoad.RoundRobin, + var s when s == ResUI.TbLeastLoad => EMultipleLoad.LeastLoad, + _ => EMultipleLoad.LeastPing, + }, + SubChildItems = SelectedSubItem?.Id, + Filter = Filter, + }); - protocolExtra.SubChildItems = SelectedSubItem?.Id; - protocolExtra.Filter = Filter; - - var hasCycle = await GroupProfileManager.HasCycle(SelectedSource.IndexId, protocolExtra); + var hasCycle = await GroupProfileManager.HasCycle(SelectedSource.IndexId, SelectedSource.GetProtocolExtra()); if (hasCycle) { NoticeManager.Instance.Enqueue(string.Format(ResUI.GroupSelfReference, remarks)); return; } - SelectedSource.SetProtocolExtra(protocolExtra); - if (await ConfigHandler.AddServerCommon(_config, SelectedSource) == 0) { NoticeManager.Instance.Enqueue(ResUI.OperationSuccess); diff --git a/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs b/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs index 7c1b3895..fd204969 100644 --- a/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs @@ -166,22 +166,23 @@ public class AddServerViewModel : MyReactiveObject SelectedSource.CoreType = CoreType.IsNullOrEmpty() ? null : (ECoreType)Enum.Parse(typeof(ECoreType), CoreType); SelectedSource.Cert = Cert.IsNullOrEmpty() ? string.Empty : Cert; SelectedSource.CertSha = CertSha.IsNullOrEmpty() ? string.Empty : CertSha; - var protocolExtra = SelectedSource.GetProtocolExtra(); - protocolExtra.Ports = Ports.IsNullOrEmpty() ? null : Ports; - protocolExtra.AlterId = AlterId > 0 ? AlterId.ToString() : string.Empty; - protocolExtra.Flow = Flow.IsNullOrEmpty() ? null : Flow; - protocolExtra.UpMbps = UpMbps > 0 ? UpMbps : null; - protocolExtra.DownMbps = DownMbps > 0 ? DownMbps : null; - protocolExtra.HopInterval = HopInterval >= 5 ? HopInterval : null; - protocolExtra.VmessSecurity = VmessSecurity.IsNullOrEmpty() ? null : VmessSecurity; - protocolExtra.VlessEncryption = VlessEncryption.IsNullOrEmpty() ? null : VlessEncryption; - protocolExtra.SsMethod = SsMethod.IsNullOrEmpty() ? null : SsMethod; - protocolExtra.Username = Username.IsNullOrEmpty() ? null : Username; - protocolExtra.WgPublicKey = WgPublicKey.IsNullOrEmpty() ? null : WgPublicKey; - protocolExtra.WgInterfaceAddress = WgInterfaceAddress.IsNullOrEmpty() ? null : WgInterfaceAddress; - protocolExtra.WgReserved = WgReserved.IsNullOrEmpty() ? null : WgReserved; - protocolExtra.WgMtu = WgMtu >= 576 ? WgMtu : null; - SelectedSource.SetProtocolExtra(protocolExtra); + SelectedSource.SetProtocolExtra(SelectedSource.GetProtocolExtra() with + { + Ports = Ports.IsNullOrEmpty() ? null : Ports, + AlterId = AlterId > 0 ? AlterId.ToString() : string.Empty, + Flow = Flow.IsNullOrEmpty() ? null : Flow, + UpMbps = UpMbps > 0 ? UpMbps : null, + DownMbps = DownMbps > 0 ? DownMbps : null, + HopInterval = HopInterval >= 5 ? HopInterval : null, + VmessSecurity = VmessSecurity.IsNullOrEmpty() ? null : VmessSecurity, + VlessEncryption = VlessEncryption.IsNullOrEmpty() ? null : VlessEncryption, + SsMethod = SsMethod.IsNullOrEmpty() ? null : SsMethod, + Username = Username.IsNullOrEmpty() ? null : Username, + WgPublicKey = WgPublicKey.IsNullOrEmpty() ? null : WgPublicKey, + WgInterfaceAddress = WgInterfaceAddress.IsNullOrEmpty() ? null : WgInterfaceAddress, + WgReserved = WgReserved.IsNullOrEmpty() ? null : WgReserved, + WgMtu = WgMtu >= 576 ? WgMtu : null, + }); if (await ConfigHandler.AddServer(_config, SelectedSource) == 0) {