Skip to content

Commit 064e448

Browse files
committed
Improved tailnode start up handling
1 parent d1be440 commit 064e448

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

api.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
5151
5252
</body>
5353
</html>
54-
54+
5555
`, mKeyStr)))
5656
}
5757

@@ -215,25 +215,40 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
215215
return
216216
}
217217

218-
log.Printf("[%s] sending initial map", m.Name)
219-
pollData <- *data
220-
221218
// We update our peers if the client is not sending ReadOnly in the MapRequest
222219
// so we don't distribute its initial request (it comes with
223220
// empty endpoints to peers)
224-
if !req.ReadOnly {
225-
peers, _ := h.getPeers(m)
226-
h.pollMu.Lock()
227-
for _, p := range *peers {
228-
log.Printf("[%s] notifying peer %s (%s)", m.Name, p.Name, p.Addresses[0])
229-
if pUp, ok := h.clientsPolling[uint64(p.ID)]; ok {
230-
pUp <- []byte{}
231-
} else {
232-
log.Printf("[%s] Peer %s does not appear to be polling", m.Name, p.Name)
233-
}
221+
222+
// Details on the protocol can be found in https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L696
223+
log.Printf("[%s] ReadOnly=%t OmitPeers=%t Stream=%t", m.Name, req.ReadOnly, req.OmitPeers, req.Stream)
224+
225+
if req.ReadOnly {
226+
log.Printf("[%s] Client is starting up. Asking for DERP map", m.Name)
227+
c.Data(200, "application/json; charset=utf-8", *data)
228+
return
229+
}
230+
if req.OmitPeers {
231+
log.Printf("[%s] Client is starting up. Ready to receive the peers", m.Name)
232+
c.Data(200, "application/json; charset=utf-8", *data)
233+
return
234+
}
235+
236+
log.Printf("[%s] Client is ready to access the tailnet", m.Name)
237+
log.Printf("[%s] Sending initial map", m.Name)
238+
pollData <- *data
239+
240+
log.Printf("[%s] Notifying peers", m.Name)
241+
peers, _ := h.getPeers(m)
242+
h.pollMu.Lock()
243+
for _, p := range *peers {
244+
log.Printf("[%s] Notifying peer %s (%s)", m.Name, p.Name, p.Addresses[0])
245+
if pUp, ok := h.clientsPolling[uint64(p.ID)]; ok {
246+
pUp <- []byte{}
247+
} else {
248+
log.Printf("[%s] Peer %s does not appear to be polling", m.Name, p.Name)
234249
}
235-
h.pollMu.Unlock()
236250
}
251+
h.pollMu.Unlock()
237252

238253
go h.keepAlive(cancelKeepAlive, pollData, mKey, req, m)
239254

@@ -290,6 +305,7 @@ func (h *Headscale) keepAlive(cancel chan []byte, pollData chan []byte, mKey wgc
290305
log.Printf("Error generating the keep alive msg: %s", err)
291306
return
292307
}
308+
log.Printf("[%s] Sending keepalive", m.Name)
293309
pollData <- *data
294310
h.pollMu.Unlock()
295311
time.Sleep(60 * time.Second)

0 commit comments

Comments
 (0)