serverconf.go 20 KB

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