1
0

server.go 9.5 KB

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