serverconf.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. cfg.EndpointDetection = EndpointDetectionEnabled()
  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. // Telemetry - checks if telemetry data should be sent
  344. func Telemetry() string {
  345. telemetry := "on"
  346. if os.Getenv("TELEMETRY") == "off" {
  347. telemetry = "off"
  348. }
  349. if config.Config.Server.Telemetry == "off" {
  350. telemetry = "off"
  351. }
  352. return telemetry
  353. }
  354. // GetServer - gets the server name
  355. func GetServer() string {
  356. server := ""
  357. if os.Getenv("SERVER_NAME") != "" {
  358. server = os.Getenv("SERVER_NAME")
  359. } else if config.Config.Server.Server != "" {
  360. server = config.Config.Server.Server
  361. }
  362. return server
  363. }
  364. func GetVerbosity() int32 {
  365. var verbosity = 0
  366. var err error
  367. if os.Getenv("VERBOSITY") != "" {
  368. verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))
  369. if err != nil {
  370. verbosity = 0
  371. }
  372. } else if config.Config.Server.Verbosity != 0 {
  373. verbosity = int(config.Config.Server.Verbosity)
  374. }
  375. if verbosity < 0 || verbosity > 4 {
  376. verbosity = 0
  377. }
  378. return int32(verbosity)
  379. }
  380. // AutoUpdateEnabled returns a boolean indicating whether netclient auto update is enabled or disabled
  381. // default is enabled
  382. func AutoUpdateEnabled() bool {
  383. if os.Getenv("NETCLIENT_AUTO_UPDATE") == "disabled" {
  384. return false
  385. } else if config.Config.Server.NetclientAutoUpdate == "disabled" {
  386. return false
  387. }
  388. return true
  389. }
  390. // EndpointDetectionEnabled returns a boolean indicating whether netclient endpoint detection is enabled or disabled
  391. // default is enabled
  392. func EndpointDetectionEnabled() bool {
  393. if os.Getenv("NETCLIENT_ENDPOINT_DETECTION") == "disabled" {
  394. return false
  395. } else if config.Config.Server.NetclientEndpointDetection == "disabled" {
  396. return false
  397. }
  398. return true
  399. }
  400. // IsDNSMode - should it run with DNS
  401. func IsDNSMode() bool {
  402. isdns := true
  403. if os.Getenv("DNS_MODE") != "" {
  404. if os.Getenv("DNS_MODE") == "off" {
  405. isdns = false
  406. }
  407. } else if config.Config.Server.DNSMode != "" {
  408. if config.Config.Server.DNSMode == "off" {
  409. isdns = false
  410. }
  411. }
  412. return isdns
  413. }
  414. // IsDisplayKeys - should server be able to display keys?
  415. func IsDisplayKeys() bool {
  416. isdisplay := true
  417. if os.Getenv("DISPLAY_KEYS") != "" {
  418. if os.Getenv("DISPLAY_KEYS") == "off" {
  419. isdisplay = false
  420. }
  421. } else if config.Config.Server.DisplayKeys != "" {
  422. if config.Config.Server.DisplayKeys == "off" {
  423. isdisplay = false
  424. }
  425. }
  426. return isdisplay
  427. }
  428. // DisableRemoteIPCheck - disable the remote ip check
  429. func DisableRemoteIPCheck() bool {
  430. disabled := false
  431. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  432. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  433. disabled = true
  434. }
  435. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  436. if config.Config.Server.DisableRemoteIPCheck == "on" {
  437. disabled = true
  438. }
  439. }
  440. return disabled
  441. }
  442. // GetPublicIP - gets public ip
  443. func GetPublicIP() (string, error) {
  444. endpoint := ""
  445. var err error
  446. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  447. publicIpService := os.Getenv("PUBLIC_IP_SERVICE")
  448. if publicIpService != "" {
  449. // prepend the user-specified service so it's checked first
  450. iplist = append([]string{publicIpService}, iplist...)
  451. } else if config.Config.Server.PublicIPService != "" {
  452. publicIpService = config.Config.Server.PublicIPService
  453. // prepend the user-specified service so it's checked first
  454. iplist = append([]string{publicIpService}, iplist...)
  455. }
  456. for _, ipserver := range iplist {
  457. client := &http.Client{
  458. Timeout: time.Second * 10,
  459. }
  460. resp, err := client.Get(ipserver)
  461. if err != nil {
  462. continue
  463. }
  464. defer resp.Body.Close()
  465. if resp.StatusCode == http.StatusOK {
  466. bodyBytes, err := io.ReadAll(resp.Body)
  467. if err != nil {
  468. continue
  469. }
  470. endpoint = string(bodyBytes)
  471. break
  472. }
  473. }
  474. if err == nil && endpoint == "" {
  475. err = errors.New("public address not found")
  476. }
  477. return endpoint, err
  478. }
  479. // GetPlatform - get the system type of server
  480. func GetPlatform() string {
  481. platform := "linux"
  482. if os.Getenv("PLATFORM") != "" {
  483. platform = os.Getenv("PLATFORM")
  484. } else if config.Config.Server.Platform != "" {
  485. platform = config.Config.Server.Platform
  486. }
  487. return platform
  488. }
  489. // GetSQLConn - get the sql connection string
  490. func GetSQLConn() string {
  491. sqlconn := "http://"
  492. if os.Getenv("SQL_CONN") != "" {
  493. sqlconn = os.Getenv("SQL_CONN")
  494. } else if config.Config.Server.SQLConn != "" {
  495. sqlconn = config.Config.Server.SQLConn
  496. }
  497. return sqlconn
  498. }
  499. // GetNodeID - gets the node id
  500. func GetNodeID() string {
  501. var id string
  502. var err error
  503. // id = getMacAddr()
  504. if os.Getenv("NODE_ID") != "" {
  505. id = os.Getenv("NODE_ID")
  506. } else if config.Config.Server.NodeID != "" {
  507. id = config.Config.Server.NodeID
  508. } else {
  509. id, err = os.Hostname()
  510. if err != nil {
  511. return ""
  512. }
  513. }
  514. return id
  515. }
  516. func SetNodeID(id string) {
  517. config.Config.Server.NodeID = id
  518. }
  519. // GetAuthProviderInfo = gets the oauth provider info
  520. func GetAuthProviderInfo() (pi []string) {
  521. var authProvider = ""
  522. defer func() {
  523. if authProvider == "oidc" {
  524. if os.Getenv("OIDC_ISSUER") != "" {
  525. pi = append(pi, os.Getenv("OIDC_ISSUER"))
  526. } else if config.Config.Server.OIDCIssuer != "" {
  527. pi = append(pi, config.Config.Server.OIDCIssuer)
  528. } else {
  529. pi = []string{"", "", ""}
  530. }
  531. }
  532. }()
  533. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  534. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  535. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  536. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  537. } else {
  538. authProvider = ""
  539. }
  540. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  541. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  542. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  543. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  544. }
  545. }
  546. return []string{"", "", ""}
  547. }
  548. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  549. func GetAzureTenant() string {
  550. var azureTenant = ""
  551. if os.Getenv("AZURE_TENANT") != "" {
  552. azureTenant = os.Getenv("AZURE_TENANT")
  553. } else if config.Config.Server.AzureTenant != "" {
  554. azureTenant = config.Config.Server.AzureTenant
  555. }
  556. return azureTenant
  557. }
  558. // GetMqPassword - fetches the MQ password
  559. func GetMqPassword() string {
  560. password := ""
  561. if os.Getenv("MQ_PASSWORD") != "" {
  562. password = os.Getenv("MQ_PASSWORD")
  563. } else if config.Config.Server.MQPassword != "" {
  564. password = config.Config.Server.MQPassword
  565. }
  566. return password
  567. }
  568. // GetMqUserName - fetches the MQ username
  569. func GetMqUserName() string {
  570. password := ""
  571. if os.Getenv("MQ_USERNAME") != "" {
  572. password = os.Getenv("MQ_USERNAME")
  573. } else if config.Config.Server.MQUserName != "" {
  574. password = config.Config.Server.MQUserName
  575. }
  576. return password
  577. }
  578. // GetEmqxRestEndpoint - returns the REST API Endpoint of EMQX
  579. func GetEmqxRestEndpoint() string {
  580. return os.Getenv("EMQX_REST_ENDPOINT")
  581. }
  582. // IsBasicAuthEnabled - checks if basic auth has been configured to be turned off
  583. func IsBasicAuthEnabled() bool {
  584. var enabled = true //default
  585. if os.Getenv("BASIC_AUTH") != "" {
  586. enabled = os.Getenv("BASIC_AUTH") == "yes"
  587. } else if config.Config.Server.BasicAuth != "" {
  588. enabled = config.Config.Server.BasicAuth == "yes"
  589. }
  590. return enabled
  591. }
  592. // GetLicenseKey - retrieves pro license value from env or conf files
  593. func GetLicenseKey() string {
  594. licenseKeyValue := os.Getenv("LICENSE_KEY")
  595. if licenseKeyValue == "" {
  596. licenseKeyValue = config.Config.Server.LicenseValue
  597. }
  598. return licenseKeyValue
  599. }
  600. // GetNetmakerAccountID - get's the associated, Netmaker, account ID to verify ownership
  601. func GetNetmakerAccountID() string {
  602. netmakerAccountID := os.Getenv("NETMAKER_ACCOUNT_ID")
  603. if netmakerAccountID == "" {
  604. netmakerAccountID = config.Config.Server.NetmakerAccountID
  605. }
  606. return netmakerAccountID
  607. }
  608. // GetStunPort - Get the port to run the stun server on
  609. func GetStunPort() int {
  610. port := 3478 //default
  611. if os.Getenv("STUN_PORT") != "" {
  612. portInt, err := strconv.Atoi(os.Getenv("STUN_PORT"))
  613. if err == nil {
  614. port = portInt
  615. }
  616. } else if config.Config.Server.StunPort != 0 {
  617. port = config.Config.Server.StunPort
  618. }
  619. return port
  620. }
  621. // GetTurnPort - Get the port to run the turn server on
  622. func GetTurnPort() int {
  623. port := 3479 //default
  624. if os.Getenv("TURN_PORT") != "" {
  625. portInt, err := strconv.Atoi(os.Getenv("TURN_PORT"))
  626. if err == nil {
  627. port = portInt
  628. }
  629. } else if config.Config.Server.TurnPort != 0 {
  630. port = config.Config.Server.TurnPort
  631. }
  632. return port
  633. }
  634. // GetTurnUserName - fetches the turn server username
  635. func GetTurnUserName() string {
  636. userName := ""
  637. if os.Getenv("TURN_USERNAME") != "" {
  638. userName = os.Getenv("TURN_USERNAME")
  639. } else {
  640. userName = config.Config.Server.TurnUserName
  641. }
  642. return userName
  643. }
  644. // GetTurnPassword - fetches the turn server password
  645. func GetTurnPassword() string {
  646. pass := ""
  647. if os.Getenv("TURN_PASSWORD") != "" {
  648. pass = os.Getenv("TURN_PASSWORD")
  649. } else {
  650. pass = config.Config.Server.TurnPassword
  651. }
  652. return pass
  653. }
  654. // parseStunList - turn string into slice of StunServers
  655. func parseStunList(stunString string) ([]models.StunServer, error) {
  656. var err error
  657. stunServers := []models.StunServer{}
  658. stuns := strings.Split(stunString, ",")
  659. if len(stuns) == 0 {
  660. return stunServers, errors.New("no stun servers provided")
  661. }
  662. for _, stun := range stuns {
  663. stun = strings.Trim(stun, " ")
  664. stunInfo := strings.Split(stun, ":")
  665. if len(stunInfo) != 2 {
  666. continue
  667. }
  668. port, err := strconv.Atoi(stunInfo[1])
  669. if err != nil || port == 0 {
  670. continue
  671. }
  672. stunServers = append(stunServers, models.StunServer{
  673. Domain: stunInfo[0],
  674. Port: port,
  675. })
  676. }
  677. if len(stunServers) == 0 {
  678. err = errors.New("no stun entries parsable")
  679. }
  680. return stunServers, err
  681. }