serverconf.go 19 KB

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