Added initial binary wg-quick equivalent

This commit is contained in:
Neven Miculinic 2019-03-28 16:05:43 +01:00
parent 5f01824316
commit 587a7db470
2 changed files with 83 additions and 5 deletions

View File

@ -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

68
cmd/wg-quick/main.go Normal file
View File

@ -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")
}
}
}