add peerwise mtu

This commit is contained in:
源文雨
2022-05-13 13:50:41 +08:00
parent 4ccd99b63f
commit 4e833c877d
10 changed files with 19 additions and 7 deletions

BIN
.github/Maria.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 KiB

BIN
.github/rikka.png vendored

Binary file not shown.

Before

Width:  |  Height:  |  Size: 209 KiB

View File

@@ -1,5 +1,5 @@
<div align="center">
<img src=".github/rikka.png" width = "360" height = "360" alt="WireGold-Rikka"><br>
<img src=".github/Maria.png" width = "400" alt="WireGold-Maria"><br>
<h1>WireGold</h1>
Wire Golang Guard = WireGold<br><br>
</div>

View File

@@ -28,6 +28,7 @@ type Peer struct {
QueryList []string `yaml:"QueryList"`
QuerySeconds int64 `yaml:"QuerySeconds"`
AllowTrans bool `yaml:"AllowTrans"`
MTU int64 `yaml:"MTU"`
}
func Parse(path string) (c Config) {

View File

@@ -32,6 +32,8 @@ type Link struct {
status int
// 是否允许转发
allowtrans bool
// udp 数据包的最大大小
mtu uint16
}
const (

View File

@@ -88,6 +88,7 @@ func NewMe(cfg *MyConfig) (m Me) {
EndPoint: "127.0.0.1:56789",
AllowedIPs: []string{cfg.MyIPwithMask},
NoPipe: cfg.NIC != nil,
MTU: cfg.MTU,
})
m.srcport = cfg.SrcPort
m.dstport = cfg.DstPort

View File

@@ -16,6 +16,7 @@ type PeerConfig struct {
AllowedIPs, Querys []string
PubicKey *[32]byte
KeepAliveDur, QueryTick int64
MTU uint16
AllowTrans, NoPipe bool
}
@@ -27,11 +28,15 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
if ok {
return
}
if cfg.MTU == 0 || cfg.MTU == 65535 {
panic("invalid mtu for peer " + cfg.PeerIP)
}
l = &Link{
pubk: cfg.PubicKey,
peerip: net.ParseIP(cfg.PeerIP),
allowtrans: cfg.AllowTrans,
me: m,
mtu: uint16(cfg.MTU),
}
if !cfg.NoPipe {

View File

@@ -12,7 +12,7 @@ import (
// WriteAndPut 向 peer 发包并将包放回缓存池
func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
teatype := uint8(rand.Intn(16))
if len(p.Data) <= int(l.me.mtu) {
if len(p.Data) <= int(l.mtu) {
if !istransfer {
p.FillHash()
p.Data = l.Encode(teatype, p.Data)
@@ -30,15 +30,15 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
i := 0
packet := head.SelectPacket()
*packet = *p
for ; int(totl)-i > int(l.me.mtu); i += int(l.me.mtu) {
logrus.Debugln("[link] split frag", i, ":", i+int(l.me.mtu), ", remain:", int(totl)-i-int(l.me.mtu))
packet.Data = data[:int(l.me.mtu)]
for ; int(totl)-i > int(l.mtu); i += int(l.mtu) {
logrus.Debugln("[link] split frag", i, ":", i+int(l.mtu), ", remain:", int(totl)-i-int(l.mtu))
packet.Data = data[:int(l.mtu)]
cnt, err := l.write(packet, teatype, totl, uint16(uint(i)>>3), istransfer, true)
n += cnt
if err != nil {
return n, err
}
data = data[int(l.me.mtu):]
data = data[int(l.mtu):]
packet.TTL = ttl
}
packet.Put()
@@ -54,7 +54,7 @@ func (l *Link) write(p *head.Packet, teatype uint8, datasz uint32, offset uint16
var d []byte
var cl func()
if istransfer {
if p.Flags&0x4000 == 0x4000 && len(p.Data) > int(l.me.mtu) {
if p.Flags&0x4000 == 0x4000 && len(p.Data) > int(l.mtu) {
return len(p.Data), errors.New("drop dont fragmnet big trans packet")
}
d, cl = p.Marshal(nil, teatype, 0, 0, false, false)

View File

@@ -41,6 +41,7 @@ func TestTunnel(t *testing.T) {
EndPoint: "127.0.0.1:1237",
AllowedIPs: []string{"192.168.1.3/32"},
PubicKey: peerpk.Public(),
MTU: 4096,
})
p := link.NewMe(&link.MyConfig{
MyIPwithMask: "192.168.1.3/32",
@@ -55,6 +56,7 @@ func TestTunnel(t *testing.T) {
EndPoint: "127.0.0.1:1236",
AllowedIPs: []string{"192.168.1.2/32"},
PubicKey: selfpk.Public(),
MTU: 4096,
})
tunnme, err := Create(&m, "192.168.1.3")
if err != nil {

View File

@@ -117,6 +117,7 @@ func (wg *WG) init(srcport, dstport, mtu uint16) {
PubicKey: &peerkey,
KeepAliveDur: peer.KeepAliveSeconds,
QueryTick: peer.QuerySeconds,
MTU: uint16(peer.MTU),
AllowTrans: peer.AllowTrans,
NoPipe: true,
})