Skip to content

Commit 677e81f

Browse files
DHR602dust
andauthored
Refactor profile item config (2dust#8659)
* Refactor * Add hysteria2 bandwidth and hop interval support * Upgrade config version and rename * Refactor id and security * Refactor flow * Fix hy2 bbr * Fix warning CS0618 * Remove unused code * Fix hy2 migrate * Fix * Refactor * Refactor ProfileItem protocol extra handling * Refactor, use record instead of class * Hy2 SalamanderPass * Fix Tuic * Fix group * Fix Tuic * Fix hy2 Brutal Bandwidth * Clean Code * Fix * Support interval range * Add Username --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
1 parent d9843dc commit 677e81f

40 files changed

Lines changed: 951 additions & 802 deletions

v2rayN/ServiceLib/Global.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public class Global
9090
public const string SingboxFakeDNSTag = "fake_dns";
9191
public const string SingboxEchDNSTag = "ech_dns";
9292

93+
public const int Hysteria2DefaultHopInt = 10;
94+
9395
public static readonly List<string> IEProxyProtocols =
9496
[
9597
"{ip}:{http_port}",

v2rayN/ServiceLib/Handler/ConfigHandler.cs

Lines changed: 68 additions & 91 deletions
Large diffs are not rendered by default.

v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class AnytlsFmt : BaseFmt
2020
Port = parsedUrl.Port,
2121
};
2222
var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo);
23-
item.Id = rawUserInfo;
23+
item.Password = rawUserInfo;
2424

2525
var query = Utils.ParseQueryString(parsedUrl.Query);
2626
ResolveUriQuery(query, ref item);
@@ -39,7 +39,7 @@ public class AnytlsFmt : BaseFmt
3939
{
4040
remark = "#" + Utils.UrlEncode(item.Remarks);
4141
}
42-
var pw = item.Id;
42+
var pw = item.Password;
4343
var dicQuery = new Dictionary<string, string>();
4444
ToUriQuery(item, Global.None, ref dicQuery);
4545

v2rayN/ServiceLib/Handler/Fmt/BaseFmt.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ protected static string GetIpv6(string address)
2121

2222
protected static int ToUriQuery(ProfileItem item, string? securityDef, ref Dictionary<string, string> dicQuery)
2323
{
24-
if (item.Flow.IsNotEmpty())
25-
{
26-
dicQuery.Add("flow", item.Flow);
27-
}
28-
2924
if (item.StreamSecurity.IsNotEmpty())
3025
{
3126
dicQuery.Add("security", item.StreamSecurity);
@@ -208,7 +203,6 @@ private static int ToUriQueryAllowInsecure(ProfileItem item, ref Dictionary<stri
208203

209204
protected static int ResolveUriQuery(NameValueCollection query, ref ProfileItem item)
210205
{
211-
item.Flow = GetQueryValue(query, "flow");
212206
item.StreamSecurity = GetQueryValue(query, "security");
213207
item.Sni = GetQueryValue(query, "sni");
214208
item.Alpn = GetQueryDecoded(query, "alpn");

v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@ public class Hysteria2Fmt : BaseFmt
1919
item.Address = url.IdnHost;
2020
item.Port = url.Port;
2121
item.Remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
22-
item.Id = Utils.UrlDecode(url.UserInfo);
22+
item.Password = Utils.UrlDecode(url.UserInfo);
2323

2424
var query = Utils.ParseQueryString(url.Query);
2525
ResolveUriQuery(query, ref item);
26-
item.Path = GetQueryDecoded(query, "obfs-password");
27-
item.Ports = GetQueryDecoded(query, "mport");
2826
if (item.CertSha.IsNullOrEmpty())
2927
{
3028
item.CertSha = GetQueryDecoded(query, "pinSHA256");
3129
}
30+
item.SetProtocolExtra(item.GetProtocolExtra() with
31+
{
32+
Ports = GetQueryDecoded(query, "mport"),
33+
SalamanderPass = GetQueryDecoded(query, "obfs-password"),
34+
});
3235

3336
return item;
3437
}
@@ -49,15 +52,16 @@ public class Hysteria2Fmt : BaseFmt
4952
}
5053
var dicQuery = new Dictionary<string, string>();
5154
ToUriQueryLite(item, ref dicQuery);
55+
var protocolExtraItem = item.GetProtocolExtra();
5256

53-
if (item.Path.IsNotEmpty())
57+
if (!protocolExtraItem.SalamanderPass.IsNullOrEmpty())
5458
{
5559
dicQuery.Add("obfs", "salamander");
56-
dicQuery.Add("obfs-password", Utils.UrlEncode(item.Path));
60+
dicQuery.Add("obfs-password", Utils.UrlEncode(protocolExtraItem.SalamanderPass));
5761
}
58-
if (item.Ports.IsNotEmpty())
62+
if (!protocolExtraItem.Ports.IsNullOrEmpty())
5963
{
60-
dicQuery.Add("mport", Utils.UrlEncode(item.Ports.Replace(':', '-')));
64+
dicQuery.Add("mport", Utils.UrlEncode(protocolExtraItem.Ports.Replace(':', '-')));
6165
}
6266
if (!item.CertSha.IsNullOrEmpty())
6367
{
@@ -70,7 +74,7 @@ public class Hysteria2Fmt : BaseFmt
7074
dicQuery.Add("pinSHA256", Utils.UrlEncode(sha));
7175
}
7276

73-
return ToUri(EConfigType.Hysteria2, item.Address, item.Port, item.Id, dicQuery, remark);
77+
return ToUri(EConfigType.Hysteria2, item.Address, item.Port, item.Password, dicQuery, remark);
7478
}
7579

7680
public static ProfileItem? ResolveFull2(string strData, string? subRemarks)

v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class ShadowsocksFmt : BaseFmt
1212
{
1313
return null;
1414
}
15-
if (item.Address.Length == 0 || item.Port == 0 || item.Security.Length == 0 || item.Id.Length == 0)
15+
16+
if (item.Address.Length == 0 || item.Port == 0 || item.GetProtocolExtra().SsMethod.IsNullOrEmpty() || item.Password.Length == 0)
1617
{
1718
return null;
1819
}
@@ -40,7 +41,7 @@ public class ShadowsocksFmt : BaseFmt
4041
// item.port);
4142
//url = Utile.Base64Encode(url);
4243
//new Sip002
43-
var pw = Utils.Base64Encode($"{item.Security}:{item.Id}", true);
44+
var pw = Utils.Base64Encode($"{item.GetProtocolExtra().SsMethod}:{item.Password}", true);
4445

4546
// plugin
4647
var plugin = string.Empty;
@@ -136,8 +137,8 @@ public class ShadowsocksFmt : BaseFmt
136137
{
137138
return null;
138139
}
139-
item.Security = details.Groups["method"].Value;
140-
item.Id = details.Groups["password"].Value;
140+
item.SetProtocolExtra(item.GetProtocolExtra() with { SsMethod = details.Groups["method"].Value });
141+
item.Password = details.Groups["password"].Value;
141142
item.Address = details.Groups["hostname"].Value;
142143
item.Port = details.Groups["port"].Value.ToInt();
143144
return item;
@@ -166,8 +167,8 @@ public class ShadowsocksFmt : BaseFmt
166167
{
167168
return null;
168169
}
169-
item.Security = userInfoParts.First();
170-
item.Id = Utils.UrlDecode(userInfoParts.Last());
170+
item.SetProtocolExtra(item.GetProtocolExtra() with { SsMethod = userInfoParts.First() });
171+
item.Password = Utils.UrlDecode(userInfoParts.Last());
171172
}
172173
else
173174
{
@@ -178,8 +179,8 @@ public class ShadowsocksFmt : BaseFmt
178179
{
179180
return null;
180181
}
181-
item.Security = userInfoParts.First();
182-
item.Id = userInfoParts.Last();
182+
item.SetProtocolExtra(item.GetProtocolExtra() with { SsMethod = userInfoParts.First() });
183+
item.Password = userInfoParts.Last();
183184
}
184185

185186
var queryParameters = Utils.ParseQueryString(parsedUrl.Query);
@@ -275,7 +276,6 @@ public class ShadowsocksFmt : BaseFmt
275276
}
276277
}
277278
}
278-
279279
return item;
280280
}
281281

@@ -300,11 +300,11 @@ public class ShadowsocksFmt : BaseFmt
300300
var ssItem = new ProfileItem()
301301
{
302302
Remarks = it.remarks,
303-
Security = it.method,
304-
Id = it.password,
303+
Password = it.password,
305304
Address = it.server,
306305
Port = it.server_port.ToInt()
307306
};
307+
ssItem.SetProtocolExtra(new ProtocolExtraItem() { SsMethod = it.method });
308308
lst.Add(ssItem);
309309
}
310310
return lst;

v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SocksFmt : BaseFmt
3333
remark = "#" + Utils.UrlEncode(item.Remarks);
3434
}
3535
//new
36-
var pw = Utils.Base64Encode($"{item.Security}:{item.Id}", true);
36+
var pw = Utils.Base64Encode($"{item.Username}:{item.Password}", true);
3737
return ToUri(EConfigType.SOCKS, item.Address, item.Port, pw, null, remark);
3838
}
3939

@@ -78,9 +78,8 @@ public class SocksFmt : BaseFmt
7878
}
7979
item.Address = arr1[1][..indexPort];
8080
item.Port = arr1[1][(indexPort + 1)..].ToInt();
81-
item.Security = arr21.First();
82-
item.Id = arr21[1];
83-
81+
item.Username = arr21.First();
82+
item.Password = arr21[1];
8483
return item;
8584
}
8685

@@ -98,15 +97,14 @@ public class SocksFmt : BaseFmt
9897
Address = parsedUrl.IdnHost,
9998
Port = parsedUrl.Port,
10099
};
101-
102100
// parse base64 UserInfo
103101
var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo);
104102
var userInfo = Utils.Base64Decode(rawUserInfo);
105103
var userInfoParts = userInfo.Split([':'], 2);
106104
if (userInfoParts.Length == 2)
107105
{
108-
item.Security = userInfoParts.First();
109-
item.Id = userInfoParts[1];
106+
item.Username = userInfoParts.First();
107+
item.Password = userInfoParts[1];
110108
}
111109

112110
return item;

v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public class TrojanFmt : BaseFmt
2020
item.Address = url.IdnHost;
2121
item.Port = url.Port;
2222
item.Remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
23-
item.Id = Utils.UrlDecode(url.UserInfo);
23+
item.Password = Utils.UrlDecode(url.UserInfo);
2424

2525
var query = Utils.ParseQueryString(url.Query);
26+
item.SetProtocolExtra(item.GetProtocolExtra() with { Flow = GetQueryValue(query, "flow") });
2627
ResolveUriQuery(query, ref item);
2728

2829
return item;
@@ -40,8 +41,12 @@ public class TrojanFmt : BaseFmt
4041
remark = "#" + Utils.UrlEncode(item.Remarks);
4142
}
4243
var dicQuery = new Dictionary<string, string>();
44+
if (!item.GetProtocolExtra().Flow.IsNullOrEmpty())
45+
{
46+
dicQuery.Add("flow", item.GetProtocolExtra().Flow);
47+
}
4348
ToUriQuery(item, null, ref dicQuery);
4449

45-
return ToUri(EConfigType.Trojan, item.Address, item.Port, item.Id, dicQuery, remark);
50+
return ToUri(EConfigType.Trojan, item.Address, item.Port, item.Password, dicQuery, remark);
4651
}
4752
}

v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class TuicFmt : BaseFmt
2424
var userInfoParts = rawUserInfo.Split(new[] { ':' }, 2);
2525
if (userInfoParts.Length == 2)
2626
{
27-
item.Id = userInfoParts.First();
28-
item.Security = userInfoParts.Last();
27+
item.Username = userInfoParts.First();
28+
item.Password = userInfoParts.Last();
2929
}
3030

3131
var query = Utils.ParseQueryString(url.Query);
@@ -53,6 +53,6 @@ public class TuicFmt : BaseFmt
5353

5454
dicQuery.Add("congestion_control", item.HeaderType);
5555

56-
return ToUri(EConfigType.TUIC, item.Address, item.Port, $"{item.Id}:{item.Security}", dicQuery, remark);
56+
return ToUri(EConfigType.TUIC, item.Address, item.Port, $"{item.Username ?? ""}:{item.Password}", dicQuery, remark);
5757
}
5858
}

v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public class VLESSFmt : BaseFmt
99
ProfileItem item = new()
1010
{
1111
ConfigType = EConfigType.VLESS,
12-
Security = Global.None
1312
};
1413

1514
var url = Utils.TryUri(str);
@@ -21,10 +20,14 @@ public class VLESSFmt : BaseFmt
2120
item.Address = url.IdnHost;
2221
item.Port = url.Port;
2322
item.Remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
24-
item.Id = Utils.UrlDecode(url.UserInfo);
23+
item.Password = Utils.UrlDecode(url.UserInfo);
2524

2625
var query = Utils.ParseQueryString(url.Query);
27-
item.Security = GetQueryValue(query, "encryption", Global.None);
26+
item.SetProtocolExtra(item.GetProtocolExtra() with
27+
{
28+
VlessEncryption = GetQueryValue(query, "encryption", Global.None),
29+
Flow = GetQueryValue(query, "flow")
30+
});
2831
item.StreamSecurity = GetQueryValue(query, "security");
2932
ResolveUriQuery(query, ref item);
3033

@@ -44,16 +47,14 @@ public class VLESSFmt : BaseFmt
4447
remark = "#" + Utils.UrlEncode(item.Remarks);
4548
}
4649
var dicQuery = new Dictionary<string, string>();
47-
if (item.Security.IsNotEmpty())
48-
{
49-
dicQuery.Add("encryption", item.Security);
50-
}
51-
else
50+
dicQuery.Add("encryption",
51+
!item.GetProtocolExtra().VlessEncryption.IsNullOrEmpty() ? item.GetProtocolExtra().VlessEncryption : Global.None);
52+
if (!item.GetProtocolExtra().Flow.IsNullOrEmpty())
5253
{
53-
dicQuery.Add("encryption", Global.None);
54+
dicQuery.Add("flow", item.GetProtocolExtra().Flow);
5455
}
5556
ToUriQuery(item, Global.None, ref dicQuery);
5657

57-
return ToUri(EConfigType.VLESS, item.Address, item.Port, item.Id, dicQuery, remark);
58+
return ToUri(EConfigType.VLESS, item.Address, item.Port, item.Password, dicQuery, remark);
5859
}
5960
}

0 commit comments

Comments
 (0)