client.go 11 KB

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