server_common.go 14 KB

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