client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package controlclient implements the client for the Tailscale
  5. // control plane.
  6. //
  7. // It handles authentication, port picking, and collects the local
  8. // network configuration.
  9. package controlclient
  10. import (
  11. "context"
  12. "tailscale.com/tailcfg"
  13. )
  14. type LoginFlags int
  15. const (
  16. LoginDefault = LoginFlags(0)
  17. LoginInteractive = LoginFlags(1 << iota) // force user login and key refresh
  18. LoginEphemeral // set RegisterRequest.Ephemeral
  19. )
  20. // Client represents a client connection to the control server.
  21. // Currently this is done through a pair of polling https requests in
  22. // the Auto client, but that might change eventually.
  23. type Client interface {
  24. // SetStatusFunc provides a callback to call when control sends us
  25. // a message.
  26. SetStatusFunc(func(Status))
  27. // Shutdown closes this session, which should not be used any further
  28. // afterwards.
  29. Shutdown()
  30. // Login begins an interactive or non-interactive login process.
  31. // Client will eventually call the Status callback with either a
  32. // LoginFinished flag (on success) or an auth URL (if further
  33. // interaction is needed).
  34. Login(*tailcfg.Oauth2Token, LoginFlags)
  35. // StartLogout starts an asynchronous logout process.
  36. // When it finishes, the Status callback will be called while
  37. // AuthCantContinue()==true.
  38. StartLogout()
  39. // Logout starts a synchronous logout process. It doesn't return
  40. // until the logout operation has been completed.
  41. Logout(context.Context) error
  42. // SetPaused pauses or unpauses the controlclient activity as much
  43. // as possible, without losing its internal state, to minimize
  44. // unnecessary network activity.
  45. // TODO: It might be better to simply shutdown the controlclient and
  46. // make a new one when it's time to unpause.
  47. SetPaused(bool)
  48. // AuthCantContinue returns whether authentication is blocked. If it
  49. // is, you either need to visit the auth URL (previously sent in a
  50. // Status callback) or call the Login function appropriately.
  51. // TODO: this probably belongs in the Status itself instead.
  52. AuthCantContinue() bool
  53. // SetHostinfo changes the Hostinfo structure that will be sent in
  54. // subsequent node registration requests.
  55. // TODO: a server-side change would let us simply upload this
  56. // in a separate http request. It has nothing to do with the rest of
  57. // the state machine.
  58. SetHostinfo(*tailcfg.Hostinfo)
  59. // SetNetinfo changes the NetIinfo structure that will be sent in
  60. // subsequent node registration requests.
  61. // TODO: a server-side change would let us simply upload this
  62. // in a separate http request. It has nothing to do with the rest of
  63. // the state machine.
  64. SetNetInfo(*tailcfg.NetInfo)
  65. // UpdateEndpoints changes the Endpoint structure that will be sent
  66. // in subsequent node registration requests.
  67. // The localPort field is unused except for integration tests in another repo.
  68. // TODO: a server-side change would let us simply upload this
  69. // in a separate http request. It has nothing to do with the rest of
  70. // the state machine.
  71. UpdateEndpoints(localPort uint16, endpoints []tailcfg.Endpoint)
  72. // SetDNS sends the SetDNSRequest request to the control plane server,
  73. // requesting a DNS record be created or updated.
  74. SetDNS(context.Context, *tailcfg.SetDNSRequest) error
  75. }
  76. // UserVisibleError is an error that should be shown to users.
  77. type UserVisibleError string
  78. func (e UserVisibleError) Error() string { return string(e) }
  79. func (e UserVisibleError) UserVisibleError() string { return string(e) }