direct.go 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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
  5. import (
  6. "bufio"
  7. "bytes"
  8. "context"
  9. "encoding/binary"
  10. "encoding/json"
  11. "errors"
  12. "flag"
  13. "fmt"
  14. "io"
  15. "io/ioutil"
  16. "log"
  17. "net/http"
  18. "net/http/httptest"
  19. "net/netip"
  20. "net/url"
  21. "os"
  22. "reflect"
  23. "runtime"
  24. "strings"
  25. "sync"
  26. "time"
  27. "go4.org/mem"
  28. "tailscale.com/control/controlknobs"
  29. "tailscale.com/envknob"
  30. "tailscale.com/health"
  31. "tailscale.com/hostinfo"
  32. "tailscale.com/ipn/ipnstate"
  33. "tailscale.com/log/logheap"
  34. "tailscale.com/logtail"
  35. "tailscale.com/net/dnscache"
  36. "tailscale.com/net/dnsfallback"
  37. "tailscale.com/net/interfaces"
  38. "tailscale.com/net/netutil"
  39. "tailscale.com/net/tlsdial"
  40. "tailscale.com/net/tsdial"
  41. "tailscale.com/net/tshttpproxy"
  42. "tailscale.com/syncs"
  43. "tailscale.com/tailcfg"
  44. "tailscale.com/types/key"
  45. "tailscale.com/types/logger"
  46. "tailscale.com/types/netmap"
  47. "tailscale.com/types/opt"
  48. "tailscale.com/types/persist"
  49. "tailscale.com/util/clientmetric"
  50. "tailscale.com/util/multierr"
  51. "tailscale.com/util/singleflight"
  52. "tailscale.com/util/systemd"
  53. "tailscale.com/wgengine/monitor"
  54. )
  55. // Direct is the client that connects to a tailcontrol server for a node.
  56. type Direct struct {
  57. httpc *http.Client // HTTP client used to talk to tailcontrol
  58. dialer *tsdial.Dialer
  59. serverURL string // URL of the tailcontrol server
  60. timeNow func() time.Time
  61. lastPrintMap time.Time
  62. newDecompressor func() (Decompressor, error)
  63. keepAlive bool
  64. logf logger.Logf
  65. linkMon *monitor.Mon // or nil
  66. discoPubKey key.DiscoPublic
  67. getMachinePrivKey func() (key.MachinePrivate, error)
  68. getNLPublicKey func() (key.NLPublic, error) // or nil
  69. debugFlags []string
  70. keepSharerAndUserSplit bool
  71. skipIPForwardingCheck bool
  72. pinger Pinger
  73. popBrowser func(url string) // or nil
  74. c2nHandler http.Handler // or nil
  75. mu sync.Mutex // mutex guards the following fields
  76. serverKey key.MachinePublic // original ("legacy") nacl crypto_box-based public key
  77. serverNoiseKey key.MachinePublic
  78. sfGroup singleflight.Group[struct{}, *noiseClient] // protects noiseClient creation.
  79. noiseClient *noiseClient
  80. persist persist.Persist
  81. authKey string
  82. tryingNewKey key.NodePrivate
  83. expiry *time.Time
  84. hostinfo *tailcfg.Hostinfo // always non-nil
  85. netinfo *tailcfg.NetInfo
  86. endpoints []tailcfg.Endpoint
  87. everEndpoints bool // whether we've ever had non-empty endpoints
  88. lastPingURL string // last PingRequest.URL received, for dup suppression
  89. }
  90. type Options struct {
  91. Persist persist.Persist // initial persistent data
  92. GetMachinePrivateKey func() (key.MachinePrivate, error) // returns the machine key to use
  93. ServerURL string // URL of the tailcontrol server
  94. AuthKey string // optional node auth key for auto registration
  95. TimeNow func() time.Time // time.Now implementation used by Client
  96. Hostinfo *tailcfg.Hostinfo // non-nil passes ownership, nil means to use default using os.Hostname, etc
  97. DiscoPublicKey key.DiscoPublic
  98. NewDecompressor func() (Decompressor, error)
  99. KeepAlive bool
  100. Logf logger.Logf
  101. HTTPTestClient *http.Client // optional HTTP client to use (for tests only)
  102. DebugFlags []string // debug settings to send to control
  103. LinkMonitor *monitor.Mon // optional link monitor
  104. PopBrowserURL func(url string) // optional func to open browser
  105. Dialer *tsdial.Dialer // non-nil
  106. C2NHandler http.Handler // or nil
  107. // GetNLPublicKey specifies an optional function to use
  108. // Network Lock. If nil, it's not used.
  109. GetNLPublicKey func() (key.NLPublic, error)
  110. // Status is called when there's a change in status.
  111. Status func(Status)
  112. // KeepSharerAndUserSplit controls whether the client
  113. // understands Node.Sharer. If false, the Sharer is mapped to the User.
  114. KeepSharerAndUserSplit bool
  115. // SkipIPForwardingCheck declares that the host's IP
  116. // forwarding works and should not be double-checked by the
  117. // controlclient package.
  118. SkipIPForwardingCheck bool
  119. // Pinger optionally specifies the Pinger to use to satisfy
  120. // MapResponse.PingRequest queries from the control plane.
  121. // If nil, PingRequest queries are not answered.
  122. Pinger Pinger
  123. }
  124. // Pinger is the LocalBackend.Ping method.
  125. type Pinger interface {
  126. // Ping is a request to do a ping with the peer handling the given IP.
  127. Ping(ctx context.Context, ip netip.Addr, pingType tailcfg.PingType) (*ipnstate.PingResult, error)
  128. }
  129. type Decompressor interface {
  130. DecodeAll(input, dst []byte) ([]byte, error)
  131. Close()
  132. }
  133. // NewDirect returns a new Direct client.
  134. func NewDirect(opts Options) (*Direct, error) {
  135. if opts.ServerURL == "" {
  136. return nil, errors.New("controlclient.New: no server URL specified")
  137. }
  138. if opts.GetMachinePrivateKey == nil {
  139. return nil, errors.New("controlclient.New: no GetMachinePrivateKey specified")
  140. }
  141. opts.ServerURL = strings.TrimRight(opts.ServerURL, "/")
  142. serverURL, err := url.Parse(opts.ServerURL)
  143. if err != nil {
  144. return nil, err
  145. }
  146. if opts.TimeNow == nil {
  147. opts.TimeNow = time.Now
  148. }
  149. if opts.Logf == nil {
  150. // TODO(apenwarr): remove this default and fail instead.
  151. // TODO(bradfitz): ... but then it shouldn't be in Options.
  152. opts.Logf = log.Printf
  153. }
  154. httpc := opts.HTTPTestClient
  155. if httpc == nil && runtime.GOOS == "js" {
  156. // In js/wasm, net/http.Transport (as of Go 1.18) will
  157. // only use the browser's Fetch API if you're using
  158. // the DefaultClient (or a client without dial hooks
  159. // etc set).
  160. httpc = http.DefaultClient
  161. }
  162. if httpc == nil {
  163. dnsCache := &dnscache.Resolver{
  164. Forward: dnscache.Get().Forward, // use default cache's forwarder
  165. UseLastGood: true,
  166. LookupIPFallback: dnsfallback.Lookup,
  167. }
  168. tr := http.DefaultTransport.(*http.Transport).Clone()
  169. tr.Proxy = tshttpproxy.ProxyFromEnvironment
  170. tshttpproxy.SetTransportGetProxyConnectHeader(tr)
  171. tr.TLSClientConfig = tlsdial.Config(serverURL.Hostname(), tr.TLSClientConfig)
  172. tr.DialContext = dnscache.Dialer(opts.Dialer.SystemDial, dnsCache)
  173. tr.DialTLSContext = dnscache.TLSDialer(opts.Dialer.SystemDial, dnsCache, tr.TLSClientConfig)
  174. tr.ForceAttemptHTTP2 = true
  175. // Disable implicit gzip compression; the various
  176. // handlers (register, map, set-dns, etc) do their own
  177. // zstd compression per naclbox.
  178. tr.DisableCompression = true
  179. httpc = &http.Client{Transport: tr}
  180. }
  181. c := &Direct{
  182. httpc: httpc,
  183. getMachinePrivKey: opts.GetMachinePrivateKey,
  184. getNLPublicKey: opts.GetNLPublicKey,
  185. serverURL: opts.ServerURL,
  186. timeNow: opts.TimeNow,
  187. logf: opts.Logf,
  188. newDecompressor: opts.NewDecompressor,
  189. keepAlive: opts.KeepAlive,
  190. persist: opts.Persist,
  191. authKey: opts.AuthKey,
  192. discoPubKey: opts.DiscoPublicKey,
  193. debugFlags: opts.DebugFlags,
  194. keepSharerAndUserSplit: opts.KeepSharerAndUserSplit,
  195. linkMon: opts.LinkMonitor,
  196. skipIPForwardingCheck: opts.SkipIPForwardingCheck,
  197. pinger: opts.Pinger,
  198. popBrowser: opts.PopBrowserURL,
  199. c2nHandler: opts.C2NHandler,
  200. dialer: opts.Dialer,
  201. }
  202. if opts.Hostinfo == nil {
  203. c.SetHostinfo(hostinfo.New())
  204. } else {
  205. ni := opts.Hostinfo.NetInfo
  206. opts.Hostinfo.NetInfo = nil
  207. c.SetHostinfo(opts.Hostinfo)
  208. if ni != nil {
  209. c.SetNetInfo(ni)
  210. }
  211. }
  212. return c, nil
  213. }
  214. // Close closes the underlying Noise connection(s).
  215. func (c *Direct) Close() error {
  216. c.mu.Lock()
  217. defer c.mu.Unlock()
  218. if c.noiseClient != nil {
  219. if err := c.noiseClient.Close(); err != nil {
  220. return err
  221. }
  222. }
  223. c.noiseClient = nil
  224. return nil
  225. }
  226. // SetHostinfo clones the provided Hostinfo and remembers it for the
  227. // next update. It reports whether the Hostinfo has changed.
  228. func (c *Direct) SetHostinfo(hi *tailcfg.Hostinfo) bool {
  229. if hi == nil {
  230. panic("nil Hostinfo")
  231. }
  232. c.mu.Lock()
  233. defer c.mu.Unlock()
  234. if hi.Equal(c.hostinfo) {
  235. return false
  236. }
  237. c.hostinfo = hi.Clone()
  238. j, _ := json.Marshal(c.hostinfo)
  239. c.logf("[v1] HostInfo: %s", j)
  240. return true
  241. }
  242. // SetNetInfo clones the provided NetInfo and remembers it for the
  243. // next update. It reports whether the NetInfo has changed.
  244. func (c *Direct) SetNetInfo(ni *tailcfg.NetInfo) bool {
  245. if ni == nil {
  246. panic("nil NetInfo")
  247. }
  248. c.mu.Lock()
  249. defer c.mu.Unlock()
  250. if reflect.DeepEqual(ni, c.netinfo) {
  251. return false
  252. }
  253. c.netinfo = ni.Clone()
  254. c.logf("NetInfo: %v", ni)
  255. return true
  256. }
  257. func (c *Direct) GetPersist() persist.Persist {
  258. c.mu.Lock()
  259. defer c.mu.Unlock()
  260. return c.persist
  261. }
  262. func (c *Direct) TryLogout(ctx context.Context) error {
  263. c.logf("[v1] direct.TryLogout()")
  264. mustRegen, newURL, err := c.doLogin(ctx, loginOpt{Logout: true})
  265. c.logf("[v1] TryLogout control response: mustRegen=%v, newURL=%v, err=%v", mustRegen, newURL, err)
  266. c.mu.Lock()
  267. c.persist = persist.Persist{}
  268. c.mu.Unlock()
  269. return err
  270. }
  271. func (c *Direct) TryLogin(ctx context.Context, t *tailcfg.Oauth2Token, flags LoginFlags) (url string, err error) {
  272. c.logf("[v1] direct.TryLogin(token=%v, flags=%v)", t != nil, flags)
  273. return c.doLoginOrRegen(ctx, loginOpt{Token: t, Flags: flags})
  274. }
  275. // WaitLoginURL sits in a long poll waiting for the user to authenticate at url.
  276. //
  277. // On success, newURL and err will both be nil.
  278. func (c *Direct) WaitLoginURL(ctx context.Context, url string) (newURL string, err error) {
  279. c.logf("[v1] direct.WaitLoginURL")
  280. return c.doLoginOrRegen(ctx, loginOpt{URL: url})
  281. }
  282. func (c *Direct) doLoginOrRegen(ctx context.Context, opt loginOpt) (newURL string, err error) {
  283. mustRegen, url, err := c.doLogin(ctx, opt)
  284. if err != nil {
  285. return url, err
  286. }
  287. if mustRegen {
  288. opt.Regen = true
  289. _, url, err = c.doLogin(ctx, opt)
  290. }
  291. return url, err
  292. }
  293. // SetExpirySooner attempts to shorten the expiry to the specified time.
  294. func (c *Direct) SetExpirySooner(ctx context.Context, expiry time.Time) error {
  295. c.logf("[v1] direct.SetExpirySooner()")
  296. newURL, err := c.doLoginOrRegen(ctx, loginOpt{Expiry: &expiry})
  297. c.logf("[v1] SetExpirySooner control response: newURL=%v, err=%v", newURL, err)
  298. return err
  299. }
  300. type loginOpt struct {
  301. Token *tailcfg.Oauth2Token
  302. Flags LoginFlags
  303. Regen bool // generate a new nodekey, can be overridden in doLogin
  304. URL string
  305. Logout bool // set the expiry to the far past, expiring the node
  306. // Expiry, if non-nil, attempts to set the node expiry to the
  307. // specified time and cannot be used to extend the expiry.
  308. // It is ignored if Logout is set since Logout works by setting a
  309. // expiry time in the far past.
  310. Expiry *time.Time
  311. }
  312. // httpClient provides a common interface for the noiseClient and
  313. // the NaCl box http.Client.
  314. type httpClient interface {
  315. Do(req *http.Request) (*http.Response, error)
  316. }
  317. // hostInfoLocked returns a Clone of c.hostinfo and c.netinfo.
  318. // It must only be called with c.mu held.
  319. func (c *Direct) hostInfoLocked() *tailcfg.Hostinfo {
  320. hi := c.hostinfo.Clone()
  321. hi.NetInfo = c.netinfo.Clone()
  322. return hi
  323. }
  324. func (c *Direct) doLogin(ctx context.Context, opt loginOpt) (mustRegen bool, newURL string, err error) {
  325. c.mu.Lock()
  326. persist := c.persist
  327. tryingNewKey := c.tryingNewKey
  328. serverKey := c.serverKey
  329. serverNoiseKey := c.serverNoiseKey
  330. authKey := c.authKey
  331. hi := c.hostInfoLocked()
  332. backendLogID := hi.BackendLogID
  333. expired := c.expiry != nil && !c.expiry.IsZero() && c.expiry.Before(c.timeNow())
  334. c.mu.Unlock()
  335. machinePrivKey, err := c.getMachinePrivKey()
  336. if err != nil {
  337. return false, "", fmt.Errorf("getMachinePrivKey: %w", err)
  338. }
  339. if machinePrivKey.IsZero() {
  340. return false, "", errors.New("getMachinePrivKey returned zero key")
  341. }
  342. regen := opt.Regen
  343. if opt.Logout {
  344. c.logf("logging out...")
  345. } else {
  346. if expired {
  347. c.logf("Old key expired -> regen=true")
  348. systemd.Status("key expired; run 'tailscale up' to authenticate")
  349. regen = true
  350. }
  351. if (opt.Flags & LoginInteractive) != 0 {
  352. c.logf("LoginInteractive -> regen=true")
  353. regen = true
  354. }
  355. }
  356. c.logf("doLogin(regen=%v, hasUrl=%v)", regen, opt.URL != "")
  357. if serverKey.IsZero() {
  358. keys, err := loadServerPubKeys(ctx, c.httpc, c.serverURL)
  359. if err != nil {
  360. return regen, opt.URL, err
  361. }
  362. c.logf("control server key from %s: ts2021=%s, legacy=%v", c.serverURL, keys.PublicKey.ShortString(), keys.LegacyPublicKey.ShortString())
  363. c.mu.Lock()
  364. c.serverKey = keys.LegacyPublicKey
  365. c.serverNoiseKey = keys.PublicKey
  366. c.mu.Unlock()
  367. serverKey = keys.LegacyPublicKey
  368. serverNoiseKey = keys.PublicKey
  369. // For servers supporting the Noise transport,
  370. // proactively shut down our TLS TCP connection.
  371. // We're not going to need it and it's nicer to the
  372. // server.
  373. if !serverNoiseKey.IsZero() {
  374. c.httpc.CloseIdleConnections()
  375. }
  376. }
  377. var oldNodeKey key.NodePublic
  378. switch {
  379. case opt.Logout:
  380. tryingNewKey = persist.PrivateNodeKey
  381. case opt.URL != "":
  382. // Nothing.
  383. case regen || persist.PrivateNodeKey.IsZero():
  384. c.logf("Generating a new nodekey.")
  385. persist.OldPrivateNodeKey = persist.PrivateNodeKey
  386. tryingNewKey = key.NewNode()
  387. default:
  388. // Try refreshing the current key first
  389. tryingNewKey = persist.PrivateNodeKey
  390. }
  391. if !persist.OldPrivateNodeKey.IsZero() {
  392. oldNodeKey = persist.OldPrivateNodeKey.Public()
  393. }
  394. var nlPub key.NLPublic
  395. if c.getNLPublicKey != nil {
  396. nlPub, err = c.getNLPublicKey()
  397. if err != nil {
  398. return false, "", fmt.Errorf("get nl key: %v", err)
  399. }
  400. }
  401. if tryingNewKey.IsZero() {
  402. if opt.Logout {
  403. return false, "", errors.New("no nodekey to log out")
  404. }
  405. log.Fatalf("tryingNewKey is empty, give up")
  406. }
  407. if backendLogID == "" {
  408. err = errors.New("hostinfo: BackendLogID missing")
  409. return regen, opt.URL, err
  410. }
  411. now := time.Now().Round(time.Second)
  412. request := tailcfg.RegisterRequest{
  413. Version: 1,
  414. OldNodeKey: oldNodeKey,
  415. NodeKey: tryingNewKey.Public(),
  416. NLKey: nlPub,
  417. Hostinfo: hi,
  418. Followup: opt.URL,
  419. Timestamp: &now,
  420. Ephemeral: (opt.Flags & LoginEphemeral) != 0,
  421. }
  422. if opt.Logout {
  423. request.Expiry = time.Unix(123, 0) // far in the past
  424. } else if opt.Expiry != nil {
  425. request.Expiry = *opt.Expiry
  426. }
  427. c.logf("RegisterReq: onode=%v node=%v fup=%v",
  428. request.OldNodeKey.ShortString(),
  429. request.NodeKey.ShortString(), opt.URL != "")
  430. request.Auth.Oauth2Token = opt.Token
  431. request.Auth.Provider = persist.Provider
  432. request.Auth.LoginName = persist.LoginName
  433. request.Auth.AuthKey = authKey
  434. err = signRegisterRequest(&request, c.serverURL, c.serverKey, machinePrivKey.Public())
  435. if err != nil {
  436. // If signing failed, clear all related fields
  437. request.SignatureType = tailcfg.SignatureNone
  438. request.Timestamp = nil
  439. request.DeviceCert = nil
  440. request.Signature = nil
  441. // Don't log the common error types. Signatures are not usually enabled,
  442. // so these are expected.
  443. if !errors.Is(err, errCertificateNotConfigured) && !errors.Is(err, errNoCertStore) {
  444. c.logf("RegisterReq sign error: %v", err)
  445. }
  446. }
  447. if debugRegister {
  448. j, _ := json.MarshalIndent(request, "", "\t")
  449. c.logf("RegisterRequest: %s", j)
  450. }
  451. // URL and httpc are protocol specific.
  452. var url string
  453. var httpc httpClient
  454. if serverNoiseKey.IsZero() {
  455. httpc = c.httpc
  456. url = fmt.Sprintf("%s/machine/%s", c.serverURL, machinePrivKey.Public().UntypedHexString())
  457. } else {
  458. request.Version = tailcfg.CurrentCapabilityVersion
  459. httpc, err = c.getNoiseClient()
  460. if err != nil {
  461. return regen, opt.URL, fmt.Errorf("getNoiseClient: %w", err)
  462. }
  463. url = fmt.Sprintf("%s/machine/register", c.serverURL)
  464. url = strings.Replace(url, "http:", "https:", 1)
  465. }
  466. bodyData, err := encode(request, serverKey, serverNoiseKey, machinePrivKey)
  467. if err != nil {
  468. return regen, opt.URL, err
  469. }
  470. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(bodyData))
  471. if err != nil {
  472. return regen, opt.URL, err
  473. }
  474. res, err := httpc.Do(req)
  475. if err != nil {
  476. return regen, opt.URL, fmt.Errorf("register request: %w", err)
  477. }
  478. if res.StatusCode != 200 {
  479. msg, _ := ioutil.ReadAll(res.Body)
  480. res.Body.Close()
  481. return regen, opt.URL, fmt.Errorf("register request: http %d: %.200s",
  482. res.StatusCode, strings.TrimSpace(string(msg)))
  483. }
  484. resp := tailcfg.RegisterResponse{}
  485. if err := decode(res, &resp, serverKey, serverNoiseKey, machinePrivKey); err != nil {
  486. c.logf("error decoding RegisterResponse with server key %s and machine key %s: %v", serverKey, machinePrivKey.Public(), err)
  487. return regen, opt.URL, fmt.Errorf("register request: %v", err)
  488. }
  489. if debugRegister {
  490. j, _ := json.MarshalIndent(resp, "", "\t")
  491. c.logf("RegisterResponse: %s", j)
  492. }
  493. // Log without PII:
  494. c.logf("RegisterReq: got response; nodeKeyExpired=%v, machineAuthorized=%v; authURL=%v",
  495. resp.NodeKeyExpired, resp.MachineAuthorized, resp.AuthURL != "")
  496. if resp.Error != "" {
  497. return false, "", UserVisibleError(resp.Error)
  498. }
  499. if resp.NodeKeyExpired {
  500. if regen {
  501. return true, "", fmt.Errorf("weird: regen=true but server says NodeKeyExpired: %v", request.NodeKey)
  502. }
  503. c.logf("server reports new node key %v has expired",
  504. request.NodeKey.ShortString())
  505. return true, "", nil
  506. }
  507. if resp.Login.Provider != "" {
  508. persist.Provider = resp.Login.Provider
  509. }
  510. if resp.Login.LoginName != "" {
  511. persist.LoginName = resp.Login.LoginName
  512. }
  513. // TODO(crawshaw): RegisterResponse should be able to mechanically
  514. // communicate some extra instructions from the server:
  515. // - new node key required
  516. // - machine key no longer supported
  517. // - user is disabled
  518. if resp.AuthURL != "" {
  519. c.logf("AuthURL is %v", resp.AuthURL)
  520. } else {
  521. c.logf("[v1] No AuthURL")
  522. }
  523. c.mu.Lock()
  524. if resp.AuthURL == "" {
  525. // key rotation is complete
  526. persist.PrivateNodeKey = tryingNewKey
  527. } else {
  528. // save it for the retry-with-URL
  529. c.tryingNewKey = tryingNewKey
  530. }
  531. c.persist = persist
  532. c.mu.Unlock()
  533. if err != nil {
  534. return regen, "", err
  535. }
  536. if ctx.Err() != nil {
  537. return regen, "", ctx.Err()
  538. }
  539. return false, resp.AuthURL, nil
  540. }
  541. func sameEndpoints(a, b []tailcfg.Endpoint) bool {
  542. if len(a) != len(b) {
  543. return false
  544. }
  545. for i := range a {
  546. if a[i] != b[i] {
  547. return false
  548. }
  549. }
  550. return true
  551. }
  552. // newEndpoints acquires c.mu and sets the local port and endpoints and reports
  553. // whether they've changed.
  554. //
  555. // It does not retain the provided slice.
  556. func (c *Direct) newEndpoints(endpoints []tailcfg.Endpoint) (changed bool) {
  557. c.mu.Lock()
  558. defer c.mu.Unlock()
  559. // Nothing new?
  560. if sameEndpoints(c.endpoints, endpoints) {
  561. return false // unchanged
  562. }
  563. var epStrs []string
  564. for _, ep := range endpoints {
  565. epStrs = append(epStrs, ep.Addr.String())
  566. }
  567. c.logf("[v2] client.newEndpoints(%v)", epStrs)
  568. c.endpoints = append(c.endpoints[:0], endpoints...)
  569. if len(endpoints) > 0 {
  570. c.everEndpoints = true
  571. }
  572. return true // changed
  573. }
  574. // SetEndpoints updates the list of locally advertised endpoints.
  575. // It won't be replicated to the server until a *fresh* call to PollNetMap().
  576. // You don't need to restart PollNetMap if we return changed==false.
  577. func (c *Direct) SetEndpoints(endpoints []tailcfg.Endpoint) (changed bool) {
  578. // (no log message on function entry, because it clutters the logs
  579. // if endpoints haven't changed. newEndpoints() will log it.)
  580. return c.newEndpoints(endpoints)
  581. }
  582. func inTest() bool { return flag.Lookup("test.v") != nil }
  583. // PollNetMap makes a /map request to download the network map, calling cb with
  584. // each new netmap.
  585. func (c *Direct) PollNetMap(ctx context.Context, cb func(*netmap.NetworkMap)) error {
  586. return c.sendMapRequest(ctx, -1, false, cb)
  587. }
  588. // FetchNetMap fetches the netmap once.
  589. func (c *Direct) FetchNetMap(ctx context.Context) (*netmap.NetworkMap, error) {
  590. var ret *netmap.NetworkMap
  591. err := c.sendMapRequest(ctx, 1, false, func(nm *netmap.NetworkMap) {
  592. ret = nm
  593. })
  594. if err == nil && ret == nil {
  595. return nil, errors.New("[unexpected] sendMapRequest success without callback")
  596. }
  597. return ret, err
  598. }
  599. // SendLiteMapUpdate makes a /map request to update the server of our latest state,
  600. // but does not fetch anything. It returns an error if the server did not return a
  601. // successful 200 OK response.
  602. func (c *Direct) SendLiteMapUpdate(ctx context.Context) error {
  603. return c.sendMapRequest(ctx, 1, false, nil)
  604. }
  605. // If we go more than pollTimeout without hearing from the server,
  606. // end the long poll. We should be receiving a keep alive ping
  607. // every minute.
  608. const pollTimeout = 120 * time.Second
  609. // cb nil means to omit peers.
  610. func (c *Direct) sendMapRequest(ctx context.Context, maxPolls int, readOnly bool, cb func(*netmap.NetworkMap)) error {
  611. metricMapRequests.Add(1)
  612. metricMapRequestsActive.Add(1)
  613. defer metricMapRequestsActive.Add(-1)
  614. if maxPolls == -1 {
  615. metricMapRequestsPoll.Add(1)
  616. } else {
  617. metricMapRequestsLite.Add(1)
  618. }
  619. c.mu.Lock()
  620. persist := c.persist
  621. serverURL := c.serverURL
  622. serverKey := c.serverKey
  623. serverNoiseKey := c.serverNoiseKey
  624. hi := c.hostInfoLocked()
  625. backendLogID := hi.BackendLogID
  626. var epStrs []string
  627. var epTypes []tailcfg.EndpointType
  628. for _, ep := range c.endpoints {
  629. epStrs = append(epStrs, ep.Addr.String())
  630. epTypes = append(epTypes, ep.Type)
  631. }
  632. everEndpoints := c.everEndpoints
  633. c.mu.Unlock()
  634. machinePrivKey, err := c.getMachinePrivKey()
  635. if err != nil {
  636. return fmt.Errorf("getMachinePrivKey: %w", err)
  637. }
  638. if machinePrivKey.IsZero() {
  639. return errors.New("getMachinePrivKey returned zero key")
  640. }
  641. if persist.PrivateNodeKey.IsZero() {
  642. return errors.New("privateNodeKey is zero")
  643. }
  644. if backendLogID == "" {
  645. return errors.New("hostinfo: BackendLogID missing")
  646. }
  647. allowStream := maxPolls != 1
  648. c.logf("[v1] PollNetMap: stream=%v ep=%v", allowStream, epStrs)
  649. vlogf := logger.Discard
  650. if DevKnob.DumpNetMaps {
  651. // TODO(bradfitz): update this to use "[v2]" prefix perhaps? but we don't
  652. // want to upload it always.
  653. vlogf = c.logf
  654. }
  655. request := &tailcfg.MapRequest{
  656. Version: tailcfg.CurrentCapabilityVersion,
  657. KeepAlive: c.keepAlive,
  658. NodeKey: persist.PrivateNodeKey.Public(),
  659. DiscoKey: c.discoPubKey,
  660. Endpoints: epStrs,
  661. EndpointTypes: epTypes,
  662. Stream: allowStream,
  663. Hostinfo: hi,
  664. DebugFlags: c.debugFlags,
  665. OmitPeers: cb == nil,
  666. // On initial startup before we know our endpoints, set the ReadOnly flag
  667. // to tell the control server not to distribute out our (empty) endpoints to peers.
  668. // Presumably we'll learn our endpoints in a half second and do another post
  669. // with useful results. The first POST just gets us the DERP map which we
  670. // need to do the STUN queries to discover our endpoints.
  671. // TODO(bradfitz): we skip this optimization in tests, though,
  672. // because the e2e tests are currently hyperspecific about the
  673. // ordering of things. The e2e tests need love.
  674. ReadOnly: readOnly || (len(epStrs) == 0 && !everEndpoints && !inTest()),
  675. }
  676. var extraDebugFlags []string
  677. if hi != nil && c.linkMon != nil && !c.skipIPForwardingCheck &&
  678. ipForwardingBroken(hi.RoutableIPs, c.linkMon.InterfaceState()) {
  679. extraDebugFlags = append(extraDebugFlags, "warn-ip-forwarding-off")
  680. }
  681. if health.RouterHealth() != nil {
  682. extraDebugFlags = append(extraDebugFlags, "warn-router-unhealthy")
  683. }
  684. if health.NetworkCategoryHealth() != nil {
  685. extraDebugFlags = append(extraDebugFlags, "warn-network-category-unhealthy")
  686. }
  687. if hostinfo.DisabledEtcAptSource() {
  688. extraDebugFlags = append(extraDebugFlags, "warn-etc-apt-source-disabled")
  689. }
  690. if len(extraDebugFlags) > 0 {
  691. old := request.DebugFlags
  692. request.DebugFlags = append(old[:len(old):len(old)], extraDebugFlags...)
  693. }
  694. if c.newDecompressor != nil {
  695. request.Compress = "zstd"
  696. }
  697. bodyData, err := encode(request, serverKey, serverNoiseKey, machinePrivKey)
  698. if err != nil {
  699. vlogf("netmap: encode: %v", err)
  700. return err
  701. }
  702. ctx, cancel := context.WithCancel(ctx)
  703. defer cancel()
  704. machinePubKey := machinePrivKey.Public()
  705. t0 := time.Now()
  706. // Url and httpc are protocol specific.
  707. var url string
  708. var httpc httpClient
  709. if serverNoiseKey.IsZero() {
  710. httpc = c.httpc
  711. url = fmt.Sprintf("%s/machine/%s/map", serverURL, machinePubKey.UntypedHexString())
  712. } else {
  713. httpc, err = c.getNoiseClient()
  714. if err != nil {
  715. return fmt.Errorf("getNoiseClient: %w", err)
  716. }
  717. url = fmt.Sprintf("%s/machine/map", serverURL)
  718. url = strings.Replace(url, "http:", "https:", 1)
  719. }
  720. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(bodyData))
  721. if err != nil {
  722. return err
  723. }
  724. res, err := httpc.Do(req)
  725. if err != nil {
  726. vlogf("netmap: Do: %v", err)
  727. return err
  728. }
  729. vlogf("netmap: Do = %v after %v", res.StatusCode, time.Since(t0).Round(time.Millisecond))
  730. if res.StatusCode != 200 {
  731. msg, _ := ioutil.ReadAll(res.Body)
  732. res.Body.Close()
  733. return fmt.Errorf("initial fetch failed %d: %.200s",
  734. res.StatusCode, strings.TrimSpace(string(msg)))
  735. }
  736. defer res.Body.Close()
  737. health.NoteMapRequestHeard(request)
  738. if cb == nil {
  739. io.Copy(ioutil.Discard, res.Body)
  740. return nil
  741. }
  742. timeout := time.NewTimer(pollTimeout)
  743. timeoutReset := make(chan struct{})
  744. pollDone := make(chan struct{})
  745. defer close(pollDone)
  746. go func() {
  747. for {
  748. select {
  749. case <-pollDone:
  750. vlogf("netmap: ending timeout goroutine")
  751. return
  752. case <-timeout.C:
  753. c.logf("map response long-poll timed out!")
  754. cancel()
  755. return
  756. case <-timeoutReset:
  757. if !timeout.Stop() {
  758. select {
  759. case <-timeout.C:
  760. case <-pollDone:
  761. vlogf("netmap: ending timeout goroutine")
  762. return
  763. }
  764. }
  765. vlogf("netmap: reset timeout timer")
  766. timeout.Reset(pollTimeout)
  767. }
  768. }
  769. }()
  770. sess := newMapSession(persist.PrivateNodeKey)
  771. sess.logf = c.logf
  772. sess.vlogf = vlogf
  773. sess.machinePubKey = machinePubKey
  774. sess.keepSharerAndUserSplit = c.keepSharerAndUserSplit
  775. // If allowStream, then the server will use an HTTP long poll to
  776. // return incremental results. There is always one response right
  777. // away, followed by a delay, and eventually others.
  778. // If !allowStream, it'll still send the first result in exactly
  779. // the same format before just closing the connection.
  780. // We can use this same read loop either way.
  781. var msg []byte
  782. for i := 0; i < maxPolls || maxPolls < 0; i++ {
  783. vlogf("netmap: starting size read after %v (poll %v)", time.Since(t0).Round(time.Millisecond), i)
  784. var siz [4]byte
  785. if _, err := io.ReadFull(res.Body, siz[:]); err != nil {
  786. vlogf("netmap: size read error after %v: %v", time.Since(t0).Round(time.Millisecond), err)
  787. return err
  788. }
  789. size := binary.LittleEndian.Uint32(siz[:])
  790. vlogf("netmap: read size %v after %v", size, time.Since(t0).Round(time.Millisecond))
  791. msg = append(msg[:0], make([]byte, size)...)
  792. if _, err := io.ReadFull(res.Body, msg); err != nil {
  793. vlogf("netmap: body read error: %v", err)
  794. return err
  795. }
  796. vlogf("netmap: read body after %v", time.Since(t0).Round(time.Millisecond))
  797. var resp tailcfg.MapResponse
  798. if err := c.decodeMsg(msg, &resp, machinePrivKey); err != nil {
  799. vlogf("netmap: decode error: %v")
  800. return err
  801. }
  802. metricMapResponseMessages.Add(1)
  803. if allowStream {
  804. health.GotStreamedMapResponse()
  805. }
  806. if pr := resp.PingRequest; pr != nil && c.isUniquePingRequest(pr) {
  807. metricMapResponsePings.Add(1)
  808. go c.answerPing(pr)
  809. }
  810. if u := resp.PopBrowserURL; u != "" && u != sess.lastPopBrowserURL {
  811. sess.lastPopBrowserURL = u
  812. if c.popBrowser != nil {
  813. c.logf("netmap: control says to open URL %v; opening...", u)
  814. c.popBrowser(u)
  815. } else {
  816. c.logf("netmap: control says to open URL %v; no popBrowser func", u)
  817. }
  818. }
  819. if resp.ControlTime != nil && !resp.ControlTime.IsZero() {
  820. c.logf.JSON(1, "controltime", resp.ControlTime.UTC())
  821. }
  822. if resp.KeepAlive {
  823. vlogf("netmap: got keep-alive")
  824. } else {
  825. vlogf("netmap: got new map")
  826. }
  827. select {
  828. case timeoutReset <- struct{}{}:
  829. vlogf("netmap: sent timer reset")
  830. case <-ctx.Done():
  831. c.logf("[v1] netmap: not resetting timer; context done: %v", ctx.Err())
  832. return ctx.Err()
  833. }
  834. if resp.KeepAlive {
  835. metricMapResponseKeepAlives.Add(1)
  836. continue
  837. }
  838. metricMapResponseMap.Add(1)
  839. if i > 0 {
  840. metricMapResponseMapDelta.Add(1)
  841. }
  842. hasDebug := resp.Debug != nil
  843. // being conservative here, if Debug not present set to False
  844. controlknobs.SetDisableUPnP(hasDebug && resp.Debug.DisableUPnP.EqualBool(true))
  845. if hasDebug {
  846. if code := resp.Debug.Exit; code != nil {
  847. c.logf("exiting process with status %v per controlplane", *code)
  848. os.Exit(*code)
  849. }
  850. if resp.Debug.DisableLogTail {
  851. logtail.Disable()
  852. envknob.SetNoLogsNoSupport()
  853. }
  854. if resp.Debug.LogHeapPprof {
  855. go logheap.LogHeap(resp.Debug.LogHeapURL)
  856. }
  857. if resp.Debug.GoroutineDumpURL != "" {
  858. go dumpGoroutinesToURL(c.httpc, resp.Debug.GoroutineDumpURL)
  859. }
  860. if sleep := time.Duration(resp.Debug.SleepSeconds * float64(time.Second)); sleep > 0 {
  861. if err := sleepAsRequested(ctx, c.logf, timeoutReset, sleep); err != nil {
  862. return err
  863. }
  864. }
  865. }
  866. nm := sess.netmapForResponse(&resp)
  867. if nm.SelfNode == nil {
  868. c.logf("MapResponse lacked node")
  869. return errors.New("MapResponse lacked node")
  870. }
  871. if d := nm.Debug; d != nil {
  872. controlUseDERPRoute.Store(d.DERPRoute)
  873. controlTrimWGConfig.Store(d.TrimWGConfig)
  874. }
  875. if DevKnob.StripEndpoints {
  876. for _, p := range resp.Peers {
  877. p.Endpoints = nil
  878. }
  879. }
  880. if DevKnob.StripCaps {
  881. nm.SelfNode.Capabilities = nil
  882. }
  883. // Occasionally print the netmap header.
  884. // This is handy for debugging, and our logs processing
  885. // pipeline depends on it. (TODO: Remove this dependency.)
  886. // Code elsewhere prints netmap diffs every time they are received.
  887. now := c.timeNow()
  888. if now.Sub(c.lastPrintMap) >= 5*time.Minute {
  889. c.lastPrintMap = now
  890. c.logf("[v1] new network map[%d]:\n%s", i, nm.VeryConcise())
  891. }
  892. c.mu.Lock()
  893. c.expiry = &nm.Expiry
  894. c.mu.Unlock()
  895. cb(nm)
  896. }
  897. if ctx.Err() != nil {
  898. return ctx.Err()
  899. }
  900. return nil
  901. }
  902. // decode JSON decodes the res.Body into v. If serverNoiseKey is not specified,
  903. // it uses the serverKey and mkey to decode the message from the NaCl-crypto-box.
  904. func decode(res *http.Response, v any, serverKey, serverNoiseKey key.MachinePublic, mkey key.MachinePrivate) error {
  905. defer res.Body.Close()
  906. msg, err := ioutil.ReadAll(io.LimitReader(res.Body, 1<<20))
  907. if err != nil {
  908. return err
  909. }
  910. if res.StatusCode != 200 {
  911. return fmt.Errorf("%d: %v", res.StatusCode, string(msg))
  912. }
  913. if !serverNoiseKey.IsZero() {
  914. return json.Unmarshal(msg, v)
  915. }
  916. return decodeMsg(msg, v, serverKey, mkey)
  917. }
  918. var (
  919. debugMap = envknob.Bool("TS_DEBUG_MAP")
  920. debugRegister = envknob.Bool("TS_DEBUG_REGISTER")
  921. )
  922. var jsonEscapedZero = []byte(`\u0000`)
  923. // decodeMsg is responsible for uncompressing msg and unmarshaling into v.
  924. // If c.serverNoiseKey is not specified, it uses the c.serverKey and mkey
  925. // to first the decrypt msg from the NaCl-crypto-box.
  926. func (c *Direct) decodeMsg(msg []byte, v any, mkey key.MachinePrivate) error {
  927. c.mu.Lock()
  928. serverKey := c.serverKey
  929. serverNoiseKey := c.serverNoiseKey
  930. c.mu.Unlock()
  931. var decrypted []byte
  932. if serverNoiseKey.IsZero() {
  933. var ok bool
  934. decrypted, ok = mkey.OpenFrom(serverKey, msg)
  935. if !ok {
  936. return errors.New("cannot decrypt response")
  937. }
  938. } else {
  939. decrypted = msg
  940. }
  941. var b []byte
  942. if c.newDecompressor == nil {
  943. b = decrypted
  944. } else {
  945. decoder, err := c.newDecompressor()
  946. if err != nil {
  947. return err
  948. }
  949. defer decoder.Close()
  950. b, err = decoder.DecodeAll(decrypted, nil)
  951. if err != nil {
  952. return err
  953. }
  954. }
  955. if debugMap {
  956. var buf bytes.Buffer
  957. json.Indent(&buf, b, "", " ")
  958. log.Printf("MapResponse: %s", buf.Bytes())
  959. }
  960. if bytes.Contains(b, jsonEscapedZero) {
  961. log.Printf("[unexpected] zero byte in controlclient.Direct.decodeMsg into %T: %q", v, b)
  962. }
  963. if err := json.Unmarshal(b, v); err != nil {
  964. return fmt.Errorf("response: %v", err)
  965. }
  966. return nil
  967. }
  968. func decodeMsg(msg []byte, v any, serverKey key.MachinePublic, machinePrivKey key.MachinePrivate) error {
  969. decrypted, ok := machinePrivKey.OpenFrom(serverKey, msg)
  970. if !ok {
  971. return errors.New("cannot decrypt response")
  972. }
  973. if bytes.Contains(decrypted, jsonEscapedZero) {
  974. log.Printf("[unexpected] zero byte in controlclient decodeMsg into %T: %q", v, decrypted)
  975. }
  976. if err := json.Unmarshal(decrypted, v); err != nil {
  977. return fmt.Errorf("response: %v", err)
  978. }
  979. return nil
  980. }
  981. // encode JSON encodes v. If serverNoiseKey is not specified, it uses the serverKey and mkey to
  982. // seal the message into a NaCl-crypto-box.
  983. func encode(v any, serverKey, serverNoiseKey key.MachinePublic, mkey key.MachinePrivate) ([]byte, error) {
  984. b, err := json.Marshal(v)
  985. if err != nil {
  986. return nil, err
  987. }
  988. if debugMap {
  989. if _, ok := v.(*tailcfg.MapRequest); ok {
  990. log.Printf("MapRequest: %s", b)
  991. }
  992. }
  993. if !serverNoiseKey.IsZero() {
  994. return b, nil
  995. }
  996. return mkey.SealTo(serverKey, b), nil
  997. }
  998. func loadServerPubKeys(ctx context.Context, httpc *http.Client, serverURL string) (*tailcfg.OverTLSPublicKeyResponse, error) {
  999. keyURL := fmt.Sprintf("%v/key?v=%d", serverURL, tailcfg.CurrentCapabilityVersion)
  1000. req, err := http.NewRequestWithContext(ctx, "GET", keyURL, nil)
  1001. if err != nil {
  1002. return nil, fmt.Errorf("create control key request: %v", err)
  1003. }
  1004. res, err := httpc.Do(req)
  1005. if err != nil {
  1006. return nil, fmt.Errorf("fetch control key: %v", err)
  1007. }
  1008. defer res.Body.Close()
  1009. b, err := ioutil.ReadAll(io.LimitReader(res.Body, 64<<10))
  1010. if err != nil {
  1011. return nil, fmt.Errorf("fetch control key response: %v", err)
  1012. }
  1013. if res.StatusCode != 200 {
  1014. return nil, fmt.Errorf("fetch control key: %d", res.StatusCode)
  1015. }
  1016. var out tailcfg.OverTLSPublicKeyResponse
  1017. jsonErr := json.Unmarshal(b, &out)
  1018. if jsonErr == nil {
  1019. return &out, nil
  1020. }
  1021. // Some old control servers might not be updated to send the new format.
  1022. // Accept the old pre-JSON format too.
  1023. out = tailcfg.OverTLSPublicKeyResponse{}
  1024. k, err := key.ParseMachinePublicUntyped(mem.B(b))
  1025. if err != nil {
  1026. return nil, multierr.New(jsonErr, err)
  1027. }
  1028. out.LegacyPublicKey = k
  1029. return &out, nil
  1030. }
  1031. // DevKnob contains temporary internal-only debug knobs.
  1032. // They're unexported to not draw attention to them.
  1033. var DevKnob = initDevKnob()
  1034. type devKnobs struct {
  1035. DumpNetMaps bool
  1036. ForceProxyDNS bool
  1037. StripEndpoints bool // strip endpoints from control (only use disco messages)
  1038. StripCaps bool // strip all local node's control-provided capabilities
  1039. }
  1040. func initDevKnob() devKnobs {
  1041. return devKnobs{
  1042. DumpNetMaps: envknob.Bool("TS_DEBUG_NETMAP"),
  1043. ForceProxyDNS: envknob.Bool("TS_DEBUG_PROXY_DNS"),
  1044. StripEndpoints: envknob.Bool("TS_DEBUG_STRIP_ENDPOINTS"),
  1045. StripCaps: envknob.Bool("TS_DEBUG_STRIP_CAPS"),
  1046. }
  1047. }
  1048. var clockNow = time.Now
  1049. // opt.Bool configs from control.
  1050. var (
  1051. controlUseDERPRoute syncs.AtomicValue[opt.Bool]
  1052. controlTrimWGConfig syncs.AtomicValue[opt.Bool]
  1053. )
  1054. // DERPRouteFlag reports the last reported value from control for whether
  1055. // DERP route optimization (Issue 150) should be enabled.
  1056. func DERPRouteFlag() opt.Bool {
  1057. return controlUseDERPRoute.Load()
  1058. }
  1059. // TrimWGConfig reports the last reported value from control for whether
  1060. // we should do lazy wireguard configuration.
  1061. func TrimWGConfig() opt.Bool {
  1062. return controlTrimWGConfig.Load()
  1063. }
  1064. // ipForwardingBroken reports whether the system's IP forwarding is disabled
  1065. // and will definitely not work for the routes provided.
  1066. //
  1067. // It should not return false positives.
  1068. //
  1069. // TODO(bradfitz): Change controlclient.Options.SkipIPForwardingCheck into a
  1070. // func([]netip.Prefix) error signature instead.
  1071. func ipForwardingBroken(routes []netip.Prefix, state *interfaces.State) bool {
  1072. warn, err := netutil.CheckIPForwarding(routes, state)
  1073. if err != nil {
  1074. // Oh well, we tried. This is just for debugging.
  1075. // We don't want false positives.
  1076. // TODO: maybe we want a different warning for inability to check?
  1077. return false
  1078. }
  1079. return warn != nil
  1080. }
  1081. // isUniquePingRequest reports whether pr contains a new PingRequest.URL
  1082. // not already handled, noting its value when returning true.
  1083. func (c *Direct) isUniquePingRequest(pr *tailcfg.PingRequest) bool {
  1084. if pr == nil || pr.URL == "" {
  1085. // Bogus.
  1086. return false
  1087. }
  1088. c.mu.Lock()
  1089. defer c.mu.Unlock()
  1090. if pr.URL == c.lastPingURL {
  1091. return false
  1092. }
  1093. c.lastPingURL = pr.URL
  1094. return true
  1095. }
  1096. func (c *Direct) answerPing(pr *tailcfg.PingRequest) {
  1097. httpc := c.httpc
  1098. useNoise := pr.URLIsNoise || pr.Types == "c2n" && c.noiseConfigured()
  1099. if useNoise {
  1100. nc, err := c.getNoiseClient()
  1101. if err != nil {
  1102. c.logf("failed to get noise client for ping request: %v", err)
  1103. return
  1104. }
  1105. httpc = nc.Client
  1106. }
  1107. if pr.URL == "" {
  1108. c.logf("invalid PingRequest with no URL")
  1109. return
  1110. }
  1111. switch pr.Types {
  1112. case "":
  1113. answerHeadPing(c.logf, httpc, pr)
  1114. return
  1115. case "c2n":
  1116. if !useNoise && !envknob.Bool("TS_DEBUG_PERMIT_HTTP_C2N") {
  1117. c.logf("refusing to answer c2n ping without noise")
  1118. return
  1119. }
  1120. answerC2NPing(c.logf, c.c2nHandler, httpc, pr)
  1121. return
  1122. }
  1123. for _, t := range strings.Split(pr.Types, ",") {
  1124. switch pt := tailcfg.PingType(t); pt {
  1125. case tailcfg.PingTSMP, tailcfg.PingDisco, tailcfg.PingICMP, tailcfg.PingPeerAPI:
  1126. go doPingerPing(c.logf, httpc, pr, c.pinger, pt)
  1127. default:
  1128. c.logf("unsupported ping request type: %q", t)
  1129. }
  1130. }
  1131. }
  1132. func answerHeadPing(logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest) {
  1133. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  1134. defer cancel()
  1135. req, err := http.NewRequestWithContext(ctx, "HEAD", pr.URL, nil)
  1136. if err != nil {
  1137. logf("answerHeadPing: NewRequestWithContext: %v", err)
  1138. return
  1139. }
  1140. if pr.Log {
  1141. logf("answerHeadPing: sending HEAD ping to %v ...", pr.URL)
  1142. }
  1143. t0 := time.Now()
  1144. _, err = c.Do(req)
  1145. d := time.Since(t0).Round(time.Millisecond)
  1146. if err != nil {
  1147. logf("answerHeadPing error: %v to %v (after %v)", err, pr.URL, d)
  1148. } else if pr.Log {
  1149. logf("answerHeadPing complete to %v (after %v)", pr.URL, d)
  1150. }
  1151. }
  1152. func answerC2NPing(logf logger.Logf, c2nHandler http.Handler, c *http.Client, pr *tailcfg.PingRequest) {
  1153. if c2nHandler == nil {
  1154. logf("answerC2NPing: c2nHandler not defined")
  1155. return
  1156. }
  1157. hreq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(pr.Payload)))
  1158. if err != nil {
  1159. logf("answerC2NPing: ReadRequest: %v", err)
  1160. return
  1161. }
  1162. if pr.Log {
  1163. logf("answerC2NPing: got c2n request for %v ...", hreq.RequestURI)
  1164. }
  1165. handlerTimeout := time.Minute
  1166. if v := hreq.Header.Get("C2n-Handler-Timeout"); v != "" {
  1167. handlerTimeout, _ = time.ParseDuration(v)
  1168. }
  1169. handlerCtx, cancel := context.WithTimeout(context.Background(), handlerTimeout)
  1170. defer cancel()
  1171. hreq = hreq.WithContext(handlerCtx)
  1172. rec := httptest.NewRecorder()
  1173. c2nHandler.ServeHTTP(rec, hreq)
  1174. cancel()
  1175. c2nResBuf := new(bytes.Buffer)
  1176. rec.Result().Write(c2nResBuf)
  1177. replyCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
  1178. defer cancel()
  1179. req, err := http.NewRequestWithContext(replyCtx, "POST", pr.URL, c2nResBuf)
  1180. if err != nil {
  1181. logf("answerC2NPing: NewRequestWithContext: %v", err)
  1182. return
  1183. }
  1184. if pr.Log {
  1185. logf("answerC2NPing: sending POST ping to %v ...", pr.URL)
  1186. }
  1187. t0 := time.Now()
  1188. _, err = c.Do(req)
  1189. d := time.Since(t0).Round(time.Millisecond)
  1190. if err != nil {
  1191. logf("answerC2NPing error: %v to %v (after %v)", err, pr.URL, d)
  1192. } else if pr.Log {
  1193. logf("answerC2NPing complete to %v (after %v)", pr.URL, d)
  1194. }
  1195. }
  1196. func sleepAsRequested(ctx context.Context, logf logger.Logf, timeoutReset chan<- struct{}, d time.Duration) error {
  1197. const maxSleep = 5 * time.Minute
  1198. if d > maxSleep {
  1199. logf("sleeping for %v, capped from server-requested %v ...", maxSleep, d)
  1200. d = maxSleep
  1201. } else {
  1202. logf("sleeping for server-requested %v ...", d)
  1203. }
  1204. ticker := time.NewTicker(pollTimeout / 2)
  1205. defer ticker.Stop()
  1206. timer := time.NewTimer(d)
  1207. defer timer.Stop()
  1208. for {
  1209. select {
  1210. case <-ctx.Done():
  1211. return ctx.Err()
  1212. case <-timer.C:
  1213. return nil
  1214. case <-ticker.C:
  1215. select {
  1216. case timeoutReset <- struct{}{}:
  1217. case <-timer.C:
  1218. return nil
  1219. case <-ctx.Done():
  1220. return ctx.Err()
  1221. }
  1222. }
  1223. }
  1224. }
  1225. // getNoiseClient returns the noise client, creating one if one doesn't exist.
  1226. func (c *Direct) getNoiseClient() (*noiseClient, error) {
  1227. c.mu.Lock()
  1228. serverNoiseKey := c.serverNoiseKey
  1229. nc := c.noiseClient
  1230. c.mu.Unlock()
  1231. if serverNoiseKey.IsZero() {
  1232. return nil, errors.New("zero serverNoiseKey")
  1233. }
  1234. if nc != nil {
  1235. return nc, nil
  1236. }
  1237. nc, err, _ := c.sfGroup.Do(struct{}{}, func() (*noiseClient, error) {
  1238. k, err := c.getMachinePrivKey()
  1239. if err != nil {
  1240. return nil, err
  1241. }
  1242. nc, err := newNoiseClient(k, serverNoiseKey, c.serverURL, c.dialer)
  1243. if err != nil {
  1244. return nil, err
  1245. }
  1246. c.mu.Lock()
  1247. defer c.mu.Unlock()
  1248. c.noiseClient = nc
  1249. return nc, nil
  1250. })
  1251. if err != nil {
  1252. return nil, err
  1253. }
  1254. return nc, nil
  1255. }
  1256. // setDNSNoise sends the SetDNSRequest request to the control plane server over Noise,
  1257. // requesting a DNS record be created or updated.
  1258. func (c *Direct) setDNSNoise(ctx context.Context, req *tailcfg.SetDNSRequest) error {
  1259. newReq := *req
  1260. newReq.Version = tailcfg.CurrentCapabilityVersion
  1261. np, err := c.getNoiseClient()
  1262. if err != nil {
  1263. return err
  1264. }
  1265. bodyData, err := json.Marshal(newReq)
  1266. if err != nil {
  1267. return err
  1268. }
  1269. res, err := np.Post(fmt.Sprintf("https://%v/%v", np.host, "machine/set-dns"), "application/json", bytes.NewReader(bodyData))
  1270. if err != nil {
  1271. return err
  1272. }
  1273. defer res.Body.Close()
  1274. if res.StatusCode != 200 {
  1275. msg, _ := ioutil.ReadAll(res.Body)
  1276. return fmt.Errorf("set-dns response: %v, %.200s", res.Status, strings.TrimSpace(string(msg)))
  1277. }
  1278. var setDNSRes tailcfg.SetDNSResponse
  1279. if err := json.NewDecoder(res.Body).Decode(&setDNSRes); err != nil {
  1280. c.logf("error decoding SetDNSResponse: %v", err)
  1281. return fmt.Errorf("set-dns-response: %w", err)
  1282. }
  1283. return nil
  1284. }
  1285. // noiseConfigured reports whether the client can communicate with Control
  1286. // over Noise.
  1287. func (c *Direct) noiseConfigured() bool {
  1288. c.mu.Lock()
  1289. defer c.mu.Unlock()
  1290. return !c.serverNoiseKey.IsZero()
  1291. }
  1292. // SetDNS sends the SetDNSRequest request to the control plane server,
  1293. // requesting a DNS record be created or updated.
  1294. func (c *Direct) SetDNS(ctx context.Context, req *tailcfg.SetDNSRequest) (err error) {
  1295. metricSetDNS.Add(1)
  1296. defer func() {
  1297. if err != nil {
  1298. metricSetDNSError.Add(1)
  1299. }
  1300. }()
  1301. if c.noiseConfigured() {
  1302. return c.setDNSNoise(ctx, req)
  1303. }
  1304. c.mu.Lock()
  1305. serverKey := c.serverKey
  1306. c.mu.Unlock()
  1307. if serverKey.IsZero() {
  1308. return errors.New("zero serverKey")
  1309. }
  1310. machinePrivKey, err := c.getMachinePrivKey()
  1311. if err != nil {
  1312. return fmt.Errorf("getMachinePrivKey: %w", err)
  1313. }
  1314. if machinePrivKey.IsZero() {
  1315. return errors.New("getMachinePrivKey returned zero key")
  1316. }
  1317. // TODO(maisem): dedupe this codepath from SetDNSNoise.
  1318. var serverNoiseKey key.MachinePublic
  1319. bodyData, err := encode(req, serverKey, serverNoiseKey, machinePrivKey)
  1320. if err != nil {
  1321. return err
  1322. }
  1323. body := bytes.NewReader(bodyData)
  1324. u := fmt.Sprintf("%s/machine/%s/set-dns", c.serverURL, machinePrivKey.Public().UntypedHexString())
  1325. hreq, err := http.NewRequestWithContext(ctx, "POST", u, body)
  1326. if err != nil {
  1327. return err
  1328. }
  1329. res, err := c.httpc.Do(hreq)
  1330. if err != nil {
  1331. return err
  1332. }
  1333. defer res.Body.Close()
  1334. if res.StatusCode != 200 {
  1335. msg, _ := ioutil.ReadAll(res.Body)
  1336. return fmt.Errorf("set-dns response: %v, %.200s", res.Status, strings.TrimSpace(string(msg)))
  1337. }
  1338. var setDNSRes tailcfg.SetDNSResponse
  1339. if err := decode(res, &setDNSRes, serverKey, serverNoiseKey, machinePrivKey); err != nil {
  1340. c.logf("error decoding SetDNSResponse with server key %s and machine key %s: %v", serverKey, machinePrivKey.Public(), err)
  1341. return fmt.Errorf("set-dns-response: %w", err)
  1342. }
  1343. return nil
  1344. }
  1345. func (c *Direct) DoNoiseRequest(req *http.Request) (*http.Response, error) {
  1346. nc, err := c.getNoiseClient()
  1347. if err != nil {
  1348. return nil, err
  1349. }
  1350. return nc.Do(req)
  1351. }
  1352. // doPingerPing sends a Ping to pr.IP using pinger, and sends an http request back to
  1353. // pr.URL with ping response data.
  1354. func doPingerPing(logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, pinger Pinger, pingType tailcfg.PingType) {
  1355. if pr.URL == "" || !pr.IP.IsValid() || pinger == nil {
  1356. logf("invalid ping request: missing url, ip or pinger")
  1357. return
  1358. }
  1359. start := time.Now()
  1360. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  1361. defer cancel()
  1362. res, err := pinger.Ping(ctx, pr.IP, pingType)
  1363. if err != nil {
  1364. d := time.Since(start).Round(time.Millisecond)
  1365. logf("doPingerPing: ping error of type %q to %v after %v: %v", pingType, pr.IP, d, err)
  1366. return
  1367. }
  1368. postPingResult(start, logf, c, pr, res.ToPingResponse(pingType))
  1369. }
  1370. func postPingResult(start time.Time, logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, res *tailcfg.PingResponse) error {
  1371. duration := time.Since(start)
  1372. if pr.Log {
  1373. if res.Err == "" {
  1374. logf("ping to %v completed in %v. pinger.Ping took %v seconds", pr.IP, res.LatencySeconds, duration)
  1375. } else {
  1376. logf("ping to %v failed after %v: %v", pr.IP, duration, res.Err)
  1377. }
  1378. }
  1379. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  1380. defer cancel()
  1381. jsonPingRes, err := json.Marshal(res)
  1382. if err != nil {
  1383. return err
  1384. }
  1385. // Send the results of the Ping, back to control URL.
  1386. req, err := http.NewRequestWithContext(ctx, "POST", pr.URL, bytes.NewReader(jsonPingRes))
  1387. if err != nil {
  1388. return fmt.Errorf("http.NewRequestWithContext(%q): %w", pr.URL, err)
  1389. }
  1390. if pr.Log {
  1391. logf("postPingResult: sending ping results to %v ...", pr.URL)
  1392. }
  1393. t0 := time.Now()
  1394. _, err = c.Do(req)
  1395. d := time.Since(t0).Round(time.Millisecond)
  1396. if err != nil {
  1397. return fmt.Errorf("postPingResult error: %w to %v (after %v)", err, pr.URL, d)
  1398. } else if pr.Log {
  1399. logf("postPingResult complete to %v (after %v)", pr.URL, d)
  1400. }
  1401. return nil
  1402. }
  1403. var (
  1404. metricMapRequestsActive = clientmetric.NewGauge("controlclient_map_requests_active")
  1405. metricMapRequests = clientmetric.NewCounter("controlclient_map_requests")
  1406. metricMapRequestsLite = clientmetric.NewCounter("controlclient_map_requests_lite")
  1407. metricMapRequestsPoll = clientmetric.NewCounter("controlclient_map_requests_poll")
  1408. metricMapResponseMessages = clientmetric.NewCounter("controlclient_map_response_message") // any message type
  1409. metricMapResponsePings = clientmetric.NewCounter("controlclient_map_response_ping")
  1410. metricMapResponseKeepAlives = clientmetric.NewCounter("controlclient_map_response_keepalive")
  1411. metricMapResponseMap = clientmetric.NewCounter("controlclient_map_response_map") // any non-keepalive map response
  1412. metricMapResponseMapDelta = clientmetric.NewCounter("controlclient_map_response_map_delta") // 2nd+ non-keepalive map response
  1413. metricSetDNS = clientmetric.NewCounter("controlclient_setdns")
  1414. metricSetDNSError = clientmetric.NewCounter("controlclient_setdns_error")
  1415. )