@@ -77,6 +77,7 @@ func NewConn(options *Options) (*Conn, error) {
77
77
nodePublicKey := nodePrivateKey .Public ()
78
78
79
79
netMap := & netmap.NetworkMap {
80
+ DERPMap : options .DERPMap ,
80
81
NodeKey : nodePublicKey ,
81
82
PrivateKey : nodePrivateKey ,
82
83
Addresses : options .Addresses ,
@@ -172,7 +173,6 @@ func NewConn(options *Options) (*Conn, error) {
172
173
if err != nil {
173
174
return nil , xerrors .Errorf ("start netstack: %w" , err )
174
175
}
175
- wireguardEngine = wgengine .NewWatchdog (wireguardEngine )
176
176
wireguardEngine .SetDERPMap (options .DERPMap )
177
177
netMapCopy := * netMap
178
178
wireguardEngine .SetNetworkMap (& netMapCopy )
@@ -246,6 +246,13 @@ func NewConn(options *Options) (*Conn, error) {
246
246
return server , nil
247
247
}
248
248
249
+ // DERPMap returns the currently set DERP mapping.
250
+ func (c * Conn ) DERPMap () * tailcfg.DERPMap {
251
+ c .mutex .Lock ()
252
+ defer c .mutex .Unlock ()
253
+ return c .netMap .DERPMap
254
+ }
255
+
249
256
// IP generates a new IP with a static service prefix.
250
257
func IP () netip.Addr {
251
258
// This is Tailscale's ephemeral service prefix.
@@ -407,23 +414,24 @@ func (c *Conn) Status() *ipnstate.Status {
407
414
}
408
415
409
416
// Ping sends a Disco ping to the Wireguard engine.
410
- func (c * Conn ) Ping (ctx context.Context , ip netip.Addr ) (time.Duration , error ) {
417
+ // The bool returns true if the ping was made P2P.
418
+ func (c * Conn ) Ping (ctx context.Context , ip netip.Addr ) (time.Duration , bool , error ) {
411
419
errCh := make (chan error , 1 )
412
- durCh := make (chan time. Duration , 1 )
420
+ prChan := make (chan * ipnstate. PingResult , 1 )
413
421
go c .wireguardEngine .Ping (ip , tailcfg .PingDisco , func (pr * ipnstate.PingResult ) {
414
422
if pr .Err != "" {
415
423
errCh <- xerrors .New (pr .Err )
416
424
return
417
425
}
418
- durCh <- time . Duration ( pr . LatencySeconds * float64 ( time . Second ))
426
+ prChan <- pr
419
427
})
420
428
select {
421
429
case err := <- errCh :
422
- return 0 , err
430
+ return 0 , false , err
423
431
case <- ctx .Done ():
424
- return 0 , ctx .Err ()
425
- case dur := <- durCh :
426
- return dur , nil
432
+ return 0 , false , ctx .Err ()
433
+ case pr := <- prChan :
434
+ return time . Duration ( pr . LatencySeconds * float64 ( time . Second )), pr . Endpoint != "" , nil
427
435
}
428
436
}
429
437
@@ -445,7 +453,7 @@ func (c *Conn) AwaitReachable(ctx context.Context, ip netip.Addr) bool {
445
453
ctx , cancel := context .WithTimeout (ctx , 5 * time .Minute )
446
454
defer cancel ()
447
455
448
- _ , err := c .Ping (ctx , ip )
456
+ _ , _ , err := c .Ping (ctx , ip )
449
457
if err == nil {
450
458
completed ()
451
459
}
@@ -523,17 +531,7 @@ func (c *Conn) sendNode() {
523
531
c .nodeChanged = true
524
532
return
525
533
}
526
- node := & Node {
527
- ID : c .netMap .SelfNode .ID ,
528
- AsOf : database .Now (),
529
- Key : c .netMap .SelfNode .Key ,
530
- Addresses : c .netMap .SelfNode .Addresses ,
531
- AllowedIPs : c .netMap .SelfNode .AllowedIPs ,
532
- DiscoKey : c .magicConn .DiscoPublicKey (),
533
- Endpoints : c .lastEndpoints ,
534
- PreferredDERP : c .lastPreferredDERP ,
535
- DERPLatency : c .lastDERPLatency ,
536
- }
534
+ node := c .selfNode ()
537
535
if c .blockEndpoints {
538
536
node .Endpoints = nil
539
537
}
@@ -557,6 +555,31 @@ func (c *Conn) sendNode() {
557
555
}()
558
556
}
559
557
558
+ // Node returns the last node that was sent to the node callback.
559
+ func (c * Conn ) Node () * Node {
560
+ c .lastMutex .Lock ()
561
+ defer c .lastMutex .Unlock ()
562
+ return c .selfNode ()
563
+ }
564
+
565
+ func (c * Conn ) selfNode () * Node {
566
+ node := & Node {
567
+ ID : c .netMap .SelfNode .ID ,
568
+ AsOf : database .Now (),
569
+ Key : c .netMap .SelfNode .Key ,
570
+ Addresses : c .netMap .SelfNode .Addresses ,
571
+ AllowedIPs : c .netMap .SelfNode .AllowedIPs ,
572
+ DiscoKey : c .magicConn .DiscoPublicKey (),
573
+ Endpoints : c .lastEndpoints ,
574
+ PreferredDERP : c .lastPreferredDERP ,
575
+ DERPLatency : c .lastDERPLatency ,
576
+ }
577
+ if c .blockEndpoints {
578
+ node .Endpoints = nil
579
+ }
580
+ return node
581
+ }
582
+
560
583
// This and below is taken _mostly_ verbatim from Tailscale:
561
584
// https://github.com/tailscale/tailscale/blob/c88bd53b1b7b2fcf7ba302f2e53dd1ce8c32dad4/tsnet/tsnet.go#L459-L494
562
585
0 commit comments