2021-08-15 16:56:26 +02:00
|
|
|
package internal
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
import (
|
2021-09-07 18:36:46 +02:00
|
|
|
"context"
|
2021-05-01 12:45:37 +02:00
|
|
|
"fmt"
|
2021-11-06 15:00:13 +01:00
|
|
|
"github.com/pion/ice/v2"
|
2021-05-01 12:45:37 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-10 18:43:13 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/client/internal/peer"
|
|
|
|
"github.com/wiretrustee/wiretrustee/client/internal/proxy"
|
2021-05-01 12:45:37 +02:00
|
|
|
"github.com/wiretrustee/wiretrustee/iface"
|
2021-08-15 16:56:26 +02:00
|
|
|
mgm "github.com/wiretrustee/wiretrustee/management/client"
|
|
|
|
mgmProto "github.com/wiretrustee/wiretrustee/management/proto"
|
2021-08-09 19:21:48 +02:00
|
|
|
signal "github.com/wiretrustee/wiretrustee/signal/client"
|
2021-05-01 12:45:37 +02:00
|
|
|
sProto "github.com/wiretrustee/wiretrustee/signal/proto"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2022-01-01 14:03:03 +01:00
|
|
|
"math/rand"
|
2021-08-15 16:56:26 +02:00
|
|
|
"strings"
|
2021-07-19 15:02:11 +02:00
|
|
|
"sync"
|
2021-05-01 12:45:37 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-01-01 14:03:03 +01:00
|
|
|
// PeerConnectionTimeoutMax is a timeout of an initial connection attempt to a remote peer.
|
|
|
|
// E.g. this peer will wait PeerConnectionTimeoutMax for the remote peer to respond, if not successful then it will retry the connection attempt.
|
2022-01-17 14:01:58 +01:00
|
|
|
// Todo pass timeout at EnginConfig
|
2022-01-10 18:43:13 +01:00
|
|
|
const PeerConnectionTimeoutMax = 45000 //ms
|
|
|
|
const PeerConnectionTimeoutMin = 30000 //ms
|
|
|
|
|
|
|
|
const WgPort = 51820
|
2021-07-19 15:02:11 +02:00
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
// EngineConfig is a config for the Engine
|
|
|
|
type EngineConfig struct {
|
2022-01-17 14:01:58 +01:00
|
|
|
WgPort int
|
|
|
|
WgIfaceName string
|
2021-08-15 16:56:26 +02:00
|
|
|
// WgAddr is a Wireguard local address (Wiretrustee Network IP)
|
|
|
|
WgAddr string
|
|
|
|
// WgPrivateKey is a Wireguard private key of our peer (it MUST never leave the machine)
|
|
|
|
WgPrivateKey wgtypes.Key
|
|
|
|
// IFaceBlackList is a list of network interfaces to ignore when discovering connection candidates (ICE related)
|
|
|
|
IFaceBlackList map[string]struct{}
|
2021-11-21 17:47:19 +01:00
|
|
|
|
|
|
|
PreSharedKey *wgtypes.Key
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Engine is a mechanism responsible for reacting on Signal and Management stream events and managing connections to the remote peers.
|
2021-05-01 12:45:37 +02:00
|
|
|
type Engine struct {
|
2021-08-15 16:56:26 +02:00
|
|
|
// signal is a Signal Service client
|
2021-05-01 12:45:37 +02:00
|
|
|
signal *signal.Client
|
2021-08-15 16:56:26 +02:00
|
|
|
// mgmClient is a Management Service client
|
|
|
|
mgmClient *mgm.Client
|
2022-01-10 18:43:13 +01:00
|
|
|
// peerConns is a map that holds all the peers that are known to this peer
|
|
|
|
peerConns map[string]*peer.Conn
|
2021-08-15 16:56:26 +02:00
|
|
|
|
|
|
|
// syncMsgMux is used to guarantee sequential Management Service message processing
|
|
|
|
syncMsgMux *sync.Mutex
|
|
|
|
|
|
|
|
config *EngineConfig
|
2021-09-03 17:47:40 +02:00
|
|
|
// STUNs is a list of STUN servers used by ICE
|
|
|
|
STUNs []*ice.URL
|
|
|
|
// TURNs is a list of STUN servers used by ICE
|
|
|
|
TURNs []*ice.URL
|
2021-09-07 18:36:46 +02:00
|
|
|
|
|
|
|
cancel context.CancelFunc
|
2021-10-17 22:15:38 +02:00
|
|
|
|
|
|
|
ctx context.Context
|
2022-01-17 14:01:58 +01:00
|
|
|
|
|
|
|
wgInterface iface.WGIface
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:23:56 +02:00
|
|
|
// Peer is an instance of the Connection Peer
|
2021-05-01 12:45:37 +02:00
|
|
|
type Peer struct {
|
|
|
|
WgPubKey string
|
|
|
|
WgAllowedIps string
|
|
|
|
}
|
|
|
|
|
2021-05-15 12:23:56 +02:00
|
|
|
// NewEngine creates a new Connection Engine
|
2021-10-17 22:15:38 +02:00
|
|
|
func NewEngine(signalClient *signal.Client, mgmClient *mgm.Client, config *EngineConfig, cancel context.CancelFunc, ctx context.Context) *Engine {
|
2021-05-01 12:45:37 +02:00
|
|
|
return &Engine{
|
2021-08-15 16:56:26 +02:00
|
|
|
signal: signalClient,
|
|
|
|
mgmClient: mgmClient,
|
2022-01-10 18:43:13 +01:00
|
|
|
peerConns: map[string]*peer.Conn{},
|
2021-08-15 16:56:26 +02:00
|
|
|
syncMsgMux: &sync.Mutex{},
|
|
|
|
config: config,
|
2021-09-06 14:23:03 +02:00
|
|
|
STUNs: []*ice.URL{},
|
|
|
|
TURNs: []*ice.URL{},
|
2021-09-07 18:36:46 +02:00
|
|
|
cancel: cancel,
|
2021-10-17 22:15:38 +02:00
|
|
|
ctx: ctx,
|
2021-09-07 18:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) Stop() error {
|
2022-01-10 18:43:13 +01:00
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
|
|
|
|
2021-10-17 22:15:38 +02:00
|
|
|
err := e.removeAllPeerConnections()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
log.Debugf("removing Wiretrustee interface %s", e.config.WgIfaceName)
|
|
|
|
if e.wgInterface.Interface != nil {
|
|
|
|
err = e.wgInterface.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed closing Wiretrustee interface %s %v", e.config.WgIfaceName, err)
|
|
|
|
return err
|
|
|
|
}
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
2021-09-07 18:36:46 +02:00
|
|
|
|
2021-10-17 22:15:38 +02:00
|
|
|
log.Infof("stopped Wiretrustee Engine")
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
return nil
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
// Start creates a new Wireguard tunnel interface and listens to events from Signal and Management services
|
|
|
|
// Connections to remote peers are not established here.
|
|
|
|
// However, they will be established once an event with a list of peers to connect to will be received from Management Service
|
|
|
|
func (e *Engine) Start() error {
|
2022-01-10 18:43:13 +01:00
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
wgIfaceName := e.config.WgIfaceName
|
2021-08-15 16:56:26 +02:00
|
|
|
wgAddr := e.config.WgAddr
|
|
|
|
myPrivateKey := e.config.WgPrivateKey
|
2022-01-17 14:01:58 +01:00
|
|
|
var err error
|
|
|
|
|
|
|
|
e.wgInterface, err = iface.NewWGIface(wgIfaceName, wgAddr, iface.DefaultMTU)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed creating wireguard interface instance %s: [%s]", wgIfaceName, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
err = e.wgInterface.Create()
|
2021-05-01 12:45:37 +02:00
|
|
|
if err != nil {
|
2022-01-17 14:01:58 +01:00
|
|
|
log.Errorf("failed creating tunnel interface %s: [%s]", wgIfaceName, err.Error())
|
2021-05-01 12:45:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
err = e.wgInterface.Configure(myPrivateKey.String(), e.config.WgPort)
|
2021-05-01 12:45:37 +02:00
|
|
|
if err != nil {
|
2022-01-17 14:01:58 +01:00
|
|
|
log.Errorf("failed configuring Wireguard interface [%s]: %s", wgIfaceName, err.Error())
|
2021-05-01 12:45:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
e.receiveSignalEvents()
|
|
|
|
e.receiveManagementEvents()
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:11:33 +01:00
|
|
|
func (e *Engine) removePeers(peers []string) error {
|
2022-01-10 18:43:13 +01:00
|
|
|
for _, p := range peers {
|
|
|
|
err := e.removePeer(p)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
log.Infof("removed peer %s", p)
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-17 22:15:38 +02:00
|
|
|
func (e *Engine) removeAllPeerConnections() error {
|
|
|
|
log.Debugf("removing all peer connections")
|
2022-01-10 18:43:13 +01:00
|
|
|
for p := range e.peerConns {
|
|
|
|
err := e.removePeer(p)
|
2021-10-17 22:15:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:11:33 +01:00
|
|
|
// removePeer closes an existing peer connection and removes a peer
|
|
|
|
func (e *Engine) removePeer(peerKey string) error {
|
2022-01-10 18:43:13 +01:00
|
|
|
log.Debugf("removing peer from engine %s", peerKey)
|
|
|
|
conn, exists := e.peerConns[peerKey]
|
|
|
|
if exists {
|
|
|
|
delete(e.peerConns, peerKey)
|
2021-07-19 15:02:11 +02:00
|
|
|
return conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeerConnectionStatus returns a connection Status or nil if peer connection wasn't found
|
2022-01-10 18:43:13 +01:00
|
|
|
func (e *Engine) GetPeerConnectionStatus(peerKey string) peer.ConnStatus {
|
2021-07-19 15:02:11 +02:00
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
conn, exists := e.peerConns[peerKey]
|
2021-07-19 15:02:11 +02:00
|
|
|
if exists && conn != nil {
|
2022-01-10 18:43:13 +01:00
|
|
|
return conn.Status()
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
return -1
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
// GetConnectedPeers returns a connection Status or nil if peer connection wasn't found
|
|
|
|
func (e *Engine) GetConnectedPeers() []string {
|
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
peers := []string{}
|
|
|
|
for s, conn := range e.peerConns {
|
|
|
|
if conn.Status() == peer.StatusConnected {
|
|
|
|
peers = append(peers, s)
|
|
|
|
}
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
return peers
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func signalCandidate(candidate ice.Candidate, myKey wgtypes.Key, remoteKey wgtypes.Key, s *signal.Client) error {
|
|
|
|
err := s.Send(&sProto.Message{
|
|
|
|
Key: myKey.PublicKey().String(),
|
|
|
|
RemoteKey: remoteKey.String(),
|
2021-05-01 18:29:59 +02:00
|
|
|
Body: &sProto.Body{
|
|
|
|
Type: sProto.Body_CANDIDATE,
|
|
|
|
Payload: candidate.Marshal(),
|
|
|
|
},
|
2021-05-01 12:45:37 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed signaling candidate to the remote peer %s %s", remoteKey.String(), err)
|
|
|
|
//todo ??
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func signalAuth(uFrag string, pwd string, myKey wgtypes.Key, remoteKey wgtypes.Key, s *signal.Client, isAnswer bool) error {
|
|
|
|
|
2021-05-01 18:29:59 +02:00
|
|
|
var t sProto.Body_Type
|
2021-05-01 12:45:37 +02:00
|
|
|
if isAnswer {
|
2021-05-01 18:29:59 +02:00
|
|
|
t = sProto.Body_ANSWER
|
2021-05-01 12:45:37 +02:00
|
|
|
} else {
|
2021-05-01 18:29:59 +02:00
|
|
|
t = sProto.Body_OFFER
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 18:29:59 +02:00
|
|
|
msg, err := signal.MarshalCredential(myKey, remoteKey, &signal.Credential{
|
2021-05-01 12:45:37 +02:00
|
|
|
UFrag: uFrag,
|
|
|
|
Pwd: pwd}, t)
|
2021-05-15 12:23:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-01 18:29:59 +02:00
|
|
|
err = s.Send(msg)
|
2021-05-01 12:45:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
// receiveManagementEvents connects to the Management Service event stream to receive updates from the management service
|
|
|
|
// E.g. when a new peer has been registered and we are allowed to connect to it.
|
|
|
|
func (e *Engine) receiveManagementEvents() {
|
2021-09-07 18:36:46 +02:00
|
|
|
go func() {
|
|
|
|
err := e.mgmClient.Sync(func(update *mgmProto.SyncResponse) error {
|
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
|
|
|
|
|
|
|
if update.GetWiretrusteeConfig() != nil {
|
|
|
|
err := e.updateTURNs(update.GetWiretrusteeConfig().GetTurns())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = e.updateSTUNs(update.GetWiretrusteeConfig().GetStuns())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//todo update signal
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
if update.GetRemotePeers() != nil || update.GetRemotePeersIsEmpty() {
|
|
|
|
// empty arrays are serialized by protobuf to null, but for our case empty array is a valid state.
|
|
|
|
err := e.updatePeers(update.GetRemotePeers())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
return nil
|
|
|
|
})
|
2021-09-03 17:47:40 +02:00
|
|
|
if err != nil {
|
2021-11-06 15:00:13 +01:00
|
|
|
// happens if management is unavailable for a long time.
|
|
|
|
// We want to cancel the operation of the whole client
|
2021-09-07 18:36:46 +02:00
|
|
|
e.cancel()
|
|
|
|
return
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2021-10-17 22:15:38 +02:00
|
|
|
log.Debugf("stopped receiving updates from Management Service")
|
2021-09-07 18:36:46 +02:00
|
|
|
}()
|
|
|
|
log.Debugf("connecting to Management Service updates stream")
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-03 17:47:40 +02:00
|
|
|
func (e *Engine) updateSTUNs(stuns []*mgmProto.HostConfig) error {
|
|
|
|
if len(stuns) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var newSTUNs []*ice.URL
|
|
|
|
log.Debugf("got STUNs update from Management Service, updating")
|
|
|
|
for _, stun := range stuns {
|
|
|
|
url, err := ice.ParseURL(stun.Uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newSTUNs = append(newSTUNs, url)
|
|
|
|
}
|
|
|
|
e.STUNs = newSTUNs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) updateTURNs(turns []*mgmProto.ProtectedHostConfig) error {
|
|
|
|
if len(turns) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var newTURNs []*ice.URL
|
|
|
|
log.Debugf("got TURNs update from Management Service, updating")
|
|
|
|
for _, turn := range turns {
|
|
|
|
url, err := ice.ParseURL(turn.HostConfig.Uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
url.Username = turn.User
|
|
|
|
url.Password = turn.Password
|
|
|
|
newTURNs = append(newTURNs, url)
|
|
|
|
}
|
|
|
|
e.TURNs = newTURNs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) updatePeers(remotePeers []*mgmProto.RemotePeerConfig) error {
|
2021-12-31 18:11:33 +01:00
|
|
|
log.Debugf("got peers update from Management Service, total peers to connect to = %d", len(remotePeers))
|
2021-09-03 17:47:40 +02:00
|
|
|
remotePeerMap := make(map[string]struct{})
|
2022-01-10 18:43:13 +01:00
|
|
|
for _, p := range remotePeers {
|
|
|
|
remotePeerMap[p.GetWgPubKey()] = struct{}{}
|
2021-09-03 17:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//remove peers that are no longer available for us
|
|
|
|
toRemove := []string{}
|
2022-01-10 18:43:13 +01:00
|
|
|
for p := range e.peerConns {
|
2021-09-03 17:47:40 +02:00
|
|
|
if _, ok := remotePeerMap[p]; !ok {
|
|
|
|
toRemove = append(toRemove, p)
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 18:11:33 +01:00
|
|
|
err := e.removePeers(toRemove)
|
2021-09-03 17:47:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new peers
|
2022-01-10 18:43:13 +01:00
|
|
|
for _, p := range remotePeers {
|
|
|
|
peerKey := p.GetWgPubKey()
|
|
|
|
peerIPs := p.GetAllowedIps()
|
|
|
|
if _, ok := e.peerConns[peerKey]; !ok {
|
|
|
|
conn, err := e.createPeerConn(peerKey, strings.Join(peerIPs, ","))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.peerConns[peerKey] = conn
|
|
|
|
|
|
|
|
go e.connWorker(conn, peerKey)
|
2021-09-03 17:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
func (e Engine) connWorker(conn *peer.Conn, peerKey string) {
|
|
|
|
for {
|
|
|
|
|
|
|
|
// randomize starting time a bit
|
|
|
|
min := 500
|
|
|
|
max := 2000
|
|
|
|
time.Sleep(time.Duration(rand.Intn(max-min)+min) * time.Millisecond)
|
|
|
|
|
|
|
|
// if peer has been removed -> give up
|
|
|
|
if !e.peerExists(peerKey) {
|
|
|
|
log.Infof("peer %s doesn't exist anymore, won't retry connection", peerKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !e.signal.Ready() {
|
|
|
|
log.Infof("signal client isn't ready, skipping connection attempt %s", peerKey)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := conn.Open()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("connection to peer %s failed: %v", peerKey, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Engine) peerExists(peerKey string) bool {
|
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
|
|
|
_, ok := e.peerConns[peerKey]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Engine) createPeerConn(pubKey string, allowedIPs string) (*peer.Conn, error) {
|
|
|
|
|
|
|
|
var stunTurn []*ice.URL
|
|
|
|
stunTurn = append(stunTurn, e.STUNs...)
|
|
|
|
stunTurn = append(stunTurn, e.TURNs...)
|
|
|
|
|
|
|
|
interfaceBlacklist := make([]string, 0, len(e.config.IFaceBlackList))
|
|
|
|
for k := range e.config.IFaceBlackList {
|
|
|
|
interfaceBlacklist = append(interfaceBlacklist, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyConfig := proxy.Config{
|
|
|
|
RemoteKey: pubKey,
|
|
|
|
WgListenAddr: fmt.Sprintf("127.0.0.1:%d", e.config.WgPort),
|
2022-01-17 14:01:58 +01:00
|
|
|
WgInterface: e.wgInterface,
|
2022-01-10 18:43:13 +01:00
|
|
|
AllowedIps: allowedIPs,
|
|
|
|
PreSharedKey: e.config.PreSharedKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomize connection timeout
|
|
|
|
timeout := time.Duration(rand.Intn(PeerConnectionTimeoutMax-PeerConnectionTimeoutMin)+PeerConnectionTimeoutMin) * time.Millisecond
|
|
|
|
config := peer.ConnConfig{
|
|
|
|
Key: pubKey,
|
|
|
|
LocalKey: e.config.WgPrivateKey.PublicKey().String(),
|
|
|
|
StunTurn: stunTurn,
|
|
|
|
InterfaceBlackList: interfaceBlacklist,
|
|
|
|
Timeout: timeout,
|
|
|
|
ProxyConfig: proxyConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
peerConn, err := peer.NewConn(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wgPubKey, err := wgtypes.ParseKey(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signalOffer := func(uFrag string, pwd string) error {
|
|
|
|
return signalAuth(uFrag, pwd, e.config.WgPrivateKey, wgPubKey, e.signal, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
signalCandidate := func(candidate ice.Candidate) error {
|
|
|
|
return signalCandidate(candidate, e.config.WgPrivateKey, wgPubKey, e.signal)
|
|
|
|
}
|
|
|
|
|
|
|
|
signalAnswer := func(uFrag string, pwd string) error {
|
|
|
|
return signalAuth(uFrag, pwd, e.config.WgPrivateKey, wgPubKey, e.signal, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerConn.SetSignalCandidate(signalCandidate)
|
|
|
|
peerConn.SetSignalOffer(signalOffer)
|
|
|
|
peerConn.SetSignalAnswer(signalAnswer)
|
|
|
|
|
|
|
|
return peerConn, nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
// receiveSignalEvents connects to the Signal Service event stream to negotiate connection with remote peers
|
|
|
|
func (e *Engine) receiveSignalEvents() {
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2021-11-06 15:00:13 +01:00
|
|
|
go func() {
|
|
|
|
// connect to a stream of messages coming from the signal server
|
|
|
|
err := e.signal.Receive(func(msg *sProto.Message) error {
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2021-11-06 15:00:13 +01:00
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
conn := e.peerConns[msg.Key]
|
2021-11-06 15:00:13 +01:00
|
|
|
if conn == nil {
|
|
|
|
return fmt.Errorf("wrongly addressed message %s", msg.Key)
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-06 15:00:13 +01:00
|
|
|
switch msg.GetBody().Type {
|
|
|
|
case sProto.Body_OFFER:
|
|
|
|
remoteCred, err := signal.UnMarshalCredential(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
conn.OnRemoteOffer(peer.IceCredentials{
|
|
|
|
UFrag: remoteCred.UFrag,
|
|
|
|
Pwd: remoteCred.Pwd,
|
2021-11-06 15:00:13 +01:00
|
|
|
})
|
|
|
|
case sProto.Body_ANSWER:
|
|
|
|
remoteCred, err := signal.UnMarshalCredential(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
conn.OnRemoteAnswer(peer.IceCredentials{
|
|
|
|
UFrag: remoteCred.UFrag,
|
|
|
|
Pwd: remoteCred.Pwd,
|
2021-11-06 15:00:13 +01:00
|
|
|
})
|
|
|
|
case sProto.Body_CANDIDATE:
|
|
|
|
candidate, err := ice.UnmarshalCandidate(msg.GetBody().Payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed on parsing remote candidate %s -> %s", candidate, err)
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
conn.OnRemoteCandidate(candidate)
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-06 15:00:13 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// happens if signal is unavailable for a long time.
|
|
|
|
// We want to cancel the operation of the whole client
|
|
|
|
e.cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2021-11-06 15:00:13 +01:00
|
|
|
e.signal.WaitStreamConnected()
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|