|
7 | 7 | "io"
|
8 | 8 | "net"
|
9 | 9 | "net/netip"
|
| 10 | + "reflect" |
10 | 11 | "strconv"
|
11 | 12 | "sync"
|
12 | 13 | "time"
|
@@ -214,28 +215,23 @@ func NewConn(options *Options) (*Conn, error) {
|
214 | 215 | return
|
215 | 216 | }
|
216 | 217 | server.lastStatus = s.AsOf
|
217 |
| - server.lastEndpoints = make([]string, 0, len(s.LocalAddrs)) |
218 |
| - for _, addr := range s.LocalAddrs { |
219 |
| - server.lastEndpoints = append(server.lastEndpoints, addr.Addr.String()) |
| 218 | + if endpointsEqual(s.LocalAddrs, server.lastEndpoints) { |
| 219 | + // No need to update the node if nothing changed! |
| 220 | + server.lastMutex.Unlock() |
| 221 | + return |
220 | 222 | }
|
| 223 | + server.lastEndpoints = append([]tailcfg.Endpoint{}, s.LocalAddrs...) |
221 | 224 | server.lastMutex.Unlock()
|
222 | 225 | server.sendNode()
|
223 | 226 | })
|
224 | 227 | wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) {
|
225 | 228 | server.logger.Debug(context.Background(), "netinfo callback", slog.F("netinfo", ni))
|
226 |
| - // If the lastMutex is blocked, it's possible that |
227 |
| - // multiple NetInfo callbacks occur at the same time. |
228 |
| - // |
229 |
| - // We need to ensure only the latest is sent! |
230 |
| - asOf := time.Now() |
231 | 229 | server.lastMutex.Lock()
|
232 |
| - if asOf.Before(server.lastNetInfo) { |
| 230 | + if reflect.DeepEqual(server.lastNetInfo, ni) { |
233 | 231 | server.lastMutex.Unlock()
|
234 | 232 | return
|
235 | 233 | }
|
236 |
| - server.lastNetInfo = asOf |
237 |
| - server.lastPreferredDERP = ni.PreferredDERP |
238 |
| - server.lastDERPLatency = ni.DERPLatency |
| 234 | + server.lastNetInfo = ni.Clone() |
239 | 235 | server.lastMutex.Unlock()
|
240 | 236 | server.sendNode()
|
241 | 237 | })
|
@@ -285,12 +281,10 @@ type Conn struct {
|
285 | 281 | nodeChanged bool
|
286 | 282 | // It's only possible to store these values via status functions,
|
287 | 283 | // so the values must be stored for retrieval later on.
|
288 |
| - lastStatus time.Time |
289 |
| - lastNetInfo time.Time |
290 |
| - lastEndpoints []string |
291 |
| - lastPreferredDERP int |
292 |
| - lastDERPLatency map[string]float64 |
293 |
| - nodeCallback func(node *Node) |
| 284 | + lastStatus time.Time |
| 285 | + lastEndpoints []tailcfg.Endpoint |
| 286 | + lastNetInfo *tailcfg.NetInfo |
| 287 | + nodeCallback func(node *Node) |
294 | 288 |
|
295 | 289 | trafficStats *connstats.Statistics
|
296 | 290 | }
|
@@ -589,16 +583,27 @@ func (c *Conn) Node() *Node {
|
589 | 583 | }
|
590 | 584 |
|
591 | 585 | func (c *Conn) selfNode() *Node {
|
| 586 | + endpoints := make([]string, 0, len(c.lastEndpoints)) |
| 587 | + for _, addr := range c.lastEndpoints { |
| 588 | + endpoints = append(endpoints, addr.Addr.String()) |
| 589 | + } |
| 590 | + var preferredDERP int |
| 591 | + var derpLatency map[string]float64 |
| 592 | + if c.lastNetInfo != nil { |
| 593 | + preferredDERP = c.lastNetInfo.PreferredDERP |
| 594 | + derpLatency = c.lastNetInfo.DERPLatency |
| 595 | + } |
| 596 | + |
592 | 597 | node := &Node{
|
593 | 598 | ID: c.netMap.SelfNode.ID,
|
594 | 599 | AsOf: database.Now(),
|
595 | 600 | Key: c.netMap.SelfNode.Key,
|
596 | 601 | Addresses: c.netMap.SelfNode.Addresses,
|
597 | 602 | AllowedIPs: c.netMap.SelfNode.AllowedIPs,
|
598 | 603 | DiscoKey: c.magicConn.DiscoPublicKey(),
|
599 |
| - Endpoints: c.lastEndpoints, |
600 |
| - PreferredDERP: c.lastPreferredDERP, |
601 |
| - DERPLatency: c.lastDERPLatency, |
| 604 | + Endpoints: endpoints, |
| 605 | + PreferredDERP: preferredDERP, |
| 606 | + DERPLatency: derpLatency, |
602 | 607 | }
|
603 | 608 | if c.blockEndpoints {
|
604 | 609 | node.Endpoints = nil
|
@@ -774,3 +779,15 @@ func Logger(logger slog.Logger) tslogger.Logf {
|
774 | 779 | logger.Debug(context.Background(), fmt.Sprintf(format, args...))
|
775 | 780 | })
|
776 | 781 | }
|
| 782 | + |
| 783 | +func endpointsEqual(x, y []tailcfg.Endpoint) bool { |
| 784 | + if len(x) != len(y) { |
| 785 | + return false |
| 786 | + } |
| 787 | + for i := range x { |
| 788 | + if x[i] != y[i] { |
| 789 | + return false |
| 790 | + } |
| 791 | + } |
| 792 | + return true |
| 793 | +} |
0 commit comments