2021-03-26 00:17:52 +08:00
|
|
|
//TODO: Harden. Add failover for every method and agent calls
|
|
|
|
//TODO: Figure out why mongodb keeps failing (log rotation?)
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2021-04-13 12:42:35 +08:00
|
|
|
"flag"
|
2021-04-06 02:47:07 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
2021-03-26 00:17:52 +08:00
|
|
|
"github.com/gravitl/netmaker/controllers"
|
2021-04-07 07:13:34 +08:00
|
|
|
"github.com/gravitl/netmaker/serverctl"
|
2021-04-06 02:47:07 +08:00
|
|
|
"github.com/gravitl/netmaker/functions"
|
2021-03-26 00:17:52 +08:00
|
|
|
"github.com/gravitl/netmaker/mongoconn"
|
|
|
|
"github.com/gravitl/netmaker/config"
|
2021-04-06 02:47:07 +08:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2021-03-26 00:17:52 +08:00
|
|
|
"fmt"
|
2021-04-06 02:47:07 +08:00
|
|
|
"time"
|
|
|
|
"net/http"
|
2021-04-15 10:59:25 +08:00
|
|
|
"strings"
|
2021-04-06 02:47:07 +08:00
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2021-03-26 00:17:52 +08:00
|
|
|
"os"
|
2021-04-13 12:42:35 +08:00
|
|
|
"os/exec"
|
2021-03-26 00:17:52 +08:00
|
|
|
"net"
|
|
|
|
"context"
|
2021-04-13 12:42:35 +08:00
|
|
|
"strconv"
|
2021-03-26 00:17:52 +08:00
|
|
|
"sync"
|
|
|
|
"os/signal"
|
2021-04-15 10:59:25 +08:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2021-03-26 00:17:52 +08:00
|
|
|
service "github.com/gravitl/netmaker/controllers"
|
|
|
|
nodepb "github.com/gravitl/netmaker/grpc"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
2021-04-06 02:47:07 +08:00
|
|
|
|
|
|
|
var ServerGRPC string
|
|
|
|
var PortGRPC string
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
//Start MongoDB Connection and start API Request Handler
|
|
|
|
func main() {
|
2021-04-13 12:42:35 +08:00
|
|
|
|
2021-04-20 09:50:58 +08:00
|
|
|
|
2021-04-13 12:42:35 +08:00
|
|
|
var clientmode string
|
|
|
|
var defaultnet string
|
|
|
|
flag.StringVar(&clientmode, "clientmode", "on", "Have a client on the server")
|
|
|
|
flag.StringVar(&defaultnet, "defaultnet", "on", "Create a default network")
|
|
|
|
flag.Parse()
|
|
|
|
if clientmode == "on" {
|
|
|
|
|
|
|
|
cmd := exec.Command("id", "-u")
|
|
|
|
output, err := cmd.Output()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(string(output[:len(output)-1]))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
log.Fatal("To run in client mode requires root privileges. Either turn off client mode with the --clientmode=off flag, or run with sudo.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
log.Println("Server starting...")
|
|
|
|
mongoconn.ConnectDatabase()
|
2021-04-15 21:48:14 +08:00
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
installserver := false
|
2021-04-13 12:42:35 +08:00
|
|
|
if !(defaultnet == "off") {
|
2021-04-06 20:24:57 +08:00
|
|
|
if config.Config.Server.CreateDefault {
|
2021-04-07 07:13:34 +08:00
|
|
|
created, err := createDefaultNetwork()
|
2021-04-06 20:24:57 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error creating default network: %v", err)
|
2021-04-07 07:13:34 +08:00
|
|
|
}
|
2021-04-13 12:42:35 +08:00
|
|
|
if created && clientmode != "off" {
|
2021-04-07 07:13:34 +08:00
|
|
|
installserver = true
|
2021-04-06 20:24:57 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 12:42:35 +08:00
|
|
|
}
|
|
|
|
var waitnetwork sync.WaitGroup
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
if config.Config.Server.AgentBackend {
|
2021-04-13 12:42:35 +08:00
|
|
|
waitnetwork.Add(1)
|
|
|
|
go runGRPC(&waitnetwork, installserver)
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Config.Server.RestBackend {
|
2021-04-13 12:42:35 +08:00
|
|
|
waitnetwork.Add(1)
|
|
|
|
controller.HandleRESTRequests(&waitnetwork)
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
if !config.Config.Server.RestBackend && !config.Config.Server.AgentBackend {
|
|
|
|
fmt.Println("Oops! No Server Mode selected. Nothing being served.")
|
|
|
|
}
|
2021-04-13 12:42:35 +08:00
|
|
|
waitnetwork.Wait()
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println("Exiting now.")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
func runGRPC(wg *sync.WaitGroup, installserver bool) {
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// Configure 'log' package to give file name and line number on eg. log.Fatal
|
|
|
|
// Pipe flags to one another (log.LstdFLags = log.Ldate | log.Ltime)
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
|
|
|
// Start our listener, 50051 is the default gRPC port
|
|
|
|
grpcport := ":50051"
|
|
|
|
if config.Config.Server.GrpcPort != "" {
|
|
|
|
grpcport = ":" + config.Config.Server.GrpcPort
|
|
|
|
}
|
|
|
|
if os.Getenv("GRPC_PORT") != "" {
|
|
|
|
grpcport = ":" + os.Getenv("GRPC_PORT")
|
|
|
|
}
|
2021-04-06 02:47:07 +08:00
|
|
|
PortGRPC = grpcport
|
|
|
|
if os.Getenv("BACKEND_URL") == "" {
|
|
|
|
if config.Config.Server.Host == "" {
|
|
|
|
ServerGRPC, _ = getPublicIP()
|
|
|
|
} else {
|
|
|
|
ServerGRPC = config.Config.Server.Host
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ServerGRPC = os.Getenv("BACKEND_URL")
|
|
|
|
}
|
|
|
|
fmt.Println("GRPC Server set to: " + ServerGRPC)
|
|
|
|
fmt.Println("GRPC Port set to: " + PortGRPC)
|
|
|
|
var gconf models.GlobalConfig
|
|
|
|
gconf.ServerGRPC = ServerGRPC
|
|
|
|
gconf.PortGRPC = PortGRPC
|
|
|
|
gconf.Name = "netmaker"
|
|
|
|
err := setGlobalConfig(gconf)
|
|
|
|
|
2021-04-15 10:59:25 +08:00
|
|
|
if err != nil && err != mongo.ErrNoDocuments{
|
2021-04-06 02:47:07 +08:00
|
|
|
log.Fatalf("Unable to set global config: %v", err)
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", grpcport)
|
|
|
|
// Handle errors if any
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to listen on port" + grpcport + ": %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := grpc.NewServer(
|
|
|
|
authServerUnaryInterceptor(),
|
|
|
|
authServerStreamInterceptor(),
|
|
|
|
)
|
|
|
|
// Create NodeService type
|
|
|
|
srv := &service.NodeServiceServer{}
|
|
|
|
|
|
|
|
// Register the service with the server
|
|
|
|
nodepb.RegisterNodeServiceServer(s, srv)
|
|
|
|
|
|
|
|
srv.NodeDB = mongoconn.NodeDB
|
|
|
|
|
|
|
|
// Start the server in a child routine
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(listener); err != nil {
|
|
|
|
log.Fatalf("Failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
fmt.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")
|
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
if installserver {
|
2021-04-15 10:59:25 +08:00
|
|
|
fmt.Println("Adding server to " + config.Config.Server.DefaultNetName)
|
2021-04-07 07:13:34 +08:00
|
|
|
success, err := serverctl.AddNetwork(config.Config.Server.DefaultNetName)
|
2021-04-18 00:13:26 +08:00
|
|
|
if err != nil {
|
2021-04-07 07:13:34 +08:00
|
|
|
fmt.Printf("Error adding to default network: %v", err)
|
2021-04-15 10:59:25 +08:00
|
|
|
fmt.Println("")
|
2021-04-07 07:13:34 +08:00
|
|
|
fmt.Println("Unable to add server to network. Continuing.")
|
2021-04-15 10:59:25 +08:00
|
|
|
fmt.Println("Please investigate client installation on server.")
|
2021-04-18 00:13:26 +08:00
|
|
|
} else if !success {
|
|
|
|
fmt.Println("Unable to add server to network. Continuing.")
|
|
|
|
fmt.Println("Please investigate client installation on server.")
|
|
|
|
} else{
|
2021-04-07 07:13:34 +08:00
|
|
|
fmt.Println("Server successfully added to default network.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("Setup complete. You are ready to begin using netmaker.")
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
// Right way to stop the server using a SHUTDOWN HOOK
|
|
|
|
// Create a channel to receive OS signals
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
|
|
|
|
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
|
|
|
|
// Ignore other incoming signals
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
// Block main routine until a signal is received
|
|
|
|
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
|
|
|
|
<-c
|
|
|
|
|
|
|
|
// After receiving CTRL+C Properly stop the server
|
|
|
|
fmt.Println("Stopping the Agent server...")
|
|
|
|
s.Stop()
|
|
|
|
listener.Close()
|
|
|
|
fmt.Println("Agent server closed..")
|
|
|
|
fmt.Println("Closing MongoDB connection")
|
|
|
|
mongoconn.Client.Disconnect(context.TODO())
|
|
|
|
fmt.Println("MongoDB connection closed.")
|
|
|
|
}
|
2021-04-06 02:47:07 +08:00
|
|
|
func setGlobalConfig(globalconf models.GlobalConfig) (error) {
|
|
|
|
|
|
|
|
collection := mongoconn.Client.Database("netmaker").Collection("config")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
2021-04-15 10:59:25 +08:00
|
|
|
create, _, err := functions.GetGlobalConfig()
|
|
|
|
if create {
|
2021-04-06 02:47:07 +08:00
|
|
|
_, err := collection.InsertOne(ctx, globalconf)
|
|
|
|
defer cancel()
|
|
|
|
if err != nil {
|
2021-04-15 10:59:25 +08:00
|
|
|
if err == mongo.ErrNoDocuments || strings.Contains(err.Error(), "no documents in result"){
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 02:47:07 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filter := bson.M{"name": "netmaker"}
|
|
|
|
update := bson.D{
|
|
|
|
{"$set", bson.D{
|
|
|
|
{"servergrpc", globalconf.ServerGRPC},
|
|
|
|
{"portgrpc", globalconf.PortGRPC},
|
|
|
|
}},
|
|
|
|
}
|
2021-04-15 10:59:25 +08:00
|
|
|
err := collection.FindOneAndUpdate(ctx, filter, update).Decode(&globalconf)
|
|
|
|
if err == mongo.ErrNoDocuments {
|
|
|
|
//if err == mongo.ErrNoDocuments || strings.Contains(err.Error(), "no documents in result"){
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-06 02:47:07 +08:00
|
|
|
}
|
2021-04-15 10:59:25 +08:00
|
|
|
return err
|
2021-04-06 02:47:07 +08:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
func createDefaultNetwork() (bool, error) {
|
2021-04-06 20:24:57 +08:00
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
iscreated := false
|
2021-04-13 12:42:35 +08:00
|
|
|
exists, err := functions.NetworkExists(config.Config.Server.DefaultNetName)
|
2021-04-06 20:24:57 +08:00
|
|
|
|
|
|
|
if exists || err != nil {
|
2021-04-13 12:42:35 +08:00
|
|
|
fmt.Println("Default network already exists")
|
|
|
|
fmt.Println("Skipping default network create")
|
2021-04-07 07:13:34 +08:00
|
|
|
return iscreated, err
|
2021-04-06 20:24:57 +08:00
|
|
|
} else {
|
|
|
|
|
2021-04-13 12:42:35 +08:00
|
|
|
var network models.Network
|
|
|
|
|
|
|
|
network.NetID = config.Config.Server.DefaultNetName
|
|
|
|
network.AddressRange = config.Config.Server.DefaultNetRange
|
|
|
|
network.DisplayName = config.Config.Server.DefaultNetName
|
|
|
|
network.SetDefaults()
|
|
|
|
network.SetNodesLastModified()
|
|
|
|
network.SetNetworkLastModified()
|
|
|
|
network.KeyUpdateTimeStamp = time.Now().Unix()
|
|
|
|
priv := false
|
2021-04-13 22:04:31 +08:00
|
|
|
network.IsLocal = &priv
|
2021-04-13 12:42:35 +08:00
|
|
|
network.KeyUpdateTimeStamp = time.Now().Unix()
|
2021-04-06 20:31:23 +08:00
|
|
|
allow := true
|
2021-04-13 12:42:35 +08:00
|
|
|
network.AllowManualSignUp = &allow
|
2021-04-06 20:24:57 +08:00
|
|
|
|
2021-04-13 12:42:35 +08:00
|
|
|
fmt.Println("Creating default network.")
|
2021-04-06 20:24:57 +08:00
|
|
|
|
|
|
|
|
2021-04-13 12:42:35 +08:00
|
|
|
collection := mongoconn.Client.Database("netmaker").Collection("networks")
|
2021-04-06 20:24:57 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
|
|
|
|
2021-04-13 12:42:35 +08:00
|
|
|
// insert our network into the network table
|
|
|
|
_, err = collection.InsertOne(ctx, network)
|
2021-04-06 20:24:57 +08:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
}
|
2021-04-07 07:13:34 +08:00
|
|
|
if err == nil {
|
|
|
|
iscreated = true
|
|
|
|
}
|
|
|
|
return iscreated, err
|
2021-04-06 20:24:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-06 02:47:07 +08:00
|
|
|
|
|
|
|
func getPublicIP() (string, error) {
|
|
|
|
|
|
|
|
iplist := []string{"https://ifconfig.me", "http://api.ipify.org", "http://ipinfo.io/ip"}
|
|
|
|
endpoint := ""
|
|
|
|
var err error
|
|
|
|
for _, ipserver := range iplist {
|
|
|
|
resp, err := http.Get(ipserver)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
endpoint = string(bodyBytes)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if err == nil && endpoint == "" {
|
|
|
|
err = errors.New("Public Address Not Found.")
|
|
|
|
}
|
|
|
|
return endpoint, err
|
|
|
|
}
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
func authServerUnaryInterceptor() grpc.ServerOption {
|
|
|
|
return grpc.UnaryInterceptor(controller.AuthServerUnaryInterceptor)
|
|
|
|
}
|
|
|
|
func authServerStreamInterceptor() grpc.ServerOption {
|
|
|
|
return grpc.StreamInterceptor(controller.AuthServerStreamInterceptor)
|
|
|
|
}
|
|
|
|
|