service.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  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 client
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "os"
  21. "runtime"
  22. "sync"
  23. "time"
  24. "github.com/fatedier/golib/crypto"
  25. "github.com/samber/lo"
  26. "github.com/fatedier/frp/client/proxy"
  27. "github.com/fatedier/frp/pkg/auth"
  28. v1 "github.com/fatedier/frp/pkg/config/v1"
  29. "github.com/fatedier/frp/pkg/msg"
  30. httppkg "github.com/fatedier/frp/pkg/util/http"
  31. "github.com/fatedier/frp/pkg/util/log"
  32. netpkg "github.com/fatedier/frp/pkg/util/net"
  33. "github.com/fatedier/frp/pkg/util/version"
  34. "github.com/fatedier/frp/pkg/util/wait"
  35. "github.com/fatedier/frp/pkg/util/xlog"
  36. )
  37. func init() {
  38. crypto.DefaultSalt = "frp"
  39. // Disable quic-go's receive buffer warning.
  40. os.Setenv("QUIC_GO_DISABLE_RECEIVE_BUFFER_WARNING", "true")
  41. }
  42. type cancelErr struct {
  43. Err error
  44. }
  45. func (e cancelErr) Error() string {
  46. return e.Err.Error()
  47. }
  48. // ServiceOptions contains options for creating a new client service.
  49. type ServiceOptions struct {
  50. Common *v1.ClientCommonConfig
  51. ProxyCfgs []v1.ProxyConfigurer
  52. VisitorCfgs []v1.VisitorConfigurer
  53. // ConfigFilePath is the path to the configuration file used to initialize.
  54. // If it is empty, it means that the configuration file is not used for initialization.
  55. // It may be initialized using command line parameters or called directly.
  56. ConfigFilePath string
  57. // ClientSpec is the client specification that control the client behavior.
  58. ClientSpec *msg.ClientSpec
  59. // ConnectorCreator is a function that creates a new connector to make connections to the server.
  60. // The Connector shields the underlying connection details, whether it is through TCP or QUIC connection,
  61. // and regardless of whether multiplexing is used.
  62. //
  63. // If it is not set, the default frpc connector will be used.
  64. // By using a custom Connector, it can be used to implement a VirtualClient, which connects to frps
  65. // through a pipe instead of a real physical connection.
  66. ConnectorCreator func(context.Context, *v1.ClientCommonConfig) Connector
  67. // HandleWorkConnCb is a callback function that is called when a new work connection is created.
  68. //
  69. // If it is not set, the default frpc implementation will be used.
  70. HandleWorkConnCb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool
  71. }
  72. // setServiceOptionsDefault sets the default values for ServiceOptions.
  73. func setServiceOptionsDefault(options *ServiceOptions) {
  74. if options.Common != nil {
  75. options.Common.Complete()
  76. }
  77. if options.ConnectorCreator == nil {
  78. options.ConnectorCreator = NewConnector
  79. }
  80. }
  81. // Service is the client service that connects to frps and provides proxy services.
  82. type Service struct {
  83. ctlMu sync.RWMutex
  84. // manager control connection with server
  85. ctl *Control
  86. // Uniq id got from frps, it will be attached to loginMsg.
  87. runID string
  88. // Sets authentication based on selected method
  89. authSetter auth.Setter
  90. // web server for admin UI and apis
  91. webServer *httppkg.Server
  92. cfgMu sync.RWMutex
  93. common *v1.ClientCommonConfig
  94. proxyCfgs []v1.ProxyConfigurer
  95. visitorCfgs []v1.VisitorConfigurer
  96. clientSpec *msg.ClientSpec
  97. // The configuration file used to initialize this client, or an empty
  98. // string if no configuration file was used.
  99. configFilePath string
  100. // service context
  101. ctx context.Context
  102. // call cancel to stop service
  103. cancel context.CancelCauseFunc
  104. gracefulShutdownDuration time.Duration
  105. connectorCreator func(context.Context, *v1.ClientCommonConfig) Connector
  106. handleWorkConnCb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool
  107. }
  108. func NewService(options ServiceOptions) (*Service, error) {
  109. setServiceOptionsDefault(&options)
  110. var webServer *httppkg.Server
  111. if options.Common.WebServer.Port > 0 {
  112. ws, err := httppkg.NewServer(options.Common.WebServer)
  113. if err != nil {
  114. return nil, err
  115. }
  116. webServer = ws
  117. }
  118. s := &Service{
  119. ctx: context.Background(),
  120. authSetter: auth.NewAuthSetter(options.Common.Auth),
  121. webServer: webServer,
  122. common: options.Common,
  123. configFilePath: options.ConfigFilePath,
  124. proxyCfgs: options.ProxyCfgs,
  125. visitorCfgs: options.VisitorCfgs,
  126. clientSpec: options.ClientSpec,
  127. connectorCreator: options.ConnectorCreator,
  128. handleWorkConnCb: options.HandleWorkConnCb,
  129. }
  130. if webServer != nil {
  131. webServer.RouteRegister(s.registerRouteHandlers)
  132. }
  133. return s, nil
  134. }
  135. func (svr *Service) Run(ctx context.Context) error {
  136. ctx, cancel := context.WithCancelCause(ctx)
  137. svr.ctx = xlog.NewContext(ctx, xlog.FromContextSafe(ctx))
  138. svr.cancel = cancel
  139. // set custom DNSServer
  140. if svr.common.DNSServer != "" {
  141. netpkg.SetDefaultDNSAddress(svr.common.DNSServer)
  142. }
  143. // first login to frps
  144. svr.loopLoginUntilSuccess(10*time.Second, lo.FromPtr(svr.common.LoginFailExit))
  145. if svr.ctl == nil {
  146. cancelCause := cancelErr{}
  147. _ = errors.As(context.Cause(svr.ctx), &cancelCause)
  148. return fmt.Errorf("login to the server failed: %v. With loginFailExit enabled, no additional retries will be attempted", cancelCause.Err)
  149. }
  150. go svr.keepControllerWorking()
  151. if svr.webServer != nil {
  152. go func() {
  153. log.Infof("admin server listen on %s", svr.webServer.Address())
  154. if err := svr.webServer.Run(); err != nil {
  155. log.Warnf("admin server exit with error: %v", err)
  156. }
  157. }()
  158. }
  159. <-svr.ctx.Done()
  160. svr.stop()
  161. return nil
  162. }
  163. func (svr *Service) keepControllerWorking() {
  164. <-svr.ctl.Done()
  165. // There is a situation where the login is successful but due to certain reasons,
  166. // the control immediately exits. It is necessary to limit the frequency of reconnection in this case.
  167. // The interval for the first three retries in 1 minute will be very short, and then it will increase exponentially.
  168. // The maximum interval is 20 seconds.
  169. wait.BackoffUntil(func() (bool, error) {
  170. // loopLoginUntilSuccess is another layer of loop that will continuously attempt to
  171. // login to the server until successful.
  172. svr.loopLoginUntilSuccess(20*time.Second, false)
  173. if svr.ctl != nil {
  174. <-svr.ctl.Done()
  175. return false, errors.New("control is closed and try another loop")
  176. }
  177. // If the control is nil, it means that the login failed and the service is also closed.
  178. return false, nil
  179. }, wait.NewFastBackoffManager(
  180. wait.FastBackoffOptions{
  181. Duration: time.Second,
  182. Factor: 2,
  183. Jitter: 0.1,
  184. MaxDuration: 20 * time.Second,
  185. FastRetryCount: 3,
  186. FastRetryDelay: 200 * time.Millisecond,
  187. FastRetryWindow: time.Minute,
  188. FastRetryJitter: 0.5,
  189. },
  190. ), true, svr.ctx.Done())
  191. }
  192. // login creates a connection to frps and registers it self as a client
  193. // conn: control connection
  194. // session: if it's not nil, using tcp mux
  195. func (svr *Service) login() (conn net.Conn, connector Connector, err error) {
  196. xl := xlog.FromContextSafe(svr.ctx)
  197. connector = svr.connectorCreator(svr.ctx, svr.common)
  198. if err = connector.Open(); err != nil {
  199. return nil, nil, err
  200. }
  201. defer func() {
  202. if err != nil {
  203. connector.Close()
  204. }
  205. }()
  206. conn, err = connector.Connect()
  207. if err != nil {
  208. return
  209. }
  210. loginMsg := &msg.Login{
  211. Arch: runtime.GOARCH,
  212. Os: runtime.GOOS,
  213. PoolCount: svr.common.Transport.PoolCount,
  214. User: svr.common.User,
  215. Version: version.Full(),
  216. Timestamp: time.Now().Unix(),
  217. RunID: svr.runID,
  218. Metas: svr.common.Metadatas,
  219. }
  220. if svr.clientSpec != nil {
  221. loginMsg.ClientSpec = *svr.clientSpec
  222. }
  223. // Add auth
  224. if err = svr.authSetter.SetLogin(loginMsg); err != nil {
  225. return
  226. }
  227. if err = msg.WriteMsg(conn, loginMsg); err != nil {
  228. return
  229. }
  230. var loginRespMsg msg.LoginResp
  231. _ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
  232. if err = msg.ReadMsgInto(conn, &loginRespMsg); err != nil {
  233. return
  234. }
  235. _ = conn.SetReadDeadline(time.Time{})
  236. if loginRespMsg.Error != "" {
  237. err = fmt.Errorf("%s", loginRespMsg.Error)
  238. xl.Errorf("%s", loginRespMsg.Error)
  239. return
  240. }
  241. svr.runID = loginRespMsg.RunID
  242. xl.AddPrefix(xlog.LogPrefix{Name: "runID", Value: svr.runID})
  243. xl.Infof("login to server success, get run id [%s]", loginRespMsg.RunID)
  244. return
  245. }
  246. func (svr *Service) loopLoginUntilSuccess(maxInterval time.Duration, firstLoginExit bool) {
  247. xl := xlog.FromContextSafe(svr.ctx)
  248. loginFunc := func() (bool, error) {
  249. xl.Infof("try to connect to server...")
  250. conn, connector, err := svr.login()
  251. if err != nil {
  252. xl.Warnf("connect to server error: %v", err)
  253. if firstLoginExit {
  254. svr.cancel(cancelErr{Err: err})
  255. }
  256. return false, err
  257. }
  258. svr.cfgMu.RLock()
  259. proxyCfgs := svr.proxyCfgs
  260. visitorCfgs := svr.visitorCfgs
  261. svr.cfgMu.RUnlock()
  262. connEncrypted := true
  263. if svr.clientSpec != nil && svr.clientSpec.Type == "ssh-tunnel" {
  264. connEncrypted = false
  265. }
  266. sessionCtx := &SessionContext{
  267. Common: svr.common,
  268. RunID: svr.runID,
  269. Conn: conn,
  270. ConnEncrypted: connEncrypted,
  271. AuthSetter: svr.authSetter,
  272. Connector: connector,
  273. }
  274. ctl, err := NewControl(svr.ctx, sessionCtx)
  275. if err != nil {
  276. conn.Close()
  277. xl.Errorf("NewControl error: %v", err)
  278. return false, err
  279. }
  280. ctl.SetInWorkConnCallback(svr.handleWorkConnCb)
  281. ctl.Run(proxyCfgs, visitorCfgs)
  282. // close and replace previous control
  283. svr.ctlMu.Lock()
  284. if svr.ctl != nil {
  285. svr.ctl.Close()
  286. }
  287. svr.ctl = ctl
  288. svr.ctlMu.Unlock()
  289. return true, nil
  290. }
  291. // try to reconnect to server until success
  292. wait.BackoffUntil(loginFunc, wait.NewFastBackoffManager(
  293. wait.FastBackoffOptions{
  294. Duration: time.Second,
  295. Factor: 2,
  296. Jitter: 0.1,
  297. MaxDuration: maxInterval,
  298. }), true, svr.ctx.Done())
  299. }
  300. func (svr *Service) UpdateAllConfigurer(proxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) error {
  301. svr.cfgMu.Lock()
  302. svr.proxyCfgs = proxyCfgs
  303. svr.visitorCfgs = visitorCfgs
  304. svr.cfgMu.Unlock()
  305. svr.ctlMu.RLock()
  306. ctl := svr.ctl
  307. svr.ctlMu.RUnlock()
  308. if ctl != nil {
  309. return svr.ctl.UpdateAllConfigurer(proxyCfgs, visitorCfgs)
  310. }
  311. return nil
  312. }
  313. func (svr *Service) Close() {
  314. svr.GracefulClose(time.Duration(0))
  315. }
  316. func (svr *Service) GracefulClose(d time.Duration) {
  317. svr.gracefulShutdownDuration = d
  318. svr.cancel(nil)
  319. }
  320. func (svr *Service) stop() {
  321. svr.ctlMu.Lock()
  322. defer svr.ctlMu.Unlock()
  323. if svr.ctl != nil {
  324. svr.ctl.GracefulClose(svr.gracefulShutdownDuration)
  325. svr.ctl = nil
  326. }
  327. }
  328. // TODO(fatedier): Use StatusExporter to provide query interfaces instead of directly using methods from the Service.
  329. func (svr *Service) GetProxyStatus(name string) (*proxy.WorkingStatus, error) {
  330. svr.ctlMu.RLock()
  331. ctl := svr.ctl
  332. svr.ctlMu.RUnlock()
  333. if ctl == nil {
  334. return nil, fmt.Errorf("control is not running")
  335. }
  336. ws, ok := ctl.pm.GetProxyStatus(name)
  337. if !ok {
  338. return nil, fmt.Errorf("proxy [%s] is not found", name)
  339. }
  340. return ws, nil
  341. }