From b6b135f0c18989babf5c4e6f139cb23e1e8d155c Mon Sep 17 00:00:00 2001 From: KevinLiang10 <37811973+KevinLiang10@users.noreply.github.com> Date: Fri, 27 Jun 2025 14:32:48 -0400 Subject: [PATCH] ipn/ipnlocal: replace the key to webHandler for services This commit replaces way we get the webhandler for vipServices. It used to use the host name from request to find the webHandler, now everything targeting the vipService IP have the same set of handlers Signed-off-by: KevinLiang10 <37811973+KevinLiang10@users.noreply.github.com> --- cmd/tailscale/cli/serve_v2.go | 11 +++++++---- ipn/ipnlocal/serve.go | 1 + ipn/serve.go | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/tailscale/cli/serve_v2.go b/cmd/tailscale/cli/serve_v2.go index 4e6a31d40..17093b49b 100644 --- a/cmd/tailscale/cli/serve_v2.go +++ b/cmd/tailscale/cli/serve_v2.go @@ -550,7 +550,7 @@ func (e *serveEnv) messageForPort(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN ips := st.TailscaleIPs host := dnsName if forService { - host = strings.Join([]string{svcName.WithoutPrefix(), st.CurrentTailnet.MagicDNSSuffix}, ".") + host = svcName.WithoutPrefix() } hp = ipn.HostPort(net.JoinHostPort(host, strconv.Itoa(int(srvPort)))) @@ -617,11 +617,14 @@ func (e *serveEnv) messageForPort(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN sort.Slice(mounts, func(i, j int) bool { return len(mounts[i]) < len(mounts[j]) }) - + hostName := host + if forService { + hostName = strings.Join([]string{svcName.WithoutPrefix(), st.CurrentTailnet.MagicDNSSuffix}, ".") + } for _, m := range mounts { h := webConfig.Handlers[m] t, d := srvTypeAndDesc(h) - output.WriteString(fmt.Sprintf("%s://%s%s%s\n", scheme, host, portPart, m)) + output.WriteString(fmt.Sprintf("%s://%s%s%s\n", scheme, hostName, portPart, m)) output.WriteString(fmt.Sprintf("%s %-5s %s\n\n", "|--", t, d)) } } else if tcpHandler != nil { @@ -932,7 +935,7 @@ func (e *serveEnv) removeWebServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN if svc == nil { return errors.New("service does not exist") } - hostName = strings.Join([]string{svcName.WithoutPrefix(), st.CurrentTailnet.MagicDNSSuffix}, ".") + hostName = svcName.WithoutPrefix() webServeMap = svc.Web } diff --git a/ipn/ipnlocal/serve.go b/ipn/ipnlocal/serve.go index 44d63fe54..0cb6bbc1f 100644 --- a/ipn/ipnlocal/serve.go +++ b/ipn/ipnlocal/serve.go @@ -1016,6 +1016,7 @@ func (b *LocalBackend) webServerConfig(hostname string, forVIPService tailcfg.Se return c, false } if forVIPService != "" { + key = ipn.HostPort(fmt.Sprintf("%s:%v", forVIPService.WithoutPrefix(), port)) return b.serveConfig.FindServiceWeb(forVIPService, key) } return b.serveConfig.FindWeb(key) diff --git a/ipn/serve.go b/ipn/serve.go index 6df181cbb..c8e32b87d 100644 --- a/ipn/serve.go +++ b/ipn/serve.go @@ -349,7 +349,7 @@ func (sc *ServeConfig) SetWebHandler(handler *HTTPHandler, host string, port uin webServerMap := &sc.Web hostName := host if svcName, ok := tailcfg.AsServiceName(host); ok { - hostName = strings.Join([]string{svcName.WithoutPrefix(), st.CurrentTailnet.MagicDNSSuffix}, ".") + hostName = svcName.WithoutPrefix() if _, ok := sc.Services[svcName]; !ok { mak.Set(&sc.Services, svcName, new(ServiceConfig)) } @@ -459,7 +459,7 @@ func (sc *ServeConfig) RemoveWebHandler(host string, port uint16, mounts []strin // RemoveServiceWebHandler deletes the web handlers at all of the given mount points // for the provided host and port in the serve config for the given service. func (sc *ServeConfig) RemoveServiceWebHandler(st *ipnstate.Status, svcName tailcfg.ServiceName, port uint16, mounts []string) { - hostName := strings.Join([]string{svcName.WithoutPrefix(), st.CurrentTailnet.MagicDNSSuffix}, ".") + hostName := svcName.WithoutPrefix() hp := HostPort(net.JoinHostPort(hostName, strconv.Itoa(int(port)))) svc, ok := sc.Services[svcName]