Improve code

This commit is contained in:
2dust 2024-12-01 11:17:36 +08:00
parent f36c06389d
commit 3b173f0b3e
17 changed files with 85 additions and 93 deletions

View file

@ -31,8 +31,8 @@
var parts = this.version.Split('.');
if (parts.Length == 2)
{
this.major = int.Parse(parts[0]);
this.minor = int.Parse(parts[1]);
this.major = int.Parse(parts.First());
this.minor = int.Parse(parts.Last());
this.patch = 0;
}
else if (parts.Length is 3 or 4)

View file

@ -22,7 +22,7 @@ namespace ServiceLib.Common
public static bool BeginWithAny(this string s, IEnumerable<char> chars)
{
if (s.IsNullOrEmpty()) return false;
return chars.Contains(s[0]);
return chars.Contains(s.First());
}
private static bool IsWhiteSpace(this string value)
@ -61,7 +61,7 @@ namespace ServiceLib.Common
return string.Empty;
}
return char.ToUpper(value[0]) + value[1..];
return char.ToUpper(value.First()) + value[1..];
}
public static string AppendQuotes(this string value)

View file

@ -313,8 +313,8 @@ namespace ServiceLib.Common
continue;
}
var key = Uri.UnescapeDataString(keyValue[0]);
var val = Uri.UnescapeDataString(keyValue[1]);
var key = Uri.UnescapeDataString(keyValue.First());
var val = Uri.UnescapeDataString(keyValue.Last());
if (result[key] is null)
{
@ -622,8 +622,8 @@ namespace ServiceLib.Common
{
if (host.StartsWith("#")) continue;
var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (hostItem.Length < 2) continue;
systemHosts.Add(hostItem[1], hostItem[0]);
if (hostItem.Length != 2) continue;
systemHosts.Add(hostItem.Last(), hostItem.First());
}
}
}

View file

@ -62,7 +62,7 @@ namespace ServiceLib.Handler
{
if (config.Inbound.Count > 0)
{
config.Inbound[0].Protocol = EInboundProtocol.socks.ToString();
config.Inbound.First().Protocol = EInboundProtocol.socks.ToString();
}
}
@ -70,7 +70,7 @@ namespace ServiceLib.Handler
if (Utils.IsNullOrEmpty(config.RoutingBasicItem.DomainStrategy))
{
config.RoutingBasicItem.DomainStrategy = Global.DomainStrategies[0];//"IPIfNonMatch";
config.RoutingBasicItem.DomainStrategy = Global.DomainStrategies.First();//"IPIfNonMatch";
}
config.KcpItem ??= new KcpItem
@ -111,7 +111,7 @@ namespace ServiceLib.Handler
{
if (Thread.CurrentThread.CurrentCulture.Name.Equals("zh-cn", StringComparison.CurrentCultureIgnoreCase))
{
config.UiItem.CurrentLanguage = Global.Languages[0];
config.UiItem.CurrentLanguage = Global.Languages.First();
}
else
{
@ -132,7 +132,7 @@ namespace ServiceLib.Handler
}
if (Utils.IsNullOrEmpty(config.SpeedTestItem.SpeedTestUrl))
{
config.SpeedTestItem.SpeedTestUrl = Global.SpeedTestUrls[0];
config.SpeedTestItem.SpeedTestUrl = Global.SpeedTestUrls.First();
}
if (Utils.IsNullOrEmpty(config.SpeedTestItem.SpeedPingTestUrl))
{
@ -148,7 +148,7 @@ namespace ServiceLib.Handler
config.Mux4SboxItem ??= new()
{
Protocol = Global.SingboxMuxs[0],
Protocol = Global.SingboxMuxs.First(),
MaxConnections = 8
};
@ -429,7 +429,7 @@ namespace ServiceLib.Handler
{
return 0;
}
sort = ProfileExHandler.Instance.GetSort(lstProfile[0].IndexId) - 1;
sort = ProfileExHandler.Instance.GetSort(lstProfile.First().IndexId) - 1;
break;
}

View file

@ -99,8 +99,8 @@ namespace ServiceLib.Handler.Fmt
{
return null;
}
item.Security = userInfoParts[0];
item.Id = Utils.UrlDecode(userInfoParts[1]);
item.Security = userInfoParts.First();
item.Id = Utils.UrlDecode(userInfoParts.Last());
}
else
{
@ -111,8 +111,8 @@ namespace ServiceLib.Handler.Fmt
{
return null;
}
item.Security = userInfoParts[0];
item.Id = userInfoParts[1];
item.Security = userInfoParts.First();
item.Id = userInfoParts.Last();
}
var queryParameters = Utils.ParseQueryString(parsedUrl.Query);

View file

@ -5,9 +5,8 @@
public static ProfileItem? Resolve(string str, out string msg)
{
msg = ResUI.ConfigurationFormatIncorrect;
ProfileItem? item;
item = ResolveSocksNew(str) ?? ResolveSocks(str);
var item = ResolveSocksNew(str) ?? ResolveSocks(str);
if (item == null)
{
return null;
@ -25,19 +24,13 @@
public static string? ToUri(ProfileItem? item)
{
if (item == null) return null;
string url = string.Empty;
var url = string.Empty;
string remark = string.Empty;
var remark = string.Empty;
if (Utils.IsNotEmpty(item.Remarks))
{
remark = "#" + Utils.UrlEncode(item.Remarks);
}
//url = string.Format("{0}:{1}@{2}:{3}",
// item.security,
// item.id,
// item.address,
// item.port);
//url = Utile.Base64Encode(url);
//new
var pw = Utils.Base64Encode($"{item.Security}:{item.Id}");
return ToUri(EConfigType.SOCKS, item.Address, item.Port, pw, null, remark);
@ -51,7 +44,7 @@
};
result = result[Global.ProtocolShares[EConfigType.SOCKS].Length..];
//remark
int indexRemark = result.IndexOf("#");
var indexRemark = result.IndexOf("#");
if (indexRemark > 0)
{
try
@ -62,7 +55,7 @@
result = result[..indexRemark];
}
//part decode
int indexS = result.IndexOf("@");
var indexS = result.IndexOf("@");
if (indexS > 0)
{
}
@ -71,21 +64,20 @@
result = Utils.Base64Decode(result);
}
string[] arr1 = result.Split('@');
var arr1 = result.Split('@');
if (arr1.Length != 2)
{
return null;
}
string[] arr21 = arr1[0].Split(':');
//string[] arr22 = arr1[1].Split(':');
int indexPort = arr1[1].LastIndexOf(":");
var arr21 = arr1.First().Split(':');
var indexPort = arr1.Last().LastIndexOf(":");
if (arr21.Length != 2 || indexPort < 0)
{
return null;
}
item.Address = arr1[1][..indexPort];
item.Port = Utils.ToInt(arr1[1][(indexPort + 1)..]);
item.Security = arr21[0];
item.Security = arr21.First();
item.Id = arr21[1];
return item;
@ -106,10 +98,10 @@
// parse base64 UserInfo
var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo);
var userInfo = Utils.Base64Decode(rawUserInfo);
var userInfoParts = userInfo.Split(new[] { ':' }, 2);
var userInfoParts = userInfo.Split([':'], 2);
if (userInfoParts.Length == 2)
{
item.Security = userInfoParts[0];
item.Security = userInfoParts.First();
item.Id = userInfoParts[1];
}

View file

@ -21,8 +21,8 @@
var userInfoParts = rawUserInfo.Split(new[] { ':' }, 2);
if (userInfoParts.Length == 2)
{
item.Id = userInfoParts[0];
item.Security = userInfoParts[1];
item.Id = userInfoParts.First();
item.Security = userInfoParts.Last();
}
var query = Utils.ParseQueryString(url.Query);

View file

@ -88,7 +88,7 @@ namespace ServiceLib.Services.CoreConfig
//external-controller
fileContent["external-controller"] = $"{Global.Loopback}:{AppHandler.Instance.StatePort2}";
//allow-lan
if (_config.Inbound[0].AllowLANConn)
if (_config.Inbound.First().AllowLANConn)
{
fileContent["allow-lan"] = "true";
fileContent["bind-address"] = "*";

View file

@ -52,7 +52,7 @@ namespace ServiceLib.Services.CoreConfig
await GenInbounds(singboxConfig);
await GenOutbound(node, singboxConfig.outbounds[0]);
await GenOutbound(node, singboxConfig.outbounds.First());
await GenMoreOutbounds(node, singboxConfig);
@ -495,8 +495,8 @@ namespace ServiceLib.Services.CoreConfig
singboxConfig.inbounds.Add(inbound);
inbound.listen_port = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
inbound.sniff = _config.Inbound[0].SniffingEnabled;
inbound.sniff_override_destination = _config.Inbound[0].RouteOnly ? false : _config.Inbound[0].SniffingEnabled;
inbound.sniff = _config.Inbound.First().SniffingEnabled;
inbound.sniff_override_destination = _config.Inbound.First().RouteOnly ? false : _config.Inbound.First().SniffingEnabled;
inbound.domain_strategy = Utils.IsNullOrEmpty(_config.RoutingBasicItem.DomainStrategy4Singbox) ? null : _config.RoutingBasicItem.DomainStrategy4Singbox;
var routing = await ConfigHandler.GetDefaultRouting(_config);
@ -509,9 +509,9 @@ namespace ServiceLib.Services.CoreConfig
var inbound2 = GetInbound(inbound, EInboundProtocol.http, false);
singboxConfig.inbounds.Add(inbound2);
if (_config.Inbound[0].AllowLANConn)
if (_config.Inbound.First().AllowLANConn)
{
if (_config.Inbound[0].NewPort4LAN)
if (_config.Inbound.First().NewPort4LAN)
{
var inbound3 = GetInbound(inbound, EInboundProtocol.socks2, true);
inbound3.listen = listen;
@ -522,10 +522,10 @@ namespace ServiceLib.Services.CoreConfig
singboxConfig.inbounds.Add(inbound4);
//auth
if (Utils.IsNotEmpty(_config.Inbound[0].User) && Utils.IsNotEmpty(_config.Inbound[0].Pass))
if (Utils.IsNotEmpty(_config.Inbound.First().User) && Utils.IsNotEmpty(_config.Inbound.First().Pass))
{
inbound3.users = new() { new() { username = _config.Inbound[0].User, password = _config.Inbound[0].Pass } };
inbound4.users = new() { new() { username = _config.Inbound[0].User, password = _config.Inbound[0].Pass } };
inbound3.users = new() { new() { username = _config.Inbound.First().User, password = _config.Inbound.First().Pass } };
inbound4.users = new() { new() { username = _config.Inbound.First().User, password = _config.Inbound.First().Pass } };
}
}
else
@ -540,11 +540,11 @@ namespace ServiceLib.Services.CoreConfig
{
if (_config.TunModeItem.Mtu <= 0)
{
_config.TunModeItem.Mtu = Utils.ToInt(Global.TunMtus[0]);
_config.TunModeItem.Mtu = Utils.ToInt(Global.TunMtus.First());
}
if (Utils.IsNullOrEmpty(_config.TunModeItem.Stack))
{
_config.TunModeItem.Stack = Global.TunStacks[0];
_config.TunModeItem.Stack = Global.TunStacks.First();
}
var tunInbound = JsonUtils.Deserialize<Inbound4Sbox>(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { };
@ -552,8 +552,8 @@ namespace ServiceLib.Services.CoreConfig
tunInbound.mtu = _config.TunModeItem.Mtu;
tunInbound.strict_route = _config.TunModeItem.StrictRoute;
tunInbound.stack = _config.TunModeItem.Stack;
tunInbound.sniff = _config.Inbound[0].SniffingEnabled;
//tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
tunInbound.sniff = _config.Inbound.First().SniffingEnabled;
//tunInbound.sniff_override_destination = _config.inbound.First().routeOnly ? false : _config.inbound.First().sniffingEnabled;
if (_config.TunModeItem.EnableIPv6Address == false)
{
tunInbound.address = ["172.18.0.1/30"];
@ -867,7 +867,7 @@ namespace ServiceLib.Services.CoreConfig
}
//current proxy
var outbound = singboxConfig.outbounds[0];
var outbound = singboxConfig.outbounds.First();
var txtOutbound = Utils.GetEmbedText(Global.SingboxSampleOutbound);
//Previous proxy
@ -910,7 +910,7 @@ namespace ServiceLib.Services.CoreConfig
try
{
var dnsOutbound = "dns_out";
if (!_config.Inbound[0].SniffingEnabled)
if (!_config.Inbound.First().SniffingEnabled)
{
singboxConfig.route.rules.Add(new()
{

View file

@ -55,7 +55,7 @@ namespace ServiceLib.Services.CoreConfig
await GenRouting(v2rayConfig);
await GenOutbound(node, v2rayConfig.outbounds[0]);
await GenOutbound(node, v2rayConfig.outbounds.First());
await GenMoreOutbounds(node, v2rayConfig);
@ -391,33 +391,33 @@ namespace ServiceLib.Services.CoreConfig
var listen = "0.0.0.0";
v2rayConfig.inbounds = [];
Inbounds4Ray? inbound = GetInbound(_config.Inbound[0], EInboundProtocol.socks, true);
Inbounds4Ray? inbound = GetInbound(_config.Inbound.First(), EInboundProtocol.socks, true);
v2rayConfig.inbounds.Add(inbound);
//http
Inbounds4Ray? inbound2 = GetInbound(_config.Inbound[0], EInboundProtocol.http, false);
Inbounds4Ray? inbound2 = GetInbound(_config.Inbound.First(), EInboundProtocol.http, false);
v2rayConfig.inbounds.Add(inbound2);
if (_config.Inbound[0].AllowLANConn)
if (_config.Inbound.First().AllowLANConn)
{
if (_config.Inbound[0].NewPort4LAN)
if (_config.Inbound.First().NewPort4LAN)
{
var inbound3 = GetInbound(_config.Inbound[0], EInboundProtocol.socks2, true);
var inbound3 = GetInbound(_config.Inbound.First(), EInboundProtocol.socks2, true);
inbound3.listen = listen;
v2rayConfig.inbounds.Add(inbound3);
var inbound4 = GetInbound(_config.Inbound[0], EInboundProtocol.http2, false);
var inbound4 = GetInbound(_config.Inbound.First(), EInboundProtocol.http2, false);
inbound4.listen = listen;
v2rayConfig.inbounds.Add(inbound4);
//auth
if (Utils.IsNotEmpty(_config.Inbound[0].User) && Utils.IsNotEmpty(_config.Inbound[0].Pass))
if (Utils.IsNotEmpty(_config.Inbound.First().User) && Utils.IsNotEmpty(_config.Inbound.First().Pass))
{
inbound3.settings.auth = "password";
inbound3.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.Inbound[0].User, pass = _config.Inbound[0].Pass } };
inbound3.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.Inbound.First().User, pass = _config.Inbound.First().Pass } };
inbound4.settings.auth = "password";
inbound4.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.Inbound[0].User, pass = _config.Inbound[0].Pass } };
inbound4.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.Inbound.First().User, pass = _config.Inbound.First().Pass } };
}
}
else
@ -587,7 +587,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
vnextItem = outbound.settings.vnext[0];
vnextItem = outbound.settings.vnext.First();
}
vnextItem.address = node.Address;
vnextItem.port = node.Port;
@ -600,7 +600,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
usersItem = vnextItem.users[0];
usersItem = vnextItem.users.First();
}
//远程服务器用户ID
usersItem.id = node.Id;
@ -630,7 +630,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
serversItem = outbound.settings.servers[0];
serversItem = outbound.settings.servers.First();
}
serversItem.address = node.Address;
serversItem.port = node.Port;
@ -656,7 +656,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
serversItem = outbound.settings.servers[0];
serversItem = outbound.settings.servers.First();
}
serversItem.address = node.Address;
serversItem.port = node.Port;
@ -691,7 +691,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
vnextItem = outbound.settings.vnext[0];
vnextItem = outbound.settings.vnext.First();
}
vnextItem.address = node.Address;
vnextItem.port = node.Port;
@ -704,7 +704,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
usersItem = vnextItem.users[0];
usersItem = vnextItem.users.First();
}
usersItem.id = node.Id;
usersItem.email = Global.UserEMail;
@ -740,7 +740,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
serversItem = outbound.settings.servers[0];
serversItem = outbound.settings.servers.First();
}
serversItem.address = node.Address;
serversItem.port = node.Port;
@ -1167,7 +1167,7 @@ namespace ServiceLib.Services.CoreConfig
{
//fragment proxy
if (_config.CoreBasicItem.EnableFragment
&& Utils.IsNotEmpty(v2rayConfig.outbounds[0].streamSettings?.security))
&& Utils.IsNotEmpty(v2rayConfig.outbounds.First().streamSettings?.security))
{
var fragmentOutbound = new Outbounds4Ray
{
@ -1185,7 +1185,7 @@ namespace ServiceLib.Services.CoreConfig
};
v2rayConfig.outbounds.Add(fragmentOutbound);
v2rayConfig.outbounds[0].streamSettings.sockopt = new()
v2rayConfig.outbounds.First().streamSettings.sockopt = new()
{
dialerProxy = fragmentOutbound.tag
};
@ -1205,7 +1205,7 @@ namespace ServiceLib.Services.CoreConfig
}
//current proxy
var outbound = v2rayConfig.outbounds[0];
var outbound = v2rayConfig.outbounds.First();
var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound);
//Previous proxy

View file

@ -348,7 +348,7 @@ namespace ServiceLib.Services
if (!IPAddress.TryParse(url, out IPAddress? ipAddress))
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(url);
ipAddress = ipHostInfo.AddressList[0];
ipAddress = ipHostInfo.AddressList.First();
}
var timer = Stopwatch.StartNew();

View file

@ -239,7 +239,7 @@ namespace ServiceLib.ViewModels
}
else
{
SelectedGroup = _proxyGroups[0];
SelectedGroup = _proxyGroups.First();
}
}
else

View file

@ -122,7 +122,7 @@ namespace ServiceLib.ViewModels
#region Core
var inbound = _config.Inbound[0];
var inbound = _config.Inbound.First();
localPort = inbound.LocalPort;
udpEnabled = inbound.UdpEnabled;
sniffingEnabled = inbound.SniffingEnabled;
@ -285,15 +285,15 @@ namespace ServiceLib.ViewModels
//}
//Core
_config.Inbound[0].LocalPort = localPort;
_config.Inbound[0].UdpEnabled = udpEnabled;
_config.Inbound[0].SniffingEnabled = sniffingEnabled;
_config.Inbound[0].DestOverride = destOverride?.ToList();
_config.Inbound[0].RouteOnly = routeOnly;
_config.Inbound[0].AllowLANConn = allowLANConn;
_config.Inbound[0].NewPort4LAN = newPort4LAN;
_config.Inbound[0].User = user;
_config.Inbound[0].Pass = pass;
_config.Inbound.First().LocalPort = localPort;
_config.Inbound.First().UdpEnabled = udpEnabled;
_config.Inbound.First().SniffingEnabled = sniffingEnabled;
_config.Inbound.First().DestOverride = destOverride?.ToList();
_config.Inbound.First().RouteOnly = routeOnly;
_config.Inbound.First().AllowLANConn = allowLANConn;
_config.Inbound.First().NewPort4LAN = newPort4LAN;
_config.Inbound.First().User = user;
_config.Inbound.First().Pass = pass;
if (_config.Inbound.Count > 1)
{
_config.Inbound.RemoveAt(1);

View file

@ -374,7 +374,7 @@ namespace ServiceLib.ViewModels
}
else
{
SelectedProfile = lstModel[0];
SelectedProfile = lstModel.First();
}
}
}
@ -395,7 +395,7 @@ namespace ServiceLib.ViewModels
}
else
{
SelectedSub = _subItems[0];
SelectedSub = _subItems.First();
}
}

View file

@ -456,9 +456,9 @@ namespace ServiceLib.ViewModels
sb.Append($"[{EInboundProtocol.http}:{AppHandler.Instance.GetLocalPort(EInboundProtocol.http)}]");
InboundDisplay = $"{ResUI.LabLocal}:{sb}";
if (_config.Inbound[0].AllowLANConn)
if (_config.Inbound.First().AllowLANConn)
{
if (_config.Inbound[0].NewPort4LAN)
if (_config.Inbound.First().NewPort4LAN)
{
StringBuilder sb2 = new();
sb2.Append($"[{EInboundProtocol.socks}:{AppHandler.Instance.GetLocalPort(EInboundProtocol.socks2)}]");

View file

@ -23,7 +23,7 @@ namespace v2rayN.Desktop.Views
{
clbdestOverride.Items.Add(it);
});
_config.Inbound[0].DestOverride?.ForEach(it =>
_config.Inbound.First().DestOverride?.ForEach(it =>
{
clbdestOverride.SelectedItems.Add(it);
});

View file

@ -25,7 +25,7 @@ namespace v2rayN.Views
{
clbdestOverride.Items.Add(it);
});
_config.Inbound[0].DestOverride?.ForEach(it =>
_config.Inbound.First().DestOverride?.ForEach(it =>
{
clbdestOverride.SelectedItems.Add(it);
});
@ -211,7 +211,7 @@ namespace v2rayN.Views
{
files.AddRange(Directory.GetFiles(path, pattern));
}
var culture = _config.UiItem.CurrentLanguage == Global.Languages[0] ? "zh-cn" : "en-us";
var culture = _config.UiItem.CurrentLanguage == Global.Languages.First() ? "zh-cn" : "en-us";
var culture2 = "en-us";
foreach (var ttf in files)
{