| // |
| 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 | "os" |
| 19 |
| 20 | "github.com/samber/lo" |
| 21 |
| 22 | "github.com/fatedier/frp/pkg/util/util" |
| 23 | ) |
| 24 |
| 25 | type ClientConfig struct { |
| 26 | ClientCommonConfig |
| 27 |
| 28 | Proxies []TypedProxyConfig `json:"proxies,omitempty"` |
| 29 | Visitors []TypedVisitorConfig `json:"visitors,omitempty"` |
| 30 | } |
| 31 |
| 32 | type ClientCommonConfig struct { |
| 33 | APIMetadata |
| 34 |
| 35 | Auth AuthClientConfig `json:"auth,omitempty"` |
| 36 | // User specifies a prefix for proxy names to distinguish them from other |
| 37 | // clients. If this value is not "", proxy names will automatically be |
| 38 | // changed to "{user}.{proxy_name}". |
| 39 | User string `json:"user,omitempty"` |
| 40 | // ClientID uniquely identifies this frpc instance. |
| 41 | ClientID string `json:"clientID,omitempty"` |
| 42 |
| 43 | // ServerAddr specifies the address of the server to connect to. By |
| 44 | // default, this value is "0.0.0.0". |
| 45 | ServerAddr string `json:"serverAddr,omitempty"` |
| 46 | // ServerPort specifies the port to connect to the server on. By default, |
| 47 | // this value is 7000. |
| 48 | ServerPort int `json:"serverPort,omitempty"` |
| 49 | // STUN server to help penetrate NAT hole. |
| 50 | NatHoleSTUNServer string `json:"natHoleStunServer,omitempty"` |
| 51 | // DNSServer specifies a DNS server address for FRPC to use. If this value |
| 52 | // is "", the default DNS will be used. |
| 53 | DNSServer string `json:"dnsServer,omitempty"` |
| 54 | // LoginFailExit controls whether or not the client should exit after a |
| 55 | // failed login attempt. If false, the client will retry until a login |
| 56 | // attempt succeeds. By default, this value is true. |
| 57 | LoginFailExit *bool `json:"loginFailExit,omitempty"` |
| 58 | // Start specifies a set of enabled proxies by name. If this set is empty, |
| 59 | // all supplied proxies are enabled. By default, this value is an empty |
| 60 | // set. |
| 61 | Start []string `json:"start,omitempty"` |
| 62 |
| 63 | Log LogConfig `json:"log,omitempty"` |
| 64 | WebServer WebServerConfig `json:"webServer,omitempty"` |
| 65 | Transport ClientTransportConfig `json:"transport,omitempty"` |
| 66 | VirtualNet VirtualNetConfig `json:"virtualNet,omitempty"` |
| 67 |
| 68 | // FeatureGates specifies a set of feature gates to enable or disable. |
| 69 | // This can be used to enable alpha/beta features or disable default features. |
| 70 | FeatureGates map[string]bool `json:"featureGates,omitempty"` |
| 71 |
| 72 | // UDPPacketSize specifies the udp packet size |
| 73 | // By default, this value is 1500 |
| 74 | UDPPacketSize int64 `json:"udpPacketSize,omitempty"` |
| 75 | // Client metadata info |
| 76 | Metadatas map[string]string `json:"metadatas,omitempty"` |
| 77 |
| 78 | // Include other config files for proxies. |
| 79 | IncludeConfigFiles []string `json:"includes,omitempty"` |
| 80 |
| 81 | // Store config enables the built-in store source (not configurable via sources list). |
| 82 | Store StoreConfig `json:"store,omitempty"` |
| 83 | } |
| 84 |
| 85 | func (c *ClientCommonConfig) Complete() error { |
| 86 | c.ServerAddr = util.EmptyOr(c.ServerAddr, "0.0.0.0") |
| 87 | c.ServerPort = util.EmptyOr(c.ServerPort, 7000) |
| 88 | c.LoginFailExit = util.EmptyOr(c.LoginFailExit, lo.ToPtr(true)) |
| 89 | c.NatHoleSTUNServer = util.EmptyOr(c.NatHoleSTUNServer, "stun.easyvoip.com:3478") |
| 90 |
| 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 |
| 98 | c.UDPPacketSize = util.EmptyOr(c.UDPPacketSize, 1500) |
| 99 | return nil |
| 100 | } |
| 101 |
| 102 | type ClientTransportConfig struct { |
| 103 | // Protocol specifies the protocol to use when interacting with the server. |
| 104 | // Valid values are "tcp", "kcp", "quic", "websocket" and "wss". By default, this value |
| 105 | // is "tcp". |
| 106 | Protocol string `json:"protocol,omitempty"` |
| 107 | // WireProtocol specifies the frpc/frps internal wire protocol version. |
| 108 | // Valid values are "v1" and "v2". By default, this value is "v1". |
| 109 | WireProtocol string `json:"wireProtocol,omitempty"` |
| 110 | // The maximum amount of time a dial to server will wait for a connect to complete. |
| 111 | DialServerTimeout int64 `json:"dialServerTimeout,omitempty"` |
| 112 | // DialServerKeepAlive specifies the interval between keep-alive probes for an active network connection between frpc and frps. |
| 113 | // If negative, keep-alive probes are disabled. |
| 114 | DialServerKeepAlive int64 `json:"dialServerKeepalive,omitempty"` |
| 115 | // ConnectServerLocalIP specifies the address of the client bind when it connect to server. |
| 116 | // Note: This value only use in TCP/Websocket protocol. Not support in KCP protocol. |
| 117 | ConnectServerLocalIP string `json:"connectServerLocalIP,omitempty"` |
| 118 | // ProxyURL specifies a proxy address to connect to the server through. If |
| 119 | // this value is "", the server will be connected to directly. By default, |
| 120 | // this value is read from the "http_proxy" environment variable. |
| 121 | ProxyURL string `json:"proxyURL,omitempty"` |
| 122 | // PoolCount specifies the number of connections the client will make to |
| 123 | // the server in advance. |
| 124 | PoolCount int `json:"poolCount,omitempty"` |
| 125 | // TCPMux toggles TCP stream multiplexing. This allows multiple requests |
| 126 | // from a client to share a single TCP connection. If this value is true, |
| 127 | // the server must have TCP multiplexing enabled as well. By default, this |
| 128 | // value is true. |
| 129 | TCPMux *bool `json:"tcpMux,omitempty"` |
| 130 | // TCPMuxKeepaliveInterval specifies the keep alive interval for TCP stream multiplier. |
| 131 | // If TCPMux is true, heartbeat of application layer is unnecessary because it can only rely on heartbeat in TCPMux. |
| 132 | TCPMuxKeepaliveInterval int64 `json:"tcpMuxKeepaliveInterval,omitempty"` |
| 133 | // QUIC protocol options. |
| 134 | QUIC QUICOptions `json:"quic,omitempty"` |
| 135 | // HeartBeatInterval specifies at what interval heartbeats are sent to the |
| 136 | // server, in seconds. It is not recommended to change this value. By |
| 137 | // default, this value is 30. Set negative value to disable it. |
| 138 | HeartbeatInterval int64 `json:"heartbeatInterval,omitempty"` |
| 139 | // HeartBeatTimeout specifies the maximum allowed heartbeat response delay |
| 140 | // before the connection is terminated, in seconds. It is not recommended |
| 141 | // to change this value. By default, this value is 90. Set negative value to disable it. |
| 142 | HeartbeatTimeout int64 `json:"heartbeatTimeout,omitempty"` |
| 143 | // TLS specifies TLS settings for the connection to the server. |
| 144 | TLS TLSClientConfig `json:"tls,omitempty"` |
| 145 | } |
| 146 |
| 147 | func (c *ClientTransportConfig) Complete() { |
| 148 | c.Protocol = util.EmptyOr(c.Protocol, "tcp") |
| 149 | c.WireProtocol = util.EmptyOr(c.WireProtocol, "v1") |
| 150 | c.DialServerTimeout = util.EmptyOr(c.DialServerTimeout, 10) |
| 151 | c.DialServerKeepAlive = util.EmptyOr(c.DialServerKeepAlive, 7200) |
| 152 | c.ProxyURL = util.EmptyOr(c.ProxyURL, os.Getenv("http_proxy")) |
| 153 | c.PoolCount = util.EmptyOr(c.PoolCount, 1) |
| 154 | c.TCPMux = util.EmptyOr(c.TCPMux, lo.ToPtr(true)) |
| 155 | c.TCPMuxKeepaliveInterval = util.EmptyOr(c.TCPMuxKeepaliveInterval, 30) |
| 156 | if lo.FromPtr(c.TCPMux) { |
| 157 | // If TCPMux is enabled, heartbeat of application layer is unnecessary because we can rely on heartbeat in tcpmux. |
| 158 | c.HeartbeatInterval = util.EmptyOr(c.HeartbeatInterval, -1) |
| 159 | c.HeartbeatTimeout = util.EmptyOr(c.HeartbeatTimeout, -1) |
| 160 | } else { |
| 161 | c.HeartbeatInterval = util.EmptyOr(c.HeartbeatInterval, 30) |
| 162 | c.HeartbeatTimeout = util.EmptyOr(c.HeartbeatTimeout, 90) |
| 163 | } |
| 164 | c.QUIC.Complete() |
| 165 | c.TLS.Complete() |
| 166 | } |
| 167 |
| 168 | type TLSClientConfig struct { |
| 169 | // TLSEnable specifies whether or not TLS should be used when communicating |
| 170 | // with the server. If "tls.certFile" and "tls.keyFile" are valid, |
| 171 | // client will load the supplied tls configuration. |
| 172 | // Since v0.50.0, the default value has been changed to true, and tls is enabled by default. |
| 173 | Enable *bool `json:"enable,omitempty"` |
| 174 | // If DisableCustomTLSFirstByte is set to false, frpc will establish a connection with frps using the |
| 175 | // first custom byte when tls is enabled. |
| 176 | // Since v0.50.0, the default value has been changed to true, and the first custom byte is disabled by default. |
| 177 | DisableCustomTLSFirstByte *bool `json:"disableCustomTLSFirstByte,omitempty"` |
| 178 |
| 179 | TLSConfig |
| 180 | } |
| 181 |
| 182 | func (c *TLSClientConfig) Complete() { |
| 183 | c.Enable = util.EmptyOr(c.Enable, lo.ToPtr(true)) |
| 184 | c.DisableCustomTLSFirstByte = util.EmptyOr(c.DisableCustomTLSFirstByte, lo.ToPtr(true)) |
| 185 | } |
| 186 |
| 187 | type AuthClientConfig struct { |
| 188 | // Method specifies what authentication method to use to |
| 189 | // authenticate frpc with frps. If "token" is specified - token will be |
| 190 | // read into login message. If "oidc" is specified - OIDC (Open ID Connect) |
| 191 | // token will be issued using OIDC settings. By default, this value is "token". |
| 192 | Method AuthMethod `json:"method,omitempty"` |
| 193 | // Specify whether to include auth info in additional scope. |
| 194 | // Current supported scopes are: "HeartBeats", "NewWorkConns". |
| 195 | AdditionalScopes []AuthScope `json:"additionalScopes,omitempty"` |
| 196 | // Token specifies the authorization token used to create keys to be sent |
| 197 | // to the server. The server must have a matching token for authorization |
| 198 | // to succeed. By default, this value is "". |
| 199 | Token string `json:"token,omitempty"` |
| 200 | // TokenSource specifies a dynamic source for the authorization token. |
| 201 | // This is mutually exclusive with Token field. |
| 202 | TokenSource *ValueSource `json:"tokenSource,omitempty"` |
| 203 | OIDC AuthOIDCClientConfig `json:"oidc,omitempty"` |
| 204 | } |
| 205 |
| 206 | func (c *AuthClientConfig) Complete() error { |
| 207 | c.Method = util.EmptyOr(c.Method, "token") |
| 208 | return nil |
| 209 | } |
| 210 |
| 211 | type AuthOIDCClientConfig struct { |
| 212 | // ClientID specifies the client ID to use to get a token in OIDC authentication. |
| 213 | ClientID string `json:"clientID,omitempty"` |
| 214 | // ClientSecret specifies the client secret to use to get a token in OIDC |
| 215 | // authentication. |
| 216 | ClientSecret string `json:"clientSecret,omitempty"` |
| 217 | // Audience specifies the audience of the token in OIDC authentication. |
| 218 | Audience string `json:"audience,omitempty"` |
| 219 | // Scope specifies the scope of the token in OIDC authentication. |
| 220 | Scope string `json:"scope,omitempty"` |
| 221 | // TokenEndpointURL specifies the URL which implements OIDC Token Endpoint. |
| 222 | // It will be used to get an OIDC token. |
| 223 | TokenEndpointURL string `json:"tokenEndpointURL,omitempty"` |
| 224 | // AdditionalEndpointParams specifies additional parameters to be sent |
| 225 | // this field will be transfer to map[string][]string in OIDC token generator. |
| 226 | AdditionalEndpointParams map[string]string `json:"additionalEndpointParams,omitempty"` |
| 227 |
| 228 | // TrustedCaFile specifies the path to a custom CA certificate file |
| 229 | // for verifying the OIDC token endpoint's TLS certificate. |
| 230 | TrustedCaFile string `json:"trustedCaFile,omitempty"` |
| 231 | // InsecureSkipVerify disables TLS certificate verification for the |
| 232 | // OIDC token endpoint. Only use this for debugging, not recommended for production. |
| 233 | InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"` |
| 234 | // ProxyURL specifies a proxy to use when connecting to the OIDC token endpoint. |
| 235 | // Supports http, https, socks5, and socks5h proxy protocols. |
| 236 | // If empty, no proxy is used for OIDC connections. |
| 237 | ProxyURL string `json:"proxyURL,omitempty"` |
| 238 |
| 239 | // TokenSource specifies a custom dynamic source for the authorization token. |
| 240 | // This is mutually exclusive with every other field of this structure. |
| 241 | TokenSource *ValueSource `json:"tokenSource,omitempty"` |
| 242 | } |
| 243 |
| 244 | type VirtualNetConfig struct { |
| 245 | Address string `json:"address,omitempty"` |
| 246 | } |