1
0

server.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright 2020 The frp Authors
  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. "strings"
  18. "github.com/go-playground/validator/v10"
  19. "gopkg.in/ini.v1"
  20. "github.com/fatedier/frp/pkg/auth"
  21. plugin "github.com/fatedier/frp/pkg/plugin/server"
  22. "github.com/fatedier/frp/pkg/util/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 `ini:",extends"`
  29. // BindAddr specifies the address that the server binds to. By default,
  30. // this value is "0.0.0.0".
  31. BindAddr string `ini:"bind_addr" json:"bind_addr"`
  32. // BindPort specifies the port that the server listens on. By default, this
  33. // value is 7000.
  34. BindPort int `ini:"bind_port" json:"bind_port" validate:"gte=0,lte=65535"`
  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 `ini:"bind_udp_port" json:"bind_udp_port" validate:"gte=0,lte=65535"`
  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 `ini:"kcp_bind_port" json:"kcp_bind_port" validate:"gte=0,lte=65535"`
  43. // QUICBindPort specifies the QUIC port that the server listens on.
  44. // Set this value to 0 will disable this feature.
  45. // By default, the value is 0.
  46. QUICBindPort int `ini:"quic_bind_port" json:"quic_bind_port" validate:"gte=0,lte=65535"`
  47. // QUIC protocol options
  48. QUICKeepalivePeriod int `ini:"quic_keepalive_period" json:"quic_keepalive_period" validate:"gte=0"`
  49. QUICMaxIdleTimeout int `ini:"quic_max_idle_timeout" json:"quic_max_idle_timeout" validate:"gte=0"`
  50. QUICMaxIncomingStreams int `ini:"quic_max_incoming_streams" json:"quic_max_incoming_streams" validate:"gte=0"`
  51. // ProxyBindAddr specifies the address that the proxy binds to. This value
  52. // may be the same as BindAddr.
  53. ProxyBindAddr string `ini:"proxy_bind_addr" json:"proxy_bind_addr"`
  54. // VhostHTTPPort specifies the port that the server listens for HTTP Vhost
  55. // requests. If this value is 0, the server will not listen for HTTP
  56. // requests. By default, this value is 0.
  57. VhostHTTPPort int `ini:"vhost_http_port" json:"vhost_http_port" validate:"gte=0,lte=65535"`
  58. // VhostHTTPSPort specifies the port that the server listens for HTTPS
  59. // Vhost requests. If this value is 0, the server will not listen for HTTPS
  60. // requests. By default, this value is 0.
  61. VhostHTTPSPort int `ini:"vhost_https_port" json:"vhost_https_port" validate:"gte=0,lte=65535"`
  62. // TCPMuxHTTPConnectPort specifies the port that the server listens for TCP
  63. // HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
  64. // requests on one single port. If it's not - it will listen on this value for
  65. // HTTP CONNECT requests. By default, this value is 0.
  66. TCPMuxHTTPConnectPort int `ini:"tcpmux_httpconnect_port" json:"tcpmux_httpconnect_port" validate:"gte=0,lte=65535"`
  67. // If TCPMuxPassthrough is true, frps won't do any update on traffic.
  68. TCPMuxPassthrough bool `ini:"tcpmux_passthrough" json:"tcpmux_passthrough"`
  69. // VhostHTTPTimeout specifies the response header timeout for the Vhost
  70. // HTTP server, in seconds. By default, this value is 60.
  71. VhostHTTPTimeout int64 `ini:"vhost_http_timeout" json:"vhost_http_timeout"`
  72. // DashboardAddr specifies the address that the dashboard binds to. By
  73. // default, this value is "0.0.0.0".
  74. DashboardAddr string `ini:"dashboard_addr" json:"dashboard_addr"`
  75. // DashboardPort specifies the port that the dashboard listens on. If this
  76. // value is 0, the dashboard will not be started. By default, this value is
  77. // 0.
  78. DashboardPort int `ini:"dashboard_port" json:"dashboard_port" validate:"gte=0,lte=65535"`
  79. // DashboardTLSCertFile specifies the path of the cert file that the server will
  80. // load. If "dashboard_tls_cert_file", "dashboard_tls_key_file" are valid, the server will use this
  81. // supplied tls configuration.
  82. DashboardTLSCertFile string `ini:"dashboard_tls_cert_file" json:"dashboard_tls_cert_file"`
  83. // DashboardTLSKeyFile specifies the path of the secret key that the server will
  84. // load. If "dashboard_tls_cert_file", "dashboard_tls_key_file" are valid, the server will use this
  85. // supplied tls configuration.
  86. DashboardTLSKeyFile string `ini:"dashboard_tls_key_file" json:"dashboard_tls_key_file"`
  87. // DashboardTLSMode specifies the mode of the dashboard between HTTP or HTTPS modes. By
  88. // default, this value is false, which is HTTP mode.
  89. DashboardTLSMode bool `ini:"dashboard_tls_mode" json:"dashboard_tls_mode"`
  90. // DashboardUser specifies the username that the dashboard will use for
  91. // login.
  92. DashboardUser string `ini:"dashboard_user" json:"dashboard_user"`
  93. // DashboardPwd specifies the password that the dashboard will use for
  94. // login.
  95. DashboardPwd string `ini:"dashboard_pwd" json:"dashboard_pwd"`
  96. // EnablePrometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port}
  97. // in /metrics api.
  98. EnablePrometheus bool `ini:"enable_prometheus" json:"enable_prometheus"`
  99. // AssetsDir specifies the local directory that the dashboard will load
  100. // resources from. If this value is "", assets will be loaded from the
  101. // bundled executable using statik. By default, this value is "".
  102. AssetsDir string `ini:"assets_dir" json:"assets_dir"`
  103. // LogFile specifies a file where logs will be written to. This value will
  104. // only be used if LogWay is set appropriately. By default, this value is
  105. // "console".
  106. LogFile string `ini:"log_file" json:"log_file"`
  107. // LogWay specifies the way logging is managed. Valid values are "console"
  108. // or "file". If "console" is used, logs will be printed to stdout. If
  109. // "file" is used, logs will be printed to LogFile. By default, this value
  110. // is "console".
  111. LogWay string `ini:"log_way" json:"log_way"`
  112. // LogLevel specifies the minimum log level. Valid values are "trace",
  113. // "debug", "info", "warn", and "error". By default, this value is "info".
  114. LogLevel string `ini:"log_level" json:"log_level"`
  115. // LogMaxDays specifies the maximum number of days to store log information
  116. // before deletion. This is only used if LogWay == "file". By default, this
  117. // value is 0.
  118. LogMaxDays int64 `ini:"log_max_days" json:"log_max_days"`
  119. // DisableLogColor disables log colors when LogWay == "console" when set to
  120. // true. By default, this value is false.
  121. DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"`
  122. // DetailedErrorsToClient defines whether to send the specific error (with
  123. // debug info) to frpc. By default, this value is true.
  124. DetailedErrorsToClient bool `ini:"detailed_errors_to_client" json:"detailed_errors_to_client"`
  125. // SubDomainHost specifies the domain that will be attached to sub-domains
  126. // requested by the client when using Vhost proxying. For example, if this
  127. // value is set to "frps.com" and the client requested the subdomain
  128. // "test", the resulting URL would be "test.frps.com". By default, this
  129. // value is "".
  130. SubDomainHost string `ini:"subdomain_host" json:"subdomain_host"`
  131. // TCPMux toggles TCP stream multiplexing. This allows multiple requests
  132. // from a client to share a single TCP connection. By default, this value
  133. // is true.
  134. TCPMux bool `ini:"tcp_mux" json:"tcp_mux"`
  135. // TCPMuxKeepaliveInterval specifies the keep alive interval for TCP stream multipler.
  136. // If TCPMux is true, heartbeat of application layer is unnecessary because it can only rely on heartbeat in TCPMux.
  137. TCPMuxKeepaliveInterval int64 `ini:"tcp_mux_keepalive_interval" json:"tcp_mux_keepalive_interval"`
  138. // TCPKeepAlive specifies the interval between keep-alive probes for an active network connection between frpc and frps.
  139. // If negative, keep-alive probes are disabled.
  140. TCPKeepAlive int64 `ini:"tcp_keepalive" json:"tcp_keepalive"`
  141. // Custom404Page specifies a path to a custom 404 page to display. If this
  142. // value is "", a default page will be displayed. By default, this value is
  143. // "".
  144. Custom404Page string `ini:"custom_404_page" json:"custom_404_page"`
  145. // AllowPorts specifies a set of ports that clients are able to proxy to.
  146. // If the length of this value is 0, all ports are allowed. By default,
  147. // this value is an empty set.
  148. AllowPorts map[int]struct{} `ini:"-" json:"-"`
  149. // Original string.
  150. AllowPortsStr string `ini:"-" json:"-"`
  151. // MaxPoolCount specifies the maximum pool size for each proxy. By default,
  152. // this value is 5.
  153. MaxPoolCount int64 `ini:"max_pool_count" json:"max_pool_count"`
  154. // MaxPortsPerClient specifies the maximum number of ports a single client
  155. // may proxy to. If this value is 0, no limit will be applied. By default,
  156. // this value is 0.
  157. MaxPortsPerClient int64 `ini:"max_ports_per_client" json:"max_ports_per_client"`
  158. // TLSOnly specifies whether to only accept TLS-encrypted connections.
  159. // By default, the value is false.
  160. TLSOnly bool `ini:"tls_only" json:"tls_only"`
  161. // TLSCertFile specifies the path of the cert file that the server will
  162. // load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
  163. // supplied tls configuration. Otherwise, the server will use the tls
  164. // configuration generated by itself.
  165. TLSCertFile string `ini:"tls_cert_file" json:"tls_cert_file"`
  166. // TLSKeyFile specifies the path of the secret key that the server will
  167. // load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
  168. // supplied tls configuration. Otherwise, the server will use the tls
  169. // configuration generated by itself.
  170. TLSKeyFile string `ini:"tls_key_file" json:"tls_key_file"`
  171. // TLSTrustedCaFile specifies the paths of the client cert files that the
  172. // server will load. It only works when "tls_only" is true. If
  173. // "tls_trusted_ca_file" is valid, the server will verify each client's
  174. // certificate.
  175. TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"`
  176. // HeartBeatTimeout specifies the maximum time to wait for a heartbeat
  177. // before terminating the connection. It is not recommended to change this
  178. // value. By default, this value is 90. Set negative value to disable it.
  179. HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"`
  180. // UserConnTimeout specifies the maximum time to wait for a work
  181. // connection. By default, this value is 10.
  182. UserConnTimeout int64 `ini:"user_conn_timeout" json:"user_conn_timeout"`
  183. // HTTPPlugins specify the server plugins support HTTP protocol.
  184. HTTPPlugins map[string]plugin.HTTPPluginOptions `ini:"-" json:"http_plugins"`
  185. // UDPPacketSize specifies the UDP packet size
  186. // By default, this value is 1500
  187. UDPPacketSize int64 `ini:"udp_packet_size" json:"udp_packet_size"`
  188. // Enable golang pprof handlers in dashboard listener.
  189. // Dashboard port must be set first.
  190. PprofEnable bool `ini:"pprof_enable" json:"pprof_enable"`
  191. // NatHoleAnalysisDataReserveHours specifies the hours to reserve nat hole analysis data.
  192. NatHoleAnalysisDataReserveHours int64 `ini:"nat_hole_analysis_data_reserve_hours" json:"nat_hole_analysis_data_reserve_hours"`
  193. }
  194. // GetDefaultServerConf returns a server configuration with reasonable
  195. // defaults.
  196. func GetDefaultServerConf() ServerCommonConf {
  197. return ServerCommonConf{
  198. ServerConfig: auth.GetDefaultServerConf(),
  199. BindAddr: "0.0.0.0",
  200. BindPort: 7000,
  201. QUICKeepalivePeriod: 10,
  202. QUICMaxIdleTimeout: 30,
  203. QUICMaxIncomingStreams: 100000,
  204. VhostHTTPTimeout: 60,
  205. DashboardAddr: "0.0.0.0",
  206. LogFile: "console",
  207. LogWay: "console",
  208. LogLevel: "info",
  209. LogMaxDays: 3,
  210. DetailedErrorsToClient: true,
  211. TCPMux: true,
  212. TCPMuxKeepaliveInterval: 60,
  213. TCPKeepAlive: 7200,
  214. AllowPorts: make(map[int]struct{}),
  215. MaxPoolCount: 5,
  216. MaxPortsPerClient: 0,
  217. HeartbeatTimeout: 90,
  218. UserConnTimeout: 10,
  219. HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
  220. UDPPacketSize: 1500,
  221. NatHoleAnalysisDataReserveHours: 7 * 24,
  222. }
  223. }
  224. func UnmarshalServerConfFromIni(source interface{}) (ServerCommonConf, error) {
  225. f, err := ini.LoadSources(ini.LoadOptions{
  226. Insensitive: false,
  227. InsensitiveSections: false,
  228. InsensitiveKeys: false,
  229. IgnoreInlineComment: true,
  230. AllowBooleanKeys: true,
  231. }, source)
  232. if err != nil {
  233. return ServerCommonConf{}, err
  234. }
  235. s, err := f.GetSection("common")
  236. if err != nil {
  237. return ServerCommonConf{}, err
  238. }
  239. common := GetDefaultServerConf()
  240. err = s.MapTo(&common)
  241. if err != nil {
  242. return ServerCommonConf{}, err
  243. }
  244. // allow_ports
  245. allowPortStr := s.Key("allow_ports").String()
  246. if allowPortStr != "" {
  247. allowPorts, err := util.ParseRangeNumbers(allowPortStr)
  248. if err != nil {
  249. return ServerCommonConf{}, fmt.Errorf("invalid allow_ports: %v", err)
  250. }
  251. for _, port := range allowPorts {
  252. common.AllowPorts[int(port)] = struct{}{}
  253. }
  254. common.AllowPortsStr = allowPortStr
  255. }
  256. // plugin.xxx
  257. pluginOpts := make(map[string]plugin.HTTPPluginOptions)
  258. for _, section := range f.Sections() {
  259. name := section.Name()
  260. if !strings.HasPrefix(name, "plugin.") {
  261. continue
  262. }
  263. opt, err := loadHTTPPluginOpt(section)
  264. if err != nil {
  265. return ServerCommonConf{}, err
  266. }
  267. pluginOpts[opt.Name] = *opt
  268. }
  269. common.HTTPPlugins = pluginOpts
  270. return common, nil
  271. }
  272. func (cfg *ServerCommonConf) Complete() {
  273. if cfg.LogFile == "console" {
  274. cfg.LogWay = "console"
  275. } else {
  276. cfg.LogWay = "file"
  277. }
  278. if cfg.ProxyBindAddr == "" {
  279. cfg.ProxyBindAddr = cfg.BindAddr
  280. }
  281. if cfg.TLSTrustedCaFile != "" {
  282. cfg.TLSOnly = true
  283. }
  284. }
  285. func (cfg *ServerCommonConf) Validate() error {
  286. if !cfg.DashboardTLSMode {
  287. if cfg.DashboardTLSCertFile != "" {
  288. fmt.Println("WARNING! dashboard_tls_cert_file is invalid when dashboard_tls_mode is false")
  289. }
  290. if cfg.DashboardTLSKeyFile != "" {
  291. fmt.Println("WARNING! dashboard_tls_key_file is invalid when dashboard_tls_mode is false")
  292. }
  293. } else {
  294. if cfg.DashboardTLSCertFile == "" {
  295. return fmt.Errorf("ERROR! dashboard_tls_cert_file must be specified when dashboard_tls_mode is true")
  296. }
  297. if cfg.DashboardTLSKeyFile == "" {
  298. return fmt.Errorf("ERROR! dashboard_tls_cert_file must be specified when dashboard_tls_mode is true")
  299. }
  300. }
  301. return validator.New().Struct(cfg)
  302. }
  303. func loadHTTPPluginOpt(section *ini.Section) (*plugin.HTTPPluginOptions, error) {
  304. name := strings.TrimSpace(strings.TrimPrefix(section.Name(), "plugin."))
  305. opt := new(plugin.HTTPPluginOptions)
  306. err := section.MapTo(opt)
  307. if err != nil {
  308. return nil, err
  309. }
  310. opt.Name = name
  311. return opt, nil
  312. }