Skip to content

Commit 7d94591

Browse files
committed
ipn/wg: skip refresh if proxy is too young
1 parent 90fae3d commit 7d94591

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

intra/ipn/wgproxy.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ func waitForDeviceUp() {
305305

306306
// onNotOK implements Proxy.
307307
func (w *wgproxy) onNotOK() (didRefresh, allok bool) {
308+
s := w.status.Load()
309+
if err := candial2(s); err != nil { // stopped or paused
310+
log.E("proxy: wg: %s onNotOK: %s; status %s; why? %v", w.tag(), pxstatus(s), err)
311+
return
312+
}
313+
314+
// TODO: skip on s == TUP?
315+
if w.tooyoung() {
316+
log.VV("proxy: wg: %s onNotOK: too young; status %s", w.tag(), pxstatus(s))
317+
return
318+
}
319+
308320
var didPing, viaDidRefresh, viaOK bool
309321

310322
if via := w.getViaIfDialed(); via != nil {
@@ -328,11 +340,15 @@ func (w *wgproxy) onNotOK() (didRefresh, allok bool) {
328340
allok = allok && w.Ping() // ping / sendkeepalive is async
329341
didPing = true
330342
}
331-
loged(err)("proxy: wg: %s; onNotOK: refresh? %t+%t; ping? %t; ok? %t+%t; err? %v",
332-
w.tag(), viaDidRefresh, didRefresh, didPing, viaOK, allok, err)
343+
loged(err)("proxy: wg: %s; onNotOK: refresh? %t+%t; ping? %t; ok? %t+%t; status? %s; err? %v",
344+
w.tag(), viaDidRefresh, didRefresh, didPing, viaOK, allok, pxstatus(s), err)
333345
return
334346
}
335347

348+
func (w *wgproxy) tooyoung() bool {
349+
return now()-w.since < ageThreshold.Milliseconds()
350+
}
351+
336352
// Refresh implements Proxy.
337353
func (w *wgproxy) Refresh() (err error) {
338354
status := w.status.Load()
@@ -343,6 +359,12 @@ func (w *wgproxy) Refresh() (err error) {
343359
return err
344360
}
345361

362+
// TODO: skip on s == TUP?
363+
if w.tooyoung() {
364+
log.VV("proxy: wg: %s refresh skipped; too young; status(%s)", w.tag(), pxstatus(status))
365+
return
366+
}
367+
346368
w.latestRefresh.Store(now())
347369
resetDevice := (resetDeviceOnTNT && status == TNT) ||
348370
(resetDeviceOnTUP && status == TUP)
@@ -599,7 +621,7 @@ func wgIfConfigOf(id string, txtptr *string) (opts wgifopts, err error) {
599621
opts.peers[v] = peerkey
600622
}
601623
// peer config: carry over public keys
602-
log.D("proxy: wg: %s ifconfig: processing key %q, err? %v", id, k, exx)
624+
log.D("proxy: wg: %s ifconfig: processing key %q, err? %v", id, k, pfxsfx(v), exx)
603625
pcfg.WriteString(line + "\n")
604626
finalizeMH(opts.eps, currentPeer)
605627
if len(v) > 8 {
@@ -1682,9 +1704,8 @@ func (h *wgtun) listener(op wg.PktDir, err error) {
16821704
}
16831705

16841706
softrefresh := false
1685-
const tenSecMillis = 10 * 1000
16861707
// s may also be TOK (for successful handshakes but not for transport data)
1687-
if age > tenSecMillis && (s == TOK || s == TKO) {
1708+
if age > ageThreshold.Milliseconds() && (s == TOK || s == TKO) {
16881709
lastSuccessfulRead := h.latestGoodRead.Load()
16891710
lastSuccessfulWrite := h.latestGoodWrite.Load()
16901711
lastRead := h.latestRx.Load()
@@ -1884,3 +1905,16 @@ func estr(err error) string {
18841905
}
18851906
return "<no err>"
18861907
}
1908+
1909+
func pfxsfx(s string) string {
1910+
if len(s) <= 4 {
1911+
return s
1912+
}
1913+
if len(s) <= 8 {
1914+
return s[:4]
1915+
}
1916+
if len(s) <= 16 {
1917+
return s[:4] + ".." + s[len(s)-4:]
1918+
}
1919+
return s[:6] + ".." + s[len(s)-6:]
1920+
}

0 commit comments

Comments
 (0)