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