Skip to content

Commit 2b5cc16

Browse files
committed
Flags refactoring + custom MTU
Signed-off-by: Francesco Cheinasso [email protected]
1 parent 12269c2 commit 2b5cc16

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed

flags/flags.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package flags
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/pflag"
8+
"golang.zx2c4.com/wireguard/device"
9+
)
10+
11+
func Parse(opts *Options) error {
12+
pflag.Usage = func() {
13+
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <interface-name>\n", os.Args[0])
14+
pflag.PrintDefaults()
15+
}
16+
17+
pflag.IntVar(&opts.MTU, "mtu", device.DefaultMTU, "Set the MTU of the device")
18+
pflag.BoolVar(&opts.Foreground, "foreground", false, "Remain in the foreground")
19+
pflag.BoolVarP(&opts.ShowVersion, "version", "v", false, "Print the version number and exit")
20+
21+
pflag.Parse()
22+
23+
if opts.ShowVersion {
24+
return nil
25+
}
26+
27+
if err := setInterfaceName(opts); err != nil {
28+
return err
29+
}
30+
return nil
31+
}
32+
33+
func setInterfaceName(opts *Options) error {
34+
if pflag.NArg() != 1 {
35+
return fmt.Errorf("Must pass exactly one interface name, but got %d", pflag.NArg())
36+
}
37+
opts.InterfaceName = pflag.Arg(0)
38+
return nil
39+
}

flags/options.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package flags
2+
3+
type Options struct {
4+
InterfaceName string
5+
6+
MTU int
7+
Foreground bool
8+
ShowVersion bool
9+
}
10+
11+
func NewOptions() *Options {
12+
return &Options{}
13+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module golang.zx2c4.com/wireguard
33
go 1.20
44

55
require (
6+
github.com/spf13/pflag v1.0.5
67
golang.org/x/crypto v0.13.0
78
golang.org/x/net v0.15.0
89
golang.org/x/sys v0.12.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
22
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
3+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
4+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
35
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
46
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
57
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=

main.go

+15-36
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.org/x/sys/unix"
1818
"golang.zx2c4.com/wireguard/conn"
1919
"golang.zx2c4.com/wireguard/device"
20+
"golang.zx2c4.com/wireguard/flags"
2021
"golang.zx2c4.com/wireguard/ipc"
2122
"golang.zx2c4.com/wireguard/tun"
2223
)
@@ -32,10 +33,6 @@ const (
3233
ENV_WG_PROCESS_FOREGROUND = "WG_PROCESS_FOREGROUND"
3334
)
3435

35-
func printUsage() {
36-
fmt.Printf("Usage: %s [-f/--foreground] INTERFACE-NAME\n", os.Args[0])
37-
}
38-
3936
func warning() {
4037
switch runtime.GOOS {
4138
case "linux", "freebsd", "openbsd":
@@ -58,41 +55,21 @@ func warning() {
5855
}
5956

6057
func main() {
61-
if len(os.Args) == 2 && os.Args[1] == "--version" {
62-
fmt.Printf("wireguard-go v%s\n\nUserspace WireGuard daemon for %s-%s.\nInformation available at https://www.wireguard.com.\nCopyright (C) Jason A. Donenfeld <[email protected]>.\n", Version, runtime.GOOS, runtime.GOARCH)
63-
return
58+
opts := flags.NewOptions()
59+
if err := flags.Parse(opts); err != nil {
60+
fmt.Fprintln(os.Stderr, err)
61+
os.Exit(ExitSetupFailed)
6462
}
6563

66-
warning()
67-
68-
var foreground bool
69-
var interfaceName string
70-
if len(os.Args) < 2 || len(os.Args) > 3 {
71-
printUsage()
64+
if opts.ShowVersion {
65+
fmt.Printf("wireguard-go v%s\n\nUserspace WireGuard daemon for %s-%s.\nInformation available at https://www.wireguard.com.\nCopyright (C) Jason A. Donenfeld <[email protected]>.\n", Version, runtime.GOOS, runtime.GOARCH)
7266
return
7367
}
7468

75-
switch os.Args[1] {
76-
77-
case "-f", "--foreground":
78-
foreground = true
79-
if len(os.Args) != 3 {
80-
printUsage()
81-
return
82-
}
83-
interfaceName = os.Args[2]
84-
85-
default:
86-
foreground = false
87-
if len(os.Args) != 2 {
88-
printUsage()
89-
return
90-
}
91-
interfaceName = os.Args[1]
92-
}
69+
warning()
9370

94-
if !foreground {
95-
foreground = os.Getenv(ENV_WG_PROCESS_FOREGROUND) == "1"
71+
if !opts.Foreground {
72+
opts.Foreground = os.Getenv(ENV_WG_PROCESS_FOREGROUND) == "1"
9673
}
9774

9875
// get log level (default: info)
@@ -111,10 +88,12 @@ func main() {
11188

11289
// open TUN device (or use supplied fd)
11390

91+
interfaceName := opts.InterfaceName
92+
11493
tdev, err := func() (tun.Device, error) {
11594
tunFdStr := os.Getenv(ENV_WG_TUN_FD)
11695
if tunFdStr == "" {
117-
return tun.CreateTUN(interfaceName, device.DefaultMTU)
96+
return tun.CreateTUN(interfaceName, opts.MTU)
11897
}
11998

12099
// construct tun device from supplied fd
@@ -130,7 +109,7 @@ func main() {
130109
}
131110

132111
file := os.NewFile(uintptr(fd), "")
133-
return tun.CreateTUNFromFile(file, device.DefaultMTU)
112+
return tun.CreateTUNFromFile(file, opts.MTU)
134113
}()
135114

136115
if err == nil {
@@ -176,7 +155,7 @@ func main() {
176155
}
177156
// daemonize the process
178157

179-
if !foreground {
158+
if !opts.Foreground {
180159
env := os.Environ()
181160
env = append(env, fmt.Sprintf("%s=3", ENV_WG_TUN_FD))
182161
env = append(env, fmt.Sprintf("%s=4", ENV_WG_UAPI_FD))

0 commit comments

Comments
 (0)