Skip to content

Commit 7ddb46e

Browse files
authored
Xray browser header masquerading (2dust#8981)
1 parent ad11a7e commit 7ddb46e

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

v2rayN/ServiceLib/Global.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,14 @@ public class Global
182182
@"https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/main/v2rayN/"
183183
];
184184

185-
public static readonly Dictionary<string, string> UserAgentTexts = new()
185+
public static readonly Dictionary<string, string> TcpHttpUserAgentTexts = new()
186186
{
187187
{"chrome","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" },
188188
{"firefox","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0" },
189189
{"safari","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" },
190190
{"edge","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.70" },
191-
{"none",""}
191+
{"none",""},
192+
{"golang",""}
192193
};
193194

194195
public const string Hysteria2ProtocolShare = "hy2://";
@@ -391,9 +392,8 @@ public class Global
391392
[
392393
"chrome",
393394
"firefox",
394-
"safari",
395395
"edge",
396-
"none"
396+
"golang"
397397
];
398398

399399
public static readonly List<string> XhttpMode =

v2rayN/ServiceLib/Models/V2rayConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ public class HttpupgradeSettings4Ray
421421
public string? path { get; set; }
422422

423423
public string? host { get; set; }
424+
425+
public Headers4Ray headers { get; set; }
424426
}
425427

426428
public class XhttpSettings4Ray
@@ -456,6 +458,7 @@ public class GrpcSettings4Ray
456458
public int? health_check_timeout { get; set; }
457459
public bool? permit_without_stream { get; set; }
458460
public int? initial_windows_size { get; set; }
461+
public string? user_agent { get; set; }
459462
}
460463

461464
public class HysteriaSettings4Ray

v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,7 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
354354
var host = _node.RequestHost.TrimEx();
355355
var path = _node.Path.TrimEx();
356356
var sni = _node.Sni.TrimEx();
357-
var useragent = "";
358-
if (!_config.CoreBasicItem.DefUserAgent.IsNullOrEmpty())
359-
{
360-
useragent = Global.UserAgentTexts.GetValueOrDefault(_config.CoreBasicItem.DefUserAgent, _config.CoreBasicItem.DefUserAgent);
361-
}
357+
var useragent = _config.CoreBasicItem.DefUserAgent ?? string.Empty;
362358

363359
//if tls
364360
if (_node.StreamSecurity == Global.StreamSecurity)
@@ -496,13 +492,17 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
496492
case nameof(ETransport.httpupgrade):
497493
HttpupgradeSettings4Ray httpupgradeSettings = new();
498494

495+
if (host.IsNotEmpty())
496+
{
497+
httpupgradeSettings.host = host;
498+
}
499499
if (path.IsNotEmpty())
500500
{
501501
httpupgradeSettings.path = path;
502502
}
503-
if (host.IsNotEmpty())
503+
if (useragent.IsNotEmpty())
504504
{
505-
httpupgradeSettings.host = host;
505+
httpupgradeSettings.headers.UserAgent = useragent;
506506
}
507507
streamSettings.httpupgradeSettings = httpupgradeSettings;
508508

@@ -581,6 +581,7 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
581581
health_check_timeout = _config.GrpcItem.HealthCheckTimeout,
582582
permit_without_stream = _config.GrpcItem.PermitWithoutStream,
583583
initial_windows_size = _config.GrpcItem.InitialWindowsSize,
584+
user_agent = useragent.NullIfEmpty(),
584585
};
585586
streamSettings.grpcSettings = grpcSettings;
586587
break;
@@ -655,10 +656,11 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
655656

656657
//request Host
657658
var request = EmbedUtils.GetEmbedText(Global.V2raySampleHttpRequestFileName);
659+
var useragentValue = Global.TcpHttpUserAgentTexts.GetValueOrDefault(useragent, useragent);
658660
var arrHost = host.Split(',');
659661
var host2 = string.Join(",".AppendQuotes(), arrHost);
660662
request = request.Replace("$requestHost$", $"{host2.AppendQuotes()}");
661-
request = request.Replace("$requestUserAgent$", $"{useragent.AppendQuotes()}");
663+
request = request.Replace("$requestUserAgent$", $"{useragentValue.AppendQuotes()}");
662664
//Path
663665
var pathHttp = @"/";
664666
if (path.IsNotEmpty())

0 commit comments

Comments
 (0)