serverconf.go 17 KB

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