Skip to content

Commit ad11a7e

Browse files
authored
Support new hysteria2 stream settings (#8908)
* Support new hysteria2 stream settings * Fix * Sync
1 parent 92c8c14 commit ad11a7e

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

v2rayN/ServiceLib/Models/V2rayConfig.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public class StreamSettings4Ray
337337

338338
public HysteriaSettings4Ray? hysteriaSettings { get; set; }
339339

340-
public Finalmask4Ray? finalmask { get; set; }
340+
public object? finalmask { get; set; }
341341

342342
public Sockopt4Ray? sockopt { get; set; }
343343
}
@@ -462,27 +462,24 @@ public class HysteriaSettings4Ray
462462
{
463463
public int version { get; set; }
464464
public string? auth { get; set; }
465-
public string? up { get; set; }
466-
public string? down { get; set; }
467-
public HysteriaUdpHop4Ray? udphop { get; set; }
468465
}
469466

470-
public class HysteriaUdpHop4Ray
467+
public class UdpHop4Ray
471468
{
472-
public string? port { get; set; }
469+
public string? ports { get; set; }
473470
public string? interval { get; set; }
474471
}
475472

476473
public class Finalmask4Ray
477474
{
478-
public List<Mask4Ray>? tcp { get; set; }
479475
public List<Mask4Ray>? udp { get; set; }
476+
public QuicParams4Ray? quicParams { get; set; }
480477
}
481478

482479
public class Mask4Ray
483480
{
484481
public string type { get; set; }
485-
public object? settings { get; set; }
482+
public MaskSettings4Ray? settings { get; set; }
486483
}
487484

488485
public class MaskSettings4Ray
@@ -491,6 +488,14 @@ public class MaskSettings4Ray
491488
public string? domain { get; set; }
492489
}
493490

491+
public class QuicParams4Ray
492+
{
493+
public string? congestion { get; set; }
494+
public string? brutalUp { get; set; }
495+
public string? brutalDown { get; set; }
496+
public UdpHop4Ray? udpHop { get; set; }
497+
}
498+
494499
public class AccountsItem4Ray
495500
{
496501
public string user { get; set; }

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,14 @@ private void FillOutbound(Outbound4Sbox outbound)
224224
password = protocolExtra.SalamanderPass.TrimEx(),
225225
};
226226
}
227-
228-
outbound.up_mbps = protocolExtra?.UpMbps is { } su and >= 0
227+
int? upMbps = protocolExtra?.UpMbps is { } su and >= 0
229228
? su
230229
: _config.HysteriaItem.UpMbps;
231-
outbound.down_mbps = protocolExtra?.DownMbps is { } sd and >= 0
230+
int? downMbps = protocolExtra?.DownMbps is { } sd and >= 0
232231
? sd
233-
: _config.HysteriaItem.DownMbps;
232+
: _config.HysteriaItem.UpMbps;
233+
outbound.up_mbps = upMbps > 0 ? upMbps : null;
234+
outbound.down_mbps = downMbps > 0 ? downMbps : null;
234235
var ports = protocolExtra?.Ports?.IsNullOrEmpty() == false ? protocolExtra.Ports : null;
235236
if ((!ports.IsNullOrEmpty()) && (ports.Contains(':') || ports.Contains('-') || ports.Contains(',')))
236237
{

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
441441
kcpSettings.congestion = _config.KcpItem.Congestion;
442442
kcpSettings.readBufferSize = _config.KcpItem.ReadBufferSize;
443443
kcpSettings.writeBufferSize = _config.KcpItem.WriteBufferSize;
444-
streamSettings.finalmask ??= new();
444+
var kcpFinalmask = new Finalmask4Ray();
445445
if (Global.KcpHeaderMaskMap.TryGetValue(_node.HeaderType, out var header))
446446
{
447-
streamSettings.finalmask.udp =
447+
kcpFinalmask.udp =
448448
[
449449
new Mask4Ray
450450
{
@@ -453,23 +453,24 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
453453
}
454454
];
455455
}
456-
streamSettings.finalmask.udp ??= [];
456+
kcpFinalmask.udp ??= [];
457457
if (path.IsNullOrEmpty())
458458
{
459-
streamSettings.finalmask.udp.Add(new Mask4Ray
459+
kcpFinalmask.udp.Add(new Mask4Ray
460460
{
461461
type = "mkcp-original"
462462
});
463463
}
464464
else
465465
{
466-
streamSettings.finalmask.udp.Add(new Mask4Ray
466+
kcpFinalmask.udp.Add(new Mask4Ray
467467
{
468468
type = "mkcp-aes128gcm",
469469
settings = new MaskSettings4Ray { password = path }
470470
});
471471
}
472472
streamSettings.kcpSettings = kcpSettings;
473+
streamSettings.finalmask = kcpFinalmask;
473474
break;
474475
//ws
475476
case nameof(ETransport.ws):
@@ -598,36 +599,46 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
598599
: (_config.HysteriaItem.HopInterval >= 5
599600
? _config.HysteriaItem.HopInterval
600601
: Global.Hysteria2DefaultHopInt).ToString();
601-
HysteriaUdpHop4Ray? udpHop = null;
602+
var hy2Finalmask = new Finalmask4Ray();
603+
var quicParams = new QuicParams4Ray();
602604
if (!ports.IsNullOrEmpty() &&
603605
(ports.Contains(':') || ports.Contains('-') || ports.Contains(',')))
604606
{
605-
udpHop = new HysteriaUdpHop4Ray
607+
var udpHop = new UdpHop4Ray
606608
{
607-
port = ports.Replace(':', '-'),
609+
ports = ports.Replace(':', '-'),
608610
interval = hopInterval,
609611
};
612+
quicParams.udpHop = udpHop;
613+
}
614+
if (upMbps > 0 || downMbps > 0)
615+
{
616+
quicParams.congestion = "brutal";
617+
quicParams.brutalUp = upMbps > 0 ? $"{upMbps}mbps" : null;
618+
quicParams.brutalDown = downMbps > 0 ? $"{downMbps}mbps" : null;
619+
}
620+
else
621+
{
622+
quicParams.congestion = "bbr";
623+
}
624+
hy2Finalmask.quicParams = quicParams;
625+
if (!protocolExtra.SalamanderPass.IsNullOrEmpty())
626+
{
627+
hy2Finalmask.udp =
628+
[
629+
new Mask4Ray
630+
{
631+
type = "salamander",
632+
settings = new MaskSettings4Ray { password = protocolExtra.SalamanderPass.TrimEx(), }
633+
}
634+
];
610635
}
611636
streamSettings.hysteriaSettings = new()
612637
{
613638
version = 2,
614639
auth = _node.Password,
615-
up = upMbps > 0 ? $"{upMbps}mbps" : null,
616-
down = downMbps > 0 ? $"{downMbps}mbps" : null,
617-
udphop = udpHop,
618640
};
619-
if (!protocolExtra.SalamanderPass.IsNullOrEmpty())
620-
{
621-
streamSettings.finalmask ??= new();
622-
streamSettings.finalmask.udp =
623-
[
624-
new Mask4Ray
625-
{
626-
type = "salamander",
627-
settings = new MaskSettings4Ray { password = protocolExtra.SalamanderPass.TrimEx(), }
628-
}
629-
];
630-
}
641+
streamSettings.finalmask = hy2Finalmask;
631642
break;
632643

633644
default:
@@ -665,7 +676,7 @@ private void FillBoundStreamSettings(Outbounds4Ray outbound)
665676

666677
if (!_node.Finalmask.IsNullOrEmpty())
667678
{
668-
streamSettings.finalmask = JsonUtils.Deserialize<Finalmask4Ray>(_node.Finalmask);
679+
streamSettings.finalmask = JsonUtils.ParseJson(_node.Finalmask);
669680
}
670681
}
671682
catch (Exception ex)

0 commit comments

Comments
 (0)