Skip to content

Commit

Permalink
Merge pull request #17 from influxdata/wirey-version
Browse files Browse the repository at this point in the history
Wirey version
  • Loading branch information
fntlnz authored May 4, 2018
2 parents 3b788f2 + f515fa4 commit e9a5075
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

32 changes: 21 additions & 11 deletions backend/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,7 +36,8 @@ func NewHTTPBackend(baseurl string) (*HTTPBackend, error) {
Timeout: time.Second * 10,
Transport: transportWithTimeout,
},
baseurl: baseurl,
baseurl: baseurl,
wireyVersion: wireyVersion,
}, nil
}

Expand All @@ -49,16 +54,15 @@ 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 {
return err
}
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 {
Expand All @@ -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 {
Expand All @@ -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)
}
}
6 changes: 4 additions & 2 deletions cmd/wirey/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions cmd/wirey/version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e9a5075

Please sign in to comment.