1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-29 07:10:25 +08:00

feat: drop json in packet

This commit is contained in:
fumiama
2021-12-30 15:19:37 +08:00
parent 4a17d7cf13
commit f91507fe6a
8 changed files with 78 additions and 26 deletions

View File

@@ -75,9 +75,11 @@ func (l *Link) Read() *head.Packet {
func (l *Link) Write(p *head.Packet) (n int, err error) {
p.FillHash()
p.Data = l.Encode(p.Data)
var d []byte
d, err = p.Marshal(l.me.me.String(), l.peerip.String())
logrus.Debugln("[link] write data", string(d))
d := p.Marshal(l.me.me)
if d == nil {
return 0, errors.New("ttl exceeded")
}
logrus.Debugln("[link] write", len(d), "bytes data")
if err == nil {
peerlink := l.me.router.NextHop(l.peerip.String() + "/32")
if peerlink != nil {

View File

@@ -2,6 +2,7 @@ package link
import (
"net"
"strconv"
"github.com/sirupsen/logrus"
@@ -29,23 +30,23 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
packet.Data = append(packet.Data, remain...)
}
}
p, ok := m.IsInPeer(packet.Src)
p, ok := m.IsInPeer(packet.Src.String())
logrus.Infoln("[link] recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
logrus.Debugln("[link] recv:", string(lbf))
// logrus.Debugln("[link] recv:", hex.EncodeToString(lbf))
if ok {
if p.pep == "" || p.pep != addr.String() {
logrus.Infoln("[link] set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr
p.pep = addr.String()
}
if p.IsToMe(net.ParseIP(packet.Dst)) {
if p.IsToMe(packet.Dst) {
packet.Data = p.Decode(packet.Data)
if packet.IsVaildHash() {
switch packet.Proto {
case head.ProtoHello:
switch p.status {
case LINK_STATUS_DOWN:
_, _ = p.Write(head.NewPacket(head.ProtoHello, 0, 0, nil))
_, _ = p.Write(head.NewPacket(head.ProtoHello, 0, p.peerip, 0, nil))
logrus.Infoln("[link] send hello ack packet")
p.status = LINK_STATUS_HALFUP
case LINK_STATUS_HALFUP:
@@ -73,13 +74,13 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
} else {
logrus.Infoln("[link] drop invalid packet")
}
} else if p.Accept(net.ParseIP(packet.Dst)) && p.allowtrans {
} else if p.Accept(packet.Dst) && p.allowtrans {
// 转发
p.Write(&packet)
logrus.Infoln("[link] trans")
logrus.Infoln("[link] trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
}
} else {
logrus.Infoln("[link] packet to", packet.Dst, "is refused")
logrus.Warnln("[link] packet to", packet.Dst, "is refused")
}
}
}

View File

@@ -15,7 +15,7 @@ func (l *Link) keepAlive() {
go func() {
t := time.NewTicker(time.Second * time.Duration(l.keepalive))
for range t.C {
_, _ = l.Write(head.NewPacket(head.ProtoHello, 0, 0, nil))
_, _ = l.Write(head.NewPacket(head.ProtoHello, 0, l.peerip, 0, nil))
logrus.Infoln("[link.nat] send keep alive packet")
}
}()

View File

@@ -27,6 +27,6 @@ func (l *Link) SendQuery(peers ...string) error {
if err != nil {
return err
}
_, err = l.Write(head.NewPacket(head.ProtoQuery, 0, 0, data))
_, err = l.Write(head.NewPacket(head.ProtoQuery, 0, l.peerip, 0, data))
return err
}