2021-10-06 03:02:09 +08:00
|
|
|
package relay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
"github.com/gravitl/netmaker/functions"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
)
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// GetNodeRelay - gets the relay node of a given network
|
2021-10-06 03:02:09 +08:00
|
|
|
func GetNodeRelay(network string, relayedNodeAddr string) (models.Node, error) {
|
|
|
|
collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
|
|
|
|
var relay models.Node
|
|
|
|
if err != nil {
|
|
|
|
if database.IsEmptyRecord(err) {
|
|
|
|
return relay, nil
|
|
|
|
}
|
|
|
|
functions.PrintUserLog("", err.Error(), 2)
|
|
|
|
return relay, err
|
|
|
|
}
|
|
|
|
for _, value := range collection {
|
|
|
|
err := json.Unmarshal([]byte(value), &relay)
|
|
|
|
if err != nil {
|
|
|
|
functions.PrintUserLog("", err.Error(), 2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if relay.IsRelay == "yes" {
|
|
|
|
for _, addr := range relay.RelayAddrs {
|
|
|
|
if addr == relayedNodeAddr {
|
|
|
|
return relay, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relay, errors.New("could not find relay for node " + relayedNodeAddr)
|
|
|
|
}
|