1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-19 01:30:26 +08:00

fix(nat): panic on nil endpoint

This commit is contained in:
源文雨
2025-02-22 20:29:49 +09:00
parent a205d889ca
commit 85a90aeb86
3 changed files with 8 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors" "errors"
"io" "io"
"net" "net"
"reflect"
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
@@ -173,7 +174,7 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
packet.Put() packet.Put()
return 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) { 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

@@ -2,6 +2,7 @@ package link
import ( import (
"encoding/json" "encoding/json"
"reflect"
"sync/atomic" "sync/atomic"
"time" "time"
"unsafe" "unsafe"
@@ -69,7 +70,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 !p.endpoint.Euqal(addr) { if reflect.ValueOf(p.endpoint).IsZero() || !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)
} }
@@ -109,7 +110,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 lnk.endpoint == nil { if reflect.ValueOf(lnk.endpoint).IsZero() {
continue continue
} }
eps = lnk.endpoint.String() eps = lnk.endpoint.String()
@@ -120,7 +121,7 @@ func (l *Link) onQuery(packet []byte) {
if eps == "" { if eps == "" {
continue continue
} }
if ok && lnk.endpoint != nil { if ok && !reflect.ValueOf(lnk.endpoint).IsZero() {
notify[p] = [2]string{ notify[p] = [2]string{
lnk.endpoint.Network(), lnk.endpoint.Network(),
eps, eps,

View File

@@ -9,6 +9,7 @@ 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"
@@ -123,7 +124,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 peerep == nil { if reflect.ValueOf(peerep).IsZero() {
return 0, errors.New("nil endpoint of " + p.Dst.String()) return 0, errors.New("nil endpoint of " + p.Dst.String())
} }