2
0

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