From 587a7db4709dc4daafa186579bd9a0ab94b9faa3 Mon Sep 17 00:00:00 2001 From: Neven Miculinic Date: Thu, 28 Mar 2019 16:05:43 +0100 Subject: [PATCH] Added initial binary wg-quick equivalent --- .gitlab-ci.yml | 20 +++++++++---- cmd/wg-quick/main.go | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 cmd/wg-quick/main.go diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b29623e..f41cdd9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,13 +3,23 @@ stages: go_build: stage: build image: golang:1.12 - cache: - key: ${CI_COMMIT_REF_SLUG} - paths: - - .cache + variables: + CGO_ENABLED: 0 script: - mkdir -p .cache/cache - mkdir -p .cache/go - export GOCACHE="$CI_PROJECT_DIR/.cache/cache" - export GOPATH="$CI_PROJECT_DIR/.cache/go" - - go test ./... + - go test -race ./... + - mkdir -p build || true + - go env + - GOOS=linux GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -o build/${CI_PROJECT_NAME}-amd64 ./cmd/wg-quick + - GOOS=linux GOARCH=arm GOARM=7 go build -a -ldflags '-extldflags "-static"' -o build/${CI_PROJECT_NAME}-arm32v7 ./cmd/wg-quick + - GOOS=linux GOARCH=arm64 go build -a -ldflags '-extldflags "-static"' -o build/${CI_PROJECT_NAME}-arm64v8 ./cmd/wg-quick + artifacts: + paths: + - build/ + cache: + key: ${CI_COMMIT_REF_SLUG} + paths: + - .cache diff --git a/cmd/wg-quick/main.go b/cmd/wg-quick/main.go new file mode 100644 index 0000000..4a63e2f --- /dev/null +++ b/cmd/wg-quick/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "flag" + "fmt" + "github.com/nmiculinic/wg-quick-go" + "github.com/sirupsen/logrus" + "io/ioutil" + "os" +) + +func printHelp() { + fmt.Println("wg-quick [-iface=wg0] [ up | down ] config_file") + os.Exit(1) +} + +func main() { + flag.String("iface", "", "interface") + flag.Parse() + args := flag.Args() + if len(args) != 2 { + printHelp() + } + + iface := flag.Lookup("iface").Value.String() + log := logrus.WithField("iface", iface) + + cfg := args[1] + + _, err := os.Stat(cfg) + switch { + case err == nil: + case os.IsNotExist(err): + if iface == "" { + iface = cfg + log = logrus.WithField("iface", iface) + } + cfg = "/etc/wireguard/" + cfg + ".conf" + _, err = os.Stat(cfg) + if err != nil { + log.WithError(err).Errorln("cannot find config file") + printHelp() + } + default: + logrus.WithError(err).Errorln("error while reading config file") + printHelp() + } + + b, err := ioutil.ReadFile(cfg) + if err != nil { + logrus.WithError(err).Fatalln("cannot read file") + } + c := &wgquick.Config{} + if err := c.UnmarshalText(b); err != nil { + logrus.WithError(err).Fatalln("cannot parse config file") + } + + switch args[0] { + case "up": + if err := wgquick.Up(c, iface, log); err != nil { + logrus.WithError(err).Errorln("cannot up interface") + } + case "down": + if err := wgquick.Down(c, iface, log); err != nil { + logrus.WithError(err).Errorln("cannot down interface") + } + } +}