@@ -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