server.go 8.6 KB

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