serverconf.go 21 KB

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