sing-box/dns/transport.go

48 lines
1.2 KiB
Go
Raw Normal View History

2022-07-06 10:48:11 +00:00
package dns
import (
"context"
2022-07-06 15:11:48 +00:00
"net/url"
2022-07-06 10:48:11 +00:00
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
2022-07-06 15:11:48 +00:00
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-07-06 10:48:11 +00:00
)
2022-07-06 15:11:48 +00:00
func NewTransport(ctx context.Context, dialer N.Dialer, logger log.Logger, address string) (adapter.DNSTransport, error) {
if address == "local" {
return NewLocalTransport(), nil
}
serverURL, err := url.Parse(address)
if err != nil {
return nil, err
}
host := serverURL.Hostname()
port := serverURL.Port()
2022-07-08 04:58:43 +00:00
switch serverURL.Scheme {
case "tls":
if port == "" {
port = "853"
}
default:
if port == "" {
port = "53"
}
2022-07-06 15:11:48 +00:00
}
destination := M.ParseSocksaddrHostPortStr(host, port)
switch serverURL.Scheme {
case "", "udp":
return NewUDPTransport(ctx, dialer, logger, destination), nil
case "tcp":
return NewTCPTransport(ctx, dialer, logger, destination), nil
case "tls":
return NewTLSTransport(ctx, dialer, logger, destination), nil
case "https":
return NewHTTPSTransport(dialer, serverURL.String()), nil
default:
return nil, E.New("unknown dns scheme: " + serverURL.Scheme)
}
2022-07-06 10:48:11 +00:00
}