Add resolver for outbound dialer
This commit is contained in:
46
dns/client_test.go
Normal file
46
dns/client_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package dns_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
|
||||
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/stretchr/testify/require"
|
||||
"golang.org/x/net/dns/dnsmessage"
|
||||
)
|
||||
|
||||
func TestClient(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
client := dns.NewClient(option.DNSClientOptions{})
|
||||
dnsTransport := dns.NewTCPTransport(context.Background(), N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
|
||||
response, err := client.Exchange(ctx, dnsTransport, makeQuery())
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, response.Answers, "no answers")
|
||||
response, err = client.Exchange(ctx, dnsTransport, makeQuery())
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, response.Answers, "no answers")
|
||||
addresses, err := client.Lookup(ctx, dnsTransport, "www.google.com", C.DomainStrategyAsIS)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, addresses, "no answers")
|
||||
cancel()
|
||||
}
|
||||
|
||||
func makeQuery() *dnsmessage.Message {
|
||||
message := &dnsmessage.Message{}
|
||||
message.Header.ID = 1
|
||||
message.Header.RecursionDesired = true
|
||||
message.Questions = append(message.Questions, dnsmessage.Question{
|
||||
Name: dnsmessage.MustNewName("google.com."),
|
||||
Type: dnsmessage.TypeA,
|
||||
Class: dnsmessage.ClassINET,
|
||||
})
|
||||
return message
|
||||
}
|
||||
68
dns/dialer.go
Normal file
68
dns/dialer.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
)
|
||||
|
||||
type DialerWrapper struct {
|
||||
dialer N.Dialer
|
||||
strategy C.DomainStrategy
|
||||
client adapter.DNSClient
|
||||
transport adapter.DNSTransport
|
||||
}
|
||||
|
||||
func NewDialerWrapper(dialer N.Dialer, strategy C.DomainStrategy, client adapter.DNSClient, transport adapter.DNSTransport) N.Dialer {
|
||||
return &DialerWrapper{dialer, strategy, client, transport}
|
||||
}
|
||||
|
||||
func (d *DialerWrapper) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
if destination.IsIP() {
|
||||
return d.dialer.DialContext(ctx, network, destination)
|
||||
}
|
||||
addresses, err := d.client.Lookup(ctx, d.transport, destination.Fqdn, d.strategy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var conn net.Conn
|
||||
var connErrors []error
|
||||
for _, address := range addresses {
|
||||
conn, err = d.dialer.DialContext(ctx, network, M.SocksaddrFromAddrPort(address, destination.Port))
|
||||
if err != nil {
|
||||
connErrors = append(connErrors, err)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
return nil, E.Errors(connErrors...)
|
||||
}
|
||||
|
||||
func (d *DialerWrapper) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
if destination.IsIP() {
|
||||
return d.dialer.ListenPacket(ctx, destination)
|
||||
}
|
||||
addresses, err := d.client.Lookup(ctx, d.transport, destination.Fqdn, d.strategy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var conn net.PacketConn
|
||||
var connErrors []error
|
||||
for _, address := range addresses {
|
||||
conn, err = d.dialer.ListenPacket(ctx, M.SocksaddrFromAddrPort(address, destination.Port))
|
||||
if err != nil {
|
||||
connErrors = append(connErrors, err)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
return nil, E.Errors(connErrors...)
|
||||
}
|
||||
|
||||
func (d *DialerWrapper) Upstream() any {
|
||||
return d.dialer
|
||||
}
|
||||
Reference in New Issue
Block a user