Skip to content

Commit

Permalink
added udp holepunching
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiszli committed Jul 25, 2021
1 parent fd223e3 commit 4a6ebfc
Show file tree
Hide file tree
Showing 10 changed files with 2,758 additions and 2,099 deletions.
25 changes: 21 additions & 4 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ package controller
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
"github.com/gravitl/netmaker/serverctl"
"golang.org/x/crypto/bcrypt"
)

func GetPeersList(networkName string) ([]models.PeersResponse, error) {

var peers []models.PeersResponse
collection, err := database.FetchRecords(database.NODES_TABLE_NAME)

if err != nil {
return peers, err
udppeers, errN := serverctl.GetPeers(networkName)
if errN != nil {
log.Println(errN)
}

for _, value := range collection {
var node models.Node
var peer models.PeersResponse
Expand All @@ -33,9 +36,23 @@ func GetPeersList(networkName string) ([]models.PeersResponse, error) {
continue
}
if node.Network == networkName && !node.IsPending {
if node.UDPHolePunch == "yes" && errN == nil {
endpointstring := udppeers[peer.PublicKey]
endpointarr := strings.Split(endpointstring, ":")
if len(endpointarr) == 2 {
port, err := strconv.Atoi(endpointarr[1])
if err == nil {
peer.Endpoint = endpointarr[0]
peer.ListenPort = int32(port)
}
}
}
peers = append(peers, peer)
}
}
if err != nil {
return peers, err
}

return peers, err
}
Expand Down
58 changes: 6 additions & 52 deletions controllers/nodeGrpcController.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,63 +59,12 @@ func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.ReadNodeRe
Islocal: network.IsLocal == "yes",
Isdualstack: network.IsDualStack == "yes",
Localrange: network.LocalRange,
Udpholepunch: node.UDPHolePunch,
},
}
return response, nil
}

/*
func (s *NodeServiceServer) GetConn(ctx context.Context, data *nodepb.Client) (*nodepb.Client, error) {
// Get the protobuf node type from the protobuf request type
// Essentially doing req.Node to access the struct with a nil check
// Now we have to convert this into a NodeItem type to convert into BSON
clientreq := models.IntClient{
// ID: primitive.NilObjectID,
Address: data.GetAddress(),
Address6: data.GetAddress6(),
AccessKey: data.GetAccesskey(),
PublicKey: data.GetPublickey(),
PrivateKey: data.GetPrivatekey(),
ServerPort: data.GetServerport(),
ServerKey: data.GetServerkey(),
ServerWGEndpoint: data.GetServerwgendpoint(),
}
//Check to see if key is valid
//TODO: Triple inefficient!!! This is the third call to the DB we make for networks
if servercfg.IsRegisterKeyRequired() {
validKey := functions.IsKeyValidGlobal(clientreq.AccessKey)
if !validKey {
return nil, status.Errorf(
codes.Internal,
fmt.Sprintf("Invalid key, and server does not allow no-key signups"),
)
}
}
client, err := RegisterIntClient(clientreq)
if err != nil {
// return internal gRPC error to be handled later
return nil, status.Errorf(
codes.Internal,
fmt.Sprintf("Internal error: %v", err),
)
}
// return the node in a CreateNodeRes type
response := &nodepb.Client{
Privatekey: client.PrivateKey,
Publickey: client.PublicKey,
Accesskey: client.AccessKey,
Address: client.Address,
Address6: client.Address6,
Serverwgendpoint: client.ServerWGEndpoint,
Serverport: client.ServerPort,
Serverkey: client.ServerKey,
}
return response, nil
}
*/
func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNodeReq) (*nodepb.CreateNodeRes, error) {
// Get the protobuf node type from the protobuf request type
// Essentially doing req.Node to access the struct with a nil check
Expand All @@ -137,6 +86,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNo
IsPending: data.GetIspending(),
PublicKey: data.GetPublickey(),
ListenPort: data.GetListenport(),
UDPHolePunch: data.GetUdpholepunch(),
}

err := node.Validate(false)
Expand Down Expand Up @@ -197,6 +147,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNo
Islocal: network.IsLocal == "yes",
Isdualstack: network.IsDualStack == "yes",
Localrange: network.LocalRange,
Udpholepunch: node.UDPHolePunch,
},
}
err = SetNetworkNodesLastModified(node.Network)
Expand Down Expand Up @@ -225,6 +176,7 @@ func (s *NodeServiceServer) CheckIn(ctx context.Context, req *nodepb.CheckInReq)
ListenPort: data.GetListenport(),
PersistentKeepalive: data.GetKeepalive(),
PublicKey: data.GetPublickey(),
UDPHolePunch: data.GetUdpholepunch(),
}

checkinresponse, err := NodeCheckIn(node, node.Network)
Expand Down Expand Up @@ -274,6 +226,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
IsPending: data.GetIspending(),
PublicKey: data.GetPublickey(),
ListenPort: data.GetListenport(),
UDPHolePunch: data.GetUdpholepunch(),
}

// Convert the Id string to a MongoDB ObjectId
Expand Down Expand Up @@ -323,6 +276,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
Islocal: network.IsLocal == "yes",
Isdualstack: network.IsDualStack == "yes",
Localrange: network.LocalRange,
Udpholepunch: newnode.UDPHolePunch,
},
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gravitl/netmaker
go 1.15

require (
github.com/aws/aws-sdk-go v1.34.28
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-playground/validator/v10 v10.5.0
Expand Down
Loading

0 comments on commit 4a6ebfc

Please sign in to comment.