2
0

serverconf.go 21 KB

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