2020-12-11 12:18:54 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-15 18:39:10 +01:00
|
|
|
"time"
|
2020-12-11 12:18:54 +01:00
|
|
|
|
|
|
|
"github.com/mr-karan/doggo/pkg/resolvers"
|
|
|
|
)
|
|
|
|
|
2020-12-12 11:57:13 +01:00
|
|
|
// initResolver checks for various flags and initialises
|
2020-12-12 08:46:45 +01:00
|
|
|
// the correct resolver based on the config.
|
2020-12-13 13:19:10 +01:00
|
|
|
func (hub *Hub) initResolver() error {
|
2020-12-12 07:16:13 +01:00
|
|
|
// check if DOH flag is set.
|
|
|
|
if hub.QueryFlags.IsDOH {
|
2020-12-15 18:39:10 +01:00
|
|
|
hub.Logger.Debug("initiating DOH resolver")
|
|
|
|
rslvr, err := resolvers.NewDOHResolver(hub.QueryFlags.Nameservers, resolvers.DOHResolverOpts{
|
|
|
|
Timeout: hub.QueryFlags.Timeout * time.Second,
|
|
|
|
})
|
2020-12-12 07:16:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 18:39:10 +01:00
|
|
|
hub.Resolver = append(hub.Resolver, rslvr)
|
2020-12-12 07:16:13 +01:00
|
|
|
}
|
2020-12-15 18:39:10 +01:00
|
|
|
if hub.QueryFlags.IsTCP {
|
|
|
|
hub.Logger.Debug("initiating TCP resolver")
|
|
|
|
rslvr, err := resolvers.NewTCPResolver(hub.QueryFlags.Nameservers, resolvers.TCPResolverOpts{
|
|
|
|
IPv4Only: hub.QueryFlags.UseIPv4,
|
|
|
|
IPv6Only: hub.QueryFlags.UseIPv6,
|
|
|
|
Timeout: hub.QueryFlags.Timeout * time.Second,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-11 12:18:54 +01:00
|
|
|
}
|
2020-12-15 18:39:10 +01:00
|
|
|
hub.Resolver = append(hub.Resolver, rslvr)
|
|
|
|
}
|
|
|
|
// If so far no resolver has been set, then fallback to UDP.
|
|
|
|
if hub.QueryFlags.IsUDP || len(hub.Resolver) == 0 {
|
|
|
|
hub.Logger.Debug("initiating UDP resolver")
|
|
|
|
rslvr, err := resolvers.NewUDPResolver(hub.QueryFlags.Nameservers, resolvers.UDPResolverOpts{
|
|
|
|
IPv4Only: hub.QueryFlags.UseIPv4,
|
|
|
|
IPv6Only: hub.QueryFlags.UseIPv6,
|
|
|
|
Timeout: hub.QueryFlags.Timeout * time.Second,
|
2020-12-12 07:46:54 +01:00
|
|
|
})
|
2020-12-12 07:16:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 18:39:10 +01:00
|
|
|
hub.Resolver = append(hub.Resolver, rslvr)
|
2020-12-11 12:18:54 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|