From 85a90aeb866b0a0cc8dbce1c0a16b6b1f92ddb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 22 Feb 2025 20:29:49 +0900 Subject: [PATCH] fix(nat): panic on nil endpoint --- gold/link/listen.go | 3 ++- gold/link/nat.go | 7 ++++--- gold/link/send.go | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/gold/link/listen.go b/gold/link/listen.go index 7180483..0927277 100644 --- a/gold/link/listen.go +++ b/gold/link/listen.go @@ -5,6 +5,7 @@ import ( "errors" "io" "net" + "reflect" "runtime" "strconv" "sync" @@ -173,7 +174,7 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish packet.Put() return } - if p.endpoint == nil || !p.endpoint.Euqal(addr) { + if reflect.ValueOf(p.endpoint).IsZero() || !p.endpoint.Euqal(addr) { if m.ep.Network() == "tcp" && !addr.Euqal(p.endpoint) { logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String()) p.endpoint = addr diff --git a/gold/link/nat.go b/gold/link/nat.go index a69e422..e669d18 100644 --- a/gold/link/nat.go +++ b/gold/link/nat.go @@ -2,6 +2,7 @@ package link import ( "encoding/json" + "reflect" "sync/atomic" "time" "unsafe" @@ -69,7 +70,7 @@ func (l *Link) onNotify(packet []byte) { if err == nil { p, ok := l.me.IsInPeer(peer) if ok { - if !p.endpoint.Euqal(addr) { + if reflect.ValueOf(p.endpoint).IsZero() || !p.endpoint.Euqal(addr) { p.endpoint = addr logrus.Infoln("[nat] notify set ep of peer", peer, "to", ep) } @@ -109,7 +110,7 @@ func (l *Link) onQuery(packet []byte) { lnk, ok := l.me.IsInPeer(p) eps := "" if l.me.ep.Network() == "udp" { // udp has real p2p - if lnk.endpoint == nil { + if reflect.ValueOf(lnk.endpoint).IsZero() { continue } eps = lnk.endpoint.String() @@ -120,7 +121,7 @@ func (l *Link) onQuery(packet []byte) { if eps == "" { continue } - if ok && lnk.endpoint != nil { + if ok && !reflect.ValueOf(lnk.endpoint).IsZero() { notify[p] = [2]string{ lnk.endpoint.Network(), eps, diff --git a/gold/link/send.go b/gold/link/send.go index d1a411f..8ffac94 100644 --- a/gold/link/send.go +++ b/gold/link/send.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "math/rand" + "reflect" "github.com/klauspost/compress/zstd" "github.com/sirupsen/logrus" @@ -123,7 +124,7 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz ui // write 向 peer 发一个包 func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool, seq uint32) (int, error) { peerep := l.endpoint - if peerep == nil { + if reflect.ValueOf(peerep).IsZero() { return 0, errors.New("nil endpoint of " + p.Dst.String()) }