mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-12 05:37:32 +00:00
tailcfg: add ServiceName
Rather than using a string everywhere and needing to clarify that the string should have the svc: prefix, create a separate type for Service names. Updates tailscale/corp#24607 Change-Id: I720e022f61a7221644bb60955b72cacf42f59960 Signed-off-by: Adrian Dewhurst <adrian@tailscale.com>
This commit is contained in:

committed by
Adrian Dewhurst

parent
d1b378504c
commit
0fa7b4a236
@@ -3417,13 +3417,13 @@ func generateInterceptVIPServicesTCPPortFunc(svcAddrPorts map[netip.Addr]func(ui
|
||||
|
||||
// setVIPServicesTCPPortsIntercepted populates b.shouldInterceptVIPServicesTCPPortAtomic with an
|
||||
// efficient func for ShouldInterceptTCPPort to use, which is called on every incoming packet.
|
||||
func (b *LocalBackend) setVIPServicesTCPPortsIntercepted(svcPorts map[string][]uint16) {
|
||||
func (b *LocalBackend) setVIPServicesTCPPortsIntercepted(svcPorts map[tailcfg.ServiceName][]uint16) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.setVIPServicesTCPPortsInterceptedLocked(svcPorts)
|
||||
}
|
||||
|
||||
func (b *LocalBackend) setVIPServicesTCPPortsInterceptedLocked(svcPorts map[string][]uint16) {
|
||||
func (b *LocalBackend) setVIPServicesTCPPortsInterceptedLocked(svcPorts map[tailcfg.ServiceName][]uint16) {
|
||||
if len(svcPorts) == 0 {
|
||||
b.shouldInterceptVIPServicesTCPPortAtomic.Store(func(netip.AddrPort) bool { return false })
|
||||
return
|
||||
@@ -6025,7 +6025,7 @@ func (b *LocalBackend) reloadServeConfigLocked(prefs ipn.PrefsView) {
|
||||
// b.mu must be held.
|
||||
func (b *LocalBackend) setTCPPortsInterceptedFromNetmapAndPrefsLocked(prefs ipn.PrefsView) {
|
||||
handlePorts := make([]uint16, 0, 4)
|
||||
var vipServicesPorts map[string][]uint16
|
||||
var vipServicesPorts map[tailcfg.ServiceName][]uint16
|
||||
|
||||
if prefs.Valid() && prefs.RunSSH() && envknob.CanSSHD() {
|
||||
handlePorts = append(handlePorts, 22)
|
||||
@@ -7815,7 +7815,7 @@ func (b *LocalBackend) vipServiceHash(services []*tailcfg.VIPService) string {
|
||||
|
||||
func (b *LocalBackend) vipServicesFromPrefsLocked(prefs ipn.PrefsView) []*tailcfg.VIPService {
|
||||
// keyed by service name
|
||||
var services map[string]*tailcfg.VIPService
|
||||
var services map[tailcfg.ServiceName]*tailcfg.VIPService
|
||||
if !b.serveConfig.Valid() {
|
||||
return nil
|
||||
}
|
||||
@@ -7828,12 +7828,13 @@ func (b *LocalBackend) vipServicesFromPrefsLocked(prefs ipn.PrefsView) []*tailcf
|
||||
}
|
||||
|
||||
for _, s := range prefs.AdvertiseServices().All() {
|
||||
if services == nil || services[s] == nil {
|
||||
mak.Set(&services, s, &tailcfg.VIPService{
|
||||
Name: s,
|
||||
sn := tailcfg.ServiceName(s)
|
||||
if services == nil || services[sn] == nil {
|
||||
mak.Set(&services, sn, &tailcfg.VIPService{
|
||||
Name: sn,
|
||||
})
|
||||
}
|
||||
services[s].Active = true
|
||||
services[sn].Active = true
|
||||
}
|
||||
|
||||
return slicesx.MapValues(services)
|
||||
|
@@ -2715,7 +2715,7 @@ func TestTCPHandlerForDstWithVIPService(t *testing.T) {
|
||||
|
||||
err = b.setServeConfigLocked(
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:foo": {
|
||||
TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
882: {HTTP: true},
|
||||
@@ -4747,7 +4747,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
"served-only",
|
||||
[]string{},
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:abc": {Tun: true},
|
||||
},
|
||||
},
|
||||
@@ -4762,7 +4762,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
"served-and-advertised",
|
||||
[]string{"svc:abc"},
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:abc": {Tun: true},
|
||||
},
|
||||
},
|
||||
@@ -4778,7 +4778,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
"served-and-advertised-different-service",
|
||||
[]string{"svc:def"},
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:abc": {Tun: true},
|
||||
},
|
||||
},
|
||||
@@ -4797,7 +4797,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
"served-with-port-ranges-one-range-single",
|
||||
[]string{},
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:abc": {TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTPS: true},
|
||||
}},
|
||||
@@ -4814,7 +4814,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
"served-with-port-ranges-one-range-multiple",
|
||||
[]string{},
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:abc": {TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTPS: true},
|
||||
81: {HTTPS: true},
|
||||
@@ -4833,7 +4833,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
"served-with-port-ranges-multiple-ranges",
|
||||
[]string{},
|
||||
&ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:abc": {TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTPS: true},
|
||||
81: {HTTPS: true},
|
||||
@@ -4866,7 +4866,7 @@ func TestGetVIPServices(t *testing.T) {
|
||||
}
|
||||
got := lb.vipServicesFromPrefsLocked(prefs.View())
|
||||
slices.SortFunc(got, func(a, b *tailcfg.VIPService) int {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
return strings.Compare(a.Name.String(), b.Name.String())
|
||||
})
|
||||
if !reflect.DeepEqual(tt.want, got) {
|
||||
t.Logf("want:")
|
||||
|
@@ -55,7 +55,7 @@ var serveHTTPContextKey ctxkey.Key[*serveHTTPContext]
|
||||
|
||||
type serveHTTPContext struct {
|
||||
SrcAddr netip.AddrPort
|
||||
ForVIPService string // VIP service name, empty string means local
|
||||
ForVIPService tailcfg.ServiceName // "" means local
|
||||
DestPort uint16
|
||||
|
||||
// provides funnel-specific context, nil if not funneled
|
||||
@@ -1006,7 +1006,7 @@ func allNumeric(s string) bool {
|
||||
return s != ""
|
||||
}
|
||||
|
||||
func (b *LocalBackend) webServerConfig(hostname string, forVIPService string, port uint16) (c ipn.WebServerConfigView, ok bool) {
|
||||
func (b *LocalBackend) webServerConfig(hostname string, forVIPService tailcfg.ServiceName, port uint16) (c ipn.WebServerConfigView, ok bool) {
|
||||
key := ipn.HostPort(fmt.Sprintf("%s:%v", hostname, port))
|
||||
|
||||
b.mu.Lock()
|
||||
@@ -1021,7 +1021,7 @@ func (b *LocalBackend) webServerConfig(hostname string, forVIPService string, po
|
||||
return b.serveConfig.FindWeb(key)
|
||||
}
|
||||
|
||||
func (b *LocalBackend) getTLSServeCertForPort(port uint16, forVIPService string) func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
func (b *LocalBackend) getTLSServeCertForPort(port uint16, forVIPService tailcfg.ServiceName) func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
return func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
if hi == nil || hi.ServerName == "" {
|
||||
return nil, errors.New("no SNI ServerName")
|
||||
|
@@ -354,7 +354,7 @@ func TestServeConfigServices(t *testing.T) {
|
||||
{
|
||||
name: "one-incorrectly-configured-service",
|
||||
conf: &ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:foo": {
|
||||
TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTP: true},
|
||||
@@ -369,7 +369,7 @@ func TestServeConfigServices(t *testing.T) {
|
||||
// one correctly configured service with packet should be intercepted
|
||||
name: "one-service-intercept-packet",
|
||||
conf: &ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:foo": {
|
||||
TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTP: true},
|
||||
@@ -388,7 +388,7 @@ func TestServeConfigServices(t *testing.T) {
|
||||
// one correctly configured service with packet should not be intercepted
|
||||
name: "one-service-not-intercept-packet",
|
||||
conf: &ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:foo": {
|
||||
TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTP: true},
|
||||
@@ -406,10 +406,10 @@ func TestServeConfigServices(t *testing.T) {
|
||||
intercepted: false,
|
||||
},
|
||||
{
|
||||
//multiple correctly configured service with packet should be intercepted
|
||||
// multiple correctly configured service with packet should be intercepted
|
||||
name: "multiple-service-intercept-packet",
|
||||
conf: &ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:foo": {
|
||||
TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTP: true},
|
||||
@@ -437,7 +437,7 @@ func TestServeConfigServices(t *testing.T) {
|
||||
// multiple correctly configured service with packet should not be intercepted
|
||||
name: "multiple-service-not-intercept-packet",
|
||||
conf: &ipn.ServeConfig{
|
||||
Services: map[string]*ipn.ServiceConfig{
|
||||
Services: map[tailcfg.ServiceName]*ipn.ServiceConfig{
|
||||
"svc:foo": {
|
||||
TCP: map[uint16]*ipn.TCPPortHandler{
|
||||
80: {HTTP: true},
|
||||
|
Reference in New Issue
Block a user