| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 | package servercfgimport (	"errors"	"io"	"net/http"	"os"	"strconv"	"strings"	"time"	"github.com/gravitl/netmaker/config"	"github.com/gravitl/netmaker/models")// EmqxBrokerType denotes the broker type for EMQX MQTTconst EmqxBrokerType = "emqx"// Emqxdeploy - emqx deploy typetype Emqxdeploy stringvar (	Version              = "dev"	IsPro                = false	ErrLicenseValidation error	EmqxCloudDeploy      Emqxdeploy = "cloud"	EmqxOnPremDeploy     Emqxdeploy = "on-prem")// SetHost - sets the host ipfunc SetHost() error {	remoteip, err := GetPublicIP()	if err != nil {		return err	}	os.Setenv("SERVER_HOST", remoteip)	return nil}// GetServerConfig - gets the server config into memory from file or envfunc GetServerConfig() config.ServerConfig {	var cfg config.ServerConfig	cfg.APIConnString = GetAPIConnString()	cfg.CoreDNSAddr = GetCoreDNSAddr()	cfg.APIHost = GetAPIHost()	cfg.APIPort = GetAPIPort()	cfg.MasterKey = "(hidden)"	cfg.DNSKey = "(hidden)"	cfg.AllowedOrigin = GetAllowedOrigin()	cfg.RestBackend = "off"	cfg.NodeID = GetNodeID()	cfg.BrokerType = GetBrokerType()	cfg.EmqxRestEndpoint = GetEmqxRestEndpoint()	if AutoUpdateEnabled() {		cfg.NetclientAutoUpdate = "enabled"	} else {		cfg.NetclientAutoUpdate = "disabled"	}	if IsRestBackend() {		cfg.RestBackend = "on"	}	cfg.DNSMode = "off"	if IsDNSMode() {		cfg.DNSMode = "on"	}	cfg.DisplayKeys = "off"	if IsDisplayKeys() {		cfg.DisplayKeys = "on"	}	cfg.DisableRemoteIPCheck = "off"	if DisableRemoteIPCheck() {		cfg.DisableRemoteIPCheck = "on"	}	cfg.Database = GetDB()	cfg.Platform = GetPlatform()	cfg.Version = GetVersion()	// == auth config ==	var authInfo = GetAuthProviderInfo()	cfg.AuthProvider = authInfo[0]	cfg.ClientID = authInfo[1]	cfg.ClientSecret = authInfo[2]	cfg.FrontendURL = GetFrontendURL()	cfg.Telemetry = Telemetry()	cfg.Server = GetServer()	cfg.Verbosity = GetVerbosity()	cfg.IsPro = "no"	if IsPro {		cfg.IsPro = "yes"	}	cfg.JwtValidityDuration = GetJwtValidityDuration()	cfg.RacAutoDisable = GetRacAutoDisable()	return cfg}// GetJwtValidityDuration - returns the JWT validity duration in secondsfunc GetJwtValidityDuration() time.Duration {	var defaultDuration = time.Duration(24) * time.Hour	if os.Getenv("JWT_VALIDITY_DURATION") != "" {		t, err := strconv.Atoi(os.Getenv("JWT_VALIDITY_DURATION"))		if err != nil {			return defaultDuration		}		return time.Duration(t) * time.Second	}	return defaultDuration}// GetRacAutoDisable - returns whether the feature to autodisable RAC is enabledfunc GetRacAutoDisable() bool {	return os.Getenv("RAC_AUTO_DISABLE") == "true"}// GetServerInfo - gets the server config into memory from file or envfunc GetServerInfo() models.ServerConfig {	var cfg models.ServerConfig	cfg.Server = GetServer()	if GetBrokerType() == EmqxBrokerType {		cfg.MQUserName = "HOST_ID"		cfg.MQPassword = "HOST_PASS"	} else {		cfg.MQUserName = GetMqUserName()		cfg.MQPassword = GetMqPassword()	}	cfg.API = GetAPIConnString()	cfg.CoreDNSAddr = GetCoreDNSAddr()	cfg.APIPort = GetAPIPort()	cfg.DNSMode = "off"	cfg.Broker = GetPublicBrokerEndpoint()	cfg.BrokerType = GetBrokerType()	if IsDNSMode() {		cfg.DNSMode = "on"	}	cfg.Version = GetVersion()	cfg.IsPro = IsPro	return cfg}// GetFrontendURL - gets the frontend urlfunc 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}// GetAPIConnString - gets the api connections stringfunc GetAPIConnString() string {	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}// SetVersion - set version of netmakerfunc SetVersion(v string) {	Version = v}// GetVersion - version of netmakerfunc GetVersion() string {	return Version}// GetDB - gets the database typefunc GetDB() string {	database := "sqlite"	if os.Getenv("DATABASE") != "" {		database = os.Getenv("DATABASE")	} else if config.Config.Server.Database != "" {		database = config.Config.Server.Database	}	return database}// CacheEnabled - checks if cache is enabledfunc CacheEnabled() bool {	caching := true	if os.Getenv("CACHING_ENABLED") == "false" {		caching = false	} else if config.Config.Server.CacheEnabled == "false" {		caching = false	}	return caching}// GetAPIHost - gets the api hostfunc GetAPIHost() string {	serverhost := "127.0.0.1"	remoteip, _ := GetPublicIP()	if os.Getenv("SERVER_HTTP_HOST") != "" {		serverhost = os.Getenv("SERVER_HTTP_HOST")	} else if config.Config.Server.APIHost != "" {		serverhost = config.Config.Server.APIHost	} else if os.Getenv("SERVER_HOST") != "" {		serverhost = os.Getenv("SERVER_HOST")	} else {		if remoteip != "" {			serverhost = remoteip		}	}	return serverhost}// GetAPIPort - gets the api portfunc GetAPIPort() string {	apiport := "8081"	if os.Getenv("API_PORT") != "" {		apiport = os.Getenv("API_PORT")	} else if config.Config.Server.APIPort != "" {		apiport = config.Config.Server.APIPort	}	return apiport}// GetCoreDNSAddr - gets the core dns addressfunc GetCoreDNSAddr() string {	addr, _ := GetPublicIP()	if os.Getenv("COREDNS_ADDR") != "" {		addr = os.Getenv("COREDNS_ADDR")	} else if config.Config.Server.CoreDNSAddr != "" {		addr = config.Config.Server.CoreDNSAddr	}	return addr}// GetPublicBrokerEndpoint - returns the public broker endpoint which shall be used by netclientfunc GetPublicBrokerEndpoint() string {	if os.Getenv("BROKER_ENDPOINT") != "" {		return os.Getenv("BROKER_ENDPOINT")	} else {		return config.Config.Server.Broker	}}// GetOwnerEmail - gets the owner email (saas)func GetOwnerEmail() string {	return os.Getenv("SAAS_OWNER_EMAIL")}// GetMessageQueueEndpoint - gets the message queue endpointfunc GetMessageQueueEndpoint() (string, bool) {	host, _ := GetPublicIP()	if os.Getenv("SERVER_BROKER_ENDPOINT") != "" {		host = os.Getenv("SERVER_BROKER_ENDPOINT")	} else if config.Config.Server.ServerBrokerEndpoint != "" {		host = config.Config.Server.ServerBrokerEndpoint	} else if os.Getenv("BROKER_ENDPOINT") != "" {		host = os.Getenv("BROKER_ENDPOINT")	} else if config.Config.Server.Broker != "" {		host = config.Config.Server.Broker	} else {		host += ":1883" // default	}	return host, strings.Contains(host, "wss") || strings.Contains(host, "ssl") || strings.Contains(host, "mqtts")}// GetBrokerType - returns the type of MQ brokerfunc GetBrokerType() string {	if os.Getenv("BROKER_TYPE") != "" {		return os.Getenv("BROKER_TYPE")	} else {		return "mosquitto"	}}// GetMasterKey - gets the configured master key of serverfunc GetMasterKey() string {	key := ""	if os.Getenv("MASTER_KEY") != "" {		key = os.Getenv("MASTER_KEY")	} else if config.Config.Server.MasterKey != "" {		key = config.Config.Server.MasterKey	}	return key}// GetAllowedOrigin - get the allowed originfunc GetAllowedOrigin() string {	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}// IsRestBackend - checks if rest is on or offfunc IsRestBackend() bool {	isrest := true	if os.Getenv("REST_BACKEND") != "" {		if os.Getenv("REST_BACKEND") == "off" {			isrest = false		}	} else if config.Config.Server.RestBackend != "" {		if config.Config.Server.RestBackend == "off" {			isrest = false		}	}	return isrest}// IsMetricsExporter - checks if metrics exporter is on or offfunc IsMetricsExporter() bool {	export := false	if os.Getenv("METRICS_EXPORTER") != "" {		if os.Getenv("METRICS_EXPORTER") == "on" {			export = true		}	} else if config.Config.Server.MetricsExporter != "" {		if config.Config.Server.MetricsExporter == "on" {			export = true		}	}	return export}// IsMessageQueueBackend - checks if message queue is on or offfunc 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}// Telemetry - checks if telemetry data should be sentfunc Telemetry() string {	telemetry := "on"	if os.Getenv("TELEMETRY") == "off" {		telemetry = "off"	}	if config.Config.Server.Telemetry == "off" {		telemetry = "off"	}	return telemetry}// GetServer - gets the server namefunc GetServer() string {	server := ""	if os.Getenv("SERVER_NAME") != "" {		server = os.Getenv("SERVER_NAME")	} else if config.Config.Server.Server != "" {		server = config.Config.Server.Server	}	return server}func GetVerbosity() int32 {	var verbosity = 0	var err error	if os.Getenv("VERBOSITY") != "" {		verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))		if err != nil {			verbosity = 0		}	} else if config.Config.Server.Verbosity != 0 {		verbosity = int(config.Config.Server.Verbosity)	}	if verbosity < 0 || verbosity > 4 {		verbosity = 0	}	return int32(verbosity)}// AutoUpdateEnabled returns a boolean indicating whether netclient auto update is enabled or disabled// default is enabledfunc AutoUpdateEnabled() bool {	if os.Getenv("NETCLIENT_AUTO_UPDATE") == "disabled" {		return false	} else if config.Config.Server.NetclientAutoUpdate == "disabled" {		return false	}	return true}// IsDNSMode - should it run with DNSfunc IsDNSMode() bool {	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}// 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}// DisableRemoteIPCheck - disable the remote ip checkfunc DisableRemoteIPCheck() bool {	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}// GetPublicIP - gets public ipfunc GetPublicIP() (string, error) {	endpoint := ""	var err error	iplist := []string{"https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}	publicIpService := os.Getenv("PUBLIC_IP_SERVICE")	if publicIpService != "" {		// prepend the user-specified service so it's checked first		iplist = append([]string{publicIpService}, iplist...)	} else if config.Config.Server.PublicIPService != "" {		publicIpService = config.Config.Server.PublicIPService		// prepend the user-specified service so it's checked first		iplist = append([]string{publicIpService}, iplist...)	}	for _, ipserver := range iplist {		client := &http.Client{			Timeout: time.Second * 10,		}		resp, err := client.Get(ipserver)		if err != nil {			continue		}		defer resp.Body.Close()		if resp.StatusCode == http.StatusOK {			bodyBytes, err := io.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}// GetPlatform - get the system type of serverfunc GetPlatform() string {	platform := "linux"	if os.Getenv("PLATFORM") != "" {		platform = os.Getenv("PLATFORM")	} else if config.Config.Server.Platform != "" {		platform = config.Config.Server.Platform	}	return platform}// GetSQLConn - get the sql connection stringfunc GetSQLConn() string {	sqlconn := "http://"	if os.Getenv("SQL_CONN") != "" {		sqlconn = os.Getenv("SQL_CONN")	} else if config.Config.Server.SQLConn != "" {		sqlconn = config.Config.Server.SQLConn	}	return sqlconn}// GetNodeID - gets the node idfunc GetNodeID() string {	var id string	var err error	// id = getMacAddr()	if os.Getenv("NODE_ID") != "" {		id = os.Getenv("NODE_ID")	} else if config.Config.Server.NodeID != "" {		id = config.Config.Server.NodeID	} else {		id, err = os.Hostname()		if err != nil {			return ""		}	}	return id}func SetNodeID(id string) {	config.Config.Server.NodeID = id}// GetAuthProviderInfo = gets the oauth provider infofunc GetAuthProviderInfo() (pi []string) {	var authProvider = ""	defer func() {		if authProvider == "oidc" {			if os.Getenv("OIDC_ISSUER") != "" {				pi = append(pi, os.Getenv("OIDC_ISSUER"))			} else if config.Config.Server.OIDCIssuer != "" {				pi = append(pi, config.Config.Server.OIDCIssuer)			} else {				pi = []string{"", "", ""}			}		}	}()	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" || authProvider == "oidc" {			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" || authProvider == "oidc" {			return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}		}	}	return []string{"", "", ""}}// GetAzureTenant - retrieve the azure tenant ID from env variable or config filefunc 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}// GetMqPassword - fetches the MQ passwordfunc GetMqPassword() string {	password := ""	if os.Getenv("MQ_PASSWORD") != "" {		password = os.Getenv("MQ_PASSWORD")	} else if config.Config.Server.MQPassword != "" {		password = config.Config.Server.MQPassword	}	return password}// GetMqUserName - fetches the MQ usernamefunc GetMqUserName() string {	password := ""	if os.Getenv("MQ_USERNAME") != "" {		password = os.Getenv("MQ_USERNAME")	} else if config.Config.Server.MQUserName != "" {		password = config.Config.Server.MQUserName	}	return password}// GetEmqxRestEndpoint - returns the REST API Endpoint of EMQXfunc GetEmqxRestEndpoint() string {	return os.Getenv("EMQX_REST_ENDPOINT")}// IsBasicAuthEnabled - checks if basic auth has been configured to be turned offfunc IsBasicAuthEnabled() bool {	var enabled = true //default	if os.Getenv("BASIC_AUTH") != "" {		enabled = os.Getenv("BASIC_AUTH") == "yes"	} else if config.Config.Server.BasicAuth != "" {		enabled = config.Config.Server.BasicAuth == "yes"	}	return enabled}// GetLicenseKey - retrieves pro license value from env or conf filesfunc GetLicenseKey() string {	licenseKeyValue := os.Getenv("LICENSE_KEY")	if licenseKeyValue == "" {		licenseKeyValue = config.Config.Server.LicenseValue	}	return licenseKeyValue}// GetNetmakerTenantID - get's the associated, Netmaker, tenant ID to verify ownershipfunc GetNetmakerTenantID() string {	netmakerTenantID := os.Getenv("NETMAKER_TENANT_ID")	if netmakerTenantID == "" {		netmakerTenantID = config.Config.Server.NetmakerTenantID	}	return netmakerTenantID}// GetUserLimit - fetches free tier limits on usersfunc GetUserLimit() int {	var userslimit int	if os.Getenv("USERS_LIMIT") != "" {		userslimit, _ = strconv.Atoi(os.Getenv("USERS_LIMIT"))	} else {		userslimit = config.Config.Server.UsersLimit	}	return userslimit}// GetNetworkLimit - fetches free tier limits on networksfunc GetNetworkLimit() int {	var networkslimit int	if os.Getenv("NETWORKS_LIMIT") != "" {		networkslimit, _ = strconv.Atoi(os.Getenv("NETWORKS_LIMIT"))	} else {		networkslimit = config.Config.Server.NetworksLimit	}	return networkslimit}// GetMachinesLimit - fetches free tier limits on machines (clients + hosts)func GetMachinesLimit() int {	if l, err := strconv.Atoi(os.Getenv("MACHINES_LIMIT")); err == nil {		return l	}	return config.Config.Server.MachinesLimit}// GetIngressLimit - fetches free tier limits on ingressesfunc GetIngressLimit() int {	if l, err := strconv.Atoi(os.Getenv("INGRESSES_LIMIT")); err == nil {		return l	}	return config.Config.Server.IngressesLimit}// GetEgressLimit - fetches free tier limits on egressesfunc GetEgressLimit() int {	if l, err := strconv.Atoi(os.Getenv("EGRESSES_LIMIT")); err == nil {		return l	}	return config.Config.Server.EgressesLimit}// DeployedByOperator - returns true if the instance is deployed by netmaker operatorfunc DeployedByOperator() bool {	if os.Getenv("DEPLOYED_BY_OPERATOR") != "" {		return os.Getenv("DEPLOYED_BY_OPERATOR") == "true"	}	return config.Config.Server.DeployedByOperator}// IsEndpointDetectionEnabled - returns true if endpoint detection enabledfunc IsEndpointDetectionEnabled() bool {	var enabled = true //default	if os.Getenv("ENDPOINT_DETECTION") != "" {		enabled = os.Getenv("ENDPOINT_DETECTION") == "true"	}	return enabled}// GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)func GetEnvironment() string {	if env := os.Getenv("ENVIRONMENT"); env != "" {		return env	}	if env := config.Config.Server.Environment; env != "" {		return env	}	return ""}// GetEmqxDeployType - fetches emqx deploy type this server usesfunc GetEmqxDeployType() (deployType Emqxdeploy) {	deployType = EmqxOnPremDeploy	if os.Getenv("EMQX_DEPLOY_TYPE") == string(EmqxCloudDeploy) {		deployType = EmqxCloudDeploy	}	return}// GetEmqxAppID - gets the emqx cloud app idfunc GetEmqxAppID() string {	return os.Getenv("EMQX_APP_ID")}// GetEmqxAppSecret - gets the emqx cloud app secretfunc GetEmqxAppSecret() string {	return os.Getenv("EMQX_APP_SECRET")}// GetAllowedEmailDomains - gets the allowed email domains for oauth signupfunc GetAllowedEmailDomains() string {	allowedDomains := "*"	if os.Getenv("ALLOWED_EMAIL_DOMAINS") != "" {		allowedDomains = os.Getenv("ALLOWED_EMAIL_DOMAINS")	} else if config.Config.Server.AllowedEmailDomains != "" {		allowedDomains = config.Config.Server.AllowedEmailDomains	}	return allowedDomains}
 |