serverconf.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package servercfg
  2. import (
  3. "github.com/gravitl/netmaker/config"
  4. "net/http"
  5. "io/ioutil"
  6. "os"
  7. "errors"
  8. "strconv"
  9. )
  10. func SetHost() error {
  11. remoteip, err := GetPublicIP()
  12. if err != nil {
  13. return err
  14. }
  15. os.Setenv("SERVER_HOST", remoteip)
  16. return nil
  17. }
  18. func GetServerConfig() config.ServerConfig {
  19. var cfg config.ServerConfig
  20. cfg.APIConnString = GetAPIConnString()
  21. cfg.APIHost = GetAPIHost()
  22. cfg.APIPort = GetAPIPort()
  23. cfg.GRPCConnString = GetGRPCConnString()
  24. cfg.GRPCHost = GetGRPCHost()
  25. cfg.GRPCPort = GetGRPCPort()
  26. cfg.MasterKey = "(hidden)"
  27. cfg.AllowedOrigin = GetAllowedOrigin()
  28. cfg.RestBackend = "off"
  29. if IsRestBackend() {
  30. cfg.RestBackend = "on"
  31. }
  32. cfg.AgentBackend = "off"
  33. if IsAgentBackend() {
  34. cfg.AgentBackend = "on"
  35. }
  36. cfg.ClientMode = "off"
  37. if IsClientMode() {
  38. cfg.ClientMode = "on"
  39. }
  40. cfg.DNSMode = "off"
  41. if IsDNSMode() {
  42. cfg.DNSMode = "on"
  43. }
  44. cfg.GRPCSSL = "off"
  45. if IsGRPCSSL() {
  46. cfg.GRPCSSL = "on"
  47. }
  48. cfg.DisableRemoteIPCheck = "off"
  49. if DisableRemoteIPCheck() {
  50. cfg.DisableRemoteIPCheck = "on"
  51. }
  52. cfg.DisableDefaultNet = "off"
  53. if DisableDefaultNet() {
  54. cfg.DisableRemoteIPCheck = "on"
  55. }
  56. return cfg
  57. }
  58. func GetWGConfig() config.WG{
  59. var cfg config.WG
  60. if IsGRPCWireGuard() {
  61. cfg.GRPCWireGuard = "on"
  62. } else {
  63. cfg.GRPCWireGuard = "off"
  64. }
  65. cfg.GRPCWGInterface = GetGRPCWGInterface()
  66. cfg.GRPCWGAddress = GetGRPCWGAddress()
  67. cfg.GRPCWGAddressRange = GetGRPCWGAddressRange()
  68. cfg.GRPCWGPort = GetGRPCWGPort()
  69. cfg.GRPCWGPubKey = GetGRPCWGPubKey()
  70. cfg.GRPCWGPrivKey = GetGRPCWGPrivKey()
  71. return cfg
  72. }
  73. func GetAPIConnString() string {
  74. conn := ""
  75. if os.Getenv("SERVER_API_CONN_STRING") != "" {
  76. conn = os.Getenv("SERVER_API_CONN_STRING")
  77. } else if config.Config.Server.APIConnString != "" {
  78. conn = config.Config.Server.APIConnString
  79. }
  80. return conn
  81. }
  82. func GetAPIHost() string {
  83. serverhost := "127.0.0.1"
  84. if os.Getenv("SERVER_HTTP_HOST") != "" {
  85. serverhost = os.Getenv("SERVER_HTTP_HOST")
  86. } else if config.Config.Server.APIHost != "" {
  87. serverhost = config.Config.Server.APIHost
  88. } else if os.Getenv("SERVER_HOST") != "" {
  89. serverhost = os.Getenv("SERVER_HOST")
  90. } else {
  91. remoteip, _ := GetPublicIP()
  92. if remoteip != "" {
  93. serverhost = remoteip
  94. }
  95. }
  96. return serverhost
  97. }
  98. func GetAPIPort() string {
  99. apiport := "8081"
  100. if os.Getenv("API_PORT") != "" {
  101. apiport = os.Getenv("API_PORT")
  102. } else if config.Config.Server.APIPort != "" {
  103. apiport = config.Config.Server.APIPort
  104. }
  105. return apiport
  106. }
  107. func GetDefaultNodeLimit() int32 {
  108. var limit int32
  109. limit = 999999999
  110. envlimit, err := strconv.Atoi(os.Getenv("DEFAULT_NODE_LIMIT"))
  111. if err == nil && envlimit != 0 {
  112. limit = int32(envlimit)
  113. } else if config.Config.Server.DefaultNodeLimit != 0 {
  114. limit = config.Config.Server.DefaultNodeLimit
  115. }
  116. return limit
  117. }
  118. func GetGRPCConnString() string {
  119. conn := ""
  120. if os.Getenv("SERVER_GRPC_CONN_STRING") != "" {
  121. conn = os.Getenv("SERVER_GRPC_CONN_STRING")
  122. } else if config.Config.Server.GRPCConnString != "" {
  123. conn = config.Config.Server.GRPCConnString
  124. }
  125. return conn
  126. }
  127. func GetGRPCHost() string {
  128. serverhost := "127.0.0.1"
  129. if IsGRPCWireGuard() {
  130. serverhost = GetGRPCWGAddress()
  131. } else {
  132. if os.Getenv("SERVER_GRPC_HOST") != "" {
  133. serverhost = os.Getenv("SERVER_GRPC_HOST")
  134. } else if config.Config.Server.GRPCHost != "" {
  135. serverhost = config.Config.Server.GRPCHost
  136. } else if os.Getenv("SERVER_HOST") != "" {
  137. serverhost = os.Getenv("SERVER_HOST")
  138. } else {
  139. remoteip, _ := GetPublicIP()
  140. if remoteip != "" {
  141. serverhost = remoteip
  142. }
  143. }
  144. }
  145. return serverhost
  146. }
  147. func GetGRPCPort() string {
  148. grpcport := "50051"
  149. if os.Getenv("GRPC_PORT") != "" {
  150. grpcport = os.Getenv("GRPC_PORT")
  151. } else if config.Config.Server.GRPCPort != "" {
  152. grpcport = config.Config.Server.GRPCPort
  153. }
  154. return grpcport
  155. }
  156. func GetMasterKey() string {
  157. key := "secretkey"
  158. if os.Getenv("MASTER_KEY") != "" {
  159. key = os.Getenv("MASTER_KEY")
  160. } else if config.Config.Server.MasterKey != "" {
  161. key = config.Config.Server.MasterKey
  162. }
  163. return key
  164. }
  165. func GetAllowedOrigin() string {
  166. allowedorigin := "*"
  167. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  168. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  169. } else if config.Config.Server.AllowedOrigin != "" {
  170. allowedorigin = config.Config.Server.AllowedOrigin
  171. }
  172. return allowedorigin
  173. }
  174. func IsRestBackend() bool {
  175. isrest := true
  176. if os.Getenv("REST_BACKEND") != "" {
  177. if os.Getenv("REST_BACKEND") == "off" {
  178. isrest = false
  179. }
  180. } else if config.Config.Server.RestBackend != "" {
  181. if config.Config.Server.RestBackend == "off" {
  182. isrest = false
  183. }
  184. }
  185. return isrest
  186. }
  187. func IsAgentBackend() bool {
  188. isagent := true
  189. if os.Getenv("AGENT_BACKEND") != "" {
  190. if os.Getenv("AGENT_BACKEND") == "off" {
  191. isagent = false
  192. }
  193. } else if config.Config.Server.AgentBackend != "" {
  194. if config.Config.Server.AgentBackend == "off" {
  195. isagent = false
  196. }
  197. }
  198. return isagent
  199. }
  200. func IsClientMode() bool {
  201. isclient := true
  202. if os.Getenv("CLIENT_MODE") != "" {
  203. if os.Getenv("CLIENT_MODE") == "off" {
  204. isclient = false
  205. }
  206. } else if config.Config.Server.ClientMode != "" {
  207. if config.Config.Server.ClientMode == "off" {
  208. isclient = false
  209. }
  210. }
  211. return isclient
  212. }
  213. func IsDNSMode() bool {
  214. isdns := true
  215. if os.Getenv("DNS_MODE") != "" {
  216. if os.Getenv("DNS_MODE") == "off" {
  217. isdns = false
  218. }
  219. } else if config.Config.Server.DNSMode != "" {
  220. if config.Config.Server.DNSMode == "off" {
  221. isdns = false
  222. }
  223. }
  224. return isdns
  225. }
  226. func IsGRPCSSL() bool {
  227. isssl := false
  228. if os.Getenv("GRPC_SSL") != "" {
  229. if os.Getenv("GRPC_SSL") == "on" {
  230. isssl = true
  231. }
  232. } else if config.Config.Server.DNSMode != "" {
  233. if config.Config.Server.DNSMode == "on" {
  234. isssl = true
  235. }
  236. }
  237. return isssl
  238. }
  239. func DisableRemoteIPCheck() bool {
  240. disabled := false
  241. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  242. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  243. disabled = true
  244. }
  245. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  246. if config.Config.Server.DisableRemoteIPCheck == "on" {
  247. disabled= true
  248. }
  249. }
  250. return disabled
  251. }
  252. func DisableDefaultNet() bool {
  253. disabled := false
  254. if os.Getenv("DISABLE_DEFAULT_NET") != "" {
  255. if os.Getenv("DISABLE_DEFAULT_NET") == "on" {
  256. disabled = true
  257. }
  258. } else if config.Config.Server.DisableDefaultNet != "" {
  259. if config.Config.Server.DisableDefaultNet == "on" {
  260. disabled= true
  261. }
  262. }
  263. return disabled
  264. }
  265. func GetPublicIP() (string, error) {
  266. endpoint := ""
  267. var err error
  268. iplist := []string{"http://ip.server.gravitl.com", "https://ifconfig.me", "http://api.ipify.org", "http://ipinfo.io/ip"}
  269. for _, ipserver := range iplist {
  270. resp, err := http.Get(ipserver)
  271. if err != nil {
  272. continue
  273. }
  274. defer resp.Body.Close()
  275. if resp.StatusCode == http.StatusOK {
  276. bodyBytes, err := ioutil.ReadAll(resp.Body)
  277. if err != nil {
  278. continue
  279. }
  280. endpoint = string(bodyBytes)
  281. break
  282. }
  283. }
  284. if err == nil && endpoint == "" {
  285. err = errors.New("Public Address Not Found.")
  286. }
  287. return endpoint, err
  288. }