chore: wiring startup with the flow [IN PROGRESS]

This commit is contained in:
braginini 2021-04-14 17:08:35 +02:00
parent 675358ce5c
commit cb60efef8d
6 changed files with 156 additions and 81 deletions

View File

@ -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
}

20
cmd/root.go Normal file
View File

@ -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)
}

109
cmd/up.go Normal file
View File

@ -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
}

View File

@ -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 <config file path>")
}
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
}

View File

@ -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
}

12
main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"github.com/wiretrustee/wiretrustee/cmd"
"os"
)
func main() {
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}