fixing server

This commit is contained in:
afeiszli
2021-06-02 02:32:20 +00:00
parent 77d0ed7991
commit e732d180fd
13 changed files with 79 additions and 41 deletions

View File

@@ -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: {}

View File

@@ -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" }
server, err := serverctl.GetServerWGConf()
wgconfig := servercfg.GetWGConfig() //spew.Dump(server)
client.ServerPublicEndpoint = servercfg.GetAPIHost() if err != nil {
client.ServerAPIPort = servercfg.GetAPIPort() return client, err
client.ServerPrivateAddress = wgconfig.GRPCWGAddress }
client.ServerWGPort = wgconfig.GRPCWGPort client.ServerPublicEndpoint = server.ServerPublicEndpoint
client.ServerGRPCPort = servercfg.GetGRPCPort() 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 {

View File

@@ -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")

View File

@@ -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))

View File

@@ -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 {

View File

@@ -8,11 +8,11 @@ type IntClient struct {
Address string `json:"address" bson:"address"` Address string `json:"address" bson:"address"`
Address6 string `json:"address6" bson:"address6"` Address6 string `json:"address6" bson:"address6"`
Network string `json:"network" bson:"network"` Network string `json:"network" bson:"network"`
ServerPublicEndpoint string `json:"serverwgendpoint" bson:"serverwgendpoint"` ServerPublicEndpoint string `json:"serverpublicendpoint" bson:"serverpublicendpoint"`
ServerAPIPort string `json:"serverapiendpoint" bson:"serverapiendpoint"` ServerAPIPort string `json:"serverapiport" bson:"serverapiport"`
ServerPrivateAddress string `json:"serveraddress" bson:"serveraddress"` ServerPrivateAddress string `json:"serverprivateaddress" bson:"serverprivateaddress"`
ServerWGPort string `json:"serverport" bson:"serverport"` ServerWGPort string `json:"serverwgport" bson:"serverwgport"`
ServerGRPCPort string `json:"serverport" bson:"serverport"` ServerGRPCPort string `json:"servergrpcport" bson:"servergrpcport"`
ServerKey string `json:"serverkey" bson:"serverkey"` ServerKey string `json:"serverkey" bson:"serverkey"`
IsServer string `json:"isserver" bson:"isserver"` IsServer string `json:"isserver" bson:"isserver"`
} }

View File

@@ -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

View File

@@ -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
scripts/reset-network.sh Executable file
View File

@@ -0,0 +1,7 @@
rm -rf /etc/systemd/system/netclient-default.timer
rm -rf /etc/systemd/system/netclient@.service
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

View File

@@ -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()

View File

@@ -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))

View File

@@ -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.ServerPrivateAddress = servercfg.GetGRPCWGAddress()
client.ServerWGPort = wgconfig.GRPCWGPort 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()