Skip to content

Commit 70ddf4e

Browse files
committed
Add allowInsecure and insecure to the shared URI
2dust#8267
1 parent 187356c commit 70ddf4e

8 files changed

Lines changed: 77 additions & 39 deletions

File tree

v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class AnytlsFmt : BaseFmt
2323
item.Id = rawUserInfo;
2424

2525
var query = Utils.ParseQueryString(parsedUrl.Query);
26-
_ = ResolveStdTransport(query, ref item);
26+
ResolveUriQuery(query, ref item);
2727

2828
return item;
2929
}
@@ -41,7 +41,7 @@ public class AnytlsFmt : BaseFmt
4141
}
4242
var pw = item.Id;
4343
var dicQuery = new Dictionary<string, string>();
44-
_ = GetStdTransport(item, Global.None, ref dicQuery);
44+
ToUriQuery(item, Global.None, ref dicQuery);
4545

4646
return ToUri(EConfigType.Anytls, item.Address, item.Port, pw, dicQuery, remark);
4747
}

v2rayN/ServiceLib/Handler/Fmt/BaseFmt.cs

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace ServiceLib.Handler.Fmt;
44

55
public class BaseFmt
66
{
7+
private static readonly string[] _allowInsecureArray = new[] { "insecure", "allowInsecure", "allow_insecure", "verify" };
8+
79
protected static string GetIpv6(string address)
810
{
911
if (Utils.IsIpv6(address))
@@ -17,7 +19,7 @@ protected static string GetIpv6(string address)
1719
}
1820
}
1921

20-
protected static int GetStdTransport(ProfileItem item, string? securityDef, ref Dictionary<string, string> dicQuery)
22+
protected static int ToUriQuery(ProfileItem item, string? securityDef, ref Dictionary<string, string> dicQuery)
2123
{
2224
if (item.Flow.IsNotEmpty())
2325
{
@@ -37,11 +39,7 @@ protected static int GetStdTransport(ProfileItem item, string? securityDef, ref
3739
}
3840
if (item.Sni.IsNotEmpty())
3941
{
40-
dicQuery.Add("sni", item.Sni);
41-
}
42-
if (item.Alpn.IsNotEmpty())
43-
{
44-
dicQuery.Add("alpn", Utils.UrlEncode(item.Alpn));
42+
dicQuery.Add("sni", Utils.UrlEncode(item.Sni));
4543
}
4644
if (item.Fingerprint.IsNotEmpty())
4745
{
@@ -63,9 +61,14 @@ protected static int GetStdTransport(ProfileItem item, string? securityDef, ref
6361
{
6462
dicQuery.Add("pqv", Utils.UrlEncode(item.Mldsa65Verify));
6563
}
66-
if (item.AllowInsecure.Equals("true"))
64+
65+
if (item.StreamSecurity.Equals(Global.StreamSecurity))
6766
{
68-
dicQuery.Add("allowInsecure", "1");
67+
if (item.Alpn.IsNotEmpty())
68+
{
69+
dicQuery.Add("alpn", Utils.UrlEncode(item.Alpn));
70+
}
71+
ToUriQueryAllowInsecure(item, ref dicQuery);
6972
}
7073

7174
dicQuery.Add("type", item.Network.IsNotEmpty() ? item.Network : nameof(ETransport.tcp));
@@ -153,7 +156,40 @@ protected static int GetStdTransport(ProfileItem item, string? securityDef, ref
153156
return 0;
154157
}
155158

156-
protected static int ResolveStdTransport(NameValueCollection query, ref ProfileItem item)
159+
protected static int ToUriQueryLite(ProfileItem item, ref Dictionary<string, string> dicQuery)
160+
{
161+
if (item.Sni.IsNotEmpty())
162+
{
163+
dicQuery.Add("sni", Utils.UrlEncode(item.Sni));
164+
}
165+
if (item.Alpn.IsNotEmpty())
166+
{
167+
dicQuery.Add("alpn", Utils.UrlEncode(item.Alpn));
168+
}
169+
170+
ToUriQueryAllowInsecure(item, ref dicQuery);
171+
172+
return 0;
173+
}
174+
175+
private static int ToUriQueryAllowInsecure(ProfileItem item, ref Dictionary<string, string> dicQuery)
176+
{
177+
if (item.AllowInsecure.Equals(Global.AllowInsecure.First()))
178+
{
179+
// Add two for compatibility
180+
dicQuery.Add("insecure", "1");
181+
dicQuery.Add("allowInsecure", "1");
182+
}
183+
else
184+
{
185+
dicQuery.Add("insecure", "0");
186+
dicQuery.Add("allowInsecure", "0");
187+
}
188+
189+
return 0;
190+
}
191+
192+
protected static int ResolveUriQuery(NameValueCollection query, ref ProfileItem item)
157193
{
158194
item.Flow = GetQueryValue(query, "flow");
159195
item.StreamSecurity = GetQueryValue(query, "security");
@@ -164,7 +200,19 @@ protected static int ResolveStdTransport(NameValueCollection query, ref ProfileI
164200
item.ShortId = GetQueryDecoded(query, "sid");
165201
item.SpiderX = GetQueryDecoded(query, "spx");
166202
item.Mldsa65Verify = GetQueryDecoded(query, "pqv");
167-
item.AllowInsecure = new[] { "allowInsecure", "allow_insecure", "insecure" }.Any(k => (query[k] ?? "") == "1") ? "true" : "";
203+
204+
if (_allowInsecureArray.Any(k => GetQueryDecoded(query, k) == "1"))
205+
{
206+
item.AllowInsecure = Global.AllowInsecure.First();
207+
}
208+
else if (_allowInsecureArray.Any(k => GetQueryDecoded(query, k) == "0"))
209+
{
210+
item.AllowInsecure = Global.AllowInsecure.Skip(1).First();
211+
}
212+
else
213+
{
214+
item.AllowInsecure = string.Empty;
215+
}
168216

169217
item.Network = GetQueryValue(query, "type", nameof(ETransport.tcp));
170218
switch (item.Network)

v2rayN/ServiceLib/Handler/Fmt/Hysteria2Fmt.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ public class Hysteria2Fmt : BaseFmt
2222
item.Id = Utils.UrlDecode(url.UserInfo);
2323

2424
var query = Utils.ParseQueryString(url.Query);
25-
ResolveStdTransport(query, ref item);
25+
ResolveUriQuery(query, ref item);
2626
item.Path = GetQueryDecoded(query, "obfs-password");
27-
item.AllowInsecure = GetQueryValue(query, "insecure") == "1" ? "true" : "false";
28-
2927
item.Ports = GetQueryDecoded(query, "mport");
3028

3129
return item;
@@ -46,20 +44,13 @@ public class Hysteria2Fmt : BaseFmt
4644
remark = "#" + Utils.UrlEncode(item.Remarks);
4745
}
4846
var dicQuery = new Dictionary<string, string>();
49-
if (item.Sni.IsNotEmpty())
50-
{
51-
dicQuery.Add("sni", item.Sni);
52-
}
53-
if (item.Alpn.IsNotEmpty())
54-
{
55-
dicQuery.Add("alpn", Utils.UrlEncode(item.Alpn));
56-
}
47+
ToUriQueryLite(item, ref dicQuery);
48+
5749
if (item.Path.IsNotEmpty())
5850
{
5951
dicQuery.Add("obfs", "salamander");
6052
dicQuery.Add("obfs-password", Utils.UrlEncode(item.Path));
6153
}
62-
dicQuery.Add("insecure", item.AllowInsecure.ToLower() == "true" ? "1" : "0");
6354
if (item.Ports.IsNotEmpty())
6455
{
6556
dicQuery.Add("mport", Utils.UrlEncode(item.Ports.Replace(':', '-')));

v2rayN/ServiceLib/Handler/Fmt/TrojanFmt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class TrojanFmt : BaseFmt
2323
item.Id = Utils.UrlDecode(url.UserInfo);
2424

2525
var query = Utils.ParseQueryString(url.Query);
26-
_ = ResolveStdTransport(query, ref item);
26+
ResolveUriQuery(query, ref item);
2727

2828
return item;
2929
}
@@ -40,7 +40,7 @@ public class TrojanFmt : BaseFmt
4040
remark = "#" + Utils.UrlEncode(item.Remarks);
4141
}
4242
var dicQuery = new Dictionary<string, string>();
43-
_ = GetStdTransport(item, null, ref dicQuery);
43+
ToUriQuery(item, null, ref dicQuery);
4444

4545
return ToUri(EConfigType.Trojan, item.Address, item.Port, item.Id, dicQuery, remark);
4646
}

v2rayN/ServiceLib/Handler/Fmt/TuicFmt.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class TuicFmt : BaseFmt
2929
}
3030

3131
var query = Utils.ParseQueryString(url.Query);
32-
ResolveStdTransport(query, ref item);
32+
ResolveUriQuery(query, ref item);
3333
item.HeaderType = GetQueryValue(query, "congestion_control");
3434

3535
return item;
@@ -47,15 +47,10 @@ public class TuicFmt : BaseFmt
4747
{
4848
remark = "#" + Utils.UrlEncode(item.Remarks);
4949
}
50+
5051
var dicQuery = new Dictionary<string, string>();
51-
if (item.Sni.IsNotEmpty())
52-
{
53-
dicQuery.Add("sni", item.Sni);
54-
}
55-
if (item.Alpn.IsNotEmpty())
56-
{
57-
dicQuery.Add("alpn", Utils.UrlEncode(item.Alpn));
58-
}
52+
ToUriQueryLite(item, ref dicQuery);
53+
5954
dicQuery.Add("congestion_control", item.HeaderType);
6055

6156
return ToUri(EConfigType.TUIC, item.Address, item.Port, $"{item.Id}:{item.Security}", dicQuery, remark);

v2rayN/ServiceLib/Handler/Fmt/VLESSFmt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class VLESSFmt : BaseFmt
2626
var query = Utils.ParseQueryString(url.Query);
2727
item.Security = GetQueryValue(query, "encryption", Global.None);
2828
item.StreamSecurity = GetQueryValue(query, "security");
29-
_ = ResolveStdTransport(query, ref item);
29+
ResolveUriQuery(query, ref item);
3030

3131
return item;
3232
}
@@ -52,7 +52,7 @@ public class VLESSFmt : BaseFmt
5252
{
5353
dicQuery.Add("encryption", Global.None);
5454
}
55-
_ = GetStdTransport(item, Global.None, ref dicQuery);
55+
ToUriQuery(item, Global.None, ref dicQuery);
5656

5757
return ToUri(EConfigType.VLESS, item.Address, item.Port, item.Id, dicQuery, remark);
5858
}

v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public class VmessFmt : BaseFmt
3939
tls = item.StreamSecurity,
4040
sni = item.Sni,
4141
alpn = item.Alpn,
42-
fp = item.Fingerprint
42+
fp = item.Fingerprint,
43+
insecure = item.AllowInsecure.Equals(Global.AllowInsecure.First()) ? "1" : "0"
4344
};
4445

4546
var url = JsonUtils.Serialize(vmessQRCode);
@@ -94,6 +95,7 @@ public class VmessFmt : BaseFmt
9495
item.Sni = Utils.ToString(vmessQRCode.sni);
9596
item.Alpn = Utils.ToString(vmessQRCode.alpn);
9697
item.Fingerprint = Utils.ToString(vmessQRCode.fp);
98+
item.AllowInsecure = vmessQRCode.insecure == "1" ? Global.AllowInsecure.First() : string.Empty;
9799

98100
return item;
99101
}
@@ -118,7 +120,7 @@ public class VmessFmt : BaseFmt
118120
item.Id = Utils.UrlDecode(url.UserInfo);
119121

120122
var query = Utils.ParseQueryString(url.Query);
121-
ResolveStdTransport(query, ref item);
123+
ResolveUriQuery(query, ref item);
122124

123125
return item;
124126
}

v2rayN/ServiceLib/Models/VmessQRCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ public class VmessQRCode
3838
public string alpn { get; set; } = string.Empty;
3939

4040
public string fp { get; set; } = string.Empty;
41+
42+
public string insecure { get; set; } = string.Empty;
4143
}

0 commit comments

Comments
 (0)