serverconf.go 6.4 KB

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