serverconf.go 17 KB

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