tailcfg, hostinfo: put envtype in Hostinfo

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
This commit is contained in:
David Crawshaw 2022-02-07 11:07:10 -08:00
parent df8f02db3f
commit 8b1b50ac27
5 changed files with 34 additions and 30 deletions

View File

@ -35,6 +35,7 @@ func New() *tailcfg.Hostinfo {
Package: packageType(), Package: packageType(),
GoArch: runtime.GOARCH, GoArch: runtime.GOARCH,
DeviceModel: deviceModel(), DeviceModel: deviceModel(),
EnvType: GetEnvType(),
} }
} }
@ -75,25 +76,10 @@ func packageType() string {
return "" return ""
} }
// EnvType represents a known environment type. var envType atomic.Value // of tailcfg.EnvType
// The empty string, the default, means unknown.
type EnvType string
const ( func GetEnvType() tailcfg.EnvType {
KNative = EnvType("kn") if e, ok := envType.Load().(tailcfg.EnvType); ok {
AWSLambda = EnvType("lm")
Heroku = EnvType("hr")
AzureAppService = EnvType("az")
AWSFargate = EnvType("fg")
FlyDotIo = EnvType("fly")
Kubernetes = EnvType("k8s")
DockerDesktop = EnvType("dde")
)
var envType atomic.Value // of EnvType
func GetEnvType() EnvType {
if e, ok := envType.Load().(EnvType); ok {
return e return e
} }
e := getEnvType() e := getEnvType()
@ -123,30 +109,30 @@ func deviceModel() string {
return s return s
} }
func getEnvType() EnvType { func getEnvType() tailcfg.EnvType {
if inKnative() { if inKnative() {
return KNative return tailcfg.KNative
} }
if inAWSLambda() { if inAWSLambda() {
return AWSLambda return tailcfg.AWSLambda
} }
if inHerokuDyno() { if inHerokuDyno() {
return Heroku return tailcfg.Heroku
} }
if inAzureAppService() { if inAzureAppService() {
return AzureAppService return tailcfg.AzureAppService
} }
if inAWSFargate() { if inAWSFargate() {
return AWSFargate return tailcfg.AWSFargate
} }
if inFlyDotIo() { if inFlyDotIo() {
return FlyDotIo return tailcfg.FlyDotIo
} }
if inKubernetes() { if inKubernetes() {
return Kubernetes return tailcfg.Kubernetes
} }
if inDockerDesktop() { if inDockerDesktop() {
return DockerDesktop return tailcfg.DockerDesktop
} }
return "" return ""
} }

View File

@ -18,6 +18,7 @@
"tailscale.com/hostinfo" "tailscale.com/hostinfo"
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
"tailscale.com/net/tshttpproxy" "tailscale.com/net/tshttpproxy"
"tailscale.com/tailcfg"
) )
// LoginEndpointForProxyDetermination is the URL used for testing // LoginEndpointForProxyDetermination is the URL used for testing
@ -153,7 +154,7 @@ func LocalAddresses() (regular, loopback []netaddr.IP, err error) {
// addresses we otherwise wouldn't, like: // addresses we otherwise wouldn't, like:
// + 169.254.x.x (AWS Lambda uses NAT with these) // + 169.254.x.x (AWS Lambda uses NAT with these)
// + IPv6 ULA (Google Cloud Run uses these with address translation) // + IPv6 ULA (Google Cloud Run uses these with address translation)
if hostinfo.GetEnvType() == hostinfo.AWSLambda { if hostinfo.GetEnvType() == tailcfg.AWSLambda {
regular4 = linklocal4 regular4 = linklocal4
} }
regular6 = ula6 regular6 = ula6
@ -615,7 +616,7 @@ func isUsableV4(ip netaddr.IP) bool {
return false return false
} }
if ip.IsLinkLocalUnicast() { if ip.IsLinkLocalUnicast() {
return hostinfo.GetEnvType() == hostinfo.AWSLambda return hostinfo.GetEnvType() == tailcfg.AWSLambda
} }
return true return true
} }

View File

@ -445,6 +445,7 @@ type Hostinfo struct {
ShieldsUp bool `json:",omitempty"` // indicates whether the host is blocking incoming connections ShieldsUp bool `json:",omitempty"` // indicates whether the host is blocking incoming connections
ShareeNode bool `json:",omitempty"` // indicates this node exists in netmap because it's owned by a shared-to user ShareeNode bool `json:",omitempty"` // indicates this node exists in netmap because it's owned by a shared-to user
GoArch string `json:",omitempty"` // the host's GOARCH value (of the running binary) GoArch string `json:",omitempty"` // the host's GOARCH value (of the running binary)
EnvType EnvType `json:",omitempty"` // the host's environment type, if known
RoutableIPs []netaddr.IPPrefix `json:",omitempty"` // set of IP ranges this client can route RoutableIPs []netaddr.IPPrefix `json:",omitempty"` // set of IP ranges this client can route
RequestTags []string `json:",omitempty"` // set of ACL tags this node wants to claim RequestTags []string `json:",omitempty"` // set of ACL tags this node wants to claim
Services []Service `json:",omitempty"` // services advertised by this machine Services []Service `json:",omitempty"` // services advertised by this machine
@ -1369,3 +1370,18 @@ type SetDNSRequest struct {
// Value is the value to add. // Value is the value to add.
Value string Value string
} }
// EnvType represents a known environment type.
// The empty string, the default, means unknown.
type EnvType string
const (
KNative = EnvType("kn")
AWSLambda = EnvType("lm")
Heroku = EnvType("hr")
AzureAppService = EnvType("az")
AWSFargate = EnvType("fg")
FlyDotIo = EnvType("fly")
Kubernetes = EnvType("k8s")
DockerDesktop = EnvType("dde")
)

View File

@ -122,6 +122,7 @@ func (src *Hostinfo) Clone() *Hostinfo {
ShieldsUp bool ShieldsUp bool
ShareeNode bool ShareeNode bool
GoArch string GoArch string
EnvType EnvType
RoutableIPs []netaddr.IPPrefix RoutableIPs []netaddr.IPPrefix
RequestTags []string RequestTags []string
Services []Service Services []Service

View File

@ -30,7 +30,7 @@ func TestHostinfoEqual(t *testing.T) {
"IPNVersion", "FrontendLogID", "BackendLogID", "IPNVersion", "FrontendLogID", "BackendLogID",
"OS", "OSVersion", "Package", "DeviceModel", "Hostname", "OS", "OSVersion", "Package", "DeviceModel", "Hostname",
"ShieldsUp", "ShareeNode", "ShieldsUp", "ShareeNode",
"GoArch", "GoArch", "EnvType",
"RoutableIPs", "RequestTags", "RoutableIPs", "RequestTags",
"Services", "NetInfo", "Services", "NetInfo",
} }