server_common.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 config
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. ini "github.com/vaughan0/go-ini"
  20. "github.com/fatedier/frp/models/auth"
  21. plugin "github.com/fatedier/frp/models/plugin/server"
  22. "github.com/fatedier/frp/utils/util"
  23. )
  24. // ServerCommonConf contains information for a server service. It is
  25. // recommended to use GetDefaultServerConf instead of creating this object
  26. // directly, so that all unspecified fields have reasonable default values.
  27. type ServerCommonConf struct {
  28. auth.ServerConfig
  29. // BindAddr specifies the address that the server binds to. By default,
  30. // this value is "0.0.0.0".
  31. BindAddr string `json:"bind_addr"`
  32. // BindPort specifies the port that the server listens on. By default, this
  33. // value is 7000.
  34. BindPort int `json:"bind_port"`
  35. // BindUDPPort specifies the UDP port that the server listens on. If this
  36. // value is 0, the server will not listen for UDP connections. By default,
  37. // this value is 0
  38. BindUDPPort int `json:"bind_udp_port"`
  39. // KCPBindPort specifies the KCP port that the server listens on. If this
  40. // value is 0, the server will not listen for KCP connections. By default,
  41. // this value is 0.
  42. KCPBindPort int `json:"kcp_bind_port"`
  43. // ProxyBindAddr specifies the address that the proxy binds to. This value
  44. // may be the same as BindAddr. By default, this value is "0.0.0.0".
  45. ProxyBindAddr string `json:"proxy_bind_addr"`
  46. // VhostHTTPPort specifies the port that the server listens for HTTP Vhost
  47. // requests. If this value is 0, the server will not listen for HTTP
  48. // requests. By default, this value is 0.
  49. VhostHTTPPort int `json:"vhost_http_port"`
  50. // VhostHTTPSPort specifies the port that the server listens for HTTPS
  51. // Vhost requests. If this value is 0, the server will not listen for HTTPS
  52. // requests. By default, this value is 0.
  53. VhostHTTPSPort int `json:"vhost_https_port"`
  54. // TCPMuxHTTPConnectPort specifies the port that the server listens for TCP
  55. // HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
  56. // requests on one single port. If it's not - it will listen on this value for
  57. // HTTP CONNECT requests. By default, this value is 0.
  58. TCPMuxHTTPConnectPort int `json:"tcpmux_httpconnect_port"`
  59. // VhostHTTPTimeout specifies the response header timeout for the Vhost
  60. // HTTP server, in seconds. By default, this value is 60.
  61. VhostHTTPTimeout int64 `json:"vhost_http_timeout"`
  62. // DashboardAddr specifies the address that the dashboard binds to. By
  63. // default, this value is "0.0.0.0".
  64. DashboardAddr string `json:"dashboard_addr"`
  65. // DashboardPort specifies the port that the dashboard listens on. If this
  66. // value is 0, the dashboard will not be started. By default, this value is
  67. // 0.
  68. DashboardPort int `json:"dashboard_port"`
  69. // DashboardUser specifies the username that the dashboard will use for
  70. // login. By default, this value is "admin".
  71. DashboardUser string `json:"dashboard_user"`
  72. // DashboardUser specifies the password that the dashboard will use for
  73. // login. By default, this value is "admin".
  74. DashboardPwd string `json:"dashboard_pwd"`
  75. // EnablePrometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port}
  76. // in /metrics api.
  77. EnablePrometheus bool `json:"enable_prometheus"`
  78. // AssetsDir specifies the local directory that the dashboard will load
  79. // resources from. If this value is "", assets will be loaded from the
  80. // bundled executable using statik. By default, this value is "".
  81. AssetsDir string `json:"asserts_dir"`
  82. // LogFile specifies a file where logs will be written to. This value will
  83. // only be used if LogWay is set appropriately. By default, this value is
  84. // "console".
  85. LogFile string `json:"log_file"`
  86. // LogWay specifies the way logging is managed. Valid values are "console"
  87. // or "file". If "console" is used, logs will be printed to stdout. If
  88. // "file" is used, logs will be printed to LogFile. By default, this value
  89. // is "console".
  90. LogWay string `json:"log_way"`
  91. // LogLevel specifies the minimum log level. Valid values are "trace",
  92. // "debug", "info", "warn", and "error". By default, this value is "info".
  93. LogLevel string `json:"log_level"`
  94. // LogMaxDays specifies the maximum number of days to store log information
  95. // before deletion. This is only used if LogWay == "file". By default, this
  96. // value is 0.
  97. LogMaxDays int64 `json:"log_max_days"`
  98. // DisableLogColor disables log colors when LogWay == "console" when set to
  99. // true. By default, this value is false.
  100. DisableLogColor bool `json:"disable_log_color"`
  101. // DetailedErrorsToClient defines whether to send the specific error (with
  102. // debug info) to frpc. By default, this value is true.
  103. DetailedErrorsToClient bool `json:"detailed_errors_to_client"`
  104. // SubDomainHost specifies the domain that will be attached to sub-domains
  105. // requested by the client when using Vhost proxying. For example, if this
  106. // value is set to "frps.com" and the client requested the subdomain
  107. // "test", the resulting URL would be "test.frps.com". By default, this
  108. // value is "".
  109. SubDomainHost string `json:"subdomain_host"`
  110. // TCPMux toggles TCP stream multiplexing. This allows multiple requests
  111. // from a client to share a single TCP connection. By default, this value
  112. // is true.
  113. TCPMux bool `json:"tcp_mux"`
  114. // Custom404Page specifies a path to a custom 404 page to display. If this
  115. // value is "", a default page will be displayed. By default, this value is
  116. // "".
  117. Custom404Page string `json:"custom_404_page"`
  118. // AllowPorts specifies a set of ports that clients are able to proxy to.
  119. // If the length of this value is 0, all ports are allowed. By default,
  120. // this value is an empty set.
  121. AllowPorts map[int]struct{}
  122. // MaxPoolCount specifies the maximum pool size for each proxy. By default,
  123. // this value is 5.
  124. MaxPoolCount int64 `json:"max_pool_count"`
  125. // MaxPortsPerClient specifies the maximum number of ports a single client
  126. // may proxy to. If this value is 0, no limit will be applied. By default,
  127. // this value is 0.
  128. MaxPortsPerClient int64 `json:"max_ports_per_client"`
  129. // TLSOnly specifies whether to only accept TLS-encrypted connections.
  130. // By default, the value is false.
  131. TLSOnly bool `json:"tls_only"`
  132. // TLSCertFile specifies the path of the cert file that the server will
  133. // load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
  134. // supplied tls configuration. Otherwise, the server will use the tls
  135. // configuration generated by itself.
  136. TLSCertFile string `json:"tls_cert_file"`
  137. // TLSKeyFile specifies the path of the secret key that the server will
  138. // load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
  139. // supplied tls configuration. Otherwise, the server will use the tls
  140. // configuration generated by itself.
  141. TLSKeyFile string `json:"tls_key_file"`
  142. // TLSTrustedCaFile specifies the paths of the client cert files that the
  143. // server will load. It only works when "tls_only" is true. If
  144. // "tls_trusted_ca_file" is valid, the server will verify each client's
  145. // certificate.
  146. TLSTrustedCaFile string `json:"tls_trusted_ca_file"`
  147. // HeartBeatTimeout specifies the maximum time to wait for a heartbeat
  148. // before terminating the connection. It is not recommended to change this
  149. // value. By default, this value is 90.
  150. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  151. // UserConnTimeout specifies the maximum time to wait for a work
  152. // connection. By default, this value is 10.
  153. UserConnTimeout int64 `json:"user_conn_timeout"`
  154. // HTTPPlugins specify the server plugins support HTTP protocol.
  155. HTTPPlugins map[string]plugin.HTTPPluginOptions `json:"http_plugins"`
  156. // UDPPacketSize specifies the UDP packet size
  157. // By default, this value is 1500
  158. UDPPacketSize int64 `json:"udp_packet_size"`
  159. }
  160. // GetDefaultServerConf returns a server configuration with reasonable
  161. // defaults.
  162. func GetDefaultServerConf() ServerCommonConf {
  163. return ServerCommonConf{
  164. BindAddr: "0.0.0.0",
  165. BindPort: 7000,
  166. BindUDPPort: 0,
  167. KCPBindPort: 0,
  168. ProxyBindAddr: "0.0.0.0",
  169. VhostHTTPPort: 0,
  170. VhostHTTPSPort: 0,
  171. TCPMuxHTTPConnectPort: 0,
  172. VhostHTTPTimeout: 60,
  173. DashboardAddr: "0.0.0.0",
  174. DashboardPort: 0,
  175. DashboardUser: "admin",
  176. DashboardPwd: "admin",
  177. EnablePrometheus: false,
  178. AssetsDir: "",
  179. LogFile: "console",
  180. LogWay: "console",
  181. LogLevel: "info",
  182. LogMaxDays: 3,
  183. DisableLogColor: false,
  184. DetailedErrorsToClient: true,
  185. SubDomainHost: "",
  186. TCPMux: true,
  187. AllowPorts: make(map[int]struct{}),
  188. MaxPoolCount: 5,
  189. MaxPortsPerClient: 0,
  190. TLSOnly: false,
  191. TLSCertFile: "",
  192. TLSKeyFile: "",
  193. TLSTrustedCaFile: "",
  194. HeartBeatTimeout: 90,
  195. UserConnTimeout: 10,
  196. Custom404Page: "",
  197. HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
  198. UDPPacketSize: 1500,
  199. }
  200. }
  201. // UnmarshalServerConfFromIni parses the contents of a server configuration ini
  202. // file and returns the resulting server configuration.
  203. func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
  204. cfg = GetDefaultServerConf()
  205. conf, err := ini.Load(strings.NewReader(content))
  206. if err != nil {
  207. err = fmt.Errorf("parse ini conf file error: %v", err)
  208. return ServerCommonConf{}, err
  209. }
  210. UnmarshalPluginsFromIni(conf, &cfg)
  211. cfg.ServerConfig = auth.UnmarshalServerConfFromIni(conf)
  212. var (
  213. tmpStr string
  214. ok bool
  215. v int64
  216. )
  217. if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
  218. cfg.BindAddr = tmpStr
  219. }
  220. if tmpStr, ok = conf.Get("common", "bind_port"); ok {
  221. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  222. err = fmt.Errorf("Parse conf error: invalid bind_port")
  223. return
  224. }
  225. cfg.BindPort = int(v)
  226. }
  227. if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
  228. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  229. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  230. return
  231. }
  232. cfg.BindUDPPort = int(v)
  233. }
  234. if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
  235. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  236. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  237. return
  238. }
  239. cfg.KCPBindPort = int(v)
  240. }
  241. if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
  242. cfg.ProxyBindAddr = tmpStr
  243. } else {
  244. cfg.ProxyBindAddr = cfg.BindAddr
  245. }
  246. if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
  247. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  248. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  249. return
  250. }
  251. cfg.VhostHTTPPort = int(v)
  252. } else {
  253. cfg.VhostHTTPPort = 0
  254. }
  255. if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
  256. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  257. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  258. return
  259. }
  260. cfg.VhostHTTPSPort = int(v)
  261. } else {
  262. cfg.VhostHTTPSPort = 0
  263. }
  264. if tmpStr, ok = conf.Get("common", "tcpmux_httpconnect_port"); ok {
  265. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  266. err = fmt.Errorf("Parse conf error: invalid tcpmux_httpconnect_port")
  267. return
  268. }
  269. cfg.TCPMuxHTTPConnectPort = int(v)
  270. } else {
  271. cfg.TCPMuxHTTPConnectPort = 0
  272. }
  273. if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
  274. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  275. if errRet != nil || v < 0 {
  276. err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
  277. return
  278. }
  279. cfg.VhostHTTPTimeout = v
  280. }
  281. if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
  282. cfg.DashboardAddr = tmpStr
  283. } else {
  284. cfg.DashboardAddr = cfg.BindAddr
  285. }
  286. if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
  287. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  288. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  289. return
  290. }
  291. cfg.DashboardPort = int(v)
  292. } else {
  293. cfg.DashboardPort = 0
  294. }
  295. if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
  296. cfg.DashboardUser = tmpStr
  297. }
  298. if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
  299. cfg.DashboardPwd = tmpStr
  300. }
  301. if tmpStr, ok = conf.Get("common", "enable_prometheus"); ok && tmpStr == "true" {
  302. cfg.EnablePrometheus = true
  303. }
  304. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  305. cfg.AssetsDir = tmpStr
  306. }
  307. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  308. cfg.LogFile = tmpStr
  309. if cfg.LogFile == "console" {
  310. cfg.LogWay = "console"
  311. } else {
  312. cfg.LogWay = "file"
  313. }
  314. }
  315. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  316. cfg.LogLevel = tmpStr
  317. }
  318. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  319. v, err = strconv.ParseInt(tmpStr, 10, 64)
  320. if err == nil {
  321. cfg.LogMaxDays = v
  322. }
  323. }
  324. if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
  325. cfg.DisableLogColor = true
  326. }
  327. if tmpStr, ok = conf.Get("common", "detailed_errors_to_client"); ok && tmpStr == "false" {
  328. cfg.DetailedErrorsToClient = false
  329. } else {
  330. cfg.DetailedErrorsToClient = true
  331. }
  332. if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
  333. // e.g. 1000-2000,2001,2002,3000-4000
  334. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  335. if errRet != nil {
  336. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  337. return
  338. }
  339. for _, port := range ports {
  340. cfg.AllowPorts[int(port)] = struct{}{}
  341. }
  342. }
  343. if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
  344. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  345. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  346. return
  347. }
  348. if v < 0 {
  349. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  350. return
  351. }
  352. cfg.MaxPoolCount = v
  353. }
  354. if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
  355. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  356. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  357. return
  358. }
  359. if v < 0 {
  360. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  361. return
  362. }
  363. cfg.MaxPortsPerClient = v
  364. }
  365. if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
  366. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  367. }
  368. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  369. cfg.TCPMux = false
  370. } else {
  371. cfg.TCPMux = true
  372. }
  373. if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
  374. cfg.Custom404Page = tmpStr
  375. }
  376. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  377. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  378. if errRet != nil {
  379. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  380. return
  381. }
  382. cfg.HeartBeatTimeout = v
  383. }
  384. if tmpStr, ok = conf.Get("common", "tls_only"); ok && tmpStr == "true" {
  385. cfg.TLSOnly = true
  386. } else {
  387. cfg.TLSOnly = false
  388. }
  389. if tmpStr, ok = conf.Get("common", "udp_packet_size"); ok {
  390. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  391. err = fmt.Errorf("Parse conf error: invalid udp_packet_size")
  392. return
  393. }
  394. cfg.UDPPacketSize = v
  395. }
  396. if tmpStr, ok := conf.Get("common", "tls_cert_file"); ok {
  397. cfg.TLSCertFile = tmpStr
  398. }
  399. if tmpStr, ok := conf.Get("common", "tls_key_file"); ok {
  400. cfg.TLSKeyFile = tmpStr
  401. }
  402. if tmpStr, ok := conf.Get("common", "tls_trusted_ca_file"); ok {
  403. cfg.TLSTrustedCaFile = tmpStr
  404. cfg.TLSOnly = true
  405. }
  406. return
  407. }
  408. func UnmarshalPluginsFromIni(sections ini.File, cfg *ServerCommonConf) {
  409. for name, section := range sections {
  410. if strings.HasPrefix(name, "plugin.") {
  411. name = strings.TrimSpace(strings.TrimPrefix(name, "plugin."))
  412. options := plugin.HTTPPluginOptions{
  413. Name: name,
  414. Addr: section["addr"],
  415. Path: section["path"],
  416. Ops: strings.Split(section["ops"], ","),
  417. }
  418. for i := range options.Ops {
  419. options.Ops[i] = strings.TrimSpace(options.Ops[i])
  420. }
  421. cfg.HTTPPlugins[name] = options
  422. }
  423. }
  424. }
  425. func (cfg *ServerCommonConf) Check() error {
  426. return nil
  427. }