1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-05 07:50:24 +08:00

fix: use nic mtu that minus packet header

This commit is contained in:
源文雨
2024-08-03 16:42:16 +08:00
parent bafeb149be
commit cf2daf9a3e
10 changed files with 46 additions and 38 deletions

View File

@@ -15,7 +15,7 @@ type Config struct {
PrivateKey string `yaml:"PrivateKey"` PrivateKey string `yaml:"PrivateKey"`
Network string `yaml:"Network"` // Network udp, tcp or ws (WIP) Network string `yaml:"Network"` // Network udp, tcp or ws (WIP)
EndPoint string `yaml:"EndPoint"` EndPoint string `yaml:"EndPoint"`
MTU int64 `yaml:"MTU"` MTU int64 `yaml:"MTU"` // MTU of nic (will minus packet header len)
SpeedLoop uint16 `yaml:"SpeedLoop"` SpeedLoop uint16 `yaml:"SpeedLoop"`
Mask uint64 `yaml:"Mask"` // Mask 是异或报文所用掩码, 必须保证各端统一 Mask uint64 `yaml:"Mask"` // Mask 是异或报文所用掩码, 必须保证各端统一
Peers []Peer `yaml:"Peers"` Peers []Peer `yaml:"Peers"`
@@ -34,7 +34,7 @@ type Peer struct {
AllowTrans bool `yaml:"AllowTrans"` AllowTrans bool `yaml:"AllowTrans"`
UseZstd bool `yaml:"UseZstd"` UseZstd bool `yaml:"UseZstd"`
DoublePacket bool `yaml:"DoublePacket"` DoublePacket bool `yaml:"DoublePacket"`
MTU int64 `yaml:"MTU"` MTU int64 `yaml:"MTU"` // MTU of PDU passed to p2p
MTURandomRange int64 `yaml:"MTURandomRange"` MTURandomRange int64 `yaml:"MTURandomRange"`
} }

View File

@@ -39,7 +39,7 @@ type Me struct {
// 本机监听的连接端点, 也用于向对端直接发送报文 // 本机监听的连接端点, 也用于向对端直接发送报文
conn p2p.Conn conn p2p.Conn
// 本机网卡 // 本机网卡
nic lower.NICIO nic *lower.NICIO
// 本机路由表 // 本机路由表
router *Router router *Router
// 本机未接收完全分片池 // 本机未接收完全分片池
@@ -60,11 +60,17 @@ type MyConfig struct {
Network string Network string
NetworkConfigs []any NetworkConfigs []any
PrivateKey *[32]byte PrivateKey *[32]byte
NIC lower.NICIO NICConfig *NICConfig
SrcPort, DstPort, MTU, SpeedLoop uint16 SrcPort, DstPort, MTU, SpeedLoop uint16
Mask uint64 Mask uint64
} }
type NICConfig struct {
IP net.IP
SubNet *net.IPNet
CIDRs []string
}
// NewMe 设置本机参数 // NewMe 设置本机参数
func NewMe(cfg *MyConfig) (m Me) { func NewMe(cfg *MyConfig) (m Me) {
m.privKey = *cfg.PrivateKey m.privKey = *cfg.PrivateKey
@@ -89,7 +95,6 @@ func NewMe(cfg *MyConfig) (m Me) {
panic(err) panic(err)
} }
m.connections = make(map[string]*Link) m.connections = make(map[string]*Link)
m.nic = cfg.NIC
m.router = &Router{ m.router = &Router{
list: make([]*net.IPNet, 1, 16), list: make([]*net.IPNet, 1, 16),
table: make(map[string]*Link, 16), table: make(map[string]*Link, 16),
@@ -98,7 +103,13 @@ func NewMe(cfg *MyConfig) (m Me) {
m.router.SetDefault(nil) m.router.SetDefault(nil)
m.srcport = cfg.SrcPort m.srcport = cfg.SrcPort
m.dstport = cfg.DstPort m.dstport = cfg.DstPort
m.mtu = cfg.MTU & 0xfff8 m.mtu = (cfg.MTU - head.PacketHeadLen) & 0xfff8
if cfg.NICConfig != nil {
m.nic = lower.NewNIC(
cfg.NICConfig.IP, cfg.NICConfig.SubNet,
strconv.FormatUint(uint64(m.MTU()), 10), cfg.NICConfig.CIDRs...,
)
}
m.speedloop = cfg.SpeedLoop m.speedloop = cfg.SpeedLoop
if m.speedloop == 0 { if m.speedloop == 0 {
m.speedloop = 4096 m.speedloop = 4096

View File

@@ -33,7 +33,7 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
if ok { if ok {
return return
} }
if m.mtu == 0 { if cfg.MTU == 0 {
panic("invalid mtu for peer " + cfg.PeerIP) panic("invalid mtu for peer " + cfg.PeerIP)
} }
l = &Link{ l = &Link{

View File

@@ -1,7 +1,6 @@
package lower package lower
import ( import (
"io"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@@ -11,14 +10,8 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type NICIO interface { // NICIO 虚拟网卡
io.ReadWriteCloser type NICIO struct {
Up()
Down()
}
// NIC 虚拟网卡
type NIC struct {
ifce *water.Interface ifce *water.Interface
ip net.IP ip net.IP
subnet *net.IPNet subnet *net.IPNet
@@ -31,7 +24,7 @@ type NIC struct {
// 网卡地址为 ip, 所属子网为 subnet // 网卡地址为 ip, 所属子网为 subnet
// 以本网卡为下一跳的所有子网为 cidrs // 以本网卡为下一跳的所有子网为 cidrs
// cidrs 不包括本网卡 subnet // cidrs 不包括本网卡 subnet
func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO { func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) *NICIO {
ifce, err := water.New(water.Config{DeviceType: water.TUN}) ifce, err := water.New(water.Config{DeviceType: water.TUN})
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
@@ -41,7 +34,7 @@ func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO {
if bitsn != 32 { if bitsn != 32 {
panic("mask len " + strconv.Itoa(bitsn) + " is not supported") panic("mask len " + strconv.Itoa(bitsn) + " is not supported")
} }
n := &NIC{ n := &NICIO{
ifce: ifce, ifce: ifce,
ip: ip, ip: ip,
subnet: subnet, subnet: subnet,
@@ -53,16 +46,16 @@ func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO {
} }
// Read 匹配 PacketsIO Interface // Read 匹配 PacketsIO Interface
func (nc *NIC) Read(buf []byte) (int, error) { func (nc *NICIO) Read(buf []byte) (int, error) {
return nc.ifce.Read(buf) return nc.ifce.Read(buf)
} }
func (nc *NIC) Write(packet []byte) (int, error) { func (nc *NICIO) Write(packet []byte) (int, error) {
return nc.ifce.Write(packet) return nc.ifce.Write(packet)
} }
// Close 关闭网卡 // Close 关闭网卡
func (n *NIC) Close() error { func (n *NICIO) Close() error {
return n.ifce.Close() return n.ifce.Close()
} }

View File

@@ -5,7 +5,7 @@ package lower
import "net" import "net"
func (n *NIC) Up() { func (n *NICIO) Up() {
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159 execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159
execute( execute(
"ifconfig", n.ifce.Name(), "ifconfig", n.ifce.Name(),
@@ -19,7 +19,7 @@ func (n *NIC) Up() {
} }
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
execute("route", "delete", n.subnet.String(), "-interface", n.ifce.Name()) execute("route", "delete", n.subnet.String(), "-interface", n.ifce.Name())
for _, c := range n.cidrs { for _, c := range n.cidrs {
execute("route", "delete", c, "-interface", n.ifce.Name()) execute("route", "delete", c, "-interface", n.ifce.Name())

View File

@@ -3,7 +3,7 @@
package lower package lower
func (n *NIC) Up() { func (n *NICIO) Up() {
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu) execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu)
execute("/sbin/ip", "addr", "add", n.rawipnet, "dev", n.ifce.Name()) execute("/sbin/ip", "addr", "add", n.rawipnet, "dev", n.ifce.Name())
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up") execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up")
@@ -12,7 +12,7 @@ func (n *NIC) Up() {
} }
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
for _, c := range n.cidrs { for _, c := range n.cidrs {
execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name()) execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name())
} }

View File

@@ -3,10 +3,10 @@
package lower package lower
func (n *NIC) Up() { func (n *NICIO) Up() {
panic("not support lower on this os now") panic("not support lower on this os now")
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
panic("not support lower on this os now") panic("not support lower on this os now")
} }

View File

@@ -5,7 +5,7 @@ package lower
import "net" import "net"
func (n *NIC) Up() { func (n *NICIO) Up() {
execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip.String()+"\" mask=\""+(net.IP)(n.subnet.Mask).String()+"\" gateway=none") execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip.String()+"\" mask=\""+(net.IP)(n.subnet.Mask).String()+"\" gateway=none")
execute("cmd", "/c", "netsh interface ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu) execute("cmd", "/c", "netsh interface ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu)
for _, c := range n.cidrs { for _, c := range n.cidrs {
@@ -17,7 +17,7 @@ func (n *NIC) Up() {
} }
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled") // execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled")
for _, c := range n.cidrs { for _, c := range n.cidrs {
ip, _, err := net.ParseCIDR(c) ip, _, err := net.ParseCIDR(c)

View File

@@ -15,6 +15,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config" "github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
"github.com/fumiama/WireGold/upper" "github.com/fumiama/WireGold/upper"
"github.com/fumiama/WireGold/upper/services/wg" "github.com/fumiama/WireGold/upper/services/wg"
@@ -144,8 +145,8 @@ func main() {
if c.EndPoint == "" { if c.EndPoint == "" {
displayHelp("nil endpoint") displayHelp("nil endpoint")
} }
if c.MTU == 0 { if c.MTU <= head.PacketHeadLen {
displayHelp("nil mtu") displayHelp("invalid mtu")
} }
w, err := wg.NewWireGold(&c) w, err := wg.NewWireGold(&c)
if err != nil { if err != nil {

View File

@@ -17,7 +17,6 @@ import (
"github.com/fumiama/WireGold/config" "github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/link" "github.com/fumiama/WireGold/gold/link"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
"github.com/fumiama/WireGold/lower"
) )
const suffix32 = "㴄" const suffix32 = "㴄"
@@ -104,12 +103,16 @@ func (wg *WG) init(srcport, dstport uint16) {
MyEndpoint: wg.c.EndPoint, MyEndpoint: wg.c.EndPoint,
Network: wg.c.Network, Network: wg.c.Network,
PrivateKey: &wg.key, PrivateKey: &wg.key,
NIC: lower.NewNIC(myip, mysubnet, strconv.FormatInt(wg.c.MTU, 10), cidrs...), NICConfig: &link.NICConfig{
SrcPort: srcport, IP: myip,
DstPort: dstport, SubNet: mysubnet,
MTU: uint16(wg.c.MTU), CIDRs: cidrs,
SpeedLoop: wg.c.SpeedLoop, },
Mask: wg.c.Mask, SrcPort: srcport,
DstPort: dstport,
MTU: uint16(wg.c.MTU),
SpeedLoop: wg.c.SpeedLoop,
Mask: wg.c.Mask,
}) })
for _, peer := range wg.c.Peers { for _, peer := range wg.c.Peers {