server_common.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.AuthServerConfig
  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. // BindKcpPort 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. // AssetsDir specifies the local directory that the dashboard will load
  76. // resources from. If this value is "", assets will be loaded from the
  77. // bundled executable using statik. By default, this value is "".
  78. AssetsDir string `json:"asserts_dir"`
  79. // LogFile specifies a file where logs will be written to. This value will
  80. // only be used if LogWay is set appropriately. By default, this value is
  81. // "console".
  82. LogFile string `json:"log_file"`
  83. // LogWay specifies the way logging is managed. Valid values are "console"
  84. // or "file". If "console" is used, logs will be printed to stdout. If
  85. // "file" is used, logs will be printed to LogFile. By default, this value
  86. // is "console".
  87. LogWay string `json:"log_way"`
  88. // LogLevel specifies the minimum log level. Valid values are "trace",
  89. // "debug", "info", "warn", and "error". By default, this value is "info".
  90. LogLevel string `json:"log_level"`
  91. // LogMaxDays specifies the maximum number of days to store log information
  92. // before deletion. This is only used if LogWay == "file". By default, this
  93. // value is 0.
  94. LogMaxDays int64 `json:"log_max_days"`
  95. // DisableLogColor disables log colors when LogWay == "console" when set to
  96. // true. By default, this value is false.
  97. DisableLogColor bool `json:"disable_log_color"`
  98. // DetailedErrorsToClient defines whether to send the specific error (with
  99. // debug info) to frpc. By default, this value is true.
  100. DetailedErrorsToClient bool `json:"detailed_errors_to_client"`
  101. // SubDomainHost specifies the domain that will be attached to sub-domains
  102. // requested by the client when using Vhost proxying. For example, if this
  103. // value is set to "frps.com" and the client requested the subdomain
  104. // "test", the resulting URL would be "test.frps.com". By default, this
  105. // value is "".
  106. SubDomainHost string `json:"subdomain_host"`
  107. // TcpMux toggles TCP stream multiplexing. This allows multiple requests
  108. // from a client to share a single TCP connection. By default, this value
  109. // is true.
  110. TcpMux bool `json:"tcp_mux"`
  111. // Custom404Page specifies a path to a custom 404 page to display. If this
  112. // value is "", a default page will be displayed. By default, this value is
  113. // "".
  114. Custom404Page string `json:"custom_404_page"`
  115. // AllowPorts specifies a set of ports that clients are able to proxy to.
  116. // If the length of this value is 0, all ports are allowed. By default,
  117. // this value is an empty set.
  118. AllowPorts map[int]struct{}
  119. // MaxPoolCount specifies the maximum pool size for each proxy. By default,
  120. // this value is 5.
  121. MaxPoolCount int64 `json:"max_pool_count"`
  122. // MaxPortsPerClient specifies the maximum number of ports a single client
  123. // may proxy to. If this value is 0, no limit will be applied. By default,
  124. // this value is 0.
  125. MaxPortsPerClient int64 `json:"max_ports_per_client"`
  126. // TlsOnly specifies whether to only accept TLS-encrypted connections. By
  127. // default, the value is false.
  128. TlsOnly bool `json:"tls_only"`
  129. // HeartBeatTimeout specifies the maximum time to wait for a heartbeat
  130. // before terminating the connection. It is not recommended to change this
  131. // value. By default, this value is 90.
  132. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  133. // UserConnTimeout specifies the maximum time to wait for a work
  134. // connection. By default, this value is 10.
  135. UserConnTimeout int64 `json:"user_conn_timeout"`
  136. // HTTPPlugins specify the server plugins support HTTP protocol.
  137. HTTPPlugins map[string]plugin.HTTPPluginOptions `json:"http_plugins"`
  138. }
  139. // GetDefaultServerConf returns a server configuration with reasonable
  140. // defaults.
  141. func GetDefaultServerConf() ServerCommonConf {
  142. return ServerCommonConf{
  143. BindAddr: "0.0.0.0",
  144. BindPort: 7000,
  145. BindUdpPort: 0,
  146. KcpBindPort: 0,
  147. ProxyBindAddr: "0.0.0.0",
  148. VhostHttpPort: 0,
  149. VhostHttpsPort: 0,
  150. TcpMuxHttpConnectPort: 0,
  151. VhostHttpTimeout: 60,
  152. DashboardAddr: "0.0.0.0",
  153. DashboardPort: 0,
  154. DashboardUser: "admin",
  155. DashboardPwd: "admin",
  156. AssetsDir: "",
  157. LogFile: "console",
  158. LogWay: "console",
  159. LogLevel: "info",
  160. LogMaxDays: 3,
  161. DisableLogColor: false,
  162. DetailedErrorsToClient: true,
  163. SubDomainHost: "",
  164. TcpMux: true,
  165. AllowPorts: make(map[int]struct{}),
  166. MaxPoolCount: 5,
  167. MaxPortsPerClient: 0,
  168. TlsOnly: false,
  169. HeartBeatTimeout: 90,
  170. UserConnTimeout: 10,
  171. Custom404Page: "",
  172. HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
  173. }
  174. }
  175. // UnmarshalServerConfFromIni parses the contents of a server configuration ini
  176. // file and returns the resulting server configuration.
  177. func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
  178. cfg = GetDefaultServerConf()
  179. conf, err := ini.Load(strings.NewReader(content))
  180. if err != nil {
  181. err = fmt.Errorf("parse ini conf file error: %v", err)
  182. return ServerCommonConf{}, err
  183. }
  184. UnmarshalPluginsFromIni(conf, &cfg)
  185. cfg.AuthServerConfig = auth.UnmarshalAuthServerConfFromIni(conf)
  186. var (
  187. tmpStr string
  188. ok bool
  189. v int64
  190. )
  191. if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
  192. cfg.BindAddr = tmpStr
  193. }
  194. if tmpStr, ok = conf.Get("common", "bind_port"); ok {
  195. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  196. err = fmt.Errorf("Parse conf error: invalid bind_port")
  197. return
  198. } else {
  199. cfg.BindPort = int(v)
  200. }
  201. }
  202. if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
  203. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  204. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  205. return
  206. } else {
  207. cfg.BindUdpPort = int(v)
  208. }
  209. }
  210. if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
  211. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  212. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  213. return
  214. } else {
  215. cfg.KcpBindPort = int(v)
  216. }
  217. }
  218. if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
  219. cfg.ProxyBindAddr = tmpStr
  220. } else {
  221. cfg.ProxyBindAddr = cfg.BindAddr
  222. }
  223. if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
  224. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  225. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  226. return
  227. } else {
  228. cfg.VhostHttpPort = int(v)
  229. }
  230. } else {
  231. cfg.VhostHttpPort = 0
  232. }
  233. if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
  234. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  235. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  236. return
  237. } else {
  238. cfg.VhostHttpsPort = int(v)
  239. }
  240. } else {
  241. cfg.VhostHttpsPort = 0
  242. }
  243. if tmpStr, ok = conf.Get("common", "tcpmux_httpconnect_port"); ok {
  244. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  245. err = fmt.Errorf("Parse conf error: invalid tcpmux_httpconnect_port")
  246. return
  247. } else {
  248. cfg.TcpMuxHttpConnectPort = int(v)
  249. }
  250. } else {
  251. cfg.TcpMuxHttpConnectPort = 0
  252. }
  253. if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
  254. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  255. if errRet != nil || v < 0 {
  256. err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
  257. return
  258. } else {
  259. cfg.VhostHttpTimeout = v
  260. }
  261. }
  262. if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
  263. cfg.DashboardAddr = tmpStr
  264. } else {
  265. cfg.DashboardAddr = cfg.BindAddr
  266. }
  267. if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
  268. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  269. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  270. return
  271. } else {
  272. cfg.DashboardPort = int(v)
  273. }
  274. } else {
  275. cfg.DashboardPort = 0
  276. }
  277. if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
  278. cfg.DashboardUser = tmpStr
  279. }
  280. if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
  281. cfg.DashboardPwd = tmpStr
  282. }
  283. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  284. cfg.AssetsDir = tmpStr
  285. }
  286. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  287. cfg.LogFile = tmpStr
  288. if cfg.LogFile == "console" {
  289. cfg.LogWay = "console"
  290. } else {
  291. cfg.LogWay = "file"
  292. }
  293. }
  294. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  295. cfg.LogLevel = tmpStr
  296. }
  297. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  298. v, err = strconv.ParseInt(tmpStr, 10, 64)
  299. if err == nil {
  300. cfg.LogMaxDays = v
  301. }
  302. }
  303. if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
  304. cfg.DisableLogColor = true
  305. }
  306. if tmpStr, ok = conf.Get("common", "detailed_errors_to_client"); ok && tmpStr == "false" {
  307. cfg.DetailedErrorsToClient = false
  308. } else {
  309. cfg.DetailedErrorsToClient = true
  310. }
  311. if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
  312. // e.g. 1000-2000,2001,2002,3000-4000
  313. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  314. if errRet != nil {
  315. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  316. return
  317. }
  318. for _, port := range ports {
  319. cfg.AllowPorts[int(port)] = struct{}{}
  320. }
  321. }
  322. if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
  323. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  324. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  325. return
  326. } else {
  327. if v < 0 {
  328. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  329. return
  330. }
  331. cfg.MaxPoolCount = v
  332. }
  333. }
  334. if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
  335. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  336. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  337. return
  338. } else {
  339. if v < 0 {
  340. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  341. return
  342. }
  343. cfg.MaxPortsPerClient = v
  344. }
  345. }
  346. if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
  347. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  348. }
  349. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  350. cfg.TcpMux = false
  351. } else {
  352. cfg.TcpMux = true
  353. }
  354. if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
  355. cfg.Custom404Page = tmpStr
  356. }
  357. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  358. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  359. if errRet != nil {
  360. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  361. return
  362. } else {
  363. cfg.HeartBeatTimeout = v
  364. }
  365. }
  366. if tmpStr, ok = conf.Get("common", "tls_only"); ok && tmpStr == "true" {
  367. cfg.TlsOnly = true
  368. } else {
  369. cfg.TlsOnly = false
  370. }
  371. return
  372. }
  373. func UnmarshalPluginsFromIni(sections ini.File, cfg *ServerCommonConf) {
  374. for name, section := range sections {
  375. if strings.HasPrefix(name, "plugin.") {
  376. name = strings.TrimSpace(strings.TrimPrefix(name, "plugin."))
  377. options := plugin.HTTPPluginOptions{
  378. Name: name,
  379. Addr: section["addr"],
  380. Path: section["path"],
  381. Ops: strings.Split(section["ops"], ","),
  382. }
  383. for i, _ := range options.Ops {
  384. options.Ops[i] = strings.TrimSpace(options.Ops[i])
  385. }
  386. cfg.HTTPPlugins[name] = options
  387. }
  388. }
  389. }
  390. func (cfg *ServerCommonConf) Check() (err error) {
  391. return
  392. }