feat: DNS. Add the ability to disable version 6 address resolution (#240)

Co-authored-by: Andrey Semenov <semenov.ao@greendatasoft.ru>
This commit is contained in:
LiquidTheDangerous 2024-09-08 18:45:24 +05:00 committed by GitHub
parent 786d31cbb7
commit abd7441d0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 2 deletions

View File

@ -26,18 +26,25 @@ type Dns struct {
systemClient Resolver systemClient Resolver
generalClient Resolver generalClient Resolver
dohClient Resolver dohClient Resolver
qTypes []uint16
} }
func NewDns(config *util.Config) *Dns { func NewDns(config *util.Config) *Dns {
addr := config.DnsAddr addr := config.DnsAddr
port := strconv.Itoa(config.DnsPort) port := strconv.Itoa(config.DnsPort)
var qTypes []uint16
if config.DnsIPv4Only {
qTypes = []uint16{dns.TypeA}
} else {
qTypes = []uint16{dns.TypeAAAA, dns.TypeA}
}
return &Dns{ return &Dns{
host: config.DnsAddr, host: config.DnsAddr,
port: port, port: port,
systemClient: resolver.NewSystemResolver(), systemClient: resolver.NewSystemResolver(),
generalClient: resolver.NewGeneralResolver(net.JoinHostPort(addr, port)), generalClient: resolver.NewGeneralResolver(net.JoinHostPort(addr, port)),
dohClient: resolver.NewDOHResolver(addr), dohClient: resolver.NewDOHResolver(addr),
qTypes: qTypes,
} }
} }
@ -57,7 +64,7 @@ func (d *Dns) ResolveHost(ctx context.Context, host string, enableDoh bool, useS
t := time.Now() t := time.Now()
addrs, err := clt.Resolve(ctx, host, []uint16{dns.TypeAAAA, dns.TypeA}) addrs, err := clt.Resolve(ctx, host, d.qTypes)
// addrs, err := clt.Resolve(ctx, host, []uint16{dns.TypeAAAA}) // addrs, err := clt.Resolve(ctx, host, []uint16{dns.TypeAAAA})
if err != nil { if err != nil {
return "", fmt.Errorf("%s: %w", clt, err) return "", fmt.Errorf("%s: %w", clt, err)

View File

@ -13,6 +13,7 @@ type Args struct {
Port uint16 Port uint16
DnsAddr string DnsAddr string
DnsPort uint16 DnsPort uint16
DnsIPv4Only bool
EnableDoh bool EnableDoh bool
Debug bool Debug bool
Silent bool Silent bool
@ -57,6 +58,7 @@ fragmentation for the first data packet and the rest
"pattern", "pattern",
"bypass DPI only on packets matching this regex pattern; can be given multiple times", "bypass DPI only on packets matching this regex pattern; can be given multiple times",
) )
flag.BoolVar(&args.DnsIPv4Only, "dns-ipv4-only", false, "resolve only version 4 addresses")
flag.Parse() flag.Parse()

View File

@ -13,6 +13,7 @@ type Config struct {
Port int Port int
DnsAddr string DnsAddr string
DnsPort int DnsPort int
DnsIPv4Only bool
EnableDoh bool EnableDoh bool
Debug bool Debug bool
Silent bool Silent bool
@ -36,6 +37,7 @@ func (c *Config) Load(args *Args) {
c.Port = int(args.Port) c.Port = int(args.Port)
c.DnsAddr = args.DnsAddr c.DnsAddr = args.DnsAddr
c.DnsPort = int(args.DnsPort) c.DnsPort = int(args.DnsPort)
c.DnsIPv4Only = args.DnsIPv4Only
c.Debug = args.Debug c.Debug = args.Debug
c.EnableDoh = args.EnableDoh c.EnableDoh = args.EnableDoh
c.Silent = args.Silent c.Silent = args.Silent