mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-03 23:28:39 +00:00
merge: Merge upstream v1.14.6
This commit is contained in:
+64
-25
@@ -5,7 +5,6 @@ import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -46,10 +45,13 @@ type Inbound struct {
|
||||
udpFiltering tun.NATFiltering
|
||||
udpNATMax uint32
|
||||
dnsHijackAddress []netip.Addr
|
||||
dnsHijackByPort bool
|
||||
redirectDNSAddress netip.Addr
|
||||
stack string
|
||||
tunIf tun.Tun
|
||||
tunStack tun.Stack
|
||||
platformInterface adapter.PlatformInterface
|
||||
usePlatformInterface bool
|
||||
platformOptions option.TunPlatformOptions
|
||||
enableAutoRedirect bool
|
||||
disableNFTables bool
|
||||
@@ -101,6 +103,7 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
})
|
||||
|
||||
platformInterface := service.FromContext[adapter.PlatformInterface](ctx)
|
||||
usePlatformInterface := platformInterface != nil && platformInterface.UsePlatformInterface()
|
||||
if options.NetNs != "" && !C.IsLinux {
|
||||
return nil, E.New("`netns` is only supported on Linux")
|
||||
}
|
||||
@@ -117,7 +120,7 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
}
|
||||
}
|
||||
var enableGSO bool
|
||||
if C.IsLinux && platformInterface == nil {
|
||||
if C.IsLinux && !usePlatformInterface {
|
||||
enableGSO = (options.Stack == "gvisor" && tunMTU < 49152)
|
||||
}
|
||||
var udpTimeout time.Duration
|
||||
@@ -143,7 +146,7 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
}
|
||||
|
||||
tableIndex := options.IPRoute2TableIndex
|
||||
if tableIndex == 0 {
|
||||
if tableIndex == 0 && options.AutoRoute {
|
||||
tableIndex = tun.DefaultIPRoute2TableIndex
|
||||
}
|
||||
ruleIndex := options.IPRoute2RuleIndex
|
||||
@@ -232,13 +235,14 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
Logger: logger,
|
||||
EXP_MultiPendingPackets: multiPendingPackets,
|
||||
},
|
||||
udpTimeout: udpTimeout,
|
||||
udpMapping: tun.NATMapping(options.UDPMapping),
|
||||
udpFiltering: tun.NATFiltering(options.UDPFiltering),
|
||||
udpNATMax: options.UDPNATMax,
|
||||
stack: options.Stack,
|
||||
platformInterface: platformInterface,
|
||||
platformOptions: common.PtrValueOrDefault(options.Platform),
|
||||
udpTimeout: udpTimeout,
|
||||
udpMapping: tun.NATMapping(options.UDPMapping),
|
||||
udpFiltering: tun.NATFiltering(options.UDPFiltering),
|
||||
udpNATMax: options.UDPNATMax,
|
||||
stack: options.Stack,
|
||||
platformInterface: platformInterface,
|
||||
usePlatformInterface: usePlatformInterface,
|
||||
platformOptions: common.PtrValueOrDefault(options.Platform),
|
||||
}
|
||||
for _, routeAddressSet := range options.RouteAddressSet {
|
||||
ruleSet, loaded := router.RuleSet(routeAddressSet)
|
||||
@@ -264,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 == "" {
|
||||
@@ -326,7 +337,7 @@ func (t *Inbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
||||
t.dnsHijackAddress = append(inet4DNSAddress, inet6DNSAddress...)
|
||||
}
|
||||
case adapter.StartStateStart:
|
||||
if t.platformInterface == nil &&
|
||||
if !t.usePlatformInterface &&
|
||||
((C.IsLinux && !t.tunOptions.GSO) || (C.IsDarwin && !t.tunOptions.EXP_MultiPendingPackets)) {
|
||||
outboundManager := service.FromContext[adapter.OutboundManager](t.ctx)
|
||||
endpointManager := service.FromContext[adapter.EndpointManager](t.ctx)
|
||||
@@ -381,7 +392,7 @@ func (t *Inbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
||||
return E.Cause(err, "initialize auto-redirect")
|
||||
}
|
||||
}
|
||||
if t.platformInterface == nil || C.IsWindows {
|
||||
if !t.usePlatformInterface {
|
||||
for _, routeRuleSet := range t.routeRuleSet {
|
||||
ipSets := routeRuleSet.ExtractIPSet()
|
||||
if len(ipSets) == 0 {
|
||||
@@ -416,7 +427,7 @@ func (t *Inbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
||||
var tunInterface tun.Tun
|
||||
monitor := taskmonitor.New(t.logger, C.StartTimeout)
|
||||
tunOptions := t.tunOptions
|
||||
if t.autoRedirect == nil && !(runtime.GOOS == "android" && t.platformInterface != nil) {
|
||||
if t.autoRedirect == nil {
|
||||
for _, ipSet := range t.routeAddressSet {
|
||||
for _, prefix := range ipSet.Prefixes() {
|
||||
if prefix.Addr().Is4() {
|
||||
@@ -437,7 +448,7 @@ func (t *Inbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
||||
}
|
||||
}
|
||||
monitor.Start("open interface")
|
||||
if t.platformInterface != nil && t.platformInterface.UsePlatformInterface() {
|
||||
if t.usePlatformInterface {
|
||||
tunInterface, err = t.platformInterface.OpenInterface(&tunOptions, t.platformOptions)
|
||||
} else {
|
||||
tunInterface, err = tun.New(tunOptions)
|
||||
@@ -525,6 +536,20 @@ func (t *Inbound) InterfaceUpdated(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Inbound) FlushSystemDNSCache() {
|
||||
linuxTUN, isLinuxTUN := t.tunIf.(tun.LinuxTUN)
|
||||
if !isLinuxTUN {
|
||||
return
|
||||
}
|
||||
serviceManager := service.FromContext[adapter.ServiceManager](t.ctx)
|
||||
if slices.ContainsFunc(serviceManager.Services(), func(it adapter.Service) bool {
|
||||
return it.Type() == C.TypeResolved
|
||||
}) {
|
||||
return
|
||||
}
|
||||
linuxTUN.FlushDNSCache()
|
||||
}
|
||||
|
||||
func (t *Inbound) JudgeFlow(network uint8, source netip.AddrPort, destination netip.AddrPort, firstPacket []byte) tun.FlowVerdict {
|
||||
if slices.Contains(t.dnsHijackAddress, destination.Addr()) {
|
||||
if network == uint8(header.UDPProtocolNumber) {
|
||||
@@ -532,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
|
||||
@@ -555,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 {
|
||||
@@ -574,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)
|
||||
@@ -594,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)
|
||||
@@ -620,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