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