-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover.go
executable file
·46 lines (38 loc) · 1.06 KB
/
discover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p2p
import (
"context"
"errors"
"fmt"
"math/rand"
ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
)
func Discover(ctx context.Context, addr ma.Multiaddr, f func(ma.Multiaddr) (bool, error)) (bool, error) {
if comp, _ := ma.SplitFirst(addr); comp.Protocol().Name != "dnsaddr" {
return f(addr)
}
dnsResolver := madns.DefaultResolver
addrs, err := dnsResolver.Resolve(ctx, addr)
if err != nil {
return false, fmt.Errorf("dns resolve address %s: %w", addr, err)
}
if len(addrs) == 0 {
return false, errors.New("non-resolvable API endpoint")
}
rand.Shuffle(len(addrs), func(i, j int) {
addrs[i], addrs[j] = addrs[j], addrs[i]
})
for _, addr := range addrs {
stopped, err := Discover(ctx, addr, f)
if err != nil {
return false, fmt.Errorf("discover %s: %w", addr, err)
}
if stopped {
return true, nil
}
}
return false, nil
}