mirror of
https://github.com/fumiama/WireGold.git
synced 2025-09-26 19:21:11 +08:00
optimize: memory consumption
This commit is contained in:
4
go.mod
4
go.mod
@@ -5,10 +5,10 @@ go 1.21
|
|||||||
require (
|
require (
|
||||||
github.com/FloatTech/ttl v0.0.0-20250224045156-012b1463287d
|
github.com/FloatTech/ttl v0.0.0-20250224045156-012b1463287d
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
||||||
github.com/fumiama/blake2b-simd v0.0.0-20220412110131-4481822068bb
|
github.com/fumiama/blake2b-simd v0.0.0-20250228045919-a5dcaba5419a
|
||||||
github.com/fumiama/go-base16384 v1.7.0
|
github.com/fumiama/go-base16384 v1.7.0
|
||||||
github.com/fumiama/go-x25519 v1.0.0
|
github.com/fumiama/go-x25519 v1.0.0
|
||||||
github.com/fumiama/orbyte v0.0.0-20250414141219-63dd01e81ea2
|
github.com/fumiama/orbyte v0.0.0-20250512155242-23a2b7120589
|
||||||
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac
|
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac
|
||||||
github.com/klauspost/compress v1.17.9
|
github.com/klauspost/compress v1.17.9
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
|
8
go.sum
8
go.sum
@@ -5,14 +5,14 @@ github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9o
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/fumiama/blake2b-simd v0.0.0-20220412110131-4481822068bb h1:Gd7gIiR68ErTWtxnl/PCycMog6IRdK1ApxrvoAy0lFY=
|
github.com/fumiama/blake2b-simd v0.0.0-20250228045919-a5dcaba5419a h1:hhCWoHNGDATjUitm8rKwrf5xRnuxO7P6UHIusLXuqag=
|
||||||
github.com/fumiama/blake2b-simd v0.0.0-20220412110131-4481822068bb/go.mod h1:Olmv2uLdFllRsvwhzOvG/O/Nvgzg0ViokUL4+hiaRSE=
|
github.com/fumiama/blake2b-simd v0.0.0-20250228045919-a5dcaba5419a/go.mod h1:Olmv2uLdFllRsvwhzOvG/O/Nvgzg0ViokUL4+hiaRSE=
|
||||||
github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMWSsXCA=
|
github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMWSsXCA=
|
||||||
github.com/fumiama/go-base16384 v1.7.0/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM=
|
github.com/fumiama/go-base16384 v1.7.0/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM=
|
||||||
github.com/fumiama/go-x25519 v1.0.0 h1:hiGg9EhseVmGCc8T1jECVkj8Keu/aJ1ZK05RM8Vuavo=
|
github.com/fumiama/go-x25519 v1.0.0 h1:hiGg9EhseVmGCc8T1jECVkj8Keu/aJ1ZK05RM8Vuavo=
|
||||||
github.com/fumiama/go-x25519 v1.0.0/go.mod h1:8VOhfyGZzw4IUs4nCjQFqW9cA3V/QpSCtP3fo2dLNg4=
|
github.com/fumiama/go-x25519 v1.0.0/go.mod h1:8VOhfyGZzw4IUs4nCjQFqW9cA3V/QpSCtP3fo2dLNg4=
|
||||||
github.com/fumiama/orbyte v0.0.0-20250414141219-63dd01e81ea2 h1:A0jvi8f+FHNtPJBKujPYshazTmujSE/trf5xkmrJvSQ=
|
github.com/fumiama/orbyte v0.0.0-20250512155242-23a2b7120589 h1:ifo33HcA8HFYWaTe4fsJjozTUaOBlA0dFa1BcTZDEHs=
|
||||||
github.com/fumiama/orbyte v0.0.0-20250414141219-63dd01e81ea2/go.mod h1:FOjdw7KdCbK2eH3gRPhwFNCoXKpu9sN5vPH4El/8e0c=
|
github.com/fumiama/orbyte v0.0.0-20250512155242-23a2b7120589/go.mod h1:FOjdw7KdCbK2eH3gRPhwFNCoXKpu9sN5vPH4El/8e0c=
|
||||||
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac h1:A/5A0rODsg+EQHH61Ew5mMUtDpRXaSNqHhPvW+fN4C4=
|
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac h1:A/5A0rODsg+EQHH61Ew5mMUtDpRXaSNqHhPvW+fN4C4=
|
||||||
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac/go.mod h1:BBnNY9PwK+UUn4trAU+H0qsMEypm7+3Bj1bVFuJItlo=
|
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac/go.mod h1:BBnNY9PwK+UUn4trAU+H0qsMEypm7+3Bj1bVFuJItlo=
|
||||||
github.com/fumiama/wintun v0.0.0-20211229152851-8bc97c8034c0 h1:WfrSFlIlCAtg6Rt2IGna0HhJYSDE45YVHiYqO4wwsEw=
|
github.com/fumiama/wintun v0.0.0-20211229152851-8bc97c8034c0 h1:WfrSFlIlCAtg6Rt2IGna0HhJYSDE45YVHiYqO4wwsEw=
|
||||||
|
@@ -59,13 +59,10 @@ func (p *Packet) WriteHeaderTo(buf *bytes.Buffer) {
|
|||||||
buf.Write((*[PacketHeadNoCRCLen]byte)(
|
buf.Write((*[PacketHeadNoCRCLen]byte)(
|
||||||
(unsafe.Pointer)(p),
|
(unsafe.Pointer)(p),
|
||||||
)[:])
|
)[:])
|
||||||
b := pbuf.NewBytes(buf.Len())
|
b := make([]byte, buf.Len())
|
||||||
b.V(func(b []byte) {
|
copy(b, buf.Bytes())
|
||||||
copy(b, buf.Bytes())
|
ClearTTL(b)
|
||||||
ClearTTL(b)
|
p.md5h8 = algo.MD5Hash8(b)
|
||||||
p.md5h8 = algo.MD5Hash8(b)
|
|
||||||
})
|
|
||||||
b.ManualDestroy()
|
|
||||||
_ = binary.Write(buf, binary.LittleEndian, p.md5h8)
|
_ = binary.Write(buf, binary.LittleEndian, p.md5h8)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -79,13 +76,10 @@ func (p *Packet) WriteHeaderTo(buf *bytes.Buffer) {
|
|||||||
w.Write(p.src[:])
|
w.Write(p.src[:])
|
||||||
w.Write(p.dst[:])
|
w.Write(p.dst[:])
|
||||||
w.P(func(buf *pbuf.Buffer) {
|
w.P(func(buf *pbuf.Buffer) {
|
||||||
b := pbuf.NewBytes(buf.Len())
|
b := make([]byte, buf.Len())
|
||||||
b.V(func(b []byte) {
|
copy(b, buf.Bytes())
|
||||||
copy(b, buf.Bytes())
|
ClearTTL(b)
|
||||||
ClearTTL(b)
|
p.md5h8 = algo.MD5Hash8(b)
|
||||||
p.md5h8 = algo.MD5Hash8(b)
|
|
||||||
})
|
|
||||||
b.ManualDestroy()
|
|
||||||
})
|
})
|
||||||
w.WriteUInt64(p.md5h8)
|
w.WriteUInt64(p.md5h8)
|
||||||
w.P(func(b *pbuf.Buffer) {
|
w.P(func(b *pbuf.Buffer) {
|
||||||
|
@@ -87,11 +87,10 @@ func (pb *DataBuilder) Zstd() *DataBuilder {
|
|||||||
return pb.p(func(ub *PacketBuf) {
|
return pb.p(func(ub *PacketBuf) {
|
||||||
data := algo.EncodeZstd(ub.Bytes())
|
data := algo.EncodeZstd(ub.Bytes())
|
||||||
ub.Reset()
|
ub.Reset()
|
||||||
data.V(func(b []byte) { ub.Write(b) })
|
ub.Write(data)
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln(file.Header(), strconv.FormatUint(ub.DAT.md5h8, 16), "data after zstd", file.ToLimitHexString(ub.Bytes(), 64))
|
logrus.Debugln(file.Header(), strconv.FormatUint(ub.DAT.md5h8, 16), "data after zstd", file.ToLimitHexString(ub.Bytes(), 64))
|
||||||
}
|
}
|
||||||
data.ManualDestroy()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,8 +124,7 @@ func (pb *DataBuilder) Seal(aead cipher.AEAD, teatyp uint8, additional uint16) *
|
|||||||
w.P(func(b *pbuf.Buffer) {
|
w.P(func(b *pbuf.Buffer) {
|
||||||
data := algo.EncodeAEAD(aead, additional, b.Bytes())
|
data := algo.EncodeAEAD(aead, additional, b.Bytes())
|
||||||
ub.Reset()
|
ub.Reset()
|
||||||
data.V(func(b []byte) { ub.Write(b) })
|
ub.Write(data)
|
||||||
data.ManualDestroy()
|
|
||||||
})
|
})
|
||||||
w.Destroy()
|
w.Destroy()
|
||||||
}))
|
}))
|
||||||
@@ -204,7 +202,7 @@ func (pb *PacketBuilder) Split(mtu int, nofrag bool) (pbs []PacketBytes) {
|
|||||||
pbs = []PacketBytes{
|
pbs = []PacketBytes{
|
||||||
pbuf.BufferItemToBytes((*PacketItem)(
|
pbuf.BufferItemToBytes((*PacketItem)(
|
||||||
pb.copy().noFrag(nofrag).hasMore(false).offset(0),
|
pb.copy().noFrag(nofrag).hasMore(false).offset(0),
|
||||||
)),
|
)).Ignore(),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -226,7 +224,7 @@ func (pb *PacketBuilder) Split(mtu int, nofrag bool) (pbs []PacketBytes) {
|
|||||||
}
|
}
|
||||||
pbs[i] = pbuf.BufferItemToBytes((*PacketItem)(
|
pbs[i] = pbuf.BufferItemToBytes((*PacketItem)(
|
||||||
pb.copy().offset(uint16(i*datalim)),
|
pb.copy().offset(uint16(i*datalim)),
|
||||||
)).Slice(a, b)
|
)).Ignore().Slice(a, b)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
@@ -12,6 +12,10 @@ import (
|
|||||||
var packetPool = pbuf.NewBufferPool[Packet]()
|
var packetPool = pbuf.NewBufferPool[Packet]()
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
packetPool.LimitInput(256)
|
||||||
|
packetPool.LimitOutput(256)
|
||||||
|
pbuf.LimitInput(256)
|
||||||
|
pbuf.LimitOutput(256)
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
go status()
|
go status()
|
||||||
}
|
}
|
||||||
@@ -23,7 +27,7 @@ func selectPacket(buf ...byte) *PacketItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func status() {
|
func status() {
|
||||||
for range time.NewTicker(time.Minute).C {
|
for range time.NewTicker(time.Second).C {
|
||||||
out, in := packetPool.CountItems()
|
out, in := packetPool.CountItems()
|
||||||
logrus.Infoln(file.Header(), "packet outside:", out, "inside:", in)
|
logrus.Infoln(file.Header(), "packet outside:", out, "inside:", in)
|
||||||
out, in = pbuf.CountItems()
|
out, in = pbuf.CountItems()
|
||||||
|
@@ -55,13 +55,10 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var crc uint64
|
var crc uint64
|
||||||
b := pbuf.NewBytes(int(PacketHeadNoCRCLen))
|
var b [PacketHeadNoCRCLen]byte
|
||||||
b.V(func(b []byte) {
|
copy(b[:], data[:PacketHeadNoCRCLen])
|
||||||
copy(b, data[:PacketHeadNoCRCLen])
|
ClearTTL(b[:])
|
||||||
ClearTTL(b)
|
crc = algo.MD5Hash8(b[:])
|
||||||
crc = algo.MD5Hash8(b)
|
|
||||||
})
|
|
||||||
b.ManualDestroy()
|
|
||||||
if crc != pb.DAT.md5h8 {
|
if crc != pb.DAT.md5h8 {
|
||||||
err = ErrBadCRCChecksum
|
err = ErrBadCRCChecksum
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
@@ -85,7 +82,7 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) {
|
|||||||
p.ManualDestroy()
|
p.ManualDestroy()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pbytes = pbuf.BufferItemToBytes(p)
|
pbytes = pbuf.BufferItemToBytes(p).Ignore()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,7 +3,6 @@ package link
|
|||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/config"
|
"github.com/fumiama/WireGold/config"
|
||||||
@@ -19,7 +18,7 @@ func (l *Link) randkeyidx() uint8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// decode by aead and put b into pool
|
// decode by aead and put b into pool
|
||||||
func (l *Link) decode(teatype uint8, additional uint16, b []byte) (db pbuf.Bytes, err error) {
|
func (l *Link) decode(teatype uint8, additional uint16, b []byte) (db []byte, err error) {
|
||||||
if len(b) == 0 || teatype >= 32 {
|
if len(b) == 0 || teatype >= 32 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -33,7 +32,9 @@ func (l *Link) decode(teatype uint8, additional uint16, b []byte) (db pbuf.Bytes
|
|||||||
}
|
}
|
||||||
logrus.Debugln(file.Header(), "copy plain text", hex.EncodeToString(b[:n]), endl)
|
logrus.Debugln(file.Header(), "copy plain text", hex.EncodeToString(b[:n]), endl)
|
||||||
}
|
}
|
||||||
return pbuf.ParseBytes(b...).Copy(), nil
|
db = make([]byte, len(b))
|
||||||
|
copy(db, b)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
aead := l.keys[teatype]
|
aead := l.keys[teatype]
|
||||||
if aead == nil {
|
if aead == nil {
|
||||||
@@ -43,7 +44,7 @@ func (l *Link) decode(teatype uint8, additional uint16, b []byte) (db pbuf.Bytes
|
|||||||
}
|
}
|
||||||
|
|
||||||
// xorenc 按 8 字节, 以初始 m.mask 循环异或编码 data
|
// xorenc 按 8 字节, 以初始 m.mask 循环异或编码 data
|
||||||
func (m *Me) xorenc(data []byte, seq uint32) pbuf.Bytes {
|
func (m *Me) xorenc(data []byte, seq uint32) []byte {
|
||||||
return algo.EncodeXOR(data, m.mask, seq)
|
return algo.EncodeXOR(data, m.mask, seq)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,13 +3,12 @@ package link
|
|||||||
import (
|
import (
|
||||||
"github.com/RomiChan/syncx"
|
"github.com/RomiChan/syncx"
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 事件分发器
|
// 事件分发器
|
||||||
var dispachers syncx.Map[uint8, Dispacher]
|
var dispachers syncx.Map[uint8, Dispacher]
|
||||||
|
|
||||||
type Dispacher func(header *head.Packet, peer *Link, data pbuf.Bytes)
|
type Dispacher func(header *head.Packet, peer *Link, data []byte)
|
||||||
|
|
||||||
// RegisterDispacher of proto
|
// RegisterDispacher of proto
|
||||||
func RegisterDispacher(p uint8, d Dispacher) (actual Dispacher, hasexist bool) {
|
func RegisterDispacher(p uint8, d Dispacher) (actual Dispacher, hasexist bool) {
|
||||||
|
@@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/fumiama/WireGold/gold/p2p"
|
"github.com/fumiama/WireGold/gold/p2p"
|
||||||
"github.com/fumiama/WireGold/internal/bin"
|
"github.com/fumiama/WireGold/internal/bin"
|
||||||
base14 "github.com/fumiama/go-base16384"
|
base14 "github.com/fumiama/go-base16384"
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -70,25 +69,24 @@ func (m *Me) Connect(peer string) (*Link, error) {
|
|||||||
return nil, ErrPerrNotExist
|
return nil, ErrPerrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Link) ToLower(header *head.Packet, data pbuf.Bytes) {
|
func (l *Link) ToLower(header *head.Packet, data []byte) {
|
||||||
if l.pipe != nil {
|
if l.pipe != nil {
|
||||||
|
d := make([]byte, len(data))
|
||||||
|
copy(d, data)
|
||||||
l.pipe <- LinkData{
|
l.pipe <- LinkData{
|
||||||
H: *header,
|
H: *header,
|
||||||
D: data.Copy().Trans(),
|
D: d,
|
||||||
}
|
}
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] deliver to pipe of", l.peerip)
|
logrus.Debugln("[listen] deliver to pipe of", l.peerip)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var err error
|
_, err := l.me.nic.Write(data)
|
||||||
data.V(func(b []byte) {
|
|
||||||
_, err = l.me.nic.Write(b)
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("[listen] deliver", data.Len(), "bytes data to nic err:", err)
|
logrus.Errorln("[listen] deliver", len(data), "bytes data to nic err:", err)
|
||||||
} else if config.ShowDebugLog {
|
} else if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] deliver", data.Len(), "bytes data to nic")
|
logrus.Debugln("[listen] deliver", len(data), "bytes data to nic")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,11 +32,17 @@ func (m *Me) runworkers() {
|
|||||||
m.jobs = make([]chan job, ncpu)
|
m.jobs = make([]chan job, ncpu)
|
||||||
for i := 0; i < ncpu; i++ {
|
for i := 0; i < ncpu; i++ {
|
||||||
m.jobs[i] = make(chan job, 4096)
|
m.jobs[i] = make(chan job, 4096)
|
||||||
go func(jobs <-chan job) {
|
go func(i int, jobs <-chan job) {
|
||||||
for jb := range jobs {
|
for jb := range jobs {
|
||||||
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] job thread", i, "call waitordispatch")
|
||||||
|
}
|
||||||
m.waitordispatch(jb.addr, jb.buf, jb.n, jb.fil)
|
m.waitordispatch(jb.addr, jb.buf, jb.n, jb.fil)
|
||||||
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] job thread", i, "fin waitordispatch")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}(m.jobs[i])
|
}(i, m.jobs[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,9 +79,9 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
|||||||
fil *uintptr
|
fil *uintptr
|
||||||
)
|
)
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
lbf = pbuf.NewBytes(lstnbufgragsz)
|
lbf = pbuf.NewLargeBytes(lstnbufgragsz)
|
||||||
} else {
|
} else {
|
||||||
lbf = pbuf.ParseBytes(bufs[idx*lstnbufgragsz : (idx+1)*lstnbufgragsz]...)
|
lbf = pbuf.ParseBytes(bufs[idx*lstnbufgragsz : (idx+1)*lstnbufgragsz : (idx+1)*lstnbufgragsz]...).Ignore()
|
||||||
fil = &fils[idx]
|
fil = &fils[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,29 +200,23 @@ func (m *Me) dispatch(header *head.Packet, body []byte, addr p2p.EndPoint) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if data.Len() < 8 {
|
if len(data) < 8 {
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] drop invalid data len packet key idx:", header.CipherIndex(), "addt:", addt, "len", data.Len())
|
logrus.Debugln("[listen] drop invalid data len packet key idx:", header.CipherIndex(), "addt:", addt, "len", len(data))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ok := false
|
ok := false
|
||||||
data.V(func(b []byte) {
|
ok = algo.IsVaildBlake2bHash8(header.PreCRC64(), data)
|
||||||
ok = algo.IsVaildBlake2bHash8(header.PreCRC64(), b)
|
|
||||||
})
|
|
||||||
if !ok {
|
if !ok {
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] drop invalid hash packet")
|
logrus.Debugln("[listen] drop invalid hash packet")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data = data.SliceFrom(8)
|
data = data[8:]
|
||||||
if p.usezstd {
|
if p.usezstd {
|
||||||
data.V(func(b []byte) {
|
data, err = algo.DecodeZstd(data) // skip hash
|
||||||
old := data
|
|
||||||
data, err = algo.DecodeZstd(b) // skip hash
|
|
||||||
old.ManualDestroy()
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] drop invalid zstd packet:", err)
|
logrus.Debugln("[listen] drop invalid zstd packet:", err)
|
||||||
@@ -224,7 +224,7 @@ func (m *Me) dispatch(header *head.Packet, body []byte, addr p2p.EndPoint) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] zstd decoded len:", data.Len())
|
logrus.Debugln("[listen] zstd decoded len:", len(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn, ok := GetDispacher(header.Proto.Proto())
|
fn, ok := GetDispacher(header.Proto.Proto())
|
||||||
@@ -233,5 +233,4 @@ func (m *Me) dispatch(header *head.Packet, body []byte, addr p2p.EndPoint) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fn(header, p, data)
|
fn(header, p, data)
|
||||||
data.ManualDestroy()
|
|
||||||
}
|
}
|
||||||
|
@@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FloatTech/ttl"
|
"github.com/FloatTech/ttl"
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
"github.com/fumiama/water/waterutil"
|
"github.com/fumiama/water/waterutil"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@@ -316,15 +315,6 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
|||||||
logrus.Warnln("[me] drop packet to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), ": nil nexthop")
|
logrus.Warnln("[me] drop packet to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), ": nil nexthop")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pcp := pbuf.NewBytes(len(packet))
|
lnk.WritePacket(head.ProtoData, packet, lnk.me.ttl)
|
||||||
pcp.V(func(b []byte) {
|
|
||||||
copy(b, packet)
|
|
||||||
})
|
|
||||||
go func() {
|
|
||||||
pcp.V(func(b []byte) {
|
|
||||||
lnk.WritePacket(head.ProtoData, b, lnk.me.ttl)
|
|
||||||
})
|
|
||||||
pcp.ManualDestroy()
|
|
||||||
}()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -123,7 +123,7 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) {
|
|||||||
logrus.Warnln("[recv] transfer drop packet: zero ttl")
|
logrus.Warnln("[recv] transfer drop packet: zero ttl")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go lnk.write2peer(pbuf.ParseBytes(data...).Copy(), seq)
|
go lnk.write2peer(pbuf.ParseBytes(data...).Ignore().Copy(), seq)
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] trans", len(data), "bytes packet to", p.Dst().String()+":"+strconv.Itoa(int(p.DstPort)))
|
logrus.Debugln("[listen] trans", len(data), "bytes packet to", p.Dst().String()+":"+strconv.Itoa(int(p.DstPort)))
|
||||||
}
|
}
|
||||||
|
@@ -110,24 +110,24 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) {
|
|||||||
}
|
}
|
||||||
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "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)
|
b = pbuf.ParseBytes(l.me.xorenc(data, seq)...).Ignore()
|
||||||
isnewb = true
|
isnewb = true
|
||||||
if config.ShowDebugLog {
|
|
||||||
bound := 64
|
|
||||||
endl := "..."
|
|
||||||
if b.Len() < bound {
|
|
||||||
bound = b.Len()
|
|
||||||
endl = "."
|
|
||||||
}
|
|
||||||
b.V(func(b []byte) {
|
|
||||||
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "xored data bytes", hex.EncodeToString(b[:bound]), endl)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
if config.ShowDebugLog {
|
||||||
|
bound := 64
|
||||||
|
endl := "..."
|
||||||
|
if b.Len() < bound {
|
||||||
|
bound = b.Len()
|
||||||
|
endl = "."
|
||||||
|
}
|
||||||
|
b.V(func(b []byte) {
|
||||||
|
logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "xored data bytes", hex.EncodeToString(b[:bound]), endl)
|
||||||
|
})
|
||||||
|
}
|
||||||
if l.me.base14 {
|
if l.me.base14 {
|
||||||
b.V(func(data []byte) {
|
b.V(func(data []byte) {
|
||||||
old := b
|
old := b
|
||||||
b = pbuf.ParseBytes(base14.Encode(data)...)
|
b = pbuf.ParseBytes(base14.Encode(data)...).Ignore()
|
||||||
if isnewb {
|
if isnewb {
|
||||||
old.ManualDestroy()
|
old.ManualDestroy()
|
||||||
}
|
}
|
||||||
|
@@ -1,14 +1,12 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/fumiama/WireGold/gold/link"
|
"github.com/fumiama/WireGold/gold/link"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
link.RegisterDispacher(head.ProtoData, func(header *head.Packet, peer *link.Link, data pbuf.Bytes) {
|
link.RegisterDispacher(head.ProtoData, func(header *head.Packet, peer *link.Link, data []byte) {
|
||||||
peer.ToLower(header, data)
|
peer.ToLower(header, data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package hello
|
package hello
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
@@ -10,17 +9,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
link.RegisterDispacher(head.ProtoHello, func(_ *head.Packet, peer *link.Link, data pbuf.Bytes) {
|
link.RegisterDispacher(head.ProtoHello, func(_ *head.Packet, peer *link.Link, data []byte) {
|
||||||
data.V(func(b []byte) {
|
switch {
|
||||||
switch {
|
case len(data) == 0:
|
||||||
case len(b) == 0:
|
logrus.Warnln(file.Header(), "recv old packet, do nothing")
|
||||||
logrus.Warnln(file.Header(), "recv old packet, do nothing")
|
case data[0] == byte(head.HelloPing):
|
||||||
case b[0] == byte(head.HelloPing):
|
go peer.WritePacket(head.ProtoHello, []byte{byte(head.HelloPong)}, peer.Me().TTL())
|
||||||
go peer.WritePacket(head.ProtoHello, []byte{byte(head.HelloPong)}, peer.Me().TTL())
|
logrus.Infoln(file.Header(), "recv, send ack")
|
||||||
logrus.Infoln(file.Header(), "recv, send ack")
|
default:
|
||||||
default:
|
logrus.Infoln(file.Header(), "recv ack, do nothing")
|
||||||
logrus.Infoln(file.Header(), "recv ack, do nothing")
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -17,97 +17,93 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// 收到通告包的处理
|
// 收到通告包的处理
|
||||||
link.RegisterDispacher(head.ProtoNotify, func(_ *head.Packet, peer *link.Link, data pbuf.Bytes) {
|
link.RegisterDispacher(head.ProtoNotify, func(_ *head.Packet, peer *link.Link, data []byte) {
|
||||||
data.V(func(b []byte) {
|
// 1. Data 解包
|
||||||
// 1. Data 解包
|
// ---- 使用 head.Notify 解释 packet
|
||||||
// ---- 使用 head.Notify 解释 packet
|
notify := make(head.Notify, 32)
|
||||||
notify := make(head.Notify, 32)
|
err := json.Unmarshal(data, ¬ify)
|
||||||
err := json.Unmarshal(b, ¬ify)
|
if err != nil {
|
||||||
if err != nil {
|
logrus.Errorln(file.Header(), "notify json unmarshal err:", err)
|
||||||
logrus.Errorln(file.Header(), "notify json unmarshal err:", err)
|
return
|
||||||
return
|
}
|
||||||
|
// 2. endpoint注册
|
||||||
|
// ---- 遍历 Notify,注册对方的 endpoint 到
|
||||||
|
// ---- connections,注意使用读写锁connmapmu
|
||||||
|
for ps, ep := range notify {
|
||||||
|
nw, epstr := ep[0], ep[1]
|
||||||
|
if nw != peer.Me().EndPoint().Network() {
|
||||||
|
logrus.Warnln(file.Header(), "ignore different network notify", nw, "addr", epstr)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
// 2. endpoint注册
|
addr, err := p2p.NewEndPoint(nw, epstr, peer.Me().NetworkConfigs()...)
|
||||||
// ---- 遍历 Notify,注册对方的 endpoint 到
|
if err == nil {
|
||||||
// ---- connections,注意使用读写锁connmapmu
|
p, ok := peer.Me().IsInPeer(ps)
|
||||||
for ps, ep := range notify {
|
if ok {
|
||||||
nw, epstr := ep[0], ep[1]
|
if bin.IsNilInterface(p.EndPoint()) || !p.EndPoint().Euqal(addr) {
|
||||||
if nw != peer.Me().EndPoint().Network() {
|
p.SetEndPoint(addr)
|
||||||
logrus.Warnln(file.Header(), "ignore different network notify", nw, "addr", epstr)
|
logrus.Infoln(file.Header(), "notify set ep of peer", ps, "to", ep)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addr, err := p2p.NewEndPoint(nw, epstr, peer.Me().NetworkConfigs()...)
|
|
||||||
if err == nil {
|
|
||||||
p, ok := peer.Me().IsInPeer(ps)
|
|
||||||
if ok {
|
|
||||||
if bin.IsNilInterface(p.EndPoint()) || !p.EndPoint().Euqal(addr) {
|
|
||||||
p.SetEndPoint(addr)
|
|
||||||
logrus.Infoln(file.Header(), "notify set ep of peer", ps, "to", ep)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if config.ShowDebugLog {
|
|
||||||
logrus.Debugln(file.Header(), "notify drop invalid peer:", ps, "ep:", ep)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln(file.Header(), "notify drop invalid peer:", ps, "ep:", ep)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
// 收到询问包的处理
|
// 收到询问包的处理
|
||||||
link.RegisterDispacher(head.ProtoQuery, func(_ *head.Packet, peer *link.Link, data pbuf.Bytes) {
|
link.RegisterDispacher(head.ProtoQuery, func(_ *head.Packet, peer *link.Link, data []byte) {
|
||||||
data.V(func(b []byte) {
|
// 完成data解包与notify分发
|
||||||
// 完成data解包与notify分发
|
|
||||||
|
|
||||||
// 1. Data 解包
|
// 1. Data 解包
|
||||||
// ---- 使用 head.Query 解释 packet
|
// ---- 使用 head.Query 解释 packet
|
||||||
// ---- 根据 Query 确定需要封装的 Notify
|
// ---- 根据 Query 确定需要封装的 Notify
|
||||||
var peers head.Query
|
var peers head.Query
|
||||||
err := json.Unmarshal(b, &peers)
|
err := json.Unmarshal(data, &peers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln(file.Header(), "query json unmarshal err:", err)
|
logrus.Errorln(file.Header(), "query json unmarshal err:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if peer == nil || peer.Me() == nil {
|
if peer == nil || peer.Me() == nil {
|
||||||
logrus.Errorln(file.Header(), "nil link/me")
|
logrus.Errorln(file.Header(), "nil link/me")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. notify分发
|
// 2. notify分发
|
||||||
// ---- 封装 Notify 到 新的 packet
|
// ---- 封装 Notify 到 新的 packet
|
||||||
// ---- 发送到对方
|
// ---- 发送到对方
|
||||||
notify := make(head.Notify, len(peers))
|
notify := make(head.Notify, len(peers))
|
||||||
for _, p := range peers {
|
for _, p := range peers {
|
||||||
lnk, ok := peer.Me().IsInPeer(p)
|
lnk, ok := peer.Me().IsInPeer(p)
|
||||||
eps := ""
|
eps := ""
|
||||||
if peer.Me().EndPoint().Network() == "udp" { // udp has real p2p
|
if peer.Me().EndPoint().Network() == "udp" { // udp has real p2p
|
||||||
if bin.IsNilInterface(lnk.EndPoint()) {
|
if bin.IsNilInterface(lnk.EndPoint()) {
|
||||||
continue
|
|
||||||
}
|
|
||||||
eps = lnk.EndPoint().String()
|
|
||||||
}
|
|
||||||
if eps == "" {
|
|
||||||
eps = peer.RawEndPoint() // use registered ep only
|
|
||||||
}
|
|
||||||
if eps == "" {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if ok && bin.IsNonNilInterface(lnk.EndPoint()) {
|
eps = lnk.EndPoint().String()
|
||||||
notify[p] = [2]string{
|
}
|
||||||
lnk.EndPoint().Network(),
|
if eps == "" {
|
||||||
eps,
|
eps = peer.RawEndPoint() // use registered ep only
|
||||||
}
|
}
|
||||||
|
if eps == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ok && bin.IsNonNilInterface(lnk.EndPoint()) {
|
||||||
|
notify[p] = [2]string{
|
||||||
|
lnk.EndPoint().Network(),
|
||||||
|
eps,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(notify) > 0 {
|
}
|
||||||
logrus.Infoln(file.Header(), "query wrap", len(notify), "notify")
|
if len(notify) > 0 {
|
||||||
w := bin.SelectWriter()
|
logrus.Infoln(file.Header(), "query wrap", len(notify), "notify")
|
||||||
_ = json.NewEncoder(w).Encode(¬ify)
|
w := bin.SelectWriter()
|
||||||
w.P(func(b *pbuf.Buffer) {
|
_ = json.NewEncoder(w).Encode(¬ify)
|
||||||
peer.WritePacket(head.ProtoNotify, b.Bytes(), peer.Me().TTL())
|
w.P(func(b *pbuf.Buffer) {
|
||||||
})
|
peer.WritePacket(head.ProtoNotify, b.Bytes(), peer.Me().TTL())
|
||||||
w.Destroy()
|
})
|
||||||
}
|
w.Destroy()
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -5,58 +5,51 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/fumiama/orbyte/pbuf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrCipherTextTooShort = errors.New("ciphertext too short")
|
ErrCipherTextTooShort = errors.New("ciphertext too short")
|
||||||
)
|
)
|
||||||
|
|
||||||
func EncodeAEAD(aead cipher.AEAD, additional uint16, b []byte) pbuf.Bytes {
|
func EncodeAEAD(aead cipher.AEAD, additional uint16, b []byte) []byte {
|
||||||
nsz := aead.NonceSize()
|
nsz := aead.NonceSize()
|
||||||
// Accocate capacity for all the stuffs.
|
// Accocate capacity for all the stuffs.
|
||||||
buf := pbuf.NewBytes(2 + nsz + len(b) + aead.Overhead())
|
buf := make([]byte, 2+nsz+len(b)+aead.Overhead())
|
||||||
n := 0
|
n := 0
|
||||||
buf.V(func(buf []byte) {
|
binary.LittleEndian.PutUint16(buf[:2], additional)
|
||||||
binary.LittleEndian.PutUint16(buf[:2], additional)
|
nonce := buf[2 : 2+nsz]
|
||||||
nonce := buf[2 : 2+nsz]
|
// Select a random nonce
|
||||||
// Select a random nonce
|
_, err := rand.Read(nonce)
|
||||||
_, err := rand.Read(nonce)
|
if err != nil {
|
||||||
if err != nil {
|
panic(err)
|
||||||
panic(err)
|
}
|
||||||
}
|
// Encrypt the message and append the ciphertext to the nonce.
|
||||||
// Encrypt the message and append the ciphertext to the nonce.
|
eb := aead.Seal(nonce[nsz:nsz], nonce, b, buf[:2])
|
||||||
eb := aead.Seal(nonce[nsz:nsz], nonce, b, buf[:2])
|
n = len(eb)
|
||||||
n = len(eb)
|
return buf[2 : 2+nsz+n]
|
||||||
})
|
|
||||||
return buf.Slice(2, 2+nsz+n)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeAEAD(aead cipher.AEAD, additional uint16, b []byte) (data pbuf.Bytes, err error) {
|
func DecodeAEAD(aead cipher.AEAD, additional uint16, b []byte) (data []byte, err error) {
|
||||||
nsz := aead.NonceSize()
|
nsz := aead.NonceSize()
|
||||||
if len(b) < nsz {
|
if len(b) < nsz {
|
||||||
return pbuf.Bytes{}, ErrCipherTextTooShort
|
return nil, ErrCipherTextTooShort
|
||||||
}
|
}
|
||||||
// Split nonce and ciphertext.
|
// Split nonce and ciphertext.
|
||||||
nonce, ciphertext := b[:nsz], b[nsz:]
|
nonce, ciphertext := b[:nsz], b[nsz:]
|
||||||
if len(ciphertext) == 0 {
|
if len(ciphertext) == 0 {
|
||||||
return pbuf.Bytes{}, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
// Decrypt the message and check it wasn't tampered with.
|
// Decrypt the message and check it wasn't tampered with.
|
||||||
var buf [2]byte
|
var buf [2]byte
|
||||||
binary.LittleEndian.PutUint16(buf[:], additional)
|
binary.LittleEndian.PutUint16(buf[:], additional)
|
||||||
data = pbuf.NewBytes(len(ciphertext))
|
data = make([]byte, len(ciphertext))
|
||||||
n := 0
|
n := 0
|
||||||
data.V(func(b []byte) {
|
d, err := aead.Open(data[:0], nonce, ciphertext, buf[:])
|
||||||
var d []byte
|
n = len(d)
|
||||||
d, err = aead.Open(b[:0], nonce, ciphertext, buf[:])
|
|
||||||
n = len(d)
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return data.SliceTo(n), nil
|
return data[:n], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func EncodeXORLen(datalen int) int {
|
func EncodeXORLen(datalen int) int {
|
||||||
@@ -65,29 +58,27 @@ func EncodeXORLen(datalen int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EncodeXOR 按 8 字节, 以初始 mask 循环异或编码 data
|
// EncodeXOR 按 8 字节, 以初始 mask 循环异或编码 data
|
||||||
func EncodeXOR(data []byte, mask uint64, seq uint32) pbuf.Bytes {
|
func EncodeXOR(data []byte, mask uint64, seq uint32) []byte {
|
||||||
batchsz := len(data) / 8
|
batchsz := len(data) / 8
|
||||||
remain := len(data) % 8
|
remain := len(data) % 8
|
||||||
sum := mask
|
sum := mask
|
||||||
newdat := pbuf.NewBytes(EncodeXORLen(len(data)))
|
buf := make([]byte, EncodeXORLen(len(data)))
|
||||||
newdat.V(func(buf []byte) {
|
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
_, _ = rand.Read(buf[4:8]) // seqrand
|
||||||
_, _ = rand.Read(buf[4:8]) // seqrand
|
sum ^= binary.LittleEndian.Uint64(buf[:8]) // init from seqrand
|
||||||
sum ^= binary.LittleEndian.Uint64(buf[:8]) // init from seqrand
|
binary.LittleEndian.PutUint64(buf[:8], sum)
|
||||||
binary.LittleEndian.PutUint64(buf[:8], sum)
|
for i := 0; i < batchsz; i++ { // range on batch data
|
||||||
for i := 0; i < batchsz; i++ { // range on batch data
|
a := i * 8
|
||||||
a := i * 8
|
b := (i + 1) * 8
|
||||||
b := (i + 1) * 8
|
sum ^= binary.LittleEndian.Uint64(data[a:b])
|
||||||
sum ^= binary.LittleEndian.Uint64(data[a:b])
|
binary.LittleEndian.PutUint64(buf[a+8:b+8], sum)
|
||||||
binary.LittleEndian.PutUint64(buf[a+8:b+8], sum)
|
}
|
||||||
}
|
p := batchsz * 8
|
||||||
p := batchsz * 8
|
copy(buf[8+p:], data[p:])
|
||||||
copy(buf[8+p:], data[p:])
|
buf[len(buf)-1] = byte(remain)
|
||||||
buf[newdat.Len()-1] = byte(remain)
|
sum ^= binary.LittleEndian.Uint64(buf[8+p:])
|
||||||
sum ^= binary.LittleEndian.Uint64(buf[8+p:])
|
binary.LittleEndian.PutUint64(buf[8+p:], sum)
|
||||||
binary.LittleEndian.PutUint64(buf[8+p:], sum)
|
return buf
|
||||||
})
|
|
||||||
return newdat
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeXOR 按 8 字节, 以初始 mask 循环异或解码 data,
|
// DecodeXOR 按 8 字节, 以初始 mask 循环异或解码 data,
|
||||||
|
@@ -25,7 +25,7 @@ func TestXOR(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
seq, dec := DecodeXOR(EncodeXOR(r1.Bytes(), mask, uint32(i)).Trans(), mask)
|
seq, dec := DecodeXOR(EncodeXOR(r1.Bytes(), mask, uint32(i)), mask)
|
||||||
if !bytes.Equal(dec, r2.Bytes()) {
|
if !bytes.Equal(dec, r2.Bytes()) {
|
||||||
t.Fatal("unexpected xor at", i, "except", hex.EncodeToString(r2.Bytes()), "got", hex.EncodeToString(dec))
|
t.Fatal("unexpected xor at", i, "except", hex.EncodeToString(r2.Bytes()), "got", hex.EncodeToString(dec))
|
||||||
}
|
}
|
||||||
@@ -51,11 +51,11 @@ func TestXChacha20(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for i := 0; i < 4096; i++ {
|
for i := 0; i < 4096; i++ {
|
||||||
db, err := DecodeAEAD(aead, uint16(i), EncodeAEAD(aead, uint16(i), data[:i]).Trans())
|
db, err := DecodeAEAD(aead, uint16(i), EncodeAEAD(aead, uint16(i), data[:i]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !bytes.Equal(db.Trans(), data[:i]) {
|
if !bytes.Equal(db, data[:i]) {
|
||||||
t.Fatal("unexpected preshared at idx(len)", i, "addt", uint16(i))
|
t.Fatal("unexpected preshared at idx(len)", i, "addt", uint16(i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/klauspost/compress/zstd"
|
"github.com/klauspost/compress/zstd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EncodeZstd(data []byte) pbuf.Bytes {
|
func EncodeZstd(data []byte) []byte {
|
||||||
return bin.SelectWriter().P(func(w *pbuf.Buffer) {
|
return bin.SelectWriter().P(func(w *pbuf.Buffer) {
|
||||||
enc, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedFastest))
|
enc, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedFastest))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -23,19 +23,19 @@ func EncodeZstd(data []byte) pbuf.Bytes {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}).ToBytes()
|
}).ToBytes().Copy().Ignore().Trans()
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeZstd(data []byte) (b pbuf.Bytes, err error) {
|
func DecodeZstd(data []byte) (b []byte, err error) {
|
||||||
dec, err := zstd.NewReader(bytes.NewReader(data))
|
dec, err := zstd.NewReader(bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return pbuf.Bytes{}, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b = bin.SelectWriter().P(func(w *pbuf.Buffer) {
|
b = bin.SelectWriter().P(func(w *pbuf.Buffer) {
|
||||||
_, err = io.Copy(w, dec)
|
_, err = io.Copy(w, dec)
|
||||||
dec.Close()
|
dec.Close()
|
||||||
}).ToBytes()
|
}).ToBytes().Copy().Ignore().Trans()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user