1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-05 07:50:24 +08:00

fix(nat): panic on nil endpoint

This commit is contained in:
源文雨
2025-02-22 20:45:27 +09:00
parent 85a90aeb86
commit 60495227fc
5 changed files with 16 additions and 11 deletions

View File

@@ -5,7 +5,6 @@ import (
"errors" "errors"
"io" "io"
"net" "net"
"reflect"
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
@@ -174,7 +173,7 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
packet.Put() packet.Put()
return return
} }
if reflect.ValueOf(p.endpoint).IsZero() || !p.endpoint.Euqal(addr) { if helper.IsNilInterface(p.endpoint) || !p.endpoint.Euqal(addr) {
if m.ep.Network() == "tcp" && !addr.Euqal(p.endpoint) { if m.ep.Network() == "tcp" && !addr.Euqal(p.endpoint) {
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String()) logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr p.endpoint = addr

View File

@@ -5,7 +5,6 @@ import (
"encoding/hex" "encoding/hex"
"io" "io"
"net" "net"
"reflect"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@@ -136,7 +135,7 @@ func NewMe(cfg *MyConfig) (m Me) {
func (m *Me) Restart() error { func (m *Me) Restart() error {
oldconn := m.conn oldconn := m.conn
m.conn = nil m.conn = nil
if !reflect.ValueOf(oldconn).IsZero() { if helper.IsNonNilInterface(oldconn) {
_ = oldconn.Close() _ = oldconn.Close()
} }
var err error var err error
@@ -177,7 +176,7 @@ func (m *Me) EndPoint() p2p.EndPoint {
func (m *Me) Close() error { func (m *Me) Close() error {
m.connections = nil m.connections = nil
if !reflect.ValueOf(m.conn).IsZero() { if helper.IsNonNilInterface(m.conn) {
_ = m.conn.Close() _ = m.conn.Close()
m.conn = nil m.conn = nil
} }

View File

@@ -2,7 +2,6 @@ package link
import ( import (
"encoding/json" "encoding/json"
"reflect"
"sync/atomic" "sync/atomic"
"time" "time"
"unsafe" "unsafe"
@@ -70,7 +69,7 @@ func (l *Link) onNotify(packet []byte) {
if err == nil { if err == nil {
p, ok := l.me.IsInPeer(peer) p, ok := l.me.IsInPeer(peer)
if ok { if ok {
if reflect.ValueOf(p.endpoint).IsZero() || !p.endpoint.Euqal(addr) { if helper.IsNilInterface(p.endpoint) || !p.endpoint.Euqal(addr) {
p.endpoint = addr p.endpoint = addr
logrus.Infoln("[nat] notify set ep of peer", peer, "to", ep) logrus.Infoln("[nat] notify set ep of peer", peer, "to", ep)
} }
@@ -110,7 +109,7 @@ func (l *Link) onQuery(packet []byte) {
lnk, ok := l.me.IsInPeer(p) lnk, ok := l.me.IsInPeer(p)
eps := "" eps := ""
if l.me.ep.Network() == "udp" { // udp has real p2p if l.me.ep.Network() == "udp" { // udp has real p2p
if reflect.ValueOf(lnk.endpoint).IsZero() { if helper.IsNilInterface(lnk.endpoint) {
continue continue
} }
eps = lnk.endpoint.String() eps = lnk.endpoint.String()
@@ -121,7 +120,7 @@ func (l *Link) onQuery(packet []byte) {
if eps == "" { if eps == "" {
continue continue
} }
if ok && !reflect.ValueOf(lnk.endpoint).IsZero() { if ok && helper.IsNonNilInterface(lnk.endpoint) {
notify[p] = [2]string{ notify[p] = [2]string{
lnk.endpoint.Network(), lnk.endpoint.Network(),
eps, eps,

View File

@@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
"reflect"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -124,7 +123,7 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz ui
// write 向 peer 发一个包 // write 向 peer 发一个包
func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool, seq uint32) (int, error) { 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 peerep := l.endpoint
if reflect.ValueOf(peerep).IsZero() { if helper.IsNilInterface(peerep) {
return 0, errors.New("nil endpoint of " + p.Dst.String()) return 0, errors.New("nil endpoint of " + p.Dst.String())
} }

View File

@@ -1,6 +1,7 @@
package helper package helper
import ( import (
"reflect"
"unsafe" "unsafe"
) )
@@ -30,3 +31,11 @@ func StringToBytes(s string) (b []byte) {
bh.cap = sh.len bh.cap = sh.len
return b return b
} }
func IsNilInterface(x any) bool {
return x == nil || reflect.ValueOf(x).IsZero()
}
func IsNonNilInterface(x any) bool {
return !IsNilInterface(x)
}