mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-08-11 12:57:13 +03:00
tailscale: Fix handle peer DNS query
This commit is contained in:
@@ -27,9 +27,10 @@ import (
|
||||
"github.com/sagernet/sing-box/adapter/endpoint"
|
||||
"github.com/sagernet/sing-box/common/dialer"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/dns"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing-box/route/rule"
|
||||
R "github.com/sagernet/sing-box/route/rule"
|
||||
"github.com/sagernet/sing-tun"
|
||||
"github.com/sagernet/sing-tun/ping"
|
||||
"github.com/sagernet/sing/common"
|
||||
@@ -60,6 +61,7 @@ import (
|
||||
"github.com/sagernet/tailscale/wgengine/router"
|
||||
"github.com/sagernet/tailscale/wgengine/wgcfg"
|
||||
|
||||
mDNS "github.com/miekg/dns"
|
||||
"go4.org/netipx"
|
||||
)
|
||||
|
||||
@@ -260,6 +262,8 @@ func NewEndpoint(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
|
||||
func (t *Endpoint) Start(stage adapter.StartStage) error {
|
||||
switch stage {
|
||||
case adapter.StartStateInitialize:
|
||||
t.server.PeerDNSQueryHandler = (*peerDNSQueryHandler)(t)
|
||||
case adapter.StartStateStart:
|
||||
return t.start()
|
||||
case adapter.StartStatePostStart:
|
||||
@@ -678,9 +682,9 @@ func (t *Endpoint) PrepareConnection(network string, source M.Socksaddr, destina
|
||||
}, routeContext, timeout, false)
|
||||
if err != nil {
|
||||
switch {
|
||||
case rule.IsBypassed(err):
|
||||
case R.IsBypassed(err):
|
||||
err = nil
|
||||
case rule.IsRejected(err):
|
||||
case R.IsRejected(err):
|
||||
t.logger.Trace("reject ", network, " connection from ", source.AddrString(), " to ", destination.AddrString())
|
||||
default:
|
||||
if network == N.NetworkICMP {
|
||||
@@ -888,3 +892,30 @@ func (c *dnsConfigurtor) GetBaseConfig() (tsDNS.OSConfig, error) {
|
||||
func (c *dnsConfigurtor) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type peerDNSQueryHandler Endpoint
|
||||
|
||||
func (t *peerDNSQueryHandler) HandlePeerDNSQuery(ctx context.Context, query []byte, sourceAddress netip.AddrPort, allowName func(name string) bool) ([]byte, error) {
|
||||
var message mDNS.Msg
|
||||
err := message.Unpack(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, question := range message.Question {
|
||||
if allowName != nil && !allowName(question.Name) {
|
||||
return dns.FixedResponseStatus(&message, mDNS.RcodeRefused).Pack()
|
||||
}
|
||||
}
|
||||
var metadata adapter.InboundContext
|
||||
metadata.Inbound = t.Tag()
|
||||
metadata.InboundType = t.Type()
|
||||
metadata.Source = M.SocksaddrFromNetIP(sourceAddress)
|
||||
response, err := t.dnsRouter.Exchange(adapter.WithContext(ctx, &metadata), &message, adapter.DNSQueryOptions{})
|
||||
if err != nil {
|
||||
if !R.IsRejected(err) && !E.IsClosedOrCanceled(err) {
|
||||
t.logger.ErrorContext(ctx, E.Cause(err, "process peer DNS query"))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return response.Pack()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user