From f515fa49703605584a8743767938db9a6d7ae877 Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Fri, 4 May 2018 12:06:38 +0200 Subject: [PATCH] Wirey user agent Signed-off-by: Lorenzo Fontana Version command Signed-off-by: Lorenzo Fontana No need to change that Signed-off-by: Lorenzo Fontana --- Makefile | 5 ++++- backend/http.go | 32 +++++++++++++++++++++----------- cmd/wirey/root.go | 6 ++++-- cmd/wirey/version.go | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 cmd/wirey/version.go diff --git a/Makefile b/Makefile index 59eadc1..7246b21 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ SHELL := /bin/bash +COMMIT_NO := $(shell git rev-parse --short=7 HEAD 2> /dev/null || true) +GIT_COMMIT := $(if $(shell git status --porcelain --untracked-files=no),"${COMMIT_NO}-dirty","${COMMIT_NO}") +LDFLAGS=-ldflags "-s -X main.Version=${GIT_COMMIT}" all: build .PHONY: build build: mkdir -p bin - go build -o bin/wirey ./cmd/wirey + go build ${LDFLAGS} -o bin/wirey ./cmd/wirey diff --git a/backend/http.go b/backend/http.go index c04238f..6ee5a6d 100644 --- a/backend/http.go +++ b/backend/http.go @@ -10,17 +10,21 @@ import ( "time" ) +const httpUserAgent = "wirey" + type BasicAuth struct { Username string Password string } + type HTTPBackend struct { - client *http.Client - baseurl string - BasicAuth *BasicAuth + client *http.Client + baseurl string + BasicAuth *BasicAuth + wireyVersion string } -func NewHTTPBackend(baseurl string) (*HTTPBackend, error) { +func NewHTTPBackend(baseurl, wireyVersion string) (*HTTPBackend, error) { var transportWithTimeout = &http.Transport{ Dial: (&net.Dialer{ Timeout: 5 * time.Second, @@ -32,7 +36,8 @@ func NewHTTPBackend(baseurl string) (*HTTPBackend, error) { Timeout: time.Second * 10, Transport: transportWithTimeout, }, - baseurl: baseurl, + baseurl: baseurl, + wireyVersion: wireyVersion, }, nil } @@ -49,6 +54,7 @@ func (b *HTTPBackend) Join(ifname string, p Peer) error { if err != nil { return err } + buf := bytes.NewBuffer(jsonPeer) req, err := http.NewRequest("POST", joinURL, buf) if err != nil { @@ -56,9 +62,7 @@ func (b *HTTPBackend) Join(ifname string, p Peer) error { } req.Header.Add("Content-Type", "application/json") - if b.BasicAuth != nil { - req.SetBasicAuth(b.BasicAuth.Username, b.BasicAuth.Password) - } + injectCommonHeaders(req, b.wireyVersion, b.BasicAuth) res, err := b.client.Do(req) if err != nil { @@ -79,9 +83,7 @@ func (b *HTTPBackend) GetPeers(ifname string) ([]Peer, error) { return nil, err } - if b.BasicAuth != nil { - req.SetBasicAuth(b.BasicAuth.Username, b.BasicAuth.Password) - } + injectCommonHeaders(req, b.wireyVersion, b.BasicAuth) res, err := b.client.Do(req) if err != nil { @@ -101,3 +103,11 @@ func (b *HTTPBackend) GetPeers(ifname string) ([]Peer, error) { return peers, nil } + +func injectCommonHeaders(req *http.Request, wireyVersion string, basicAuth *BasicAuth) { + req.Header.Add("User-Agent", fmt.Sprintf("%s/%s", httpUserAgent, wireyVersion)) + + if basicAuth != nil { + req.SetBasicAuth(basicAuth.Username, basicAuth.Password) + } +} diff --git a/cmd/wirey/root.go b/cmd/wirey/root.go index 0d06427..69eb90f 100644 --- a/cmd/wirey/root.go +++ b/cmd/wirey/root.go @@ -13,6 +13,8 @@ import ( "github.com/spf13/viper" ) +var Version string + var rootCmd = &cobra.Command{ Use: "wirey", Short: "manage local wireguard interfaces in a distributed system", @@ -69,7 +71,7 @@ func backendFactory() (backend.Backend, error) { httpBackend := viper.GetString("http") if len(httpBackend) != 0 { - b, err := backend.NewHTTPBackend(httpBackend) + b, err := backend.NewHTTPBackend(httpBackend, Version) if err != nil { return nil, err } @@ -110,7 +112,7 @@ func init() { pflags.String("httpbasicauth", "", "basic auth for the http backend, in form username:password") pflags.String("ifname", "wg0", "the name to use for the interface (must be the same in all the peers)") pflags.String("ipaddr", "", "the ip for this node inside the tunnel, e.g: 10.0.0.3") - pflags.String("peerdiscoveryttl", "5s", "the time to wait to discover new peers using the configured backend") + pflags.String("peerdiscoveryttl", "30s", "the time to wait to discover new peers using the configured backend") pflags.String("privatekeypath", "/etc/wirey/privkey", "the local path where to load the private key from, if empty, a private key will be generated.") rootCmd.MarkFlagRequired("endpoint") diff --git a/cmd/wirey/version.go b/cmd/wirey/version.go new file mode 100644 index 0000000..8aaf017 --- /dev/null +++ b/cmd/wirey/version.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "print the current wirey version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("%s\n", Version) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +}