// tracks message count by peer, so we can prevent abuse
msgCountByPeer *cmn.CMap
maxMsgCountByPeer uint16
- wg sync.WaitGroup
}
// NewPEXReactor creates new PEX reactor.
}
toDial[picked.IP.String()] = picked
}
- // Dial picked addresses
+
+ var wg *sync.WaitGroup
for _, item := range toDial {
- r.wg.Add(1)
- go func(picked *NetAddress) {
- if _, err := r.Switch.DialPeerWithAddress(picked, false); err != nil {
- r.book.MarkAttempt(picked)
- } else {
- r.book.MarkGood(picked)
- }
- r.wg.Done()
- }(item)
+ wg.Add(1)
+ go r.dialPeerWorker(item, wg)
}
- r.wg.Wait()
+ wg.Wait()
// If we need more addresses, pick a random peer and ask for more.
if r.book.NeedMoreAddrs() {
}
}
+func (r *PEXReactor) dialPeerWorker(a *NetAddress, wg *sync.WaitGroup) {
+ if _, err := r.Switch.DialPeerWithAddress(a, false); err != nil {
+ r.book.MarkAttempt(a)
+ } else {
+ r.book.MarkGood(a)
+ }
+ wg.Done()
+}
+
func (r *PEXReactor) flushMsgCountByPeer() {
ticker := time.NewTicker(msgCountByPeerFlushInterval)
"errors"
"fmt"
"io/ioutil"
+ "net"
"net/http"
"strings"
"time"
- "regexp"
)
-var defaultServices = []string{
+var defaultServices = []string{
"http://members.3322.org/dyndns/getip",
"http://ifconfig.me/",
"http://icanhazip.com/",
type IpResult struct {
Success bool
- Ip string
- Error error
+ Ip string
+ Error error
}
var timeout time.Duration
for k := range services {
go ipAddress(services[k], done)
}
- for ;; {
- select{
+ for {
+ select {
case result := <-done:
if result.Success {
return result
address, err := ioutil.ReadAll(resp.Body)
ip := fmt.Sprintf("%s", strings.TrimSpace(string(address)))
- if err== nil && checkIp(ip) {
+ if err == nil && net.ParseIP(ip) != nil {
sendResult(&IpResult{true, ip, nil}, done)
return
}
return
}
}
-
-func checkIp(ip string) bool {
- match, _ := regexp.MatchString(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`, ip)
- if match {
- return true
- }
- return false
-}