Skip to content

Commit

Permalink
chore: fix linting errors and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
clementnuss committed Feb 20, 2023
1 parent f7701bc commit 39108fc
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,8 @@ func (srv DNSServer) String() string {
return fmt.Sprintf("%s://%s", srv.network, srv.name)
}

func main() {
fs := flag.NewFlagSet("hostlookuper", flag.ExitOnError)

var (
debug = fs.Bool("debug", false, "enable verbose logging")
interval = fs.Duration("interval", 5*time.Second, "interval between DNS checks. must be in Go time.ParseDuration format, e.g. 5s or 5m or 1h, etc")
timeout = fs.Duration("timeout", 5*time.Second, "maximum timeout for a DNS query. must be in Go time.ParseDuration format, e.g. 5s or 5m or 1h, etc")
listen = fs.String("listen", ":9090", "address on which hostlookuper listens. e.g. 0.0.0.0:9090")
hostsVal = fs.String("hosts", "google.ch,ch.ch", "comma-separated list of hosts against which to perform DNS lookups")
dnsServersVal = fs.String("dns-servers", "udp://9.9.9.9:53,udp://8.8.8.8:53,udp://one.one.one.one:53", "comma-separated list of DNS servers. if the protocol is omitted, udp is implied, and if the port is omitted, 53 is implied")
)

err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("HOSTLOOKUPER"))
if err != nil {
fmt.Printf("unable to parse args/envs, exiting. error message: %v", err)

os.Exit(2)
}

rand.Seed(time.Now().UnixNano())

logger := flash.New(flash.WithoutCaller())
logger.SetDebug(*debug)
l := logger.Get()

var hosts hosts = strings.Split(*hostsVal, ",")
if err := hosts.isValid(); err != nil {
l.Fatalw("parsing hosts failed",
"val", hostsVal,
"err", err,
)
}

dnsServersList := strings.Split(*dnsServersVal, ",")
func parseDNSServers(l *zap.SugaredLogger, dnsServersStr string) []DNSServer {
dnsServersList := strings.Split(dnsServersStr, ",")
dnsServers := make([]DNSServer, 0, len(dnsServersList))

for _, dnsServer := range dnsServersList {
Expand Down Expand Up @@ -105,6 +73,7 @@ func main() {
name = address
spl = strings.Split(address, ":")
host, port := spl[0], spl[1]

if ip := net.ParseIP(host); ip == nil { // dns server specified using DNS name
ips, err := net.DefaultResolver.LookupIP(context.Background(), "ip", host)
if err != nil {
Expand All @@ -127,6 +96,44 @@ func main() {
})
}

return dnsServers
}

func main() {
fs := flag.NewFlagSet("hostlookuper", flag.ExitOnError)

var (
debug = fs.Bool("debug", false, "enable verbose logging")
interval = fs.Duration("interval", 5*time.Second, "interval between DNS checks. must be in Go time.ParseDuration format, e.g. 5s or 5m or 1h, etc")
timeout = fs.Duration("timeout", 5*time.Second, "maximum timeout for a DNS query. must be in Go time.ParseDuration format, e.g. 5s or 5m or 1h, etc")
listen = fs.String("listen", ":9090", "address on which hostlookuper listens. e.g. 0.0.0.0:9090")
hostsVal = fs.String("hosts", "google.ch,ch.ch", "comma-separated list of hosts against which to perform DNS lookups")
dnsServersVal = fs.String("dns-servers", "udp://9.9.9.9:53,udp://8.8.8.8:53,udp://one.one.one.one:53", "comma-separated list of DNS servers. if the protocol is omitted, udp is implied, and if the port is omitted, 53 is implied")
)

err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("HOSTLOOKUPER"))
if err != nil {
fmt.Printf("unable to parse args/envs, exiting. error message: %v", err)

os.Exit(2)
}

rand.Seed(time.Now().UnixNano())

logger := flash.New(flash.WithoutCaller())
logger.SetDebug(*debug)
l := logger.Get()

var hosts hosts = strings.Split(*hostsVal, ",")
if err := hosts.isValid(); err != nil {
l.Fatalw("parsing hosts failed",
"val", hostsVal,
"err", err,
)
}

dnsServers := parseDNSServers(l, *dnsServersVal)

for _, host := range hosts {
for _, dnsServer := range dnsServers {
look := newLookuper(host, dnsServer, timeout, l)
Expand Down

0 comments on commit 39108fc

Please sign in to comment.