From cb60efef8d95059e4a17e111f39863f01900bb88 Mon Sep 17 00:00:00 2001 From: braginini Date: Wed, 14 Apr 2021 17:08:35 +0200 Subject: [PATCH] chore: wiring startup with the flow [IN PROGRESS] --- cmd/{wiretrustee => }/config.go | 10 ++- cmd/root.go | 20 ++++++ cmd/up.go | 109 ++++++++++++++++++++++++++++++++ cmd/wiretrustee/cmd_start.go | 77 ---------------------- engine/engine.go | 9 ++- main.go | 12 ++++ 6 files changed, 156 insertions(+), 81 deletions(-) rename cmd/{wiretrustee => }/config.go (64%) create mode 100644 cmd/root.go create mode 100644 cmd/up.go delete mode 100644 cmd/wiretrustee/cmd_start.go create mode 100644 main.go diff --git a/cmd/wiretrustee/config.go b/cmd/config.go similarity index 64% rename from cmd/wiretrustee/config.go rename to cmd/config.go index f58891627..5231bdeeb 100644 --- a/cmd/wiretrustee/config.go +++ b/cmd/config.go @@ -1,4 +1,4 @@ -package wiretrustee +package cmd import "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -6,7 +6,13 @@ type Config struct { // Wireguard private key of local peer PrivateKey wgtypes.Key // configured remote peers (Wireguard public keys) - Peers string + Peers []string + StunURL string + TurnURL string + TurnUser string + TurnPwd string // host:port of the signal server SignalAddr string + WgAddr string + WgIface string } diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 000000000..61bb3737a --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,20 @@ +package cmd + +import "github.com/spf13/cobra" + +var ( + rootCmd = &cobra.Command{ + Use: "wiretrustee", + Short: "", + Long: "", + } +) + +// Execute executes the root command. +func Execute() error { + return rootCmd.Execute() +} + +func init() { + rootCmd.AddCommand(upCmd) +} diff --git a/cmd/up.go b/cmd/up.go new file mode 100644 index 000000000..e19d44f4b --- /dev/null +++ b/cmd/up.go @@ -0,0 +1,109 @@ +package cmd + +import ( + "context" + "fmt" + "github.com/pion/ice/v2" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/wiretrustee/wiretrustee/engine" + "github.com/wiretrustee/wiretrustee/signal" + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" + "os" +) + +const ( + ExitSetupFailed = 1 +) + +var ( + cfgFile string + + upCmd = &cobra.Command{ + Use: "up", + Short: "start wiretrustee", + Run: func(cmd *cobra.Command, args []string) { + /*config, err := ReadConfig("config.yml") + if err != nil { + log.Fatal("failed to load config") + os.Exit(ExitSetupFailed) + }*/ + + c := defaultConfig() + + //todo print config + + //todo connect to signal + ctx := context.Background() + signalClient, err := signal.NewClient(c.SignalAddr, ctx) + if err != nil { + log.Errorf("error while connecting to the Signal Exchange Service %s: %s", c.SignalAddr, err) + os.Exit(ExitSetupFailed) + } + //todo proper close handling + defer func() { signalClient.Close() }() + + stunURL, _ := ice.ParseURL(fmt.Sprintf("stun:%s", c.StunURL)) + turnURL, _ := ice.ParseURL(fmt.Sprintf("turn:%s", c.StunURL)) + turnURL.Password = c.TurnPwd + turnURL.Username = c.TurnUser + urls := []*ice.URL{turnURL, stunURL} + + s := c.PrivateKey.PublicKey().String() + + engine := engine.NewEngine(signalClient, urls, c.WgIface, c.WgAddr) + err = engine.Start(s, c.Peers) + + signalClient.WaitConnected() + + select {} + }, + } +) + +func init() { + upCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.wiretrustee.yaml)") + //upCmd.MarkPersistentFlagRequired("config") + fmt.Printf("") +} + +func defaultConfig() *Config { + + key, _ := wgtypes.ParseKey("OCVgR9VJT4y4tBscRQ6SYHWocQlykUMCDI6APjp3ilY=") + + return &Config{ + PrivateKey: key, + Peers: []string{"uRoZAk1g90WXXvazH0SS6URZ2/Kmhx+hbVhUt2ipzlU="}, + SignalAddr: "signal.wiretrustee.com:10000", + StunURL: "stun.wiretrustee.com:3468", + TurnURL: "stun.wiretrustee.com:3468", + TurnPwd: "wt2021hello@", + TurnUser: "wiretrustee", + WgAddr: "10.30.30.1/24", + WgIface: "wt0", + } +} + +func ReadConfig(path string) (*Config, error) { + /*f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + + bs, err := ioutil.ReadAll(f) + if err != nil { + return nil, err + } + + var cfg Config + + err = yaml.Unmarshal(bs, &cfg) + if err != nil { + return nil, err + } + + return &cfg, nil*/ + + return &Config{}, nil +} diff --git a/cmd/wiretrustee/cmd_start.go b/cmd/wiretrustee/cmd_start.go deleted file mode 100644 index 314329ac9..000000000 --- a/cmd/wiretrustee/cmd_start.go +++ /dev/null @@ -1,77 +0,0 @@ -package wiretrustee - -import ( - "context" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/wiretrustee/wiretrustee/signal" - "os" -) - -const ( - ExitSetupFailed = 1 -) - -var ( - opts struct { - config string - logLevel string - } -) - -func init() { - runCmd := &cobra.Command{ - Use: "start", - Short: "start wiretrustee", - Run: func(cmd *cobra.Command, args []string) { - config, err := ReadConfig("config.yml") - if err != nil { - log.Fatal("failed to load config") - os.Exit(ExitSetupFailed) - } - - //todo print config - - //todo connect to signal - ctx := context.Background() - signalClient, err := signal.NewClient(config.SignalAddr, ctx) - if err != nil { - log.Errorf("error while connecting to the Signal Exchange Service %s: %s", config.SignalAddr, err) - os.Exit(ExitSetupFailed) - } - //todo proper close handling - defer func() { signalClient.Close() }() - - signalClient.WaitConnected() - - select {} - }, - } - - // todo generate config if doesn't exist - runCmd.PersistentFlags().StringVar(&opts.config, "config", "", "--config ") -} - -func ReadConfig(path string) (*Config, error) { - /*f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() - - bs, err := ioutil.ReadAll(f) - if err != nil { - return nil, err - } - - var cfg Config - - err = yaml.Unmarshal(bs, &cfg) - if err != nil { - return nil, err - } - - return &cfg, nil*/ - - return &Config{}, nil -} diff --git a/engine/engine.go b/engine/engine.go index 5e10ab96e..abae0613c 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -24,10 +24,13 @@ type Engine struct { wgAddr string } -func NewEngine(signal *signal.Client, stunsTurns []*ice.URL) *Engine { +func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgAddr string) *Engine { return &Engine{ stunsTurns: stunsTurns, signal: signal, + wgIface: wgIface, + wgAddr: wgAddr, + agents: map[string]*PeerAgent{}, } } @@ -40,7 +43,7 @@ func (e *Engine) Start(localKey string, peers []string) error { return err } - err = iface.Create(e.wgIface, e.wgIface) + err = iface.Create(e.wgIface, e.wgAddr) if err != nil { log.Errorf("error while creating interface %s: [%s]", e.wgIface, err.Error()) return err @@ -70,6 +73,8 @@ func (e *Engine) Start(localKey string, peers []string) error { e.receiveSignal(localKey) + // todo send offer to each peer + return nil } diff --git a/main.go b/main.go new file mode 100644 index 000000000..0a71638c8 --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/wiretrustee/wiretrustee/cmd" + "os" +) + +func main() { + if err := cmd.Execute(); err != nil { + os.Exit(1) + } +}