initial commit
This commit is contained in:
220
resolver/exchanger/exchanger.go
Normal file
220
resolver/exchanger/exchanger.go
Normal file
@ -0,0 +1,220 @@
|
||||
package exchanger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/chain"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
router *chain.Router
|
||||
tlsConfig *tls.Config
|
||||
timeout time.Duration
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
// Option allows a common way to set Exchanger options.
|
||||
type Option func(opts *Options)
|
||||
|
||||
// RouterOption sets the router for Exchanger.
|
||||
func RouterOption(router *chain.Router) Option {
|
||||
return func(opts *Options) {
|
||||
opts.router = router
|
||||
}
|
||||
}
|
||||
|
||||
// TLSConfigOption sets the TLS config for Exchanger.
|
||||
func TLSConfigOption(cfg *tls.Config) Option {
|
||||
return func(opts *Options) {
|
||||
opts.tlsConfig = cfg
|
||||
}
|
||||
}
|
||||
|
||||
// LoggerOption sets the logger for Exchanger.
|
||||
func LoggerOption(logger logger.Logger) Option {
|
||||
return func(opts *Options) {
|
||||
opts.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
// TimeoutOption sets the timeout for Exchanger.
|
||||
func TimeoutOption(timeout time.Duration) Option {
|
||||
return func(opts *Options) {
|
||||
opts.timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// Exchanger is an interface for DNS synchronous query.
|
||||
type Exchanger interface {
|
||||
Exchange(ctx context.Context, msg []byte) ([]byte, error)
|
||||
String() string
|
||||
}
|
||||
|
||||
type exchanger struct {
|
||||
network string
|
||||
addr string
|
||||
rawAddr string
|
||||
router *chain.Router
|
||||
client *http.Client
|
||||
options Options
|
||||
}
|
||||
|
||||
// NewExchanger create an Exchanger.
|
||||
// The addr should be URL-like format,
|
||||
// e.g. udp://1.1.1.1:53, tls://1.1.1.1:853, https://1.0.0.1/dns-query
|
||||
func NewExchanger(addr string, opts ...Option) (Exchanger, error) {
|
||||
var options Options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
if !strings.Contains(addr, "://") {
|
||||
addr = "udp://" + addr
|
||||
}
|
||||
u, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.timeout <= 0 {
|
||||
options.timeout = 5 * time.Second
|
||||
}
|
||||
|
||||
ex := &exchanger{
|
||||
network: u.Scheme,
|
||||
addr: u.Host,
|
||||
rawAddr: addr,
|
||||
router: options.router,
|
||||
options: options,
|
||||
}
|
||||
if _, port, _ := net.SplitHostPort(ex.addr); port == "" {
|
||||
ex.addr = net.JoinHostPort(ex.addr, "53")
|
||||
}
|
||||
if ex.router == nil {
|
||||
ex.router = (&chain.Router{}).WithLogger(options.logger)
|
||||
}
|
||||
|
||||
switch ex.network {
|
||||
case "tcp":
|
||||
case "dot", "tls":
|
||||
if ex.options.tlsConfig == nil {
|
||||
ex.options.tlsConfig = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
}
|
||||
ex.network = "tcp"
|
||||
case "https":
|
||||
ex.addr = addr
|
||||
if ex.options.tlsConfig == nil {
|
||||
ex.options.tlsConfig = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
}
|
||||
ex.client = &http.Client{
|
||||
Timeout: options.timeout,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: options.tlsConfig,
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: options.timeout,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
DialContext: ex.dial,
|
||||
},
|
||||
}
|
||||
default:
|
||||
ex.network = "udp"
|
||||
}
|
||||
|
||||
return ex, nil
|
||||
}
|
||||
|
||||
func (ex *exchanger) Exchange(ctx context.Context, msg []byte) ([]byte, error) {
|
||||
if ex.network == "https" {
|
||||
return ex.dohExchange(ctx, msg)
|
||||
}
|
||||
return ex.exchange(ctx, msg)
|
||||
}
|
||||
|
||||
func (ex *exchanger) dohExchange(ctx context.Context, msg []byte) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", ex.addr, bytes.NewBuffer(msg))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create an HTTPS request: %w", err)
|
||||
}
|
||||
|
||||
// req.Header.Add("Content-Type", "application/dns-udpwireformat")
|
||||
req.Header.Add("Content-Type", "application/dns-message")
|
||||
|
||||
client := ex.client
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to perform an HTTPS request: %w", err)
|
||||
}
|
||||
|
||||
// Check response status code
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("returned status code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Read wireformat response from the body
|
||||
buf, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read the response body: %w", err)
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (ex *exchanger) exchange(ctx context.Context, msg []byte) ([]byte, error) {
|
||||
if ex.options.timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, ex.options.timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
c, err := ex.dial(ctx, ex.network, ex.addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if ex.options.tlsConfig != nil {
|
||||
c = tls.Client(c, ex.options.tlsConfig)
|
||||
}
|
||||
|
||||
conn := &dns.Conn{Conn: c}
|
||||
|
||||
if _, err = conn.Write(msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mr, err := conn.ReadMsg()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mr.Pack()
|
||||
}
|
||||
|
||||
func (ex *exchanger) dial(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return ex.router.Dial(ctx, network, address)
|
||||
}
|
||||
|
||||
func (ex *exchanger) String() string {
|
||||
return ex.rawAddr
|
||||
}
|
178
resolver/impl/resolver.go
Normal file
178
resolver/impl/resolver.go
Normal file
@ -0,0 +1,178 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/chain"
|
||||
resolver_util "github.com/go-gost/core/common/util/resolver"
|
||||
"github.com/go-gost/core/logger"
|
||||
resolverpkg "github.com/go-gost/core/resolver"
|
||||
"github.com/go-gost/core/resolver/exchanger"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
type NameServer struct {
|
||||
Addr string
|
||||
Chain chain.Chainer
|
||||
TTL time.Duration
|
||||
Timeout time.Duration
|
||||
ClientIP net.IP
|
||||
Prefer string
|
||||
Hostname string // for TLS handshake verification
|
||||
exchanger exchanger.Exchanger
|
||||
}
|
||||
|
||||
type resolverOptions struct {
|
||||
domain string
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
type ResolverOption func(opts *resolverOptions)
|
||||
|
||||
func DomainResolverOption(domain string) ResolverOption {
|
||||
return func(opts *resolverOptions) {
|
||||
opts.domain = domain
|
||||
}
|
||||
}
|
||||
|
||||
func LoggerResolverOption(logger logger.Logger) ResolverOption {
|
||||
return func(opts *resolverOptions) {
|
||||
opts.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
type resolver struct {
|
||||
servers []NameServer
|
||||
cache *resolver_util.Cache
|
||||
options resolverOptions
|
||||
}
|
||||
|
||||
func NewResolver(nameservers []NameServer, opts ...ResolverOption) (resolverpkg.Resolver, error) {
|
||||
options := resolverOptions{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
var servers []NameServer
|
||||
for _, server := range nameservers {
|
||||
addr := strings.TrimSpace(server.Addr)
|
||||
if addr == "" {
|
||||
continue
|
||||
}
|
||||
ex, err := exchanger.NewExchanger(
|
||||
addr,
|
||||
exchanger.RouterOption(
|
||||
(&chain.Router{}).
|
||||
WithChain(server.Chain).
|
||||
WithLogger(options.logger),
|
||||
),
|
||||
exchanger.TimeoutOption(server.Timeout),
|
||||
exchanger.LoggerOption(options.logger),
|
||||
)
|
||||
if err != nil {
|
||||
options.logger.Warnf("parse %s: %v", server, err)
|
||||
continue
|
||||
}
|
||||
|
||||
server.exchanger = ex
|
||||
servers = append(servers, server)
|
||||
}
|
||||
cache := resolver_util.NewCache().
|
||||
WithLogger(options.logger)
|
||||
|
||||
return &resolver{
|
||||
servers: servers,
|
||||
cache: cache,
|
||||
options: options,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *resolver) Resolve(ctx context.Context, network, host string) (ips []net.IP, err error) {
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
return []net.IP{ip}, nil
|
||||
}
|
||||
|
||||
if r.options.domain != "" &&
|
||||
!strings.Contains(host, ".") {
|
||||
host = host + "." + r.options.domain
|
||||
}
|
||||
|
||||
for _, server := range r.servers {
|
||||
ips, err = r.resolve(ctx, &server, host)
|
||||
if err != nil {
|
||||
r.options.logger.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
r.options.logger.Debugf("resolve %s via %s: %v", host, server.exchanger.String(), ips)
|
||||
|
||||
if len(ips) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *resolver) resolve(ctx context.Context, server *NameServer, host string) (ips []net.IP, err error) {
|
||||
if server == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if server.Prefer == "ipv6" { // prefer ipv6
|
||||
mq := dns.Msg{}
|
||||
mq.SetQuestion(dns.Fqdn(host), dns.TypeAAAA)
|
||||
ips, err = r.resolveIPs(ctx, server, &mq)
|
||||
if err != nil || len(ips) > 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to ipv4
|
||||
mq := dns.Msg{}
|
||||
mq.SetQuestion(dns.Fqdn(host), dns.TypeA)
|
||||
return r.resolveIPs(ctx, server, &mq)
|
||||
}
|
||||
|
||||
func (r *resolver) resolveIPs(ctx context.Context, server *NameServer, mq *dns.Msg) (ips []net.IP, err error) {
|
||||
key := resolver_util.NewCacheKey(&mq.Question[0])
|
||||
mr := r.cache.Load(key)
|
||||
if mr == nil {
|
||||
resolver_util.AddSubnetOpt(mq, server.ClientIP)
|
||||
mr, err = r.exchange(ctx, server.exchanger, mq)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r.cache.Store(key, mr, server.TTL)
|
||||
}
|
||||
|
||||
for _, ans := range mr.Answer {
|
||||
if ar, _ := ans.(*dns.AAAA); ar != nil {
|
||||
ips = append(ips, ar.AAAA)
|
||||
}
|
||||
if ar, _ := ans.(*dns.A); ar != nil {
|
||||
ips = append(ips, ar.A)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *resolver) exchange(ctx context.Context, ex exchanger.Exchanger, mq *dns.Msg) (mr *dns.Msg, err error) {
|
||||
query, err := mq.Pack()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
reply, err := ex.Exchange(ctx, query)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mr = &dns.Msg{}
|
||||
err = mr.Unpack(reply)
|
||||
|
||||
return
|
||||
}
|
17
resolver/resolver.go
Normal file
17
resolver/resolver.go
Normal file
@ -0,0 +1,17 @@
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalid = errors.New("resolver is invalid")
|
||||
)
|
||||
|
||||
type Resolver interface {
|
||||
// Resolve returns a slice of the host's IPv4 and IPv6 addresses.
|
||||
// The network should be 'ip', 'ip4' or 'ip6', default network is 'ip'.
|
||||
Resolve(ctx context.Context, network, host string) ([]net.IP, error)
|
||||
}
|
Reference in New Issue
Block a user