serverconf.go 19 KB

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