2021-06-17 21:16:09 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package hostinfo answers questions about the host environment that Tailscale is
|
|
|
|
// running on.
|
|
|
|
package hostinfo
|
|
|
|
|
|
|
|
import (
|
2021-11-10 16:09:29 +00:00
|
|
|
"bufio"
|
2021-06-17 21:16:09 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2023-01-22 18:16:15 +00:00
|
|
|
"runtime/debug"
|
2021-11-10 16:09:29 +00:00
|
|
|
"strings"
|
2022-09-11 17:57:18 +00:00
|
|
|
"sync"
|
2021-06-17 21:16:09 +00:00
|
|
|
"sync/atomic"
|
2021-11-10 16:09:29 +00:00
|
|
|
"time"
|
2021-06-17 21:16:09 +00:00
|
|
|
|
|
|
|
"go4.org/mem"
|
2022-09-13 14:09:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2021-08-20 17:34:13 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-04-07 18:42:48 +00:00
|
|
|
"tailscale.com/types/opt"
|
2022-12-01 01:33:29 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2022-06-30 05:58:44 +00:00
|
|
|
"tailscale.com/util/cloudenv"
|
2021-08-20 17:34:13 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2021-06-17 21:16:09 +00:00
|
|
|
"tailscale.com/util/lineread"
|
2021-08-20 17:34:13 +00:00
|
|
|
"tailscale.com/version"
|
2021-06-17 21:16:09 +00:00
|
|
|
)
|
|
|
|
|
2022-04-07 18:42:48 +00:00
|
|
|
var started = time.Now()
|
|
|
|
|
2021-08-20 17:34:13 +00:00
|
|
|
// New returns a partially populated Hostinfo for the current host.
|
|
|
|
func New() *tailcfg.Hostinfo {
|
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
hostname = dnsname.FirstLabel(hostname)
|
|
|
|
return &tailcfg.Hostinfo{
|
2022-09-13 14:09:57 +00:00
|
|
|
IPNVersion: version.Long,
|
|
|
|
Hostname: hostname,
|
|
|
|
OS: version.OS(),
|
|
|
|
OSVersion: GetOSVersion(),
|
|
|
|
Container: lazyInContainer.Get(),
|
|
|
|
Distro: condCall(distroName),
|
|
|
|
DistroVersion: condCall(distroVersion),
|
|
|
|
DistroCodeName: condCall(distroCodeName),
|
|
|
|
Env: string(GetEnvType()),
|
|
|
|
Desktop: desktop(),
|
|
|
|
Package: packageTypeCached(),
|
|
|
|
GoArch: runtime.GOARCH,
|
2023-01-22 18:16:15 +00:00
|
|
|
GoArchVar: lazyGoArchVar.Get(),
|
2022-09-13 14:09:57 +00:00
|
|
|
GoVersion: runtime.Version(),
|
2023-01-22 18:16:15 +00:00
|
|
|
Machine: condCall(unameMachine),
|
2022-09-13 14:09:57 +00:00
|
|
|
DeviceModel: deviceModel(),
|
|
|
|
Cloud: string(cloudenv.Get()),
|
|
|
|
NoLogsNoSupport: envknob.NoLogsNoSupport(),
|
2023-01-21 18:04:43 +00:00
|
|
|
AllowsUpdate: envknob.AllowsRemoteUpdate(),
|
2021-08-20 17:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:01:36 +00:00
|
|
|
// non-nil on some platforms
|
|
|
|
var (
|
2022-09-11 17:57:18 +00:00
|
|
|
osVersion func() string
|
|
|
|
packageType func() string
|
|
|
|
distroName func() string
|
|
|
|
distroVersion func() string
|
|
|
|
distroCodeName func() string
|
2023-01-22 18:16:15 +00:00
|
|
|
unameMachine func() string
|
2022-02-22 18:01:36 +00:00
|
|
|
)
|
2021-10-05 20:23:47 +00:00
|
|
|
|
2022-09-11 17:57:18 +00:00
|
|
|
func condCall[T any](fn func() T) T {
|
|
|
|
var zero T
|
|
|
|
if fn == nil {
|
|
|
|
return zero
|
|
|
|
}
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-12-01 01:33:29 +00:00
|
|
|
lazyInContainer = &lazyAtomicValue[opt.Bool]{f: ptr.To(inContainer)}
|
2023-01-22 18:16:15 +00:00
|
|
|
lazyGoArchVar = &lazyAtomicValue[string]{f: ptr.To(goArchVar)}
|
2022-09-11 17:57:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type lazyAtomicValue[T any] struct {
|
|
|
|
// f is a pointer to a fill function. If it's nil or points
|
|
|
|
// to nil, then Get returns the zero value for T.
|
|
|
|
f *func() T
|
|
|
|
|
|
|
|
once sync.Once
|
|
|
|
v T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *lazyAtomicValue[T]) Get() T {
|
|
|
|
v.once.Do(v.fill)
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *lazyAtomicValue[T]) fill() {
|
|
|
|
if v.f == nil || *v.f == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.v = (*v.f)()
|
|
|
|
}
|
|
|
|
|
2021-10-15 02:39:11 +00:00
|
|
|
// GetOSVersion returns the OSVersion of current host if available.
|
|
|
|
func GetOSVersion() string {
|
2021-10-05 20:23:47 +00:00
|
|
|
if s, _ := osVersionAtomic.Load().(string); s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if osVersion != nil {
|
|
|
|
return osVersion()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:01:36 +00:00
|
|
|
func packageTypeCached() string {
|
2021-10-05 22:02:35 +00:00
|
|
|
if v, _ := packagingType.Load().(string); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
2022-02-22 18:01:36 +00:00
|
|
|
if packageType == nil {
|
|
|
|
return ""
|
2021-08-20 17:34:13 +00:00
|
|
|
}
|
2022-02-22 18:01:36 +00:00
|
|
|
v := packageType()
|
|
|
|
if v != "" {
|
|
|
|
SetPackage(v)
|
|
|
|
}
|
|
|
|
return v
|
2021-08-20 17:34:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:16:09 +00:00
|
|
|
// 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")
|
2021-07-05 01:58:58 +00:00
|
|
|
AWSFargate = EnvType("fg")
|
2021-08-09 01:37:37 +00:00
|
|
|
FlyDotIo = EnvType("fly")
|
2021-10-15 02:57:36 +00:00
|
|
|
Kubernetes = EnvType("k8s")
|
2021-11-22 05:32:39 +00:00
|
|
|
DockerDesktop = EnvType("dde")
|
2021-06-17 21:16:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var envType atomic.Value // of EnvType
|
|
|
|
|
|
|
|
func GetEnvType() EnvType {
|
|
|
|
if e, ok := envType.Load().(EnvType); ok {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
e := getEnvType()
|
|
|
|
envType.Store(e)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-10-05 20:23:47 +00:00
|
|
|
var (
|
|
|
|
deviceModelAtomic atomic.Value // of string
|
|
|
|
osVersionAtomic atomic.Value // of string
|
2022-04-07 18:42:48 +00:00
|
|
|
desktopAtomic atomic.Value // of opt.Bool
|
2021-10-05 22:02:35 +00:00
|
|
|
packagingType atomic.Value // of string
|
2021-10-05 20:23:47 +00:00
|
|
|
)
|
2021-08-20 17:34:13 +00:00
|
|
|
|
|
|
|
// SetDeviceModel sets the device model for use in Hostinfo updates.
|
|
|
|
func SetDeviceModel(model string) { deviceModelAtomic.Store(model) }
|
|
|
|
|
2021-10-05 20:23:47 +00:00
|
|
|
// SetOSVersion sets the OS version.
|
|
|
|
func SetOSVersion(v string) { osVersionAtomic.Store(v) }
|
|
|
|
|
2021-10-05 22:02:35 +00:00
|
|
|
// SetPackage sets the packaging type for the app.
|
2022-03-25 15:53:42 +00:00
|
|
|
//
|
|
|
|
// As of 2022-03-25, this is used by Android ("nogoogle" for the
|
|
|
|
// F-Droid build) and tsnet (set to "tsnet").
|
2021-10-05 22:02:35 +00:00
|
|
|
func SetPackage(v string) { packagingType.Store(v) }
|
|
|
|
|
2021-08-20 17:34:13 +00:00
|
|
|
func deviceModel() string {
|
|
|
|
s, _ := deviceModelAtomic.Load().(string)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-04-07 18:42:48 +00:00
|
|
|
func desktop() (ret opt.Bool) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return opt.Bool("")
|
|
|
|
}
|
|
|
|
if v := desktopAtomic.Load(); v != nil {
|
|
|
|
v, _ := v.(opt.Bool)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
seenDesktop := false
|
|
|
|
lineread.File("/proc/net/unix", func(line []byte) error {
|
|
|
|
seenDesktop = seenDesktop || mem.Contains(mem.B(line), mem.S(" @/tmp/dbus-"))
|
|
|
|
seenDesktop = seenDesktop || mem.Contains(mem.B(line), mem.S(".X11-unix"))
|
|
|
|
seenDesktop = seenDesktop || mem.Contains(mem.B(line), mem.S("/wayland-1"))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
ret.Set(seenDesktop)
|
|
|
|
|
|
|
|
// Only cache after a minute - compositors might not have started yet.
|
|
|
|
if time.Since(started) > time.Minute {
|
|
|
|
desktopAtomic.Store(ret)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:16:09 +00:00
|
|
|
func getEnvType() EnvType {
|
|
|
|
if inKnative() {
|
|
|
|
return KNative
|
|
|
|
}
|
|
|
|
if inAWSLambda() {
|
|
|
|
return AWSLambda
|
|
|
|
}
|
|
|
|
if inHerokuDyno() {
|
|
|
|
return Heroku
|
|
|
|
}
|
|
|
|
if inAzureAppService() {
|
|
|
|
return AzureAppService
|
|
|
|
}
|
2021-07-05 01:58:58 +00:00
|
|
|
if inAWSFargate() {
|
|
|
|
return AWSFargate
|
|
|
|
}
|
2021-08-09 01:37:37 +00:00
|
|
|
if inFlyDotIo() {
|
|
|
|
return FlyDotIo
|
|
|
|
}
|
2021-10-15 02:57:36 +00:00
|
|
|
if inKubernetes() {
|
|
|
|
return Kubernetes
|
|
|
|
}
|
2021-11-22 05:32:39 +00:00
|
|
|
if inDockerDesktop() {
|
|
|
|
return DockerDesktop
|
|
|
|
}
|
2021-06-17 21:16:09 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:34:13 +00:00
|
|
|
// inContainer reports whether we're running in a container.
|
2022-09-11 17:57:18 +00:00
|
|
|
func inContainer() opt.Bool {
|
2021-06-17 21:16:09 +00:00
|
|
|
if runtime.GOOS != "linux" {
|
2022-09-11 17:57:18 +00:00
|
|
|
return ""
|
2021-06-17 21:16:09 +00:00
|
|
|
}
|
2022-09-11 17:57:18 +00:00
|
|
|
var ret opt.Bool
|
|
|
|
ret.Set(false)
|
2022-09-12 04:45:08 +00:00
|
|
|
if _, err := os.Stat("/.dockerenv"); err == nil {
|
|
|
|
ret.Set(true)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
if _, err := os.Stat("/run/.containerenv"); err == nil {
|
|
|
|
// See https://github.com/cri-o/cri-o/issues/5461
|
|
|
|
ret.Set(true)
|
|
|
|
return ret
|
|
|
|
}
|
2021-06-17 21:16:09 +00:00
|
|
|
lineread.File("/proc/1/cgroup", func(line []byte) error {
|
|
|
|
if mem.Contains(mem.B(line), mem.S("/docker/")) ||
|
|
|
|
mem.Contains(mem.B(line), mem.S("/lxc/")) {
|
2022-09-11 17:57:18 +00:00
|
|
|
ret.Set(true)
|
2021-06-17 21:16:09 +00:00
|
|
|
return io.EOF // arbitrary non-nil error to stop loop
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
lineread.File("/proc/mounts", func(line []byte) error {
|
|
|
|
if mem.Contains(mem.B(line), mem.S("fuse.lxcfs")) {
|
2022-09-11 17:57:18 +00:00
|
|
|
ret.Set(true)
|
2021-06-17 21:16:09 +00:00
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func inKnative() bool {
|
|
|
|
// https://cloud.google.com/run/docs/reference/container-contract#env-vars
|
|
|
|
if os.Getenv("K_REVISION") != "" && os.Getenv("K_CONFIGURATION") != "" &&
|
|
|
|
os.Getenv("K_SERVICE") != "" && os.Getenv("PORT") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func inAWSLambda() bool {
|
|
|
|
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
|
|
|
|
if os.Getenv("AWS_LAMBDA_FUNCTION_NAME") != "" &&
|
|
|
|
os.Getenv("AWS_LAMBDA_FUNCTION_VERSION") != "" &&
|
|
|
|
os.Getenv("AWS_LAMBDA_INITIALIZATION_TYPE") != "" &&
|
|
|
|
os.Getenv("AWS_LAMBDA_RUNTIME_API") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func inHerokuDyno() bool {
|
|
|
|
// https://devcenter.heroku.com/articles/dynos#local-environment-variables
|
|
|
|
if os.Getenv("PORT") != "" && os.Getenv("DYNO") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func inAzureAppService() bool {
|
|
|
|
if os.Getenv("APPSVC_RUN_ZIP") != "" && os.Getenv("WEBSITE_STACK") != "" &&
|
|
|
|
os.Getenv("WEBSITE_AUTH_AUTO_AAD") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-07-05 01:58:58 +00:00
|
|
|
|
|
|
|
func inAWSFargate() bool {
|
|
|
|
if os.Getenv("AWS_EXECUTION_ENV") == "AWS_ECS_FARGATE" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-08-09 01:37:37 +00:00
|
|
|
|
|
|
|
func inFlyDotIo() bool {
|
|
|
|
if os.Getenv("FLY_APP_NAME") != "" && os.Getenv("FLY_REGION") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-10-15 02:57:36 +00:00
|
|
|
|
|
|
|
func inKubernetes() bool {
|
|
|
|
if os.Getenv("KUBERNETES_SERVICE_HOST") != "" && os.Getenv("KUBERNETES_SERVICE_PORT") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-11-10 16:09:29 +00:00
|
|
|
|
2021-11-22 05:32:39 +00:00
|
|
|
func inDockerDesktop() bool {
|
|
|
|
if os.Getenv("TS_HOST_ENV") == "dde" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-22 18:16:15 +00:00
|
|
|
// goArchVar returns the GOARM or GOAMD64 etc value that the binary was built
|
|
|
|
// with.
|
|
|
|
func goArchVar() string {
|
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// Look for GOARM, GOAMD64, GO386, etc. Note that the little-endian
|
|
|
|
// "le"-suffixed GOARCH values don't have their own environment variable.
|
|
|
|
//
|
|
|
|
// See https://pkg.go.dev/cmd/go#hdr-Environment_variables and the
|
|
|
|
// "Architecture-specific environment variables" section:
|
|
|
|
wantKey := "GO" + strings.ToUpper(strings.TrimSuffix(runtime.GOARCH, "le"))
|
|
|
|
for _, s := range bi.Settings {
|
|
|
|
if s.Key == wantKey {
|
|
|
|
return s.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-11-10 16:09:29 +00:00
|
|
|
type etcAptSrcResult struct {
|
|
|
|
mod time.Time
|
|
|
|
disabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var etcAptSrcCache atomic.Value // of etcAptSrcResult
|
|
|
|
|
|
|
|
// DisabledEtcAptSource reports whether Ubuntu (or similar) has disabled
|
|
|
|
// the /etc/apt/sources.list.d/tailscale.list file contents upon upgrade
|
|
|
|
// to a new release of the distro.
|
|
|
|
//
|
|
|
|
// See https://github.com/tailscale/tailscale/issues/3177
|
|
|
|
func DisabledEtcAptSource() bool {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const path = "/etc/apt/sources.list.d/tailscale.list"
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil || !fi.Mode().IsRegular() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
mod := fi.ModTime()
|
|
|
|
if c, ok := etcAptSrcCache.Load().(etcAptSrcResult); ok && c.mod == mod {
|
|
|
|
return c.disabled
|
|
|
|
}
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
v := etcAptSourceFileIsDisabled(f)
|
|
|
|
etcAptSrcCache.Store(etcAptSrcResult{mod: mod, disabled: v})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func etcAptSourceFileIsDisabled(r io.Reader) bool {
|
|
|
|
bs := bufio.NewScanner(r)
|
|
|
|
disabled := false // did we find the "disabled on upgrade" comment?
|
|
|
|
for bs.Scan() {
|
|
|
|
line := strings.TrimSpace(bs.Text())
|
|
|
|
if strings.Contains(line, "# disabled on upgrade") {
|
|
|
|
disabled = true
|
|
|
|
}
|
|
|
|
if line == "" || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Well, it has some contents in it at least.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return disabled
|
|
|
|
}
|