1
0

client.go 9.6 KB

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