Browse Source

Merge branch 'develop' into feature_v0.5_netclient

Alex 4 years ago
parent
commit
a8dc388640

+ 1 - 1
compose/docker-compose.localserver.yml

@@ -21,6 +21,6 @@ services:
     ports:
       - "80:80"
     environment:
-      BACKEND_URL: "http://localhost:8081"
+      BACKEND_URL: "http://3.236.111.47:8081"
 volumes:
   mongovol: {}

+ 1 - 1
controllers/common.go

@@ -304,7 +304,7 @@ func DeleteIntClient(clientid string) (bool, error) {
 
         deleted := false
 
-        collection := mongoconn.Client.Database("netmaker").Collection("clientid")
+        collection := mongoconn.Client.Database("netmaker").Collection("intclients")
 
         filter := bson.M{"clientid": clientid}
 

+ 38 - 1
controllers/intClientHttpController.go

@@ -20,7 +20,9 @@ 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("POST")
+        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")
 }
@@ -79,6 +81,40 @@ func getIntClient(w http.ResponseWriter, r *http.Request) {
         json.NewEncoder(w).Encode(client)
 }
 
+func updateIntClient(w http.ResponseWriter, r *http.Request) {
+        w.Header().Set("Content-Type", "application/json")
+
+        var errorResponse = models.ErrorResponse{
+                Code: http.StatusInternalServerError, Message: "W1R3: It's not you it's me.",
+        }
+
+        var clientreq models.IntClient
+
+        //get node from body of request
+        err := json.NewDecoder(r.Body).Decode(&clientreq)
+        if err != nil {
+                returnErrorResponse(w, r, formatError(err, "internal"))
+                return
+        }
+        if servercfg.IsRegisterKeyRequired() {
+                validKey := functions.IsKeyValidGlobal(clientreq.AccessKey)
+                if !validKey {
+                                errorResponse = models.ErrorResponse{
+                                        Code: http.StatusUnauthorized, Message: "W1R3: Key invalid, or none provided.",
+                                }
+                                returnErrorResponse(w, r, errorResponse)
+                                return
+                }
+        }
+        client, err := RegisterIntClient(clientreq)
+
+        if err != nil {
+                returnErrorResponse(w, r, formatError(err, "internal"))
+                return
+        }
+        w.WriteHeader(http.StatusOK)
+        json.NewEncoder(w).Encode(client)
+}
 
 
 func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
@@ -108,6 +144,7 @@ func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
 		return client, err
 	}
 	client.ServerEndpoint = server.ServerEndpoint
+	client.ServerAPIEndpoint = servercfg.GetAPIHost() + ":" + servercfg.GetAPIPort()
 	client.ServerAddress = server.ServerAddress
 	client.ServerPort = server.ServerPort
 	client.ServerKey = server.ServerKey

+ 36 - 3
functions/helpers.go

@@ -702,9 +702,15 @@ func UniqueAddress(networkName string) (string, error) {
 			offset = false
 			continue
 		}
-		if IsIPUnique(networkName, ip.String())  && IsIPUniqueExtClients(networkName, ip.String()) {
-			return ip.String(), err
-		}
+                if networkName == "comms" {
+                        if IsIPUniqueClients(networkName, ip.String()) {
+                                return ip.String(), err
+                        }
+                } else {
+                        if IsIPUnique(networkName, ip.String()) && IsIPUniqueExtClients(networkName, ip.String()) {
+                                return ip.String(), err
+                        }
+                }
 	}
 
 	//TODO
@@ -894,6 +900,33 @@ func IsIP6UniqueClients(network string, ip string) bool {
         return isunique
 }
 
+//checks if IP is unique in the address range
+//used by UniqueAddress
+func IsIPUniqueClients(network string, ip string) bool {
+
+        var client models.IntClient
+
+        isunique := true
+
+        collection := mongoconn.Client.Database("netmaker").Collection("intclients")
+        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+
+        filter := bson.M{"address": ip, "network": network}
+
+        err := collection.FindOne(ctx, filter).Decode(&client)
+
+        defer cancel()
+
+        if err != nil {
+                return isunique
+        }
+
+        if client.Address == ip {
+                isunique = false
+        }
+        return isunique
+}
+
 //called once key has been used by createNode
 //reduces value by one and deletes if necessary
 func DecrimentKey(networkName string, keyvalue string) {

+ 4 - 4
models/intclient.go

@@ -1,15 +1,15 @@
 package models
 
 type IntClient struct {
-        ClientID       string             `json:"clientid" bson:"clientid"`
+  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"`
+	Address6        string            `json:"address6" bson:"address6"`
 	Network        string             `json:"network" bson:"network"`
-	ServerEndpoint  string             `json:"serverendpoint" bson:"serverendpoint"`
-        ServerAPIEndpoint  string             `json:"serverapiendpoint" bson:"serverapiendpoint"`
+	ServerEndpoint  string            `json:"serverendpoint" bson:"serverendpoint"`
+  ServerAPIEndpoint  string         `json:"serverapiendpoint" bson:"serverapiendpoint"`
 	ServerAddress  string             `json:"serveraddress" bson:"serveraddress"`
 	ServerPort     string             `json:"serverport" bson:"serverport"`
 	ServerKey      string             `json:"serverkey" bson:"serverkey"`

+ 22 - 14
servercfg/serverconf.go

@@ -90,27 +90,35 @@ func GetAPIPort() string {
 
 func GetGRPCHost() string {
 	serverhost := "127.0.0.1"
-	if os.Getenv("SERVER_GRPC_HOST") != ""  {
-	       serverhost = os.Getenv("SERVER_GRPC_HOST")
-	} else if config.Config.Server.GRPCHost != "" {
-	       serverhost = config.Config.Server.GRPCHost
-	} else if os.Getenv("SERVER_HOST") != ""  {
-	       serverhost = os.Getenv("SERVER_HOST")
+	if IsGRPCWireGuard() {
+		serverhost = GetGRPCWGAddress()
 	} else {
-	        remoteip, _ := GetPublicIP()
-		if remoteip != "" {
-			serverhost = remoteip
+		if os.Getenv("SERVER_GRPC_HOST") != ""  {
+			serverhost = os.Getenv("SERVER_GRPC_HOST")
+		} else if config.Config.Server.GRPCHost != "" {
+		       serverhost = config.Config.Server.GRPCHost
+		} else if os.Getenv("SERVER_HOST") != ""  {
+		       serverhost = os.Getenv("SERVER_HOST")
+		} else {
+		        remoteip, _ := GetPublicIP()
+			if remoteip != "" {
+				serverhost = remoteip
+			}
 		}
 	}
         return serverhost
 }
 func GetGRPCPort() string {
         grpcport := "50051"
-        if os.Getenv("GRPC_PORT") != "" {
-                grpcport = os.Getenv("GRPC_PORT")
-        } else if  config.Config.Server.GRPCPort != "" {
-                grpcport = config.Config.Server.GRPCPort
-        }
+        if IsGRPCWireGuard() {
+                grpcport = GetGRPCWGPort()
+        } else {
+	        if os.Getenv("GRPC_PORT") != "" {
+	                grpcport = os.Getenv("GRPC_PORT")
+	        } else if  config.Config.Server.GRPCPort != "" {
+	                grpcport = config.Config.Server.GRPCPort
+	        }
+	}
         return grpcport
 }
 func GetMasterKey() string {

+ 9 - 0
serverctl/wireguard.go

@@ -72,10 +72,19 @@ func InitServerWireGuard() error {
 	client.Address = wgconfig.GRPCWGAddress
 	client.IsServer = "yes"
 	client.Network = "comms"
+	exists, _ := functions.ServerIntClientExists()
+	if exists {
+
+	}
 	err = RegisterServer(client)
         return err
 }
 
+func DeleteServerClient() error {
+	return nil
+}
+
+
 func RegisterServer(client models.IntClient) error {
         if client.PrivateKey == "" {
                 privateKey, err := wgtypes.GeneratePrivateKey()