1
0

service.go 12 KB

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