mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-05 07:50:24 +08:00
feat: add ztsd for data compressing
This commit is contained in:
@@ -30,6 +30,7 @@ type Peer struct {
|
|||||||
QueryList []string `yaml:"QueryList"`
|
QueryList []string `yaml:"QueryList"`
|
||||||
QuerySeconds int64 `yaml:"QuerySeconds"`
|
QuerySeconds int64 `yaml:"QuerySeconds"`
|
||||||
AllowTrans bool `yaml:"AllowTrans"`
|
AllowTrans bool `yaml:"AllowTrans"`
|
||||||
|
UseZstd bool `yaml:"UseZstd"`
|
||||||
MTU int64 `yaml:"MTU"`
|
MTU int64 `yaml:"MTU"`
|
||||||
MTURandomRange int64 `yaml:"MTURandomRange"`
|
MTURandomRange int64 `yaml:"MTURandomRange"`
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -9,6 +9,7 @@ require (
|
|||||||
github.com/fumiama/go-x25519 v1.0.0
|
github.com/fumiama/go-x25519 v1.0.0
|
||||||
github.com/fumiama/gofastTEA v0.0.10
|
github.com/fumiama/gofastTEA v0.0.10
|
||||||
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac
|
github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac
|
||||||
|
github.com/klauspost/compress v1.16.7
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
golang.org/x/crypto v0.11.1-0.20230731181441-edc325d13aa9
|
golang.org/x/crypto v0.11.1-0.20230731181441-edc325d13aa9
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -15,6 +15,8 @@ github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac h1:A/5A0rODsg+EQHH61
|
|||||||
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=
|
||||||
github.com/fumiama/wintun v0.0.0-20211229152851-8bc97c8034c0/go.mod h1:dPOG7Af/ArO62RgBz2JJTNFByBn/IXWLo/1kZKcLSe8=
|
github.com/fumiama/wintun v0.0.0-20211229152851-8bc97c8034c0/go.mod h1:dPOG7Af/ArO62RgBz2JJTNFByBn/IXWLo/1kZKcLSe8=
|
||||||
|
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
|
||||||
|
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ type Link struct {
|
|||||||
status int8
|
status int8
|
||||||
// 是否允许转发
|
// 是否允许转发
|
||||||
allowtrans bool
|
allowtrans bool
|
||||||
|
// 是否对数据进行 zstd 压缩
|
||||||
|
usezstd bool
|
||||||
// udp 数据包的最大大小
|
// udp 数据包的最大大小
|
||||||
mtu uint16
|
mtu uint16
|
||||||
// 随机放缩 mtu 范围 (只减不增)
|
// 随机放缩 mtu 范围 (只减不增)
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package link
|
package link
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
@@ -71,6 +74,16 @@ func (m *Me) listenthread(conn *net.UDPConn, mu *sync.Mutex) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if p.usezstd {
|
||||||
|
dec, _ := zstd.NewReader(bytes.NewReader(packet.Data))
|
||||||
|
packet.Data, err = io.ReadAll(dec)
|
||||||
|
dec.Close()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Debugln("[listen] drop invalid zstd packet:", err)
|
||||||
|
packet.Put()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
if !packet.IsVaildHash() {
|
if !packet.IsVaildHash() {
|
||||||
logrus.Debugln("[listen] drop invalid hash packet")
|
logrus.Debugln("[listen] drop invalid hash packet")
|
||||||
packet.Put()
|
packet.Put()
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type PeerConfig struct {
|
|||||||
MTU uint16
|
MTU uint16
|
||||||
MTURandomRange uint16
|
MTURandomRange uint16
|
||||||
AllowTrans, NoPipe bool
|
AllowTrans, NoPipe bool
|
||||||
|
UseZstd bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPeer 添加一个 peer
|
// AddPeer 添加一个 peer
|
||||||
@@ -38,6 +39,7 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
|
|||||||
pubk: cfg.PubicKey,
|
pubk: cfg.PubicKey,
|
||||||
peerip: net.ParseIP(cfg.PeerIP),
|
peerip: net.ParseIP(cfg.PeerIP),
|
||||||
allowtrans: cfg.AllowTrans,
|
allowtrans: cfg.AllowTrans,
|
||||||
|
usezstd: cfg.UseZstd,
|
||||||
me: m,
|
me: m,
|
||||||
mtu: uint16(cfg.MTU),
|
mtu: uint16(cfg.MTU),
|
||||||
mturandomrange: uint16(cfg.MTURandomRange),
|
mturandomrange: uint16(cfg.MTURandomRange),
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package link
|
package link
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
|
"github.com/fumiama/WireGold/helper"
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,21 +25,13 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
logrus.Debugln("[send] mtu:", mtu, ", count:", sndcnt, ", additional data:", uint16(sndcnt))
|
logrus.Debugln("[send] mtu:", mtu, ", count:", sndcnt, ", additional data:", uint16(sndcnt))
|
||||||
if len(p.Data) <= int(mtu) {
|
if len(p.Data) <= int(mtu) {
|
||||||
if !istransfer {
|
if !istransfer {
|
||||||
p.FillHash()
|
l.encrypt(p, uint16(sndcnt), teatype)
|
||||||
if l.aead != nil {
|
|
||||||
p.Data = l.EncodePreshared(uint16(sndcnt), p.Data)
|
|
||||||
}
|
|
||||||
p.Data = l.Encode(teatype, p.Data)
|
|
||||||
}
|
}
|
||||||
defer p.Put()
|
defer p.Put()
|
||||||
return l.write(p, teatype, uint16(sndcnt), mtu, uint32(len(p.Data)), 0, istransfer, false)
|
return l.write(p, teatype, uint16(sndcnt), mtu, uint32(len(p.Data)), 0, istransfer, false)
|
||||||
}
|
}
|
||||||
if !istransfer {
|
if !istransfer {
|
||||||
p.FillHash()
|
l.encrypt(p, uint16(sndcnt), teatype)
|
||||||
if l.aead != nil {
|
|
||||||
p.Data = l.EncodePreshared(uint16(sndcnt), p.Data)
|
|
||||||
}
|
|
||||||
p.Data = l.Encode(teatype, p.Data)
|
|
||||||
}
|
}
|
||||||
data := p.Data
|
data := p.Data
|
||||||
ttl := p.TTL
|
ttl := p.TTL
|
||||||
@@ -62,6 +58,22 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
||||||
|
p.FillHash()
|
||||||
|
if l.usezstd {
|
||||||
|
w := helper.SelectWriter()
|
||||||
|
defer helper.PutWriter(w)
|
||||||
|
enc, _ := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedFastest))
|
||||||
|
defer enc.Close()
|
||||||
|
_, _ = io.Copy(enc, bytes.NewReader(p.Data))
|
||||||
|
p.Data = w.Bytes()
|
||||||
|
}
|
||||||
|
if l.aead != nil {
|
||||||
|
p.Data = l.EncodePreshared(sndcnt, p.Data)
|
||||||
|
}
|
||||||
|
p.Data = l.Encode(teatype, p.Data)
|
||||||
|
}
|
||||||
|
|
||||||
// write 向 peer 发一个包
|
// write 向 peer 发一个包
|
||||||
func (l *Link) write(p *head.Packet, teatype uint8, additional, mtu uint16, datasz uint32, offset uint16, istransfer, hasmore bool) (n int, err error) {
|
func (l *Link) write(p *head.Packet, teatype uint8, additional, mtu uint16, datasz uint32, offset uint16, istransfer, hasmore bool) (n int, err error) {
|
||||||
var d []byte
|
var d []byte
|
||||||
|
|||||||
@@ -140,6 +140,7 @@ func (wg *WG) init(srcport, dstport uint16) {
|
|||||||
MTURandomRange: uint16(peer.MTURandomRange),
|
MTURandomRange: uint16(peer.MTURandomRange),
|
||||||
AllowTrans: peer.AllowTrans,
|
AllowTrans: peer.AllowTrans,
|
||||||
NoPipe: true,
|
NoPipe: true,
|
||||||
|
UseZstd: peer.UseZstd,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user