Skip to content

Commit

Permalink
Merge pull request #50 from mrozitron/mrozitron-drop-dial-error-checks
Browse files Browse the repository at this point in the history
simulator: don't treat dial errors as errors
  • Loading branch information
tg committed Oct 27, 2021
2 parents 66c7320 + 270f8fe commit 9e03c61
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
19 changes: 4 additions & 15 deletions simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ func (s *TCPConnectSimulator) Simulate(ctx context.Context, dst string) error {
if conn != nil {
conn.Close()
}
// This will likely generate some superfluous io-timeout error messages, but if the
// user specifies a misconfigured interface, they'll see the simulation failing.
if err != nil && (!isSoftError(err, "connect: connection refused") || isDialError(err)) {
// Ignore "connection refused" and timeouts.
if err != nil && !isSoftError(err, "connect: connection refused") {
return err
}
return nil
Expand Down Expand Up @@ -111,24 +110,14 @@ func (s *DNSResolveSimulator) Simulate(ctx context.Context, dst string) error {
host = utils.FQDN(host)

_, err := r.LookupHost(ctx, host)
// Ignore "no such host". Will ignore timeouts as well, so check for dial errors.
if err != nil && (!isSoftError(err, "no such host") || isDialError(err)) {
// Ignore "no such host" and timeouts.
if err != nil && !isSoftError(err, "no such host") {
return err
}

return nil
}

// isDialError scans an error message for signs of a dial error, returning a boolean.
func isDialError(err error) bool {
// Errors we're after are of the form:
// lookup abc.sandbox.alphasoc.xyz. on A.B.C.D:53: dial udp E.F.G.H:0->A.B.C.D:53: i/o timeout
// TODO: something more robust? I don't want to double-dial though.
return isTimeout(err) &&
strings.Contains(err.Error(), "dial") &&
strings.Count(err.Error(), "->") == 1
}

func isTimeout(err error) bool {
netErr, ok := err.(net.Error)
if !ok {
Expand Down
7 changes: 3 additions & 4 deletions simulator/spambot.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ func (s *Spambot) Hosts(scope string, size int) ([]string, error) {
for n := 0; len(hosts) < size && n < len(idx); n++ {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
host := utils.FQDN(domains[idx[n]])
mx, err := rv.LookupMX(ctx, host)
mx, _ := rv.LookupMX(ctx, host)
cancel()
// Check error message for sign of resolver/routing issue.
if err != nil && isDialError(err) {
return hosts, err
}
// TODO: at some point we'll want to check dialer errors for a sign of resolver
// problems
if len(mx) > 0 {
host := strings.TrimSuffix(mx[0].Host, ".")
if !seen[host] {
Expand Down
4 changes: 2 additions & 2 deletions simulator/tunnel-dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (s *Tunnel) Simulate(ctx context.Context, host string) error {
defer cancelFn()
_, err := r.LookupTXT(ctx, fmt.Sprintf("%s.%s", label, host))

// Ignore "no such host". Will ignore timeouts as well, so check for dial errors.
if err != nil && (!isSoftError(err, "no such host") || isDialError(err)) {
// Ignore "no such host". Will ignore timeouts as well.
if err != nil && !isSoftError(err, "no such host") {
return err
}

Expand Down

0 comments on commit 9e03c61

Please sign in to comment.