2020-07-28 04:14:28 +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.
|
|
|
|
|
|
|
|
// +build linux,!android
|
|
|
|
|
|
|
|
package controlclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2021-04-02 07:08:58 +00:00
|
|
|
"os"
|
2020-07-28 04:14:28 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"go4.org/mem"
|
|
|
|
"tailscale.com/util/lineread"
|
2020-09-15 15:32:06 +00:00
|
|
|
"tailscale.com/version/distro"
|
2020-07-28 04:14:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
osVersion = osVersionLinux
|
|
|
|
}
|
|
|
|
|
|
|
|
func osVersionLinux() string {
|
2020-09-15 15:32:06 +00:00
|
|
|
dist := distro.Get()
|
|
|
|
propFile := "/etc/os-release"
|
2020-09-22 17:28:40 +00:00
|
|
|
switch dist {
|
|
|
|
case distro.Synology:
|
2020-09-15 15:32:06 +00:00
|
|
|
propFile = "/etc.defaults/VERSION"
|
2020-09-22 17:28:40 +00:00
|
|
|
case distro.OpenWrt:
|
|
|
|
propFile = "/etc/openwrt_release"
|
2020-09-15 15:32:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 04:14:28 +00:00
|
|
|
m := map[string]string{}
|
2020-09-15 15:32:06 +00:00
|
|
|
lineread.File(propFile, func(line []byte) error {
|
2020-07-28 04:14:28 +00:00
|
|
|
eq := bytes.IndexByte(line, '=')
|
|
|
|
if eq == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-22 17:28:40 +00:00
|
|
|
k, v := string(line[:eq]), strings.Trim(string(line[eq+1:]), `"'`)
|
2020-07-28 04:14:28 +00:00
|
|
|
m[k] = v
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
var un syscall.Utsname
|
|
|
|
syscall.Uname(&un)
|
|
|
|
|
|
|
|
var attrBuf strings.Builder
|
|
|
|
attrBuf.WriteString("; kernel=")
|
|
|
|
for _, b := range un.Release {
|
|
|
|
if b == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
attrBuf.WriteByte(byte(b))
|
|
|
|
}
|
|
|
|
if inContainer() {
|
|
|
|
attrBuf.WriteString("; container")
|
|
|
|
}
|
2021-04-02 07:08:58 +00:00
|
|
|
if inKnative() {
|
|
|
|
attrBuf.WriteString("; env=kn")
|
|
|
|
}
|
2021-06-14 21:52:57 +00:00
|
|
|
if inAWSLambda() {
|
2021-04-25 19:38:37 +00:00
|
|
|
attrBuf.WriteString("; env=lm")
|
|
|
|
}
|
2021-05-08 14:40:45 +00:00
|
|
|
if inHerokuDyno() {
|
|
|
|
attrBuf.WriteString("; env=hr")
|
|
|
|
}
|
2021-06-13 14:31:52 +00:00
|
|
|
if inAzureAppService() {
|
|
|
|
attrBuf.WriteString("; env=az")
|
|
|
|
}
|
2020-07-28 04:14:28 +00:00
|
|
|
attr := attrBuf.String()
|
|
|
|
|
|
|
|
id := m["ID"]
|
|
|
|
|
|
|
|
switch id {
|
|
|
|
case "debian":
|
|
|
|
slurp, _ := ioutil.ReadFile("/etc/debian_version")
|
|
|
|
return fmt.Sprintf("Debian %s (%s)%s", bytes.TrimSpace(slurp), m["VERSION_CODENAME"], attr)
|
|
|
|
case "ubuntu":
|
|
|
|
return fmt.Sprintf("Ubuntu %s%s", m["VERSION"], attr)
|
|
|
|
case "", "centos": // CentOS 6 has no /etc/os-release, so its id is ""
|
|
|
|
if cr, _ := ioutil.ReadFile("/etc/centos-release"); len(cr) > 0 { // "CentOS release 6.10 (Final)
|
|
|
|
return fmt.Sprintf("%s%s", bytes.TrimSpace(cr), attr)
|
|
|
|
}
|
|
|
|
fallthrough
|
2020-12-22 04:57:35 +00:00
|
|
|
case "fedora", "rhel", "alpine", "nixos":
|
2020-07-28 04:14:28 +00:00
|
|
|
// Their PRETTY_NAME is fine as-is for all versions I tested.
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
if v := m["PRETTY_NAME"]; v != "" {
|
|
|
|
return fmt.Sprintf("%s%s", v, attr)
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 17:28:40 +00:00
|
|
|
switch dist {
|
|
|
|
case distro.Synology:
|
2020-09-15 15:32:06 +00:00
|
|
|
return fmt.Sprintf("Synology %s%s", m["productversion"], attr)
|
2020-09-22 17:28:40 +00:00
|
|
|
case distro.OpenWrt:
|
|
|
|
return fmt.Sprintf("OpenWrt %s%s", m["DISTRIB_RELEASE"], attr)
|
2020-09-15 15:32:06 +00:00
|
|
|
}
|
2020-07-28 04:14:28 +00:00
|
|
|
return fmt.Sprintf("Other%s", attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func inContainer() (ret bool) {
|
|
|
|
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/")) {
|
|
|
|
ret = true
|
|
|
|
return io.EOF // arbitrary non-nil error to stop loop
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-04-02 07:08:58 +00:00
|
|
|
lineread.File("/proc/mounts", func(line []byte) error {
|
|
|
|
if mem.Contains(mem.B(line), mem.S("fuse.lxcfs")) {
|
|
|
|
ret = true
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-28 04:14:28 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-02 07:08:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-04-25 19:38:37 +00:00
|
|
|
|
2021-06-14 21:52:57 +00:00
|
|
|
func inAWSLambda() bool {
|
2021-04-25 19:38:37 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-06-13 14:31:52 +00:00
|
|
|
|
2021-05-08 14:40:45 +00:00
|
|
|
func inHerokuDyno() bool {
|
|
|
|
// https://devcenter.heroku.com/articles/dynos#local-environment-variables
|
|
|
|
if os.Getenv("PORT") != "" && os.Getenv("DYNO") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-06-13 14:31:52 +00:00
|
|
|
|
|
|
|
func inAzureAppService() bool {
|
|
|
|
if os.Getenv("APPSVC_RUN_ZIP") != "" && os.Getenv("WEBSITE_STACK") != "" &&
|
|
|
|
os.Getenv("WEBSITE_AUTH_AUTO_AAD") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|