From bdd08ce4a02e940a5f2d3624b9aed3374cd4b7d9 Mon Sep 17 00:00:00 2001 From: Jan Saidl Date: Mon, 9 Dec 2024 20:46:32 +0100 Subject: [PATCH] add MTU flag for vpn up --- src/cmd/vpnUp.go | 3 ++- src/i18n/en.go | 1 + src/i18n/i18n.go | 1 + src/wg/darwin.go | 5 +++-- src/wg/linux.go | 5 +++-- src/wg/wg.go | 4 +++- src/wg/windows.go | 5 +++-- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/cmd/vpnUp.go b/src/cmd/vpnUp.go index a1e2856d..9f2d031c 100644 --- a/src/cmd/vpnUp.go +++ b/src/cmd/vpnUp.go @@ -35,6 +35,7 @@ func vpnUpCmd() *cmdBuilder.Cmd { Short(i18n.T(i18n.CmdDescVpnUp)). ScopeLevel(scope.Project). Arg(scope.ProjectArgName, cmdBuilder.OptionalArg()). + IntFlag("mtu", 1420, i18n.T(i18n.VpnMtuFlag)). BoolFlag("auto-disconnect", false, i18n.T(i18n.VpnAutoDisconnectFlag)). HelpFlag(i18n.T(i18n.CmdHelpVpnUp)). LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error { @@ -98,7 +99,7 @@ func vpnUpCmd() *cmdBuilder.Cmd { } defer f.Close() - err = wg.GenerateConfig(f, privateKey, vpnSettings) + err = wg.GenerateConfig(f, privateKey, vpnSettings, cmdData.Params.GetInt("mtu")) if err != nil { return err } diff --git a/src/i18n/en.go b/src/i18n/en.go index 1053ae3e..50470578 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -246,6 +246,7 @@ at https://docs.zerops.io/references/cli for further details.`, ServiceIdFlag: "If you have access to more than one service, you must specify the service ID for which the\ncommand is to be executed.", ProjectIdFlag: "If you have access to more than one project, you must specify the project ID for which the\ncommand is to be executed.", VpnAutoDisconnectFlag: "If set, zCLI will automatically disconnect from the VPN if it is already connected.", + VpnMtuFlag: "If set, Wireguard interface will use this value for MTU. If VPN is not working, try a lower value.", ZeropsYamlSetup: "Choose setup to be used from zerops.yml.", // archiveClient diff --git a/src/i18n/i18n.go b/src/i18n/i18n.go index 55091c49..062c2f86 100644 --- a/src/i18n/i18n.go +++ b/src/i18n/i18n.go @@ -228,6 +228,7 @@ const ( ServiceIdFlag = "ServiceIdFlag" ProjectIdFlag = "ProjectIdFlag" VpnAutoDisconnectFlag = "VpnAutoDisconnectFlag" + VpnMtuFlag = "VpnMtuFlag" ZeropsYamlSetup = "ZeropsYamlSetup" // archiveClient diff --git a/src/wg/darwin.go b/src/wg/darwin.go index 2ee9cf09..59e723a1 100644 --- a/src/wg/darwin.go +++ b/src/wg/darwin.go @@ -26,8 +26,8 @@ func CheckWgInstallation() error { return nil } -func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem) error { - data, err := defaultTemplateData(privateKey, vpnSettings) +func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem, mtu int) error { + data, err := defaultTemplateData(privateKey, vpnSettings, mtu) if err != nil { return err } @@ -46,6 +46,7 @@ func DownCmd(ctx context.Context, filePath, _ string) (err *cmdRunner.ExecCmd) { var vpnTmpl = ` [Interface] PrivateKey = {{.PrivateKey}} +MTU = {{.Mtu}} Address = {{if .AssignedIpv4Address}}{{.AssignedIpv4Address}}/32{{end}}, {{.AssignedIpv6Address}}/128 PostUp = mkdir -p /etc/resolver diff --git a/src/wg/linux.go b/src/wg/linux.go index c35c842a..6bb59587 100644 --- a/src/wg/linux.go +++ b/src/wg/linux.go @@ -27,8 +27,8 @@ func CheckWgInstallation() error { return nil } -func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem) error { - data, err := defaultTemplateData(privateKey, vpnSettings) +func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem, mtu int) error { + data, err := defaultTemplateData(privateKey, vpnSettings, mtu) if err != nil { return err } @@ -47,6 +47,7 @@ func DownCmd(ctx context.Context, filePath, _ string) (err *cmdRunner.ExecCmd) { var vpnTmpl = ` [Interface] PrivateKey = {{.PrivateKey}} +MTU = {{.Mtu}} Address = {{if .AssignedIpv4Address}}{{.AssignedIpv4Address}}/32{{end}}, {{.AssignedIpv6Address}}/128 PostUp = resolvectl dns %i {{.Ipv4NetworkGateway}} diff --git a/src/wg/wg.go b/src/wg/wg.go index a7d888b0..6d33fcb4 100644 --- a/src/wg/wg.go +++ b/src/wg/wg.go @@ -2,13 +2,14 @@ package wg import ( "net" + "strconv" "github.com/pkg/errors" "github.com/zeropsio/zerops-go/dto/output" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) -func defaultTemplateData(privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem) (map[string]string, error) { +func defaultTemplateData(privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem, mtu int) (map[string]string, error) { projectIpv4Network := "" if vpnSettings.Project.Ipv4.Network.Network != "" { _, n, err := net.ParseCIDR(string(vpnSettings.Project.Ipv4.Network.Network)) @@ -46,6 +47,7 @@ func defaultTemplateData(privateKey wgtypes.Key, vpnSettings output.ProjectVpnIt } return map[string]string{ + "Mtu": strconv.Itoa(mtu), "PrivateKey": privateKey.String(), "PublicKey": string(vpnSettings.Project.PublicKey), "AssignedIpv4Address": string(vpnSettings.Peer.Ipv4.AssignedIpAddress), diff --git a/src/wg/windows.go b/src/wg/windows.go index a7ae1c19..940aee04 100644 --- a/src/wg/windows.go +++ b/src/wg/windows.go @@ -33,8 +33,8 @@ func CheckWgInstallation() error { return nil } -func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem) error { - data, err := defaultTemplateData(privateKey, vpnSettings) +func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem, mtu int) error { + data, err := defaultTemplateData(privateKey, vpnSettings, mtu) if err != nil { return err } @@ -117,6 +117,7 @@ func DownCmd(ctx context.Context, _, interfaceName string) (err *cmdRunner.ExecC var vpnTmpl = ` [Interface] PrivateKey = {{.PrivateKey}} +MTU = {{.Mtu}} Address = {{if .AssignedIpv4Address}}{{.AssignedIpv4Address}}/32{{end}}, {{.AssignedIpv6Address}}/128 DNS = {{.Ipv4NetworkGateway}}, zerops