service.go 12 KB

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