serverconf.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 IsGRPCWireGuard() {
  88. serverhost = GetGRPCWGAddress()
  89. } else {
  90. if os.Getenv("SERVER_GRPC_HOST") != "" {
  91. serverhost = os.Getenv("SERVER_GRPC_HOST")
  92. } else if config.Config.Server.GRPCHost != "" {
  93. serverhost = config.Config.Server.GRPCHost
  94. } else if os.Getenv("SERVER_HOST") != "" {
  95. serverhost = os.Getenv("SERVER_HOST")
  96. } else {
  97. remoteip, _ := GetPublicIP()
  98. if remoteip != "" {
  99. serverhost = remoteip
  100. }
  101. }
  102. }
  103. return serverhost
  104. }
  105. func GetGRPCPort() string {
  106. grpcport := "50051"
  107. if os.Getenv("GRPC_PORT") != "" {
  108. grpcport = os.Getenv("GRPC_PORT")
  109. } else if config.Config.Server.GRPCPort != "" {
  110. grpcport = config.Config.Server.GRPCPort
  111. }
  112. return grpcport
  113. }
  114. func GetMasterKey() string {
  115. key := "secretkey"
  116. if os.Getenv("MASTER_KEY") != "" {
  117. key = os.Getenv("MASTER_KEY")
  118. } else if config.Config.Server.MasterKey != "" {
  119. key = config.Config.Server.MasterKey
  120. }
  121. return key
  122. }
  123. func GetAllowedOrigin() string {
  124. allowedorigin := "*"
  125. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  126. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  127. } else if config.Config.Server.AllowedOrigin != "" {
  128. allowedorigin = config.Config.Server.AllowedOrigin
  129. }
  130. return allowedorigin
  131. }
  132. func IsRestBackend() bool {
  133. isrest := true
  134. if os.Getenv("REST_BACKEND") != "" {
  135. if os.Getenv("REST_BACKEND") == "off" {
  136. isrest = false
  137. }
  138. } else if config.Config.Server.RestBackend != "" {
  139. if config.Config.Server.RestBackend == "off" {
  140. isrest = false
  141. }
  142. }
  143. return isrest
  144. }
  145. func IsAgentBackend() bool {
  146. isagent := true
  147. if os.Getenv("AGENT_BACKEND") != "" {
  148. if os.Getenv("AGENT_BACKEND") == "off" {
  149. isagent = false
  150. }
  151. } else if config.Config.Server.AgentBackend != "" {
  152. if config.Config.Server.AgentBackend == "off" {
  153. isagent = false
  154. }
  155. }
  156. return isagent
  157. }
  158. func IsClientMode() bool {
  159. isclient := true
  160. if os.Getenv("CLIENT_MODE") != "" {
  161. if os.Getenv("CLIENT_MODE") == "off" {
  162. isclient = false
  163. }
  164. } else if config.Config.Server.ClientMode != "" {
  165. if config.Config.Server.ClientMode == "off" {
  166. isclient = false
  167. }
  168. }
  169. return isclient
  170. }
  171. func IsDNSMode() bool {
  172. isdns := true
  173. if os.Getenv("DNS_MODE") != "" {
  174. if os.Getenv("DNS_MODE") == "off" {
  175. isdns = false
  176. }
  177. } else if config.Config.Server.DNSMode != "" {
  178. if config.Config.Server.DNSMode == "off" {
  179. isdns = false
  180. }
  181. }
  182. return isdns
  183. }
  184. func DisableRemoteIPCheck() bool {
  185. disabled := false
  186. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  187. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  188. disabled = true
  189. }
  190. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  191. if config.Config.Server.DisableRemoteIPCheck == "on" {
  192. disabled= true
  193. }
  194. }
  195. return disabled
  196. }
  197. func GetPublicIP() (string, error) {
  198. endpoint := ""
  199. var err error
  200. iplist := []string{"http://ip.server.gravitl.com", "https://ifconfig.me", "http://api.ipify.org", "http://ipinfo.io/ip"}
  201. for _, ipserver := range iplist {
  202. resp, err := http.Get(ipserver)
  203. if err != nil {
  204. continue
  205. }
  206. defer resp.Body.Close()
  207. if resp.StatusCode == http.StatusOK {
  208. bodyBytes, err := ioutil.ReadAll(resp.Body)
  209. if err != nil {
  210. continue
  211. }
  212. endpoint = string(bodyBytes)
  213. break
  214. }
  215. }
  216. if err == nil && endpoint == "" {
  217. err = errors.New("Public Address Not Found.")
  218. }
  219. return endpoint, err
  220. }