serverconf.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package servercfg
  2. import (
  3. "github.com/gravitl/netmaker/config"
  4. "net/http"
  5. "io/ioutil"
  6. "os"
  7. "errors"
  8. )
  9. func SetHost() error {
  10. remoteip, err := GetPublicIP()
  11. if err != nil {
  12. return err
  13. }
  14. os.Setenv("SERVER_HOST", remoteip)
  15. return nil
  16. }
  17. func GetConfig() config.ServerConfig {
  18. var cfg config.ServerConfig
  19. cfg.APIHost = GetAPIHost()
  20. cfg.APIPort = GetAPIPort()
  21. cfg.GRPCHost = GetGRPCHost()
  22. cfg.GRPCPort = GetGRPCPort()
  23. cfg.MasterKey = "(hidden)"
  24. cfg.AllowedOrigin = GetAllowedOrigin()
  25. cfg.RestBackend = "off"
  26. if IsRestBackend() {
  27. cfg.RestBackend = "on"
  28. }
  29. cfg.AgentBackend = "off"
  30. if IsAgentBackend() {
  31. cfg.AgentBackend = "on"
  32. }
  33. cfg.ClientMode = "off"
  34. if IsClientMode() {
  35. cfg.ClientMode = "on"
  36. }
  37. cfg.DNSMode = "off"
  38. if IsDNSMode() {
  39. cfg.DNSMode = "on"
  40. }
  41. cfg.DisableRemoteIPCheck = "off"
  42. if DisableRemoteIPCheck() {
  43. cfg.DisableRemoteIPCheck = "on"
  44. }
  45. return cfg
  46. }
  47. func GetWGConfig() config.WG{
  48. var cfg config.WG
  49. if IsGRPCWireGuard() {
  50. cfg.GRPCWireGuard = "on"
  51. } else {
  52. cfg.GRPCWireGuard = "off"
  53. }
  54. cfg.GRPCWGInterface = GetGRPCWGInterface()
  55. cfg.GRPCWGAddress = GetGRPCWGAddress()
  56. cfg.GRPCWGPort = GetGRPCWGPort()
  57. cfg.GRPCWGEndpoint = GetGRPCHost()
  58. return cfg
  59. }
  60. func GetAPIHost() string {
  61. serverhost := "127.0.0.1"
  62. if os.Getenv("SERVER_HTTP_HOST") != "" {
  63. serverhost = os.Getenv("SERVER_HTTP_HOST")
  64. } else if config.Config.Server.APIHost != "" {
  65. serverhost = config.Config.Server.APIHost
  66. } else if os.Getenv("SERVER_HOST") != "" {
  67. serverhost = os.Getenv("SERVER_HOST")
  68. } else {
  69. remoteip, _ := GetPublicIP()
  70. if remoteip != "" {
  71. serverhost = remoteip
  72. }
  73. }
  74. return serverhost
  75. }
  76. func GetAPIPort() string {
  77. apiport := "8081"
  78. if os.Getenv("API_PORT") != "" {
  79. apiport = os.Getenv("API_PORT")
  80. } else if config.Config.Server.APIPort != "" {
  81. apiport = config.Config.Server.APIPort
  82. }
  83. return apiport
  84. }
  85. func GetGRPCHost() string {
  86. serverhost := "127.0.0.1"
  87. if os.Getenv("SERVER_GRPC_HOST") != "" {
  88. serverhost = os.Getenv("SERVER_GRPC_HOST")
  89. } else if config.Config.Server.GRPCHost != "" {
  90. serverhost = config.Config.Server.GRPCHost
  91. } else if os.Getenv("SERVER_HOST") != "" {
  92. serverhost = os.Getenv("SERVER_HOST")
  93. } else {
  94. remoteip, _ := GetPublicIP()
  95. if remoteip != "" {
  96. serverhost = remoteip
  97. }
  98. }
  99. return serverhost
  100. }
  101. func GetGRPCPort() string {
  102. grpcport := "50051"
  103. if os.Getenv("GRPC_PORT") != "" {
  104. grpcport = os.Getenv("GRPC_PORT")
  105. } else if config.Config.Server.GRPCPort != "" {
  106. grpcport = config.Config.Server.GRPCPort
  107. }
  108. return grpcport
  109. }
  110. func GetMasterKey() string {
  111. key := "secretkey"
  112. if os.Getenv("MASTER_KEY") != "" {
  113. key = os.Getenv("MASTER_KEY")
  114. } else if config.Config.Server.MasterKey != "" {
  115. key = config.Config.Server.MasterKey
  116. }
  117. return key
  118. }
  119. func GetAllowedOrigin() string {
  120. allowedorigin := "*"
  121. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  122. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  123. } else if config.Config.Server.AllowedOrigin != "" {
  124. allowedorigin = config.Config.Server.AllowedOrigin
  125. }
  126. return allowedorigin
  127. }
  128. func IsRestBackend() bool {
  129. isrest := true
  130. if os.Getenv("REST_BACKEND") != "" {
  131. if os.Getenv("REST_BACKEND") == "off" {
  132. isrest = false
  133. }
  134. } else if config.Config.Server.RestBackend != "" {
  135. if config.Config.Server.RestBackend == "off" {
  136. isrest = false
  137. }
  138. }
  139. return isrest
  140. }
  141. func IsAgentBackend() bool {
  142. isagent := true
  143. if os.Getenv("AGENT_BACKEND") != "" {
  144. if os.Getenv("AGENT_BACKEND") == "off" {
  145. isagent = false
  146. }
  147. } else if config.Config.Server.AgentBackend != "" {
  148. if config.Config.Server.AgentBackend == "off" {
  149. isagent = false
  150. }
  151. }
  152. return isagent
  153. }
  154. func IsClientMode() bool {
  155. isclient := true
  156. if os.Getenv("CLIENT_MODE") != "" {
  157. if os.Getenv("CLIENT_MODE") == "off" {
  158. isclient = false
  159. }
  160. } else if config.Config.Server.ClientMode != "" {
  161. if config.Config.Server.ClientMode == "off" {
  162. isclient = false
  163. }
  164. }
  165. return isclient
  166. }
  167. func IsDNSMode() bool {
  168. isdns := true
  169. if os.Getenv("DNS_MODE") != "" {
  170. if os.Getenv("DNS_MODE") == "off" {
  171. isdns = false
  172. }
  173. } else if config.Config.Server.DNSMode != "" {
  174. if config.Config.Server.DNSMode == "off" {
  175. isdns = false
  176. }
  177. }
  178. return isdns
  179. }
  180. func DisableRemoteIPCheck() bool {
  181. disabled := false
  182. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  183. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  184. disabled = true
  185. }
  186. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  187. if config.Config.Server.DisableRemoteIPCheck == "on" {
  188. disabled= true
  189. }
  190. }
  191. return disabled
  192. }
  193. func GetPublicIP() (string, error) {
  194. endpoint := ""
  195. var err error
  196. iplist := []string{"http://ip.server.gravitl.com", "https://ifconfig.me", "http://api.ipify.org", "http://ipinfo.io/ip"}
  197. for _, ipserver := range iplist {
  198. resp, err := http.Get(ipserver)
  199. if err != nil {
  200. continue
  201. }
  202. defer resp.Body.Close()
  203. if resp.StatusCode == http.StatusOK {
  204. bodyBytes, err := ioutil.ReadAll(resp.Body)
  205. if err != nil {
  206. continue
  207. }
  208. endpoint = string(bodyBytes)
  209. break
  210. }
  211. }
  212. if err == nil && endpoint == "" {
  213. err = errors.New("Public Address Not Found.")
  214. }
  215. return endpoint, err
  216. }