mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-23 03:50:32 +08:00
feat: add MTU random range
This commit is contained in:
@@ -31,8 +31,10 @@ type Peer struct {
|
|||||||
QuerySeconds int64 `yaml:"QuerySeconds"`
|
QuerySeconds int64 `yaml:"QuerySeconds"`
|
||||||
AllowTrans bool `yaml:"AllowTrans"`
|
AllowTrans bool `yaml:"AllowTrans"`
|
||||||
MTU int64 `yaml:"MTU"`
|
MTU int64 `yaml:"MTU"`
|
||||||
|
MTURandomRange int64 `yaml:"MTURandomRange"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse 解析配置文件
|
||||||
func Parse(path string) (c Config) {
|
func Parse(path string) (c Config) {
|
||||||
file, err := os.ReadFile(path)
|
file, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -34,11 +34,13 @@ type Link struct {
|
|||||||
// 本机信息
|
// 本机信息
|
||||||
me *Me
|
me *Me
|
||||||
// 连接的状态,详见下方 const
|
// 连接的状态,详见下方 const
|
||||||
status int
|
status int8
|
||||||
// 是否允许转发
|
// 是否允许转发
|
||||||
allowtrans bool
|
allowtrans bool
|
||||||
// udp 数据包的最大大小
|
// udp 数据包的最大大小
|
||||||
mtu uint16
|
mtu uint16
|
||||||
|
// 随机放缩 mtu 范围 (只减不增)
|
||||||
|
mturandomrange uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type PeerConfig struct {
|
|||||||
PresharedKey *[32]byte
|
PresharedKey *[32]byte
|
||||||
KeepAliveDur, QueryTick int64
|
KeepAliveDur, QueryTick int64
|
||||||
MTU uint16
|
MTU uint16
|
||||||
|
MTURandomRange uint16
|
||||||
AllowTrans, NoPipe bool
|
AllowTrans, NoPipe bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,11 +35,12 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
|
|||||||
panic("invalid mtu for peer " + cfg.PeerIP)
|
panic("invalid mtu for peer " + cfg.PeerIP)
|
||||||
}
|
}
|
||||||
l = &Link{
|
l = &Link{
|
||||||
pubk: cfg.PubicKey,
|
pubk: cfg.PubicKey,
|
||||||
peerip: net.ParseIP(cfg.PeerIP),
|
peerip: net.ParseIP(cfg.PeerIP),
|
||||||
allowtrans: cfg.AllowTrans,
|
allowtrans: cfg.AllowTrans,
|
||||||
me: m,
|
me: m,
|
||||||
mtu: uint16(cfg.MTU),
|
mtu: uint16(cfg.MTU),
|
||||||
|
mturandomrange: uint16(cfg.MTURandomRange),
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cfg.NoPipe {
|
if !cfg.NoPipe {
|
||||||
|
|||||||
@@ -15,7 +15,11 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
teatype := uint8(rand.Intn(16))
|
teatype := uint8(rand.Intn(16))
|
||||||
sndcnt := atomic.AddUintptr(&l.sendcount, 1)
|
sndcnt := atomic.AddUintptr(&l.sendcount, 1)
|
||||||
logrus.Debugln("[send] count:", sndcnt, ", additional data:", uint16(sndcnt))
|
logrus.Debugln("[send] count:", sndcnt, ", additional data:", uint16(sndcnt))
|
||||||
if len(p.Data) <= int(l.mtu) {
|
mtu := l.mtu
|
||||||
|
if l.mturandomrange > 0 {
|
||||||
|
mtu -= uint16(rand.Intn(int(l.mturandomrange)))
|
||||||
|
}
|
||||||
|
if len(p.Data) <= int(mtu) {
|
||||||
if !istransfer {
|
if !istransfer {
|
||||||
p.FillHash()
|
p.FillHash()
|
||||||
if l.aead != nil {
|
if l.aead != nil {
|
||||||
@@ -24,7 +28,7 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
p.Data = l.Encode(teatype, p.Data)
|
p.Data = l.Encode(teatype, p.Data)
|
||||||
}
|
}
|
||||||
defer p.Put()
|
defer p.Put()
|
||||||
return l.write(p, teatype, uint16(sndcnt), 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()
|
p.FillHash()
|
||||||
@@ -39,31 +43,31 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
i := 0
|
i := 0
|
||||||
packet := head.SelectPacket()
|
packet := head.SelectPacket()
|
||||||
*packet = *p
|
*packet = *p
|
||||||
for ; int(totl)-i > int(l.mtu); i += int(l.mtu) {
|
for ; int(totl)-i > int(mtu); i += int(mtu) {
|
||||||
logrus.Debugln("[send] split frag", i, ":", i+int(l.mtu), ", remain:", int(totl)-i-int(l.mtu))
|
logrus.Debugln("[send] split frag", i, ":", i+int(mtu), ", remain:", int(totl)-i-int(mtu))
|
||||||
packet.Data = data[:int(l.mtu)]
|
packet.Data = data[:int(mtu)]
|
||||||
cnt, err := l.write(packet, teatype, uint16(sndcnt), totl, uint16(i>>3), istransfer, true)
|
cnt, err := l.write(packet, teatype, uint16(sndcnt), mtu, totl, uint16(i>>3), istransfer, true)
|
||||||
n += cnt
|
n += cnt
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
data = data[int(l.mtu):]
|
data = data[int(mtu):]
|
||||||
packet.TTL = ttl
|
packet.TTL = ttl
|
||||||
}
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
p.Data = data
|
p.Data = data
|
||||||
cnt, err := l.write(p, teatype, uint16(sndcnt), totl, uint16(i>>3), istransfer, false)
|
cnt, err := l.write(p, teatype, uint16(sndcnt), mtu, totl, uint16(i>>3), istransfer, false)
|
||||||
p.Put()
|
p.Put()
|
||||||
n += cnt
|
n += cnt
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// write 向 peer 发一个包
|
// write 向 peer 发一个包
|
||||||
func (l *Link) write(p *head.Packet, teatype uint8, additional 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
|
||||||
var cl func()
|
var cl func()
|
||||||
if istransfer {
|
if istransfer {
|
||||||
if p.Flags&0x4000 == 0x4000 && len(p.Data) > int(l.mtu) {
|
if p.Flags&0x4000 == 0x4000 && len(p.Data) > int(mtu) {
|
||||||
return len(p.Data), errors.New("drop dont fragmnet big trans packet")
|
return len(p.Data), errors.New("drop dont fragmnet big trans packet")
|
||||||
}
|
}
|
||||||
d, cl = p.Marshal(nil, teatype, additional, 0, 0, false, false)
|
d, cl = p.Marshal(nil, teatype, additional, 0, 0, false, false)
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ func (wg *WG) init(srcport, dstport uint16) {
|
|||||||
}
|
}
|
||||||
n := copy(peerkey[:], base14.Decode(k))
|
n := copy(peerkey[:], base14.Decode(k))
|
||||||
if n != 32 {
|
if n != 32 {
|
||||||
panic("peer public key length < 32")
|
panic("peer " + peer.IP + ": public key length < 32")
|
||||||
}
|
}
|
||||||
var pshk *[32]byte
|
var pshk *[32]byte
|
||||||
if peer.PresharedKey != "" {
|
if peer.PresharedKey != "" {
|
||||||
@@ -118,21 +118,28 @@ func (wg *WG) init(srcport, dstport uint16) {
|
|||||||
pshk = &[32]byte{}
|
pshk = &[32]byte{}
|
||||||
n := copy(pshk[:], base14.Decode(k))
|
n := copy(pshk[:], base14.Decode(k))
|
||||||
if n != 32 {
|
if n != 32 {
|
||||||
panic("peer preshared key length < 32")
|
panic("peer " + peer.IP + ": preshared key length < 32")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if peer.MTU >= 65535 {
|
||||||
|
panic("peer " + peer.IP + ": MTU too large")
|
||||||
|
}
|
||||||
|
if peer.MTURandomRange >= peer.MTU/2 {
|
||||||
|
panic("peer " + peer.IP + ": MTURandomRange too large")
|
||||||
|
}
|
||||||
wg.me.AddPeer(&link.PeerConfig{
|
wg.me.AddPeer(&link.PeerConfig{
|
||||||
PeerIP: peer.IP,
|
PeerIP: peer.IP,
|
||||||
EndPoint: peer.EndPoint,
|
EndPoint: peer.EndPoint,
|
||||||
AllowedIPs: peer.AllowedIPs,
|
AllowedIPs: peer.AllowedIPs,
|
||||||
Querys: peer.QueryList,
|
Querys: peer.QueryList,
|
||||||
PubicKey: &peerkey,
|
PubicKey: &peerkey,
|
||||||
PresharedKey: pshk,
|
PresharedKey: pshk,
|
||||||
KeepAliveDur: peer.KeepAliveSeconds,
|
KeepAliveDur: peer.KeepAliveSeconds,
|
||||||
QueryTick: peer.QuerySeconds,
|
QueryTick: peer.QuerySeconds,
|
||||||
MTU: uint16(peer.MTU),
|
MTU: uint16(peer.MTU),
|
||||||
AllowTrans: peer.AllowTrans,
|
MTURandomRange: uint16(peer.MTURandomRange),
|
||||||
NoPipe: true,
|
AllowTrans: peer.AllowTrans,
|
||||||
|
NoPipe: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user