From 4f3799ac6586bf407503b640ed7c66648b83eedf Mon Sep 17 00:00:00 2001 From: braginini Date: Sat, 1 May 2021 16:03:43 +0200 Subject: [PATCH] feat: add addpeer cmd to add a remote Wireguard peer --- cmd/addpeer.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + 2 files changed, 46 insertions(+) create mode 100644 cmd/addpeer.go diff --git a/cmd/addpeer.go b/cmd/addpeer.go new file mode 100644 index 00000000000..0caaaeb0710 --- /dev/null +++ b/cmd/addpeer.go @@ -0,0 +1,45 @@ +package cmd + +import ( + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/wiretrustee/wiretrustee/connection" + "os" +) + +var ( + key string + allowedIPs string + + addPeerCmd = &cobra.Command{ + Use: "add-peer", + Short: "add remote peer", + Run: func(cmd *cobra.Command, args []string) { + InitLog(logLevel) + + if _, err := os.Stat(configPath); os.IsNotExist(err) { + log.Error("config doesn't exist, please run 'wiretrustee init' first") + os.Exit(ExitSetupFailed) + } + + config, _ := Read(configPath) + config.Peers = append(config.Peers, connection.Peer{ + WgPubKey: key, + WgAllowedIps: allowedIPs, + }) + + err := config.Write(configPath) + if err != nil { + log.Errorf("failed writing config to %s: %s", config, err.Error()) + os.Exit(ExitSetupFailed) + } + }, + } +) + +func init() { + addPeerCmd.PersistentFlags().StringVar(&key, "key", "", "Wireguard public key of the remote peer") + addPeerCmd.PersistentFlags().StringVar(&allowedIPs, "allowedIPs", "", "Wireguard Allowed IPs for the remote peer, e.g 10.30.30.2/32") + addPeerCmd.MarkPersistentFlagRequired("key") + addPeerCmd.MarkPersistentFlagRequired("allowedIPs") +} diff --git a/cmd/root.go b/cmd/root.go index 29a6e1383bc..014bd42e03b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,6 +33,7 @@ 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(addPeerCmd) rootCmd.AddCommand(upCmd) rootCmd.AddCommand(signalCmd) }