-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeers.go
More file actions
34 lines (29 loc) · 632 Bytes
/
peers.go
File metadata and controls
34 lines (29 loc) · 632 Bytes
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
package main
import (
"context"
"fmt"
"regexp"
)
const peerProtoReg = `(tcp|quic|tcp-tls){0,1}(?:\:\/\/){0,1}`
// peerAddrReg is a regular expression for matching the supported
// address formats
// <proto>://<server>[:<port>].
var peerAddrReg = regexp.MustCompile(
fmt.Sprintf(`^%s(%s|%s)%s$`, peerProtoReg, ipv4Reg, ipv6Reg, portReg),
)
func Peers(
ctx context.Context,
addresses ...string,
) error {
for _, addr := range addresses {
select {
case <-ctx.Done():
return ctx.Err()
default:
if !peerAddrReg.MatchString(addr) {
return fmt.Errorf("invalid peer address: %s", addr)
}
}
}
return nil
}