Skip to content

Commit

Permalink
chore: wiring startup with the flow [IN PROGRESS]
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed Apr 14, 2021
1 parent 675358c commit cb60efe
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 81 deletions.
10 changes: 8 additions & 2 deletions cmd/wiretrustee/config.go → cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package wiretrustee
package cmd

import "golang.zx2c4.com/wireguard/wgctrl/wgtypes"

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 changes: 20 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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 changes: 109 additions & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
@@ -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
}
77 changes: 0 additions & 77 deletions cmd/wiretrustee/cmd_start.go

This file was deleted.

9 changes: 7 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
}

Expand All @@ -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
Expand Down Expand Up @@ -70,6 +73,8 @@ func (e *Engine) Start(localKey string, peers []string) error {

e.receiveSignal(localKey)

// todo send offer to each peer

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/wiretrustee/wiretrustee/cmd"
"os"
)

func main() {
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit cb60efe

Please sign in to comment.