2021-05-06 06:03:37 +08:00
|
|
|
package servercfg
|
|
|
|
|
|
|
|
import (
|
2021-07-15 04:47:05 +08:00
|
|
|
"errors"
|
2022-01-07 04:05:38 +08:00
|
|
|
"io"
|
2021-10-09 10:46:39 +08:00
|
|
|
"net"
|
2021-10-14 03:15:20 +08:00
|
|
|
"net/http"
|
2021-05-06 06:03:37 +08:00
|
|
|
"os"
|
2021-07-03 11:25:36 +08:00
|
|
|
"strconv"
|
2021-10-21 02:17:31 +08:00
|
|
|
"strings"
|
2021-10-14 03:15:20 +08:00
|
|
|
|
2021-07-15 04:47:05 +08:00
|
|
|
"github.com/gravitl/netmaker/config"
|
2021-05-06 06:03:37 +08:00
|
|
|
)
|
2021-05-06 04:42:17 +08:00
|
|
|
|
2022-02-04 04:33:19 +08:00
|
|
|
var Version = "dev"
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// SetHost - sets the host ip
|
2021-05-06 04:42:17 +08:00
|
|
|
func SetHost() error {
|
|
|
|
remoteip, err := GetPublicIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.Setenv("SERVER_HOST", remoteip)
|
2021-05-06 06:03:37 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetServerConfig - gets the server config into memory from file or env
|
2021-06-02 10:32:20 +08:00
|
|
|
func GetServerConfig() config.ServerConfig {
|
2021-05-06 06:03:37 +08:00
|
|
|
var cfg config.ServerConfig
|
2021-07-11 12:49:31 +08:00
|
|
|
cfg.APIConnString = GetAPIConnString()
|
2021-07-16 03:14:48 +08:00
|
|
|
cfg.CoreDNSAddr = GetCoreDNSAddr()
|
2021-05-06 06:03:37 +08:00
|
|
|
cfg.APIHost = GetAPIHost()
|
|
|
|
cfg.APIPort = GetAPIPort()
|
2021-07-11 12:49:31 +08:00
|
|
|
cfg.GRPCConnString = GetGRPCConnString()
|
2021-05-06 06:03:37 +08:00
|
|
|
cfg.GRPCHost = GetGRPCHost()
|
|
|
|
cfg.GRPCPort = GetGRPCPort()
|
|
|
|
cfg.MasterKey = "(hidden)"
|
2021-11-16 00:42:52 +08:00
|
|
|
cfg.DNSKey = "(hidden)"
|
2021-05-06 06:03:37 +08:00
|
|
|
cfg.AllowedOrigin = GetAllowedOrigin()
|
|
|
|
cfg.RestBackend = "off"
|
2021-10-09 10:46:39 +08:00
|
|
|
cfg.NodeID = GetNodeID()
|
2021-10-03 00:28:17 +08:00
|
|
|
cfg.CheckinInterval = GetCheckinInterval()
|
2021-10-18 21:28:46 +08:00
|
|
|
cfg.ServerCheckinInterval = GetServerCheckinInterval()
|
2021-05-06 06:03:37 +08:00
|
|
|
if IsRestBackend() {
|
|
|
|
cfg.RestBackend = "on"
|
|
|
|
}
|
|
|
|
cfg.AgentBackend = "off"
|
2021-07-15 04:47:05 +08:00
|
|
|
if IsAgentBackend() {
|
|
|
|
cfg.AgentBackend = "on"
|
|
|
|
}
|
2021-05-06 06:03:37 +08:00
|
|
|
cfg.ClientMode = "off"
|
2021-09-28 05:51:20 +08:00
|
|
|
if IsClientMode() != "off" {
|
|
|
|
cfg.ClientMode = IsClientMode()
|
2021-05-06 06:03:37 +08:00
|
|
|
}
|
|
|
|
cfg.DNSMode = "off"
|
|
|
|
if IsDNSMode() {
|
2021-07-15 04:47:05 +08:00
|
|
|
cfg.DNSMode = "on"
|
|
|
|
}
|
2021-11-15 05:50:20 +08:00
|
|
|
cfg.DisplayKeys = "off"
|
|
|
|
if IsDisplayKeys() {
|
|
|
|
cfg.DisplayKeys = "on"
|
|
|
|
}
|
2021-07-15 04:47:05 +08:00
|
|
|
cfg.GRPCSSL = "off"
|
|
|
|
if IsGRPCSSL() {
|
|
|
|
cfg.GRPCSSL = "on"
|
2021-05-06 06:03:37 +08:00
|
|
|
}
|
|
|
|
cfg.DisableRemoteIPCheck = "off"
|
|
|
|
if DisableRemoteIPCheck() {
|
|
|
|
cfg.DisableRemoteIPCheck = "on"
|
|
|
|
}
|
2021-07-15 04:47:05 +08:00
|
|
|
cfg.DisableDefaultNet = "off"
|
|
|
|
if DisableDefaultNet() {
|
|
|
|
cfg.DisableRemoteIPCheck = "on"
|
|
|
|
}
|
2021-08-20 05:35:14 +08:00
|
|
|
cfg.Database = GetDB()
|
2021-08-20 01:41:04 +08:00
|
|
|
cfg.Platform = GetPlatform()
|
2021-08-17 04:30:55 +08:00
|
|
|
cfg.Version = GetVersion()
|
2021-10-21 02:17:31 +08:00
|
|
|
|
|
|
|
// == auth config ==
|
|
|
|
var authInfo = GetAuthProviderInfo()
|
|
|
|
cfg.AuthProvider = authInfo[0]
|
|
|
|
cfg.ClientID = authInfo[1]
|
|
|
|
cfg.ClientSecret = authInfo[2]
|
2021-10-22 03:28:58 +08:00
|
|
|
cfg.FrontendURL = GetFrontendURL()
|
2022-01-06 02:13:03 +08:00
|
|
|
if GetRce() {
|
|
|
|
cfg.RCE = "on"
|
|
|
|
} else {
|
|
|
|
cfg.RCE = "off"
|
|
|
|
}
|
2022-01-21 18:19:17 +08:00
|
|
|
cfg.Debug = GetDebug()
|
2022-01-21 06:50:42 +08:00
|
|
|
cfg.Telemetry = Telemetry()
|
2022-01-26 00:58:51 +08:00
|
|
|
cfg.ManageIPTables = ManageIPTables()
|
2022-01-26 13:46:12 +08:00
|
|
|
services := strings.Join(GetPortForwardServiceList(), ",")
|
|
|
|
cfg.PortForwardServices = services
|
2021-10-22 03:28:58 +08:00
|
|
|
|
2021-05-06 06:03:37 +08:00
|
|
|
return cfg
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetFrontendURL - gets the frontend url
|
2021-10-22 03:28:58 +08:00
|
|
|
func GetFrontendURL() string {
|
|
|
|
var frontend = ""
|
|
|
|
if os.Getenv("FRONTEND_URL") != "" {
|
|
|
|
frontend = os.Getenv("FRONTEND_URL")
|
|
|
|
} else if config.Config.Server.FrontendURL != "" {
|
|
|
|
frontend = config.Config.Server.FrontendURL
|
|
|
|
}
|
|
|
|
return frontend
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetAPIConnString - gets the api connections string
|
2021-07-11 12:49:31 +08:00
|
|
|
func GetAPIConnString() string {
|
2021-07-15 04:47:05 +08:00
|
|
|
conn := ""
|
|
|
|
if os.Getenv("SERVER_API_CONN_STRING") != "" {
|
|
|
|
conn = os.Getenv("SERVER_API_CONN_STRING")
|
|
|
|
} else if config.Config.Server.APIConnString != "" {
|
|
|
|
conn = config.Config.Server.APIConnString
|
|
|
|
}
|
|
|
|
return conn
|
2021-07-11 12:49:31 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
2022-02-08 20:01:39 +08:00
|
|
|
// SetVersion - set version of netmaker
|
|
|
|
func SetVersion(v string) {
|
|
|
|
Version = v
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetVersion - version of netmaker
|
2021-08-17 04:30:55 +08:00
|
|
|
func GetVersion() string {
|
2022-02-04 04:33:19 +08:00
|
|
|
return Version
|
2021-08-17 04:30:55 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetDB - gets the database type
|
2021-08-19 06:12:08 +08:00
|
|
|
func GetDB() string {
|
2021-09-11 03:48:18 +08:00
|
|
|
database := "sqlite"
|
2021-10-09 01:11:31 +08:00
|
|
|
if os.Getenv("DATABASE") != "" {
|
2021-08-19 06:12:08 +08:00
|
|
|
database = os.Getenv("DATABASE")
|
2021-10-09 01:11:31 +08:00
|
|
|
} else if config.Config.Server.Database != "" {
|
2021-08-19 06:12:08 +08:00
|
|
|
database = config.Config.Server.Database
|
|
|
|
}
|
|
|
|
return database
|
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetAPIHost - gets the api host
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetAPIHost() string {
|
2021-07-15 04:47:05 +08:00
|
|
|
serverhost := "127.0.0.1"
|
2021-07-28 21:31:18 +08:00
|
|
|
remoteip, _ := GetPublicIP()
|
2021-07-15 04:47:05 +08:00
|
|
|
if os.Getenv("SERVER_HTTP_HOST") != "" {
|
|
|
|
serverhost = os.Getenv("SERVER_HTTP_HOST")
|
|
|
|
} else if config.Config.Server.APIHost != "" {
|
2021-05-06 04:42:17 +08:00
|
|
|
serverhost = config.Config.Server.APIHost
|
2021-07-15 04:47:05 +08:00
|
|
|
} else if os.Getenv("SERVER_HOST") != "" {
|
|
|
|
serverhost = os.Getenv("SERVER_HOST")
|
|
|
|
} else {
|
|
|
|
if remoteip != "" {
|
|
|
|
serverhost = remoteip
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 04:42:17 +08:00
|
|
|
return serverhost
|
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetPodIP - get the pod's ip
|
2021-08-20 01:41:04 +08:00
|
|
|
func GetPodIP() string {
|
|
|
|
podip := "127.0.0.1"
|
|
|
|
if os.Getenv("POD_IP") != "" {
|
|
|
|
podip = os.Getenv("POD_IP")
|
|
|
|
}
|
|
|
|
return podip
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetAPIPort - gets the api port
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetAPIPort() string {
|
|
|
|
apiport := "8081"
|
|
|
|
if os.Getenv("API_PORT") != "" {
|
|
|
|
apiport = os.Getenv("API_PORT")
|
2021-07-15 04:47:05 +08:00
|
|
|
} else if config.Config.Server.APIPort != "" {
|
2021-05-06 04:42:17 +08:00
|
|
|
apiport = config.Config.Server.APIPort
|
|
|
|
}
|
|
|
|
return apiport
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetCheckinInterval - get check in interval for nodes
|
2021-10-03 00:28:17 +08:00
|
|
|
func GetCheckinInterval() string {
|
|
|
|
seconds := "15"
|
|
|
|
if os.Getenv("CHECKIN_INTERVAL") != "" {
|
|
|
|
seconds = os.Getenv("CHECKIN_INTERVAL")
|
|
|
|
} else if config.Config.Server.CheckinInterval != "" {
|
|
|
|
seconds = config.Config.Server.CheckinInterval
|
|
|
|
}
|
|
|
|
return seconds
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetDefaultNodeLimit - get node limit if one is set
|
2021-07-03 11:25:36 +08:00
|
|
|
func GetDefaultNodeLimit() int32 {
|
2021-07-15 04:47:05 +08:00
|
|
|
var limit int32
|
2021-07-03 11:25:36 +08:00
|
|
|
limit = 999999999
|
|
|
|
envlimit, err := strconv.Atoi(os.Getenv("DEFAULT_NODE_LIMIT"))
|
|
|
|
if err == nil && envlimit != 0 {
|
2021-07-15 04:47:05 +08:00
|
|
|
limit = int32(envlimit)
|
|
|
|
} else if config.Config.Server.DefaultNodeLimit != 0 {
|
|
|
|
limit = config.Config.Server.DefaultNodeLimit
|
|
|
|
}
|
|
|
|
return limit
|
2021-07-03 11:25:36 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetGRPCConnString - get grpc conn string
|
2021-07-11 12:49:31 +08:00
|
|
|
func GetGRPCConnString() string {
|
2021-07-15 04:47:05 +08:00
|
|
|
conn := ""
|
|
|
|
if os.Getenv("SERVER_GRPC_CONN_STRING") != "" {
|
|
|
|
conn = os.Getenv("SERVER_GRPC_CONN_STRING")
|
|
|
|
} else if config.Config.Server.GRPCConnString != "" {
|
|
|
|
conn = config.Config.Server.GRPCConnString
|
|
|
|
}
|
|
|
|
return conn
|
2021-07-11 12:49:31 +08:00
|
|
|
}
|
2021-07-03 11:25:36 +08:00
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetCoreDNSAddr - gets the core dns address
|
2021-07-16 03:14:48 +08:00
|
|
|
func GetCoreDNSAddr() string {
|
2021-08-17 04:30:55 +08:00
|
|
|
addr, _ := GetPublicIP()
|
|
|
|
if os.Getenv("COREDNS_ADDR") != "" {
|
|
|
|
addr = os.Getenv("COREDNS_ADDR")
|
|
|
|
} else if config.Config.Server.CoreDNSAddr != "" {
|
|
|
|
addr = config.Config.Server.GRPCConnString
|
|
|
|
}
|
|
|
|
return addr
|
2021-07-16 03:14:48 +08:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetGRPCHost - get the grpc host url
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetGRPCHost() string {
|
2021-05-26 00:48:04 +08:00
|
|
|
serverhost := "127.0.0.1"
|
2021-07-28 21:31:18 +08:00
|
|
|
remoteip, _ := GetPublicIP()
|
2021-08-10 05:34:13 +08:00
|
|
|
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")
|
2021-05-28 02:57:59 +08:00
|
|
|
} else {
|
2021-08-10 05:34:13 +08:00
|
|
|
if remoteip != "" {
|
|
|
|
serverhost = remoteip
|
2021-05-28 02:57:59 +08:00
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-07-15 04:47:05 +08:00
|
|
|
return serverhost
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetGRPCPort - gets the grpc port
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetGRPCPort() string {
|
2021-07-15 04:47:05 +08:00
|
|
|
grpcport := "50051"
|
2021-06-02 03:33:36 +08:00
|
|
|
if os.Getenv("GRPC_PORT") != "" {
|
2021-07-15 04:47:05 +08:00
|
|
|
grpcport = os.Getenv("GRPC_PORT")
|
|
|
|
} else if config.Config.Server.GRPCPort != "" {
|
|
|
|
grpcport = config.Config.Server.GRPCPort
|
2021-05-30 03:06:35 +08:00
|
|
|
}
|
2021-07-15 04:47:05 +08:00
|
|
|
return grpcport
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
2022-01-13 05:23:34 +08:00
|
|
|
// GetMessageQueueEndpoint - gets the message queue endpoint
|
|
|
|
func GetMessageQueueEndpoint() string {
|
|
|
|
host, _ := GetPublicIP()
|
|
|
|
if os.Getenv("MQ_HOST") != "" {
|
|
|
|
host = os.Getenv("MQ_HOST")
|
|
|
|
} else if config.Config.Server.MQHOST != "" {
|
|
|
|
host = config.Config.Server.MQHOST
|
|
|
|
}
|
|
|
|
//Do we want MQ port configurable???
|
|
|
|
return host + ":1883"
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetMasterKey - gets the configured master key of server
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetMasterKey() string {
|
2022-02-14 22:58:50 +08:00
|
|
|
key := ""
|
2021-07-15 04:47:05 +08:00
|
|
|
if os.Getenv("MASTER_KEY") != "" {
|
|
|
|
key = os.Getenv("MASTER_KEY")
|
|
|
|
} else if config.Config.Server.MasterKey != "" {
|
|
|
|
key = config.Config.Server.MasterKey
|
|
|
|
}
|
|
|
|
return key
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
2021-11-16 00:42:52 +08:00
|
|
|
// GetDNSKey - gets the configured dns key of server
|
|
|
|
func GetDNSKey() string {
|
|
|
|
key := "secretkey"
|
|
|
|
if os.Getenv("DNS_KEY") != "" {
|
|
|
|
key = os.Getenv("DNS_KEY")
|
|
|
|
} else if config.Config.Server.DNSKey != "" {
|
|
|
|
key = config.Config.Server.DNSKey
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetAllowedOrigin - get the allowed origin
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetAllowedOrigin() string {
|
2021-07-15 04:47:05 +08:00
|
|
|
allowedorigin := "*"
|
|
|
|
if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
|
|
|
|
allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
|
|
|
|
} else if config.Config.Server.AllowedOrigin != "" {
|
|
|
|
allowedorigin = config.Config.Server.AllowedOrigin
|
|
|
|
}
|
|
|
|
return allowedorigin
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// IsRestBackend - checks if rest is on or off
|
2021-05-06 04:42:17 +08:00
|
|
|
func IsRestBackend() bool {
|
2021-07-15 04:47:05 +08:00
|
|
|
isrest := true
|
|
|
|
if os.Getenv("REST_BACKEND") != "" {
|
2021-05-06 06:03:37 +08:00
|
|
|
if os.Getenv("REST_BACKEND") == "off" {
|
2021-05-06 04:42:17 +08:00
|
|
|
isrest = false
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.RestBackend != "" {
|
|
|
|
if config.Config.Server.RestBackend == "off" {
|
|
|
|
isrest = false
|
|
|
|
}
|
2021-07-15 04:47:05 +08:00
|
|
|
}
|
|
|
|
return isrest
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// IsAgentBackend - checks if agent backed is on or off
|
2021-05-06 04:42:17 +08:00
|
|
|
func IsAgentBackend() bool {
|
2021-07-15 04:47:05 +08:00
|
|
|
isagent := true
|
|
|
|
if os.Getenv("AGENT_BACKEND") != "" {
|
|
|
|
if os.Getenv("AGENT_BACKEND") == "off" {
|
|
|
|
isagent = false
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.AgentBackend != "" {
|
|
|
|
if config.Config.Server.AgentBackend == "off" {
|
|
|
|
isagent = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isagent
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
2022-01-13 05:23:34 +08:00
|
|
|
// IsMessageQueueBackend - checks if message queue is on or off
|
|
|
|
func IsMessageQueueBackend() bool {
|
|
|
|
ismessagequeue := true
|
|
|
|
if os.Getenv("MESSAGEQUEUE_BACKEND") != "" {
|
|
|
|
if os.Getenv("MESSAGEQUEUE_BACKEND") == "off" {
|
|
|
|
ismessagequeue = false
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.MessageQueueBackend != "" {
|
|
|
|
if config.Config.Server.MessageQueueBackend == "off" {
|
|
|
|
ismessagequeue = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ismessagequeue
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// IsClientMode - checks if it should run in client mode
|
2021-09-28 05:51:20 +08:00
|
|
|
func IsClientMode() string {
|
|
|
|
isclient := "on"
|
2022-01-19 23:44:00 +08:00
|
|
|
if os.Getenv("CLIENT_MODE") == "off" {
|
|
|
|
isclient = "off"
|
|
|
|
}
|
|
|
|
if config.Config.Server.ClientMode == "off" {
|
|
|
|
isclient = "off"
|
2021-07-15 04:47:05 +08:00
|
|
|
}
|
|
|
|
return isclient
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
2022-01-21 06:52:49 +08:00
|
|
|
// Telemetry - checks if telemetry data should be sent
|
2022-01-21 06:50:42 +08:00
|
|
|
func Telemetry() string {
|
|
|
|
telemetry := "on"
|
|
|
|
if os.Getenv("TELEMETRY") == "off" {
|
|
|
|
telemetry = "off"
|
|
|
|
}
|
|
|
|
if config.Config.Server.Telemetry == "off" {
|
|
|
|
telemetry = "off"
|
|
|
|
}
|
|
|
|
return telemetry
|
|
|
|
}
|
|
|
|
|
2022-01-26 00:58:51 +08:00
|
|
|
// ManageIPTables - checks if iptables should be manipulated on host
|
|
|
|
func ManageIPTables() string {
|
|
|
|
manage := "on"
|
|
|
|
if os.Getenv("MANAGE_IPTABLES") == "off" {
|
|
|
|
manage = "off"
|
|
|
|
}
|
|
|
|
if config.Config.Server.ManageIPTables == "off" {
|
|
|
|
manage = "off"
|
|
|
|
}
|
|
|
|
return manage
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// IsDNSMode - should it run with DNS
|
2021-05-06 04:42:17 +08:00
|
|
|
func IsDNSMode() bool {
|
2021-07-15 04:47:05 +08:00
|
|
|
isdns := true
|
|
|
|
if os.Getenv("DNS_MODE") != "" {
|
|
|
|
if os.Getenv("DNS_MODE") == "off" {
|
|
|
|
isdns = false
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.DNSMode != "" {
|
|
|
|
if config.Config.Server.DNSMode == "off" {
|
|
|
|
isdns = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isdns
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-07-11 12:49:31 +08:00
|
|
|
|
2021-11-15 05:50:20 +08:00
|
|
|
// IsDisplayKeys - should server be able to display keys?
|
|
|
|
func IsDisplayKeys() bool {
|
|
|
|
isdisplay := true
|
|
|
|
if os.Getenv("DISPLAY_KEYS") != "" {
|
|
|
|
if os.Getenv("DISPLAY_KEYS") == "off" {
|
|
|
|
isdisplay = false
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.DisplayKeys != "" {
|
|
|
|
if config.Config.Server.DisplayKeys == "off" {
|
|
|
|
isdisplay = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isdisplay
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// IsGRPCSSL - ssl grpc on or off
|
2021-07-11 12:49:31 +08:00
|
|
|
func IsGRPCSSL() bool {
|
2021-07-15 04:47:05 +08:00
|
|
|
isssl := false
|
|
|
|
if os.Getenv("GRPC_SSL") != "" {
|
|
|
|
if os.Getenv("GRPC_SSL") == "on" {
|
|
|
|
isssl = true
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.DNSMode != "" {
|
|
|
|
if config.Config.Server.DNSMode == "on" {
|
|
|
|
isssl = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isssl
|
2021-07-11 12:49:31 +08:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// DisableRemoteIPCheck - disable the remote ip check
|
2021-05-06 04:42:17 +08:00
|
|
|
func DisableRemoteIPCheck() bool {
|
2021-07-15 04:47:05 +08:00
|
|
|
disabled := false
|
|
|
|
if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
|
|
|
|
if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
|
|
|
|
disabled = true
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.DisableRemoteIPCheck != "" {
|
|
|
|
if config.Config.Server.DisableRemoteIPCheck == "on" {
|
|
|
|
disabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return disabled
|
2021-05-06 04:42:17 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// DisableDefaultNet - disable default net
|
2021-07-11 12:49:31 +08:00
|
|
|
func DisableDefaultNet() bool {
|
2021-07-15 04:47:05 +08:00
|
|
|
disabled := false
|
|
|
|
if os.Getenv("DISABLE_DEFAULT_NET") != "" {
|
|
|
|
if os.Getenv("DISABLE_DEFAULT_NET") == "on" {
|
|
|
|
disabled = true
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.DisableDefaultNet != "" {
|
|
|
|
if config.Config.Server.DisableDefaultNet == "on" {
|
|
|
|
disabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return disabled
|
2021-07-11 12:49:31 +08:00
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetPublicIP - gets public ip
|
2021-05-06 04:42:17 +08:00
|
|
|
func GetPublicIP() (string, error) {
|
|
|
|
|
2021-07-15 04:47:05 +08:00
|
|
|
endpoint := ""
|
|
|
|
var err error
|
2021-05-06 04:42:17 +08:00
|
|
|
|
2021-11-17 22:04:55 +08:00
|
|
|
iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
|
2021-07-15 04:47:05 +08:00
|
|
|
for _, ipserver := range iplist {
|
|
|
|
resp, err := http.Get(ipserver)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
2022-01-07 04:05:38 +08:00
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
2021-07-15 04:47:05 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
endpoint = string(bodyBytes)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil && endpoint == "" {
|
2021-12-07 04:31:08 +08:00
|
|
|
err = errors.New("public address not found")
|
2021-07-15 04:47:05 +08:00
|
|
|
}
|
|
|
|
return endpoint, err
|
|
|
|
}
|
2021-10-27 04:12:13 +08:00
|
|
|
|
|
|
|
// GetPlatform - get the system type of server
|
2021-08-20 01:41:04 +08:00
|
|
|
func GetPlatform() string {
|
|
|
|
platform := "linux"
|
|
|
|
if os.Getenv("PLATFORM") != "" {
|
|
|
|
platform = os.Getenv("PLATFORM")
|
2021-08-26 01:09:23 +08:00
|
|
|
} else if config.Config.Server.Platform != "" {
|
2021-08-20 01:41:04 +08:00
|
|
|
platform = config.Config.Server.SQLConn
|
|
|
|
}
|
|
|
|
return platform
|
|
|
|
}
|
|
|
|
|
2022-01-26 00:58:51 +08:00
|
|
|
// GetIPForwardServiceList - get the list of services that the server should be forwarding
|
|
|
|
func GetPortForwardServiceList() []string {
|
|
|
|
//services := "mq,dns,ssh"
|
|
|
|
services := ""
|
|
|
|
if os.Getenv("PORT_FORWARD_SERVICES") != "" {
|
|
|
|
services = os.Getenv("PORT_FORWARD_SERVICES")
|
|
|
|
} else if config.Config.Server.PortForwardServices != "" {
|
|
|
|
services = config.Config.Server.PortForwardServices
|
|
|
|
}
|
|
|
|
serviceSlice := strings.Split(services, ",")
|
|
|
|
return serviceSlice
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetSQLConn - get the sql connection string
|
2021-08-19 02:55:41 +08:00
|
|
|
func GetSQLConn() string {
|
|
|
|
sqlconn := "http://"
|
|
|
|
if os.Getenv("SQL_CONN") != "" {
|
|
|
|
sqlconn = os.Getenv("SQL_CONN")
|
2021-08-19 06:12:08 +08:00
|
|
|
} else if config.Config.Server.SQLConn != "" {
|
2021-08-19 02:55:41 +08:00
|
|
|
sqlconn = config.Config.Server.SQLConn
|
|
|
|
}
|
|
|
|
return sqlconn
|
2021-08-19 06:12:08 +08:00
|
|
|
}
|
2021-09-29 01:16:41 +08:00
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// IsSplitDNS - checks if split dns is on
|
2021-09-29 01:16:41 +08:00
|
|
|
func IsSplitDNS() bool {
|
|
|
|
issplit := false
|
|
|
|
if os.Getenv("IS_SPLIT_DNS") == "yes" {
|
|
|
|
issplit = true
|
|
|
|
} else if config.Config.Server.SplitDNS == "yes" {
|
|
|
|
issplit = true
|
|
|
|
}
|
|
|
|
return issplit
|
|
|
|
}
|
2021-10-09 10:46:39 +08:00
|
|
|
|
2022-01-26 13:46:12 +08:00
|
|
|
// IsSplitDNS - checks if split dns is on
|
|
|
|
func IsHostNetwork() bool {
|
|
|
|
ishost := false
|
|
|
|
if os.Getenv("HOST_NETWORK") == "on" {
|
|
|
|
ishost = true
|
|
|
|
} else if config.Config.Server.HostNetwork == "on" {
|
|
|
|
ishost = true
|
|
|
|
}
|
|
|
|
return ishost
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetNodeID - gets the node id
|
2021-10-09 10:46:39 +08:00
|
|
|
func GetNodeID() string {
|
|
|
|
var id string
|
2022-02-01 22:59:23 +08:00
|
|
|
// id = getMacAddr()
|
2021-10-09 10:46:39 +08:00
|
|
|
if os.Getenv("NODE_ID") != "" {
|
|
|
|
id = os.Getenv("NODE_ID")
|
|
|
|
} else if config.Config.Server.NodeID != "" {
|
|
|
|
id = config.Config.Server.NodeID
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:12:13 +08:00
|
|
|
// GetServerCheckinInterval - gets the server check-in time
|
2021-10-18 21:28:46 +08:00
|
|
|
func GetServerCheckinInterval() int64 {
|
|
|
|
var t = int64(5)
|
|
|
|
var envt, _ = strconv.Atoi(os.Getenv("SERVER_CHECKIN_INTERVAL"))
|
|
|
|
if envt > 0 {
|
|
|
|
t = int64(envt)
|
|
|
|
} else if config.Config.Server.ServerCheckinInterval > 0 {
|
|
|
|
t = config.Config.Server.ServerCheckinInterval
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-10-21 02:17:31 +08:00
|
|
|
// GetAuthProviderInfo = gets the oauth provider info
|
|
|
|
func GetAuthProviderInfo() []string {
|
|
|
|
var authProvider = ""
|
|
|
|
if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
|
|
|
|
authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
|
|
|
|
if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
|
|
|
|
return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
|
|
|
|
} else {
|
|
|
|
authProvider = ""
|
|
|
|
}
|
|
|
|
} else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
|
|
|
|
authProvider = strings.ToLower(config.Config.Server.AuthProvider)
|
|
|
|
if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
|
|
|
|
return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []string{"", "", ""}
|
|
|
|
}
|
|
|
|
|
2022-01-03 23:01:37 +08:00
|
|
|
// GetAzureTenant - retrieve the azure tenant ID from env variable or config file
|
|
|
|
func GetAzureTenant() string {
|
|
|
|
var azureTenant = ""
|
|
|
|
if os.Getenv("AZURE_TENANT") != "" {
|
|
|
|
azureTenant = os.Getenv("AZURE_TENANT")
|
|
|
|
} else if config.Config.Server.AzureTenant != "" {
|
|
|
|
azureTenant = config.Config.Server.AzureTenant
|
|
|
|
}
|
|
|
|
return azureTenant
|
|
|
|
}
|
|
|
|
|
2021-10-09 10:46:39 +08:00
|
|
|
// GetMacAddr - get's mac address
|
|
|
|
func getMacAddr() string {
|
|
|
|
ifas, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var as []string
|
|
|
|
for _, ifa := range ifas {
|
|
|
|
a := ifa.HardwareAddr.String()
|
|
|
|
if a != "" {
|
|
|
|
as = append(as, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return as[0]
|
2021-10-14 03:15:20 +08:00
|
|
|
}
|
2022-01-06 02:13:03 +08:00
|
|
|
|
|
|
|
// GetRce - sees if Rce is enabled, off by default
|
|
|
|
func GetRce() bool {
|
|
|
|
return os.Getenv("RCE") == "on" || config.Config.Server.RCE == "on"
|
|
|
|
}
|
2022-01-21 18:19:17 +08:00
|
|
|
|
|
|
|
// GetDebug -- checks if debugging is enabled, off by default
|
|
|
|
func GetDebug() bool {
|
|
|
|
return os.Getenv("DEBUG") == "on" || config.Config.Server.Debug == true
|
|
|
|
}
|