serverconf.go 19 KB

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