Browse Source

fixing server

afeiszli 4 years ago
parent
commit
e732d180fd

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

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

+ 15 - 11
controllers/intClientHttpController.go

@@ -1,7 +1,8 @@
 package controller
 package controller
 
 
 import (
 import (
-//	"fmt"
+	//	"fmt"
+	// "github.com/davecgh/go-spew/spew"
 	"errors"
 	"errors"
 	"context"
 	"context"
 	"encoding/json"
 	"encoding/json"
@@ -21,7 +22,6 @@ func intClientHandlers(r *mux.Router) {
 	r.HandleFunc("/api/intclient/{clientid}", securityCheck(http.HandlerFunc(getIntClient))).Methods("GET")
 	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", securityCheck(http.HandlerFunc(getAllIntClients))).Methods("GET")
         r.HandleFunc("/api/intclients/deleteall", securityCheck(http.HandlerFunc(deleteAllIntClients))).Methods("DELETE")
         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/{clientid}", securityCheck(http.HandlerFunc(updateIntClient))).Methods("PUT")
 	r.HandleFunc("/api/intclient/register", http.HandlerFunc(registerIntClient)).Methods("POST")
 	r.HandleFunc("/api/intclient/register", http.HandlerFunc(registerIntClient)).Methods("POST")
 	r.HandleFunc("/api/intclient/{clientid}", http.HandlerFunc(deleteIntClient)).Methods("DELETE")
 	r.HandleFunc("/api/intclient/{clientid}", http.HandlerFunc(deleteIntClient)).Methods("DELETE")
@@ -138,13 +138,17 @@ func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
 		client.Address = newAddress
 		client.Address = newAddress
 	}
 	}
         if client.Network == "" { client.Network = "comms" }
         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 == "" {
         if client.ClientID == "" {
                 clientid := StringWithCharset(7, charset)
                 clientid := StringWithCharset(7, charset)
@@ -152,11 +156,11 @@ func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
                 client.ClientID = clientname
                 client.ClientID = clientname
         }
         }
 
 
-
+	//spew.Dump(client)
 	collection := mongoconn.Client.Database("netmaker").Collection("intclients")
 	collection := mongoconn.Client.Database("netmaker").Collection("intclients")
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 	// insert our network into the network table
 	// insert our network into the network table
-	_, err := collection.InsertOne(ctx, client)
+	_, err = collection.InsertOne(ctx, client)
 	defer cancel()
 	defer cancel()
 
 
 	if err != nil {
 	if err != nil {

+ 26 - 1
controllers/serverHttpController.go

@@ -14,6 +14,7 @@ import (
 func serverHandlers(r *mux.Router) {
 func serverHandlers(r *mux.Router) {
     r.HandleFunc("/api/server/addnetwork/{network}", securityCheckServer(http.HandlerFunc(addNetwork))).Methods("POST")
     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/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")
     r.HandleFunc("/api/server/removenetwork/{network}", securityCheckServer(http.HandlerFunc(removeNetwork))).Methods("DELETE")
 }
 }
 
 
@@ -84,11 +85,35 @@ func getConfig(w http.ResponseWriter, r *http.Request) {
 
 
         // get params
         // get params
 
 
-        scfg := servercfg.GetConfig()
+        scfg := servercfg.GetServerConfig()
         w.WriteHeader(http.StatusOK)
         w.WriteHeader(http.StatusOK)
         json.NewEncoder(w).Encode(scfg)
         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) {
 func addNetwork(w http.ResponseWriter, r *http.Request) {
         // Set header
         // Set header
         w.Header().Set("Content-Type", "application/json")
         w.Header().Set("Content-Type", "application/json")

+ 1 - 1
functions/helpers.go

@@ -45,7 +45,7 @@ func CreateServerToken(netID string) (string, error) {
 		privAddr = network.LocalRange
 		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))
 	accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec))
 
 

+ 1 - 1
main.go

@@ -146,7 +146,7 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) {
         }()
         }()
         log.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")
         log.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")
 
 
-	if installserver {
+	if !installserver {
 			fmt.Println("Adding server to default network")
 			fmt.Println("Adding server to default network")
                         success, err := serverctl.AddNetwork("default")
                         success, err := serverctl.AddNetwork("default")
                         if err != nil {
                         if err != nil {

+ 14 - 14
models/intclient.go

@@ -1,18 +1,18 @@
 package models
 package models
 
 
 type IntClient struct {
 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"`
 }
 }

+ 3 - 3
netclient/functions/register.go

@@ -13,7 +13,7 @@ import (
 	"encoding/json"
 	"encoding/json"
 	"net/http"
 	"net/http"
 	"errors"
 	"errors"
-	"github.com/davecgh/go-spew/spew"
+//	"github.com/davecgh/go-spew/spew"
 )
 )
 
 
 func Register(cfg config.GlobalConfig) error {
 func Register(cfg config.GlobalConfig) error {
@@ -57,12 +57,12 @@ func Register(cfg config.GlobalConfig) error {
 	}
 	}
 	var wgclient models.IntClient
 	var wgclient models.IntClient
 	json.Unmarshal(bodyBytes, &wgclient)
 	json.Unmarshal(bodyBytes, &wgclient)
-        spew.Dump(wgclient)
+        //spew.Dump(wgclient)
 	err = config.ModGlobalConfig(wgclient)
 	err = config.ModGlobalConfig(wgclient)
         if err != nil {
         if err != nil {
                 return err
                 return err
         }
         }
-        spew.Dump(wgclient)
+        //spew.Dump(wgclient)
 	err = wireguard.InitGRPCWireguard(wgclient)
 	err = wireguard.InitGRPCWireguard(wgclient)
         if err != nil {
         if err != nil {
                 return err
                 return err

+ 2 - 2
netclient/wireguard/kernel.go

@@ -1,7 +1,7 @@
 package wireguard
 package wireguard
 
 
 import (
 import (
-"github.com/davecgh/go-spew/spew"
+	// "github.com/davecgh/go-spew/spew"
 	"fmt"
 	"fmt"
 	"strconv"
 	"strconv"
 	"errors"
 	"errors"
@@ -117,7 +117,7 @@ func InitGRPCWireguard(client models.IntClient) error {
                         return err
                         return err
                 }
                 }
         }
         }
-	spew.Dump(conf)
+	//spew.Dump(conf)
         err = wgclient.ConfigureDevice(ifacename, conf)
         err = wgclient.ConfigureDevice(ifacename, conf)
 
 
         if err != nil {
         if err != nil {

+ 7 - 0
scripts/reset-network.sh

@@ -0,0 +1,7 @@
+rm -rf /etc/systemd/system/netclient-default.timer
+rm -rf /etc/systemd/system/[email protected] 
+rm -rf /etc/netclient/
+systemctl daemon-reload
+ip link del nm-default
+ip link del nm-grpc-wg
+docker-compose -f /root/netmaker/compose/docker-compose.yml down --volumes

+ 2 - 1
servercfg/serverconf.go

@@ -16,7 +16,7 @@ func SetHost() error {
 	os.Setenv("SERVER_HOST", remoteip)
 	os.Setenv("SERVER_HOST", remoteip)
 	return nil
 	return nil
 }
 }
-func GetConfig() config.ServerConfig {
+func GetServerConfig() config.ServerConfig {
 	var cfg config.ServerConfig
 	var cfg config.ServerConfig
 	cfg.APIHost = GetAPIHost()
 	cfg.APIHost = GetAPIHost()
 	cfg.APIPort = GetAPIPort()
 	cfg.APIPort = GetAPIPort()
@@ -56,6 +56,7 @@ func GetWGConfig() config.WG{
 	}
 	}
 	cfg.GRPCWGInterface = GetGRPCWGInterface()
 	cfg.GRPCWGInterface = GetGRPCWGInterface()
 	cfg.GRPCWGAddress = GetGRPCWGAddress()
 	cfg.GRPCWGAddress = GetGRPCWGAddress()
+        cfg.GRPCWGAddressRange = GetGRPCWGAddressRange()
 	cfg.GRPCWGPort = GetGRPCWGPort()
 	cfg.GRPCWGPort = GetGRPCWGPort()
 	cfg.GRPCWGPubKey =  GetGRPCWGPubKey()
 	cfg.GRPCWGPubKey =  GetGRPCWGPubKey()
 	cfg.GRPCWGPrivKey =  GetGRPCWGPrivKey()
 	cfg.GRPCWGPrivKey =  GetGRPCWGPrivKey()

+ 3 - 3
servercfg/wireguardconf.go

@@ -40,7 +40,7 @@ func GetGRPCWGInterface() string {
        return iface
        return iface
 }
 }
 func GetGRPCWGAddress() string {
 func GetGRPCWGAddress() string {
-        address := "10.101.0.1"
+      address := "10.101.0.1"
       if os.Getenv("SERVER_GRPC_WG_ADDRESS") != ""  {
       if os.Getenv("SERVER_GRPC_WG_ADDRESS") != ""  {
               address = os.Getenv("SERVER_GRPC_WG_ADDRESS")
               address = os.Getenv("SERVER_GRPC_WG_ADDRESS")
       } else if config.Config.WG.GRPCWGAddress != "" {
       } else if config.Config.WG.GRPCWGAddress != "" {
@@ -49,7 +49,7 @@ func GetGRPCWGAddress() string {
       return address
       return address
 }
 }
 func GetGRPCWGAddressRange() string {
 func GetGRPCWGAddressRange() string {
-        address := "10.101.0.0/16"
+      address := "10.101.0.0/16"
       if os.Getenv("SERVER_GRPC_WG_ADDRESS_RANGE") != ""  {
       if os.Getenv("SERVER_GRPC_WG_ADDRESS_RANGE") != ""  {
               address = os.Getenv("SERVER_GRPC_WG_ADDRESS_RANGE")
               address = os.Getenv("SERVER_GRPC_WG_ADDRESS_RANGE")
       } else if config.Config.WG.GRPCWGAddressRange != "" {
       } else if config.Config.WG.GRPCWGAddressRange != "" {
@@ -58,7 +58,7 @@ func GetGRPCWGAddressRange() string {
       return address
       return address
 }
 }
 func GetGRPCWGPort() string {
 func GetGRPCWGPort() string {
-        port := "50555"
+      port := "50555"
       if os.Getenv("SERVER_GRPC_WG_PORT") != ""  {
       if os.Getenv("SERVER_GRPC_WG_PORT") != ""  {
               port = os.Getenv("SERVER_GRPC_WG_PORT")
               port = os.Getenv("SERVER_GRPC_WG_PORT")
       } else if config.Config.WG.GRPCWGPort != "" {
       } else if config.Config.WG.GRPCWGPort != "" {

+ 1 - 0
serverctl/serverctl.go

@@ -224,6 +224,7 @@ func AddNetwork(network string) (bool, error) {
                 log.Println("could not change netclient directory permissions")
                 log.Println("could not change netclient directory permissions")
                 return false, err
                 return false, err
         }
         }
+	log.Println("executing network join: " + "/etc/netclient/netclient "+"join "+"-t "+token+" -name "+"netmaker"+" -endpoint "+pubip)
 	out, err := exec.Command("/etc/netclient/netclient","join","-t",token,"-name","netmaker","-endpoint",pubip).Output()
 	out, err := exec.Command("/etc/netclient/netclient","join","-t",token,"-name","netmaker","-endpoint",pubip).Output()
         if string(out) != "" {
         if string(out) != "" {
 	        log.Println(string(out))
 	        log.Println(string(out))

+ 3 - 3
serverctl/wireguard.go

@@ -63,10 +63,10 @@ func InitServerWireGuard() error {
 	client.PublicKey = wgconfig.GRPCWGPubKey
 	client.PublicKey = wgconfig.GRPCWGPubKey
 	client.ServerPublicEndpoint = servercfg.GetAPIHost()
 	client.ServerPublicEndpoint = servercfg.GetAPIHost()
 	client.ServerAPIPort = servercfg.GetAPIPort()
 	client.ServerAPIPort = servercfg.GetAPIPort()
-	client.ServerPrivateAddress = wgconfig.GRPCWGAddress
-	client.ServerWGPort = wgconfig.GRPCWGPort
+	client.ServerPrivateAddress = servercfg.GetGRPCWGAddress()
+	client.ServerWGPort = servercfg.GetGRPCWGPort()
 	client.ServerGRPCPort = servercfg.GetGRPCPort()
 	client.ServerGRPCPort = servercfg.GetGRPCPort()
-	client.Address = wgconfig.GRPCWGAddress
+	client.Address = servercfg.GetGRPCWGAddress()
 	client.IsServer = "yes"
 	client.IsServer = "yes"
 	client.Network = "comms"
 	client.Network = "comms"
 	exists, _ := functions.ServerIntClientExists()
 	exists, _ := functions.ServerIntClientExists()