@@ -21,10 +21,11 @@ import (
2121)
2222
2323const (
24- DNSRemoteTag = "dns-remote"
25- DNSLocalTag = "dns-local"
26- DNSStaticTag = "dns-static"
27- DNSDirectTag = "dns-direct"
24+ DNSRemoteTag = "dns-remote"
25+ DNSLocalTag = "dns-local"
26+ DNSStaticTag = "dns-static"
27+ DNSDirectTag = "dns-direct"
28+ DNSRemoteNoWarpTag = "dns-remote-no-warp"
2829 // DNSBlockTag = "dns-block"
2930 DNSFakeTag = "dns-fake"
3031 DNSTricksDirectTag = "dns-trick-direct"
@@ -36,16 +37,18 @@ const (
3637 OutboundURLTestTag = "auto"
3738 OutboundDNSTag = "dns-out §hide§"
3839 OutboundDirectFragmentTag = "direct-fragment §hide§"
39- WARPConfigTag = "Hiddify Warp ✅"
40+
41+ WARPConfigTag = "Hiddify Warp ✅"
4042
4143 InboundTUNTag = "tun-in"
4244 InboundMixedTag = "mixed-in"
4345 InboundDNSTag = "dns-in"
4446)
4547
4648var (
47- OutboundMainProxyTag = OutboundSelectTag
48- PredefinedOutboundTags = []string {OutboundDirectTag , OutboundBypassTag , OutboundSelectTag , OutboundURLTestTag , OutboundDNSTag , OutboundDirectFragmentTag }
49+ OutboundMainDetour = OutboundSelectTag
50+ OutboundWARPConfigDetour = OutboundDirectTag
51+ PredefinedOutboundTags = []string {OutboundDirectTag , OutboundBypassTag , OutboundSelectTag , OutboundURLTestTag , OutboundDNSTag , OutboundDirectFragmentTag , WARPConfigTag }
4952)
5053
5154// TODO include selectors
@@ -67,6 +70,7 @@ func BuildConfig(ctx context.Context, hopts *HiddifyOptions, inputOpt *ReadOptio
6770 setLog (& options , hopts )
6871 setInbound (& options , hopts )
6972 staticIPs := make (map [string ][]string )
73+ // staticIPs["api.cloudflareclient.com"] = []string{"104.16.192.82", "2606:4700::6810:1854", getRandomWarpIP()}
7074 // setNTP(&options)
7175 if err := setOutbounds (& options , input , hopts , & staticIPs ); err != nil {
7276 return nil , err
@@ -87,7 +91,9 @@ func setNTP(options *option.Options) {
8791 Enabled : true ,
8892 ServerOptions : option.ServerOptions {ServerPort : 123 , Server : "time.apple.com" },
8993 Interval : badoption .Duration (12 * time .Hour ),
90- DialerOptions : option.DialerOptions {},
94+ DialerOptions : option.DialerOptions {
95+ Detour : OutboundDirectTag ,
96+ },
9197 }
9298}
9399
@@ -108,33 +114,9 @@ func setOutbounds(options *option.Options, input *option.Options, opt *HiddifyOp
108114 // OutboundMainProxyTag = OutboundSelectTag
109115 // inbound==warp over proxies
110116 // outbound==proxies over warp
111- OutboundMainProxyTag = OutboundSelectTag
112- if opt .Warp .EnableWarp && (opt .Warp .Mode == "warp_over_proxy" || opt .Warp .Mode == "proxy_over_warp" ) {
113- // wg := getOrGenerateWarpLocallyIfNeeded(&opt.Warp)
114-
115- // out, err := GenerateWarpSingbox(wg, opt.Warp.CleanIP, opt.Warp.CleanPort, &option.WireGuardHiddify{
116- // FakePackets: opt.Warp.FakePackets,
117- // FakePacketsSize: opt.Warp.FakePacketSize,
118- // FakePacketsDelay: opt.Warp.FakePacketDelay,
119- // FakePacketsMode: opt.Warp.FakePacketMode,
120- // })
121- out , err := GenerateWarpSingboxNew ("p1" , & option.WireGuardHiddify {})
122- if err != nil {
123- return fmt .Errorf ("failed to generate warp config: %v" , err )
124- }
125- out .Tag = WARPConfigTag
126- if opts , ok := out .Options .(* option.WireGuardEndpointOptions ); ok {
127- if opt .Warp .Mode == "warp_over_proxy" {
128- opts .Detour = OutboundSelectTag
129- } else {
130- opts .Detour = OutboundDirectTag
131- }
132- }
117+ OutboundMainDetour = OutboundSelectTag
118+ OutboundWARPConfigDetour = OutboundDirectFragmentTag
133119
134- OutboundMainProxyTag = WARPConfigTag
135- // patchWarp(out, opt, true, nil)
136- endpoints = append (endpoints , * out )
137- }
138120 for _ , out := range input .Outbounds {
139121 if contains (PredefinedOutboundTags , out .Tag ) {
140122 continue
@@ -160,11 +142,47 @@ func setOutbounds(options *option.Options, input *option.Options, opt *HiddifyOp
160142 if ! strings .Contains (out .Tag , "§hide§" ) {
161143 tags = append (tags , out .Tag )
162144 }
145+ OutboundWARPConfigDetour = OutboundSelectTag
163146 out = * patchHiddifyWarpFromConfig (& out , * opt )
164147 outbounds = append (outbounds , out )
165148 }
166149 }
150+
151+ if opt .Warp .EnableWarp {
152+ // wg := getOrGenerateWarpLocallyIfNeeded(&opt.Warp)
153+
154+ // out, err := GenerateWarpSingbox(wg, opt.Warp.CleanIP, opt.Warp.CleanPort, &option.WireGuardHiddify{
155+ // FakePackets: opt.Warp.FakePackets,
156+ // FakePacketsSize: opt.Warp.FakePacketSize,
157+ // FakePacketsDelay: opt.Warp.FakePacketDelay,
158+ // FakePacketsMode: opt.Warp.FakePacketMode,
159+ // })
160+ out , err := GenerateWarpSingboxNew ("p1" , & option.WireGuardHiddify {})
161+ if err != nil {
162+ return fmt .Errorf ("failed to generate warp config: %v" , err )
163+ }
164+ out .Tag = WARPConfigTag
165+ if opts , ok := out .Options .(* option.WireGuardWARPEndpointOptions ); ok {
166+ if opt .Warp .Mode == "warp_over_proxy" {
167+ opts .Detour = OutboundSelectTag
168+ } else {
169+ opts .Detour = OutboundDirectTag
170+ }
171+
172+ }
173+
174+ OutboundMainDetour = WARPConfigTag
175+ // patchWarp(out, opt, true, nil)
176+ out , err = patchEndpoint (out , * opt , staticIPs )
177+ if err != nil {
178+ return err
179+ }
180+ endpoints = append (endpoints , * out )
181+ }
167182 for _ , end := range input .Endpoints {
183+ if contains (PredefinedOutboundTags , end .Tag ) {
184+ continue
185+ }
168186 if opt .Warp .EnableWarp {
169187 if end .Type == C .TypeWARP {
170188 if opts , ok := end .Options .(* option.WireGuardWARPEndpointOptions ); ok {
@@ -180,12 +198,9 @@ func setOutbounds(options *option.Options, input *option.Options, opt *HiddifyOp
180198 }
181199 }
182200 }
183- if opt .Warp .Mode == "warp_over_proxy" && end .Tag == WARPConfigTag {
184- continue
185- }
186201 }
187202
188- out , err := patchEndpoint (end , * opt , staticIPs )
203+ out , err := patchEndpoint (& end , * opt , staticIPs )
189204 if err != nil {
190205 return err
191206 }
@@ -314,34 +329,31 @@ func setLog(options *option.Options, opt *HiddifyOptions) {
314329 }
315330}
316331
317- func setInbound (options * option.Options , opt * HiddifyOptions ) {
318- var inboundDomainStrategy option.DomainStrategy
319- if ! opt .ResolveDestination {
320- inboundDomainStrategy = option .DomainStrategy (dns .DomainStrategyAsIS )
321- } else {
322- inboundDomainStrategy = opt .IPv6Mode
323- }
324- if opt .EnableTun {
332+ func setInbound (options * option.Options , hopt * HiddifyOptions ) {
333+ // var inboundDomainStrategy option.DomainStrategy
334+ // if !opt.ResolveDestination {
335+ // inboundDomainStrategy = option.DomainStrategy(dns.DomainStrategyAsIS)
336+ // } else {
337+ // inboundDomainStrategy = opt.IPv6Mode
338+ // }
339+
340+ if hopt .EnableTun {
325341 opts := option.TunInboundOptions {
326- Stack : opt .TUNStack ,
327- MTU : opt .MTU ,
328- AutoRoute : true ,
329- StrictRoute : opt .StrictRoute ,
330- EndpointIndependentNat : true ,
342+ Stack : hopt .TUNStack ,
343+ MTU : hopt .MTU ,
344+ AutoRoute : true ,
345+ StrictRoute : hopt .StrictRoute ,
346+ // EndpointIndependentNat: true,
331347 // GSO: runtime.GOOS != "windows",
332- InboundOptions : option.InboundOptions {
333- SniffEnabled : true ,
334- SniffOverrideDestination : false ,
335- DomainStrategy : inboundDomainStrategy ,
336- },
348+
337349 }
338350 tunInbound := option.Inbound {
339351 Type : C .TypeTun ,
340352 Tag : InboundTUNTag ,
341353
342354 Options : & opts ,
343355 }
344- switch opt .IPv6Mode {
356+ switch hopt .IPv6Mode {
345357 case option .DomainStrategy (dns .DomainStrategyUseIPv4 ):
346358 opts .Address = []netip.Prefix {
347359 netip .MustParsePrefix ("172.19.0.1/28" ),
@@ -362,7 +374,7 @@ func setInbound(options *option.Options, opt *HiddifyOptions) {
362374 }
363375
364376 var bind string
365- if opt .AllowConnectionFromLAN {
377+ if hopt .AllowConnectionFromLAN {
366378 bind = "0.0.0.0"
367379 } else {
368380 bind = "127.0.0.1"
@@ -377,14 +389,14 @@ func setInbound(options *option.Options, opt *HiddifyOptions) {
377389 Options : & option.HTTPMixedInboundOptions {
378390 ListenOptions : option.ListenOptions {
379391 Listen : & addr ,
380- ListenPort : opt .MixedPort ,
392+ ListenPort : hopt .MixedPort ,
381393 // InboundOptions: option.InboundOptions{
382394 // SniffEnabled: true,
383395 // SniffOverrideDestination: true,
384396 // DomainStrategy: inboundDomainStrategy,
385397 // },
386398 },
387- SetSystemProxy : opt .SetSystemProxy ,
399+ SetSystemProxy : hopt .SetSystemProxy ,
388400 },
389401 },
390402 )
@@ -397,7 +409,7 @@ func setInbound(options *option.Options, opt *HiddifyOptions) {
397409 Options : & option.DirectInboundOptions {
398410 ListenOptions : option.ListenOptions {
399411 Listen : & addr ,
400- ListenPort : opt .LocalDnsPort ,
412+ ListenPort : hopt .LocalDnsPort ,
401413 },
402414 // OverrideAddress: "1.1.1.1",
403415 // OverridePort: 53,
@@ -452,6 +464,7 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
452464 if err != nil {
453465 return err
454466 }
467+
455468 dnsRules = append (dnsRules , forceDirectRules ... )
456469
457470 if len (hopt .ConnectionTestUrls ) > 0 { //To avoid dns bug when using urltest
@@ -475,6 +488,17 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
475488 })
476489 }
477490 }
491+ dnsRules = append (dnsRules , option.DefaultDNSRule {
492+ RawDefaultDNSRule : option.RawDefaultDNSRule {
493+ Domain : []string {"api.cloudflareclient.com" },
494+ },
495+ DNSRuleAction : option.DNSRuleAction {
496+ Action : C .RuleActionTypeRoute ,
497+ RouteOptions : option.DNSRouteActionOptions {
498+ Server : DNSRemoteNoWarpTag ,
499+ },
500+ },
501+ })
478502 if ipinfoDomains := ipinfo .GetAllIPCheckerDomainsDomains (); len (ipinfoDomains ) > 0 { //To avoid dns bug when using urltest
479503 dnsRules = append (dnsRules , option.DefaultDNSRule {
480504 RawDefaultDNSRule : option.RawDefaultDNSRule {
@@ -522,7 +546,7 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
522546 RuleAction : option.RuleAction {
523547 Action : C .RuleActionTypeRoute ,
524548 RouteOptions : option.RouteActionOptions {
525- Outbound : OutboundMainProxyTag ,
549+ Outbound : OutboundMainDetour ,
526550 },
527551 },
528552 },
@@ -835,14 +859,15 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
835859 }
836860 options .Route = & option.RouteOptions {
837861 Rules : routeRules ,
838- Final : OutboundMainProxyTag ,
839- AutoDetectInterface : hopt .EnableTun || hopt .EnableTunService ,
862+ Final : OutboundMainDetour ,
863+ AutoDetectInterface : ( ! C . IsAndroid ) && ( hopt .EnableTun || hopt .EnableTunService ) ,
840864 DefaultDomainResolver : & option.DomainResolveOptions {
841865 Server : DNSDirectTag ,
842866 Strategy : hopt .DirectDnsDomainStrategy ,
843867 },
844- // OverrideAndroidVPN: true,
845- RuleSet : rulesets ,
868+ // OverrideAndroidVPN: hopt.EnableTun && C.IsAndroid,
869+ RuleSet : rulesets ,
870+ FindProcess : false ,
846871 // GeoIP: &option.GeoIPOptions{
847872 // Path: opt.GeoIPPath,
848873 // },
0 commit comments