2023-06-18 02:00:32 +00:00
|
|
|
package libbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-dns"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
"github.com/sagernet/sing/common/task"
|
|
|
|
|
|
|
|
mDNS "github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalDNSTransport interface {
|
|
|
|
Raw() bool
|
|
|
|
Lookup(ctx *ExchangeContext, network string, domain string) error
|
|
|
|
Exchange(ctx *ExchangeContext, message []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterLocalDNSTransport(transport LocalDNSTransport) {
|
|
|
|
if transport == nil {
|
2024-02-09 10:37:25 +00:00
|
|
|
dns.RegisterTransport([]string{"local"}, func(options dns.TransportOptions) (dns.Transport, error) {
|
|
|
|
return dns.NewLocalTransport(options), nil
|
|
|
|
})
|
2023-06-18 02:00:32 +00:00
|
|
|
} else {
|
2024-02-09 10:37:25 +00:00
|
|
|
dns.RegisterTransport([]string{"local"}, func(options dns.TransportOptions) (dns.Transport, error) {
|
2023-06-18 02:00:32 +00:00
|
|
|
return &platformLocalDNSTransport{
|
|
|
|
iif: transport,
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ dns.Transport = (*platformLocalDNSTransport)(nil)
|
|
|
|
|
|
|
|
type platformLocalDNSTransport struct {
|
|
|
|
iif LocalDNSTransport
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *platformLocalDNSTransport) Name() string {
|
|
|
|
return "local"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *platformLocalDNSTransport) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:52:38 +00:00
|
|
|
func (p *platformLocalDNSTransport) Reset() {
|
|
|
|
}
|
|
|
|
|
2023-06-18 02:00:32 +00:00
|
|
|
func (p *platformLocalDNSTransport) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *platformLocalDNSTransport) Raw() bool {
|
|
|
|
return p.iif.Raw()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *platformLocalDNSTransport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
|
|
|
messageBytes, err := message.Pack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response := &ExchangeContext{
|
|
|
|
context: ctx,
|
|
|
|
}
|
|
|
|
var responseMessage *mDNS.Msg
|
|
|
|
return responseMessage, task.Run(ctx, func() error {
|
|
|
|
err = p.iif.Exchange(response, messageBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if response.error != nil {
|
|
|
|
return response.error
|
|
|
|
}
|
|
|
|
responseMessage = &response.message
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *platformLocalDNSTransport) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
|
|
|
|
var network string
|
|
|
|
switch strategy {
|
|
|
|
case dns.DomainStrategyUseIPv4:
|
|
|
|
network = "ip4"
|
|
|
|
case dns.DomainStrategyPreferIPv6:
|
|
|
|
network = "ip6"
|
|
|
|
default:
|
|
|
|
network = "ip"
|
|
|
|
}
|
|
|
|
response := &ExchangeContext{
|
|
|
|
context: ctx,
|
|
|
|
}
|
|
|
|
var responseAddr []netip.Addr
|
|
|
|
return responseAddr, task.Run(ctx, func() error {
|
|
|
|
err := p.iif.Lookup(response, network, domain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if response.error != nil {
|
|
|
|
return response.error
|
|
|
|
}
|
|
|
|
switch strategy {
|
|
|
|
case dns.DomainStrategyUseIPv4:
|
|
|
|
responseAddr = common.Filter(response.addresses, func(it netip.Addr) bool {
|
|
|
|
return it.Is4()
|
|
|
|
})
|
|
|
|
case dns.DomainStrategyPreferIPv6:
|
|
|
|
responseAddr = common.Filter(response.addresses, func(it netip.Addr) bool {
|
|
|
|
return it.Is6()
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
responseAddr = response.addresses
|
|
|
|
}
|
|
|
|
/*if len(responseAddr) == 0 {
|
|
|
|
response.error = dns.RCodeSuccess
|
|
|
|
}*/
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-19 06:11:26 +00:00
|
|
|
type Func interface {
|
|
|
|
Invoke() error
|
|
|
|
}
|
|
|
|
|
2023-06-18 02:00:32 +00:00
|
|
|
type ExchangeContext struct {
|
|
|
|
context context.Context
|
|
|
|
message mDNS.Msg
|
|
|
|
addresses []netip.Addr
|
|
|
|
error error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExchangeContext) OnCancel(callback Func) {
|
|
|
|
go func() {
|
|
|
|
<-c.context.Done()
|
|
|
|
callback.Invoke()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExchangeContext) Success(result string) {
|
|
|
|
c.addresses = common.Map(common.Filter(strings.Split(result, "\n"), func(it string) bool {
|
|
|
|
return !common.IsEmpty(it)
|
|
|
|
}), func(it string) netip.Addr {
|
|
|
|
return M.ParseSocksaddrHostPort(it, 0).Unwrap().Addr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExchangeContext) RawSuccess(result []byte) {
|
|
|
|
err := c.message.Unpack(result)
|
|
|
|
if err != nil {
|
|
|
|
c.error = E.Cause(err, "parse response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExchangeContext) ErrorCode(code int32) {
|
|
|
|
c.error = dns.RCodeError(code)
|
|
|
|
}
|
|
|
|
|
2023-06-19 06:11:26 +00:00
|
|
|
func (c *ExchangeContext) ErrnoCode(code int32) {
|
|
|
|
c.error = syscall.Errno(code)
|
2023-06-18 02:00:32 +00:00
|
|
|
}
|