serverconf.go 12 KB

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