serverconf.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. package servercfg
  2. import (
  3. "errors"
  4. "io"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/gravitl/netmaker/config"
  11. "github.com/gravitl/netmaker/models"
  12. )
  13. // EmqxBrokerType denotes the broker type for EMQX MQTT
  14. const EmqxBrokerType = "emqx"
  15. var (
  16. Version = "dev"
  17. IsPro = false
  18. ErrLicenseValidation error
  19. )
  20. // SetHost - sets the host ip
  21. func SetHost() error {
  22. remoteip, err := GetPublicIP()
  23. if err != nil {
  24. return err
  25. }
  26. os.Setenv("SERVER_HOST", remoteip)
  27. return nil
  28. }
  29. // GetServerConfig - gets the server config into memory from file or env
  30. func GetServerConfig() config.ServerConfig {
  31. var cfg config.ServerConfig
  32. cfg.APIConnString = GetAPIConnString()
  33. cfg.CoreDNSAddr = GetCoreDNSAddr()
  34. cfg.APIHost = GetAPIHost()
  35. cfg.APIPort = GetAPIPort()
  36. cfg.MasterKey = "(hidden)"
  37. cfg.DNSKey = "(hidden)"
  38. cfg.AllowedOrigin = GetAllowedOrigin()
  39. cfg.RestBackend = "off"
  40. cfg.NodeID = GetNodeID()
  41. cfg.StunPort = GetStunPort()
  42. cfg.BrokerType = GetBrokerType()
  43. cfg.EmqxRestEndpoint = GetEmqxRestEndpoint()
  44. if AutoUpdateEnabled() {
  45. cfg.NetclientAutoUpdate = "enabled"
  46. } else {
  47. cfg.NetclientAutoUpdate = "disabled"
  48. }
  49. if IsRestBackend() {
  50. cfg.RestBackend = "on"
  51. }
  52. cfg.DNSMode = "off"
  53. if IsDNSMode() {
  54. cfg.DNSMode = "on"
  55. }
  56. cfg.DisplayKeys = "off"
  57. if IsDisplayKeys() {
  58. cfg.DisplayKeys = "on"
  59. }
  60. cfg.DisableRemoteIPCheck = "off"
  61. if DisableRemoteIPCheck() {
  62. cfg.DisableRemoteIPCheck = "on"
  63. }
  64. cfg.Database = GetDB()
  65. cfg.Platform = GetPlatform()
  66. cfg.Version = GetVersion()
  67. // == auth config ==
  68. var authInfo = GetAuthProviderInfo()
  69. cfg.AuthProvider = authInfo[0]
  70. cfg.ClientID = authInfo[1]
  71. cfg.ClientSecret = authInfo[2]
  72. cfg.FrontendURL = GetFrontendURL()
  73. cfg.Telemetry = Telemetry()
  74. cfg.Server = GetServer()
  75. cfg.Verbosity = GetVerbosity()
  76. cfg.IsPro = "no"
  77. if IsPro {
  78. cfg.IsPro = "yes"
  79. }
  80. cfg.JwtValidityDuration = GetJwtValidityDuration()
  81. cfg.RacAutoDisable = GetRacAutoDisable()
  82. return cfg
  83. }
  84. // GetJwtValidityDuration - returns the JWT validity duration in seconds
  85. func GetJwtValidityDuration() time.Duration {
  86. var defaultDuration = time.Duration(24) * time.Hour
  87. if os.Getenv("JWT_VALIDITY_DURATION") != "" {
  88. t, err := strconv.Atoi(os.Getenv("JWT_VALIDITY_DURATION"))
  89. if err != nil {
  90. return defaultDuration
  91. }
  92. return time.Duration(t) * time.Second
  93. }
  94. return defaultDuration
  95. }
  96. // GetRacAutoDisable - returns whether the feature to autodisable RAC is enabled
  97. func GetRacAutoDisable() bool {
  98. return os.Getenv("RAC_AUTO_DISABLE") == "true"
  99. }
  100. // GetServerInfo - gets the server config into memory from file or env
  101. func GetServerInfo() models.ServerConfig {
  102. var cfg models.ServerConfig
  103. cfg.Server = GetServer()
  104. cfg.MQUserName = GetMqUserName()
  105. cfg.MQPassword = GetMqPassword()
  106. cfg.API = GetAPIConnString()
  107. cfg.CoreDNSAddr = GetCoreDNSAddr()
  108. cfg.APIPort = GetAPIPort()
  109. cfg.DNSMode = "off"
  110. cfg.Broker = GetPublicBrokerEndpoint()
  111. if IsDNSMode() {
  112. cfg.DNSMode = "on"
  113. }
  114. cfg.Version = GetVersion()
  115. cfg.IsPro = IsPro
  116. cfg.StunPort = GetStunPort()
  117. cfg.TurnDomain = GetTurnHost()
  118. cfg.TurnPort = GetTurnPort()
  119. cfg.UseTurn = IsUsingTurn()
  120. return cfg
  121. }
  122. // GetTurnHost - fetches the turn host domain
  123. func GetTurnHost() string {
  124. turnServer := ""
  125. if os.Getenv("TURN_SERVER_HOST") != "" {
  126. turnServer = os.Getenv("TURN_SERVER_HOST")
  127. } else if config.Config.Server.TurnServer != "" {
  128. turnServer = config.Config.Server.TurnServer
  129. }
  130. return turnServer
  131. }
  132. // IsUsingTurn - check if server has turn configured
  133. func IsUsingTurn() (b bool) {
  134. if os.Getenv("USE_TURN") != "" {
  135. b = os.Getenv("USE_TURN") == "true"
  136. } else {
  137. b = config.Config.Server.UseTurn
  138. }
  139. return
  140. }
  141. // GetTurnApiHost - fetches the turn api host domain
  142. func GetTurnApiHost() string {
  143. turnApiServer := ""
  144. if os.Getenv("TURN_SERVER_API_HOST") != "" {
  145. turnApiServer = os.Getenv("TURN_SERVER_API_HOST")
  146. } else if config.Config.Server.TurnApiServer != "" {
  147. turnApiServer = config.Config.Server.TurnApiServer
  148. }
  149. return turnApiServer
  150. }
  151. // GetFrontendURL - gets the frontend url
  152. func GetFrontendURL() string {
  153. var frontend = ""
  154. if os.Getenv("FRONTEND_URL") != "" {
  155. frontend = os.Getenv("FRONTEND_URL")
  156. } else if config.Config.Server.FrontendURL != "" {
  157. frontend = config.Config.Server.FrontendURL
  158. }
  159. return frontend
  160. }
  161. // GetAPIConnString - gets the api connections string
  162. func GetAPIConnString() string {
  163. conn := ""
  164. if os.Getenv("SERVER_API_CONN_STRING") != "" {
  165. conn = os.Getenv("SERVER_API_CONN_STRING")
  166. } else if config.Config.Server.APIConnString != "" {
  167. conn = config.Config.Server.APIConnString
  168. }
  169. return conn
  170. }
  171. // SetVersion - set version of netmaker
  172. func SetVersion(v string) {
  173. Version = v
  174. }
  175. // GetVersion - version of netmaker
  176. func GetVersion() string {
  177. return Version
  178. }
  179. // GetDB - gets the database type
  180. func GetDB() string {
  181. database := "sqlite"
  182. if os.Getenv("DATABASE") != "" {
  183. database = os.Getenv("DATABASE")
  184. } else if config.Config.Server.Database != "" {
  185. database = config.Config.Server.Database
  186. }
  187. return database
  188. }
  189. // CacheEnabled - checks if cache is enabled
  190. func CacheEnabled() bool {
  191. caching := false
  192. if os.Getenv("CACHING_ENABLED") != "" {
  193. caching = os.Getenv("CACHING_ENABLED") == "true"
  194. } else if config.Config.Server.Database != "" {
  195. caching = config.Config.Server.CacheEnabled
  196. }
  197. return caching
  198. }
  199. // GetAPIHost - gets the api host
  200. func GetAPIHost() string {
  201. serverhost := "127.0.0.1"
  202. remoteip, _ := GetPublicIP()
  203. if os.Getenv("SERVER_HTTP_HOST") != "" {
  204. serverhost = os.Getenv("SERVER_HTTP_HOST")
  205. } else if config.Config.Server.APIHost != "" {
  206. serverhost = config.Config.Server.APIHost
  207. } else if os.Getenv("SERVER_HOST") != "" {
  208. serverhost = os.Getenv("SERVER_HOST")
  209. } else {
  210. if remoteip != "" {
  211. serverhost = remoteip
  212. }
  213. }
  214. return serverhost
  215. }
  216. // GetAPIPort - gets the api port
  217. func GetAPIPort() string {
  218. apiport := "8081"
  219. if os.Getenv("API_PORT") != "" {
  220. apiport = os.Getenv("API_PORT")
  221. } else if config.Config.Server.APIPort != "" {
  222. apiport = config.Config.Server.APIPort
  223. }
  224. return apiport
  225. }
  226. // GetCoreDNSAddr - gets the core dns address
  227. func GetCoreDNSAddr() string {
  228. addr, _ := GetPublicIP()
  229. if os.Getenv("COREDNS_ADDR") != "" {
  230. addr = os.Getenv("COREDNS_ADDR")
  231. } else if config.Config.Server.CoreDNSAddr != "" {
  232. addr = config.Config.Server.CoreDNSAddr
  233. }
  234. return addr
  235. }
  236. // GetPublicBrokerEndpoint - returns the public broker endpoint which shall be used by netclient
  237. func GetPublicBrokerEndpoint() string {
  238. if os.Getenv("BROKER_ENDPOINT") != "" {
  239. return os.Getenv("BROKER_ENDPOINT")
  240. } else {
  241. return config.Config.Server.Broker
  242. }
  243. }
  244. // GetOwnerEmail - gets the owner email (saas)
  245. func GetOwnerEmail() string {
  246. return os.Getenv("SAAS_OWNER_EMAIL")
  247. }
  248. // GetMessageQueueEndpoint - gets the message queue endpoint
  249. func GetMessageQueueEndpoint() (string, bool) {
  250. host, _ := GetPublicIP()
  251. if os.Getenv("SERVER_BROKER_ENDPOINT") != "" {
  252. host = os.Getenv("SERVER_BROKER_ENDPOINT")
  253. } else if config.Config.Server.ServerBrokerEndpoint != "" {
  254. host = config.Config.Server.ServerBrokerEndpoint
  255. } else if os.Getenv("BROKER_ENDPOINT") != "" {
  256. host = os.Getenv("BROKER_ENDPOINT")
  257. } else if config.Config.Server.Broker != "" {
  258. host = config.Config.Server.Broker
  259. } else {
  260. host += ":1883" // default
  261. }
  262. return host, strings.Contains(host, "wss") || strings.Contains(host, "ssl") || strings.Contains(host, "mqtts")
  263. }
  264. // GetBrokerType - returns the type of MQ broker
  265. func GetBrokerType() string {
  266. if os.Getenv("BROKER_TYPE") != "" {
  267. return os.Getenv("BROKER_TYPE")
  268. } else {
  269. return "mosquitto"
  270. }
  271. }
  272. // GetMasterKey - gets the configured master key of server
  273. func GetMasterKey() string {
  274. key := ""
  275. if os.Getenv("MASTER_KEY") != "" {
  276. key = os.Getenv("MASTER_KEY")
  277. } else if config.Config.Server.MasterKey != "" {
  278. key = config.Config.Server.MasterKey
  279. }
  280. return key
  281. }
  282. // GetAllowedOrigin - get the allowed origin
  283. func GetAllowedOrigin() string {
  284. allowedorigin := "*"
  285. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  286. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  287. } else if config.Config.Server.AllowedOrigin != "" {
  288. allowedorigin = config.Config.Server.AllowedOrigin
  289. }
  290. return allowedorigin
  291. }
  292. // IsRestBackend - checks if rest is on or off
  293. func IsRestBackend() bool {
  294. isrest := true
  295. if os.Getenv("REST_BACKEND") != "" {
  296. if os.Getenv("REST_BACKEND") == "off" {
  297. isrest = false
  298. }
  299. } else if config.Config.Server.RestBackend != "" {
  300. if config.Config.Server.RestBackend == "off" {
  301. isrest = false
  302. }
  303. }
  304. return isrest
  305. }
  306. // IsMetricsExporter - checks if metrics exporter is on or off
  307. func IsMetricsExporter() bool {
  308. export := false
  309. if os.Getenv("METRICS_EXPORTER") != "" {
  310. if os.Getenv("METRICS_EXPORTER") == "on" {
  311. export = true
  312. }
  313. } else if config.Config.Server.MetricsExporter != "" {
  314. if config.Config.Server.MetricsExporter == "on" {
  315. export = true
  316. }
  317. }
  318. return export
  319. }
  320. // IsMessageQueueBackend - checks if message queue is on or off
  321. func IsMessageQueueBackend() bool {
  322. ismessagequeue := true
  323. if os.Getenv("MESSAGEQUEUE_BACKEND") != "" {
  324. if os.Getenv("MESSAGEQUEUE_BACKEND") == "off" {
  325. ismessagequeue = false
  326. }
  327. } else if config.Config.Server.MessageQueueBackend != "" {
  328. if config.Config.Server.MessageQueueBackend == "off" {
  329. ismessagequeue = false
  330. }
  331. }
  332. return ismessagequeue
  333. }
  334. // Telemetry - checks if telemetry data should be sent
  335. func Telemetry() string {
  336. telemetry := "on"
  337. if os.Getenv("TELEMETRY") == "off" {
  338. telemetry = "off"
  339. }
  340. if config.Config.Server.Telemetry == "off" {
  341. telemetry = "off"
  342. }
  343. return telemetry
  344. }
  345. // GetServer - gets the server name
  346. func GetServer() string {
  347. server := ""
  348. if os.Getenv("SERVER_NAME") != "" {
  349. server = os.Getenv("SERVER_NAME")
  350. } else if config.Config.Server.Server != "" {
  351. server = config.Config.Server.Server
  352. }
  353. return server
  354. }
  355. func GetVerbosity() int32 {
  356. var verbosity = 0
  357. var err error
  358. if os.Getenv("VERBOSITY") != "" {
  359. verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))
  360. if err != nil {
  361. verbosity = 0
  362. }
  363. } else if config.Config.Server.Verbosity != 0 {
  364. verbosity = int(config.Config.Server.Verbosity)
  365. }
  366. if verbosity < 0 || verbosity > 4 {
  367. verbosity = 0
  368. }
  369. return int32(verbosity)
  370. }
  371. // AutoUpdateEnabled returns a boolean indicating whether netclient auto update is enabled or disabled
  372. // default is enabled
  373. func AutoUpdateEnabled() bool {
  374. if os.Getenv("NETCLIENT_AUTO_UPDATE") == "disabled" {
  375. return false
  376. } else if config.Config.Server.NetclientAutoUpdate == "disabled" {
  377. return false
  378. }
  379. return true
  380. }
  381. // IsDNSMode - should it run with DNS
  382. func IsDNSMode() bool {
  383. isdns := true
  384. if os.Getenv("DNS_MODE") != "" {
  385. if os.Getenv("DNS_MODE") == "off" {
  386. isdns = false
  387. }
  388. } else if config.Config.Server.DNSMode != "" {
  389. if config.Config.Server.DNSMode == "off" {
  390. isdns = false
  391. }
  392. }
  393. return isdns
  394. }
  395. // IsDisplayKeys - should server be able to display keys?
  396. func IsDisplayKeys() bool {
  397. isdisplay := true
  398. if os.Getenv("DISPLAY_KEYS") != "" {
  399. if os.Getenv("DISPLAY_KEYS") == "off" {
  400. isdisplay = false
  401. }
  402. } else if config.Config.Server.DisplayKeys != "" {
  403. if config.Config.Server.DisplayKeys == "off" {
  404. isdisplay = false
  405. }
  406. }
  407. return isdisplay
  408. }
  409. // DisableRemoteIPCheck - disable the remote ip check
  410. func DisableRemoteIPCheck() bool {
  411. disabled := false
  412. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  413. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  414. disabled = true
  415. }
  416. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  417. if config.Config.Server.DisableRemoteIPCheck == "on" {
  418. disabled = true
  419. }
  420. }
  421. return disabled
  422. }
  423. // GetPublicIP - gets public ip
  424. func GetPublicIP() (string, error) {
  425. endpoint := ""
  426. var err error
  427. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  428. publicIpService := os.Getenv("PUBLIC_IP_SERVICE")
  429. if publicIpService != "" {
  430. // prepend the user-specified service so it's checked first
  431. iplist = append([]string{publicIpService}, iplist...)
  432. } else if config.Config.Server.PublicIPService != "" {
  433. publicIpService = config.Config.Server.PublicIPService
  434. // prepend the user-specified service so it's checked first
  435. iplist = append([]string{publicIpService}, iplist...)
  436. }
  437. for _, ipserver := range iplist {
  438. client := &http.Client{
  439. Timeout: time.Second * 10,
  440. }
  441. resp, err := client.Get(ipserver)
  442. if err != nil {
  443. continue
  444. }
  445. defer resp.Body.Close()
  446. if resp.StatusCode == http.StatusOK {
  447. bodyBytes, err := io.ReadAll(resp.Body)
  448. if err != nil {
  449. continue
  450. }
  451. endpoint = string(bodyBytes)
  452. break
  453. }
  454. }
  455. if err == nil && endpoint == "" {
  456. err = errors.New("public address not found")
  457. }
  458. return endpoint, err
  459. }
  460. // GetPlatform - get the system type of server
  461. func GetPlatform() string {
  462. platform := "linux"
  463. if os.Getenv("PLATFORM") != "" {
  464. platform = os.Getenv("PLATFORM")
  465. } else if config.Config.Server.Platform != "" {
  466. platform = config.Config.Server.Platform
  467. }
  468. return platform
  469. }
  470. // GetSQLConn - get the sql connection string
  471. func GetSQLConn() string {
  472. sqlconn := "http://"
  473. if os.Getenv("SQL_CONN") != "" {
  474. sqlconn = os.Getenv("SQL_CONN")
  475. } else if config.Config.Server.SQLConn != "" {
  476. sqlconn = config.Config.Server.SQLConn
  477. }
  478. return sqlconn
  479. }
  480. // GetNodeID - gets the node id
  481. func GetNodeID() string {
  482. var id string
  483. var err error
  484. // id = getMacAddr()
  485. if os.Getenv("NODE_ID") != "" {
  486. id = os.Getenv("NODE_ID")
  487. } else if config.Config.Server.NodeID != "" {
  488. id = config.Config.Server.NodeID
  489. } else {
  490. id, err = os.Hostname()
  491. if err != nil {
  492. return ""
  493. }
  494. }
  495. return id
  496. }
  497. func SetNodeID(id string) {
  498. config.Config.Server.NodeID = id
  499. }
  500. // GetAuthProviderInfo = gets the oauth provider info
  501. func GetAuthProviderInfo() (pi []string) {
  502. var authProvider = ""
  503. defer func() {
  504. if authProvider == "oidc" {
  505. if os.Getenv("OIDC_ISSUER") != "" {
  506. pi = append(pi, os.Getenv("OIDC_ISSUER"))
  507. } else if config.Config.Server.OIDCIssuer != "" {
  508. pi = append(pi, config.Config.Server.OIDCIssuer)
  509. } else {
  510. pi = []string{"", "", ""}
  511. }
  512. }
  513. }()
  514. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  515. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  516. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  517. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  518. } else {
  519. authProvider = ""
  520. }
  521. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  522. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  523. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  524. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  525. }
  526. }
  527. return []string{"", "", ""}
  528. }
  529. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  530. func GetAzureTenant() string {
  531. var azureTenant = ""
  532. if os.Getenv("AZURE_TENANT") != "" {
  533. azureTenant = os.Getenv("AZURE_TENANT")
  534. } else if config.Config.Server.AzureTenant != "" {
  535. azureTenant = config.Config.Server.AzureTenant
  536. }
  537. return azureTenant
  538. }
  539. // GetMqPassword - fetches the MQ password
  540. func GetMqPassword() string {
  541. password := ""
  542. if os.Getenv("MQ_PASSWORD") != "" {
  543. password = os.Getenv("MQ_PASSWORD")
  544. } else if config.Config.Server.MQPassword != "" {
  545. password = config.Config.Server.MQPassword
  546. }
  547. return password
  548. }
  549. // GetMqUserName - fetches the MQ username
  550. func GetMqUserName() string {
  551. password := ""
  552. if os.Getenv("MQ_USERNAME") != "" {
  553. password = os.Getenv("MQ_USERNAME")
  554. } else if config.Config.Server.MQUserName != "" {
  555. password = config.Config.Server.MQUserName
  556. }
  557. return password
  558. }
  559. // GetEmqxRestEndpoint - returns the REST API Endpoint of EMQX
  560. func GetEmqxRestEndpoint() string {
  561. return os.Getenv("EMQX_REST_ENDPOINT")
  562. }
  563. // IsBasicAuthEnabled - checks if basic auth has been configured to be turned off
  564. func IsBasicAuthEnabled() bool {
  565. var enabled = true //default
  566. if os.Getenv("BASIC_AUTH") != "" {
  567. enabled = os.Getenv("BASIC_AUTH") == "yes"
  568. } else if config.Config.Server.BasicAuth != "" {
  569. enabled = config.Config.Server.BasicAuth == "yes"
  570. }
  571. return enabled
  572. }
  573. // GetLicenseKey - retrieves pro license value from env or conf files
  574. func GetLicenseKey() string {
  575. licenseKeyValue := os.Getenv("LICENSE_KEY")
  576. if licenseKeyValue == "" {
  577. licenseKeyValue = config.Config.Server.LicenseValue
  578. }
  579. return licenseKeyValue
  580. }
  581. // GetNetmakerTenantID - get's the associated, Netmaker, tenant ID to verify ownership
  582. func GetNetmakerTenantID() string {
  583. netmakerTenantID := os.Getenv("NETMAKER_TENANT_ID")
  584. if netmakerTenantID == "" {
  585. netmakerTenantID = config.Config.Server.NetmakerTenantID
  586. }
  587. return netmakerTenantID
  588. }
  589. // GetStunPort - Get the port to run the stun server on
  590. func GetStunPort() int {
  591. port := 3478 //default
  592. if os.Getenv("STUN_PORT") != "" {
  593. portInt, err := strconv.Atoi(os.Getenv("STUN_PORT"))
  594. if err == nil {
  595. port = portInt
  596. }
  597. } else if config.Config.Server.StunPort != 0 {
  598. port = config.Config.Server.StunPort
  599. }
  600. return port
  601. }
  602. // GetTurnPort - Get the port to run the turn server on
  603. func GetTurnPort() int {
  604. port := 3479 //default
  605. if os.Getenv("TURN_PORT") != "" {
  606. portInt, err := strconv.Atoi(os.Getenv("TURN_PORT"))
  607. if err == nil {
  608. port = portInt
  609. }
  610. } else if config.Config.Server.TurnPort != 0 {
  611. port = config.Config.Server.TurnPort
  612. }
  613. return port
  614. }
  615. // GetTurnUserName - fetches the turn server username
  616. func GetTurnUserName() string {
  617. userName := ""
  618. if os.Getenv("TURN_USERNAME") != "" {
  619. userName = os.Getenv("TURN_USERNAME")
  620. } else {
  621. userName = config.Config.Server.TurnUserName
  622. }
  623. return userName
  624. }
  625. // GetTurnPassword - fetches the turn server password
  626. func GetTurnPassword() string {
  627. pass := ""
  628. if os.Getenv("TURN_PASSWORD") != "" {
  629. pass = os.Getenv("TURN_PASSWORD")
  630. } else {
  631. pass = config.Config.Server.TurnPassword
  632. }
  633. return pass
  634. }
  635. // GetNetworkLimit - fetches free tier limits on users
  636. func GetUserLimit() int {
  637. var userslimit int
  638. if os.Getenv("USERS_LIMIT") != "" {
  639. userslimit, _ = strconv.Atoi(os.Getenv("USERS_LIMIT"))
  640. } else {
  641. userslimit = config.Config.Server.UsersLimit
  642. }
  643. return userslimit
  644. }
  645. // GetNetworkLimit - fetches free tier limits on networks
  646. func GetNetworkLimit() int {
  647. var networkslimit int
  648. if os.Getenv("NETWORKS_LIMIT") != "" {
  649. networkslimit, _ = strconv.Atoi(os.Getenv("NETWORKS_LIMIT"))
  650. } else {
  651. networkslimit = config.Config.Server.NetworksLimit
  652. }
  653. return networkslimit
  654. }
  655. // GetMachinesLimit - fetches free tier limits on machines (clients + hosts)
  656. func GetMachinesLimit() int {
  657. if l, err := strconv.Atoi(os.Getenv("MACHINES_LIMIT")); err == nil {
  658. return l
  659. }
  660. return config.Config.Server.MachinesLimit
  661. }
  662. // GetIngressLimit - fetches free tier limits on ingresses
  663. func GetIngressLimit() int {
  664. if l, err := strconv.Atoi(os.Getenv("INGRESSES_LIMIT")); err == nil {
  665. return l
  666. }
  667. return config.Config.Server.IngressesLimit
  668. }
  669. // GetEgressLimit - fetches free tier limits on egresses
  670. func GetEgressLimit() int {
  671. if l, err := strconv.Atoi(os.Getenv("EGRESSES_LIMIT")); err == nil {
  672. return l
  673. }
  674. return config.Config.Server.EgressesLimit
  675. }
  676. // DeployedByOperator - returns true if the instance is deployed by netmaker operator
  677. func DeployedByOperator() bool {
  678. if os.Getenv("DEPLOYED_BY_OPERATOR") != "" {
  679. return os.Getenv("DEPLOYED_BY_OPERATOR") == "true"
  680. }
  681. return config.Config.Server.DeployedByOperator
  682. }
  683. // GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)
  684. func GetEnvironment() string {
  685. if env := os.Getenv("ENVIRONMENT"); env != "" {
  686. return env
  687. }
  688. if env := config.Config.Server.Environment; env != "" {
  689. return env
  690. }
  691. return ""
  692. }