replace IsServiceName with tailcfg.AsServiceName

Signed-off-by: KevinLiang10 <37811973+KevinLiang10@users.noreply.github.com>
This commit is contained in:
KevinLiang10 2025-06-26 15:30:31 -04:00
parent ec35e6c1d8
commit 8d861680d2
3 changed files with 28 additions and 37 deletions

View File

@ -440,10 +440,9 @@ const backgroundExistsMsg = "background configuration already exists, use `tails
// validateConfig checks if the serve config is valid to serve the type wanted on the port. // validateConfig checks if the serve config is valid to serve the type wanted on the port.
// dnsName is a FQDN or a serviceName (with `svc:` prefix). // dnsName is a FQDN or a serviceName (with `svc:` prefix).
func (e *serveEnv) validateConfig(sc *ipn.ServeConfig, port uint16, wantServe serveType, dnsName string) error { func (e *serveEnv) validateConfig(sc *ipn.ServeConfig, port uint16, wantServe serveType, dnsName string) error {
forService := ipn.IsServiceName(dnsName)
var tcpHandlerForPort *ipn.TCPPortHandler var tcpHandlerForPort *ipn.TCPPortHandler
if forService { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
svc := sc.Services[tailcfg.ServiceName(dnsName)] svc := sc.Services[svcName]
if svc == nil { if svc == nil {
return nil return nil
} }
@ -521,7 +520,7 @@ func (e *serveEnv) setServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsName st
// update the serve config based on if funnel is enabled // update the serve config based on if funnel is enabled
// Since funnel is not supported for services, we only apply it for node's serve. // Since funnel is not supported for services, we only apply it for node's serve.
if !ipn.IsServiceName(dnsName) { if _, ok := tailcfg.AsServiceName(dnsName); !ok {
e.applyFunnel(sc, dnsName, srvPort, allowFunnel) e.applyFunnel(sc, dnsName, srvPort, allowFunnel)
} }
return nil return nil
@ -544,14 +543,14 @@ var (
// serve config and status. // serve config and status.
func (e *serveEnv) messageForPort(sc *ipn.ServeConfig, st *ipnstate.Status, dnsName string, srvType serveType, srvPort uint16) string { func (e *serveEnv) messageForPort(sc *ipn.ServeConfig, st *ipnstate.Status, dnsName string, srvType serveType, srvPort uint16) string {
var output strings.Builder var output strings.Builder
forService := ipn.IsServiceName(dnsName) svcName, forService := tailcfg.AsServiceName(dnsName)
var hp ipn.HostPort var hp ipn.HostPort
var webConfig *ipn.WebServerConfig var webConfig *ipn.WebServerConfig
var tcpHandler *ipn.TCPPortHandler var tcpHandler *ipn.TCPPortHandler
ips := st.TailscaleIPs ips := st.TailscaleIPs
host := dnsName host := dnsName
if forService { if forService {
host = tailcfg.ServiceName(dnsName).WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix host = strings.Join([]string{svcName.WithoutPrefix(), st.CurrentTailnet.MagicDNSSuffix}, ".")
} }
hp = ipn.HostPort(net.JoinHostPort(host, strconv.Itoa(int(srvPort)))) hp = ipn.HostPort(net.JoinHostPort(host, strconv.Itoa(int(srvPort))))
@ -578,7 +577,6 @@ func (e *serveEnv) messageForPort(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN
return "", "" return "", ""
} }
if forService { if forService {
svcName := tailcfg.ServiceName(dnsName)
serviceIPMaps, err := tailcfg.UnmarshalNodeCapJSON[tailcfg.ServiceIPMappings](st.Self.CapMap, tailcfg.NodeAttrServiceHost) serviceIPMaps, err := tailcfg.UnmarshalNodeCapJSON[tailcfg.ServiceIPMappings](st.Self.CapMap, tailcfg.NodeAttrServiceHost)
if err != nil || len(serviceIPMaps) == 0 || serviceIPMaps[0][svcName] == nil { if err != nil || len(serviceIPMaps) == 0 || serviceIPMaps[0][svcName] == nil {
// The capmap does not contain IPs for this service yet. Usually this means // The capmap does not contain IPs for this service yet. Usually this means
@ -924,12 +922,12 @@ func (e *serveEnv) removeWebServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN
if sc == nil { if sc == nil {
return nil return nil
} }
forService := ipn.IsServiceName(dnsName)
portStr := strconv.Itoa(int(srvPort)) portStr := strconv.Itoa(int(srvPort))
hostName := dnsName hostName := dnsName
webServeMap := sc.Web webServeMap := sc.Web
svcName, forService := tailcfg.AsServiceName(dnsName)
if forService { if forService {
svcName := tailcfg.ServiceName(dnsName)
svc := sc.Services[svcName] svc := sc.Services[svcName]
if svc == nil { if svc == nil {
return errors.New("service does not exist") return errors.New("service does not exist")

View File

@ -177,8 +177,8 @@ func (sc *ServeConfig) GetWebHandler(dnsName string, hp HostPort, mount string)
if sc == nil { if sc == nil {
return nil return nil
} }
if IsServiceName(dnsName) { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
if svc, ok := sc.Services[tailcfg.ServiceName(dnsName)]; ok && svc.Web != nil { if svc, ok := sc.Services[svcName]; ok && svc.Web != nil {
if webCfg, ok := svc.Web[hp]; ok { if webCfg, ok := svc.Web[hp]; ok {
return webCfg.Handlers[mount] return webCfg.Handlers[mount]
} }
@ -197,8 +197,8 @@ func (sc *ServeConfig) GetTCPPortHandler(port uint16, dnsName string) *TCPPortHa
if sc == nil { if sc == nil {
return nil return nil
} }
if IsServiceName(dnsName) { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
if svc, ok := sc.Services[tailcfg.ServiceName(dnsName)]; ok && svc != nil { if svc, ok := sc.Services[svcName]; ok && svc != nil {
return svc.TCP[port] return svc.TCP[port]
} }
return nil return nil
@ -244,18 +244,6 @@ func (sc *ServeConfig) IsTCPForwardingAny() bool {
return false return false
} }
// IsServiceName reports whether if the given string is a valid service name.
func IsServiceName(s string) bool {
return tailcfg.ServiceName(s).Validate() == nil
}
// asServiceName reports whether if the given string is a valid service name,
// and if so returns the name as a [tailcfg.ServiceName].
func asServiceName(s string) (svcName tailcfg.ServiceName, ok bool) {
svcName = tailcfg.ServiceName(s)
return svcName, svcName.Validate() == nil
}
// IsTCPForwardingOnPort reports whether ServeConfig is currently forwarding // IsTCPForwardingOnPort reports whether ServeConfig is currently forwarding
// in TCPForward mode on the given port for a DNSName. DNSName will be either node's DNSName, or a // in TCPForward mode on the given port for a DNSName. DNSName will be either node's DNSName, or a
// serviceName for service hosted on node. This is exclusive of Web/HTTPS serving. // serviceName for service hosted on node. This is exclusive of Web/HTTPS serving.
@ -263,9 +251,9 @@ func (sc *ServeConfig) IsTCPForwardingOnPort(port uint16, dnsName string) bool {
if sc == nil { if sc == nil {
return false return false
} }
forService := IsServiceName(dnsName)
if forService { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
svc, ok := sc.Services[tailcfg.ServiceName(dnsName)] svc, ok := sc.Services[svcName]
if !ok || svc == nil { if !ok || svc == nil {
return false return false
} }
@ -293,7 +281,7 @@ func (sc *ServeConfig) IsServingHTTPS(port uint16, dnsName string) bool {
return false return false
} }
var tcpHandlers map[uint16]*TCPPortHandler var tcpHandlers map[uint16]*TCPPortHandler
if svcName, ok := asServiceName(dnsName); ok { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
if svc := sc.Services[svcName]; svc != nil { if svc := sc.Services[svcName]; svc != nil {
tcpHandlers = svc.TCP tcpHandlers = svc.TCP
} }
@ -315,8 +303,8 @@ func (sc *ServeConfig) IsServingHTTP(port uint16, dnsName string) bool {
if sc == nil { if sc == nil {
return false return false
} }
if IsServiceName(dnsName) { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
if svc, ok := sc.Services[tailcfg.ServiceName(dnsName)]; ok && svc != nil { if svc, ok := sc.Services[svcName]; ok && svc != nil {
if svc.TCP[port] != nil { if svc.TCP[port] != nil {
return svc.TCP[port].HTTP return svc.TCP[port].HTTP
} }
@ -359,8 +347,7 @@ func (sc *ServeConfig) SetWebHandler(st *ipnstate.Status, handler *HTTPHandler,
tcpMap := &sc.TCP tcpMap := &sc.TCP
webServerMap := &sc.Web webServerMap := &sc.Web
hostName := host hostName := host
if IsServiceName(host) { if svcName, ok := tailcfg.AsServiceName(host); ok {
svcName := tailcfg.ServiceName(host)
hostName = svcName.WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix hostName = svcName.WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix
if _, ok := sc.Services[svcName]; !ok { if _, ok := sc.Services[svcName]; !ok {
mak.Set(&sc.Services, svcName, new(ServiceConfig)) mak.Set(&sc.Services, svcName, new(ServiceConfig))
@ -403,8 +390,7 @@ func (sc *ServeConfig) SetTCPForwarding(port uint16, fwdAddr string, terminateTL
sc = new(ServeConfig) sc = new(ServeConfig)
} }
tcpPortHandler := &sc.TCP tcpPortHandler := &sc.TCP
if IsServiceName(host) { if svcName, ok := tailcfg.AsServiceName(host); ok {
svcName := tailcfg.ServiceName(host)
svcConfig, ok := sc.Services[svcName] svcConfig, ok := sc.Services[svcName]
if !ok { if !ok {
svcConfig = new(ServiceConfig) svcConfig = new(ServiceConfig)
@ -499,8 +485,8 @@ func (sc *ServeConfig) RemoveServiceWebHandler(st *ipnstate.Status, svcName tail
// RemoveTCPForwarding deletes the TCP forwarding configuration for the given // RemoveTCPForwarding deletes the TCP forwarding configuration for the given
// port from the serve config. // port from the serve config.
func (sc *ServeConfig) RemoveTCPForwarding(dnsName string, port uint16) { func (sc *ServeConfig) RemoveTCPForwarding(dnsName string, port uint16) {
if IsServiceName(dnsName) { if svcName, ok := tailcfg.AsServiceName(dnsName); ok {
if svc, ok := sc.Services[tailcfg.ServiceName(dnsName)]; ok && svc != nil { if svc := sc.Services[svcName]; svc != nil {
delete(svc.TCP, port) delete(svc.TCP, port)
if len(svc.TCP) == 0 { if len(svc.TCP) == 0 {
svc.TCP = nil svc.TCP = nil

View File

@ -915,6 +915,13 @@ type TPMInfo struct {
// This is not related to the older [Service] used in [Hostinfo.Services]. // This is not related to the older [Service] used in [Hostinfo.Services].
type ServiceName string type ServiceName string
// AsServiceName reports whether if the given string is a valid service name,
// and if so returns the name as a [tailcfg.ServiceName].
func AsServiceName(s string) (svcName ServiceName, ok bool) {
svcName = ServiceName(s)
return svcName, svcName.Validate() == nil
}
// Validate validates if the service name is formatted correctly. // Validate validates if the service name is formatted correctly.
// We only allow valid DNS labels, since the expectation is that these will be // We only allow valid DNS labels, since the expectation is that these will be
// used as parts of domain names. All errors are [vizerror.Error]. // used as parts of domain names. All errors are [vizerror.Error].