mirror of
https://github.com/netbirdio/netbird.git
synced 2025-01-22 13:58:55 +01:00
fix: signal key transfer
This commit is contained in:
parent
cb60efef8d
commit
303f955e32
@ -1,12 +1,10 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// Wireguard private key of local peer
|
// Wireguard private key of local peer
|
||||||
PrivateKey wgtypes.Key
|
PrivateKey string
|
||||||
// configured remote peers (Wireguard public keys)
|
// configured remote peers (Wireguard public keys)
|
||||||
Peers []string
|
Peers string
|
||||||
StunURL string
|
StunURL string
|
||||||
TurnURL string
|
TurnURL string
|
||||||
TurnUser string
|
TurnUser string
|
||||||
|
60
cmd/up.go
60
cmd/up.go
@ -7,9 +7,11 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/wiretrustee/wiretrustee/engine"
|
"github.com/wiretrustee/wiretrustee/engine"
|
||||||
"github.com/wiretrustee/wiretrustee/signal"
|
sig "github.com/wiretrustee/wiretrustee/signal"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -19,6 +21,8 @@ const (
|
|||||||
var (
|
var (
|
||||||
cfgFile string
|
cfgFile string
|
||||||
|
|
||||||
|
config = &Config{}
|
||||||
|
|
||||||
upCmd = &cobra.Command{
|
upCmd = &cobra.Command{
|
||||||
Use: "up",
|
Use: "up",
|
||||||
Short: "start wiretrustee",
|
Short: "start wiretrustee",
|
||||||
@ -29,51 +33,69 @@ var (
|
|||||||
os.Exit(ExitSetupFailed)
|
os.Exit(ExitSetupFailed)
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
c := defaultConfig()
|
//c := defaultConfig()
|
||||||
|
|
||||||
//todo print config
|
//todo print config
|
||||||
|
|
||||||
//todo connect to signal
|
//todo connect to signal
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
signalClient, err := signal.NewClient(c.SignalAddr, ctx)
|
signalClient, err := sig.NewClient(config.SignalAddr, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error while connecting to the Signal Exchange Service %s: %s", c.SignalAddr, err)
|
log.Errorf("error while connecting to the Signal Exchange Service %s: %s", config.SignalAddr, err)
|
||||||
os.Exit(ExitSetupFailed)
|
os.Exit(ExitSetupFailed)
|
||||||
}
|
}
|
||||||
//todo proper close handling
|
//todo proper close handling
|
||||||
defer func() { signalClient.Close() }()
|
defer func() { signalClient.Close() }()
|
||||||
|
|
||||||
stunURL, _ := ice.ParseURL(fmt.Sprintf("stun:%s", c.StunURL))
|
stunURL, _ := ice.ParseURL(config.StunURL)
|
||||||
turnURL, _ := ice.ParseURL(fmt.Sprintf("turn:%s", c.StunURL))
|
turnURL, _ := ice.ParseURL(config.TurnURL)
|
||||||
turnURL.Password = c.TurnPwd
|
turnURL.Password = config.TurnPwd
|
||||||
turnURL.Username = c.TurnUser
|
turnURL.Username = config.TurnUser
|
||||||
urls := []*ice.URL{turnURL, stunURL}
|
urls := []*ice.URL{turnURL, stunURL}
|
||||||
|
|
||||||
s := c.PrivateKey.PublicKey().String()
|
engine := engine.NewEngine(signalClient, urls, config.WgIface, config.WgAddr)
|
||||||
|
|
||||||
engine := engine.NewEngine(signalClient, urls, c.WgIface, c.WgAddr)
|
err = engine.Start(config.PrivateKey, strings.Split(config.Peers, ","))
|
||||||
err = engine.Start(s, c.Peers)
|
|
||||||
|
|
||||||
signalClient.WaitConnected()
|
//signalClient.WaitConnected()
|
||||||
|
|
||||||
select {}
|
SetupCloseHandler(signalClient)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
upCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.wiretrustee.yaml)")
|
//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")
|
||||||
//upCmd.MarkPersistentFlagRequired("config")
|
//upCmd.MarkPersistentFlagRequired("config")
|
||||||
fmt.Printf("")
|
fmt.Printf("")
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfig() *Config {
|
func defaultConfig() *Config {
|
||||||
|
|
||||||
key, _ := wgtypes.ParseKey("OCVgR9VJT4y4tBscRQ6SYHWocQlykUMCDI6APjp3ilY=")
|
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
PrivateKey: key,
|
PrivateKey: "OCVgR9VJT4y4tBscRQ6SYHWocQlykUMCDI6APjp3ilY=",
|
||||||
Peers: []string{"uRoZAk1g90WXXvazH0SS6URZ2/Kmhx+hbVhUt2ipzlU="},
|
Peers: "uRoZAk1g90WXXvazH0SS6URZ2/Kmhx+hbVhUt2ipzlU=",
|
||||||
SignalAddr: "signal.wiretrustee.com:10000",
|
SignalAddr: "signal.wiretrustee.com:10000",
|
||||||
StunURL: "stun.wiretrustee.com:3468",
|
StunURL: "stun.wiretrustee.com:3468",
|
||||||
TurnURL: "stun.wiretrustee.com:3468",
|
TurnURL: "stun.wiretrustee.com:3468",
|
||||||
|
@ -21,7 +21,7 @@ type PeerAgent struct {
|
|||||||
// Actual peer-to-peer connection
|
// Actual peer-to-peer connection
|
||||||
conn *ice.Conn
|
conn *ice.Conn
|
||||||
// a signal.Client to negotiate initial connection
|
// a signal.Client to negotiate initial connection
|
||||||
signal signal.Client
|
signal *signal.Client
|
||||||
// a connection to a local Wireguard instance to proxy data
|
// a connection to a local Wireguard instance to proxy data
|
||||||
wgConn net.Conn
|
wgConn net.Conn
|
||||||
// an address of local Wireguard instance
|
// an address of local Wireguard instance
|
||||||
@ -29,7 +29,7 @@ type PeerAgent struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPeerAgent creates a new PeerAgent with give local and remote Wireguard public keys and initializes an ICE Agent
|
// NewPeerAgent creates a new PeerAgent with give local and remote Wireguard public keys and initializes an ICE Agent
|
||||||
func NewPeerAgent(localKey string, remoteKey string, stunTurnURLS []*ice.URL, wgAddr string) (*PeerAgent, error) {
|
func NewPeerAgent(localKey string, remoteKey string, stunTurnURLS []*ice.URL, wgAddr string, signal *signal.Client) (*PeerAgent, error) {
|
||||||
|
|
||||||
// init ICE Agent
|
// init ICE Agent
|
||||||
iceAgent, err := ice.NewAgent(&ice.AgentConfig{
|
iceAgent, err := ice.NewAgent(&ice.AgentConfig{
|
||||||
@ -47,6 +47,7 @@ func NewPeerAgent(localKey string, remoteKey string, stunTurnURLS []*ice.URL, wg
|
|||||||
wgAddr: wgAddr,
|
wgAddr: wgAddr,
|
||||||
conn: nil,
|
conn: nil,
|
||||||
wgConn: nil,
|
wgConn: nil,
|
||||||
|
signal: signal,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = peerAgent.onConnectionStateChange()
|
err = peerAgent.onConnectionStateChange()
|
||||||
@ -217,6 +218,24 @@ func (pa *PeerAgent) onConnectionStateChange() error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pa *PeerAgent) Start() error {
|
||||||
|
localUFrag, localPwd, err := pa.iceAgent.GetLocalUserCredentials()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
offer := signal.MarshalCredential(pa.LocalKey, pa.RemoteKey, &signal.Credential{
|
||||||
|
UFrag: localUFrag,
|
||||||
|
Pwd: localPwd}, sProto.Message_OFFER)
|
||||||
|
|
||||||
|
err = pa.signal.Send(offer)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// authenticate sets the signal.Credential of the remote peer
|
// authenticate sets the signal.Credential of the remote peer
|
||||||
// and returns local Credentials
|
// and returns local Credentials
|
||||||
func (pa *PeerAgent) Authenticate(credential *signal.Credential) (*signal.Credential, error) {
|
func (pa *PeerAgent) Authenticate(credential *signal.Credential) (*signal.Credential, error) {
|
||||||
|
@ -34,12 +34,13 @@ func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) Start(localKey string, peers []string) error {
|
func (e *Engine) Start(privateKey string, peers []string) error {
|
||||||
|
|
||||||
// setup wireguard
|
// setup wireguard
|
||||||
myKey, err := wgtypes.ParseKey(localKey)
|
myKey, err := wgtypes.ParseKey(privateKey)
|
||||||
|
myPubKey := myKey.PublicKey().String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error parsing Wireguard key %s: [%s]", localKey, err.Error())
|
log.Errorf("error parsing Wireguard key %s: [%s]", privateKey, err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,17 +64,23 @@ func (e *Engine) Start(localKey string, peers []string) error {
|
|||||||
|
|
||||||
// initialize peer agents
|
// initialize peer agents
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
peerAgent, err := NewPeerAgent(localKey, peer, e.stunsTurns, fmt.Sprintf("127.0.0.1:%d", *wgPort))
|
peerAgent, err := NewPeerAgent(myPubKey, peer, e.stunsTurns, fmt.Sprintf("127.0.0.1:%d", *wgPort), e.signal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed creating peer agent for pair %s - %s", localKey, peer)
|
log.Fatalf("failed creating peer agent for pair %s - %s", myPubKey, peer)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
e.agents[localKey] = peerAgent
|
e.agents[myPubKey] = peerAgent
|
||||||
}
|
}
|
||||||
|
|
||||||
e.receiveSignal(localKey)
|
e.receiveSignal(myPubKey)
|
||||||
|
|
||||||
// todo send offer to each peer
|
for _, pa := range e.agents {
|
||||||
|
err := pa.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed starting agent %s %s", myPubKey, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user