OSDN Git Service

fix for nil pointer bug
authorpaladz <453256728@qq.com>
Wed, 9 May 2018 12:15:47 +0000 (20:15 +0800)
committerpaladz <453256728@qq.com>
Wed, 9 May 2018 12:15:47 +0000 (20:15 +0800)
p2p/pex_reactor.go
p2p/public_ip.go

index 79f8020..86b11b8 100644 (file)
@@ -58,7 +58,6 @@ type PEXReactor struct {
        // tracks message count by peer, so we can prevent abuse
        msgCountByPeer    *cmn.CMap
        maxMsgCountByPeer uint16
-       wg                sync.WaitGroup
 }
 
 // NewPEXReactor creates new PEX reactor.
@@ -330,19 +329,13 @@ func (r *PEXReactor) ensurePeers() {
                }
                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() {
@@ -357,6 +350,15 @@ func (r *PEXReactor) ensurePeers() {
        }
 }
 
+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)
 
index 441f7b6..6f6bc74 100644 (file)
@@ -4,13 +4,13 @@ import (
        "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/",
@@ -24,8 +24,8 @@ var defaultServices  = []string{
 
 type IpResult struct {
        Success bool
-       Ip string
-       Error error
+       Ip      string
+       Error   error
 }
 
 var timeout time.Duration
@@ -45,8 +45,8 @@ func GetIP(services []string, to time.Duration) *IpResult {
        for k := range services {
                go ipAddress(services[k], done)
        }
-       for ;; {
-               select{
+       for {
+               select {
                case result := <-done:
                        if result.Success {
                                return result
@@ -81,7 +81,7 @@ func ipAddress(service string, done chan<- *IpResult) {
 
                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
                }
@@ -97,11 +97,3 @@ func sendResult(result *IpResult, done chan<- *IpResult) {
                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
-}