2024-07-21 07:57:47 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-18 04:34:09 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2024-07-21 07:57:47 +00:00
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
"github.com/miekg/dns"
|
2024-08-18 07:11:04 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/dns/resolver"
|
2024-08-18 06:55:47 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/util"
|
2024-08-22 08:49:05 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/util/log"
|
2024-08-18 06:55:47 +00:00
|
|
|
)
|
2024-07-21 07:57:47 +00:00
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
const scopeDNS = "DNS"
|
|
|
|
|
2024-08-19 01:50:06 +00:00
|
|
|
type Resolver interface {
|
|
|
|
Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error)
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
type Dns struct {
|
|
|
|
host string
|
|
|
|
port string
|
2024-08-19 01:50:06 +00:00
|
|
|
systemClient Resolver
|
|
|
|
generalClient Resolver
|
|
|
|
dohClient Resolver
|
2024-09-08 13:45:24 +00:00
|
|
|
qTypes []uint16
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 02:00:26 +00:00
|
|
|
func NewDns(config *util.Config) *Dns {
|
2024-08-23 12:17:35 +00:00
|
|
|
addr := config.DnsAddr
|
|
|
|
port := strconv.Itoa(config.DnsPort)
|
2024-09-08 13:45:24 +00:00
|
|
|
var qTypes []uint16
|
|
|
|
if config.DnsIPv4Only {
|
|
|
|
qTypes = []uint16{dns.TypeA}
|
|
|
|
} else {
|
|
|
|
qTypes = []uint16{dns.TypeAAAA, dns.TypeA}
|
|
|
|
}
|
2024-08-18 06:55:47 +00:00
|
|
|
return &Dns{
|
2024-08-23 12:17:35 +00:00
|
|
|
host: config.DnsAddr,
|
2024-08-18 06:55:47 +00:00
|
|
|
port: port,
|
2024-08-19 02:00:26 +00:00
|
|
|
systemClient: resolver.NewSystemResolver(),
|
|
|
|
generalClient: resolver.NewGeneralResolver(net.JoinHostPort(addr, port)),
|
|
|
|
dohClient: resolver.NewDOHResolver(addr),
|
2024-09-08 13:45:24 +00:00
|
|
|
qTypes: qTypes,
|
2024-08-18 04:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
func (d *Dns) ResolveHost(ctx context.Context, host string, enableDoh bool, useSystemDns bool) (string, error) {
|
|
|
|
ctx = util.GetCtxWithScope(ctx, scopeDNS)
|
|
|
|
logger := log.GetCtxLogger(ctx)
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
if ip, err := parseIpAddr(host); err == nil {
|
|
|
|
return ip.String(), nil
|
2024-08-06 08:48:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
clt := d.clientFactory(enableDoh, useSystemDns)
|
2024-08-22 08:49:05 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
2024-08-18 04:34:09 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("resolving %s using %s", host, clt)
|
|
|
|
|
2024-08-18 04:34:09 +00:00
|
|
|
t := time.Now()
|
2024-07-21 07:57:47 +00:00
|
|
|
|
2024-09-08 13:45:24 +00:00
|
|
|
addrs, err := clt.Resolve(ctx, host, d.qTypes)
|
2024-08-18 06:55:47 +00:00
|
|
|
// addrs, err := clt.Resolve(ctx, host, []uint16{dns.TypeAAAA})
|
2024-07-21 07:57:47 +00:00
|
|
|
if err != nil {
|
2024-08-18 06:55:47 +00:00
|
|
|
return "", fmt.Errorf("%s: %w", clt, err)
|
2024-08-06 08:48:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
d := time.Since(t).Milliseconds()
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("resolved %s from %s in %d ms", addrs[0].String(), host, d)
|
2024-08-18 06:55:47 +00:00
|
|
|
return addrs[0].String(), nil
|
2024-08-18 04:34:09 +00:00
|
|
|
}
|
2024-08-12 22:37:20 +00:00
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
return "", fmt.Errorf("could not resolve %s using %s", host, clt)
|
2024-08-18 04:34:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 01:50:06 +00:00
|
|
|
func (d *Dns) clientFactory(enableDoh bool, useSystemDns bool) Resolver {
|
2024-08-18 06:55:47 +00:00
|
|
|
if useSystemDns {
|
|
|
|
return d.systemClient
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
if enableDoh {
|
|
|
|
return d.dohClient
|
2024-08-18 04:34:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
return d.generalClient
|
2024-08-18 04:34:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
func parseIpAddr(addr string) (*net.IPAddr, error) {
|
|
|
|
ip := net.ParseIP(addr)
|
|
|
|
if ip == nil {
|
|
|
|
return nil, fmt.Errorf("%s is not an ip address", addr)
|
2024-08-12 22:37:20 +00:00
|
|
|
}
|
2024-08-18 04:34:09 +00:00
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
ipAddr := &net.IPAddr{
|
|
|
|
IP: ip,
|
2024-08-18 04:34:09 +00:00
|
|
|
}
|
2024-08-12 22:37:20 +00:00
|
|
|
|
2024-08-18 06:55:47 +00:00
|
|
|
return ipAddr, nil
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|