1
0

service.go 12 KB

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