2021-04-14 17:08:35 +02:00
package cmd
import (
"context"
"fmt"
"github.com/pion/ice/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/wiretrustee/wiretrustee/engine"
2021-04-15 14:57:15 +02:00
sig "github.com/wiretrustee/wiretrustee/signal"
2021-04-14 17:08:35 +02:00
"os"
2021-04-15 14:57:15 +02:00
"os/signal"
"strings"
"syscall"
2021-04-14 17:08:35 +02:00
)
const (
ExitSetupFailed = 1
)
var (
cfgFile string
2021-04-15 14:57:15 +02:00
config = & Config { }
2021-04-14 17:08:35 +02:00
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 )
} * /
2021-04-15 14:57:15 +02:00
//c := defaultConfig()
2021-04-14 17:08:35 +02:00
//todo print config
//todo connect to signal
ctx := context . Background ( )
2021-04-15 14:57:15 +02:00
signalClient , err := sig . NewClient ( config . SignalAddr , ctx )
2021-04-14 17:08:35 +02:00
if err != nil {
2021-04-15 14:57:15 +02:00
log . Errorf ( "error while connecting to the Signal Exchange Service %s: %s" , config . SignalAddr , err )
2021-04-14 17:08:35 +02:00
os . Exit ( ExitSetupFailed )
}
//todo proper close handling
defer func ( ) { signalClient . Close ( ) } ( )
2021-04-15 14:57:15 +02:00
stunURL , _ := ice . ParseURL ( config . StunURL )
turnURL , _ := ice . ParseURL ( config . TurnURL )
turnURL . Password = config . TurnPwd
turnURL . Username = config . TurnUser
2021-04-14 17:08:35 +02:00
urls := [ ] * ice . URL { turnURL , stunURL }
2021-04-15 14:57:15 +02:00
engine := engine . NewEngine ( signalClient , urls , config . WgIface , config . WgAddr )
2021-04-14 17:08:35 +02:00
2021-04-15 14:57:15 +02:00
err = engine . Start ( config . PrivateKey , strings . Split ( config . Peers , "," ) )
2021-04-14 17:08:35 +02:00
2021-04-15 14:57:15 +02:00
//signalClient.WaitConnected()
2021-04-14 17:08:35 +02:00
2021-04-15 14:57:15 +02:00
SetupCloseHandler ( signalClient )
2021-04-14 17:08:35 +02:00
} ,
}
)
2021-04-15 14:57:15 +02:00
func SetupCloseHandler ( signalClient * sig . Client ) {
c := make ( chan os . Signal )
signal . Notify ( c , os . Interrupt , syscall . SIGTERM )
<- c
fmt . Println ( "\r- Ctrl+C pressed in Terminal" )
signalClient . Close ( )
os . Exit ( 0 )
}
2021-04-14 17:08:35 +02:00
func init ( ) {
2021-04-15 14:57:15 +02:00
//upCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.wiretrustee.yaml)")
upCmd . PersistentFlags ( ) . StringVar ( & config . WgAddr , "address" , "" , "IP address of a peer in CIDR notation (e.g. 10.30.30.1/24)" )
upCmd . PersistentFlags ( ) . StringVar ( & config . PrivateKey , "key" , "" , "Peers Wireguard private key" )
upCmd . PersistentFlags ( ) . StringVar ( & config . Peers , "peers" , "" , "A comma separated list of peers (Wireguard public keys) to connect to" )
upCmd . MarkPersistentFlagRequired ( "key" )
upCmd . MarkPersistentFlagRequired ( "ip" )
upCmd . MarkPersistentFlagRequired ( "peers" )
upCmd . PersistentFlags ( ) . StringVar ( & config . WgIface , "interface" , "wiretrustee0" , "Wireguard interface name" )
upCmd . PersistentFlags ( ) . StringVar ( & config . StunURL , "stun" , "stun:stun.wiretrustee.com:3468" , "A comma separated list of STUN servers including protocol (e.g. stun:stun.wiretrustee.com:3468" )
upCmd . PersistentFlags ( ) . StringVar ( & config . TurnURL , "turn" , "turn:stun.wiretrustee.com:3468" , "A comma separated list of TURN servers including protocol (e.g. stun:stun.wiretrustee.com:3468" )
upCmd . PersistentFlags ( ) . StringVar ( & config . TurnPwd , "turnUser" , "wiretrustee" , "A comma separated list of TURN servers including protocol (e.g. stun:stun.wiretrustee.com:3468" )
upCmd . PersistentFlags ( ) . StringVar ( & config . TurnUser , "turnPwd" , "wt2021hello@" , "A comma separated list of TURN servers including protocol (e.g. stun:stun.wiretrustee.com:3468" )
upCmd . PersistentFlags ( ) . StringVar ( & config . SignalAddr , "signal" , "signal.wiretrustee.com:10000" , "Signal server URL (e.g. signal.wiretrustee.com:10000" )
2021-04-14 17:08:35 +02:00
//upCmd.MarkPersistentFlagRequired("config")
fmt . Printf ( "" )
}
func defaultConfig ( ) * Config {
return & Config {
2021-04-15 14:57:15 +02:00
PrivateKey : "OCVgR9VJT4y4tBscRQ6SYHWocQlykUMCDI6APjp3ilY=" ,
Peers : "uRoZAk1g90WXXvazH0SS6URZ2/Kmhx+hbVhUt2ipzlU=" ,
2021-04-14 17:08:35 +02:00
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
}