1
0

server.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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" validate:"gte=0,lte=65535"`
  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" validate:"gte=0,lte=65535"`
  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" validate:"gte=0,lte=65535"`
  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" validate:"gte=0,lte=65535"`
  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" validate:"gte=0,lte=65535"`
  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" validate:"gte=0,lte=65535"`
  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. WebServer WebServerConfig `json:"webServer,omitempty"`
  65. // EnablePrometheus will export prometheus metrics on webserver address
  66. // in /metrics api.
  67. EnablePrometheus bool `json:"enablePrometheus,omitempty"`
  68. Log LogConfig `json:"log,omitempty"`
  69. Transport ServerTransportConfig `json:"transport,omitempty"`
  70. TLS TLSServerConfig `json:"tls,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.BindAddr = util.EmptyOr(c.BindAddr, "0.0.0.0")
  94. c.BindPort = util.EmptyOr(c.BindPort, 7000)
  95. if c.ProxyBindAddr == "" {
  96. c.ProxyBindAddr = c.BindAddr
  97. }
  98. if c.TLS.TrustedCaFile != "" {
  99. c.TLS.Force = true
  100. }
  101. if c.WebServer.Port > 0 {
  102. c.WebServer.Addr = util.EmptyOr(c.WebServer.Addr, "0.0.0.0")
  103. }
  104. c.VhostHTTPTimeout = util.EmptyOr(c.VhostHTTPTimeout, 60)
  105. c.DetailedErrorsToClient = util.EmptyOr(c.DetailedErrorsToClient, lo.ToPtr(true))
  106. c.UserConnTimeout = util.EmptyOr(c.UserConnTimeout, 10)
  107. c.UDPPacketSize = util.EmptyOr(c.UDPPacketSize, 1500)
  108. c.NatHoleAnalysisDataReserveHours = util.EmptyOr(c.NatHoleAnalysisDataReserveHours, 7*24)
  109. }
  110. type AuthServerConfig struct {
  111. Method string `json:"method,omitempty"`
  112. AdditionalAuthScopes []AuthScope `json:"additionalAuthScopes,omitempty"`
  113. Token string `json:"token,omitempty"`
  114. OIDC AuthOIDCServerConfig `json:"oidc,omitempty"`
  115. }
  116. func (c *AuthServerConfig) Complete() {
  117. c.Method = util.EmptyOr(c.Method, "token")
  118. }
  119. type AuthOIDCServerConfig struct {
  120. // Issuer specifies the issuer to verify OIDC tokens with. This issuer
  121. // will be used to load public keys to verify signature and will be compared
  122. // with the issuer claim in the OIDC token.
  123. Issuer string `json:"issuer,omitempty"`
  124. // Audience specifies the audience OIDC tokens should contain when validated.
  125. // If this value is empty, audience ("client ID") verification will be skipped.
  126. Audience string `json:"audience,omitempty"`
  127. // SkipExpiryCheck specifies whether to skip checking if the OIDC token is
  128. // expired.
  129. SkipExpiryCheck bool `json:"skipExpiryCheck,omitempty"`
  130. // SkipIssuerCheck specifies whether to skip checking if the OIDC token's
  131. // issuer claim matches the issuer specified in OidcIssuer.
  132. SkipIssuerCheck bool `json:"skipIssuerCheck,omitempty"`
  133. }
  134. type ServerTransportConfig struct {
  135. // TCPMux toggles TCP stream multiplexing. This allows multiple requests
  136. // from a client to share a single TCP connection. By default, this value
  137. // is true.
  138. TCPMux *bool `json:"tcpMux,omitempty"`
  139. // TCPMuxKeepaliveInterval specifies the keep alive interval for TCP stream multipler.
  140. // If TCPMux is true, heartbeat of application layer is unnecessary because it can only rely on heartbeat in TCPMux.
  141. TCPMuxKeepaliveInterval int64 `json:"tcpMuxKeepaliveInterval,omitempty"`
  142. // TCPKeepAlive specifies the interval between keep-alive probes for an active network connection between frpc and frps.
  143. // If negative, keep-alive probes are disabled.
  144. TCPKeepAlive int64 `json:"tcpKeepalive,omitempty"`
  145. // MaxPoolCount specifies the maximum pool size for each proxy. By default,
  146. // this value is 5.
  147. MaxPoolCount int64 `json:"maxPoolCount,omitempty"`
  148. // HeartBeatTimeout specifies the maximum time to wait for a heartbeat
  149. // before terminating the connection. It is not recommended to change this
  150. // value. By default, this value is 90. Set negative value to disable it.
  151. HeartbeatTimeout int64 `json:"heartbeatTimeout,omitempty"`
  152. // QUIC options.
  153. QUIC QUICOptions `json:"quic,omitempty"`
  154. }
  155. func (c *ServerTransportConfig) Complete() {
  156. c.TCPMux = util.EmptyOr(c.TCPMux, lo.ToPtr(true))
  157. c.TCPMuxKeepaliveInterval = util.EmptyOr(c.TCPMuxKeepaliveInterval, 60)
  158. c.TCPKeepAlive = util.EmptyOr(c.TCPKeepAlive, 7200)
  159. c.MaxPoolCount = util.EmptyOr(c.MaxPoolCount, 5)
  160. c.HeartbeatTimeout = util.EmptyOr(c.HeartbeatTimeout, 90)
  161. c.QUIC.Complete()
  162. }
  163. type TLSServerConfig struct {
  164. // Force specifies whether to only accept TLS-encrypted connections.
  165. Force bool `json:"force,omitempty"`
  166. TLSConfig
  167. }