From 52215ec63accf390761aa0e0108a8031e99093e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 13 May 2022 23:13:30 +0800 Subject: [PATCH] drop commandline mtu --- README.md | 14 ++++++++------ config/cfg.go | 1 + main.go | 3 +-- upper/services/wg/wg.go | 16 ++++++++-------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 56bb4e5..8295b2c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ## Usage > If you are running in windows, remember to select the `wintun.dll` of your arch in `lower/wintun` and place it alongside the compiled exe ```bash -wg [-c config.yaml] [-d|w] [-g] [-h] [-m mtu] [-p] [-l log.txt] +wg [-c config.yaml] [-d|w] [-g] [-h] [-p] [-l log.txt] ``` #### Instructions ```bash @@ -18,21 +18,21 @@ wg [-c config.yaml] [-d|w] [-g] [-h] [-m mtu] [-p] [-l log.txt] -h display this help -l string write log to file (default "-") - -m int - set the mtu of wg (default 1432) -p show my publickey -w only show logs above warn level ``` -- **macos mojave**: max mtu (under ipv4 endpoint) is `9159` -- **ipv6 endpoint**: the recommand mtu is `1280~1400` to prevent the big segments from being dropped - ## Config file example + +- **macos mojave**: max mtu (under ipv4 endpoint) is `9159` +- **ipv6 endpoint**: the recommand mtu is `1280~1500` to prevent the big segments from being dropped + ```yaml IP: 192.168.233.1 SubNet: 192.168.233.0/24 PrivateKey: 暲菉斂狧污爉窫擸紈卆帞蔩慈睠庮扝憚瞼縀 EndPoint: 0.0.0.0:56789 +MTU: 1500 Peers: - IP: "192.168.233.2" @@ -42,6 +42,7 @@ Peers: AllowedIPs: ["192.168.233.2/32"] KeepAliveSeconds: 0 QueryList: ["192.168.233.3"] + MTU: 1400 QuerySeconds: 10 AllowTrans: false - @@ -50,6 +51,7 @@ Peers: PublicKey: 牢喨粷詸衭譛浾蘹櫠砙杹蟫瑳叩刋橋経挵蘀 EndPoint: "" AllowedIPs: ["192.168.233.3/32"] + MTU: 1300 KeepAliveSeconds: 0 AllowTrans: false ``` \ No newline at end of file diff --git a/config/cfg.go b/config/cfg.go index 4a3faf9..d65301a 100644 --- a/config/cfg.go +++ b/config/cfg.go @@ -14,6 +14,7 @@ type Config struct { SubNet string `yaml:"SubNet"` PrivateKey string `yaml:"PrivateKey"` EndPoint string `yaml:"EndPoint"` + MTU int64 `yaml:"MTU"` Peers []Peer `yaml:"Peers"` } diff --git a/main.go b/main.go index 7cec9f4..7d4401a 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,6 @@ func main() { gen := flag.Bool("g", false, "generate key pair") showp := flag.Bool("p", false, "show my publickey") file := flag.String("c", "config.yaml", "specify conf file") - mtu := flag.Int("m", 1500-68, "set the mtu of wg") debug := flag.Bool("d", false, "print debug logs") warn := flag.Bool("w", false, "only show logs above warn level") logfile := flag.String("l", "-", "write log to file") @@ -129,7 +128,7 @@ func main() { } defer w.Stop() - w.Run(upper.ServiceWireGold, upper.ServiceWireGold, uint16(*mtu)) + w.Run(upper.ServiceWireGold, upper.ServiceWireGold) } func displayHelp(hint string) { diff --git a/upper/services/wg/wg.go b/upper/services/wg/wg.go index 173a826..e2bc0a8 100644 --- a/upper/services/wg/wg.go +++ b/upper/services/wg/wg.go @@ -2,8 +2,8 @@ package wg import ( "errors" - "fmt" "net" + "strconv" base14 "github.com/fumiama/go-base16384" curve "github.com/fumiama/go-x25519" @@ -48,13 +48,13 @@ func NewWireGold(c *config.Config) (wg WG, err error) { return } -func (wg *WG) Start(srcport, destport, mtu uint16) { - wg.init(srcport, destport, mtu) +func (wg *WG) Start(srcport, destport uint16) { + wg.init(srcport, destport) go wg.me.ListenFromNIC() } -func (wg *WG) Run(srcport, destport, mtu uint16) { - wg.init(srcport, destport, mtu) +func (wg *WG) Run(srcport, destport uint16) { + wg.init(srcport, destport) _, err := wg.me.ListenFromNIC() if err != nil { logrus.Panicln(err) @@ -65,7 +65,7 @@ func (wg *WG) Stop() { _ = wg.me.Close() } -func (wg *WG) init(srcport, dstport, mtu uint16) { +func (wg *WG) init(srcport, dstport uint16) { cidrsmap := make(map[string]bool, 32) _, mysubnet, err := net.ParseCIDR(wg.c.SubNet) if err != nil { @@ -93,10 +93,10 @@ func (wg *WG) init(srcport, dstport, mtu uint16) { MyIPwithMask: wg.c.IP + "/32", MyEndpoint: wg.c.EndPoint, PrivateKey: &wg.key, - NIC: lower.NewNIC(wg.c.IP, wg.c.SubNet, fmt.Sprintf("%d", mtu), cidrs...), + NIC: lower.NewNIC(wg.c.IP, wg.c.SubNet, strconv.FormatInt(wg.c.MTU, 64), cidrs...), SrcPort: srcport, DstPort: dstport, - MTU: mtu, + MTU: uint16(wg.c.MTU), }) for _, peer := range wg.c.Peers {