serverconf.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. package servercfg
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/gravitl/netmaker/config"
  11. )
  12. // SetHost - sets the host ip
  13. func SetHost() error {
  14. remoteip, err := GetPublicIP()
  15. if err != nil {
  16. return err
  17. }
  18. os.Setenv("SERVER_HOST", remoteip)
  19. return nil
  20. }
  21. // GetServerConfig - gets the server config into memory from file or env
  22. func GetServerConfig() config.ServerConfig {
  23. var cfg config.ServerConfig
  24. cfg.APIConnString = GetAPIConnString()
  25. cfg.CoreDNSAddr = GetCoreDNSAddr()
  26. cfg.APIHost = GetAPIHost()
  27. cfg.APIPort = GetAPIPort()
  28. cfg.GRPCConnString = GetGRPCConnString()
  29. cfg.GRPCHost = GetGRPCHost()
  30. cfg.GRPCPort = GetGRPCPort()
  31. cfg.MasterKey = "(hidden)"
  32. cfg.DNSKey = "(hidden)"
  33. cfg.AllowedOrigin = GetAllowedOrigin()
  34. cfg.RestBackend = "off"
  35. cfg.NodeID = GetNodeID()
  36. cfg.CheckinInterval = GetCheckinInterval()
  37. cfg.ServerCheckinInterval = GetServerCheckinInterval()
  38. if IsRestBackend() {
  39. cfg.RestBackend = "on"
  40. }
  41. cfg.AgentBackend = "off"
  42. if IsAgentBackend() {
  43. cfg.AgentBackend = "on"
  44. }
  45. cfg.ClientMode = "off"
  46. if IsClientMode() != "off" {
  47. cfg.ClientMode = IsClientMode()
  48. }
  49. cfg.DNSMode = "off"
  50. if IsDNSMode() {
  51. cfg.DNSMode = "on"
  52. }
  53. cfg.DisplayKeys = "off"
  54. if IsDisplayKeys() {
  55. cfg.DisplayKeys = "on"
  56. }
  57. cfg.GRPCSSL = "off"
  58. if IsGRPCSSL() {
  59. cfg.GRPCSSL = "on"
  60. }
  61. cfg.DisableRemoteIPCheck = "off"
  62. if DisableRemoteIPCheck() {
  63. cfg.DisableRemoteIPCheck = "on"
  64. }
  65. cfg.DisableDefaultNet = "off"
  66. if DisableDefaultNet() {
  67. cfg.DisableRemoteIPCheck = "on"
  68. }
  69. cfg.Database = GetDB()
  70. cfg.Platform = GetPlatform()
  71. cfg.Version = GetVersion()
  72. // == auth config ==
  73. var authInfo = GetAuthProviderInfo()
  74. cfg.AuthProvider = authInfo[0]
  75. cfg.ClientID = authInfo[1]
  76. cfg.ClientSecret = authInfo[2]
  77. cfg.FrontendURL = GetFrontendURL()
  78. if GetRce() {
  79. cfg.RCE = "on"
  80. } else {
  81. cfg.RCE = "off"
  82. }
  83. cfg.Telemetry = Telemetry()
  84. cfg.ManageIPTables = ManageIPTables()
  85. return cfg
  86. }
  87. // GetFrontendURL - gets the frontend url
  88. func GetFrontendURL() string {
  89. var frontend = ""
  90. if os.Getenv("FRONTEND_URL") != "" {
  91. frontend = os.Getenv("FRONTEND_URL")
  92. } else if config.Config.Server.FrontendURL != "" {
  93. frontend = config.Config.Server.FrontendURL
  94. }
  95. return frontend
  96. }
  97. // GetAPIConnString - gets the api connections string
  98. func GetAPIConnString() string {
  99. conn := ""
  100. if os.Getenv("SERVER_API_CONN_STRING") != "" {
  101. conn = os.Getenv("SERVER_API_CONN_STRING")
  102. } else if config.Config.Server.APIConnString != "" {
  103. conn = config.Config.Server.APIConnString
  104. }
  105. return conn
  106. }
  107. // GetVersion - version of netmaker
  108. func GetVersion() string {
  109. version := "0.10.0"
  110. if config.Config.Server.Version != "" {
  111. version = config.Config.Server.Version
  112. }
  113. return version
  114. }
  115. // GetDB - gets the database type
  116. func GetDB() string {
  117. database := "sqlite"
  118. if os.Getenv("DATABASE") != "" {
  119. database = os.Getenv("DATABASE")
  120. } else if config.Config.Server.Database != "" {
  121. database = config.Config.Server.Database
  122. }
  123. return database
  124. }
  125. // GetAPIHost - gets the api host
  126. func GetAPIHost() string {
  127. serverhost := "127.0.0.1"
  128. remoteip, _ := GetPublicIP()
  129. if os.Getenv("SERVER_HTTP_HOST") != "" {
  130. serverhost = os.Getenv("SERVER_HTTP_HOST")
  131. } else if config.Config.Server.APIHost != "" {
  132. serverhost = config.Config.Server.APIHost
  133. } else if os.Getenv("SERVER_HOST") != "" {
  134. serverhost = os.Getenv("SERVER_HOST")
  135. } else {
  136. if remoteip != "" {
  137. serverhost = remoteip
  138. }
  139. }
  140. return serverhost
  141. }
  142. // GetPodIP - get the pod's ip
  143. func GetPodIP() string {
  144. podip := "127.0.0.1"
  145. if os.Getenv("POD_IP") != "" {
  146. podip = os.Getenv("POD_IP")
  147. }
  148. return podip
  149. }
  150. // GetAPIPort - gets the api port
  151. func GetAPIPort() string {
  152. apiport := "8081"
  153. if os.Getenv("API_PORT") != "" {
  154. apiport = os.Getenv("API_PORT")
  155. } else if config.Config.Server.APIPort != "" {
  156. apiport = config.Config.Server.APIPort
  157. }
  158. return apiport
  159. }
  160. // GetCheckinInterval - get check in interval for nodes
  161. func GetCheckinInterval() string {
  162. seconds := "15"
  163. if os.Getenv("CHECKIN_INTERVAL") != "" {
  164. seconds = os.Getenv("CHECKIN_INTERVAL")
  165. } else if config.Config.Server.CheckinInterval != "" {
  166. seconds = config.Config.Server.CheckinInterval
  167. }
  168. return seconds
  169. }
  170. // GetDefaultNodeLimit - get node limit if one is set
  171. func GetDefaultNodeLimit() int32 {
  172. var limit int32
  173. limit = 999999999
  174. envlimit, err := strconv.Atoi(os.Getenv("DEFAULT_NODE_LIMIT"))
  175. if err == nil && envlimit != 0 {
  176. limit = int32(envlimit)
  177. } else if config.Config.Server.DefaultNodeLimit != 0 {
  178. limit = config.Config.Server.DefaultNodeLimit
  179. }
  180. return limit
  181. }
  182. // GetGRPCConnString - get grpc conn string
  183. func GetGRPCConnString() string {
  184. conn := ""
  185. if os.Getenv("SERVER_GRPC_CONN_STRING") != "" {
  186. conn = os.Getenv("SERVER_GRPC_CONN_STRING")
  187. } else if config.Config.Server.GRPCConnString != "" {
  188. conn = config.Config.Server.GRPCConnString
  189. }
  190. return conn
  191. }
  192. // GetCoreDNSAddr - gets the core dns address
  193. func GetCoreDNSAddr() string {
  194. addr, _ := GetPublicIP()
  195. if os.Getenv("COREDNS_ADDR") != "" {
  196. addr = os.Getenv("COREDNS_ADDR")
  197. } else if config.Config.Server.CoreDNSAddr != "" {
  198. addr = config.Config.Server.GRPCConnString
  199. }
  200. return addr
  201. }
  202. // GetGRPCHost - get the grpc host url
  203. func GetGRPCHost() string {
  204. serverhost := "127.0.0.1"
  205. remoteip, _ := GetPublicIP()
  206. if os.Getenv("SERVER_GRPC_HOST") != "" {
  207. serverhost = os.Getenv("SERVER_GRPC_HOST")
  208. } else if config.Config.Server.GRPCHost != "" {
  209. serverhost = config.Config.Server.GRPCHost
  210. } else if os.Getenv("SERVER_HOST") != "" {
  211. serverhost = os.Getenv("SERVER_HOST")
  212. } else {
  213. if remoteip != "" {
  214. serverhost = remoteip
  215. }
  216. }
  217. return serverhost
  218. }
  219. // GetGRPCPort - gets the grpc port
  220. func GetGRPCPort() string {
  221. grpcport := "50051"
  222. if os.Getenv("GRPC_PORT") != "" {
  223. grpcport = os.Getenv("GRPC_PORT")
  224. } else if config.Config.Server.GRPCPort != "" {
  225. grpcport = config.Config.Server.GRPCPort
  226. }
  227. return grpcport
  228. }
  229. // GetMasterKey - gets the configured master key of server
  230. func GetMasterKey() string {
  231. key := "secretkey"
  232. if os.Getenv("MASTER_KEY") != "" {
  233. key = os.Getenv("MASTER_KEY")
  234. } else if config.Config.Server.MasterKey != "" {
  235. key = config.Config.Server.MasterKey
  236. }
  237. return key
  238. }
  239. // GetDNSKey - gets the configured dns key of server
  240. func GetDNSKey() string {
  241. key := "secretkey"
  242. if os.Getenv("DNS_KEY") != "" {
  243. key = os.Getenv("DNS_KEY")
  244. } else if config.Config.Server.DNSKey != "" {
  245. key = config.Config.Server.DNSKey
  246. }
  247. return key
  248. }
  249. // GetAllowedOrigin - get the allowed origin
  250. func GetAllowedOrigin() string {
  251. allowedorigin := "*"
  252. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  253. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  254. } else if config.Config.Server.AllowedOrigin != "" {
  255. allowedorigin = config.Config.Server.AllowedOrigin
  256. }
  257. return allowedorigin
  258. }
  259. // IsRestBackend - checks if rest is on or off
  260. func IsRestBackend() bool {
  261. isrest := true
  262. if os.Getenv("REST_BACKEND") != "" {
  263. if os.Getenv("REST_BACKEND") == "off" {
  264. isrest = false
  265. }
  266. } else if config.Config.Server.RestBackend != "" {
  267. if config.Config.Server.RestBackend == "off" {
  268. isrest = false
  269. }
  270. }
  271. return isrest
  272. }
  273. // IsAgentBackend - checks if agent backed is on or off
  274. func IsAgentBackend() bool {
  275. isagent := true
  276. if os.Getenv("AGENT_BACKEND") != "" {
  277. if os.Getenv("AGENT_BACKEND") == "off" {
  278. isagent = false
  279. }
  280. } else if config.Config.Server.AgentBackend != "" {
  281. if config.Config.Server.AgentBackend == "off" {
  282. isagent = false
  283. }
  284. }
  285. return isagent
  286. }
  287. // IsClientMode - checks if it should run in client mode
  288. func IsClientMode() string {
  289. isclient := "on"
  290. if os.Getenv("CLIENT_MODE") == "off" {
  291. isclient = "off"
  292. }
  293. if config.Config.Server.ClientMode == "off" {
  294. isclient = "off"
  295. }
  296. return isclient
  297. }
  298. // Telemetry - checks if telemetry data should be sent
  299. func Telemetry() string {
  300. telemetry := "on"
  301. if os.Getenv("TELEMETRY") == "off" {
  302. telemetry = "off"
  303. }
  304. if config.Config.Server.Telemetry == "off" {
  305. telemetry = "off"
  306. }
  307. return telemetry
  308. }
  309. // ManageIPTables - checks if iptables should be manipulated on host
  310. func ManageIPTables() string {
  311. manage := "on"
  312. if os.Getenv("MANAGE_IPTABLES") == "off" {
  313. manage = "off"
  314. }
  315. if config.Config.Server.ManageIPTables == "off" {
  316. manage = "off"
  317. }
  318. return manage
  319. }
  320. // IsDNSMode - should it run with DNS
  321. func IsDNSMode() bool {
  322. isdns := true
  323. if os.Getenv("DNS_MODE") != "" {
  324. if os.Getenv("DNS_MODE") == "off" {
  325. isdns = false
  326. }
  327. } else if config.Config.Server.DNSMode != "" {
  328. if config.Config.Server.DNSMode == "off" {
  329. isdns = false
  330. }
  331. }
  332. return isdns
  333. }
  334. // IsDisplayKeys - should server be able to display keys?
  335. func IsDisplayKeys() bool {
  336. isdisplay := true
  337. if os.Getenv("DISPLAY_KEYS") != "" {
  338. if os.Getenv("DISPLAY_KEYS") == "off" {
  339. isdisplay = false
  340. }
  341. } else if config.Config.Server.DisplayKeys != "" {
  342. if config.Config.Server.DisplayKeys == "off" {
  343. isdisplay = false
  344. }
  345. }
  346. return isdisplay
  347. }
  348. // IsGRPCSSL - ssl grpc on or off
  349. func IsGRPCSSL() bool {
  350. isssl := false
  351. if os.Getenv("GRPC_SSL") != "" {
  352. if os.Getenv("GRPC_SSL") == "on" {
  353. isssl = true
  354. }
  355. } else if config.Config.Server.DNSMode != "" {
  356. if config.Config.Server.DNSMode == "on" {
  357. isssl = true
  358. }
  359. }
  360. return isssl
  361. }
  362. // DisableRemoteIPCheck - disable the remote ip check
  363. func DisableRemoteIPCheck() bool {
  364. disabled := false
  365. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  366. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  367. disabled = true
  368. }
  369. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  370. if config.Config.Server.DisableRemoteIPCheck == "on" {
  371. disabled = true
  372. }
  373. }
  374. return disabled
  375. }
  376. // DisableDefaultNet - disable default net
  377. func DisableDefaultNet() bool {
  378. disabled := false
  379. if os.Getenv("DISABLE_DEFAULT_NET") != "" {
  380. if os.Getenv("DISABLE_DEFAULT_NET") == "on" {
  381. disabled = true
  382. }
  383. } else if config.Config.Server.DisableDefaultNet != "" {
  384. if config.Config.Server.DisableDefaultNet == "on" {
  385. disabled = true
  386. }
  387. }
  388. return disabled
  389. }
  390. // GetPublicIP - gets public ip
  391. func GetPublicIP() (string, error) {
  392. endpoint := ""
  393. var err error
  394. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  395. for _, ipserver := range iplist {
  396. resp, err := http.Get(ipserver)
  397. if err != nil {
  398. continue
  399. }
  400. defer resp.Body.Close()
  401. if resp.StatusCode == http.StatusOK {
  402. bodyBytes, err := io.ReadAll(resp.Body)
  403. if err != nil {
  404. continue
  405. }
  406. endpoint = string(bodyBytes)
  407. break
  408. }
  409. }
  410. if err == nil && endpoint == "" {
  411. err = errors.New("public address not found")
  412. }
  413. return endpoint, err
  414. }
  415. // GetPlatform - get the system type of server
  416. func GetPlatform() string {
  417. platform := "linux"
  418. if os.Getenv("PLATFORM") != "" {
  419. platform = os.Getenv("PLATFORM")
  420. } else if config.Config.Server.Platform != "" {
  421. platform = config.Config.Server.SQLConn
  422. }
  423. return platform
  424. }
  425. // GetIPForwardServiceList - get the list of services that the server should be forwarding
  426. func GetPortForwardServiceList() []string {
  427. //services := "mq,dns,ssh"
  428. services := ""
  429. if os.Getenv("PORT_FORWARD_SERVICES") != "" {
  430. services = os.Getenv("PORT_FORWARD_SERVICES")
  431. } else if config.Config.Server.PortForwardServices != "" {
  432. services = config.Config.Server.PortForwardServices
  433. }
  434. serviceSlice := strings.Split(services, ",")
  435. return serviceSlice
  436. }
  437. // GetSQLConn - get the sql connection string
  438. func GetSQLConn() string {
  439. sqlconn := "http://"
  440. if os.Getenv("SQL_CONN") != "" {
  441. sqlconn = os.Getenv("SQL_CONN")
  442. } else if config.Config.Server.SQLConn != "" {
  443. sqlconn = config.Config.Server.SQLConn
  444. }
  445. return sqlconn
  446. }
  447. // IsSplitDNS - checks if split dns is on
  448. func IsSplitDNS() bool {
  449. issplit := false
  450. if os.Getenv("IS_SPLIT_DNS") == "yes" {
  451. issplit = true
  452. } else if config.Config.Server.SplitDNS == "yes" {
  453. issplit = true
  454. }
  455. return issplit
  456. }
  457. // GetNodeID - gets the node id
  458. func GetNodeID() string {
  459. var id string
  460. id = getMacAddr()
  461. if os.Getenv("NODE_ID") != "" {
  462. id = os.Getenv("NODE_ID")
  463. } else if config.Config.Server.NodeID != "" {
  464. id = config.Config.Server.NodeID
  465. }
  466. return id
  467. }
  468. // GetServerCheckinInterval - gets the server check-in time
  469. func GetServerCheckinInterval() int64 {
  470. var t = int64(5)
  471. var envt, _ = strconv.Atoi(os.Getenv("SERVER_CHECKIN_INTERVAL"))
  472. if envt > 0 {
  473. t = int64(envt)
  474. } else if config.Config.Server.ServerCheckinInterval > 0 {
  475. t = config.Config.Server.ServerCheckinInterval
  476. }
  477. return t
  478. }
  479. // GetAuthProviderInfo = gets the oauth provider info
  480. func GetAuthProviderInfo() []string {
  481. var authProvider = ""
  482. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  483. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  484. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  485. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  486. } else {
  487. authProvider = ""
  488. }
  489. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  490. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  491. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  492. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  493. }
  494. }
  495. return []string{"", "", ""}
  496. }
  497. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  498. func GetAzureTenant() string {
  499. var azureTenant = ""
  500. if os.Getenv("AZURE_TENANT") != "" {
  501. azureTenant = os.Getenv("AZURE_TENANT")
  502. } else if config.Config.Server.AzureTenant != "" {
  503. azureTenant = config.Config.Server.AzureTenant
  504. }
  505. return azureTenant
  506. }
  507. // GetMacAddr - get's mac address
  508. func getMacAddr() string {
  509. ifas, err := net.Interfaces()
  510. if err != nil {
  511. return ""
  512. }
  513. var as []string
  514. for _, ifa := range ifas {
  515. a := ifa.HardwareAddr.String()
  516. if a != "" {
  517. as = append(as, a)
  518. }
  519. }
  520. return as[0]
  521. }
  522. // GetRce - sees if Rce is enabled, off by default
  523. func GetRce() bool {
  524. return os.Getenv("RCE") == "on" || config.Config.Server.RCE == "on"
  525. }