config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. "github.com/fatedier/frp/src/models/consts"
  22. "github.com/fatedier/frp/src/models/metric"
  23. "github.com/fatedier/frp/src/utils/log"
  24. "github.com/fatedier/frp/src/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. DashboardUsername string = "admin"
  35. DashboardPassword string = "admin"
  36. AssetsDir string = ""
  37. LogFile string = "console"
  38. LogWay string = "console" // console or file
  39. LogLevel string = "info"
  40. LogMaxDays int64 = 3
  41. PrivilegeMode bool = false
  42. PrivilegeToken string = ""
  43. // if PrivilegeAllowPorts is not nil, tcp proxies which remote port exist in this map can be connected
  44. PrivilegeAllowPorts map[int64]struct{}
  45. MaxPoolCount int64 = 100
  46. HeartBeatTimeout int64 = 90
  47. UserConnTimeout int64 = 10
  48. VhostHttpMuxer *vhost.HttpMuxer
  49. VhostHttpsMuxer *vhost.HttpsMuxer
  50. ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer) // all proxy servers info and resources
  51. ProxyServersMutex sync.RWMutex
  52. )
  53. func LoadConf(confFile string) (err error) {
  54. err = loadCommonConf(confFile)
  55. if err != nil {
  56. return err
  57. }
  58. // load all proxy server's configure and initialize
  59. // and set ProxyServers map
  60. newProxyServers, err := loadProxyConf(confFile)
  61. if err != nil {
  62. return err
  63. }
  64. for _, proxyServer := range newProxyServers {
  65. proxyServer.Init()
  66. }
  67. ProxyServersMutex.Lock()
  68. ProxyServers = newProxyServers
  69. ProxyServersMutex.Unlock()
  70. return nil
  71. }
  72. func loadCommonConf(confFile string) error {
  73. var tmpStr string
  74. var ok bool
  75. conf, err := ini.LoadFile(confFile)
  76. if err != nil {
  77. return err
  78. }
  79. // common
  80. tmpStr, ok = conf.Get("common", "bind_addr")
  81. if ok {
  82. BindAddr = tmpStr
  83. }
  84. tmpStr, ok = conf.Get("common", "bind_port")
  85. if ok {
  86. v, err := strconv.ParseInt(tmpStr, 10, 64)
  87. if err == nil {
  88. BindPort = v
  89. }
  90. }
  91. tmpStr, ok = conf.Get("common", "vhost_http_port")
  92. if ok {
  93. VhostHttpPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  94. } else {
  95. VhostHttpPort = 0
  96. }
  97. tmpStr, ok = conf.Get("common", "vhost_https_port")
  98. if ok {
  99. VhostHttpsPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  100. } else {
  101. VhostHttpsPort = 0
  102. }
  103. tmpStr, ok = conf.Get("common", "dashboard_port")
  104. if ok {
  105. DashboardPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  106. } else {
  107. DashboardPort = 0
  108. }
  109. tmpStr, ok = conf.Get("common", "dashboard_username")
  110. if ok {
  111. DashboardUsername = tmpStr
  112. }
  113. tmpStr, ok = conf.Get("common", "dashboard_password")
  114. if ok {
  115. DashboardPassword = tmpStr
  116. }
  117. tmpStr, ok = conf.Get("common", "assets_dir")
  118. if ok {
  119. AssetsDir = tmpStr
  120. }
  121. tmpStr, ok = conf.Get("common", "log_file")
  122. if ok {
  123. LogFile = tmpStr
  124. if LogFile == "console" {
  125. LogWay = "console"
  126. } else {
  127. LogWay = "file"
  128. }
  129. }
  130. tmpStr, ok = conf.Get("common", "log_level")
  131. if ok {
  132. LogLevel = tmpStr
  133. }
  134. tmpStr, ok = conf.Get("common", "log_max_days")
  135. if ok {
  136. v, err := strconv.ParseInt(tmpStr, 10, 64)
  137. if err == nil {
  138. LogMaxDays = v
  139. }
  140. }
  141. tmpStr, ok = conf.Get("common", "privilege_mode")
  142. if ok {
  143. if tmpStr == "true" {
  144. PrivilegeMode = true
  145. }
  146. }
  147. if PrivilegeMode == true {
  148. tmpStr, ok = conf.Get("common", "privilege_token")
  149. if ok {
  150. if tmpStr == "" {
  151. return fmt.Errorf("Parse conf error: privilege_token can not be null")
  152. }
  153. PrivilegeToken = tmpStr
  154. } else {
  155. return fmt.Errorf("Parse conf error: privilege_token must be set if privilege_mode is enabled")
  156. }
  157. PrivilegeAllowPorts = make(map[int64]struct{})
  158. tmpStr, ok = conf.Get("common", "privilege_allow_ports")
  159. if ok {
  160. // for example: 1000-2000,2001,2002,3000-4000
  161. portRanges := strings.Split(tmpStr, ",")
  162. for _, portRangeStr := range portRanges {
  163. // 1000-2000 or 2001
  164. portArray := strings.Split(portRangeStr, "-")
  165. // lenght: only 1 or 2 is correct
  166. rangeType := len(portArray)
  167. if rangeType == 1 {
  168. singlePort, err := strconv.ParseInt(portArray[0], 10, 64)
  169. if err != nil {
  170. return fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", err)
  171. }
  172. PrivilegeAllowPorts[singlePort] = struct{}{}
  173. } else if rangeType == 2 {
  174. min, err := strconv.ParseInt(portArray[0], 10, 64)
  175. if err != nil {
  176. return fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", err)
  177. }
  178. max, err := strconv.ParseInt(portArray[1], 10, 64)
  179. if err != nil {
  180. return fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", err)
  181. }
  182. if max < min {
  183. return fmt.Errorf("Parse conf error: privilege_allow_ports range incorrect")
  184. }
  185. for i := min; i <= max; i++ {
  186. PrivilegeAllowPorts[i] = struct{}{}
  187. }
  188. } else {
  189. return fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect")
  190. }
  191. }
  192. }
  193. }
  194. tmpStr, ok = conf.Get("common", "max_pool_count")
  195. if ok {
  196. v, err := strconv.ParseInt(tmpStr, 10, 64)
  197. if err == nil && v >= 0 {
  198. MaxPoolCount = v
  199. }
  200. }
  201. return nil
  202. }
  203. func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err error) {
  204. var ok bool
  205. proxyServers = make(map[string]*ProxyServer)
  206. conf, err := ini.LoadFile(confFile)
  207. if err != nil {
  208. return proxyServers, err
  209. }
  210. // servers
  211. for name, section := range conf {
  212. if name != "common" {
  213. proxyServer := NewProxyServer()
  214. proxyServer.Name = name
  215. proxyServer.Type, ok = section["type"]
  216. if ok {
  217. if proxyServer.Type != "tcp" && proxyServer.Type != "http" && proxyServer.Type != "https" && proxyServer.Type != "udp" {
  218. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] type error", proxyServer.Name)
  219. }
  220. } else {
  221. proxyServer.Type = "tcp"
  222. }
  223. proxyServer.AuthToken, ok = section["auth_token"]
  224. if !ok {
  225. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] no auth_token found", proxyServer.Name)
  226. }
  227. // for tcp and udp
  228. if proxyServer.Type == "tcp" || proxyServer.Type == "udp" {
  229. proxyServer.BindAddr, ok = section["bind_addr"]
  230. if !ok {
  231. proxyServer.BindAddr = "0.0.0.0"
  232. }
  233. portStr, ok := section["listen_port"]
  234. if ok {
  235. proxyServer.ListenPort, err = strconv.ParseInt(portStr, 10, 64)
  236. if err != nil {
  237. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] listen_port error", proxyServer.Name)
  238. }
  239. } else {
  240. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] listen_port not found", proxyServer.Name)
  241. }
  242. } else if proxyServer.Type == "http" {
  243. // for http
  244. proxyServer.ListenPort = VhostHttpPort
  245. domainStr, ok := section["custom_domains"]
  246. if ok {
  247. proxyServer.CustomDomains = strings.Split(domainStr, ",")
  248. if len(proxyServer.CustomDomains) == 0 {
  249. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name)
  250. }
  251. for i, domain := range proxyServer.CustomDomains {
  252. proxyServer.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  253. }
  254. } else {
  255. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name)
  256. }
  257. } else if proxyServer.Type == "https" {
  258. // for https
  259. proxyServer.ListenPort = VhostHttpsPort
  260. domainStr, ok := section["custom_domains"]
  261. if ok {
  262. proxyServer.CustomDomains = strings.Split(domainStr, ",")
  263. if len(proxyServer.CustomDomains) == 0 {
  264. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals https", proxyServer.Name)
  265. }
  266. for i, domain := range proxyServer.CustomDomains {
  267. proxyServer.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  268. }
  269. } else {
  270. return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals https", proxyServer.Name)
  271. }
  272. }
  273. proxyServers[proxyServer.Name] = proxyServer
  274. }
  275. }
  276. // set metric statistics of all proxies
  277. for name, p := range proxyServers {
  278. metric.SetProxyInfo(name, p.Type, p.BindAddr, p.UseEncryption, p.UseGzip,
  279. p.PrivilegeMode, p.CustomDomains, p.ListenPort)
  280. }
  281. return proxyServers, nil
  282. }
  283. // the function can only reload proxy configures
  284. // common section won't be changed
  285. func ReloadConf(confFile string) (err error) {
  286. loadProxyServers, err := loadProxyConf(confFile)
  287. if err != nil {
  288. return err
  289. }
  290. ProxyServersMutex.Lock()
  291. for name, proxyServer := range loadProxyServers {
  292. oldProxyServer, ok := ProxyServers[name]
  293. if ok {
  294. if !oldProxyServer.Compare(proxyServer) {
  295. oldProxyServer.Close()
  296. proxyServer.Init()
  297. ProxyServers[name] = proxyServer
  298. log.Info("ProxyName [%s] configure change, restart", name)
  299. }
  300. } else {
  301. proxyServer.Init()
  302. ProxyServers[name] = proxyServer
  303. log.Info("ProxyName [%s] is new, init it", name)
  304. }
  305. }
  306. // proxies created by PrivilegeMode won't be deleted
  307. for name, oldProxyServer := range ProxyServers {
  308. _, ok := loadProxyServers[name]
  309. if !ok {
  310. if !oldProxyServer.PrivilegeMode {
  311. oldProxyServer.Close()
  312. delete(ProxyServers, name)
  313. log.Info("ProxyName [%s] deleted, close it", name)
  314. } else {
  315. log.Info("ProxyName [%s] created by PrivilegeMode, won't be closed", name)
  316. }
  317. }
  318. }
  319. ProxyServersMutex.Unlock()
  320. return nil
  321. }
  322. func CreateProxy(s *ProxyServer) error {
  323. ProxyServersMutex.Lock()
  324. defer ProxyServersMutex.Unlock()
  325. oldServer, ok := ProxyServers[s.Name]
  326. if ok {
  327. if oldServer.Status == consts.Working {
  328. return fmt.Errorf("this proxy is already working now")
  329. }
  330. oldServer.Close()
  331. if oldServer.PrivilegeMode {
  332. delete(ProxyServers, s.Name)
  333. }
  334. }
  335. ProxyServers[s.Name] = s
  336. metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip,
  337. s.PrivilegeMode, s.CustomDomains, s.ListenPort)
  338. s.Init()
  339. return nil
  340. }
  341. func DeleteProxy(proxyName string) {
  342. ProxyServersMutex.Lock()
  343. defer ProxyServersMutex.Unlock()
  344. delete(ProxyServers, proxyName)
  345. }