fix nil pointer and testing public ip
This commit is contained in:
@@ -39,6 +39,35 @@ func natTest(serverHost string, serverPort int, localPort int, echoPort int) (pu
|
|||||||
}
|
}
|
||||||
natRsp := NatDetectRsp{}
|
natRsp := NatDetectRsp{}
|
||||||
err = json.Unmarshal(buffer[openP2PHeaderSize:nRead], &natRsp)
|
err = json.Unmarshal(buffer[openP2PHeaderSize:nRead], &natRsp)
|
||||||
|
|
||||||
|
// testing for public ip
|
||||||
|
if echoPort != 0 {
|
||||||
|
for {
|
||||||
|
gLog.Printf(LevelINFO, "public ip test start %s:%d", natRsp.IP, echoPort)
|
||||||
|
conn, err := net.ListenUDP("udp", nil)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
dst, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", natRsp.IP, echoPort))
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
conn.WriteTo([]byte("echo"), dst)
|
||||||
|
buf := make([]byte, 1600)
|
||||||
|
|
||||||
|
// wait for echo testing
|
||||||
|
conn.SetReadDeadline(time.Now().Add(PublicIPEchoTimeout))
|
||||||
|
_, _, err = conn.ReadFromUDP(buf)
|
||||||
|
if err == nil {
|
||||||
|
gLog.Println(LevelINFO, "public ip:YES")
|
||||||
|
natRsp.IsPublicIP = 1
|
||||||
|
} else {
|
||||||
|
gLog.Println(LevelINFO, "public ip:NO")
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
return natRsp.IP, natRsp.IsPublicIP, natRsp.Port, nil
|
return natRsp.IP, natRsp.IsPublicIP, natRsp.Port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ type p2pApp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (app *p2pApp) isActive() bool {
|
func (app *p2pApp) isActive() bool {
|
||||||
|
if app.tunnel == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if app.rtid == 0 { // direct mode app heartbeat equals to tunnel heartbeat
|
if app.rtid == 0 { // direct mode app heartbeat equals to tunnel heartbeat
|
||||||
return app.tunnel.isActive()
|
return app.tunnel.isActive()
|
||||||
}
|
}
|
||||||
@@ -119,7 +122,9 @@ func (app *p2pApp) close() {
|
|||||||
if app.listener != nil {
|
if app.listener != nil {
|
||||||
app.listener.Close()
|
app.listener.Close()
|
||||||
}
|
}
|
||||||
app.tunnel.closeOverlayConns(app.id)
|
if app.tunnel != nil {
|
||||||
|
app.tunnel.closeOverlayConns(app.id)
|
||||||
|
}
|
||||||
app.wg.Wait()
|
app.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -11,7 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const OpenP2PVersion = "0.96.0"
|
const OpenP2PVersion = "0.96.1"
|
||||||
const ProducnName string = "openp2p"
|
const ProducnName string = "openp2p"
|
||||||
|
|
||||||
type openP2PHeader struct {
|
type openP2PHeader struct {
|
||||||
@@ -127,7 +127,7 @@ const (
|
|||||||
AESKeySize = 16
|
AESKeySize = 16
|
||||||
MaxRetry = 10
|
MaxRetry = 10
|
||||||
RetryInterval = time.Second * 30
|
RetryInterval = time.Second * 30
|
||||||
PublicIPEchoTimeout = time.Second * 5
|
PublicIPEchoTimeout = time.Second * 3
|
||||||
NatTestTimeout = time.Second * 10
|
NatTestTimeout = time.Second * 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user