Skip to content

Commit

Permalink
Merge pull request #166 from gravitl/feature_v0.5_clientgrpc
Browse files Browse the repository at this point in the history
Feature v0.5 clientgrpc
  • Loading branch information
afeiszli authored Jun 2, 2021
2 parents 77d0ed7 + f757b9e commit 4c3b7c1
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 161 deletions.
2 changes: 1 addition & 1 deletion compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
ports:
- "80:80"
environment:
BACKEND_URL: "http://3.235.190.90:8081"
BACKEND_URL: "http://HOST_IP:8081"
coredns:
depends_on:
- netmaker
Expand Down
2 changes: 1 addition & 1 deletion config/dnsconfig/netmaker.hosts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.10.10.1 nethub.default
10.10.10.1 netmaker.default
26 changes: 15 additions & 11 deletions controllers/intClientHttpController.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package controller

import (
// "fmt"
// "fmt"
// "github.com/davecgh/go-spew/spew"
"errors"
"context"
"encoding/json"
Expand All @@ -21,7 +22,6 @@ func intClientHandlers(r *mux.Router) {
r.HandleFunc("/api/intclient/{clientid}", securityCheck(http.HandlerFunc(getIntClient))).Methods("GET")
r.HandleFunc("/api/intclients", securityCheck(http.HandlerFunc(getAllIntClients))).Methods("GET")
r.HandleFunc("/api/intclients/deleteall", securityCheck(http.HandlerFunc(deleteAllIntClients))).Methods("DELETE")
r.HandleFunc("/api/intclient/{clientid}", securityCheck(http.HandlerFunc(deleteIntClient))).Methods("DELETE")
r.HandleFunc("/api/intclient/{clientid}", securityCheck(http.HandlerFunc(updateIntClient))).Methods("PUT")
r.HandleFunc("/api/intclient/register", http.HandlerFunc(registerIntClient)).Methods("POST")
r.HandleFunc("/api/intclient/{clientid}", http.HandlerFunc(deleteIntClient)).Methods("DELETE")
Expand Down Expand Up @@ -138,25 +138,29 @@ func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
client.Address = newAddress
}
if client.Network == "" { client.Network = "comms" }

wgconfig := servercfg.GetWGConfig()
client.ServerPublicEndpoint = servercfg.GetAPIHost()
client.ServerAPIPort = servercfg.GetAPIPort()
client.ServerPrivateAddress = wgconfig.GRPCWGAddress
client.ServerWGPort = wgconfig.GRPCWGPort
client.ServerGRPCPort = servercfg.GetGRPCPort()
server, err := serverctl.GetServerWGConf()
//spew.Dump(server)
if err != nil {
return client, err
}
client.ServerPublicEndpoint = server.ServerPublicEndpoint
client.ServerAPIPort = server.ServerAPIPort
client.ServerPrivateAddress = server.ServerPrivateAddress
client.ServerWGPort = server.ServerWGPort
client.ServerGRPCPort = server.ServerGRPCPort
client.ServerKey = server.ServerKey

if client.ClientID == "" {
clientid := StringWithCharset(7, charset)
clientname := "client-" + clientid
client.ClientID = clientname
}


//spew.Dump(client)
collection := mongoconn.Client.Database("netmaker").Collection("intclients")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// insert our network into the network table
_, err := collection.InsertOne(ctx, client)
_, err = collection.InsertOne(ctx, client)
defer cancel()

if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions controllers/nodeGrpcController.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controller
import (
"context"
"fmt"

"log"
"github.com/gravitl/netmaker/functions"
nodepb "github.com/gravitl/netmaker/grpc"
"github.com/gravitl/netmaker/models"
Expand Down Expand Up @@ -162,7 +162,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNo
if err != nil {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not find network: %v", err))
} else {
fmt.Println("Creating node in network " + network.NetID)
log.Println("Creating node in network " + network.NetID)
}

if !validKey {
Expand Down Expand Up @@ -356,7 +356,6 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
}

func (s *NodeServiceServer) DeleteNode(ctx context.Context, req *nodepb.DeleteNodeReq) (*nodepb.DeleteNodeRes, error) {
fmt.Println("beginning node delete")
macaddress := req.GetMacaddress()
network := req.GetNetworkName()

Expand Down
27 changes: 26 additions & 1 deletion controllers/serverHttpController.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func serverHandlers(r *mux.Router) {
r.HandleFunc("/api/server/addnetwork/{network}", securityCheckServer(http.HandlerFunc(addNetwork))).Methods("POST")
r.HandleFunc("/api/server/getconfig", securityCheckServer(http.HandlerFunc(getConfig))).Methods("GET")
r.HandleFunc("/api/server/getwgconfig", securityCheckServer(http.HandlerFunc(getWGConfig))).Methods("GET")
r.HandleFunc("/api/server/removenetwork/{network}", securityCheckServer(http.HandlerFunc(removeNetwork))).Methods("DELETE")
}

Expand Down Expand Up @@ -84,11 +85,35 @@ func getConfig(w http.ResponseWriter, r *http.Request) {

// get params

scfg := servercfg.GetConfig()
scfg := servercfg.GetServerConfig()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(scfg)
}

func getWGConfig(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")

// get params

wgcfg := servercfg.GetWGConfig()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(wgcfg)
}

/*
func getMongoConfig(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")
// get params
mcfg := servercfg.GetMongoConfig()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(mcfg)
}
*/

func addNetwork(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")
Expand Down
3 changes: 1 addition & 2 deletions functions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func CreateServerToken(netID string) (string, error) {
if *network.IsLocal {
privAddr = network.LocalRange
}

accessstringdec := " " + "|"+ address + "|" + address + "|" + netID + "|" + accesskey.Value + "|" + privAddr
accessstringdec := address + "|"+ address + "|" + address + "|" + netID + "|" + accesskey.Value + "|" + privAddr

accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec))

Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/gravitl/netmaker/serverctl"
"github.com/gravitl/netmaker/mongoconn"
"github.com/gravitl/netmaker/functions"
"fmt"
"os"
"os/exec"
"net"
Expand All @@ -33,12 +32,12 @@ func main() {
output, err := cmd.Output()

if err != nil {
fmt.Println("Error running 'id -u' for prereq check. Please investigate or disable client mode.")
log.Println("Error running 'id -u' for prereq check. Please investigate or disable client mode.")
log.Fatal(err)
}
i, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil {
fmt.Println("Error retrieving uid from 'id -u' for prereq check. Please investigate or disable client mode.")
log.Println("Error retrieving uid from 'id -u' for prereq check. Please investigate or disable client mode.")
log.Fatal(err)
}
if i != 0 {
Expand Down Expand Up @@ -147,7 +146,7 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) {
log.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")

if installserver {
fmt.Println("Adding server to default network")
log.Println("Adding server to default network")
success, err := serverctl.AddNetwork("default")
if err != nil {
log.Printf("Error adding to default network: %v", err)
Expand Down
28 changes: 14 additions & 14 deletions models/intclient.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package models

type IntClient struct {
ClientID string `json:"clientid" bson:"clientid"`
PrivateKey string `json:"privatekey" bson:"privatekey"`
PublicKey string `json:"publickey" bson:"publickey"`
AccessKey string `json:"accesskey" bson:"accesskey"`
Address string `json:"address" bson:"address"`
Address6 string `json:"address6" bson:"address6"`
Network string `json:"network" bson:"network"`
ServerPublicEndpoint string `json:"serverwgendpoint" bson:"serverwgendpoint"`
ServerAPIPort string `json:"serverapiendpoint" bson:"serverapiendpoint"`
ServerPrivateAddress string `json:"serveraddress" bson:"serveraddress"`
ServerWGPort string `json:"serverport" bson:"serverport"`
ServerGRPCPort string `json:"serverport" bson:"serverport"`
ServerKey string `json:"serverkey" bson:"serverkey"`
IsServer string `json:"isserver" bson:"isserver"`
ClientID string `json:"clientid" bson:"clientid"`
PrivateKey string `json:"privatekey" bson:"privatekey"`
PublicKey string `json:"publickey" bson:"publickey"`
AccessKey string `json:"accesskey" bson:"accesskey"`
Address string `json:"address" bson:"address"`
Address6 string `json:"address6" bson:"address6"`
Network string `json:"network" bson:"network"`
ServerPublicEndpoint string `json:"serverpublicendpoint" bson:"serverpublicendpoint"`
ServerAPIPort string `json:"serverapiport" bson:"serverapiport"`
ServerPrivateAddress string `json:"serverprivateaddress" bson:"serverprivateaddress"`
ServerWGPort string `json:"serverwgport" bson:"serverwgport"`
ServerGRPCPort string `json:"servergrpcport" bson:"servergrpcport"`
ServerKey string `json:"serverkey" bson:"serverkey"`
IsServer string `json:"isserver" bson:"isserver"`
}
4 changes: 0 additions & 4 deletions netclient/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ func Uninstall(cfg config.GlobalConfig) error {
err = functions.Unregister(cfg)
return err
}
func Reregister(cfg config.GlobalConfig) error {
err := functions.Reregister(cfg)
return err
}
func Unregister(cfg config.GlobalConfig) error {
err := functions.Unregister(cfg)
return err
Expand Down
49 changes: 28 additions & 21 deletions netclient/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ func ModGlobalConfig(cfg models.IntClient) error{
if cfg.ServerKey != ""{
modconfig.Client.ServerKey = cfg.ServerKey
}
if cfg.AccessKey != ""{
modconfig.Client.AccessKey = cfg.AccessKey
}
if cfg.ClientID != ""{
modconfig.Client.ClientID = cfg.ClientID
}

err = WriteGlobal(&modconfig)
return err
}
Expand Down Expand Up @@ -369,13 +376,14 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, error){
}
token := string(tokenbytes)
tokenvals := strings.Split(token, "|")
cfg.Server.GRPCAddress = tokenvals[1]
cfg.Server.APIAddress = tokenvals[2]
cfg.Network = tokenvals[3]
cfg.Node.Network = tokenvals[4]
cfg.Server.AccessKey = tokenvals[5]
cfg.Node.LocalRange = tokenvals[6]

cfg.Server.GRPCAddress = tokenvals[1]
cfg.Network = tokenvals[3]
cfg.Node.Network = tokenvals[3]
cfg.Server.AccessKey = tokenvals[4]
if len(tokenvals) > 5 {
cfg.Node.LocalRange = tokenvals[5]
}
if c.String("grpcserver") != "" {
cfg.Server.GRPCAddress = c.String("grpcserver")
}
Expand Down Expand Up @@ -405,22 +413,22 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, error){
cfg.Node.Password = c.String("password")
cfg.Node.MacAddress = c.String("macaddress")
cfg.Node.LocalAddress = c.String("localaddress")
cfg.Node.LocalRange = c.String("localrange")
cfg.Node.WGAddress = c.String("address")
cfg.Node.WGAddress6 = c.String("addressIPV6")
cfg.Node.Roaming = c.String("")
cfg.Node.DNS = c.String("")
cfg.Node.IsLocal = c.String("")
cfg.Node.IsDualStack = c.String("")
cfg.Node.IsIngressGateway = c.String("")
cfg.Node.PostUp = c.String("")
cfg.Node.PostDown = c.String("")
cfg.Node.Port = int32(c.Int(""))
cfg.Node.KeepAlive = int32(c.Int(""))
cfg.Node.PublicKey = c.String("")
cfg.Node.PrivateKey = c.String("")
cfg.Node.Endpoint = c.String("")
cfg.Node.IPForwarding = c.String("")
cfg.Node.Roaming = c.String("roaming")
cfg.Node.DNS = c.String("dns")
cfg.Node.IsLocal = c.String("islocal")
cfg.Node.IsDualStack = c.String("isdualstack")
cfg.Node.PostUp = c.String("postup")
cfg.Node.PostDown = c.String("postdown")
cfg.Node.Port = int32(c.Int("port"))
cfg.Node.KeepAlive = int32(c.Int("keepalive"))
cfg.Node.PublicKey = c.String("publickey")
cfg.Node.PrivateKey = c.String("privatekey")
cfg.Node.Endpoint = c.String("endpoint")
cfg.Node.IPForwarding = c.String("ipforwarding")
cfg.OperatingSystem = c.String("operatingsystem")
cfg.Daemon = c.String("daemon")

return cfg, nil
}
Expand Down Expand Up @@ -531,4 +539,3 @@ func FileExists(f string) bool {
}
return !info.IsDir()
}

14 changes: 8 additions & 6 deletions netclient/functions/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,17 @@ func LeaveNetwork(network string) error {
)
if err != nil {
log.Printf("Encountered error deleting node: %v", err)
fmt.Println(err)
log.Println(err)
} else {
fmt.Println("delete node " + node.MacAddress + "from remote server on network " + node.Network)
log.Println("Removed machine from " + node.Network + " network on remote server")
}
}
}
err = local.WipeLocal(network)
if err != nil {
log.Printf("Unable to wipe local config: %v", err)
} else {
log.Println("Removed " + node.Network + " network locally")
}
if cfg.Daemon != "off" {
err = local.RemoveSystemDServices(network)
Expand All @@ -336,13 +338,13 @@ func DeleteInterface(ifacename string, postdown string) error{
}
err = cmdIPLinkDel.Run()
if err != nil {
fmt.Println(err)
log.Println(err)
}
if postdown != "" {
runcmds := strings.Split(postdown, "; ")
err = local.RunCmds(runcmds)
if err != nil {
fmt.Println("Error encountered running PostDown: " + err.Error())
log.Println("Error encountered running PostDown: " + err.Error())
}
}
return err
Expand All @@ -367,9 +369,9 @@ func List() error{
PublicEndpoint: cfg.Node.Endpoint,
}
jsoncfg, _ := json.Marshal(listconfig)
fmt.Println(network + ": " + string(jsoncfg))
log.Println(network + ": " + string(jsoncfg))
} else {
fmt.Println(network + ": Could not retrieve network configuration.")
log.Println(network + ": Could not retrieve network configuration.")
}
}
return nil
Expand Down
Loading

0 comments on commit 4c3b7c1

Please sign in to comment.