Skip to content

Commit

Permalink
feat: add wiretrustee init cmd to initialize config
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed May 1, 2021
1 parent 2b77da4 commit ff225a4
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 23 deletions.
109 changes: 109 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cmd

import (
"github.com/pion/ice/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"os"
"strings"
)

var (
wgKey string
wgInterface string
wgLocalAddr string
signalAddr string
stunURLs string
turnURLs string

initCmd = &cobra.Command{
Use: "init",
Short: "init wiretrustee",
Run: func(cmd *cobra.Command, args []string) {
InitLog(logLevel)

if wgKey == "" {
wgKey = generateKey()
}

var stunTurnURLs []*ice.URL
stuns := strings.Split(stunURLs, ",")
for _, url := range stuns {

parsedURL, err := ice.ParseURL(url)
if err != nil {
log.Errorf("failed parsing STUN URL %s: %s", url, err.Error())
os.Exit(ExitSetupFailed)
}
stunTurnURLs = append(stunTurnURLs, parsedURL)
}

turns := strings.Split(turnURLs, ",")
for _, url := range turns {

var urlToParse string
var user string
var pwd string
//extract user:password from user:password@proto:host:port
urlSplit := strings.Split(url, "@")
if len(urlSplit) == 2 {
urlToParse = urlSplit[1]
credential := strings.Split(urlSplit[0], ":")
user = credential[0]
pwd = credential[1]
} else {
urlToParse = url
}

parsedURL, err := ice.ParseURL(urlToParse)
if err != nil {
log.Errorf("failed parsing TURN URL %s: %s", url, err.Error())
os.Exit(ExitSetupFailed)
}
parsedURL.Username = user
parsedURL.Password = pwd
stunTurnURLs = append(stunTurnURLs, parsedURL)
}

config := &Config{
PrivateKey: wgKey,
Peers: nil,
StunTurnURLs: stunTurnURLs,
SignalAddr: signalAddr,
WgAddr: wgLocalAddr,
WgIface: wgInterface,
}

err := config.Write(configPath)
if err != nil {
log.Errorf("failed writing config to %s: %s", config, err.Error())
os.Exit(ExitSetupFailed)
}
},
}
)

func init() {
initCmd.PersistentFlags().StringVar(&wgKey, "wgKey", "", "Wireguard private key, if not specified a new one will be generated")
initCmd.PersistentFlags().StringVar(&wgInterface, "wgInterface", "wiretrustee0", "Wireguard interface name, e.g. wiretreustee0 or wg0")
initCmd.PersistentFlags().StringVar(&wgLocalAddr, "wgLocalAddr", "", "Wireguard local address, e.g. 10.30.30.1/24")
initCmd.PersistentFlags().StringVar(&signalAddr, "signalAddr", "", "Signal server address, e.g. signal.wiretrustee.com:10000")
initCmd.PersistentFlags().StringVar(&stunURLs, "stunURLs", "", "Comma separated STUN server URLs: protocol:host:port, e.g. stun:stun.l.google.com:19302,stun:stun1.l.google.com:19302")
//todo user:password@protocol:host:port not the best way to pass TURN credentials, do it according to https://tools.ietf.org/html/rfc7065 E.g. use oauth
initCmd.PersistentFlags().StringVar(&turnURLs, "turnURLs", "", "Comma separated TURN server URLs: user:password@protocol:host:port, e.g. user:password@turn:stun.wiretrustee.com:3468")
//initCmd.MarkPersistentFlagRequired("configPath")
initCmd.MarkPersistentFlagRequired("wgLocalAddr")
initCmd.MarkPersistentFlagRequired("signalAddr")
initCmd.MarkPersistentFlagRequired("stunURLs")
initCmd.MarkPersistentFlagRequired("turnURLs")
}

// generateKey generates a new Wireguard private key
func generateKey() string {
key, err := wgtypes.GenerateKey()
if err != nil {
panic(err)
}
return key.String()
}
20 changes: 20 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package cmd

import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"os/signal"
"syscall"
)

const (
ExitSetupFailed = 1
)

var (
configPath string
logLevel string

rootCmd = &cobra.Command{
Use: "wiretrustee",
Short: "",
Expand All @@ -22,6 +30,9 @@ func Execute() error {
}

func init() {
rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/wiretrustee/config.json", "Wiretrustee config file location to write new config to")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "")
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(upCmd)
rootCmd.AddCommand(signalCmd)
}
Expand All @@ -33,3 +44,12 @@ func SetupCloseHandler() {
fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0)
}

func InitLog(logLevel string) {
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Errorf("efailed parsing log-level %s: %s", logLevel, err)
os.Exit(ExitSetupFailed)
}
log.SetLevel(level)
}
17 changes: 2 additions & 15 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@ import (
"os"
)

const (
ExitSetupFailed = 1
)

var (
configPath string
logLevel string

upCmd = &cobra.Command{
Use: "up",
Short: "start wiretrustee",
Run: func(cmd *cobra.Command, args []string) {
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Errorf("efailed parsing log-level %s: %s", logLevel, err)
os.Exit(ExitSetupFailed)
}
log.SetLevel(level)
InitLog(logLevel)

config, _ := Read(configPath)

Expand All @@ -51,7 +39,6 @@ var (
)

func init() {
upCmd.PersistentFlags().StringVar(&configPath, "config", "", "")
upCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "")
//upCmd.PersistentFlags().StringVar(&configPath, "config", "", "")
upCmd.MarkPersistentFlagRequired("config")
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.16

require (
github.com/cenkalti/backoff/v4 v4.1.0
github.com/golang/protobuf v1.4.2
github.com/golang/protobuf v1.4.3
github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31
github.com/pion/ice/v2 v2.0.17
github.com/pion/ice/v2 v2.1.7
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.3
github.com/vishvananda/netlink v1.1.0
Expand Down
Loading

0 comments on commit ff225a4

Please sign in to comment.