config.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package server
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. ini "github.com/vaughan0/go-ini"
  21. "frp/models/consts"
  22. "frp/models/metric"
  23. "frp/utils/log"
  24. "frp/utils/vhost"
  25. )
  26. // common config
  27. var (
  28. ConfigFile string = "./frps.ini"
  29. BindAddr string = "0.0.0.0"
  30. BindPort int64 = 7000
  31. VhostHttpPort int64 = 0 // if VhostHttpPort equals 0, don't listen a public port for http protocol
  32. VhostHttpsPort int64 = 0 // if VhostHttpsPort equals 0, don't listen a public port for https protocol
  33. DashboardPort int64 = 0 // if DashboardPort equals 0, dashboard is not available
  34. LogFile string = "console"
  35. LogWay string = "console" // console or file
  36. LogLevel string = "info"
  37. LogMaxDays int64 = 3
  38. PrivilegeMode bool = false
  39. PrivilegeToken string = ""
  40. HeartBeatTimeout int64 = 90
  41. UserConnTimeout int64 = 10
  42. VhostHttpMuxer *vhost.HttpMuxer
  43. VhostHttpsMuxer *vhost.HttpsMuxer
  44. ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer) // all proxy servers info and resources
  45. ProxyServersMutex sync.RWMutex
  46. )
  47. func LoadConf(confFile string) (err error) {
  48. err = loadCommonConf(confFile)
  49. if err != nil {
  50. return err
  51. }
  52. // load all proxy server's configure and initialize
  53. // and set ProxyServers map
  54. newProxyServers, err := loadProxyConf(confFile)
  55. if err != nil {
  56. return err
  57. }
  58. for _, proxyServer := range newProxyServers {
  59. proxyServer.Init()
  60. }
  61. ProxyServersMutex.Lock()
  62. ProxyServers = newProxyServers
  63. ProxyServersMutex.Unlock()
  64. return nil
  65. }
  66. func loadCommonConf(confFile string) error {
  67. var tmpStr string
  68. var ok bool
  69. conf, err := ini.LoadFile(confFile)
  70. if err != nil {
  71. return err
  72. }
  73. // common
  74. tmpStr, ok = conf.Get("common", "bind_addr")
  75. if ok {
  76. BindAddr = tmpStr
  77. }
  78. tmpStr, ok = conf.Get("common", "bind_port")
  79. if ok {
  80. v, err := strconv.ParseInt(tmpStr, 10, 64)
  81. if err == nil {
  82. BindPort = v
  83. }
  84. }
  85. tmpStr, ok = conf.Get("common", "vhost_http_port")
  86. if ok {
  87. VhostHttpPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  88. } else {
  89. VhostHttpPort = 0
  90. }
  91. tmpStr, ok = conf.Get("common", "vhost_https_port")
  92. if ok {
  93. VhostHttpsPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  94. } else {
  95. VhostHttpsPort = 0
  96. }
  97. tmpStr, ok = conf.Get("common", "dashboard_port")
  98. if ok {
  99. DashboardPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  100. } else {
  101. DashboardPort = 0
  102. }
  103. tmpStr, ok = conf.Get("common", "log_file")
  104. if ok {
  105. LogFile = tmpStr
  106. if LogFile == "console" {
  107. LogWay = "console"
  108. } else {
  109. LogWay = "file"
  110. }
  111. }
  112. tmpStr, ok = conf.Get("common", "log_level")
  113. if ok {
  114. LogLevel = tmpStr
  115. }
  116. tmpStr, ok = conf.Get("common", "log_max_days")
  117. if ok {
  118. v, err := strconv.ParseInt(tmpStr, 10, 64)
  119. if err == nil {
  120. LogMaxDays = v
  121. }
  122. }
  123. tmpStr, ok = conf.Get("common", "privilege_mode")
  124. if ok {
  125. if tmpStr == "true" {
  126. PrivilegeMode = true
  127. }
  128. }
  129. if PrivilegeMode == true {
  130. tmpStr, ok = conf.Get("common", "privilege_token")
  131. if ok {
  132. if tmpStr == "" {
  133. return fmt.Errorf("Parse conf error: privilege_token can not be null")
  134. }
  135. PrivilegeToken = tmpStr
  136. } else {
  137. return fmt.Errorf("Parse conf error: privilege_token must be set if privilege_mode is enabled")
  138. }
  139. }
  140. return nil
  141. }
  142. func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err error) {
  143. var ok bool
  144. proxyServers = make(map[string]*ProxyServer)
  145. conf, err := ini.LoadFile(confFile)
  146. if err != nil {
  147. return proxyServers, err
  148. }
  149. // servers
  150. for name, section := range conf {
  151. if name != "common" {
  152. proxyServer := NewProxyServer()
  153. proxyServer.Name = name
  154. proxyServer.Type, ok = section["type"]
  155. if ok {
  156. if proxyServer.Type != "tcp" && proxyServer.Type != "http" && proxyServer.Type != "https" {
  157. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] type error", proxyServer.Name)
  158. }
  159. } else {
  160. proxyServer.Type = "tcp"
  161. }
  162. proxyServer.AuthToken, ok = section["auth_token"]
  163. if !ok {
  164. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] no auth_token found", proxyServer.Name)
  165. }
  166. // for tcp
  167. if proxyServer.Type == "tcp" {
  168. proxyServer.BindAddr, ok = section["bind_addr"]
  169. if !ok {
  170. proxyServer.BindAddr = "0.0.0.0"
  171. }
  172. portStr, ok := section["listen_port"]
  173. if ok {
  174. proxyServer.ListenPort, err = strconv.ParseInt(portStr, 10, 64)
  175. if err != nil {
  176. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] listen_port error", proxyServer.Name)
  177. }
  178. } else {
  179. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] listen_port not found", proxyServer.Name)
  180. }
  181. } else if proxyServer.Type == "http" {
  182. // for http
  183. domainStr, ok := section["custom_domains"]
  184. if ok {
  185. proxyServer.CustomDomains = strings.Split(domainStr, ",")
  186. if len(proxyServer.CustomDomains) == 0 {
  187. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name)
  188. }
  189. for i, domain := range proxyServer.CustomDomains {
  190. proxyServer.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  191. }
  192. } else {
  193. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name)
  194. }
  195. } else if proxyServer.Type == "https" {
  196. // for https
  197. domainStr, ok := section["custom_domains"]
  198. if ok {
  199. proxyServer.CustomDomains = strings.Split(domainStr, ",")
  200. if len(proxyServer.CustomDomains) == 0 {
  201. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals https", proxyServer.Name)
  202. }
  203. for i, domain := range proxyServer.CustomDomains {
  204. proxyServer.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  205. }
  206. } else {
  207. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals https", proxyServer.Name)
  208. }
  209. }
  210. proxyServers[proxyServer.Name] = proxyServer
  211. }
  212. }
  213. // set metric statistics of all proxies
  214. for name, p := range proxyServers {
  215. metric.SetProxyInfo(name, p.Type, p.BindAddr, p.UseEncryption, p.UseGzip,
  216. p.PrivilegeMode, p.CustomDomains, p.ListenPort)
  217. }
  218. return proxyServers, nil
  219. }
  220. // the function can only reload proxy configures
  221. // common section won't be changed
  222. func ReloadConf(confFile string) (err error) {
  223. loadProxyServers, err := loadProxyConf(confFile)
  224. if err != nil {
  225. return err
  226. }
  227. ProxyServersMutex.Lock()
  228. for name, proxyServer := range loadProxyServers {
  229. oldProxyServer, ok := ProxyServers[name]
  230. if ok {
  231. if !oldProxyServer.Compare(proxyServer) {
  232. oldProxyServer.Close()
  233. proxyServer.Init()
  234. ProxyServers[name] = proxyServer
  235. log.Info("ProxyName [%s] configure change, restart", name)
  236. }
  237. } else {
  238. proxyServer.Init()
  239. ProxyServers[name] = proxyServer
  240. log.Info("ProxyName [%s] is new, init it", name)
  241. }
  242. }
  243. // proxies created by PrivilegeMode won't be deleted
  244. for name, oldProxyServer := range ProxyServers {
  245. _, ok := loadProxyServers[name]
  246. if !ok {
  247. if !oldProxyServer.PrivilegeMode {
  248. oldProxyServer.Close()
  249. delete(ProxyServers, name)
  250. log.Info("ProxyName [%s] deleted, close it", name)
  251. } else {
  252. log.Info("ProxyName [%s] created by PrivilegeMode, won't be closed", name)
  253. }
  254. }
  255. }
  256. ProxyServersMutex.Unlock()
  257. return nil
  258. }
  259. func CreateProxy(s *ProxyServer) error {
  260. ProxyServersMutex.Lock()
  261. defer ProxyServersMutex.Unlock()
  262. oldServer, ok := ProxyServers[s.Name]
  263. if ok {
  264. if oldServer.Status == consts.Working {
  265. return fmt.Errorf("this proxy is already working now")
  266. }
  267. oldServer.Close()
  268. if oldServer.PrivilegeMode {
  269. delete(ProxyServers, s.Name)
  270. }
  271. }
  272. ProxyServers[s.Name] = s
  273. metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip,
  274. s.PrivilegeMode, s.CustomDomains, s.ListenPort)
  275. s.Init()
  276. return nil
  277. }
  278. func DeleteProxy(proxyName string) {
  279. ProxyServersMutex.Lock()
  280. defer ProxyServersMutex.Unlock()
  281. delete(ProxyServers, proxyName)
  282. }