1
0

client.go 9.2 KB

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