server.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2023 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 v1
  15. import (
  16. "github.com/samber/lo"
  17. "github.com/fatedier/frp/pkg/config/types"
  18. "github.com/fatedier/frp/pkg/util/util"
  19. )
  20. type ServerConfig struct {
  21. APIMetadata
  22. Auth AuthServerConfig `json:"auth,omitempty"`
  23. // BindAddr specifies the address that the server binds to. By default,
  24. // this value is "0.0.0.0".
  25. BindAddr string `json:"bindAddr,omitempty"`
  26. // BindPort specifies the port that the server listens on. By default, this
  27. // value is 7000.
  28. BindPort int `json:"bindPort,omitempty"`
  29. // KCPBindPort specifies the KCP port that the server listens on. If this
  30. // value is 0, the server will not listen for KCP connections.
  31. KCPBindPort int `json:"kcpBindPort,omitempty"`
  32. // QUICBindPort specifies the QUIC port that the server listens on.
  33. // Set this value to 0 will disable this feature.
  34. QUICBindPort int `json:"quicBindPort,omitempty"`
  35. // ProxyBindAddr specifies the address that the proxy binds to. This value
  36. // may be the same as BindAddr.
  37. ProxyBindAddr string `json:"proxyBindAddr,omitempty"`
  38. // VhostHTTPPort specifies the port that the server listens for HTTP Vhost
  39. // requests. If this value is 0, the server will not listen for HTTP
  40. // requests.
  41. VhostHTTPPort int `json:"vhostHTTPPort,omitempty"`
  42. // VhostHTTPTimeout specifies the response header timeout for the Vhost
  43. // HTTP server, in seconds. By default, this value is 60.
  44. VhostHTTPTimeout int64 `json:"vhostHTTPTimeout,omitempty"`
  45. // VhostHTTPSPort specifies the port that the server listens for HTTPS
  46. // Vhost requests. If this value is 0, the server will not listen for HTTPS
  47. // requests.
  48. VhostHTTPSPort int `json:"vhostHTTPSPort,omitempty"`
  49. // TCPMuxHTTPConnectPort specifies the port that the server listens for TCP
  50. // HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
  51. // requests on one single port. If it's not - it will listen on this value for
  52. // HTTP CONNECT requests.
  53. TCPMuxHTTPConnectPort int `json:"tcpmuxHTTPConnectPort,omitempty"`
  54. // If TCPMuxPassthrough is true, frps won't do any update on traffic.
  55. TCPMuxPassthrough bool `json:"tcpmuxPassthrough,omitempty"`
  56. // SubDomainHost specifies the domain that will be attached to sub-domains
  57. // requested by the client when using Vhost proxying. For example, if this
  58. // value is set to "frps.com" and the client requested the subdomain
  59. // "test", the resulting URL would be "test.frps.com".
  60. SubDomainHost string `json:"subDomainHost,omitempty"`
  61. // Custom404Page specifies a path to a custom 404 page to display. If this
  62. // value is "", a default page will be displayed.
  63. Custom404Page string `json:"custom404Page,omitempty"`
  64. SSHTunnelGateway SSHTunnelGateway `json:"sshTunnelGateway,omitempty"`
  65. WebServer WebServerConfig `json:"webServer,omitempty"`
  66. // EnablePrometheus will export prometheus metrics on webserver address
  67. // in /metrics api.
  68. EnablePrometheus bool `json:"enablePrometheus,omitempty"`
  69. Log LogConfig `json:"log,omitempty"`
  70. Transport ServerTransportConfig `json:"transport,omitempty"`
  71. // DetailedErrorsToClient defines whether to send the specific error (with
  72. // debug info) to frpc. By default, this value is true.
  73. DetailedErrorsToClient *bool `json:"detailedErrorsToClient,omitempty"`
  74. // MaxPortsPerClient specifies the maximum number of ports a single client
  75. // may proxy to. If this value is 0, no limit will be applied.
  76. MaxPortsPerClient int64 `json:"maxPortsPerClient,omitempty"`
  77. // UserConnTimeout specifies the maximum time to wait for a work
  78. // connection. By default, this value is 10.
  79. UserConnTimeout int64 `json:"userConnTimeout,omitempty"`
  80. // UDPPacketSize specifies the UDP packet size
  81. // By default, this value is 1500
  82. UDPPacketSize int64 `json:"udpPacketSize,omitempty"`
  83. // NatHoleAnalysisDataReserveHours specifies the hours to reserve nat hole analysis data.
  84. NatHoleAnalysisDataReserveHours int64 `json:"natholeAnalysisDataReserveHours,omitempty"`
  85. AllowPorts []types.PortsRange `json:"allowPorts,omitempty"`
  86. HTTPPlugins []HTTPPluginOptions `json:"httpPlugins,omitempty"`
  87. }
  88. func (c *ServerConfig) Complete() {
  89. c.Auth.Complete()
  90. c.Log.Complete()
  91. c.Transport.Complete()
  92. c.WebServer.Complete()
  93. c.SSHTunnelGateway.Complete()
  94. c.BindAddr = util.EmptyOr(c.BindAddr, "0.0.0.0")
  95. c.BindPort = util.EmptyOr(c.BindPort, 7000)
  96. if c.ProxyBindAddr == "" {
  97. c.ProxyBindAddr = c.BindAddr
  98. }
  99. if c.WebServer.Port > 0 {
  100. c.WebServer.Addr = util.EmptyOr(c.WebServer.Addr, "0.0.0.0")
  101. }
  102. c.VhostHTTPTimeout = util.EmptyOr(c.VhostHTTPTimeout, 60)
  103. c.DetailedErrorsToClient = util.EmptyOr(c.DetailedErrorsToClient, lo.ToPtr(true))
  104. c.UserConnTimeout = util.EmptyOr(c.UserConnTimeout, 10)
  105. c.UDPPacketSize = util.EmptyOr(c.UDPPacketSize, 1500)
  106. c.NatHoleAnalysisDataReserveHours = util.EmptyOr(c.NatHoleAnalysisDataReserveHours, 7*24)
  107. }
  108. type AuthServerConfig struct {
  109. Method AuthMethod `json:"method,omitempty"`
  110. AdditionalScopes []AuthScope `json:"additionalScopes,omitempty"`
  111. Token string `json:"token,omitempty"`
  112. OIDC AuthOIDCServerConfig `json:"oidc,omitempty"`
  113. }
  114. func (c *AuthServerConfig) Complete() {
  115. c.Method = util.EmptyOr(c.Method, "token")
  116. }
  117. type AuthOIDCServerConfig struct {
  118. // Issuer specifies the issuer to verify OIDC tokens with. This issuer
  119. // will be used to load public keys to verify signature and will be compared
  120. // with the issuer claim in the OIDC token.
  121. Issuer string `json:"issuer,omitempty"`
  122. // Audience specifies the audience OIDC tokens should contain when validated.
  123. // If this value is empty, audience ("client ID") verification will be skipped.
  124. Audience string `json:"audience,omitempty"`
  125. // SkipExpiryCheck specifies whether to skip checking if the OIDC token is
  126. // expired.
  127. SkipExpiryCheck bool `json:"skipExpiryCheck,omitempty"`
  128. // SkipIssuerCheck specifies whether to skip checking if the OIDC token's
  129. // issuer claim matches the issuer specified in OidcIssuer.
  130. SkipIssuerCheck bool `json:"skipIssuerCheck,omitempty"`
  131. }
  132. type ServerTransportConfig struct {
  133. // TCPMux toggles TCP stream multiplexing. This allows multiple requests
  134. // from a client to share a single TCP connection. By default, this value
  135. // is true.
  136. // $HideFromDoc
  137. TCPMux *bool `json:"tcpMux,omitempty"`
  138. // TCPMuxKeepaliveInterval specifies the keep alive interval for TCP stream multiplier.
  139. // If TCPMux is true, heartbeat of application layer is unnecessary because it can only rely on heartbeat in TCPMux.
  140. TCPMuxKeepaliveInterval int64 `json:"tcpMuxKeepaliveInterval,omitempty"`
  141. // TCPKeepAlive specifies the interval between keep-alive probes for an active network connection between frpc and frps.
  142. // If negative, keep-alive probes are disabled.
  143. TCPKeepAlive int64 `json:"tcpKeepalive,omitempty"`
  144. // MaxPoolCount specifies the maximum pool size for each proxy. By default,
  145. // this value is 5.
  146. MaxPoolCount int64 `json:"maxPoolCount,omitempty"`
  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. Set negative value to disable it.
  150. HeartbeatTimeout int64 `json:"heartbeatTimeout,omitempty"`
  151. // QUIC options.
  152. QUIC QUICOptions `json:"quic,omitempty"`
  153. // TLS specifies TLS settings for the connection from the client.
  154. TLS TLSServerConfig `json:"tls,omitempty"`
  155. }
  156. func (c *ServerTransportConfig) Complete() {
  157. c.TCPMux = util.EmptyOr(c.TCPMux, lo.ToPtr(true))
  158. c.TCPMuxKeepaliveInterval = util.EmptyOr(c.TCPMuxKeepaliveInterval, 30)
  159. c.TCPKeepAlive = util.EmptyOr(c.TCPKeepAlive, 7200)
  160. c.MaxPoolCount = util.EmptyOr(c.MaxPoolCount, 5)
  161. if lo.FromPtr(c.TCPMux) {
  162. // If TCPMux is enabled, heartbeat of application layer is unnecessary because we can rely on heartbeat in tcpmux.
  163. c.HeartbeatTimeout = util.EmptyOr(c.HeartbeatTimeout, -1)
  164. } else {
  165. c.HeartbeatTimeout = util.EmptyOr(c.HeartbeatTimeout, 90)
  166. }
  167. c.QUIC.Complete()
  168. if c.TLS.TrustedCaFile != "" {
  169. c.TLS.Force = true
  170. }
  171. }
  172. type TLSServerConfig struct {
  173. // Force specifies whether to only accept TLS-encrypted connections.
  174. Force bool `json:"force,omitempty"`
  175. TLSConfig
  176. }
  177. type SSHTunnelGateway struct {
  178. BindPort int `json:"bindPort,omitempty"`
  179. PrivateKeyFile string `json:"privateKeyFile,omitempty"`
  180. AutoGenPrivateKeyPath string `json:"autoGenPrivateKeyPath,omitempty"`
  181. AuthorizedKeysFile string `json:"authorizedKeysFile,omitempty"`
  182. }
  183. func (c *SSHTunnelGateway) Complete() {
  184. c.AutoGenPrivateKeyPath = util.EmptyOr(c.AutoGenPrivateKeyPath, "./.autogen_ssh_key")
  185. }