Skip to content

Commit

Permalink
Merge pull request #52 from /issues/51-dns-probe
Browse files Browse the repository at this point in the history
cmd/run: failed dns probe for any reason should not be fatal
  • Loading branch information
kmroz committed Feb 15, 2022
2 parents f650b95 + 1eee537 commit 8303298
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cmd/run/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func printMsg(s *Simulation, msg string) {
}

func printWelcome(ip, dnsIntfIP string) {
if dnsIntfIP == "" {
dnsIntfIP = "UNKNOWN, system defaults will be used"
}
fmt.Printf(`
AlphaSOC Network Flight Simulator™ %s (https://github.com/alphasoc/flightsim)
The address of the network interface for IP traffic is %s
Expand Down
23 changes: 13 additions & 10 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ const (
)

// getDefaultDNSIntf runs a DNS probe using default system resolver and returns the IP of
// the interface used and an error. Thanks @tg.
func getDefaultDNSIntf() (string, error) {
// the interface used, or an empty string. Thanks @tg.
func getDefaultDNSIntf() string {
timeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand All @@ -345,31 +345,34 @@ func getDefaultDNSIntf() (string, error) {
}
_, err := r.LookupHost(ctx, "alphasoc.com")
if err != nil {
return "", err
return ""
}
// In cases where r.Dial() is not invoked, defaultDNSServer will be "", so don't bother
// continuing with detection (ie. on Windows).
if defaultDNSServer == "" {
return ""
}
conn, err := net.DialTimeout("udp", defaultDNSServer, timeout)
if err != nil {
return "", err
return ""
}
dnsIntfIP, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
return "", err
return ""
}
return dnsIntfIP, nil
return dnsIntfIP
}

func run(sims []*Simulation, bind simulator.BindAddr, size int) error {
// If user override on iface, both IP and DNS traffic will flow through bind.Addr.
// NOTE: not passing the DNS server to printWelcome(), as it may be confusing in cases
// where there are multiple nameservers configured (ie. resolver errors will carry
// the address of the last queried nameserver).
defaultDNSIntfIP, err := getDefaultDNSIntf()
if err != nil {
return fmt.Errorf("Failed DNS probe: %v", err)
}
defaultDNSIntfIP := getDefaultDNSIntf()
if bind.UserSet {
printWelcome(bind.String(), bind.String())
} else {
// NOTE: defaultDNSIntfIP _may_ be "".
printWelcome(bind.String(), defaultDNSIntfIP)
}
printHeader()
Expand Down

0 comments on commit 8303298

Please sign in to comment.