From 7783a960e866a1f0e6979ff76e07c896095aa419 Mon Sep 17 00:00:00 2001 From: Naman Sood Date: Thu, 12 Oct 2023 17:02:20 -0400 Subject: [PATCH] client/web: add metric for exit node advertising (#9781) * client/web: add metric for exit node advertising Updates tailscale/corp#15215 Signed-off-by: Naman Sood * client/web: use http request's context for IncrementCounter Updates #cleanup Signed-off-by: Naman Sood --------- Signed-off-by: Naman Sood --- client/web/web.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/client/web/web.go b/client/web/web.go index b680ea7f4..dc79e4169 100644 --- a/client/web/web.go +++ b/client/web/web.go @@ -66,6 +66,11 @@ const ( sessionCookieExpiry = time.Hour * 24 * 30 // 30 days ) +var ( + exitNodeRouteV4 = netip.MustParsePrefix("0.0.0.0/0") + exitNodeRouteV6 = netip.MustParsePrefix("::/0") +) + // browserSession holds data about a user's browser session // on the full management web client. type browserSession struct { @@ -189,7 +194,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request) { return } if !s.devMode { - s.lc.IncrementCounter(context.Background(), "web_client_page_load", 1) + s.lc.IncrementCounter(r.Context(), "web_client_page_load", 1) } s.assetsHandler.ServeHTTP(w, r) } @@ -427,8 +432,6 @@ func (s *Server) serveGetNodeData(w http.ResponseWriter, r *http.Request) { IPNVersion: versionShort, DebugMode: s.tsDebugMode, } - exitNodeRouteV4 := netip.MustParsePrefix("0.0.0.0/0") - exitNodeRouteV6 := netip.MustParsePrefix("::/0") for _, r := range prefs.AdvertiseRoutes { if r == exitNodeRouteV4 || r == exitNodeRouteV6 { data.AdvertiseExitNode = true @@ -473,6 +476,22 @@ func (s *Server) servePostNodeUpdate(w http.ResponseWriter, r *http.Request) { return } + prefs, err := s.lc.GetPrefs(r.Context()) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + isCurrentlyExitNode := slices.Contains(prefs.AdvertiseRoutes, exitNodeRouteV4) || slices.Contains(prefs.AdvertiseRoutes, exitNodeRouteV6) + + if postData.AdvertiseExitNode != isCurrentlyExitNode { + if postData.AdvertiseExitNode { + s.lc.IncrementCounter(r.Context(), "web_client_advertise_exitnode_enable", 1) + } else { + s.lc.IncrementCounter(r.Context(), "web_client_advertise_exitnode_disable", 1) + } + } + routes, err := netutil.CalcAdvertiseRoutes(postData.AdvertiseRoutes, postData.AdvertiseExitNode) if err != nil { w.WriteHeader(http.StatusInternalServerError)