Skip to content

Commit 35b98f9

Browse files
authored
Support new fragment (2dust#9122)
1 parent cabd0df commit 35b98f9

3 files changed

Lines changed: 84 additions & 33 deletions

File tree

v2rayN/ServiceLib/Handler/ConfigHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static class ConfigHandler
162162
config.Fragment4RayItem ??= new()
163163
{
164164
Packets = "tlshello",
165-
Length = "100-200",
165+
Length = "50-100",
166166
Interval = "10-20"
167167
};
168168
config.GlobalHotkeys ??= new();

v2rayN/ServiceLib/Models/V2rayConfig.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ public class Outboundsettings4Ray
140140

141141
public int? userLevel { get; set; }
142142

143-
public FragmentItem4Ray? fragment { get; set; }
144-
145143
public string? secretKey { get; set; }
146144

147-
public Object? address { get; set; }
145+
public object? address { get; set; }
146+
148147
public int? port { get; set; }
149148

150149
public List<WireguardPeer4Ray>? peers { get; set; }
@@ -501,6 +500,19 @@ public class MaskSettings4Ray
501500
{
502501
public string? password { get; set; }
503502
public string? domain { get; set; }
503+
// fragment
504+
public string? packets { get; set; }
505+
public string? length { get; set; }
506+
public string? delay { get; set; }
507+
// noise
508+
public int? reset { get; set; }
509+
public List<NoiseMask4Ray>? noise { get; set; }
510+
}
511+
512+
public class NoiseMask4Ray
513+
{
514+
public string? rand { get; set; }
515+
public string? delay { get; set; }
504516
}
505517

506518
public class QuicParams4Ray

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

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ private void GenOutbounds()
1212
GenObservatory(multipleLoad);
1313
GenBalancer(multipleLoad);
1414
}
15+
if (_config.CoreBasicItem.EnableFragment)
16+
{
17+
ApplyOutboundFragment();
18+
}
1519
if (context.IsTunEnabled)
1620
{
1721
_coreConfig.outbounds.Add(BuildDnsOutbound());
@@ -29,35 +33,6 @@ private List<Outbounds4Ray> BuildAllProxyOutbounds(string baseTagName = Global.P
2933
{
3034
proxyOutboundList.Add(BuildProxyOutbound(baseTagName));
3135
}
32-
33-
if (_config.CoreBasicItem.EnableFragment)
34-
{
35-
var fragmentOutbound = new Outbounds4Ray
36-
{
37-
protocol = "freedom",
38-
tag = $"frag-{baseTagName}",
39-
settings = new()
40-
{
41-
fragment = new()
42-
{
43-
packets = _config.Fragment4RayItem?.Packets,
44-
length = _config.Fragment4RayItem?.Length,
45-
interval = _config.Fragment4RayItem?.Interval
46-
}
47-
}
48-
};
49-
var actOutboundWithTlsList =
50-
proxyOutboundList.Where(n => n.streamSettings?.security.IsNullOrEmpty() == false
51-
&& (n.streamSettings?.sockopt?.dialerProxy?.IsNullOrEmpty() ?? true)).ToList();
52-
if (actOutboundWithTlsList.Count > 0)
53-
{
54-
proxyOutboundList.Add(fragmentOutbound);
55-
}
56-
foreach (var outbound in actOutboundWithTlsList)
57-
{
58-
FillDialerProxy(outbound, fragmentOutbound.tag);
59-
}
60-
}
6136
return proxyOutboundList;
6237
}
6338

@@ -837,4 +812,68 @@ private static Outbounds4Ray BuildDnsOutbound()
837812
var outbound = new Outbounds4Ray { tag = Global.DnsOutboundTag, protocol = "dns", };
838813
return outbound;
839814
}
815+
816+
private void ApplyOutboundFragment()
817+
{
818+
var actOutboundWithTlsList =
819+
_coreConfig.outbounds.Where(n => n.streamSettings?.security.IsNullOrEmpty() == false
820+
&& (n.streamSettings?.sockopt?.dialerProxy?.IsNullOrEmpty() ?? true))
821+
.ToList();
822+
823+
var configPackets = _config.Fragment4RayItem?.Packets ?? "tlshello";
824+
var configLength = _config.Fragment4RayItem?.Length ?? "50-100";
825+
var configDelay = _config.Fragment4RayItem?.Interval ?? "10-20";
826+
827+
var fragmentMask = new Mask4Ray
828+
{
829+
type = "fragment",
830+
settings = new MaskSettings4Ray
831+
{
832+
packets = configPackets,
833+
length = configLength,
834+
delay = configDelay,
835+
}
836+
};
837+
var noiseMask = new Mask4Ray
838+
{
839+
type = "noise",
840+
settings = new MaskSettings4Ray
841+
{
842+
length = "10-20",
843+
delay = "10-16",
844+
}
845+
};
846+
847+
foreach (var outbound in actOutboundWithTlsList)
848+
{
849+
//var packets = configPackets;
850+
//if (outbound.streamSettings.security == Global.StreamSecurityReality
851+
// && packets == "tlshello")
852+
//{
853+
// packets = "1-3";
854+
//}
855+
//else if (outbound.streamSettings.security == Global.StreamSecurity
856+
// && packets != "tlshello")
857+
//{
858+
// packets = "tlshello";
859+
//}
860+
var finalMaskJsonObj = JsonUtils.ParseJson(JsonUtils.Serialize(outbound.streamSettings?.finalmask)) as JsonObject ?? new JsonObject();
861+
// tcp fragment
862+
var tcpFinalmaskList = finalMaskJsonObj["tcp"] as JsonArray ?? [];
863+
if (tcpFinalmaskList.Count == 0)
864+
{
865+
tcpFinalmaskList.Add(JsonUtils.SerializeToNode(fragmentMask));
866+
finalMaskJsonObj["tcp"] = tcpFinalmaskList;
867+
}
868+
// udp noise
869+
var udpFinalmaskList = finalMaskJsonObj["udp"] as JsonArray ?? [];
870+
if (udpFinalmaskList.Count == 0)
871+
{
872+
udpFinalmaskList.Add(JsonUtils.SerializeToNode(noiseMask));
873+
finalMaskJsonObj["udp"] = udpFinalmaskList;
874+
}
875+
// write back
876+
outbound.streamSettings.finalmask = finalMaskJsonObj;
877+
}
878+
}
840879
}

0 commit comments

Comments
 (0)