mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-10 11:03:13 +00:00
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
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"
|
|
"github.com/sagernet/sing-box/common/dialer"
|
|
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
|
|
}
|
|
return dialer.DialSerial(ctx, d.dialer, network, destination, addresses)
|
|
}
|
|
|
|
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
|
|
}
|
|
return dialer.ListenSerial(ctx, d.dialer, destination, addresses)
|
|
}
|
|
|
|
func (d *DialerWrapper) Upstream() any {
|
|
return d.dialer
|
|
}
|