diff --git a/simulator/simulator.go b/simulator/simulator.go index 36d2478..b5a42e3 100644 --- a/simulator/simulator.go +++ b/simulator/simulator.go @@ -39,18 +39,14 @@ func (TCPConnectSimulator) Simulate(ctx context.Context, bind net.IP, dst string } conn, err := d.DialContext(ctx, "tcp", dst) - if err != nil { - if err, ok := err.(net.Error); ok { - // TODO: find a better way of determining refused connection? - if err.Timeout() || strings.HasSuffix(err.Error(), "connect: connection refused") { - return nil - } - } - return err + if conn != nil { + conn.Close() } - conn.Close() - return nil + if isSoftError(err, "connect: connection refused") { + return nil + } + return err } type DNSResolveSimulator struct { @@ -72,11 +68,25 @@ func (DNSResolveSimulator) Simulate(ctx context.Context, bind net.IP, dst string } _, err := r.LookupHost(ctx, utils.FQDN(host)) - if err, ok := err.(*net.DNSError); ok { - if err.IsNotFound || err.IsTimeout { - return nil - } + if isSoftError(err, "no such host") { + return nil } - return err } + +func isSoftError(err error, ss ...string) bool { + netErr, ok := err.(net.Error) + if !ok { + return false + } + if netErr.Timeout() { + return true + } + errStr := err.Error() + for n := range ss { + if strings.Contains(errStr, ss[n]) { + return true + } + } + return false +} diff --git a/simulator/tunnel.go b/simulator/tunnel.go index 584431d..a2cb4c8 100644 --- a/simulator/tunnel.go +++ b/simulator/tunnel.go @@ -43,17 +43,9 @@ func (*Tunnel) Simulate(ctx context.Context, extIP net.IP, host string) error { ctx, _ := context.WithTimeout(ctx, 200*time.Millisecond) _, err := r.LookupTXT(ctx, fmt.Sprintf("%s.%s", label, host)) - if err != nil { - // ignore timeouts and NotFound; - // TODO: actually make sure we get a valid response - switch e := err.(type) { - case *net.DNSError: - if !(e.IsNotFound || e.IsTimeout) { - return err - } - default: - return err - } + // ignore timeout and "no such host" + if err != nil && !isSoftError(err, "no such host") { + return err } // wait until context expires so we don't flood