2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-08-27 19:55:41 +00:00
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
2022-09-18 04:32:21 +00:00
|
|
|
"encoding/json"
|
2023-01-21 18:04:43 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-08-27 19:55:41 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-01-21 18:04:43 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-10-04 03:39:45 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2022-09-18 04:32:21 +00:00
|
|
|
|
2023-01-21 18:04:43 +00:00
|
|
|
"tailscale.com/envknob"
|
2022-09-18 04:32:21 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-09-26 17:52:41 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
|
|
|
"tailscale.com/util/goroutines"
|
2023-01-21 18:04:43 +00:00
|
|
|
"tailscale.com/version"
|
|
|
|
"tailscale.com/version/distro"
|
2022-08-27 19:55:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *LocalBackend) handleC2N(w http.ResponseWriter, r *http.Request) {
|
2022-09-26 17:52:41 +00:00
|
|
|
writeJSON := func(v any) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(v)
|
|
|
|
}
|
2022-08-27 19:55:41 +00:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/echo":
|
|
|
|
// Test handler.
|
|
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
w.Write(body)
|
2023-01-21 18:04:43 +00:00
|
|
|
case "/update":
|
|
|
|
b.handleC2NUpdate(w, r)
|
2023-01-05 19:00:42 +00:00
|
|
|
case "/logtail/flush":
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "bad method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if b.TryFlushLogs() {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "no log flusher wired up", http.StatusInternalServerError)
|
|
|
|
}
|
2022-09-26 17:52:41 +00:00
|
|
|
case "/debug/goroutines":
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.Write(goroutines.ScrubbedGoroutineDump())
|
|
|
|
case "/debug/prefs":
|
|
|
|
writeJSON(b.Prefs())
|
|
|
|
case "/debug/metrics":
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
clientmetric.WritePrometheusExpositionFormat(w)
|
2022-10-04 03:39:45 +00:00
|
|
|
case "/debug/component-logging":
|
|
|
|
component := r.FormValue("component")
|
|
|
|
secs, _ := strconv.Atoi(r.FormValue("secs"))
|
|
|
|
if secs == 0 {
|
|
|
|
secs -= 1
|
|
|
|
}
|
|
|
|
until := time.Now().Add(time.Duration(secs) * time.Second)
|
|
|
|
err := b.SetComponentDebugLogging(component, until)
|
|
|
|
var res struct {
|
|
|
|
Error string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
}
|
|
|
|
writeJSON(res)
|
2022-09-18 04:32:21 +00:00
|
|
|
case "/ssh/usernames":
|
|
|
|
var req tailcfg.C2NSSHUsernamesRequest
|
|
|
|
if r.Method == "POST" {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res, err := b.getSSHUsernames(&req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2022-09-26 17:52:41 +00:00
|
|
|
writeJSON(res)
|
2022-08-27 19:55:41 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "unknown c2n path", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 18:04:43 +00:00
|
|
|
|
|
|
|
func (b *LocalBackend) handleC2NUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// TODO(bradfitz): add some sort of semaphore that prevents two concurrent
|
|
|
|
// updates, or if one happened in the past 5 minutes, or something.
|
|
|
|
|
|
|
|
// TODO(bradfitz): move this type to some leaf package
|
|
|
|
type updateResponse struct {
|
|
|
|
Err string // error message, if any
|
|
|
|
Enabled bool // user has opted-in to remote updates
|
|
|
|
Supported bool // Tailscale supports updating this OS/platform
|
|
|
|
Started bool
|
|
|
|
}
|
|
|
|
var res updateResponse
|
|
|
|
res.Enabled = envknob.AllowsRemoteUpdate()
|
|
|
|
res.Supported = runtime.GOOS == "windows" || (runtime.GOOS == "linux" && distro.Get() == distro.Debian)
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case "GET", "POST":
|
|
|
|
default:
|
|
|
|
http.Error(w, "bad method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if r.Method == "GET" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.Enabled {
|
|
|
|
res.Err = "not enabled"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.Supported {
|
|
|
|
res.Err = "not supported"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmdTS, err := findCmdTailscale()
|
|
|
|
if err != nil {
|
|
|
|
res.Err = fmt.Sprintf("failed to find cmd/tailscale binary: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var ver struct {
|
|
|
|
Long string `json:"long"`
|
|
|
|
}
|
|
|
|
out, err := exec.Command(cmdTS, "version", "--json").Output()
|
|
|
|
if err != nil {
|
|
|
|
res.Err = fmt.Sprintf("failed to find cmd/tailscale binary: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(out, &ver); err != nil {
|
|
|
|
res.Err = "invalid JSON from cmd/tailscale version --json"
|
|
|
|
return
|
|
|
|
}
|
2023-02-11 06:20:36 +00:00
|
|
|
if ver.Long != version.Long() {
|
2023-01-21 18:04:43 +00:00
|
|
|
res.Err = "cmd/tailscale version mismatch"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd := exec.Command(cmdTS, "update", "--yes")
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
res.Err = fmt.Sprintf("failed to start cmd/tailscale update: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.Started = true
|
|
|
|
|
|
|
|
// TODO(bradfitz,andrew): There might be a race condition here on Windows:
|
|
|
|
// * We start the update process.
|
|
|
|
// * tailscale.exe copies itself and kicks off the update process
|
|
|
|
// * msiexec stops this process during the update before the selfCopy exits(?)
|
|
|
|
// * This doesn't return because the process is dead.
|
|
|
|
//
|
|
|
|
// This seems fairly unlikely, but worth checking.
|
|
|
|
defer cmd.Wait()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// findCmdTailscale looks for the cmd/tailscale that corresponds to the
|
|
|
|
// currently running cmd/tailscaled. It's up to the caller to verify that the
|
|
|
|
// two match, but this function does its best to find the right one. Notably, it
|
|
|
|
// doesn't use $PATH for security reasons.
|
|
|
|
func findCmdTailscale() (string, error) {
|
|
|
|
self, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
if self == "/usr/sbin/tailscaled" {
|
|
|
|
return "/usr/bin/tailscale", nil
|
|
|
|
}
|
|
|
|
return "", errors.New("tailscale not found in expected place")
|
|
|
|
case "windows":
|
|
|
|
dir := filepath.Dir(self)
|
|
|
|
ts := filepath.Join(dir, "tailscale.exe")
|
|
|
|
if fi, err := os.Stat(ts); err == nil && fi.Mode().IsRegular() {
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("tailscale.exe not found in expected place")
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("unsupported OS %v", runtime.GOOS)
|
|
|
|
}
|