fix 1.12 dns

This commit is contained in:
armv9 2025-09-02 22:11:42 +09:00
parent e4d38ba44d
commit 893ba090f6
9 changed files with 51 additions and 43 deletions

View File

@ -21,6 +21,7 @@ import io.nekohasekai.sagernet.plugin.PluginManager
import kotlinx.coroutines.*
import libcore.BoxInstance
import libcore.Libcore
import moe.matsuri.nb4a.net.LocalResolverImpl
import java.io.File
abstract class BoxInstance(
@ -48,7 +49,7 @@ abstract class BoxInstance(
}
protected open suspend fun loadConfig() {
box = Libcore.newSingBoxInstance(config.config)
box = Libcore.newSingBoxInstance(config.config, LocalResolverImpl)
}
open suspend fun init() {

View File

@ -7,8 +7,6 @@ import io.nekohasekai.sagernet.database.ProxyEntity
import io.nekohasekai.sagernet.ktx.Logs
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
import kotlinx.coroutines.runBlocking
import libcore.Libcore
import moe.matsuri.nb4a.net.LocalResolverImpl
import moe.matsuri.nb4a.utils.JavaUtil
class ProxyInstance(profile: ProxyEntity, var service: BaseService.Interface? = null) :
@ -45,7 +43,6 @@ class ProxyInstance(profile: ProxyEntity, var service: BaseService.Interface? =
}
override suspend fun loadConfig() {
Libcore.registerLocalDNSTransport(LocalResolverImpl)
super.loadConfig()
}
@ -59,7 +56,6 @@ class ProxyInstance(profile: ProxyEntity, var service: BaseService.Interface? =
}
override fun close() {
Libcore.registerLocalDNSTransport(null)
super.close()
runBlocking {
looper?.stop()

View File

@ -10,6 +10,7 @@ import io.nekohasekai.sagernet.ktx.tryResume
import io.nekohasekai.sagernet.ktx.tryResumeWithException
import kotlinx.coroutines.delay
import libcore.Libcore
import moe.matsuri.nb4a.net.LocalResolverImpl
import kotlin.coroutines.suspendCoroutine
class TestInstance(profile: ProxyEntity, val link: String, private val timeout: Int) :
@ -46,7 +47,7 @@ class TestInstance(profile: ProxyEntity, val link: String, private val timeout:
override suspend fun loadConfig() {
// don't call destroyAllJsi here
if (BuildConfig.DEBUG) Logs.d(config.config)
box = Libcore.newSingBoxInstance(config.config)
box = Libcore.newSingBoxInstance(config.config, LocalResolverImpl)
}
}

View File

@ -79,12 +79,15 @@ type BoxInstance struct {
pauseManager pause.Manager
}
func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
func NewSingBoxInstance(config string, localTransport LocalDNSTransport) (b *BoxInstance, err error) {
defer device.DeferPanicToError("NewSingBoxInstance", func(err_ error) { err = err_ })
// create box context
ctx, cancel := context.WithCancel(context.Background())
ctx = box.Context(ctx, nekoboxAndroidInboundRegistry(), nekoboxAndroidOutboundRegistry(), nekoboxAndroidEndpointRegistry(), nekoboxAndroidDNSTransportRegistry(), nekoboxAndroidServiceRegistry())
ctx = box.Context(ctx,
nekoboxAndroidInboundRegistry(), nekoboxAndroidOutboundRegistry(), nekoboxAndroidEndpointRegistry(),
nekoboxAndroidDNSTransportRegistry(localTransport), nekoboxAndroidServiceRegistry(),
)
ctx = service.ContextWithDefaultRegistry(ctx)
service.MustRegister[platform.Interface](ctx, boxPlatformInterfaceInstance)

View File

@ -1,6 +1,9 @@
package libcore
import (
"context"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/adapter/endpoint"
"github.com/sagernet/sing-box/adapter/inbound"
"github.com/sagernet/sing-box/adapter/outbound"
@ -11,6 +14,8 @@ import (
"github.com/sagernet/sing-box/dns/transport/hosts"
"github.com/sagernet/sing-box/dns/transport/local"
"github.com/sagernet/sing-box/dns/transport/quic"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/protocol/anytls"
"github.com/sagernet/sing-box/protocol/block"
"github.com/sagernet/sing-box/protocol/direct"
@ -35,7 +40,6 @@ import (
_ "github.com/sagernet/sing-box/experimental/clashapi"
_ "github.com/sagernet/sing-box/transport/v2rayquic"
_ "github.com/sagernet/sing-dns/quic"
)
func nekoboxAndroidInboundRegistry() *inbound.Registry {
@ -92,7 +96,7 @@ func nekoboxAndroidEndpointRegistry() *endpoint.Registry {
return registry
}
func nekoboxAndroidDNSTransportRegistry() *dns.TransportRegistry {
func nekoboxAndroidDNSTransportRegistry(localTransport LocalDNSTransport) *dns.TransportRegistry {
registry := dns.NewTransportRegistry()
transport.RegisterTCP(registry)
@ -100,12 +104,23 @@ func nekoboxAndroidDNSTransportRegistry() *dns.TransportRegistry {
transport.RegisterTLS(registry)
transport.RegisterHTTPS(registry)
hosts.RegisterTransport(registry)
local.RegisterTransport(registry)
// local.RegisterTransport(registry)
fakeip.RegisterTransport(registry)
quic.RegisterTransport(registry)
quic.RegisterHTTP3Transport(registry)
if localTransport == nil {
local.RegisterTransport(registry)
} else {
dns.RegisterTransport(registry, "local", func(ctx context.Context, logger log.ContextLogger, tag string, options option.LocalDNSServerOptions) (adapter.DNSTransport, error) {
return &platformLocalDNSTransport{
iif: localTransport,
tag: tag,
}, nil
})
}
return registry
}

View File

@ -8,7 +8,9 @@ import (
"strings"
"syscall"
dns "github.com/sagernet/sing-dns"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/dns"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
@ -23,29 +25,12 @@ type LocalDNSTransport interface {
Exchange(ctx *ExchangeContext, message []byte) error
}
func RegisterLocalDNSTransport(transport LocalDNSTransport) {
if transport == nil {
dns.RegisterTransport([]string{"local"}, dns.CreateTransport)
} else {
dns.RegisterTransport([]string{"local"}, func(options dns.TransportOptions) (dns.Transport, error) {
return &platformLocalDNSTransport{
iif: transport,
}, nil
})
}
}
var _ dns.Transport = (*platformLocalDNSTransport)(nil)
type platformLocalDNSTransport struct {
iif LocalDNSTransport
tag string
}
func (p *platformLocalDNSTransport) Name() string {
return "local"
}
func (p *platformLocalDNSTransport) Start() error {
func (p *platformLocalDNSTransport) Start(adapter.StartStage) error {
return nil
}
@ -82,12 +67,12 @@ func (p *platformLocalDNSTransport) Exchange(ctx context.Context, message *mDNS.
})
}
func (p *platformLocalDNSTransport) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
func (p *platformLocalDNSTransport) Lookup(ctx context.Context, domain string, strategy constant.DomainStrategy) ([]netip.Addr, error) {
var network string
switch strategy {
case dns.DomainStrategyUseIPv4:
case constant.DomainStrategyIPv4Only:
network = "ip4"
case dns.DomainStrategyPreferIPv6:
case constant.DomainStrategyPreferIPv6:
network = "ip6"
default:
network = "ip"
@ -105,11 +90,11 @@ func (p *platformLocalDNSTransport) Lookup(ctx context.Context, domain string, s
return response.error
}
switch strategy {
case dns.DomainStrategyUseIPv4:
case constant.DomainStrategyIPv4Only:
responseAddr = common.Filter(response.addresses, func(it netip.Addr) bool {
return it.Is4()
})
case dns.DomainStrategyPreferIPv6:
case constant.DomainStrategyPreferIPv6:
responseAddr = common.Filter(response.addresses, func(it netip.Addr) bool {
return it.Is6()
})
@ -123,6 +108,18 @@ func (p *platformLocalDNSTransport) Lookup(ctx context.Context, domain string, s
})
}
func (p *platformLocalDNSTransport) Tag() string {
return p.tag
}
func (p *platformLocalDNSTransport) Type() string {
return "local"
}
func (p *platformLocalDNSTransport) Dependencies() []string {
return nil
}
type Func interface {
Invoke() error
}
@ -157,7 +154,7 @@ func (c *ExchangeContext) RawSuccess(result []byte) {
}
func (c *ExchangeContext) ErrorCode(code int32) {
c.error = dns.RCodeError(code)
c.error = dns.RcodeError(code)
}
func (c *ExchangeContext) ErrnoCode(code int32) {

View File

@ -10,7 +10,6 @@ require (
github.com/oschwald/maxminddb-golang v1.13.1
github.com/sagernet/sing v0.7.6-0.20250825114712-2aeec120ce28
github.com/sagernet/sing-box v1.0.0 // replaced
github.com/sagernet/sing-dns v0.4.1
github.com/sagernet/sing-tun v0.7.0-beta.1
github.com/ulikunitz/xz v0.5.11
golang.org/x/mobile v0.0.0-20231108233038-35478a0c49da
@ -90,5 +89,3 @@ replace github.com/sagernet/sing-box => ../../sing-box
// replace github.com/sagernet/sing-quic => ../../sing-quic
// replace github.com/sagernet/sing => ../../sing
// replace github.com/sagernet/sing-dns => ../../sing-dns

View File

@ -91,8 +91,6 @@ github.com/sagernet/quic-go v0.52.0-beta.1/go.mod h1:OV+V5kEBb8kJS7k29MzDu6oj9Gy
github.com/sagernet/sing v0.6.9/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing v0.7.6-0.20250825114712-2aeec120ce28 h1:C8Lnqd0Q+C15kwaMiDsfq5S45rhhaQMBG91TT+6oFVo=
github.com/sagernet/sing v0.7.6-0.20250825114712-2aeec120ce28/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing-dns v0.4.1 h1:nozS7iqpxZ7aV73oHbkD/8haOvf3XXDCgT//8NdYirk=
github.com/sagernet/sing-dns v0.4.1/go.mod h1:dweQs54ng2YGzoJfz+F9dGuDNdP5pJ3PLeggnK5VWc8=
github.com/sagernet/sing-mux v0.3.3 h1:YFgt9plMWzH994BMZLmyKL37PdIVaIilwP0Jg+EcLfw=
github.com/sagernet/sing-mux v0.3.3/go.mod h1:pht8iFY4c9Xltj7rhVd208npkNaeCxzyXCgulDPLUDA=
github.com/sagernet/sing-quic v0.5.0 h1:jNLIyVk24lFPvu8A4x+ZNEnZdI+Tg1rp7eCJ6v0Csak=

View File

@ -1,4 +1,4 @@
PACKAGE_NAME=moe.nb4a
VERSION_NAME=1.3.9
PRE_VERSION_NAME=pre-1.4.0-20250902-1
PRE_VERSION_NAME=pre-1.4.0-20250902-2
VERSION_CODE=43