doggo/cmd/resolver.go

48 lines
1.1 KiB
Go
Raw Normal View History

2020-12-11 12:18:54 +01:00
package main
import (
"runtime"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/urfave/cli/v2"
)
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-12 11:57:13 +01:00
func (hub *Hub) initResolver(c *cli.Context) error {
2020-12-12 07:16:13 +01:00
// check if DOH flag is set.
if hub.QueryFlags.IsDOH {
rslvr, err := resolvers.NewDOHResolver(hub.QueryFlags.Nameservers.Value())
if err != nil {
return err
}
hub.Resolver = rslvr
return nil
}
2020-12-11 12:18:54 +01:00
if len(hub.QueryFlags.Nameservers.Value()) == 0 {
if runtime.GOOS == "windows" {
// TODO: Add a method for reading system default nameserver in windows.
} else {
2020-12-13 08:15:45 +01:00
rslvr, err := resolvers.NewSystemResolver(resolvers.DefaultResolvConfPath)
2020-12-11 12:18:54 +01:00
if err != nil {
return err
}
hub.Resolver = rslvr
2020-12-12 07:16:13 +01:00
return nil
2020-12-11 12:18:54 +01:00
}
} else {
2020-12-12 07:46:54 +01:00
rslvr, err := resolvers.NewClassicResolver(hub.QueryFlags.Nameservers.Value(), resolvers.ClassicResolverOpts{
UseIPv4: hub.QueryFlags.UseIPv4,
UseIPv6: hub.QueryFlags.UseIPv6,
2020-12-12 08:46:45 +01:00
UseTLS: hub.QueryFlags.IsDOT,
UseTCP: hub.QueryFlags.UseTCP,
2020-12-12 07:46:54 +01:00
})
2020-12-12 07:16:13 +01:00
if err != nil {
return err
}
2020-12-11 12:18:54 +01:00
hub.Resolver = rslvr
2020-12-12 07:16:13 +01:00
return nil
2020-12-11 12:18:54 +01:00
}
return nil
}