mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Align tun auto-redirect DNS hijack and default marks with sing-tun
This commit is contained in:
@@ -88,9 +88,9 @@ icon: material/new-box
|
||||
"iproute2_table_index": 2022,
|
||||
"iproute2_rule_index": 9000,
|
||||
"auto_redirect": true,
|
||||
"auto_redirect_input_mark": "0x2023",
|
||||
"auto_redirect_output_mark": "0x2024",
|
||||
"auto_redirect_reset_mark": "0x2025",
|
||||
"auto_redirect_input_mark": "0x04000000",
|
||||
"auto_redirect_output_mark": "0x08000000",
|
||||
"auto_redirect_reset_mark": "0x10000000",
|
||||
"auto_redirect_nfqueue": 100,
|
||||
"auto_redirect_iproute2_fallback_rule_index": 32768,
|
||||
"exclude_mptcp": false,
|
||||
@@ -363,7 +363,7 @@ Conflict with `route.default_mark` and `[dialOptions].routing_mark`.
|
||||
|
||||
Connection input mark used by `auto_redirect`.
|
||||
|
||||
`0x2023` is used by default.
|
||||
`0x04000000` is used by default.
|
||||
|
||||
#### auto_redirect_output_mark
|
||||
|
||||
@@ -371,7 +371,7 @@ Connection input mark used by `auto_redirect`.
|
||||
|
||||
Connection output mark used by `auto_redirect`.
|
||||
|
||||
`0x2024` is used by default.
|
||||
`0x08000000` is used by default.
|
||||
|
||||
#### auto_redirect_reset_mark
|
||||
|
||||
@@ -379,7 +379,7 @@ Connection output mark used by `auto_redirect`.
|
||||
|
||||
Connection reset mark used by `auto_redirect` pre-matching.
|
||||
|
||||
`0x2025` is used by default.
|
||||
`0x10000000` is used by default.
|
||||
|
||||
#### auto_redirect_nfqueue
|
||||
|
||||
|
||||
@@ -88,9 +88,9 @@ icon: material/new-box
|
||||
"iproute2_table_index": 2022,
|
||||
"iproute2_rule_index": 9000,
|
||||
"auto_redirect": true,
|
||||
"auto_redirect_input_mark": "0x2023",
|
||||
"auto_redirect_output_mark": "0x2024",
|
||||
"auto_redirect_reset_mark": "0x2025",
|
||||
"auto_redirect_input_mark": "0x04000000",
|
||||
"auto_redirect_output_mark": "0x08000000",
|
||||
"auto_redirect_reset_mark": "0x10000000",
|
||||
"auto_redirect_nfqueue": 100,
|
||||
"auto_redirect_iproute2_fallback_rule_index": 32768,
|
||||
"exclude_mptcp": false,
|
||||
@@ -356,7 +356,7 @@ TUN 接口上 DNS 的处理方式。
|
||||
|
||||
`auto_redirect` 使用的连接输入标记。
|
||||
|
||||
默认使用 `0x2023`。
|
||||
默认使用 `0x04000000`。
|
||||
|
||||
#### auto_redirect_output_mark
|
||||
|
||||
@@ -364,7 +364,7 @@ TUN 接口上 DNS 的处理方式。
|
||||
|
||||
`auto_redirect` 使用的连接输出标记。
|
||||
|
||||
默认使用 `0x2024`。
|
||||
默认使用 `0x08000000`。
|
||||
|
||||
#### auto_redirect_reset_mark
|
||||
|
||||
@@ -372,7 +372,7 @@ TUN 接口上 DNS 的处理方式。
|
||||
|
||||
`auto_redirect` 预匹配使用的连接重置标记。
|
||||
|
||||
默认使用 `0x2025`。
|
||||
默认使用 `0x10000000`。
|
||||
|
||||
#### auto_redirect_nfqueue
|
||||
|
||||
|
||||
+34
-11
@@ -45,6 +45,8 @@ type Inbound struct {
|
||||
udpFiltering tun.NATFiltering
|
||||
udpNATMax uint32
|
||||
dnsHijackAddress []netip.Addr
|
||||
dnsHijackByPort bool
|
||||
redirectDNSAddress netip.Addr
|
||||
stack string
|
||||
tunIf tun.Tun
|
||||
tunStack tun.Stack
|
||||
@@ -266,6 +268,13 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
disableNFTables, parseErr := strconv.ParseBool(os.Getenv("DISABLE_NFTABLES"))
|
||||
inbound.enableAutoRedirect = true
|
||||
inbound.disableNFTables = parseErr == nil && disableNFTables
|
||||
inbound.dnsHijackByPort = inbound.tunOptions.DNSModeOrDefault() == tun.DNSModeHijack && len(options.DNSAddress) == 0
|
||||
if inbound.tunOptions.DNSModeOrDefault() == tun.DNSModeHijack && len(options.DNSAddress) > 0 {
|
||||
inet6DNSAddress, _ := inbound.tunOptions.Inet6DNSAddress()
|
||||
if len(inet6DNSAddress) > 0 {
|
||||
inbound.redirectDNSAddress = inet6DNSAddress[0]
|
||||
}
|
||||
}
|
||||
if !C.IsAndroid {
|
||||
inbound.tunOptions.AutoRedirectMarkMode = true
|
||||
if options.NetNs == "" {
|
||||
@@ -548,9 +557,20 @@ func (t *Inbound) JudgeFlow(network uint8, source netip.AddrPort, destination ne
|
||||
}
|
||||
return tun.FlowVerdict{Action: tun.ActionAccept}
|
||||
}
|
||||
if t.dnsHijackByPort && destination.Port() == 53 &&
|
||||
(network == uint8(header.TCPProtocolNumber) || network == uint8(header.UDPProtocolNumber)) {
|
||||
if network == uint8(header.UDPProtocolNumber) {
|
||||
return tun.FlowVerdict{Action: tun.ActionHijackDNS}
|
||||
}
|
||||
return tun.FlowVerdict{Action: tun.ActionAccept}
|
||||
}
|
||||
return adapter.JudgeFlow(t.router, t.tag, C.TypeTun, network, source, destination, firstPacket)
|
||||
}
|
||||
|
||||
func (t *Inbound) isDNSHijackDestination(destination M.Socksaddr) bool {
|
||||
return slices.Contains(t.dnsHijackAddress, destination.Addr) || t.dnsHijackByPort && destination.Port == 53
|
||||
}
|
||||
|
||||
func (t *Inbound) NewDNSPacket(payload []byte, source M.Socksaddr, destination M.Socksaddr, writer N.PacketWriter) {
|
||||
ctx := log.ContextWithNewID(t.ctx)
|
||||
var metadata adapter.InboundContext
|
||||
@@ -571,7 +591,7 @@ func (t *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, source M.S
|
||||
metadata.InboundType = C.TypeTun
|
||||
metadata.Source = source
|
||||
metadata.Destination = destination
|
||||
if slices.Contains(t.dnsHijackAddress, destination.Addr) {
|
||||
if t.isDNSHijackDestination(destination) {
|
||||
metadata.Protocol = C.ProtocolDNS
|
||||
}
|
||||
if metadata.Protocol == C.ProtocolDNS {
|
||||
@@ -590,10 +610,8 @@ func (t *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
||||
metadata.InboundType = C.TypeTun
|
||||
metadata.Source = source
|
||||
metadata.Destination = destination
|
||||
for _, dnsHijackAddress := range t.dnsHijackAddress {
|
||||
if destination.Addr == dnsHijackAddress {
|
||||
metadata.Protocol = C.ProtocolDNS
|
||||
}
|
||||
if t.isDNSHijackDestination(destination) {
|
||||
metadata.Protocol = C.ProtocolDNS
|
||||
}
|
||||
if metadata.Protocol == C.ProtocolDNS {
|
||||
t.logger.InfoContext(ctx, "inbound DNS packet connection from ", metadata.Source)
|
||||
@@ -610,17 +628,22 @@ func (t *autoRedirectHandler) JudgeFlow(network uint8, source netip.AddrPort, de
|
||||
return (*Inbound)(t).JudgeFlow(network, source, destination, firstPacket)
|
||||
}
|
||||
|
||||
func (t *autoRedirectHandler) redirectDNSDestination(source M.Socksaddr, destination M.Socksaddr) M.Socksaddr {
|
||||
if t.redirectDNSAddress.IsValid() && destination.Port == 53 && source.Addr.Is6() && source.Addr.IsLinkLocalUnicast() {
|
||||
return M.SocksaddrFrom(t.redirectDNSAddress, 53)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
func (t *autoRedirectHandler) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
||||
ctx = log.ContextWithNewID(ctx)
|
||||
var metadata adapter.InboundContext
|
||||
metadata.Inbound = t.tag
|
||||
metadata.InboundType = C.TypeTun
|
||||
metadata.Source = source
|
||||
metadata.Destination = destination
|
||||
for _, dnsHijackAddress := range t.dnsHijackAddress {
|
||||
if destination.Addr == dnsHijackAddress {
|
||||
metadata.Protocol = C.ProtocolDNS
|
||||
}
|
||||
metadata.Destination = t.redirectDNSDestination(source, destination)
|
||||
if (*Inbound)(t).isDNSHijackDestination(metadata.Destination) {
|
||||
metadata.Protocol = C.ProtocolDNS
|
||||
}
|
||||
if metadata.Protocol == C.ProtocolDNS {
|
||||
t.logger.InfoContext(ctx, "inbound redirect DNS connection from ", metadata.Source)
|
||||
@@ -636,5 +659,5 @@ func (t *autoRedirectHandler) NewPacketConnectionEx(ctx context.Context, conn N.
|
||||
}
|
||||
|
||||
func (t *autoRedirectHandler) NewDNSPacket(payload []byte, source M.Socksaddr, destination M.Socksaddr, writer N.PacketWriter) {
|
||||
(*Inbound)(t).NewDNSPacket(payload, source, destination, writer)
|
||||
(*Inbound)(t).NewDNSPacket(payload, source, t.redirectDNSDestination(source, destination), writer)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user