auto.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package controlclient
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "tailscale.com/logtail/backoff"
  13. "tailscale.com/net/sockstats"
  14. "tailscale.com/tailcfg"
  15. "tailscale.com/tstime"
  16. "tailscale.com/types/key"
  17. "tailscale.com/types/logger"
  18. "tailscale.com/types/netmap"
  19. "tailscale.com/types/persist"
  20. "tailscale.com/types/structs"
  21. "tailscale.com/util/execqueue"
  22. )
  23. type LoginGoal struct {
  24. _ structs.Incomparable
  25. token *tailcfg.Oauth2Token // oauth token to use when logging in
  26. flags LoginFlags // flags to use when logging in
  27. url string // auth url that needs to be visited
  28. }
  29. var _ Client = (*Auto)(nil)
  30. // waitUnpause waits until either the client is unpaused or the Auto client is
  31. // shut down. It reports whether the client should keep running (i.e. it's not
  32. // closed).
  33. func (c *Auto) waitUnpause(routineLogName string) (keepRunning bool) {
  34. c.mu.Lock()
  35. if !c.paused || c.closed {
  36. defer c.mu.Unlock()
  37. return !c.closed
  38. }
  39. unpaused := c.unpausedChanLocked()
  40. c.mu.Unlock()
  41. c.logf("%s: awaiting unpause", routineLogName)
  42. return <-unpaused
  43. }
  44. // updateRoutine is responsible for informing the server of worthy changes to
  45. // our local state. It runs in its own goroutine.
  46. func (c *Auto) updateRoutine() {
  47. defer close(c.updateDone)
  48. bo := backoff.NewBackoff("updateRoutine", c.logf, 30*time.Second)
  49. // lastUpdateGenInformed is the value of lastUpdateAt that we've successfully
  50. // informed the server of.
  51. var lastUpdateGenInformed updateGen
  52. for {
  53. if !c.waitUnpause("updateRoutine") {
  54. c.logf("updateRoutine: exiting")
  55. return
  56. }
  57. c.mu.Lock()
  58. gen := c.lastUpdateGen
  59. ctx := c.mapCtx
  60. needUpdate := gen > 0 && gen != lastUpdateGenInformed && c.loggedIn
  61. c.mu.Unlock()
  62. if !needUpdate {
  63. // Nothing to do, wait for a signal.
  64. select {
  65. case <-ctx.Done():
  66. continue
  67. case <-c.updateCh:
  68. continue
  69. }
  70. }
  71. t0 := c.clock.Now()
  72. err := c.direct.SendUpdate(ctx)
  73. d := time.Since(t0).Round(time.Millisecond)
  74. if err != nil {
  75. if ctx.Err() == nil {
  76. c.direct.logf("lite map update error after %v: %v", d, err)
  77. }
  78. bo.BackOff(ctx, err)
  79. continue
  80. }
  81. bo.BackOff(ctx, nil)
  82. c.direct.logf("[v1] successful lite map update in %v", d)
  83. lastUpdateGenInformed = gen
  84. }
  85. }
  86. // atomicGen is an atomic int64 generator. It is used to generate monotonically
  87. // increasing numbers for updateGen.
  88. var atomicGen atomic.Int64
  89. func nextUpdateGen() updateGen {
  90. return updateGen(atomicGen.Add(1))
  91. }
  92. // updateGen is a monotonically increasing number that represents a particular
  93. // update to the local state.
  94. type updateGen int64
  95. // Auto connects to a tailcontrol server for a node.
  96. // It's a concrete implementation of the Client interface.
  97. type Auto struct {
  98. direct *Direct // our interface to the server APIs
  99. clock tstime.Clock
  100. logf logger.Logf
  101. closed bool
  102. updateCh chan struct{} // readable when we should inform the server of a change
  103. observer Observer // called to update Client status; always non-nil
  104. observerQueue execqueue.ExecQueue
  105. unregisterHealthWatch func()
  106. mu sync.Mutex // mutex guards the following fields
  107. wantLoggedIn bool // whether the user wants to be logged in per last method call
  108. urlToVisit string // the last url we were told to visit
  109. expiry time.Time
  110. // lastUpdateGen is the gen of last update we had an update worth sending to
  111. // the server.
  112. lastUpdateGen updateGen
  113. paused bool // whether we should stop making HTTP requests
  114. unpauseWaiters []chan bool // chans that gets sent true (once) on wake, or false on Shutdown
  115. loggedIn bool // true if currently logged in
  116. loginGoal *LoginGoal // non-nil if some login activity is desired
  117. inMapPoll bool // true once we get the first MapResponse in a stream; false when HTTP response ends
  118. state State // TODO(bradfitz): delete this, make it computed by method from other state
  119. authCtx context.Context // context used for auth requests
  120. mapCtx context.Context // context used for netmap and update requests
  121. authCancel func() // cancel authCtx
  122. mapCancel func() // cancel mapCtx
  123. authDone chan struct{} // when closed, authRoutine is done
  124. mapDone chan struct{} // when closed, mapRoutine is done
  125. updateDone chan struct{} // when closed, updateRoutine is done
  126. }
  127. // New creates and starts a new Auto.
  128. func New(opts Options) (*Auto, error) {
  129. c, err := NewNoStart(opts)
  130. if c != nil {
  131. c.Start()
  132. }
  133. return c, err
  134. }
  135. // NewNoStart creates a new Auto, but without calling Start on it.
  136. func NewNoStart(opts Options) (_ *Auto, err error) {
  137. direct, err := NewDirect(opts)
  138. if err != nil {
  139. return nil, err
  140. }
  141. defer func() {
  142. if err != nil {
  143. direct.Close()
  144. }
  145. }()
  146. if opts.Observer == nil {
  147. return nil, errors.New("missing required Options.Observer")
  148. }
  149. if opts.Logf == nil {
  150. opts.Logf = func(fmt string, args ...any) {}
  151. }
  152. if opts.Clock == nil {
  153. opts.Clock = tstime.StdClock{}
  154. }
  155. c := &Auto{
  156. direct: direct,
  157. clock: opts.Clock,
  158. logf: opts.Logf,
  159. updateCh: make(chan struct{}, 1),
  160. authDone: make(chan struct{}),
  161. mapDone: make(chan struct{}),
  162. updateDone: make(chan struct{}),
  163. observer: opts.Observer,
  164. }
  165. c.authCtx, c.authCancel = context.WithCancel(context.Background())
  166. c.authCtx = sockstats.WithSockStats(c.authCtx, sockstats.LabelControlClientAuto, opts.Logf)
  167. c.mapCtx, c.mapCancel = context.WithCancel(context.Background())
  168. c.mapCtx = sockstats.WithSockStats(c.mapCtx, sockstats.LabelControlClientAuto, opts.Logf)
  169. c.unregisterHealthWatch = opts.HealthTracker.RegisterWatcher(direct.ReportHealthChange)
  170. return c, nil
  171. }
  172. // SetPaused controls whether HTTP activity should be paused.
  173. //
  174. // The client can be paused and unpaused repeatedly, unlike Start and Shutdown, which can only be used once.
  175. func (c *Auto) SetPaused(paused bool) {
  176. c.mu.Lock()
  177. defer c.mu.Unlock()
  178. if paused == c.paused || c.closed {
  179. return
  180. }
  181. c.logf("setPaused(%v)", paused)
  182. c.paused = paused
  183. if paused {
  184. c.cancelMapCtxLocked()
  185. c.cancelAuthCtxLocked()
  186. return
  187. }
  188. for _, ch := range c.unpauseWaiters {
  189. ch <- true
  190. }
  191. c.unpauseWaiters = nil
  192. }
  193. // Start starts the client's goroutines.
  194. //
  195. // It should only be called for clients created by NewNoStart.
  196. func (c *Auto) Start() {
  197. go c.authRoutine()
  198. go c.mapRoutine()
  199. go c.updateRoutine()
  200. }
  201. // updateControl sends a new OmitPeers, non-streaming map request (to just send
  202. // Hostinfo/Netinfo/Endpoints info, while keeping an existing streaming response
  203. // open).
  204. //
  205. // It should be called whenever there's something new to tell the server.
  206. func (c *Auto) updateControl() {
  207. gen := nextUpdateGen()
  208. c.mu.Lock()
  209. if gen < c.lastUpdateGen {
  210. // This update is out of date.
  211. c.mu.Unlock()
  212. return
  213. }
  214. c.lastUpdateGen = gen
  215. c.mu.Unlock()
  216. select {
  217. case c.updateCh <- struct{}{}:
  218. default:
  219. }
  220. }
  221. // cancelAuthCtxLocked is like cancelAuthCtx, but assumes the caller holds c.mu.
  222. func (c *Auto) cancelAuthCtxLocked() {
  223. if c.authCancel != nil {
  224. c.authCancel()
  225. }
  226. if !c.closed {
  227. c.authCtx, c.authCancel = context.WithCancel(context.Background())
  228. c.authCtx = sockstats.WithSockStats(c.authCtx, sockstats.LabelControlClientAuto, c.logf)
  229. }
  230. }
  231. // cancelMapCtxLocked is like cancelMapCtx, but assumes the caller holds c.mu.
  232. func (c *Auto) cancelMapCtxLocked() {
  233. if c.mapCancel != nil {
  234. c.mapCancel()
  235. }
  236. if !c.closed {
  237. c.mapCtx, c.mapCancel = context.WithCancel(context.Background())
  238. c.mapCtx = sockstats.WithSockStats(c.mapCtx, sockstats.LabelControlClientAuto, c.logf)
  239. }
  240. }
  241. // restartMap cancels the existing mapPoll and liteUpdates, and then starts a
  242. // new one.
  243. func (c *Auto) restartMap() {
  244. c.mu.Lock()
  245. c.cancelMapCtxLocked()
  246. synced := c.inMapPoll
  247. c.mu.Unlock()
  248. c.logf("[v1] restartMap: synced=%v", synced)
  249. c.updateControl()
  250. }
  251. func (c *Auto) authRoutine() {
  252. defer close(c.authDone)
  253. bo := backoff.NewBackoff("authRoutine", c.logf, 30*time.Second)
  254. for {
  255. if !c.waitUnpause("authRoutine") {
  256. c.logf("authRoutine: exiting")
  257. return
  258. }
  259. c.mu.Lock()
  260. goal := c.loginGoal
  261. ctx := c.authCtx
  262. if goal != nil {
  263. c.logf("[v1] authRoutine: %s; wantLoggedIn=%v", c.state, true)
  264. } else {
  265. c.logf("[v1] authRoutine: %s; goal=nil paused=%v", c.state, c.paused)
  266. }
  267. c.mu.Unlock()
  268. report := func(err error, msg string) {
  269. c.logf("[v1] %s: %v", msg, err)
  270. // don't send status updates for context errors,
  271. // since context cancelation is always on purpose.
  272. if ctx.Err() == nil {
  273. c.sendStatus("authRoutine-report", err, "", nil)
  274. }
  275. }
  276. if goal == nil {
  277. c.direct.health.SetAuthRoutineInError(nil)
  278. // Wait for user to Login or Logout.
  279. <-ctx.Done()
  280. c.logf("[v1] authRoutine: context done.")
  281. continue
  282. }
  283. c.mu.Lock()
  284. c.urlToVisit = goal.url
  285. if goal.url != "" {
  286. c.state = StateURLVisitRequired
  287. } else {
  288. c.state = StateAuthenticating
  289. }
  290. c.mu.Unlock()
  291. var url string
  292. var err error
  293. var f string
  294. if goal.url != "" {
  295. url, err = c.direct.WaitLoginURL(ctx, goal.url)
  296. f = "WaitLoginURL"
  297. } else {
  298. url, err = c.direct.TryLogin(ctx, goal.token, goal.flags)
  299. f = "TryLogin"
  300. }
  301. if err != nil {
  302. c.direct.health.SetAuthRoutineInError(err)
  303. report(err, f)
  304. bo.BackOff(ctx, err)
  305. continue
  306. }
  307. if url != "" {
  308. // goal.url ought to be empty here. However, not all control servers
  309. // get this right, and logging about it here just generates noise.
  310. //
  311. // TODO(bradfitz): I don't follow that comment. Our own testcontrol
  312. // used by tstest/integration hits this path, in fact.
  313. if c.direct.panicOnUse {
  314. panic("tainted client")
  315. }
  316. c.mu.Lock()
  317. c.urlToVisit = url
  318. c.loginGoal = &LoginGoal{
  319. flags: LoginDefault,
  320. url: url,
  321. }
  322. c.state = StateURLVisitRequired
  323. c.mu.Unlock()
  324. c.sendStatus("authRoutine-url", err, url, nil)
  325. if goal.url == url {
  326. // The server sent us the same URL we already tried,
  327. // backoff to avoid a busy loop.
  328. bo.BackOff(ctx, errors.New("login URL not changing"))
  329. } else {
  330. bo.BackOff(ctx, nil)
  331. }
  332. continue
  333. }
  334. // success
  335. c.direct.health.SetAuthRoutineInError(nil)
  336. c.mu.Lock()
  337. c.urlToVisit = ""
  338. c.loggedIn = true
  339. c.loginGoal = nil
  340. c.state = StateAuthenticated
  341. c.mu.Unlock()
  342. c.sendStatus("authRoutine-success", nil, "", nil)
  343. c.restartMap()
  344. bo.BackOff(ctx, nil)
  345. }
  346. }
  347. // ExpiryForTests returns the credential expiration time, or the zero value if
  348. // the expiration time isn't known. It's used in tests only.
  349. func (c *Auto) ExpiryForTests() time.Time {
  350. c.mu.Lock()
  351. defer c.mu.Unlock()
  352. return c.expiry
  353. }
  354. // DirectForTest returns the underlying direct client object.
  355. // It's used in tests only.
  356. func (c *Auto) DirectForTest() *Direct {
  357. return c.direct
  358. }
  359. // unpausedChanLocked returns a new channel that gets sent
  360. // either a true when unpaused or false on Auto.Shutdown.
  361. //
  362. // c.mu must be held
  363. func (c *Auto) unpausedChanLocked() <-chan bool {
  364. unpaused := make(chan bool, 1)
  365. c.unpauseWaiters = append(c.unpauseWaiters, unpaused)
  366. return unpaused
  367. }
  368. // mapRoutineState is the state of Auto.mapRoutine while it's running.
  369. type mapRoutineState struct {
  370. c *Auto
  371. bo *backoff.Backoff
  372. }
  373. var _ NetmapDeltaUpdater = mapRoutineState{}
  374. func (mrs mapRoutineState) UpdateFullNetmap(nm *netmap.NetworkMap) {
  375. c := mrs.c
  376. c.mu.Lock()
  377. ctx := c.mapCtx
  378. c.inMapPoll = true
  379. if c.loggedIn {
  380. c.state = StateSynchronized
  381. }
  382. c.expiry = nm.Expiry
  383. stillAuthed := c.loggedIn
  384. c.logf("[v1] mapRoutine: netmap received: %s", c.state)
  385. c.mu.Unlock()
  386. if stillAuthed {
  387. c.sendStatus("mapRoutine-got-netmap", nil, "", nm)
  388. }
  389. // Reset the backoff timer if we got a netmap.
  390. mrs.bo.BackOff(ctx, nil)
  391. }
  392. func (mrs mapRoutineState) UpdateNetmapDelta(muts []netmap.NodeMutation) bool {
  393. c := mrs.c
  394. c.mu.Lock()
  395. goodState := c.loggedIn && c.inMapPoll
  396. ndu, canDelta := c.observer.(NetmapDeltaUpdater)
  397. c.mu.Unlock()
  398. if !goodState || !canDelta {
  399. return false
  400. }
  401. ctx, cancel := context.WithTimeout(c.mapCtx, 2*time.Second)
  402. defer cancel()
  403. var ok bool
  404. err := c.observerQueue.RunSync(ctx, func() {
  405. ok = ndu.UpdateNetmapDelta(muts)
  406. })
  407. return err == nil && ok
  408. }
  409. // mapRoutine is responsible for keeping a read-only streaming connection to the
  410. // control server, and keeping the netmap up to date.
  411. func (c *Auto) mapRoutine() {
  412. defer close(c.mapDone)
  413. mrs := mapRoutineState{
  414. c: c,
  415. bo: backoff.NewBackoff("mapRoutine", c.logf, 30*time.Second),
  416. }
  417. for {
  418. if !c.waitUnpause("mapRoutine") {
  419. c.logf("mapRoutine: exiting")
  420. return
  421. }
  422. c.mu.Lock()
  423. c.logf("[v1] mapRoutine: %s", c.state)
  424. loggedIn := c.loggedIn
  425. ctx := c.mapCtx
  426. c.mu.Unlock()
  427. report := func(err error, msg string) {
  428. c.logf("[v1] %s: %v", msg, err)
  429. err = fmt.Errorf("%s: %w", msg, err)
  430. // don't send status updates for context errors,
  431. // since context cancelation is always on purpose.
  432. if ctx.Err() == nil {
  433. c.sendStatus("mapRoutine1", err, "", nil)
  434. }
  435. }
  436. if !loggedIn {
  437. // Wait for something interesting to happen
  438. c.mu.Lock()
  439. c.inMapPoll = false
  440. c.mu.Unlock()
  441. <-ctx.Done()
  442. c.logf("[v1] mapRoutine: context done.")
  443. continue
  444. }
  445. c.direct.health.SetOutOfPollNetMap()
  446. err := c.direct.PollNetMap(ctx, mrs)
  447. c.direct.health.SetOutOfPollNetMap()
  448. c.mu.Lock()
  449. c.inMapPoll = false
  450. if c.state == StateSynchronized {
  451. c.state = StateAuthenticated
  452. }
  453. paused := c.paused
  454. c.mu.Unlock()
  455. if paused {
  456. mrs.bo.BackOff(ctx, nil)
  457. c.logf("mapRoutine: paused")
  458. } else {
  459. mrs.bo.BackOff(ctx, err)
  460. report(err, "PollNetMap")
  461. }
  462. }
  463. }
  464. func (c *Auto) AuthCantContinue() bool {
  465. if c == nil {
  466. return true
  467. }
  468. c.mu.Lock()
  469. defer c.mu.Unlock()
  470. return !c.loggedIn && (c.loginGoal == nil || c.loginGoal.url != "")
  471. }
  472. func (c *Auto) SetHostinfo(hi *tailcfg.Hostinfo) {
  473. if hi == nil {
  474. panic("nil Hostinfo")
  475. }
  476. if !c.direct.SetHostinfo(hi) {
  477. // No changes. Don't log.
  478. return
  479. }
  480. // Send new Hostinfo to server
  481. c.updateControl()
  482. }
  483. func (c *Auto) SetNetInfo(ni *tailcfg.NetInfo) {
  484. if ni == nil {
  485. panic("nil NetInfo")
  486. }
  487. if !c.direct.SetNetInfo(ni) {
  488. return
  489. }
  490. // Send new NetInfo to server
  491. c.updateControl()
  492. }
  493. // SetTKAHead updates the TKA head hash that map-request infrastructure sends.
  494. func (c *Auto) SetTKAHead(headHash string) {
  495. if !c.direct.SetTKAHead(headHash) {
  496. return
  497. }
  498. // Send new TKAHead to server
  499. c.updateControl()
  500. }
  501. // sendStatus can not be called with the c.mu held.
  502. func (c *Auto) sendStatus(who string, err error, url string, nm *netmap.NetworkMap) {
  503. c.mu.Lock()
  504. if c.closed {
  505. c.mu.Unlock()
  506. return
  507. }
  508. state := c.state
  509. loggedIn := c.loggedIn
  510. inMapPoll := c.inMapPoll
  511. c.mu.Unlock()
  512. c.logf("[v1] sendStatus: %s: %v", who, state)
  513. var p persist.PersistView
  514. if nm != nil && loggedIn && inMapPoll {
  515. p = c.direct.GetPersist()
  516. } else {
  517. // don't send netmap status, as it's misleading when we're
  518. // not logged in.
  519. nm = nil
  520. }
  521. new := Status{
  522. URL: url,
  523. Persist: p,
  524. NetMap: nm,
  525. Err: err,
  526. state: state,
  527. }
  528. // Launch a new goroutine to avoid blocking the caller while the observer
  529. // does its thing, which may result in a call back into the client.
  530. c.observerQueue.Add(func() {
  531. c.observer.SetControlClientStatus(c, new)
  532. })
  533. }
  534. func (c *Auto) Login(t *tailcfg.Oauth2Token, flags LoginFlags) {
  535. c.logf("client.Login(%v, %v)", t != nil, flags)
  536. c.mu.Lock()
  537. defer c.mu.Unlock()
  538. if c.closed {
  539. return
  540. }
  541. if c.direct != nil && c.direct.panicOnUse {
  542. panic("tainted client")
  543. }
  544. c.wantLoggedIn = true
  545. c.loginGoal = &LoginGoal{
  546. token: t,
  547. flags: flags,
  548. }
  549. c.cancelMapCtxLocked()
  550. c.cancelAuthCtxLocked()
  551. }
  552. var ErrClientClosed = errors.New("client closed")
  553. func (c *Auto) Logout(ctx context.Context) error {
  554. c.logf("client.Logout()")
  555. c.mu.Lock()
  556. c.wantLoggedIn = false
  557. c.loginGoal = nil
  558. closed := c.closed
  559. if c.direct != nil && c.direct.panicOnUse {
  560. panic("tainted client")
  561. }
  562. c.mu.Unlock()
  563. if closed {
  564. return ErrClientClosed
  565. }
  566. if err := c.direct.TryLogout(ctx); err != nil {
  567. return err
  568. }
  569. c.mu.Lock()
  570. c.loggedIn = false
  571. c.state = StateNotAuthenticated
  572. c.cancelAuthCtxLocked()
  573. c.cancelMapCtxLocked()
  574. c.mu.Unlock()
  575. c.sendStatus("authRoutine-wantout", nil, "", nil)
  576. return nil
  577. }
  578. func (c *Auto) SetExpirySooner(ctx context.Context, expiry time.Time) error {
  579. return c.direct.SetExpirySooner(ctx, expiry)
  580. }
  581. // UpdateEndpoints sets the client's discovered endpoints and sends
  582. // them to the control server if they've changed.
  583. //
  584. // It does not retain the provided slice.
  585. func (c *Auto) UpdateEndpoints(endpoints []tailcfg.Endpoint) {
  586. changed := c.direct.SetEndpoints(endpoints)
  587. if changed {
  588. c.updateControl()
  589. }
  590. }
  591. func (c *Auto) Shutdown() {
  592. c.mu.Lock()
  593. if c.closed {
  594. c.mu.Unlock()
  595. return
  596. }
  597. c.logf("client.Shutdown ...")
  598. direct := c.direct
  599. c.closed = true
  600. c.observerQueue.Shutdown()
  601. c.cancelAuthCtxLocked()
  602. c.cancelMapCtxLocked()
  603. for _, w := range c.unpauseWaiters {
  604. w <- false
  605. }
  606. c.unpauseWaiters = nil
  607. c.mu.Unlock()
  608. c.unregisterHealthWatch()
  609. <-c.authDone
  610. <-c.mapDone
  611. <-c.updateDone
  612. if direct != nil {
  613. direct.Close()
  614. }
  615. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  616. defer cancel()
  617. c.observerQueue.Wait(ctx)
  618. c.logf("Client.Shutdown done.")
  619. }
  620. // NodePublicKey returns the node public key currently in use. This is
  621. // used exclusively in tests.
  622. func (c *Auto) TestOnlyNodePublicKey() key.NodePublic {
  623. priv := c.direct.GetPersist()
  624. return priv.PrivateNodeKey().Public()
  625. }
  626. func (c *Auto) TestOnlySetAuthKey(authkey string) {
  627. c.direct.mu.Lock()
  628. defer c.direct.mu.Unlock()
  629. c.direct.authKey = authkey
  630. }
  631. func (c *Auto) TestOnlyTimeNow() time.Time {
  632. return c.clock.Now()
  633. }
  634. // SetDNS sends the SetDNSRequest request to the control plane server,
  635. // requesting a DNS record be created or updated.
  636. func (c *Auto) SetDNS(ctx context.Context, req *tailcfg.SetDNSRequest) error {
  637. return c.direct.SetDNS(ctx, req)
  638. }
  639. func (c *Auto) DoNoiseRequest(req *http.Request) (*http.Response, error) {
  640. return c.direct.DoNoiseRequest(req)
  641. }
  642. // GetSingleUseNoiseRoundTripper returns a RoundTripper that can be only be used
  643. // once (and must be used once) to make a single HTTP request over the noise
  644. // channel to the coordination server.
  645. //
  646. // In addition to the RoundTripper, it returns the HTTP/2 channel's early noise
  647. // payload, if any.
  648. func (c *Auto) GetSingleUseNoiseRoundTripper(ctx context.Context) (http.RoundTripper, *tailcfg.EarlyNoise, error) {
  649. return c.direct.GetSingleUseNoiseRoundTripper(ctx)
  650. }