MCPcopy
hub / github.com/tailscale/tailscale / addrDiscoveryLoop

Method addrDiscoveryLoop

net/udprelay/server.go:458–560  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

456}
457
458func (s *Server) addrDiscoveryLoop() {
459 defer s.wg.Done()
460 timer := time.NewTimer(0) // fire immediately
461 defer timer.Stop()
462
463 getAddrPorts := func() ([]netip.AddrPort, error) {
464 ctx, cancel := context.WithCancel(context.Background())
465 defer cancel()
466
467 var addrPorts set.Set[netip.AddrPort]
468 addrPorts.Make()
469
470 // get local addresses
471 ips, _, err := netmon.LocalAddresses()
472 if err != nil {
473 return nil, err
474 }
475 for _, ip := range ips {
476 if ip.IsValid() {
477 if ip.Is4() {
478 addrPorts.Add(netip.AddrPortFrom(ip, s.uc4Port))
479 } else {
480 addrPorts.Add(netip.AddrPortFrom(ip, s.uc6Port))
481 }
482 }
483 }
484
485 // Get cloud metadata service addresses.
486 // TODO(illotum) Same is done within magicsock, consider caching within cloudInfo
487 cloudIPs, err := s.cloudInfo.GetPublicIPs(ctx)
488 if err == nil { // Not handling the err, GetPublicIPs already printed to log.
489 for _, ip := range cloudIPs {
490 if ip.IsValid() {
491 if ip.Is4() {
492 addrPorts.Add(netip.AddrPortFrom(ip, s.uc4Port))
493 } else {
494 addrPorts.Add(netip.AddrPortFrom(ip, s.uc6Port))
495 }
496 }
497 }
498 }
499
500 dm := s.getDERPMap()
501 if dm == nil {
502 // We don't have a DERPMap which is required to dynamically
503 // discover external addresses, but we can return the endpoints we
504 // do have.
505 return addrPorts.Slice(), nil
506 }
507
508 // get addrPorts as visible from DERP
509 rep, err := s.netChecker.GetReport(ctx, dm, &netcheck.GetReportOpts{
510 OnlySTUN: true,
511 })
512 if err != nil {
513 return nil, err
514 }
515 // Add STUN-discovered endpoints with their observed ports.

Callers 1

NewServerFunction · 0.80

Calls 15

StopMethod · 0.95
getDERPMapMethod · 0.95
ResetMethod · 0.95
logfMethod · 0.95
LocalAddressesFunction · 0.92
RandomDurationBetweenFunction · 0.92
GetReportMethod · 0.80
GetGlobalAddrsMethod · 0.80
EqualBoolMethod · 0.80
DoneMethod · 0.65
NewTimerMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected