serverconf.go 13 KB

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