sing-box/dns/dialer.go

51 lines
1.4 KiB
Go
Raw Normal View History

2022-07-07 21:47:21 +08:00
package dns
import (
"context"
"net"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing-box/adapter"
2022-07-07 23:36:32 +08:00
"github.com/sagernet/sing-box/common/dialer"
2022-07-07 21:47:21 +08:00
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
}
2022-07-07 23:36:32 +08:00
return dialer.DialSerial(ctx, d.dialer, network, destination, addresses)
2022-07-07 21:47:21 +08:00
}
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
}
2022-07-07 23:36:32 +08:00
return dialer.ListenSerial(ctx, d.dialer, destination, addresses)
2022-07-07 21:47:21 +08:00
}
func (d *DialerWrapper) Upstream() any {
return d.dialer
}