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>
This commit is contained in:
KevinLiang10 2025-06-27 14:32:48 -04:00
parent a6634ce2c4
commit b6b135f0c1
3 changed files with 10 additions and 6 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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]