Skip to content

Commit

Permalink
Bring back support for go version <1.13
Browse files Browse the repository at this point in the history
- net.DNSError.IsNotFound required go1.13
  • Loading branch information
tg committed Sep 18, 2019
1 parent 02a271e commit 19ef593
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
40 changes: 25 additions & 15 deletions simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
14 changes: 3 additions & 11 deletions simulator/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19ef593

Please sign in to comment.