Skip to content

Commit ab6a6b8

Browse files
committed
Code clean
1 parent b218f0b commit ab6a6b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+326
-52
lines changed

v2rayN/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<TargetFramework>net8.0</TargetFramework>
99
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
1010
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
11-
<NoWarn>CA1031;CS1591;NU1507;CA1416;IDE0058</NoWarn>
11+
<NoWarn>CA1031;CS1591;NU1507;CA1416;IDE0058;IDE0053;IDE0200</NoWarn>
1212
<Nullable>annotations</Nullable>
1313
<ImplicitUsings>enable</ImplicitUsings>
1414
<Authors>2dust</Authors>

v2rayN/ServiceLib/Common/Utils.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ public static string GetPunycode(string url)
306306
public static bool IsBase64String(string? plainText)
307307
{
308308
if (plainText.IsNullOrEmpty())
309+
{
309310
return false;
311+
}
312+
310313
var buffer = new Span<byte>(new byte[plainText.Length]);
311314
return Convert.TryFromBase64String(plainText, buffer, out var _);
312315
}
@@ -520,40 +523,62 @@ public static bool IsPrivateNetwork(string ip)
520523
{
521524
// Loopback address check (127.0.0.1 for IPv4, ::1 for IPv6)
522525
if (IPAddress.IsLoopback(address))
526+
{
523527
return true;
528+
}
524529

525530
var ipBytes = address.GetAddressBytes();
526531
if (address.AddressFamily == AddressFamily.InterNetwork)
527532
{
528533
// IPv4 private address check
529534
if (ipBytes[0] == 10)
535+
{
530536
return true;
537+
}
538+
531539
if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31)
540+
{
532541
return true;
542+
}
543+
533544
if (ipBytes[0] == 192 && ipBytes[1] == 168)
545+
{
534546
return true;
547+
}
535548
}
536549
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
537550
{
538551
// IPv6 private address check
539552
// Link-local address fe80::/10
540553
if (ipBytes[0] == 0xfe && (ipBytes[1] & 0xc0) == 0x80)
554+
{
541555
return true;
556+
}
542557

543558
// Unique local address fc00::/7 (typically fd00::/8)
544559
if ((ipBytes[0] & 0xfe) == 0xfc)
560+
{
545561
return true;
562+
}
546563

547564
// Private portion in IPv4-mapped addresses ::ffff:0:0/96
548565
if (address.IsIPv4MappedToIPv6)
549566
{
550567
var ipv4Bytes = ipBytes.Skip(12).ToArray();
551568
if (ipv4Bytes[0] == 10)
569+
{
552570
return true;
571+
}
572+
553573
if (ipv4Bytes[0] == 172 && ipv4Bytes[1] >= 16 && ipv4Bytes[1] <= 31)
574+
{
554575
return true;
576+
}
577+
555578
if (ipv4Bytes[0] == 192 && ipv4Bytes[1] == 168)
579+
{
556580
return true;
581+
}
557582
}
558583
}
559584
}
@@ -708,10 +733,16 @@ public static Dictionary<string, string> GetSystemHosts()
708733
foreach (var host in hostsList)
709734
{
710735
if (host.StartsWith("#"))
736+
{
711737
continue;
738+
}
739+
712740
var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
713741
if (hostItem.Length < 2)
742+
{
714743
continue;
744+
}
745+
715746
systemHosts.Add(hostItem[1], hostItem[0]);
716747
}
717748
}

v2rayN/ServiceLib/Handler/ConfigHandler.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,9 @@ public static async Task<int> AddSubItem(Config config, string url)
16511651

16521652
var uri = Utils.TryUri(url);
16531653
if (uri == null)
1654+
{
16541655
return -1;
1656+
}
16551657
//Do not allow http protocol
16561658
if (url.StartsWith(Global.HttpProtocol) && !Utils.IsPrivateNetwork(uri.IdnHost))
16571659
{
@@ -2018,11 +2020,15 @@ public static async Task<int> InitExternalRouting(Config config, bool blImportAd
20182020
var downloadHandle = new DownloadService();
20192021
var templateContent = await downloadHandle.TryDownloadString(config.ConstItem.RouteRulesTemplateSourceUrl, true, "");
20202022
if (templateContent.IsNullOrEmpty())
2023+
{
20212024
return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback
2025+
}
20222026

20232027
var template = JsonUtils.Deserialize<RoutingTemplate>(templateContent);
20242028
if (template == null)
2029+
{
20252030
return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback
2031+
}
20262032

20272033
var items = await AppManager.Instance.RoutingItems();
20282034
var maxSort = items.Count;
@@ -2035,14 +2041,18 @@ public static async Task<int> InitExternalRouting(Config config, bool blImportAd
20352041
var item = template.RoutingItems[i];
20362042

20372043
if (item.Url.IsNullOrEmpty() && item.RuleSet.IsNullOrEmpty())
2044+
{
20382045
continue;
2046+
}
20392047

20402048
var ruleSetsString = !item.RuleSet.IsNullOrEmpty()
20412049
? item.RuleSet
20422050
: await downloadHandle.TryDownloadString(item.Url, true, "");
20432051

20442052
if (ruleSetsString.IsNullOrEmpty())
2053+
{
20452054
continue;
2055+
}
20462056

20472057
item.Remarks = $"{template.Version}-{item.Remarks}";
20482058
item.Enabled = true;
@@ -2238,17 +2248,25 @@ public static async Task<DNSItem> GetExternalDNSItem(ECoreType type, string url)
22382248
var downloadHandle = new DownloadService();
22392249
var templateContent = await downloadHandle.TryDownloadString(url, true, "");
22402250
if (templateContent.IsNullOrEmpty())
2251+
{
22412252
return currentItem;
2253+
}
22422254

22432255
var template = JsonUtils.Deserialize<DNSItem>(templateContent);
22442256
if (template == null)
2257+
{
22452258
return currentItem;
2259+
}
22462260

22472261
if (!template.NormalDNS.IsNullOrEmpty())
2262+
{
22482263
template.NormalDNS = await downloadHandle.TryDownloadString(template.NormalDNS, true, "");
2264+
}
22492265

22502266
if (!template.TunDNS.IsNullOrEmpty())
2267+
{
22512268
template.TunDNS = await downloadHandle.TryDownloadString(template.TunDNS, true, "");
2269+
}
22522270

22532271
template.Id = currentItem.Id;
22542272
template.Enabled = currentItem.Enabled;
@@ -2282,10 +2300,16 @@ public static async Task<SimpleDNSItem> GetExternalSimpleDNSItem(string url)
22822300
var downloadHandle = new DownloadService();
22832301
var templateContent = await downloadHandle.TryDownloadString(url, true, "");
22842302
if (templateContent.IsNullOrEmpty())
2303+
{
22852304
return null;
2305+
}
2306+
22862307
var template = JsonUtils.Deserialize<SimpleDNSItem>(templateContent);
22872308
if (template == null)
2309+
{
22882310
return null;
2311+
}
2312+
22892313
return template;
22902314
}
22912315

v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class Hysteria2Fmt : BaseFmt
1212

1313
var url = Utils.TryUri(str);
1414
if (url == null)
15+
{
1516
return null;
17+
}
1618

1719
item.Address = url.IdnHost;
1820
item.Port = url.Port;
@@ -32,7 +34,10 @@ public class Hysteria2Fmt : BaseFmt
3234
public static string? ToUri(ProfileItem? item)
3335
{
3436
if (item == null)
37+
{
3538
return null;
39+
}
40+
3641
var url = string.Empty;
3742

3843
var remark = string.Empty;

v2rayN/ServiceLib/Manager/ActionPrecheckManager.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,36 @@ private async Task<List<string>> ValidateNodeAndCoreSupport(ProfileItem item, EC
8181
{
8282
case EConfigType.VMess:
8383
if (item.Id.IsNullOrEmpty() || !Utils.IsGuidByParse(item.Id))
84+
{
8485
errors.Add(string.Format(ResUI.InvalidProperty, "Id"));
86+
}
87+
8588
break;
8689

8790
case EConfigType.VLESS:
8891
if (item.Id.IsNullOrEmpty() || (!Utils.IsGuidByParse(item.Id) && item.Id.Length > 30))
92+
{
8993
errors.Add(string.Format(ResUI.InvalidProperty, "Id"));
94+
}
95+
9096
if (!Global.Flows.Contains(item.Flow))
97+
{
9198
errors.Add(string.Format(ResUI.InvalidProperty, "Flow"));
99+
}
100+
92101
break;
93102

94103
case EConfigType.Shadowsocks:
95104
if (item.Id.IsNullOrEmpty())
105+
{
96106
errors.Add(string.Format(ResUI.InvalidProperty, "Id"));
107+
}
108+
97109
if (string.IsNullOrEmpty(item.Security) || !Global.SsSecuritiesInSingbox.Contains(item.Security))
110+
{
98111
errors.Add(string.Format(ResUI.InvalidProperty, "Security"));
112+
}
113+
99114
break;
100115
}
101116

v2rayN/ServiceLib/Manager/ProfileGroupItemManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,19 @@ public static bool HasCycle(string? indexId)
173173
public static bool HasCycle(string? indexId, HashSet<string> visited, HashSet<string> stack)
174174
{
175175
if (indexId.IsNullOrEmpty())
176+
{
176177
return false;
178+
}
177179

178180
if (stack.Contains(indexId))
181+
{
179182
return true;
183+
}
180184

181185
if (visited.Contains(indexId))
186+
{
182187
return false;
188+
}
183189

184190
visited.Add(indexId);
185191
stack.Add(indexId);
@@ -289,7 +295,9 @@ public static async Task<HashSet<string>> GetAllChildDomainAddresses(string inde
289295
{
290296
var childNode = await AppManager.Instance.GetProfileItem(childId);
291297
if (childNode == null)
298+
{
292299
continue;
300+
}
293301

294302
if (!childNode.IsComplex())
295303
{

v2rayN/ServiceLib/Models/ProfileItem.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,49 @@ public bool IsComplex()
6969
public bool IsValid()
7070
{
7171
if (IsComplex())
72+
{
7273
return true;
74+
}
7375

7476
if (Address.IsNullOrEmpty() || Port is <= 0 or >= 65536)
77+
{
7578
return false;
79+
}
7680

7781
switch (ConfigType)
7882
{
7983
case EConfigType.VMess:
8084
if (Id.IsNullOrEmpty() || !Utils.IsGuidByParse(Id))
85+
{
8186
return false;
87+
}
88+
8289
break;
8390

8491
case EConfigType.VLESS:
8592
if (Id.IsNullOrEmpty() || (!Utils.IsGuidByParse(Id) && Id.Length > 30))
93+
{
8694
return false;
95+
}
96+
8797
if (!Global.Flows.Contains(Flow))
98+
{
8899
return false;
100+
}
101+
89102
break;
90103

91104
case EConfigType.Shadowsocks:
92105
if (Id.IsNullOrEmpty())
106+
{
93107
return false;
108+
}
109+
94110
if (string.IsNullOrEmpty(Security) || !Global.SsSecuritiesInSingbox.Contains(Security))
111+
{
95112
return false;
113+
}
114+
96115
break;
97116
}
98117

v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ private async Task<int> GenDnsRules(SingboxConfig singboxConfig, SimpleDNSItem s
202202

203203
var routing = await ConfigHandler.GetDefaultRouting(_config);
204204
if (routing == null)
205+
{
205206
return 0;
207+
}
206208

207209
var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.RuleSet) ?? [];
208210
var expectedIPCidr = new List<string>();

v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,10 @@ private async Task<int> GenOutboundsList(List<ProfileItem> nodes, SingboxConfig
679679
{
680680
var node = nodes[i];
681681
if (node == null)
682+
{
682683
continue;
684+
}
685+
683686
if (node.ConfigType.IsGroupType())
684687
{
685688
var (childProfiles, profileGroupItem) = await ProfileGroupItemManager.GetChildProfileItems(node.IndexId);

v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ private async Task<int> GenRoutingUserRule(RulesItem item, SingboxConfig singbox
250250
foreach (var it in item.Domain)
251251
{
252252
if (ParseV2Domain(it, rule1))
253+
{
253254
countDomain++;
255+
}
254256
}
255257
if (countDomain > 0)
256258
{
@@ -265,7 +267,9 @@ private async Task<int> GenRoutingUserRule(RulesItem item, SingboxConfig singbox
265267
foreach (var it in item.Ip)
266268
{
267269
if (ParseV2Address(it, rule2))
270+
{
268271
countIp++;
272+
}
269273
}
270274
if (countIp > 0)
271275
{

0 commit comments

Comments
 (0)