diff --git a/.drone.yml b/.drone.yml index 11843a8..4741b4c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,15 +8,34 @@ steps: commands: - git fetch --tags + - name: go-bindata + image: golang:latest + commands: + - (cd /tmp; go get -u github.com/go-bindata/go-bindata/...) + - go-bindata -pkg assets -o assets/bindata.go -nomemcopy /bin/true + - name: lint image: golangci/golangci-lint:latest commands: - make lint + depends_on: + - go-bindata - name: test image: golang:latest commands: - - make lint + - make test + depends_on: + - go-bindata + + - name: clean + image: golang:latest + commands: + - make clean + - git checkout -- . + depends_on: + - lint + - test - name: build image: goreleaser/goreleaser:latest @@ -25,14 +44,14 @@ steps: path: /var/run environment: commands: + - (cd /tmp; go get -u github.com/go-bindata/go-bindata/...) - make build when: event: exclude: - tag depends_on: - - lint - - test + - clean - name: release image: goreleaser/goreleaser:latest @@ -43,13 +62,13 @@ steps: GITEA_TOKEN: from_secret: gitea_token commands: - - make release + - (cd /tmp; go get -u github.com/go-bindata/go-bindata/...) + - goreleaser release --rm-dist --parallelism=1 when: event: - tag depends_on: - - lint - - test + - clean services: - name: docker diff --git a/.gitignore b/.gitignore index a1c0a7e..4579dc2 100644 --- a/.gitignore +++ b/.gitignore @@ -80,7 +80,6 @@ fabric.properties .idea/caches/build_file_checksums.ser # OWN -wg-quicker assets/* dist/* bin/* diff --git a/.goreleaser.yml b/.goreleaser.yml index 7dc7f43..eb67c08 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,5 +1,7 @@ --- project_name: wg-quicker +gitea_urls: + api: https://git.xsfx.dev/api/v1/ builds: - main: ./cmd/wg-quicker/main.go env: diff --git a/README.md b/README.md index 24aa6c6..b883699 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# wg-quick-go +# wg-quicker [![Build Status](https://gitlab.com/neven-miculinic/wg-quick-go/badges/master/pipeline.svg)](https://gitlab.com/neven-miculinic/wg-quick-go/pipelines) [![GoDoc](https://godoc.org/github.com/nmiculinic/wireguardctrl?status.svg)](https://godoc.org/github.com/nmiculinic/wg-quick-go) [![Go Report Card](https://goreportcard.com/badge/github.com/nmiculinic/wg-quick-go)](https://goreportcard.com/report/github.com/nmiculinic/wg-quick-go) @@ -6,24 +6,24 @@ wg-quick like library in go for embedding # Roadmap -* [x] full wg-quick feature parity - * [x] PreUp - * [x] PostUp - * [x] PreDown - * [x] PostDown - * [x] DNS - * [x] MTU - * [x] Save --> Use MarshallText interface to save config -* [x] Sync -* [x] Up -* [x] Down -* [x] MarshallText -* [x] UnmarshallText -* [x] Minimal test -* [ ] Integration tests ((TODO; have some virtual machines/kvm and wreck havoc :) )) +- [x] full wg-quick feature parity + - [x] PreUp + - [x] PostUp + - [x] PreDown + - [x] PostDown + - [x] DNS + - [x] MTU + - [x] Save --> Use MarshallText interface to save config +- [x] Sync +- [x] Up +- [x] Down +- [x] MarshallText +- [x] UnmarshallText +- [x] Minimal test +- [ ] Integration tests ((TODO; have some virtual machines/kvm and wreck havoc :) )) # Caveats -* Endpoints DNS MarshallText is unsupported -* Pre/Post Up/Down doesn't support escaped `%i`, that is all `%i` are expanded to interface name. -* SaveConfig in config is only a placeholder (( since there's no reading/writing from files )). Use Unmarshall/Marshall Text to save/load config (( you're responsible for IO)). +- Endpoints DNS MarshallText is unsupported +- Pre/Post Up/Down doesn't support escaped `%i`, that is all `%i` are expanded to interface name. +- SaveConfig in config is only a placeholder (( since there's no reading/writing from files )). Use Unmarshall/Marshall Text to save/load config (( you're responsible for IO)). diff --git a/cmd/wg-quicker/main.go b/cmd/wg-quicker/main.go new file mode 100644 index 0000000..f65d675 --- /dev/null +++ b/cmd/wg-quicker/main.go @@ -0,0 +1,94 @@ +// nolint: gomnd +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "os" + + "github.com/sirupsen/logrus" + wgquick "go.xsfx.dev/wg-quicker" +) + +func printHelp() { + fmt.Print("wg-quick [flags] [ up | down | sync ] [ config_file | interface ]\n\n") // nolint: forbidigo + flag.Usage() + os.Exit(1) +} + +// nolint: funlen +func main() { + flag.String("iface", "", "interface") + verbose := flag.Bool("v", false, "verbose") + protocol := flag.Int("route-protocol", 0, "route protocol to use for our routes") + metric := flag.Int("route-metric", 0, "route metric to use for our routes") + flag.Parse() + args := flag.Args() + + if len(args) != 2 { + printHelp() + } + + if *verbose { + logrus.SetLevel(logrus.DebugLevel) + } + + 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") + } + + c.RouteProtocol = *protocol + c.RouteMetric = *metric + + 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") + } + case "sync": + if err := wgquick.Sync(c, iface, log); err != nil { + logrus.WithError(err).Errorln("cannot sync interface") + } + default: + printHelp() + } +}