Skip to content

Commit

Permalink
attack: ensure go-routines finish in DNSCaching
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Senart <[email protected]>
  • Loading branch information
tsenart committed Jul 16, 2023
1 parent 174d804 commit 074c530
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,18 @@ func DNSCaching(ttl time.Duration) func(*Attacker) {
for _, ip := range ips {
go func(ip string) {
conn, err := dial(ctx, network, net.JoinHostPort(ip, port))
cancel()
ch <- result{conn, err}
}(ip)
}

for i := 0; i < cap(ch); i++ {
select {
case <-ctx.Done():
return nil, ctx.Err()
case r := <-ch:
if err = r.err; err != nil {
continue
}
return r.conn, nil
if r := <-ch; conn == nil {
conn, err = r.conn, r.err
}
}

return nil, err
return conn, err
}
}
}
Expand Down

0 comments on commit 074c530

Please sign in to comment.