config.go 12 KB

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