mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-17 07:40:24 +08:00
fix: multi-segment hash checking
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
package link
|
||||
|
||||
import (
|
||||
"github.com/fumiama/orbyte/pbuf"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/fumiama/orbyte/pbuf"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/internal/algo"
|
||||
"github.com/fumiama/WireGold/internal/file"
|
||||
)
|
||||
|
||||
func (l *Link) randkeyidx() uint8 {
|
||||
@@ -19,11 +24,20 @@ func (l *Link) decode(teatype uint8, additional uint16, b []byte) (db pbuf.Bytes
|
||||
return
|
||||
}
|
||||
if l.keys[0] == nil {
|
||||
if config.ShowDebugLog {
|
||||
n := len(b)
|
||||
endl := "."
|
||||
if n > 64 {
|
||||
n = 64
|
||||
endl = "..."
|
||||
}
|
||||
logrus.Debugln(file.Header(), "copy plain text", hex.EncodeToString(b[:n]), endl)
|
||||
}
|
||||
return pbuf.ParseBytes(b...).Copy(), nil
|
||||
}
|
||||
aead := l.keys[teatype]
|
||||
if aead == nil {
|
||||
return
|
||||
panic("unexpected empty aead")
|
||||
}
|
||||
return algo.DecodeAEAD(aead, additional, b)
|
||||
}
|
||||
|
||||
@@ -69,7 +69,17 @@ func (m *Me) waitordispatch(addr p2p.EndPoint, buf pbuf.Bytes, n int) {
|
||||
recvlooptime := atomic.LoadInt64(&m.recvlooptime)
|
||||
if recvloopcnt%uintptr(m.speedloop) == 0 {
|
||||
now := time.Now().UnixMilli()
|
||||
logrus.Infof("[listen] queue recv avg speed: %.2f KB/s", float64(recvtotlcnt)/float64(now-recvlooptime))
|
||||
kb := float64(recvtotlcnt) / float64(now-recvlooptime)
|
||||
if kb < 1024 {
|
||||
logrus.Infof("[listen] queue recv avg speed: %.2f KB/s", kb)
|
||||
} else {
|
||||
kb /= 1024
|
||||
if kb < 1024 {
|
||||
logrus.Infof("[listen] queue recv avg speed: %.2f MB/s", kb)
|
||||
} else {
|
||||
logrus.Infof("[listen] queue recv avg speed: %.2f GB/s", kb/1024)
|
||||
}
|
||||
}
|
||||
atomic.StoreUint64(&m.recvtotlcnt, 0)
|
||||
atomic.StoreInt64(&m.recvlooptime, now)
|
||||
}
|
||||
@@ -85,6 +95,9 @@ func (m *Me) waitordispatch(addr p2p.EndPoint, buf pbuf.Bytes, n int) {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] dispatch", len(b), "bytes packet")
|
||||
}
|
||||
if !p.HasFinished() {
|
||||
panic("unexpected unfinished")
|
||||
}
|
||||
m.dispatch(p, b, addr)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -49,7 +49,7 @@ type Me struct {
|
||||
// 本机未接收完全分片池
|
||||
recving *ttl.Cache[uint16, head.PacketBytes]
|
||||
// 抗重放攻击记录池
|
||||
recved *ttl.Cache[uint32, struct{}]
|
||||
recved *ttl.Cache[uint64, struct{}]
|
||||
// 本机上层配置
|
||||
srcport, dstport, mtu, speedloop uint16
|
||||
// 报头掩码
|
||||
@@ -143,7 +143,7 @@ func NewMe(cfg *MyConfig) (m Me) {
|
||||
binary.BigEndian.PutUint64(buf[:], m.mask)
|
||||
logrus.Infoln("[me] xor mask", hex.EncodeToString(buf[:]))
|
||||
m.recving = ttl.NewCache[uint16, head.PacketBytes](time.Second * 10)
|
||||
m.recved = ttl.NewCache[uint32, struct{}](time.Minute)
|
||||
m.recved = ttl.NewCache[uint64, struct{}](time.Minute)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -78,9 +78,13 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugf("[recv] packet seq %08x", seq)
|
||||
}
|
||||
if _, got := m.recved.GetOrSet(seq, struct{}{}); got {
|
||||
crc := uint64(0)
|
||||
header.B(func(_ []byte, p *head.Packet) {
|
||||
crc = p.CRC64()
|
||||
})
|
||||
if _, got := m.recved.GetOrSet(uint64(seq)^crc, struct{}{}); got {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[recv] ignore duplicated seq packet", strconv.FormatUint(uint64(seq), 16))
|
||||
logrus.Debugln("[recv] ignore duplicated seq^crc packet, seq", strconv.FormatUint(uint64(seq), 16), "crc", strconv.FormatUint(crc, 16))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -145,8 +149,9 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "get frag part isnew:", !got)
|
||||
}
|
||||
ok := false
|
||||
h.B(func(buf []byte, p *head.Packet) {
|
||||
ok := p.WriteDataSegment(data, buf)
|
||||
ok = p.WriteDataSegment(data, buf)
|
||||
if !ok {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "wait other frag parts isnew:", !got)
|
||||
@@ -158,5 +163,8 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) {
|
||||
logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "all parts has reached")
|
||||
}
|
||||
})
|
||||
if !ok {
|
||||
return head.PacketBytes{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
|
||||
@@ -40,7 +41,7 @@ func (l *Link) WritePacket(proto uint8, data []byte, ttl uint8) {
|
||||
mtu -= uint16(rand.Intn(int(l.mturandomrange)))
|
||||
}
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] write mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype)
|
||||
logrus.Debugln("[send] write mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype, ", data len:", len(data))
|
||||
}
|
||||
pb := head.NewPacketBuilder().
|
||||
Src(l.me.me, l.me.srcport).Dst(l.peerip, l.me.dstport).
|
||||
@@ -55,7 +56,11 @@ func (l *Link) WritePacket(proto uint8, data []byte, ttl uint8) {
|
||||
} else {
|
||||
pktb = pb.Seal(l.keys[teatype], teatype, sndcnt&0x07ff)
|
||||
}
|
||||
for _, b := range pktb.Split(int(mtu), false) { //TODO: impl. nofrag
|
||||
bs := pktb.Split(int(mtu), false)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] split packet into", len(bs), "parts")
|
||||
}
|
||||
for _, b := range bs { //TODO: impl. nofrag
|
||||
go l.write2peer(head.BuildPacketFromBytes(b), randseq(sndcnt))
|
||||
}
|
||||
}
|
||||
@@ -99,7 +104,7 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) {
|
||||
bound = len(data)
|
||||
endl = "."
|
||||
}
|
||||
logrus.Debugln("[send] raw data bytes", hex.EncodeToString(data[:bound]), endl)
|
||||
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "raw data bytes", hex.EncodeToString(data[:bound]), endl)
|
||||
}
|
||||
b = l.me.xorenc(data, seq)
|
||||
if config.ShowDebugLog {
|
||||
@@ -110,7 +115,7 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) {
|
||||
endl = "."
|
||||
}
|
||||
b.V(func(b []byte) {
|
||||
logrus.Debugln("[send] xored data bytes", hex.EncodeToString(b[:bound]), endl)
|
||||
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "xored data bytes", hex.EncodeToString(b[:bound]), endl)
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -125,14 +130,14 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) {
|
||||
endl = "."
|
||||
}
|
||||
b.V(func(b []byte) {
|
||||
logrus.Debugln("[send] xored data bytes", hex.EncodeToString(b[:bound]), endl)
|
||||
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "b14ed data bytes", hex.EncodeToString(b[:bound]), endl)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
b.V(func(b []byte) {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] write", len(b), "bytes data from ep", conn.LocalAddr(), "to", peerep)
|
||||
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "write", len(b), "bytes data from ep", conn.LocalAddr(), "to", peerep)
|
||||
}
|
||||
_, err = conn.WriteToPeer(b, peerep)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user