serverconf.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. // GetAPIHost - gets the api host
  190. func GetAPIHost() string {
  191. serverhost := "127.0.0.1"
  192. remoteip, _ := GetPublicIP()
  193. if os.Getenv("SERVER_HTTP_HOST") != "" {
  194. serverhost = os.Getenv("SERVER_HTTP_HOST")
  195. } else if config.Config.Server.APIHost != "" {
  196. serverhost = config.Config.Server.APIHost
  197. } else if os.Getenv("SERVER_HOST") != "" {
  198. serverhost = os.Getenv("SERVER_HOST")
  199. } else {
  200. if remoteip != "" {
  201. serverhost = remoteip
  202. }
  203. }
  204. return serverhost
  205. }
  206. // GetAPIPort - gets the api port
  207. func GetAPIPort() string {
  208. apiport := "8081"
  209. if os.Getenv("API_PORT") != "" {
  210. apiport = os.Getenv("API_PORT")
  211. } else if config.Config.Server.APIPort != "" {
  212. apiport = config.Config.Server.APIPort
  213. }
  214. return apiport
  215. }
  216. // GetCoreDNSAddr - gets the core dns address
  217. func GetCoreDNSAddr() string {
  218. addr, _ := GetPublicIP()
  219. if os.Getenv("COREDNS_ADDR") != "" {
  220. addr = os.Getenv("COREDNS_ADDR")
  221. } else if config.Config.Server.CoreDNSAddr != "" {
  222. addr = config.Config.Server.CoreDNSAddr
  223. }
  224. return addr
  225. }
  226. // GetPublicBrokerEndpoint - returns the public broker endpoint which shall be used by netclient
  227. func GetPublicBrokerEndpoint() string {
  228. if os.Getenv("BROKER_ENDPOINT") != "" {
  229. return os.Getenv("BROKER_ENDPOINT")
  230. } else {
  231. return config.Config.Server.Broker
  232. }
  233. }
  234. // GetOwnerEmail - gets the owner email (saas)
  235. func GetOwnerEmail() string {
  236. return os.Getenv("SAAS_OWNER_EMAIL")
  237. }
  238. // GetMessageQueueEndpoint - gets the message queue endpoint
  239. func GetMessageQueueEndpoint() (string, bool) {
  240. host, _ := GetPublicIP()
  241. if os.Getenv("SERVER_BROKER_ENDPOINT") != "" {
  242. host = os.Getenv("SERVER_BROKER_ENDPOINT")
  243. } else if config.Config.Server.ServerBrokerEndpoint != "" {
  244. host = config.Config.Server.ServerBrokerEndpoint
  245. } else if os.Getenv("BROKER_ENDPOINT") != "" {
  246. host = os.Getenv("BROKER_ENDPOINT")
  247. } else if config.Config.Server.Broker != "" {
  248. host = config.Config.Server.Broker
  249. } else {
  250. host += ":1883" // default
  251. }
  252. return host, strings.Contains(host, "wss") || strings.Contains(host, "ssl") || strings.Contains(host, "mqtts")
  253. }
  254. // GetBrokerType - returns the type of MQ broker
  255. func GetBrokerType() string {
  256. if os.Getenv("BROKER_TYPE") != "" {
  257. return os.Getenv("BROKER_TYPE")
  258. } else {
  259. return "mosquitto"
  260. }
  261. }
  262. // GetMasterKey - gets the configured master key of server
  263. func GetMasterKey() string {
  264. key := ""
  265. if os.Getenv("MASTER_KEY") != "" {
  266. key = os.Getenv("MASTER_KEY")
  267. } else if config.Config.Server.MasterKey != "" {
  268. key = config.Config.Server.MasterKey
  269. }
  270. return key
  271. }
  272. // GetAllowedOrigin - get the allowed origin
  273. func GetAllowedOrigin() string {
  274. allowedorigin := "*"
  275. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  276. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  277. } else if config.Config.Server.AllowedOrigin != "" {
  278. allowedorigin = config.Config.Server.AllowedOrigin
  279. }
  280. return allowedorigin
  281. }
  282. // IsRestBackend - checks if rest is on or off
  283. func IsRestBackend() bool {
  284. isrest := true
  285. if os.Getenv("REST_BACKEND") != "" {
  286. if os.Getenv("REST_BACKEND") == "off" {
  287. isrest = false
  288. }
  289. } else if config.Config.Server.RestBackend != "" {
  290. if config.Config.Server.RestBackend == "off" {
  291. isrest = false
  292. }
  293. }
  294. return isrest
  295. }
  296. // IsMetricsExporter - checks if metrics exporter is on or off
  297. func IsMetricsExporter() bool {
  298. export := false
  299. if os.Getenv("METRICS_EXPORTER") != "" {
  300. if os.Getenv("METRICS_EXPORTER") == "on" {
  301. export = true
  302. }
  303. } else if config.Config.Server.MetricsExporter != "" {
  304. if config.Config.Server.MetricsExporter == "on" {
  305. export = true
  306. }
  307. }
  308. return export
  309. }
  310. // IsMessageQueueBackend - checks if message queue is on or off
  311. func IsMessageQueueBackend() bool {
  312. ismessagequeue := true
  313. if os.Getenv("MESSAGEQUEUE_BACKEND") != "" {
  314. if os.Getenv("MESSAGEQUEUE_BACKEND") == "off" {
  315. ismessagequeue = false
  316. }
  317. } else if config.Config.Server.MessageQueueBackend != "" {
  318. if config.Config.Server.MessageQueueBackend == "off" {
  319. ismessagequeue = false
  320. }
  321. }
  322. return ismessagequeue
  323. }
  324. // Telemetry - checks if telemetry data should be sent
  325. func Telemetry() string {
  326. telemetry := "on"
  327. if os.Getenv("TELEMETRY") == "off" {
  328. telemetry = "off"
  329. }
  330. if config.Config.Server.Telemetry == "off" {
  331. telemetry = "off"
  332. }
  333. return telemetry
  334. }
  335. // GetServer - gets the server name
  336. func GetServer() string {
  337. server := ""
  338. if os.Getenv("SERVER_NAME") != "" {
  339. server = os.Getenv("SERVER_NAME")
  340. } else if config.Config.Server.Server != "" {
  341. server = config.Config.Server.Server
  342. }
  343. return server
  344. }
  345. func GetVerbosity() int32 {
  346. var verbosity = 0
  347. var err error
  348. if os.Getenv("VERBOSITY") != "" {
  349. verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))
  350. if err != nil {
  351. verbosity = 0
  352. }
  353. } else if config.Config.Server.Verbosity != 0 {
  354. verbosity = int(config.Config.Server.Verbosity)
  355. }
  356. if verbosity < 0 || verbosity > 4 {
  357. verbosity = 0
  358. }
  359. return int32(verbosity)
  360. }
  361. // AutoUpdateEnabled returns a boolean indicating whether netclient auto update is enabled or disabled
  362. // default is enabled
  363. func AutoUpdateEnabled() bool {
  364. if os.Getenv("NETCLIENT_AUTO_UPDATE") == "disabled" {
  365. return false
  366. } else if config.Config.Server.NetclientAutoUpdate == "disabled" {
  367. return false
  368. }
  369. return true
  370. }
  371. // IsDNSMode - should it run with DNS
  372. func IsDNSMode() bool {
  373. isdns := true
  374. if os.Getenv("DNS_MODE") != "" {
  375. if os.Getenv("DNS_MODE") == "off" {
  376. isdns = false
  377. }
  378. } else if config.Config.Server.DNSMode != "" {
  379. if config.Config.Server.DNSMode == "off" {
  380. isdns = false
  381. }
  382. }
  383. return isdns
  384. }
  385. // IsDisplayKeys - should server be able to display keys?
  386. func IsDisplayKeys() bool {
  387. isdisplay := true
  388. if os.Getenv("DISPLAY_KEYS") != "" {
  389. if os.Getenv("DISPLAY_KEYS") == "off" {
  390. isdisplay = false
  391. }
  392. } else if config.Config.Server.DisplayKeys != "" {
  393. if config.Config.Server.DisplayKeys == "off" {
  394. isdisplay = false
  395. }
  396. }
  397. return isdisplay
  398. }
  399. // DisableRemoteIPCheck - disable the remote ip check
  400. func DisableRemoteIPCheck() bool {
  401. disabled := false
  402. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  403. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  404. disabled = true
  405. }
  406. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  407. if config.Config.Server.DisableRemoteIPCheck == "on" {
  408. disabled = true
  409. }
  410. }
  411. return disabled
  412. }
  413. // GetPublicIP - gets public ip
  414. func GetPublicIP() (string, error) {
  415. endpoint := ""
  416. var err error
  417. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  418. publicIpService := os.Getenv("PUBLIC_IP_SERVICE")
  419. if publicIpService != "" {
  420. // prepend the user-specified service so it's checked first
  421. iplist = append([]string{publicIpService}, iplist...)
  422. } else if config.Config.Server.PublicIPService != "" {
  423. publicIpService = config.Config.Server.PublicIPService
  424. // prepend the user-specified service so it's checked first
  425. iplist = append([]string{publicIpService}, iplist...)
  426. }
  427. for _, ipserver := range iplist {
  428. client := &http.Client{
  429. Timeout: time.Second * 10,
  430. }
  431. resp, err := client.Get(ipserver)
  432. if err != nil {
  433. continue
  434. }
  435. defer resp.Body.Close()
  436. if resp.StatusCode == http.StatusOK {
  437. bodyBytes, err := io.ReadAll(resp.Body)
  438. if err != nil {
  439. continue
  440. }
  441. endpoint = string(bodyBytes)
  442. break
  443. }
  444. }
  445. if err == nil && endpoint == "" {
  446. err = errors.New("public address not found")
  447. }
  448. return endpoint, err
  449. }
  450. // GetPlatform - get the system type of server
  451. func GetPlatform() string {
  452. platform := "linux"
  453. if os.Getenv("PLATFORM") != "" {
  454. platform = os.Getenv("PLATFORM")
  455. } else if config.Config.Server.Platform != "" {
  456. platform = config.Config.Server.Platform
  457. }
  458. return platform
  459. }
  460. // GetSQLConn - get the sql connection string
  461. func GetSQLConn() string {
  462. sqlconn := "http://"
  463. if os.Getenv("SQL_CONN") != "" {
  464. sqlconn = os.Getenv("SQL_CONN")
  465. } else if config.Config.Server.SQLConn != "" {
  466. sqlconn = config.Config.Server.SQLConn
  467. }
  468. return sqlconn
  469. }
  470. // GetNodeID - gets the node id
  471. func GetNodeID() string {
  472. var id string
  473. var err error
  474. // id = getMacAddr()
  475. if os.Getenv("NODE_ID") != "" {
  476. id = os.Getenv("NODE_ID")
  477. } else if config.Config.Server.NodeID != "" {
  478. id = config.Config.Server.NodeID
  479. } else {
  480. id, err = os.Hostname()
  481. if err != nil {
  482. return ""
  483. }
  484. }
  485. return id
  486. }
  487. func SetNodeID(id string) {
  488. config.Config.Server.NodeID = id
  489. }
  490. // GetAuthProviderInfo = gets the oauth provider info
  491. func GetAuthProviderInfo() (pi []string) {
  492. var authProvider = ""
  493. defer func() {
  494. if authProvider == "oidc" {
  495. if os.Getenv("OIDC_ISSUER") != "" {
  496. pi = append(pi, os.Getenv("OIDC_ISSUER"))
  497. } else if config.Config.Server.OIDCIssuer != "" {
  498. pi = append(pi, config.Config.Server.OIDCIssuer)
  499. } else {
  500. pi = []string{"", "", ""}
  501. }
  502. }
  503. }()
  504. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  505. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  506. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  507. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  508. } else {
  509. authProvider = ""
  510. }
  511. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  512. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  513. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  514. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  515. }
  516. }
  517. return []string{"", "", ""}
  518. }
  519. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  520. func GetAzureTenant() string {
  521. var azureTenant = ""
  522. if os.Getenv("AZURE_TENANT") != "" {
  523. azureTenant = os.Getenv("AZURE_TENANT")
  524. } else if config.Config.Server.AzureTenant != "" {
  525. azureTenant = config.Config.Server.AzureTenant
  526. }
  527. return azureTenant
  528. }
  529. // GetMqPassword - fetches the MQ password
  530. func GetMqPassword() string {
  531. password := ""
  532. if os.Getenv("MQ_PASSWORD") != "" {
  533. password = os.Getenv("MQ_PASSWORD")
  534. } else if config.Config.Server.MQPassword != "" {
  535. password = config.Config.Server.MQPassword
  536. }
  537. return password
  538. }
  539. // GetMqUserName - fetches the MQ username
  540. func GetMqUserName() string {
  541. password := ""
  542. if os.Getenv("MQ_USERNAME") != "" {
  543. password = os.Getenv("MQ_USERNAME")
  544. } else if config.Config.Server.MQUserName != "" {
  545. password = config.Config.Server.MQUserName
  546. }
  547. return password
  548. }
  549. // GetEmqxRestEndpoint - returns the REST API Endpoint of EMQX
  550. func GetEmqxRestEndpoint() string {
  551. return os.Getenv("EMQX_REST_ENDPOINT")
  552. }
  553. // IsBasicAuthEnabled - checks if basic auth has been configured to be turned off
  554. func IsBasicAuthEnabled() bool {
  555. var enabled = true //default
  556. if os.Getenv("BASIC_AUTH") != "" {
  557. enabled = os.Getenv("BASIC_AUTH") == "yes"
  558. } else if config.Config.Server.BasicAuth != "" {
  559. enabled = config.Config.Server.BasicAuth == "yes"
  560. }
  561. return enabled
  562. }
  563. // GetLicenseKey - retrieves pro license value from env or conf files
  564. func GetLicenseKey() string {
  565. licenseKeyValue := os.Getenv("LICENSE_KEY")
  566. if licenseKeyValue == "" {
  567. licenseKeyValue = config.Config.Server.LicenseValue
  568. }
  569. return licenseKeyValue
  570. }
  571. // GetNetmakerTenantID - get's the associated, Netmaker, tenant ID to verify ownership
  572. func GetNetmakerTenantID() string {
  573. netmakerTenantID := os.Getenv("NETMAKER_TENANT_ID")
  574. if netmakerTenantID == "" {
  575. netmakerTenantID = config.Config.Server.NetmakerTenantID
  576. }
  577. return netmakerTenantID
  578. }
  579. // GetStunPort - Get the port to run the stun server on
  580. func GetStunPort() int {
  581. port := 3478 //default
  582. if os.Getenv("STUN_PORT") != "" {
  583. portInt, err := strconv.Atoi(os.Getenv("STUN_PORT"))
  584. if err == nil {
  585. port = portInt
  586. }
  587. } else if config.Config.Server.StunPort != 0 {
  588. port = config.Config.Server.StunPort
  589. }
  590. return port
  591. }
  592. // GetTurnPort - Get the port to run the turn server on
  593. func GetTurnPort() int {
  594. port := 3479 //default
  595. if os.Getenv("TURN_PORT") != "" {
  596. portInt, err := strconv.Atoi(os.Getenv("TURN_PORT"))
  597. if err == nil {
  598. port = portInt
  599. }
  600. } else if config.Config.Server.TurnPort != 0 {
  601. port = config.Config.Server.TurnPort
  602. }
  603. return port
  604. }
  605. // GetTurnUserName - fetches the turn server username
  606. func GetTurnUserName() string {
  607. userName := ""
  608. if os.Getenv("TURN_USERNAME") != "" {
  609. userName = os.Getenv("TURN_USERNAME")
  610. } else {
  611. userName = config.Config.Server.TurnUserName
  612. }
  613. return userName
  614. }
  615. // GetTurnPassword - fetches the turn server password
  616. func GetTurnPassword() string {
  617. pass := ""
  618. if os.Getenv("TURN_PASSWORD") != "" {
  619. pass = os.Getenv("TURN_PASSWORD")
  620. } else {
  621. pass = config.Config.Server.TurnPassword
  622. }
  623. return pass
  624. }
  625. // GetNetworkLimit - fetches free tier limits on users
  626. func GetUserLimit() int {
  627. var userslimit int
  628. if os.Getenv("USERS_LIMIT") != "" {
  629. userslimit, _ = strconv.Atoi(os.Getenv("USERS_LIMIT"))
  630. } else {
  631. userslimit = config.Config.Server.UsersLimit
  632. }
  633. return userslimit
  634. }
  635. // GetNetworkLimit - fetches free tier limits on networks
  636. func GetNetworkLimit() int {
  637. var networkslimit int
  638. if os.Getenv("NETWORKS_LIMIT") != "" {
  639. networkslimit, _ = strconv.Atoi(os.Getenv("NETWORKS_LIMIT"))
  640. } else {
  641. networkslimit = config.Config.Server.NetworksLimit
  642. }
  643. return networkslimit
  644. }
  645. // GetMachinesLimit - fetches free tier limits on machines (clients + hosts)
  646. func GetMachinesLimit() int {
  647. if l, err := strconv.Atoi(os.Getenv("MACHINES_LIMIT")); err == nil {
  648. return l
  649. }
  650. return config.Config.Server.MachinesLimit
  651. }
  652. // GetIngressLimit - fetches free tier limits on ingresses
  653. func GetIngressLimit() int {
  654. if l, err := strconv.Atoi(os.Getenv("INGRESSES_LIMIT")); err == nil {
  655. return l
  656. }
  657. return config.Config.Server.IngressesLimit
  658. }
  659. // GetEgressLimit - fetches free tier limits on egresses
  660. func GetEgressLimit() int {
  661. if l, err := strconv.Atoi(os.Getenv("EGRESSES_LIMIT")); err == nil {
  662. return l
  663. }
  664. return config.Config.Server.EgressesLimit
  665. }
  666. // DeployedByOperator - returns true if the instance is deployed by netmaker operator
  667. func DeployedByOperator() bool {
  668. if os.Getenv("DEPLOYED_BY_OPERATOR") != "" {
  669. return os.Getenv("DEPLOYED_BY_OPERATOR") == "true"
  670. }
  671. return config.Config.Server.DeployedByOperator
  672. }
  673. // GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)
  674. func GetEnvironment() string {
  675. if env := os.Getenv("ENVIRONMENT"); env != "" {
  676. return env
  677. }
  678. if env := config.Config.Server.Environment; env != "" {
  679. return env
  680. }
  681. return ""
  682. }