Skip to content

Commit

Permalink
NET-725: FailOvers (#637)
Browse files Browse the repository at this point in the history
* signal to relay the host behind NAT if unreachable

* use relays to traverse over NAT when PRO

* check conn status only peer are not connected

* failover using nodes data

* ignore signal if peer doesn't exist

* clear retained msg on peer signal mq update

* add timestamps to signals
  • Loading branch information
abhishek9686 authored Nov 29, 2023
1 parent a9844b0 commit 95a2ced
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 51 deletions.
4 changes: 4 additions & 0 deletions functions/mqhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func HostUpdate(client mqtt.Client, msg mqtt.Message) {
slog.Error("server not found in config", "server", serverName)
return
}
if len(msg.Payload()) == 0 {
return
}
data, err := decryptMsg(serverName, msg.Payload())
if err != nil {
slog.Error("error decrypting message", "error", err)
Expand Down Expand Up @@ -270,6 +273,7 @@ func HostUpdate(client mqtt.Client, msg mqtt.Message) {
slog.Error("failed to response with ACK to server", "server", serverName, "error", err)
}
case models.SignalHost:
clearRetainedMsg(client, msg.Topic())
turn.PeerSignalCh <- hostUpdate.Signal
case models.UpdateKeys:
clearRetainedMsg(client, msg.Topic()) // clear message
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/google/nftables v0.1.0
github.com/google/uuid v1.4.0
github.com/gorilla/websocket v1.5.1
github.com/gravitl/netmaker v0.21.2-0.20231031062230-896041e44236
github.com/gravitl/netmaker v0.21.3-0.20231122123620-290b393ae58b
github.com/gravitl/tcping v0.1.2-0.20230801110928-546055ebde06
github.com/gravitl/txeh v0.0.0-20230509181318-3778c58bd69f
github.com/guumaster/hostctl v1.1.4
Expand All @@ -23,15 +23,16 @@ require (
github.com/minio/selfupdate v0.6.0
github.com/pion/logging v0.2.2
github.com/pion/turn/v2 v2.1.3
github.com/sagikazarmark/slog-shim v0.1.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.1.0
github.com/wailsapp/wails/v2 v2.6.0
golang.design/x/clipboard v0.7.0
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.15.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.17.0
golang.org/x/net v0.18.0
golang.org/x/sys v0.14.0
golang.org/x/term v0.14.0
golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c
Expand Down Expand Up @@ -59,7 +60,7 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
Expand Down Expand Up @@ -99,7 +100,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
Expand All @@ -122,7 +122,7 @@ require (
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24=
github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -188,8 +188,8 @@ github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gravitl/netmaker v0.21.2-0.20231031062230-896041e44236 h1:OnXwXGbeMR2f0qJja2zo/axyUouJDEyXCcg0rc2qqWk=
github.com/gravitl/netmaker v0.21.2-0.20231031062230-896041e44236/go.mod h1:XSuEtCr4ZBT2mZlTHVqTZhCPlXWq6W+iEILwC5yuLtw=
github.com/gravitl/netmaker v0.21.3-0.20231122123620-290b393ae58b h1:eTnizt7GB48KnIOuaMSmiHZMcsiCBnKAZnChodNLDwE=
github.com/gravitl/netmaker v0.21.3-0.20231122123620-290b393ae58b/go.mod h1:AXjK913dwW2ZL0X5pGPaPp9VGL7xALymQ94QaLuuVJ4=
github.com/gravitl/tcping v0.1.2-0.20230801110928-546055ebde06 h1:g2fBXRNT9eiQohyHcoME3SVmeG7OKoJPWrs7A+009kU=
github.com/gravitl/tcping v0.1.2-0.20230801110928-546055ebde06/go.mod h1:12iViYKWAzRPj5/oEGAaD7Wje+Nuz8M9eDJbV7qhKAA=
github.com/gravitl/txeh v0.0.0-20230509181318-3778c58bd69f h1:XzsYovKdrDvj2z2HEHoeHU67+JIEFMHQKHU6oU+1fVE=
Expand Down Expand Up @@ -400,8 +400,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -487,8 +487,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -593,8 +593,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
4 changes: 2 additions & 2 deletions nmproxy/nm-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func Start(ctx context.Context, waitg *sync.WaitGroup) {
time.Sleep(time.Second * 2) // add a delay for clients to send turn register message to server
turn.Init(ctx, proxyWaitG, turnCfgs)
defer turn.DissolvePeerConnections()
proxyWaitG.Add(1)
go turn.WatchPeerConnections(ctx, proxyWaitG)
}
proxyWaitG.Add(1)
go turn.WatchPeerConnections(ctx, proxyWaitG)
proxyWaitG.Wait()
}
34 changes: 34 additions & 0 deletions nmproxy/turn/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
nm_models "github.com/gravitl/netmaker/models"
"github.com/pion/logging"
"github.com/pion/turn/v2"
"github.com/sagikazarmark/slog-shim"
"gortc.io/stun"
)

Expand Down Expand Up @@ -119,6 +120,38 @@ func allocateAddr(client *turn.Client) (net.PacketConn, error) {
return relayConn, nil
}

// failOverMe - signals the server to failOver ME
func failOverMe(serverName, nodeID, peernodeID string) error {
server := ncconfig.GetServer(serverName)
if server == nil {
return errors.New("server config not found")
}
host := ncconfig.Netclient()
if host == nil {
return fmt.Errorf("no configured host found")
}
token, err := auth.Authenticate(server, host)
if err != nil {
return err
}
endpoint := httpclient.JSONEndpoint[nm_models.SuccessResponse, nm_models.ErrorResponse]{
URL: "https://" + server.API,
Route: fmt.Sprintf("/api/v1/node/%s/failover_me", nodeID),
Method: http.MethodPost,
Data: nm_models.FailOverMeReq{NodeID: peernodeID},
Authorization: "Bearer " + token,
ErrorResponse: nm_models.ErrorResponse{},
}
_, errData, err := endpoint.GetJSON(nm_models.SuccessResponse{}, nm_models.ErrorResponse{})
if err != nil {
if errors.Is(err, httpclient.ErrStatus) {
slog.Error("error asking server to relay me", "code", strconv.Itoa(errData.Code), "error", errData.Message)
}
return err
}
return nil
}

// SignalPeer - signals the peer with host's turn relay endpoint
func SignalPeer(serverName string, signal nm_models.Signal) error {
server := ncconfig.GetServer(serverName)
Expand Down Expand Up @@ -251,6 +284,7 @@ func startTurnListener(ctx context.Context, wg *sync.WaitGroup, serverName strin
TurnRelayEndpoint: turnConn.LocalAddr().String(),
ToHostPubKey: peerKey,
Action: nm_models.ConnNegotiation,
TimeStamp: time.Now().Unix(),
})
if err != nil {
logger.Log(0, "failed to signal peer: ", err.Error())
Expand Down
Loading

0 comments on commit 95a2ced

Please sign in to comment.