1.5.5
This commit is contained in:
+29
-29
@@ -11,8 +11,8 @@ import (
|
||||
)
|
||||
|
||||
func handshakeC2C(t *P2PTunnel) (err error) {
|
||||
gLog.Printf(LevelDEBUG, "handshakeC2C %s:%d:%d to %s:%d", t.pn.config.Node, t.coneLocalPort, t.coneNatPort, t.config.peerIP, t.config.peerConeNatPort)
|
||||
defer gLog.Printf(LevelDEBUG, "handshakeC2C ok")
|
||||
gLog.Printf(LvDEBUG, "handshakeC2C %s:%d:%d to %s:%d", t.pn.config.Node, t.coneLocalPort, t.coneNatPort, t.config.peerIP, t.config.peerConeNatPort)
|
||||
defer gLog.Printf(LvDEBUG, "handshakeC2C ok")
|
||||
conn, err := net.ListenUDP("udp", t.la)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -20,40 +20,40 @@ func handshakeC2C(t *P2PTunnel) (err error) {
|
||||
defer conn.Close()
|
||||
_, err = UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshake, P2PHandshakeReq{ID: t.id})
|
||||
if err != nil {
|
||||
gLog.Println(LevelDEBUG, "handshakeC2C write MsgPunchHandshake error:", err)
|
||||
gLog.Println(LvDEBUG, "handshakeC2C write MsgPunchHandshake error:", err)
|
||||
return err
|
||||
}
|
||||
ra, head, _, _, err := UDPRead(conn, 5000)
|
||||
if err != nil {
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
gLog.Println(LevelDEBUG, err, ", return this error when ip was not reachable, retry read")
|
||||
gLog.Println(LvDEBUG, err, ", return this error when ip was not reachable, retry read")
|
||||
ra, head, _, _, err = UDPRead(conn, 5000)
|
||||
if err != nil {
|
||||
gLog.Println(LevelDEBUG, "handshakeC2C read MsgPunchHandshake error:", err)
|
||||
gLog.Println(LvDEBUG, "handshakeC2C read MsgPunchHandshake error:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
t.ra, _ = net.ResolveUDPAddr("udp", ra.String())
|
||||
// cone server side
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshake {
|
||||
gLog.Printf(LevelDEBUG, "read %d handshake ", t.id)
|
||||
gLog.Printf(LvDEBUG, "read %d handshake ", t.id)
|
||||
UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshakeAck, P2PHandshakeReq{ID: t.id})
|
||||
_, head, _, _, err = UDPRead(conn, 5000)
|
||||
if err != nil {
|
||||
gLog.Println(LevelDEBUG, "handshakeC2C write MsgPunchHandshakeAck error", err)
|
||||
gLog.Println(LvDEBUG, "handshakeC2C write MsgPunchHandshakeAck error", err)
|
||||
return err
|
||||
}
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshakeAck {
|
||||
gLog.Printf(LevelDEBUG, "read %d handshake ack ", t.id)
|
||||
gLog.Printf(LvDEBUG, "read %d handshake ack ", t.id)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// cone client side will only read handshake ack
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshakeAck {
|
||||
gLog.Printf(LevelDEBUG, "read %d handshake ack ", t.id)
|
||||
gLog.Printf(LvDEBUG, "read %d handshake ack ", t.id)
|
||||
_, err = UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshakeAck, P2PHandshakeReq{ID: t.id})
|
||||
if err != nil {
|
||||
gLog.Println(LevelDEBUG, "handshakeC2C write MsgPunchHandshakeAck error", err)
|
||||
gLog.Println(LvDEBUG, "handshakeC2C write MsgPunchHandshakeAck error", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -61,8 +61,8 @@ func handshakeC2C(t *P2PTunnel) (err error) {
|
||||
}
|
||||
|
||||
func handshakeC2S(t *P2PTunnel) error {
|
||||
gLog.Printf(LevelDEBUG, "handshakeC2S start")
|
||||
defer gLog.Printf(LevelDEBUG, "handshakeC2S end")
|
||||
gLog.Printf(LvDEBUG, "handshakeC2S start")
|
||||
defer gLog.Printf(LvDEBUG, "handshakeC2S end")
|
||||
// even if read timeout, continue handshake
|
||||
t.pn.read(t.config.PeerNode, MsgPush, MsgPushHandshakeStart, SymmetricHandshakeAckTimeout)
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
@@ -73,7 +73,7 @@ func handshakeC2S(t *P2PTunnel) error {
|
||||
}
|
||||
defer conn.Close()
|
||||
go func() error {
|
||||
gLog.Printf(LevelDEBUG, "send symmetric handshake to %s from %d:%d start", t.config.peerIP, t.coneLocalPort, t.coneNatPort)
|
||||
gLog.Printf(LvDEBUG, "send symmetric handshake to %s from %d:%d start", t.config.peerIP, t.coneLocalPort, t.coneNatPort)
|
||||
for i := 0; i < SymmetricHandshakeNum; i++ {
|
||||
// TODO: auto calc cost time
|
||||
time.Sleep(SymmetricHandshakeInterval)
|
||||
@@ -83,35 +83,35 @@ func handshakeC2S(t *P2PTunnel) error {
|
||||
}
|
||||
_, err = UDPWrite(conn, dst, MsgP2P, MsgPunchHandshake, P2PHandshakeReq{ID: t.id})
|
||||
if err != nil {
|
||||
gLog.Println(LevelDEBUG, "handshakeC2S write MsgPunchHandshake error:", err)
|
||||
gLog.Println(LvDEBUG, "handshakeC2S write MsgPunchHandshake error:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
gLog.Println(LevelDEBUG, "send symmetric handshake end")
|
||||
gLog.Println(LvDEBUG, "send symmetric handshake end")
|
||||
return nil
|
||||
}()
|
||||
deadline := time.Now().Add(SymmetricHandshakeAckTimeout)
|
||||
err = conn.SetReadDeadline(deadline)
|
||||
if err != nil {
|
||||
gLog.Println(LevelERROR, "SymmetricHandshakeAckTimeout SetReadDeadline error")
|
||||
gLog.Println(LvERROR, "SymmetricHandshakeAckTimeout SetReadDeadline error")
|
||||
return err
|
||||
}
|
||||
// read response of the punching hole ok port
|
||||
result := make([]byte, 1024)
|
||||
_, dst, err := conn.ReadFrom(result)
|
||||
if err != nil {
|
||||
gLog.Println(LevelERROR, "handshakeC2S wait timeout")
|
||||
gLog.Println(LvERROR, "handshakeC2S wait timeout")
|
||||
return err
|
||||
}
|
||||
head := &openP2PHeader{}
|
||||
err = binary.Read(bytes.NewReader(result[:openP2PHeaderSize]), binary.LittleEndian, head)
|
||||
if err != nil {
|
||||
gLog.Println(LevelERROR, "parse p2pheader error:", err)
|
||||
gLog.Println(LvERROR, "parse p2pheader error:", err)
|
||||
return err
|
||||
}
|
||||
t.ra, _ = net.ResolveUDPAddr("udp", dst.String())
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshakeAck {
|
||||
gLog.Printf(LevelDEBUG, "handshakeC2S read %d handshake ack %s", t.id, dst.String())
|
||||
gLog.Printf(LvDEBUG, "handshakeC2S read %d handshake ack %s", t.id, dst.String())
|
||||
_, err = UDPWrite(conn, dst, MsgP2P, MsgPunchHandshakeAck, P2PHandshakeReq{ID: t.id})
|
||||
return err
|
||||
}
|
||||
@@ -119,11 +119,11 @@ func handshakeC2S(t *P2PTunnel) error {
|
||||
}
|
||||
|
||||
func handshakeS2C(t *P2PTunnel) error {
|
||||
gLog.Printf(LevelDEBUG, "handshakeS2C start")
|
||||
defer gLog.Printf(LevelDEBUG, "handshakeS2C end")
|
||||
gLog.Printf(LvDEBUG, "handshakeS2C start")
|
||||
defer gLog.Printf(LvDEBUG, "handshakeS2C end")
|
||||
gotCh := make(chan *net.UDPAddr, 5)
|
||||
// sequencely udp send handshake, do not parallel send
|
||||
gLog.Printf(LevelDEBUG, "send symmetric handshake to %s:%d start", t.config.peerIP, t.config.peerConeNatPort)
|
||||
gLog.Printf(LvDEBUG, "send symmetric handshake to %s:%d start", t.config.peerIP, t.config.peerConeNatPort)
|
||||
gotIt := false
|
||||
gotMtx := sync.Mutex{}
|
||||
for i := 0; i < SymmetricHandshakeNum; i++ {
|
||||
@@ -132,7 +132,7 @@ func handshakeS2C(t *P2PTunnel) error {
|
||||
go func(t *P2PTunnel) error {
|
||||
conn, err := net.ListenUDP("udp", nil)
|
||||
if err != nil {
|
||||
gLog.Printf(LevelDEBUG, "listen error")
|
||||
gLog.Printf(LvDEBUG, "listen error")
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
@@ -150,15 +150,15 @@ func handshakeS2C(t *P2PTunnel) error {
|
||||
gotIt = true
|
||||
t.la, _ = net.ResolveUDPAddr("udp", conn.LocalAddr().String())
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshake {
|
||||
gLog.Printf(LevelDEBUG, "handshakeS2C read %d handshake ", t.id)
|
||||
gLog.Printf(LvDEBUG, "handshakeS2C read %d handshake ", t.id)
|
||||
UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshakeAck, P2PHandshakeReq{ID: t.id})
|
||||
_, head, _, _, err = UDPRead(conn, 5000)
|
||||
if err != nil {
|
||||
gLog.Println(LevelDEBUG, "handshakeS2C handshake error")
|
||||
gLog.Println(LvDEBUG, "handshakeS2C handshake error")
|
||||
return err
|
||||
}
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshakeAck {
|
||||
gLog.Printf(LevelDEBUG, "handshakeS2C read %d handshake ack %s", t.id, conn.LocalAddr().String())
|
||||
gLog.Printf(LvDEBUG, "handshakeS2C read %d handshake ack %s", t.id, conn.LocalAddr().String())
|
||||
gotCh <- t.la
|
||||
return nil
|
||||
}
|
||||
@@ -166,15 +166,15 @@ func handshakeS2C(t *P2PTunnel) error {
|
||||
return nil
|
||||
}(t)
|
||||
}
|
||||
gLog.Printf(LevelDEBUG, "send symmetric handshake end")
|
||||
gLog.Println(LevelDEBUG, "handshakeS2C ready, notify peer connect")
|
||||
gLog.Printf(LvDEBUG, "send symmetric handshake end")
|
||||
gLog.Println(LvDEBUG, "handshakeS2C ready, notify peer connect")
|
||||
t.pn.push(t.config.PeerNode, MsgPushHandshakeStart, TunnelMsg{ID: t.id})
|
||||
|
||||
select {
|
||||
case <-time.After(SymmetricHandshakeAckTimeout):
|
||||
return fmt.Errorf("wait handshake failed")
|
||||
case la := <-gotCh:
|
||||
gLog.Println(LevelDEBUG, "symmetric handshake ok", la)
|
||||
gLog.Println(LvDEBUG, "symmetric handshake ok", la)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user