Skip to content

Commit 8215718

Browse files
committed
Fix ping domain
1 parent e2476d1 commit 8215718

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/sagernet/sing-shadowsocks v0.2.8
3434
github.com/sagernet/sing-shadowsocks2 v0.2.1
3535
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11
36-
github.com/sagernet/sing-tun v0.8.0-beta.8
36+
github.com/sagernet/sing-tun v0.8.0-beta.8.0.20250911105100-0381a06643bc
3737
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1
3838
github.com/sagernet/smux v1.5.34-mod.2
3939
github.com/sagernet/tailscale v1.80.3-sing-box-1.13-mod.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.1 h1:dWV9OXCeFPuYGHb6IRqlSptVnSzOelnq
179179
github.com/sagernet/sing-shadowsocks2 v0.2.1/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
180180
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 h1:tK+75l64tm9WvEFrYRE1t0YxoFdWQqw/h7Uhzj0vJ+w=
181181
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA=
182-
github.com/sagernet/sing-tun v0.8.0-beta.8 h1:K/tPAiuW4Qf1usZvdqjAMVwaxAVE/fb64YqO9SCvcTg=
183-
github.com/sagernet/sing-tun v0.8.0-beta.8/go.mod h1:DCGwHe70ujuzmQ3bvUnf9u1FFRoRvBQ1dDpqZov1ZDA=
182+
github.com/sagernet/sing-tun v0.8.0-beta.8.0.20250911105100-0381a06643bc h1:u6MwQ35W6gIVWRAVjKx3TtaNIhO39Bsz5LkvkWGnnuk=
183+
github.com/sagernet/sing-tun v0.8.0-beta.8.0.20250911105100-0381a06643bc/go.mod h1:DCGwHe70ujuzmQ3bvUnf9u1FFRoRvBQ1dDpqZov1ZDA=
184184
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1 h1:aSwUNYUkVyVvdmBSufR8/nRFonwJeKSIROxHcm5br9o=
185185
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
186186
github.com/sagernet/smux v1.5.34-mod.2 h1:gkmBjIjlJ2zQKpLigOkFur5kBKdV6bNRoFu2WkltRQ4=

route/route.go

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
R "github.com/sagernet/sing-box/route/rule"
1818
"github.com/sagernet/sing-mux"
1919
"github.com/sagernet/sing-tun"
20+
"github.com/sagernet/sing-tun/ping"
2021
"github.com/sagernet/sing-vmess"
2122
"github.com/sagernet/sing/common"
2223
"github.com/sagernet/sing/common/buf"
@@ -271,6 +272,7 @@ func (r *Router) PreMatch(metadata adapter.InboundContext, routeContext tun.Dire
271272
if err != nil {
272273
return nil, err
273274
}
275+
var directRouteOutbound adapter.DirectRouteOutbound
274276
if selectedRule != nil {
275277
switch action := selectedRule.Action().(type) {
276278
case *R.RuleActionReject:
@@ -296,17 +298,69 @@ func (r *Router) PreMatch(metadata adapter.InboundContext, routeContext tun.Dire
296298
if !common.Contains(outbound.Network(), metadata.Network) {
297299
return nil, E.New(metadata.Network, " is not supported by outbound: ", action.Outbound)
298300
}
299-
return outbound.(adapter.DirectRouteOutbound).NewDirectRouteConnection(metadata, routeContext, timeout)
301+
directRouteOutbound = outbound.(adapter.DirectRouteOutbound)
300302
}
301303
}
302-
if selectedRule != nil || metadata.Network != N.NetworkICMP {
303-
return nil, nil
304+
if directRouteOutbound == nil {
305+
if selectedRule != nil || metadata.Network != N.NetworkICMP {
306+
return nil, nil
307+
}
308+
defaultOutbound := r.outbound.Default()
309+
if !common.Contains(defaultOutbound.Network(), metadata.Network) {
310+
return nil, E.New(metadata.Network, " is not supported by default outbound: ", defaultOutbound.Tag())
311+
}
312+
directRouteOutbound = defaultOutbound.(adapter.DirectRouteOutbound)
304313
}
305-
defaultOutbound := r.outbound.Default()
306-
if !common.Contains(defaultOutbound.Network(), metadata.Network) {
307-
return nil, E.New(metadata.Network, " is not supported by default outbound: ", defaultOutbound.Tag())
314+
if metadata.Destination.IsFqdn() {
315+
if len(metadata.DestinationAddresses) == 0 {
316+
var strategy C.DomainStrategy
317+
if metadata.Source.IsIPv4() {
318+
strategy = C.DomainStrategyIPv4Only
319+
} else {
320+
strategy = C.DomainStrategyIPv6Only
321+
}
322+
err = r.actionResolve(r.ctx, &metadata, &R.RuleActionResolve{
323+
Strategy: strategy,
324+
})
325+
if err != nil {
326+
return nil, err
327+
}
328+
}
329+
var newDestination netip.Addr
330+
if metadata.Source.IsIPv4() {
331+
for _, address := range metadata.DestinationAddresses {
332+
if address.Is4() {
333+
newDestination = address
334+
break
335+
}
336+
}
337+
} else {
338+
for _, address := range metadata.DestinationAddresses {
339+
if address.Is6() {
340+
newDestination = address
341+
break
342+
}
343+
}
344+
}
345+
if !newDestination.IsValid() {
346+
if metadata.Source.IsIPv4() {
347+
return nil, E.New("no IPv4 address found for domain: ", metadata.Destination.Fqdn)
348+
} else {
349+
return nil, E.New("no IPv6 address found for domain: ", metadata.Destination.Fqdn)
350+
}
351+
}
352+
metadata.Destination = M.Socksaddr{
353+
Addr: newDestination,
354+
}
355+
routeContext = ping.NewContextDestinationWriter(routeContext, metadata.OriginDestination.Addr)
356+
var routeDestination tun.DirectRouteDestination
357+
routeDestination, err = directRouteOutbound.NewDirectRouteConnection(metadata, routeContext, timeout)
358+
if err != nil {
359+
return nil, err
360+
}
361+
return ping.NewDestinationWriter(routeDestination, newDestination), nil
308362
}
309-
return defaultOutbound.(adapter.DirectRouteOutbound).NewDirectRouteConnection(metadata, routeContext, timeout)
363+
return directRouteOutbound.NewDirectRouteConnection(metadata, routeContext, timeout)
310364
}
311365

312366
func (r *Router) matchRule(

transport/wireguard/device_nat.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type natDeviceWrapper struct {
2020
ctx context.Context
2121
logger logger.ContextLogger
2222
packetOutbound chan *buf.Buffer
23-
rewriter *ping.Rewriter
23+
rewriter *ping.SourceRewriter
2424
buffer [][]byte
2525
}
2626

@@ -30,7 +30,7 @@ func NewNATDevice(ctx context.Context, logger logger.ContextLogger, upstream Dev
3030
ctx: ctx,
3131
logger: logger,
3232
packetOutbound: make(chan *buf.Buffer, 256),
33-
rewriter: ping.NewRewriter(ctx, logger, upstream.Inet4Address(), upstream.Inet6Address()),
33+
rewriter: ping.NewSourceRewriter(ctx, logger, upstream.Inet4Address(), upstream.Inet6Address()),
3434
}
3535
return wrapper
3636
}

0 commit comments

Comments
 (0)