2021-02-15 18:41:52 +00:00
|
|
|
// Copyright (c) 2021 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 localapi contains the HTTP server handlers for tailscaled's API server.
|
|
|
|
package localapi
|
|
|
|
|
|
|
|
import (
|
2022-03-29 20:43:26 +01:00
|
|
|
"bytes"
|
2022-11-20 21:25:54 +00:00
|
|
|
"context"
|
2021-03-30 23:59:44 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2021-02-15 18:41:52 +00:00
|
|
|
"encoding/json"
|
2021-04-16 18:57:46 +01:00
|
|
|
"errors"
|
2021-03-30 20:56:00 +01:00
|
|
|
"fmt"
|
2021-02-15 18:41:52 +00:00
|
|
|
"io"
|
2022-11-04 19:12:51 +00:00
|
|
|
"io/ioutil"
|
2022-03-24 16:04:01 +00:00
|
|
|
"net"
|
2021-02-15 18:41:52 +00:00
|
|
|
"net/http"
|
2021-04-06 21:38:47 +01:00
|
|
|
"net/http/httputil"
|
2022-07-26 04:55:44 +01:00
|
|
|
"net/netip"
|
2021-03-30 20:56:00 +01:00
|
|
|
"net/url"
|
2021-03-05 20:07:00 +00:00
|
|
|
"runtime"
|
2021-03-19 04:07:58 +00:00
|
|
|
"strconv"
|
2021-03-30 20:56:00 +01:00
|
|
|
"strings"
|
2022-07-08 19:57:34 +01:00
|
|
|
"sync"
|
2021-03-30 23:59:44 +01:00
|
|
|
"time"
|
2021-02-15 18:41:52 +00:00
|
|
|
|
2022-11-10 21:38:22 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2021-04-13 16:13:46 +01:00
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2022-09-13 15:09:57 +01:00
|
|
|
"tailscale.com/envknob"
|
2022-10-03 15:54:46 +01:00
|
|
|
"tailscale.com/health"
|
|
|
|
"tailscale.com/hostinfo"
|
2021-04-12 00:10:31 +01:00
|
|
|
"tailscale.com/ipn"
|
2021-02-15 18:41:52 +00:00
|
|
|
"tailscale.com/ipn/ipnlocal"
|
2021-03-19 04:07:58 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-03-24 16:04:01 +00:00
|
|
|
"tailscale.com/net/netutil"
|
2022-11-16 15:19:07 +00:00
|
|
|
"tailscale.com/safesocket"
|
2021-02-15 18:41:52 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-08-11 18:43:09 +01:00
|
|
|
"tailscale.com/tka"
|
2022-10-31 23:47:51 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-03-30 23:59:44 +01:00
|
|
|
"tailscale.com/types/logger"
|
2022-12-01 22:39:03 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2020-12-14 02:51:24 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
2022-09-10 20:11:59 +01:00
|
|
|
"tailscale.com/util/mak"
|
2022-10-09 16:57:02 +01:00
|
|
|
"tailscale.com/util/strs"
|
2021-08-19 16:36:13 +01:00
|
|
|
"tailscale.com/version"
|
2021-02-15 18:41:52 +00:00
|
|
|
)
|
|
|
|
|
2022-10-09 16:57:02 +01:00
|
|
|
type localAPIHandler func(*Handler, http.ResponseWriter, *http.Request)
|
|
|
|
|
|
|
|
// handler is the set of LocalAPI handlers, keyed by the part of the
|
|
|
|
// Request.URL.Path after "/localapi/v0/". If the key ends with a trailing slash
|
|
|
|
// then it's a prefix match.
|
|
|
|
var handler = map[string]localAPIHandler{
|
|
|
|
// The prefix match handlers end with a slash:
|
|
|
|
"cert/": (*Handler).serveCert,
|
|
|
|
"file-put/": (*Handler).serveFilePut,
|
|
|
|
"files/": (*Handler).serveFiles,
|
2022-11-10 21:38:22 +00:00
|
|
|
"profiles/": (*Handler).serveProfiles,
|
2022-10-09 16:57:02 +01:00
|
|
|
|
|
|
|
// The other /localapi/v0/NAME handlers are exact matches and contain only NAME
|
|
|
|
// without a trailing slash:
|
|
|
|
"bugreport": (*Handler).serveBugReport,
|
|
|
|
"check-ip-forwarding": (*Handler).serveCheckIPForwarding,
|
|
|
|
"check-prefs": (*Handler).serveCheckPrefs,
|
|
|
|
"component-debug-logging": (*Handler).serveComponentDebugLogging,
|
|
|
|
"debug": (*Handler).serveDebug,
|
2022-11-26 22:23:00 +00:00
|
|
|
"debug-derp-region": (*Handler).serveDebugDERPRegion,
|
2022-10-09 16:57:02 +01:00
|
|
|
"derpmap": (*Handler).serveDERPMap,
|
2022-11-07 21:04:10 +00:00
|
|
|
"dev-set-state-store": (*Handler).serveDevSetStateStore,
|
2022-10-09 16:57:02 +01:00
|
|
|
"dial": (*Handler).serveDial,
|
|
|
|
"file-targets": (*Handler).serveFileTargets,
|
|
|
|
"goroutines": (*Handler).serveGoroutines,
|
|
|
|
"id-token": (*Handler).serveIDToken,
|
|
|
|
"login-interactive": (*Handler).serveLoginInteractive,
|
|
|
|
"logout": (*Handler).serveLogout,
|
|
|
|
"metrics": (*Handler).serveMetrics,
|
|
|
|
"ping": (*Handler).servePing,
|
|
|
|
"prefs": (*Handler).servePrefs,
|
2022-11-10 19:41:04 +00:00
|
|
|
"pprof": (*Handler).servePprof,
|
2022-11-11 03:58:40 +00:00
|
|
|
"serve-config": (*Handler).serveServeConfig,
|
2022-10-09 16:57:02 +01:00
|
|
|
"set-dns": (*Handler).serveSetDNS,
|
|
|
|
"set-expiry-sooner": (*Handler).serveSetExpirySooner,
|
2022-11-22 19:41:03 +00:00
|
|
|
"start": (*Handler).serveStart,
|
2022-10-09 16:57:02 +01:00
|
|
|
"status": (*Handler).serveStatus,
|
|
|
|
"tka/init": (*Handler).serveTKAInit,
|
2022-11-14 23:04:10 +00:00
|
|
|
"tka/log": (*Handler).serveTKALog,
|
2022-10-09 16:57:02 +01:00
|
|
|
"tka/modify": (*Handler).serveTKAModify,
|
2022-10-31 23:47:51 +00:00
|
|
|
"tka/sign": (*Handler).serveTKASign,
|
2022-10-09 16:57:02 +01:00
|
|
|
"tka/status": (*Handler).serveTKAStatus,
|
2022-11-04 19:12:51 +00:00
|
|
|
"tka/disable": (*Handler).serveTKADisable,
|
2022-11-29 00:39:03 +00:00
|
|
|
"tka/force-local-disable": (*Handler).serveTKALocalDisable,
|
2022-10-09 16:57:02 +01:00
|
|
|
"upload-client-metrics": (*Handler).serveUploadClientMetrics,
|
2022-11-22 19:41:03 +00:00
|
|
|
"watch-ipn-bus": (*Handler).serveWatchIPNBus,
|
2022-10-09 16:57:02 +01:00
|
|
|
"whois": (*Handler).serveWhoIs,
|
|
|
|
}
|
|
|
|
|
2021-03-30 23:59:44 +01:00
|
|
|
func randHex(n int) string {
|
|
|
|
b := make([]byte, n)
|
|
|
|
rand.Read(b)
|
|
|
|
return hex.EncodeToString(b)
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:57:34 +01:00
|
|
|
var (
|
|
|
|
// The clientmetrics package is stateful, but we want to expose a simple
|
|
|
|
// imperative API to local clients, so we need to keep track of
|
|
|
|
// clientmetric.Metric instances that we've created for them. These need to
|
|
|
|
// be globals because we end up creating many Handler instances for the
|
|
|
|
// lifetime of a client.
|
|
|
|
metricsMu sync.Mutex
|
|
|
|
metrics = map[string]*clientmetric.Metric{}
|
|
|
|
)
|
|
|
|
|
2021-03-30 23:59:44 +01:00
|
|
|
func NewHandler(b *ipnlocal.LocalBackend, logf logger.Logf, logID string) *Handler {
|
|
|
|
return &Handler{b: b, logf: logf, backendLogID: logID}
|
2021-02-15 18:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
// RequiredPassword, if non-empty, forces all HTTP
|
|
|
|
// requests to have HTTP basic auth with this password.
|
|
|
|
// It's used by the sandboxed macOS sameuserproof GUI auth mechanism.
|
|
|
|
RequiredPassword string
|
|
|
|
|
|
|
|
// PermitRead is whether read-only HTTP handlers are allowed.
|
|
|
|
PermitRead bool
|
|
|
|
|
|
|
|
// PermitWrite is whether mutating HTTP handlers are allowed.
|
2022-01-25 18:33:11 +00:00
|
|
|
// If PermitWrite is true, everything is allowed.
|
|
|
|
// It effectively means that the user is root or the admin
|
|
|
|
// (operator user).
|
2021-02-15 18:41:52 +00:00
|
|
|
PermitWrite bool
|
|
|
|
|
2022-01-25 18:33:11 +00:00
|
|
|
// PermitCert is whether the client is additionally granted
|
|
|
|
// cert fetching access.
|
|
|
|
PermitCert bool
|
|
|
|
|
2021-03-30 23:59:44 +01:00
|
|
|
b *ipnlocal.LocalBackend
|
|
|
|
logf logger.Logf
|
|
|
|
backendLogID string
|
2021-02-15 18:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.b == nil {
|
|
|
|
http.Error(w, "server has no local backend", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-11-16 15:19:07 +00:00
|
|
|
if r.Referer() != "" || r.Header.Get("Origin") != "" || !validHost(r.Host) {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricInvalidRequests.Add(1)
|
2022-11-16 15:19:07 +00:00
|
|
|
http.Error(w, "invalid localapi request", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-08-19 16:36:13 +01:00
|
|
|
w.Header().Set("Tailscale-Version", version.Long)
|
2022-11-17 14:24:21 +00:00
|
|
|
w.Header().Set("Content-Security-Policy", `default-src 'none'; frame-ancestors 'none'; script-src 'none'; script-src-elem 'none'; script-src-attr 'none'`)
|
|
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
2021-02-15 18:41:52 +00:00
|
|
|
if h.RequiredPassword != "" {
|
|
|
|
_, pass, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricInvalidRequests.Add(1)
|
2021-02-15 18:41:52 +00:00
|
|
|
http.Error(w, "auth required", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pass != h.RequiredPassword {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricInvalidRequests.Add(1)
|
2021-02-15 18:41:52 +00:00
|
|
|
http.Error(w, "bad password", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 16:57:02 +01:00
|
|
|
if fn, ok := handlerForPath(r.URL.Path); ok {
|
|
|
|
fn(h, w, r)
|
|
|
|
} else {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 15:19:07 +00:00
|
|
|
// validHost reports whether h is a valid Host header value for a LocalAPI request.
|
|
|
|
func validHost(h string) bool {
|
|
|
|
// The client code sends a hostname of "local-tailscaled.sock".
|
|
|
|
switch h {
|
|
|
|
case "", apitype.LocalAPIHost:
|
|
|
|
return true
|
|
|
|
}
|
2022-11-16 19:35:01 +00:00
|
|
|
// Allow either localhost or loopback IP hosts.
|
|
|
|
host, portStr, err := net.SplitHostPort(h)
|
2022-11-16 15:19:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-11-16 19:35:01 +00:00
|
|
|
port, err := strconv.ParseUint(portStr, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" && port != safesocket.WindowsLocalPort {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if host == "localhost" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
addr, err := netip.ParseAddr(h)
|
|
|
|
if err != nil {
|
2022-11-16 15:19:07 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-11-16 19:35:01 +00:00
|
|
|
return addr.IsLoopback()
|
2022-11-16 15:19:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 16:57:02 +01:00
|
|
|
// handlerForPath returns the LocalAPI handler for the provided Request.URI.Path.
|
|
|
|
// (the path doesn't include any query parameters)
|
|
|
|
func handlerForPath(urlPath string) (h localAPIHandler, ok bool) {
|
|
|
|
if urlPath == "/" {
|
|
|
|
return (*Handler).serveLocalAPIRoot, true
|
|
|
|
}
|
|
|
|
suff, ok := strs.CutPrefix(urlPath, "/localapi/v0/")
|
|
|
|
if !ok {
|
|
|
|
// Currently all LocalAPI methods start with "/localapi/v0/" to signal
|
|
|
|
// to people that they're not necessarily stable APIs. In practice we'll
|
|
|
|
// probably need to keep them pretty stable anyway, but for now treat
|
|
|
|
// them as an internal implementation detail.
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if fn, ok := handler[suff]; ok {
|
|
|
|
// Here we match exact handler suffixes like "status" or ones with a
|
|
|
|
// slash already in their name, like "tka/status".
|
|
|
|
return fn, true
|
|
|
|
}
|
|
|
|
// Otherwise, it might be a prefix match like "files/*" which we look up
|
|
|
|
// by the prefix including first trailing slash.
|
|
|
|
if i := strings.IndexByte(suff, '/'); i != -1 {
|
|
|
|
suff = suff[:i+1]
|
|
|
|
if fn, ok := handler[suff]; ok {
|
|
|
|
return fn, true
|
|
|
|
}
|
2021-02-15 18:41:52 +00:00
|
|
|
}
|
2022-10-09 16:57:02 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Handler) serveLocalAPIRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
io.WriteString(w, "tailscaled\n")
|
2021-02-15 18:41:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:43:26 +01:00
|
|
|
// serveIDToken handles requests to get an OIDC ID token.
|
|
|
|
func (h *Handler) serveIDToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "id-token access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nm := h.b.NetMap()
|
|
|
|
if nm == nil {
|
|
|
|
http.Error(w, "no netmap", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
aud := strings.TrimSpace(r.FormValue("aud"))
|
|
|
|
if len(aud) == 0 {
|
|
|
|
http.Error(w, "no audience requested", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req := &tailcfg.TokenRequest{
|
|
|
|
CapVersion: tailcfg.CurrentCapabilityVersion,
|
|
|
|
Audience: aud,
|
|
|
|
NodeKey: nm.NodeKey,
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httpReq, err := http.NewRequest("POST", "https://unused/machine/id-token", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp, err := h.b.DoNoiseRequest(httpReq)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 23:59:44 +01:00
|
|
|
func (h *Handler) serveBugReport(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "bugreport access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-11-18 05:40:40 +00:00
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2021-03-30 23:59:44 +01:00
|
|
|
|
2022-10-15 18:31:35 +01:00
|
|
|
logMarker := func() string {
|
|
|
|
return fmt.Sprintf("BUG-%v-%v-%v", h.backendLogID, time.Now().UTC().Format("20060102150405Z"), randHex(8))
|
|
|
|
}
|
2022-09-13 15:09:57 +01:00
|
|
|
if envknob.NoLogsNoSupport() {
|
2022-10-15 18:31:35 +01:00
|
|
|
logMarker = func() string { return "BUG-NO-LOGS-NO-SUPPORT-this-node-has-had-its-logging-disabled" }
|
2022-09-13 15:09:57 +01:00
|
|
|
}
|
2022-10-15 18:31:35 +01:00
|
|
|
|
|
|
|
startMarker := logMarker()
|
|
|
|
h.logf("user bugreport: %s", startMarker)
|
|
|
|
if note := r.URL.Query().Get("note"); len(note) > 0 {
|
2021-03-30 23:59:44 +01:00
|
|
|
h.logf("user bugreport note: %s", note)
|
|
|
|
}
|
2022-10-03 15:54:46 +01:00
|
|
|
hi, _ := json.Marshal(hostinfo.New())
|
|
|
|
h.logf("user bugreport hostinfo: %s", hi)
|
|
|
|
if err := health.OverallError(); err != nil {
|
|
|
|
h.logf("user bugreport health: %s", err.Error())
|
|
|
|
} else {
|
|
|
|
h.logf("user bugreport health: ok")
|
|
|
|
}
|
2022-10-15 18:31:35 +01:00
|
|
|
if defBool(r.URL.Query().Get("diagnose"), false) {
|
2022-09-26 18:07:28 +01:00
|
|
|
h.b.Doctor(r.Context(), logger.WithPrefix(h.logf, "diag: "))
|
|
|
|
}
|
2021-03-30 23:59:44 +01:00
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2022-10-15 18:31:35 +01:00
|
|
|
fmt.Fprintln(w, startMarker)
|
|
|
|
|
|
|
|
// Nothing else to do if we're not in record mode; we wrote the marker
|
|
|
|
// above, so we can just finish our response now.
|
|
|
|
if !defBool(r.URL.Query().Get("record"), false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
until := time.Now().Add(12 * time.Hour)
|
|
|
|
|
|
|
|
var changed map[string]bool
|
|
|
|
for _, component := range []string{"magicsock"} {
|
|
|
|
if h.b.GetComponentDebugLogging(component).IsZero() {
|
|
|
|
if err := h.b.SetComponentDebugLogging(component, until); err != nil {
|
|
|
|
h.logf("bugreport: error setting component %q logging: %v", component, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mak.Set(&changed, component, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for component := range changed {
|
|
|
|
h.b.SetComponentDebugLogging(component, time.Time{})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// NOTE(andrew): if we have anything else we want to do while recording
|
|
|
|
// a bugreport, we can add it here.
|
|
|
|
|
|
|
|
// Read from the client; this will also return when the client closes
|
|
|
|
// the connection.
|
|
|
|
var buf [1]byte
|
|
|
|
_, err := r.Body.Read(buf[:])
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
// good
|
|
|
|
case errors.Is(err, io.EOF):
|
|
|
|
// good
|
|
|
|
case errors.Is(err, io.ErrUnexpectedEOF):
|
|
|
|
// this happens when Ctrl-C'ing the tailscale client; don't
|
|
|
|
// bother logging an error
|
|
|
|
default:
|
|
|
|
// Log but continue anyway.
|
|
|
|
h.logf("user bugreport: error reading body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate another log marker and return it to the client.
|
|
|
|
endMarker := logMarker()
|
|
|
|
h.logf("user bugreport end: %s", endMarker)
|
|
|
|
fmt.Fprintln(w, endMarker)
|
2021-03-30 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
2021-02-15 18:41:52 +00:00
|
|
|
func (h *Handler) serveWhoIs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "whois access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b := h.b
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
var ipp netip.AddrPort
|
2021-03-15 21:59:35 +00:00
|
|
|
if v := r.FormValue("addr"); v != "" {
|
2021-02-15 18:41:52 +00:00
|
|
|
var err error
|
2022-07-26 04:55:44 +01:00
|
|
|
ipp, err = netip.ParseAddrPort(v)
|
2021-02-15 18:41:52 +00:00
|
|
|
if err != nil {
|
2021-03-15 21:59:35 +00:00
|
|
|
http.Error(w, "invalid 'addr' parameter", 400)
|
2021-02-15 18:41:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-15 21:59:35 +00:00
|
|
|
http.Error(w, "missing 'addr' parameter", 400)
|
2021-02-15 18:41:52 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-15 21:59:35 +00:00
|
|
|
n, u, ok := b.WhoIs(ipp)
|
2021-02-15 18:41:52 +00:00
|
|
|
if !ok {
|
2021-03-15 21:59:35 +00:00
|
|
|
http.Error(w, "no match for IP:port", 404)
|
2021-02-15 18:41:52 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-13 16:13:46 +01:00
|
|
|
res := &apitype.WhoIsResponse{
|
2021-02-15 18:41:52 +00:00
|
|
|
Node: n,
|
|
|
|
UserProfile: &u,
|
2022-07-25 04:08:42 +01:00
|
|
|
Caps: b.PeerCaps(ipp.Addr()),
|
2021-02-15 18:41:52 +00:00
|
|
|
}
|
|
|
|
j, err := json.MarshalIndent(res, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "JSON encoding error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(j)
|
|
|
|
}
|
2021-03-05 20:07:00 +00:00
|
|
|
|
|
|
|
func (h *Handler) serveGoroutines(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Require write access out of paranoia that the goroutine dump
|
|
|
|
// (at least its arguments) might contain something sensitive.
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "goroutine dump access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := make([]byte, 2<<20)
|
|
|
|
buf = buf[:runtime.Stack(buf, true)]
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
2021-03-19 02:34:59 +00:00
|
|
|
|
2020-12-14 02:51:24 +00:00
|
|
|
func (h *Handler) serveMetrics(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Require write access out of paranoia that the metrics
|
|
|
|
// might contain something sensitive.
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "metric access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
clientmetric.WritePrometheusExpositionFormat(w)
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:39:04 +00:00
|
|
|
func (h *Handler) serveDebug(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "debug access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "POST required", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2022-12-03 04:13:36 +00:00
|
|
|
// The action is normally in a POST form parameter, but
|
|
|
|
// some actions (like "notify") want a full JSON body, so
|
|
|
|
// permit some to have their action in a header.
|
|
|
|
var action string
|
|
|
|
switch v := r.Header.Get("Debug-Action"); v {
|
|
|
|
case "notify":
|
|
|
|
action = v
|
|
|
|
default:
|
|
|
|
action = r.FormValue("action")
|
|
|
|
}
|
2021-12-28 21:39:04 +00:00
|
|
|
var err error
|
|
|
|
switch action {
|
|
|
|
case "rebind":
|
|
|
|
err = h.b.DebugRebind()
|
|
|
|
case "restun":
|
|
|
|
err = h.b.DebugReSTUN()
|
2022-11-25 15:22:20 +00:00
|
|
|
case "enginestatus":
|
|
|
|
// serveRequestEngineStatus kicks off a call to RequestEngineStatus (via
|
|
|
|
// LocalBackend => UserspaceEngine => LocalBackend =>
|
|
|
|
// ipn.Notify{Engine}).
|
|
|
|
//
|
|
|
|
// This is a temporary (2022-11-25) measure for the Windows client's
|
|
|
|
// move to the LocalAPI HTTP interface. It was polling this over the IPN
|
|
|
|
// bus before every 2 seconds which is wasteful. We should add a bit to
|
|
|
|
// WatchIPNMask instead to let an IPN bus watcher say that it's
|
|
|
|
// interested in that info and then only send it on demand, not via
|
|
|
|
// polling. But for now we keep this interface because that's what the
|
|
|
|
// client already did. A future change will remove this, so don't depend
|
|
|
|
// on it.
|
|
|
|
h.b.RequestEngineStatus()
|
2022-12-03 04:13:36 +00:00
|
|
|
case "notify":
|
|
|
|
var n ipn.Notify
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&n)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
h.b.DebugNotify(n)
|
|
|
|
|
2021-12-28 21:39:04 +00:00
|
|
|
case "":
|
|
|
|
err = fmt.Errorf("missing parameter 'action'")
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown action %q", action)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
io.WriteString(w, "done\n")
|
|
|
|
}
|
|
|
|
|
2022-11-07 21:04:10 +00:00
|
|
|
func (h *Handler) serveDevSetStateStore(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "debug access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "POST required", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := h.b.SetDevStateStore(r.FormValue("key"), r.FormValue("value")); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
io.WriteString(w, "done\n")
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:39:45 +01:00
|
|
|
func (h *Handler) serveComponentDebugLogging(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "debug access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
component := r.FormValue("component")
|
|
|
|
secs, _ := strconv.Atoi(r.FormValue("secs"))
|
|
|
|
err := h.b.SetComponentDebugLogging(component, time.Now().Add(time.Duration(secs)*time.Second))
|
|
|
|
var res struct {
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2022-11-10 19:41:04 +00:00
|
|
|
// servePprofFunc is the implementation of Handler.servePprof, after auth,
|
2021-09-23 17:20:14 +01:00
|
|
|
// for platforms where we want to link it in.
|
2022-11-10 19:41:04 +00:00
|
|
|
var servePprofFunc func(http.ResponseWriter, *http.Request)
|
2021-09-23 17:20:14 +01:00
|
|
|
|
2022-11-10 19:41:04 +00:00
|
|
|
func (h *Handler) servePprof(w http.ResponseWriter, r *http.Request) {
|
2021-09-23 17:20:14 +01:00
|
|
|
// Require write access out of paranoia that the profile dump
|
|
|
|
// might contain something sensitive.
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "profile access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-11-10 19:41:04 +00:00
|
|
|
if servePprofFunc == nil {
|
2021-09-23 17:20:14 +01:00
|
|
|
http.Error(w, "not implemented on this platform", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
2022-11-10 19:41:04 +00:00
|
|
|
servePprofFunc(w, r)
|
2021-09-23 17:20:14 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 03:58:40 +00:00
|
|
|
func (h *Handler) serveServeConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
2022-12-08 03:17:40 +00:00
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "serve config denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-11-11 03:58:40 +00:00
|
|
|
config := h.b.ServeConfig()
|
|
|
|
json.NewEncoder(w).Encode(config)
|
|
|
|
case "POST":
|
2022-12-08 03:17:40 +00:00
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "serve config denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-11-11 03:58:40 +00:00
|
|
|
configIn := new(ipn.ServeConfig)
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(configIn); err != nil {
|
2022-12-08 03:17:40 +00:00
|
|
|
writeErrorJSON(w, fmt.Errorf("decoding config: %w", err))
|
2022-11-11 03:58:40 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-08 03:17:40 +00:00
|
|
|
if err := h.b.SetServeConfig(configIn); err != nil {
|
|
|
|
writeErrorJSON(w, fmt.Errorf("updating config: %w", err))
|
2022-11-11 03:58:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-12-08 03:17:40 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
2022-11-11 03:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:55:21 +01:00
|
|
|
func (h *Handler) serveCheckIPForwarding(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "IP forwarding check access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var warning string
|
|
|
|
if err := h.b.CheckIPForwarding(); err != nil {
|
|
|
|
warning = err.Error()
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(struct {
|
|
|
|
Warning string
|
|
|
|
}{
|
|
|
|
Warning: warning,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-19 02:34:59 +00:00
|
|
|
func (h *Handler) serveStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "status access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-03-19 04:07:58 +00:00
|
|
|
var st *ipnstate.Status
|
|
|
|
if defBool(r.FormValue("peers"), true) {
|
|
|
|
st = h.b.Status()
|
|
|
|
} else {
|
|
|
|
st = h.b.StatusWithoutPeers()
|
|
|
|
}
|
2021-03-19 02:34:59 +00:00
|
|
|
e := json.NewEncoder(w)
|
|
|
|
e.SetIndent("", "\t")
|
2021-03-19 04:07:58 +00:00
|
|
|
e.Encode(st)
|
|
|
|
}
|
|
|
|
|
2022-12-01 22:39:03 +00:00
|
|
|
// InUseOtherUserIPNStream reports whether r is a request for the watch-ipn-bus
|
|
|
|
// handler. If so, it writes an ipn.Notify InUseOtherUser message to the user
|
|
|
|
// and returns true. Otherwise it returns false, in which case it doesn't write
|
|
|
|
// to w.
|
|
|
|
//
|
|
|
|
// Unlike the regular watch-ipn-bus handler, this one doesn't block. The caller
|
|
|
|
// (in ipnserver.Server) provides the blocking until the connection is no longer
|
|
|
|
// in use.
|
|
|
|
func InUseOtherUserIPNStream(w http.ResponseWriter, r *http.Request, err error) (handled bool) {
|
|
|
|
if r.Method != "GET" || r.URL.Path != "/localapi/v0/watch-ipn-bus" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
js, err := json.Marshal(&ipn.Notify{
|
|
|
|
Version: version.Long,
|
|
|
|
State: ptr.To(ipn.InUseOtherUser),
|
|
|
|
ErrMessage: ptr.To(err.Error()),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
js = append(js, '\n')
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:41:03 +00:00
|
|
|
func (h *Handler) serveWatchIPNBus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "not a flusher", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
f.Flush()
|
|
|
|
|
2022-11-26 20:19:16 +00:00
|
|
|
var mask ipn.NotifyWatchOpt
|
|
|
|
if s := r.FormValue("mask"); s != "" {
|
|
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bad mask", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mask = ipn.NotifyWatchOpt(v)
|
|
|
|
}
|
2022-11-22 19:41:03 +00:00
|
|
|
ctx := r.Context()
|
2022-11-26 20:19:16 +00:00
|
|
|
h.b.WatchNotifications(ctx, mask, func(roNotify *ipn.Notify) (keepGoing bool) {
|
2022-11-22 19:41:03 +00:00
|
|
|
js, err := json.Marshal(roNotify)
|
|
|
|
if err != nil {
|
|
|
|
h.logf("json.Marshal: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(w, "%s\n", js); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
f.Flush()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:08:27 +01:00
|
|
|
func (h *Handler) serveLoginInteractive(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "login access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "want POST", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.b.StartLoginInteractive()
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:41:03 +00:00
|
|
|
func (h *Handler) serveStart(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "want POST", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var o ipn.Options
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&o); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := h.b.Start(o)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(bradfitz): map error to a good HTTP error
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2021-04-08 05:06:31 +01:00
|
|
|
func (h *Handler) serveLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "logout access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "want POST", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := h.b.LogoutSync(r.Context())
|
|
|
|
if err == nil {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
|
|
|
|
2021-04-07 16:27:35 +01:00
|
|
|
func (h *Handler) servePrefs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "prefs access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-10-23 18:07:10 +01:00
|
|
|
var prefs ipn.PrefsView
|
2021-04-12 04:49:07 +01:00
|
|
|
switch r.Method {
|
|
|
|
case "PATCH":
|
2021-04-12 05:31:15 +01:00
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "prefs write access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-04-12 00:10:31 +01:00
|
|
|
mp := new(ipn.MaskedPrefs)
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(mp); err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
prefs, err = h.b.EditPrefs(mp)
|
|
|
|
if err != nil {
|
2022-04-18 17:37:23 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
json.NewEncoder(w).Encode(resJSON{Error: err.Error()})
|
2021-04-12 00:10:31 +01:00
|
|
|
return
|
|
|
|
}
|
2021-04-12 04:49:07 +01:00
|
|
|
case "GET", "HEAD":
|
2022-10-24 01:15:04 +01:00
|
|
|
prefs = h.b.Prefs()
|
2021-04-12 04:49:07 +01:00
|
|
|
default:
|
|
|
|
http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
2021-04-12 00:10:31 +01:00
|
|
|
}
|
2021-04-07 16:27:35 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
e := json.NewEncoder(w)
|
|
|
|
e.SetIndent("", "\t")
|
2021-04-12 00:10:31 +01:00
|
|
|
e.Encode(prefs)
|
2021-04-07 16:27:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:37:23 +01:00
|
|
|
type resJSON struct {
|
|
|
|
Error string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) serveCheckPrefs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "checkprefs access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := new(ipn.Prefs)
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(p); err != nil {
|
|
|
|
http.Error(w, "invalid JSON body", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := h.b.CheckPrefs(p)
|
|
|
|
var res resJSON
|
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2021-03-30 20:56:00 +01:00
|
|
|
func (h *Handler) serveFiles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "file access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-11-18 18:13:14 +00:00
|
|
|
suffix, ok := strs.CutPrefix(r.URL.EscapedPath(), "/localapi/v0/files/")
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "misconfigured", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-03-30 20:56:00 +01:00
|
|
|
if suffix == "" {
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "want GET to list files", 400)
|
|
|
|
return
|
|
|
|
}
|
2022-11-20 21:25:54 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
if s := r.FormValue("waitsec"); s != "" && s != "0" {
|
|
|
|
d, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "invalid waitsec", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
deadline := time.Now().Add(time.Duration(d) * time.Second)
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithDeadline(ctx, deadline)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
wfs, err := h.b.AwaitWaitingFiles(ctx)
|
|
|
|
if err != nil && ctx.Err() == nil {
|
2021-03-30 20:56:00 +01:00
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(wfs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := url.PathUnescape(suffix)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bad filename", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method == "DELETE" {
|
|
|
|
if err := h.b.DeleteFile(name); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rc, size, err := h.b.OpenFile(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprint(size))
|
2022-11-16 14:45:35 +00:00
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
2021-03-30 20:56:00 +01:00
|
|
|
io.Copy(w, rc)
|
|
|
|
}
|
|
|
|
|
2021-04-16 18:57:46 +01:00
|
|
|
func writeErrorJSON(w http.ResponseWriter, err error) {
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("unexpected nil error")
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
type E struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(E{err.Error()})
|
|
|
|
}
|
|
|
|
|
2021-04-05 05:35:52 +01:00
|
|
|
func (h *Handler) serveFileTargets(w http.ResponseWriter, r *http.Request) {
|
2021-04-13 16:35:16 +01:00
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "access denied", http.StatusForbidden)
|
2021-04-05 05:35:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "want GET to list targets", 400)
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 21:38:47 +01:00
|
|
|
fts, err := h.b.FileTargets()
|
2021-04-05 05:35:52 +01:00
|
|
|
if err != nil {
|
2021-04-16 18:57:46 +01:00
|
|
|
writeErrorJSON(w, err)
|
2021-04-05 05:35:52 +01:00
|
|
|
return
|
|
|
|
}
|
2022-09-10 20:11:59 +01:00
|
|
|
mak.NonNilSliceForJSON(&fts)
|
2021-04-05 05:35:52 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-04-06 21:38:47 +01:00
|
|
|
json.NewEncoder(w).Encode(fts)
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:47:52 +00:00
|
|
|
// serveFilePut sends a file to another node.
|
|
|
|
//
|
|
|
|
// It's sometimes possible for clients to do this themselves, without
|
|
|
|
// tailscaled, except in the case of tailscaled running in
|
|
|
|
// userspace-networking ("netstack") mode, in which case tailscaled
|
|
|
|
// needs to a do a netstack dial out.
|
|
|
|
//
|
|
|
|
// Instead, the CLI also goes through tailscaled so it doesn't need to be
|
|
|
|
// aware of the network mode in use.
|
|
|
|
//
|
|
|
|
// macOS/iOS have always used this localapi method to simplify the GUI
|
|
|
|
// clients.
|
|
|
|
//
|
|
|
|
// The Windows client currently (2021-11-30) uses the peerapi (/v0/put/)
|
|
|
|
// directly, as the Windows GUI always runs in tun mode anyway.
|
|
|
|
//
|
|
|
|
// URL format:
|
|
|
|
//
|
2022-08-02 17:33:46 +01:00
|
|
|
// - PUT /localapi/v0/file-put/:stableID/:escaped-filename
|
2021-04-06 21:38:47 +01:00
|
|
|
func (h *Handler) serveFilePut(w http.ResponseWriter, r *http.Request) {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricFilePutCalls.Add(1)
|
|
|
|
|
2021-04-06 21:38:47 +01:00
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "file access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "PUT" {
|
|
|
|
http.Error(w, "want PUT to put file", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fts, err := h.b.FileTargets()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:13:14 +00:00
|
|
|
upath, ok := strs.CutPrefix(r.URL.EscapedPath(), "/localapi/v0/file-put/")
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "misconfigured", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-03-16 21:25:31 +00:00
|
|
|
stableIDStr, filenameEscaped, ok := strings.Cut(upath, "/")
|
|
|
|
if !ok {
|
2021-04-06 21:38:47 +01:00
|
|
|
http.Error(w, "bogus URL", 400)
|
|
|
|
return
|
|
|
|
}
|
2022-03-16 21:25:31 +00:00
|
|
|
stableID := tailcfg.StableNodeID(stableIDStr)
|
2021-04-06 21:38:47 +01:00
|
|
|
|
2021-04-13 16:13:46 +01:00
|
|
|
var ft *apitype.FileTarget
|
2021-04-06 21:38:47 +01:00
|
|
|
for _, x := range fts {
|
|
|
|
if x.Node.StableID == stableID {
|
|
|
|
ft = x
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ft == nil {
|
|
|
|
http.Error(w, "node not found", 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dstURL, err := url.Parse(ft.PeerAPIURL)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bogus peer URL", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
outReq, err := http.NewRequestWithContext(r.Context(), "PUT", "http://peer/v0/put/"+filenameEscaped, r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bogus outreq", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
outReq.ContentLength = r.ContentLength
|
|
|
|
|
|
|
|
rp := httputil.NewSingleHostReverseProxy(dstURL)
|
2021-12-01 04:39:12 +00:00
|
|
|
rp.Transport = h.b.Dialer().PeerAPITransport()
|
2021-04-06 21:38:47 +01:00
|
|
|
rp.ServeHTTP(w, outReq)
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:03:16 +01:00
|
|
|
func (h *Handler) serveSetDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "want POST", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
err := h.b.SetDNS(ctx, r.FormValue("name"), r.FormValue("value"))
|
|
|
|
if err != nil {
|
|
|
|
writeErrorJSON(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(struct{}{})
|
|
|
|
}
|
|
|
|
|
2021-06-25 19:44:40 +01:00
|
|
|
func (h *Handler) serveDERPMap(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "want GET", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
e := json.NewEncoder(w)
|
|
|
|
e.SetIndent("", "\t")
|
|
|
|
e.Encode(h.b.DERPMap())
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:42:42 +00:00
|
|
|
// serveSetExpirySooner sets the expiry date on the current machine, specified
|
|
|
|
// by an `expiry` unix timestamp as POST or query param.
|
|
|
|
func (h *Handler) serveSetExpirySooner(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "POST required", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var expiryTime time.Time
|
|
|
|
if v := r.FormValue("expiry"); v != "" {
|
|
|
|
expiryInt, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "can't parse expiry time, expects a unix timestamp", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expiryTime = time.Unix(expiryInt, 0)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "missing 'expiry' parameter, a unix timestamp", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := h.b.SetExpirySooner(r.Context(), expiryTime)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
io.WriteString(w, "done\n")
|
|
|
|
}
|
|
|
|
|
2022-05-03 22:16:34 +01:00
|
|
|
func (h *Handler) servePing(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "want POST", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipStr := r.FormValue("ip")
|
|
|
|
if ipStr == "" {
|
|
|
|
http.Error(w, "missing 'ip' parameter", 400)
|
|
|
|
return
|
|
|
|
}
|
2022-07-26 04:55:44 +01:00
|
|
|
ip, err := netip.ParseAddr(ipStr)
|
2022-05-03 22:16:34 +01:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "invalid IP", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pingTypeStr := r.FormValue("type")
|
|
|
|
if ipStr == "" {
|
|
|
|
http.Error(w, "missing 'type' parameter", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err := h.b.Ping(ctx, ip, tailcfg.PingType(pingTypeStr))
|
|
|
|
if err != nil {
|
|
|
|
writeErrorJSON(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:04:01 +00:00
|
|
|
func (h *Handler) serveDial(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "POST required", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const upgradeProto = "ts-dial"
|
|
|
|
if !strings.Contains(r.Header.Get("Connection"), "upgrade") ||
|
|
|
|
r.Header.Get("Upgrade") != upgradeProto {
|
|
|
|
http.Error(w, "bad ts-dial upgrade", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hostStr, portStr := r.Header.Get("Dial-Host"), r.Header.Get("Dial-Port")
|
|
|
|
if hostStr == "" || portStr == "" {
|
|
|
|
http.Error(w, "missing Dial-Host or Dial-Port header", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hijacker, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "make request over HTTP/1", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := net.JoinHostPort(hostStr, portStr)
|
|
|
|
outConn, err := h.b.Dialer().UserDial(r.Context(), "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "dial failure: "+err.Error(), http.StatusBadGateway)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer outConn.Close()
|
|
|
|
|
|
|
|
w.Header().Set("Upgrade", upgradeProto)
|
|
|
|
w.Header().Set("Connection", "upgrade")
|
|
|
|
w.WriteHeader(http.StatusSwitchingProtocols)
|
|
|
|
|
|
|
|
reqConn, brw, err := hijacker.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
h.logf("localapi dial Hijack error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer reqConn.Close()
|
|
|
|
if err := brw.Flush(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqConn = netutil.NewDrainBufConn(reqConn, brw.Reader)
|
|
|
|
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(reqConn, outConn)
|
|
|
|
errc <- err
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(outConn, reqConn)
|
|
|
|
errc <- err
|
|
|
|
}()
|
|
|
|
<-errc
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:57:34 +01:00
|
|
|
func (h *Handler) serveUploadClientMetrics(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
type clientMetricJSON struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
// One of "counter" or "gauge"
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value int `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientMetrics []clientMetricJSON
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&clientMetrics); err != nil {
|
|
|
|
http.Error(w, "invalid JSON body", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metricsMu.Lock()
|
|
|
|
defer metricsMu.Unlock()
|
|
|
|
|
|
|
|
for _, m := range clientMetrics {
|
|
|
|
if metric, ok := metrics[m.Name]; ok {
|
|
|
|
metric.Add(int64(m.Value))
|
|
|
|
} else {
|
|
|
|
if clientmetric.HasPublished(m.Name) {
|
|
|
|
http.Error(w, "Already have a metric named "+m.Name, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var metric *clientmetric.Metric
|
|
|
|
switch m.Type {
|
|
|
|
case "counter":
|
|
|
|
metric = clientmetric.NewCounter(m.Name)
|
|
|
|
case "gauge":
|
|
|
|
metric = clientmetric.NewGauge(m.Name)
|
|
|
|
default:
|
|
|
|
http.Error(w, "Unknown metric type "+m.Type, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
metrics[m.Name] = metric
|
|
|
|
metric.Add(int64(m.Value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(struct{}{})
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:57:02 +01:00
|
|
|
func (h *Handler) serveTKAStatus(w http.ResponseWriter, r *http.Request) {
|
2022-08-11 18:43:09 +01:00
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "lock status access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodGet {
|
2022-10-09 16:57:02 +01:00
|
|
|
http.Error(w, "use GET", http.StatusMethodNotAllowed)
|
2022-08-11 18:43:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := json.MarshalIndent(h.b.NetworkLockStatus(), "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "JSON encoding error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(j)
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:47:51 +00:00
|
|
|
func (h *Handler) serveTKASign(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitRead {
|
|
|
|
http.Error(w, "lock status access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "use POST", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type signRequest struct {
|
|
|
|
NodeKey key.NodePublic
|
|
|
|
RotationPublic []byte
|
|
|
|
}
|
|
|
|
var req signRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
http.Error(w, "invalid JSON body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.b.NetworkLockSign(req.NodeKey, req.RotationPublic); err != nil {
|
|
|
|
http.Error(w, "signing failed: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:57:02 +01:00
|
|
|
func (h *Handler) serveTKAInit(w http.ResponseWriter, r *http.Request) {
|
2022-08-11 18:43:09 +01:00
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "lock init access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "use POST", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type initRequest struct {
|
2022-11-23 19:19:30 +00:00
|
|
|
Keys []tka.Key
|
|
|
|
DisablementValues [][]byte
|
|
|
|
SupportDisablement []byte
|
2022-08-11 18:43:09 +01:00
|
|
|
}
|
|
|
|
var req initRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
http.Error(w, "invalid JSON body", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:19:30 +00:00
|
|
|
if err := h.b.NetworkLockInit(req.Keys, req.DisablementValues, req.SupportDisablement); err != nil {
|
2022-08-11 18:43:09 +01:00
|
|
|
http.Error(w, "initialization failed: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := json.MarshalIndent(h.b.NetworkLockStatus(), "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "JSON encoding error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(j)
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:57:02 +01:00
|
|
|
func (h *Handler) serveTKAModify(w http.ResponseWriter, r *http.Request) {
|
2022-09-15 18:51:23 +01:00
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "network-lock modify access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "use POST", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type modifyRequest struct {
|
|
|
|
AddKeys []tka.Key
|
|
|
|
RemoveKeys []tka.Key
|
|
|
|
}
|
|
|
|
var req modifyRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
http.Error(w, "invalid JSON body", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.b.NetworkLockModify(req.AddKeys, req.RemoveKeys); err != nil {
|
|
|
|
http.Error(w, "network-lock modify failed: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-12-05 06:55:57 +00:00
|
|
|
w.WriteHeader(204)
|
2022-09-15 18:51:23 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:12:51 +00:00
|
|
|
func (h *Handler) serveTKADisable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "network-lock modify access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "use POST", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body := io.LimitReader(r.Body, 1024*1024)
|
|
|
|
secret, err := ioutil.ReadAll(body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "reading secret", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.b.NetworkLockDisable(secret); err != nil {
|
|
|
|
http.Error(w, "network-lock disable failed: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
2022-11-29 00:39:03 +00:00
|
|
|
func (h *Handler) serveTKALocalDisable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "network-lock modify access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "use POST", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require a JSON stanza for the body as an additional CSRF protection.
|
|
|
|
var req struct{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
http.Error(w, "invalid JSON body", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.b.NetworkLockForceLocalDisable(); err != nil {
|
|
|
|
http.Error(w, "network-lock local disable failed: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:04:10 +00:00
|
|
|
func (h *Handler) serveTKALog(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "use GET", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limit := 50
|
|
|
|
if limitStr := r.FormValue("limit"); limitStr != "" {
|
|
|
|
l, err := strconv.Atoi(limitStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "parsing 'limit' parameter: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
limit = int(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
updates, err := h.b.NetworkLockLog(limit)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "reading log failed: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := json.MarshalIndent(updates, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "JSON encoding error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(j)
|
|
|
|
}
|
|
|
|
|
2022-11-10 21:38:22 +00:00
|
|
|
// serveProfiles serves profile switching-related endpoints. Supported methods
|
|
|
|
// and paths are:
|
|
|
|
// - GET /profiles/: list all profiles (JSON-encoded array of ipn.LoginProfiles)
|
|
|
|
// - PUT /profiles/: add new profile (no response). A separate
|
|
|
|
// StartLoginInteractive() is needed to populate and persist the new profile.
|
|
|
|
// - GET /profiles/current: current profile (JSON-ecoded ipn.LoginProfile)
|
|
|
|
// - GET /profiles/<id>: output profile (JSON-ecoded ipn.LoginProfile)
|
|
|
|
// - POST /profiles/<id>: switch to profile (no response)
|
|
|
|
// - DELETE /profiles/<id>: delete profile (no response)
|
|
|
|
func (h *Handler) serveProfiles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.PermitWrite {
|
|
|
|
http.Error(w, "profiles access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
suffix, ok := strs.CutPrefix(r.URL.EscapedPath(), "/localapi/v0/profiles/")
|
|
|
|
if !ok {
|
2022-11-18 18:13:14 +00:00
|
|
|
http.Error(w, "misconfigured", http.StatusInternalServerError)
|
|
|
|
return
|
2022-11-10 21:38:22 +00:00
|
|
|
}
|
|
|
|
if suffix == "" {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(h.b.ListProfiles())
|
|
|
|
case http.MethodPut:
|
|
|
|
err := h.b.NewProfile()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
default:
|
|
|
|
http.Error(w, "use GET or PUT", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
suffix, err := url.PathUnescape(suffix)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bad profile ID", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if suffix == "current" {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(h.b.CurrentProfile())
|
|
|
|
default:
|
|
|
|
http.Error(w, "use GET", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
profileID := ipn.ProfileID(suffix)
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
profiles := h.b.ListProfiles()
|
|
|
|
profileIndex := slices.IndexFunc(profiles, func(p ipn.LoginProfile) bool {
|
|
|
|
return p.ID == profileID
|
|
|
|
})
|
|
|
|
if profileIndex == -1 {
|
|
|
|
http.Error(w, "Profile not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(profiles[profileIndex])
|
|
|
|
case http.MethodPost:
|
|
|
|
err := h.b.SwitchProfile(profileID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
case http.MethodDelete:
|
|
|
|
err := h.b.DeleteProfile(profileID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
default:
|
|
|
|
http.Error(w, "use POST or DELETE", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 04:07:58 +00:00
|
|
|
func defBool(a string, def bool) bool {
|
|
|
|
if a == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseBool(a)
|
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return v
|
2021-03-19 02:34:59 +00:00
|
|
|
}
|
2022-12-09 22:21:53 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
metricInvalidRequests = clientmetric.NewCounter("localapi_invalid_requests")
|
|
|
|
|
|
|
|
// User-visible LocalAPI endpoints.
|
|
|
|
metricFilePutCalls = clientmetric.NewCounter("localapi_file_put")
|
|
|
|
)
|