2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-03-25 22:38:40 +00:00
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-23 05:45:34 +00:00
|
|
|
"encoding/base64"
|
2021-11-03 18:17:56 +00:00
|
|
|
"encoding/json"
|
2021-03-25 22:38:40 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-12-06 20:05:51 +00:00
|
|
|
"hash/adler32"
|
2021-03-25 22:38:40 +00:00
|
|
|
"hash/crc32"
|
|
|
|
"html"
|
|
|
|
"io"
|
2021-04-26 16:48:34 +00:00
|
|
|
"io/fs"
|
2021-03-25 22:38:40 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2021-03-29 17:42:33 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-03-26 20:44:55 +00:00
|
|
|
"runtime"
|
2021-04-26 16:48:34 +00:00
|
|
|
"sort"
|
2021-03-25 22:38:40 +00:00
|
|
|
"strconv"
|
2021-03-29 17:42:33 +00:00
|
|
|
"strings"
|
2021-04-08 21:54:25 +00:00
|
|
|
"sync"
|
2022-08-04 04:51:02 +00:00
|
|
|
"sync/atomic"
|
2021-04-08 21:54:25 +00:00
|
|
|
"time"
|
2021-04-20 04:57:08 +00:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2021-03-25 22:38:40 +00:00
|
|
|
|
2022-04-26 19:29:41 +00:00
|
|
|
"github.com/kortschak/wol"
|
2022-11-16 16:53:51 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2021-11-23 17:58:34 +00:00
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
2022-11-19 23:29:15 +00:00
|
|
|
"golang.org/x/net/http/httpguts"
|
2021-04-13 15:13:46 +00:00
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2022-11-07 15:46:42 +00:00
|
|
|
"tailscale.com/envknob"
|
2021-12-21 21:52:50 +00:00
|
|
|
"tailscale.com/health"
|
2021-11-03 18:17:56 +00:00
|
|
|
"tailscale.com/hostinfo"
|
2021-03-30 18:19:42 +00:00
|
|
|
"tailscale.com/ipn"
|
2021-04-22 16:21:30 +00:00
|
|
|
"tailscale.com/logtail/backoff"
|
2021-11-23 05:45:34 +00:00
|
|
|
"tailscale.com/net/dns/resolver"
|
2021-03-26 04:41:37 +00:00
|
|
|
"tailscale.com/net/interfaces"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2022-02-13 22:45:50 +00:00
|
|
|
"tailscale.com/net/netutil"
|
2023-02-03 20:07:58 +00:00
|
|
|
"tailscale.com/net/sockstats"
|
2021-03-25 22:38:40 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-11-18 15:54:24 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
2022-12-13 01:51:03 +00:00
|
|
|
"tailscale.com/util/multierr"
|
2023-05-03 03:57:52 +00:00
|
|
|
"tailscale.com/version/distro"
|
2021-11-25 17:43:39 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2021-03-25 22:38:40 +00:00
|
|
|
)
|
|
|
|
|
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 04:14:09 +00:00
|
|
|
var initListenConfig func(*net.ListenConfig, netip.Addr, *interfaces.State, string) error
|
2021-03-26 20:44:55 +00:00
|
|
|
|
2021-12-06 22:58:29 +00:00
|
|
|
// addH2C is non-nil on platforms where we want to add H2C
|
|
|
|
// ("cleartext" HTTP/2) support to the peerAPI.
|
|
|
|
var addH2C func(*http.Server)
|
|
|
|
|
2021-03-29 17:42:33 +00:00
|
|
|
type peerAPIServer struct {
|
2021-03-30 18:19:42 +00:00
|
|
|
b *LocalBackend
|
2021-12-01 23:00:23 +00:00
|
|
|
rootDir string // empty means file receiving unavailable
|
2022-08-04 04:51:02 +00:00
|
|
|
knownEmpty atomic.Bool
|
2021-11-23 05:45:34 +00:00
|
|
|
resolver *resolver.Resolver
|
2021-04-12 21:05:44 +00:00
|
|
|
|
|
|
|
// directFileMode is whether we're writing files directly to a
|
|
|
|
// download directory (as *.partial files), rather than making
|
|
|
|
// the frontend retrieve it over localapi HTTP and write it
|
2021-12-06 20:24:25 +00:00
|
|
|
// somewhere itself. This is used on the GUI macOS versions
|
|
|
|
// and on Synology.
|
2021-04-26 18:28:27 +00:00
|
|
|
// In directFileMode, the peerapi doesn't do the final rename
|
2021-12-06 20:24:25 +00:00
|
|
|
// from "foo.jpg.partial" to "foo.jpg" unless
|
|
|
|
// directFileDoFinalRename is set.
|
2021-04-12 21:05:44 +00:00
|
|
|
directFileMode bool
|
2021-12-06 20:24:25 +00:00
|
|
|
|
|
|
|
// directFileDoFinalRename is whether in directFileMode we
|
|
|
|
// additionally move the *.direct file to its final name after
|
|
|
|
// it's received.
|
|
|
|
directFileDoFinalRename bool
|
2021-03-30 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:48:34 +00:00
|
|
|
const (
|
2022-09-25 18:29:55 +00:00
|
|
|
// partialSuffix is the suffix appended to files while they're
|
2021-04-26 18:28:27 +00:00
|
|
|
// still in the process of being transferred.
|
2021-04-26 16:48:34 +00:00
|
|
|
partialSuffix = ".partial"
|
|
|
|
|
|
|
|
// deletedSuffix is the suffix for a deleted marker file
|
|
|
|
// that's placed next to a file (without the suffix) that we
|
|
|
|
// tried to delete, but Windows wouldn't let us. These are
|
|
|
|
// only written on Windows (and in tests), but they're not
|
|
|
|
// permitted to be uploaded directly on any platform, like
|
|
|
|
// partial files.
|
|
|
|
deletedSuffix = ".deleted"
|
|
|
|
)
|
2021-03-30 18:19:42 +00:00
|
|
|
|
2021-04-20 04:57:08 +00:00
|
|
|
func validFilenameRune(r rune) bool {
|
|
|
|
switch r {
|
|
|
|
case '/':
|
|
|
|
return false
|
|
|
|
case '\\', ':', '*', '"', '<', '>', '|':
|
|
|
|
// Invalid stuff on Windows, but we reject them everywhere
|
|
|
|
// for now.
|
|
|
|
// TODO(bradfitz): figure out a better plan. We initially just
|
|
|
|
// wrote things to disk URL path-escaped, but that's gross
|
|
|
|
// when debugging, and just moves the problem to callers.
|
|
|
|
// So now we put the UTF-8 filenames on disk directly as
|
|
|
|
// sent.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return unicode.IsPrint(r)
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:56:00 +00:00
|
|
|
func (s *peerAPIServer) diskPath(baseName string) (fullPath string, ok bool) {
|
2021-04-20 04:57:08 +00:00
|
|
|
if !utf8.ValidString(baseName) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(baseName) != baseName {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
if len(baseName) > 255 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
// TODO: validate unicode normalization form too? Varies by platform.
|
2021-03-30 19:56:00 +00:00
|
|
|
clean := path.Clean(baseName)
|
|
|
|
if clean != baseName ||
|
2021-04-20 04:57:08 +00:00
|
|
|
clean == "." || clean == ".." ||
|
2021-04-26 16:48:34 +00:00
|
|
|
strings.HasSuffix(clean, deletedSuffix) ||
|
2021-03-30 19:56:00 +00:00
|
|
|
strings.HasSuffix(clean, partialSuffix) {
|
|
|
|
return "", false
|
|
|
|
}
|
2021-04-20 04:57:08 +00:00
|
|
|
for _, r := range baseName {
|
|
|
|
if !validFilenameRune(r) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filepath.Join(s.rootDir, baseName), true
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:19:42 +00:00
|
|
|
// hasFilesWaiting reports whether any files are buffered in the
|
|
|
|
// tailscaled daemon storage.
|
|
|
|
func (s *peerAPIServer) hasFilesWaiting() bool {
|
2021-12-01 23:00:23 +00:00
|
|
|
if s == nil || s.rootDir == "" || s.directFileMode {
|
2021-03-30 18:19:42 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-08-04 04:51:02 +00:00
|
|
|
if s.knownEmpty.Load() {
|
2021-03-30 18:19:42 +00:00
|
|
|
// Optimization: this is usually empty, so avoid opening
|
|
|
|
// the directory and checking. We can't cache the actual
|
|
|
|
// has-files-or-not values as the macOS/iOS client might
|
|
|
|
// in the future use+delete the files directly. So only
|
|
|
|
// keep this negative cache.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
f, err := os.Open(s.rootDir)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
for {
|
|
|
|
des, err := f.ReadDir(10)
|
|
|
|
for _, de := range des {
|
2021-04-26 16:48:34 +00:00
|
|
|
name := de.Name()
|
|
|
|
if strings.HasSuffix(name, partialSuffix) {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-01 21:43:06 +00:00
|
|
|
if name, ok := strings.CutSuffix(name, deletedSuffix); ok { // for Windows + tests
|
2021-04-26 16:48:34 +00:00
|
|
|
// After we're done looping over files, then try
|
|
|
|
// to delete this file. Don't do it proactively,
|
|
|
|
// as the OS may return "foo.jpg.deleted" before "foo.jpg"
|
|
|
|
// and we don't want to delete the ".deleted" file before
|
|
|
|
// enumerating to the "foo.jpg" file.
|
2022-11-18 18:13:14 +00:00
|
|
|
defer tryDeleteAgain(filepath.Join(s.rootDir, name))
|
2021-03-30 18:19:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if de.Type().IsRegular() {
|
2021-04-26 16:48:34 +00:00
|
|
|
_, err := os.Stat(filepath.Join(s.rootDir, name+deletedSuffix))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
tryDeleteAgain(filepath.Join(s.rootDir, name))
|
|
|
|
continue
|
|
|
|
}
|
2021-03-30 18:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
2022-08-04 04:51:02 +00:00
|
|
|
s.knownEmpty.Store(true)
|
2021-03-30 18:19:42 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2021-03-29 17:42:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:48:34 +00:00
|
|
|
// WaitingFiles returns the list of files that have been sent by a
|
|
|
|
// peer that are waiting in the buffered "pick up" directory owned by
|
|
|
|
// the Tailscale daemon.
|
|
|
|
//
|
|
|
|
// As a side effect, it also does any lazy deletion of files as
|
|
|
|
// required by Windows.
|
2021-04-13 15:13:46 +00:00
|
|
|
func (s *peerAPIServer) WaitingFiles() (ret []apitype.WaitingFile, err error) {
|
2021-12-01 23:00:23 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil, errNilPeerAPIServer
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
if s.rootDir == "" {
|
2021-12-01 23:00:23 +00:00
|
|
|
return nil, errNoTaildrop
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
2021-04-12 22:12:47 +00:00
|
|
|
if s.directFileMode {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
f, err := os.Open(s.rootDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2021-04-26 16:48:34 +00:00
|
|
|
var deleted map[string]bool // "foo.jpg" => true (if "foo.jpg.deleted" exists)
|
2021-03-30 19:56:00 +00:00
|
|
|
for {
|
|
|
|
des, err := f.ReadDir(10)
|
|
|
|
for _, de := range des {
|
|
|
|
name := de.Name()
|
|
|
|
if strings.HasSuffix(name, partialSuffix) {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-01 21:43:06 +00:00
|
|
|
if name, ok := strings.CutSuffix(name, deletedSuffix); ok { // for Windows + tests
|
2021-04-26 16:48:34 +00:00
|
|
|
if deleted == nil {
|
|
|
|
deleted = map[string]bool{}
|
|
|
|
}
|
2022-11-18 18:13:14 +00:00
|
|
|
deleted[name] = true
|
2021-04-26 16:48:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
if de.Type().IsRegular() {
|
|
|
|
fi, err := de.Info()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-13 15:13:46 +00:00
|
|
|
ret = append(ret, apitype.WaitingFile{
|
2021-03-30 19:56:00 +00:00
|
|
|
Name: filepath.Base(name),
|
|
|
|
Size: fi.Size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-04-26 16:48:34 +00:00
|
|
|
if len(deleted) > 0 {
|
|
|
|
// Filter out any return values "foo.jpg" where a
|
|
|
|
// "foo.jpg.deleted" marker file exists on disk.
|
|
|
|
all := ret
|
|
|
|
ret = ret[:0]
|
|
|
|
for _, wf := range all {
|
|
|
|
if !deleted[wf.Name] {
|
|
|
|
ret = append(ret, wf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// And do some opportunistic deleting while we're here.
|
|
|
|
// Maybe Windows is done virus scanning the file we tried
|
|
|
|
// to delete a long time ago and will let us delete it now.
|
|
|
|
for name := range deleted {
|
|
|
|
tryDeleteAgain(filepath.Join(s.rootDir, name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(ret, func(i, j int) bool { return ret[i].Name < ret[j].Name })
|
2021-03-30 19:56:00 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-12-01 23:00:23 +00:00
|
|
|
var (
|
|
|
|
errNilPeerAPIServer = errors.New("peerapi unavailable; not listening")
|
|
|
|
errNoTaildrop = errors.New("Taildrop disabled; no storage directory")
|
|
|
|
)
|
|
|
|
|
2021-04-26 16:48:34 +00:00
|
|
|
// tryDeleteAgain tries to delete path (and path+deletedSuffix) after
|
|
|
|
// it failed earlier. This happens on Windows when various anti-virus
|
|
|
|
// tools hook into filesystem operations and have the file open still
|
|
|
|
// while we're trying to delete it. In that case we instead mark it as
|
|
|
|
// deleted (writing a "foo.jpg.deleted" marker file), but then we
|
|
|
|
// later try to clean them up.
|
|
|
|
//
|
|
|
|
// fullPath is the full path to the file without the deleted suffix.
|
|
|
|
func tryDeleteAgain(fullPath string) {
|
|
|
|
if err := os.Remove(fullPath); err == nil || os.IsNotExist(err) {
|
|
|
|
os.Remove(fullPath + deletedSuffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:56:00 +00:00
|
|
|
func (s *peerAPIServer) DeleteFile(baseName string) error {
|
2021-12-01 23:00:23 +00:00
|
|
|
if s == nil {
|
|
|
|
return errNilPeerAPIServer
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
if s.rootDir == "" {
|
2021-12-01 23:00:23 +00:00
|
|
|
return errNoTaildrop
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
2021-04-12 22:12:47 +00:00
|
|
|
if s.directFileMode {
|
|
|
|
return errors.New("deletes not allowed in direct mode")
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
path, ok := s.diskPath(baseName)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("bad filename")
|
|
|
|
}
|
2021-04-22 16:21:30 +00:00
|
|
|
var bo *backoff.Backoff
|
|
|
|
logf := s.b.logf
|
|
|
|
t0 := time.Now()
|
|
|
|
for {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2021-04-26 16:48:34 +00:00
|
|
|
err = redactErr(err)
|
2021-04-22 16:21:30 +00:00
|
|
|
// Put a retry loop around deletes on Windows. Windows
|
|
|
|
// file descriptor closes are effectively asynchronous,
|
|
|
|
// as a bunch of hooks run on/after close, and we can't
|
|
|
|
// necessarily delete the file for a while after close,
|
|
|
|
// as we need to wait for everybody to be done with
|
|
|
|
// it. (on Windows, unlike Unix, a file can't be deleted
|
2021-04-26 16:48:34 +00:00
|
|
|
// if it's open anywhere)
|
|
|
|
// So try a few times but ultimately just leave a
|
|
|
|
// "foo.jpg.deleted" marker file to note that it's
|
|
|
|
// deleted and we clean it up later.
|
2021-04-22 16:21:30 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if bo == nil {
|
|
|
|
bo = backoff.NewBackoff("delete-retry", logf, 1*time.Second)
|
|
|
|
}
|
2021-04-26 16:48:34 +00:00
|
|
|
if time.Since(t0) < 5*time.Second {
|
2021-04-22 16:21:30 +00:00
|
|
|
bo.BackOff(context.Background(), err)
|
|
|
|
continue
|
|
|
|
}
|
2021-05-04 16:53:37 +00:00
|
|
|
if err := touchFile(path + deletedSuffix); err != nil {
|
2021-04-26 16:48:34 +00:00
|
|
|
logf("peerapi: failed to leave deleted marker: %v", err)
|
|
|
|
}
|
2021-04-22 16:21:30 +00:00
|
|
|
}
|
|
|
|
logf("peerapi: failed to DeleteFile: %v", err)
|
|
|
|
return err
|
2021-04-22 14:47:57 +00:00
|
|
|
}
|
2021-04-22 16:21:30 +00:00
|
|
|
return nil
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:53:37 +00:00
|
|
|
// redacted is a fake path name we use in errors, to avoid
|
|
|
|
// accidentally logging actual filenames anywhere.
|
|
|
|
const redacted = "redacted"
|
|
|
|
|
2022-12-06 20:05:51 +00:00
|
|
|
type redactedErr struct {
|
|
|
|
msg string
|
|
|
|
inner error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (re *redactedErr) Error() string {
|
|
|
|
return re.msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (re *redactedErr) Unwrap() error {
|
|
|
|
return re.inner
|
|
|
|
}
|
|
|
|
|
|
|
|
func redactString(s string) string {
|
|
|
|
hash := adler32.Checksum([]byte(s))
|
|
|
|
|
|
|
|
var buf [len(redacted) + len(".12345678")]byte
|
|
|
|
b := append(buf[:0], []byte(redacted)...)
|
|
|
|
b = append(b, '.')
|
|
|
|
b = strconv.AppendUint(b, uint64(hash), 16)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:51:03 +00:00
|
|
|
func redactErr(root error) error {
|
|
|
|
// redactStrings is a list of sensitive strings that were redacted.
|
|
|
|
// It is not sufficient to just snub out sensitive fields in Go errors
|
|
|
|
// since some wrapper errors like fmt.Errorf pre-cache the error string,
|
|
|
|
// which would unfortunately remain unaffected.
|
2022-12-06 20:05:51 +00:00
|
|
|
var redactStrings []string
|
|
|
|
|
2022-12-13 01:51:03 +00:00
|
|
|
// Redact sensitive fields in known Go error types.
|
|
|
|
var unknownErrors int
|
|
|
|
multierr.Range(root, func(err error) bool {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *os.PathError:
|
|
|
|
redactStrings = append(redactStrings, err.Path)
|
|
|
|
err.Path = redactString(err.Path)
|
|
|
|
case *os.LinkError:
|
|
|
|
redactStrings = append(redactStrings, err.New, err.Old)
|
|
|
|
err.New = redactString(err.New)
|
|
|
|
err.Old = redactString(err.Old)
|
|
|
|
default:
|
|
|
|
unknownErrors++
|
2022-12-06 20:05:51 +00:00
|
|
|
}
|
2022-12-13 01:51:03 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// If there are no redacted strings or no unknown error types,
|
|
|
|
// then we can return the possibly modified root error verbatim.
|
|
|
|
// Otherwise, we must replace redacted strings from any wrappers.
|
|
|
|
if len(redactStrings) == 0 || unknownErrors == 0 {
|
|
|
|
return root
|
2022-12-06 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 01:51:03 +00:00
|
|
|
// Stringify and replace any paths that we found above, then return
|
2022-12-06 20:05:51 +00:00
|
|
|
// the error wrapped in a type that uses the newly-redacted string
|
|
|
|
// while also allowing Unwrap()-ing to the inner error type(s).
|
2022-12-13 01:51:03 +00:00
|
|
|
s := root.Error()
|
|
|
|
for _, toRedact := range redactStrings {
|
|
|
|
s = strings.ReplaceAll(s, toRedact, redactString(toRedact))
|
|
|
|
}
|
|
|
|
return &redactedErr{msg: s, inner: root}
|
2021-04-26 16:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func touchFile(path string) error {
|
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
2021-05-04 16:53:37 +00:00
|
|
|
return redactErr(err)
|
2021-04-26 16:48:34 +00:00
|
|
|
}
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:56:00 +00:00
|
|
|
func (s *peerAPIServer) OpenFile(baseName string) (rc io.ReadCloser, size int64, err error) {
|
2021-12-01 23:00:23 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil, 0, errNilPeerAPIServer
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
if s.rootDir == "" {
|
2021-12-01 23:00:23 +00:00
|
|
|
return nil, 0, errNoTaildrop
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
2021-04-12 22:12:47 +00:00
|
|
|
if s.directFileMode {
|
|
|
|
return nil, 0, errors.New("opens not allowed in direct mode")
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
path, ok := s.diskPath(baseName)
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, errors.New("bad filename")
|
|
|
|
}
|
2021-04-26 16:48:34 +00:00
|
|
|
if fi, err := os.Stat(path + deletedSuffix); err == nil && fi.Mode().IsRegular() {
|
|
|
|
tryDeleteAgain(path)
|
2021-05-04 16:53:37 +00:00
|
|
|
return nil, 0, &fs.PathError{Op: "open", Path: redacted, Err: fs.ErrNotExist}
|
2021-04-26 16:48:34 +00:00
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2021-05-04 16:53:37 +00:00
|
|
|
return nil, 0, redactErr(err)
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2021-05-04 16:53:37 +00:00
|
|
|
return nil, 0, redactErr(err)
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
|
|
|
return f, fi.Size(), nil
|
|
|
|
}
|
|
|
|
|
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 04:14:09 +00:00
|
|
|
func (s *peerAPIServer) listen(ip netip.Addr, ifState *interfaces.State) (ln net.Listener, err error) {
|
2022-04-20 18:57:34 +00:00
|
|
|
// Android for whatever reason often has problems creating the peerapi listener.
|
|
|
|
// But since we started intercepting it with netstack, it's not even important that
|
|
|
|
// we have a real kernel-level listener. So just create a dummy listener on Android
|
|
|
|
// and let netstack intercept it.
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
return newFakePeerAPIListener(ip), nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:44:55 +00:00
|
|
|
ipStr := ip.String()
|
2021-03-25 22:38:40 +00:00
|
|
|
|
|
|
|
var lc net.ListenConfig
|
|
|
|
if initListenConfig != nil {
|
|
|
|
// On iOS/macOS, this sets the lc.Control hook to
|
|
|
|
// setsockopt the interface index to bind to, to get
|
|
|
|
// out of the network sandbox.
|
2021-12-01 17:18:17 +00:00
|
|
|
if err := initListenConfig(&lc, ip, ifState, s.b.dialer.TUNName()); err != nil {
|
2021-03-25 22:38:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-26 20:44:55 +00:00
|
|
|
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
|
|
|
|
ipStr = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:57:17 +00:00
|
|
|
if s.b.sys.IsNetstack() {
|
2021-03-30 16:54:52 +00:00
|
|
|
ipStr = ""
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:44:55 +00:00
|
|
|
tcp4or6 := "tcp4"
|
|
|
|
if ip.Is6() {
|
|
|
|
tcp4or6 = "tcp6"
|
2021-03-25 22:38:40 +00:00
|
|
|
}
|
2021-03-26 20:44:55 +00:00
|
|
|
|
2021-03-25 22:38:40 +00:00
|
|
|
// Make a best effort to pick a deterministic port number for
|
2022-02-22 21:29:17 +00:00
|
|
|
// the ip. The lower three bytes are the same for IPv4 and IPv6
|
2021-03-25 22:38:40 +00:00
|
|
|
// Tailscale addresses (at least currently), so we'll usually
|
|
|
|
// get the same port number on both address families for
|
|
|
|
// dev/debugging purposes, which is nice. But it's not so
|
|
|
|
// deterministic that people will bake this into clients.
|
|
|
|
// We try a few times just in case something's already
|
|
|
|
// listening on that port (on all interfaces, probably).
|
|
|
|
for try := uint8(0); try < 5; try++ {
|
|
|
|
a16 := ip.As16()
|
|
|
|
hashData := a16[len(a16)-3:]
|
|
|
|
hashData[0] += try
|
|
|
|
tryPort := (32 << 10) | uint16(crc32.ChecksumIEEE(hashData))
|
2021-03-26 20:44:55 +00:00
|
|
|
ln, err = lc.Listen(context.Background(), tcp4or6, net.JoinHostPort(ipStr, strconv.Itoa(int(tryPort))))
|
2021-03-25 22:38:40 +00:00
|
|
|
if err == nil {
|
|
|
|
return ln, nil
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 20:22:42 +00:00
|
|
|
// Fall back to some random ephemeral port.
|
|
|
|
ln, err = lc.Listen(context.Background(), tcp4or6, net.JoinHostPort(ipStr, "0"))
|
|
|
|
|
|
|
|
// And if we're on a platform with netstack (anything but iOS), then just fallback to netstack.
|
|
|
|
if err != nil && runtime.GOOS != "ios" {
|
|
|
|
s.b.logf("peerapi: failed to do peerAPI listen, harmless (netstack available) but error was: %v", err)
|
|
|
|
return newFakePeerAPIListener(ip), nil
|
|
|
|
}
|
|
|
|
return ln, err
|
2021-03-25 22:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type peerAPIListener struct {
|
2021-04-02 05:04:46 +00:00
|
|
|
ps *peerAPIServer
|
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 04:14:09 +00:00
|
|
|
ip netip.Addr
|
2021-04-02 05:04:46 +00:00
|
|
|
lb *LocalBackend
|
|
|
|
|
|
|
|
// ln is the Listener. It can be nil in netstack mode if there are more than
|
|
|
|
// 1 local addresses (e.g. both an IPv4 and IPv6). When it's nil, port
|
|
|
|
// and urlStr are still populated.
|
|
|
|
ln net.Listener
|
|
|
|
|
2022-11-16 17:38:38 +00:00
|
|
|
// urlStr is the base URL to access the PeerAPI (http://ip:port/).
|
2021-03-29 17:42:33 +00:00
|
|
|
urlStr string
|
2021-04-02 05:04:46 +00:00
|
|
|
// port is just the port of urlStr.
|
|
|
|
port int
|
2021-03-26 20:44:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:54:52 +00:00
|
|
|
func (pln *peerAPIListener) Close() error {
|
|
|
|
if pln.ln != nil {
|
|
|
|
return pln.ln.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 22:38:40 +00:00
|
|
|
func (pln *peerAPIListener) serve() {
|
2021-03-30 16:54:52 +00:00
|
|
|
if pln.ln == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 22:38:40 +00:00
|
|
|
defer pln.ln.Close()
|
|
|
|
logf := pln.lb.logf
|
|
|
|
for {
|
|
|
|
c, err := pln.ln.Accept()
|
|
|
|
if errors.Is(err, net.ErrClosed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logf("peerapi.Accept: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ta, ok := c.RemoteAddr().(*net.TCPAddr)
|
|
|
|
if !ok {
|
|
|
|
c.Close()
|
|
|
|
logf("peerapi: unexpected RemoteAddr %#v", c.RemoteAddr())
|
|
|
|
continue
|
|
|
|
}
|
2022-08-03 04:48:56 +00:00
|
|
|
ipp := netaddr.Unmap(ta.AddrPort())
|
|
|
|
if !ipp.IsValid() {
|
2021-03-25 22:38:40 +00:00
|
|
|
logf("peerapi: bogus TCPAddr %#v", ta)
|
|
|
|
c.Close()
|
|
|
|
continue
|
|
|
|
}
|
2022-01-31 17:20:22 +00:00
|
|
|
pln.ServeConn(ipp, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 04:14:09 +00:00
|
|
|
func (pln *peerAPIListener) ServeConn(src netip.AddrPort, c net.Conn) {
|
2022-01-31 17:20:22 +00:00
|
|
|
logf := pln.lb.logf
|
|
|
|
peerNode, peerUser, ok := pln.lb.WhoIs(src)
|
|
|
|
if !ok {
|
|
|
|
logf("peerapi: unknown peer %v", src)
|
|
|
|
c.Close()
|
|
|
|
return
|
|
|
|
}
|
2022-11-16 19:07:21 +00:00
|
|
|
nm := pln.lb.NetMap()
|
|
|
|
if nm == nil || nm.SelfNode == nil {
|
|
|
|
logf("peerapi: no netmap")
|
|
|
|
c.Close()
|
|
|
|
return
|
|
|
|
}
|
2022-01-31 17:20:22 +00:00
|
|
|
h := &peerAPIHandler{
|
|
|
|
ps: pln.ps,
|
2022-11-16 19:07:21 +00:00
|
|
|
isSelf: nm.SelfNode.User == peerNode.User,
|
2022-01-31 17:20:22 +00:00
|
|
|
remoteAddr: src,
|
2022-11-16 19:07:21 +00:00
|
|
|
selfNode: nm.SelfNode,
|
2022-01-31 17:20:22 +00:00
|
|
|
peerNode: peerNode,
|
|
|
|
peerUser: peerUser,
|
|
|
|
}
|
|
|
|
httpServer := &http.Server{
|
|
|
|
Handler: h,
|
|
|
|
}
|
|
|
|
if addH2C != nil {
|
|
|
|
addH2C(httpServer)
|
2021-03-25 22:38:40 +00:00
|
|
|
}
|
2022-12-01 00:21:56 +00:00
|
|
|
go httpServer.Serve(netutil.NewOneConnListener(c, nil))
|
2021-03-25 22:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 17:38:38 +00:00
|
|
|
// peerAPIHandler serves the PeerAPI for a source specific client.
|
2021-03-27 04:24:02 +00:00
|
|
|
type peerAPIHandler struct {
|
2021-03-29 17:42:33 +00:00
|
|
|
ps *peerAPIServer
|
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 04:14:09 +00:00
|
|
|
remoteAddr netip.AddrPort
|
2021-03-27 04:24:02 +00:00
|
|
|
isSelf bool // whether peerNode is owned by same user as this node
|
2022-11-16 19:07:21 +00:00
|
|
|
selfNode *tailcfg.Node // this node; always non-nil
|
2021-03-27 04:24:02 +00:00
|
|
|
peerNode *tailcfg.Node // peerNode is who's making the request
|
|
|
|
peerUser tailcfg.UserProfile // profile of peerNode
|
2021-03-25 22:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
func (h *peerAPIHandler) logf(format string, a ...any) {
|
2021-03-29 17:42:33 +00:00
|
|
|
h.ps.b.logf("peerapi: "+format, a...)
|
|
|
|
}
|
|
|
|
|
2023-04-19 22:23:26 +00:00
|
|
|
// isAddressValid reports whether addr is a valid destination address for this
|
|
|
|
// node originating from the peer.
|
|
|
|
func (h *peerAPIHandler) isAddressValid(addr netip.Addr) bool {
|
|
|
|
if h.peerNode.SelfNodeV4MasqAddrForThisPeer != nil {
|
|
|
|
return *h.peerNode.SelfNodeV4MasqAddrForThisPeer == addr
|
|
|
|
}
|
|
|
|
pfx := netip.PrefixFrom(addr, addr.BitLen())
|
|
|
|
return slices.Contains(h.selfNode.Addresses, pfx)
|
|
|
|
}
|
|
|
|
|
2022-11-16 16:53:51 +00:00
|
|
|
func (h *peerAPIHandler) validateHost(r *http.Request) error {
|
|
|
|
if r.Host == "peer" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ap, err := netip.ParseAddrPort(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-19 22:23:26 +00:00
|
|
|
if !h.isAddressValid(ap.Addr()) {
|
|
|
|
return fmt.Errorf("%v not found in self addresses", ap.Addr())
|
2022-11-16 16:53:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *peerAPIHandler) validatePeerAPIRequest(r *http.Request) error {
|
|
|
|
if r.Referer() != "" {
|
|
|
|
return errors.New("unexpected Referer")
|
|
|
|
}
|
|
|
|
if r.Header.Get("Origin") != "" {
|
|
|
|
return errors.New("unexpected Origin")
|
|
|
|
}
|
|
|
|
return h.validateHost(r)
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:29:15 +00:00
|
|
|
// peerAPIRequestShouldGetSecurityHeaders reports whether the PeerAPI request r
|
|
|
|
// should get security response headers. It aims to report true for any request
|
|
|
|
// from a browser and false for requests from tailscaled (Go) clients.
|
|
|
|
//
|
|
|
|
// PeerAPI is primarily an RPC mechanism between Tailscale instances. Some of
|
|
|
|
// the HTTP handlers are useful for debugging with curl or browsers, but in
|
|
|
|
// general the client is always tailscaled itself. Because PeerAPI only uses
|
|
|
|
// HTTP/1 without HTTP/2 and its HPACK helping with repetitive headers, we try
|
|
|
|
// to minimize header bytes sent in the common case when the client isn't a
|
|
|
|
// browser. Minimizing bytes is important in particular with the ExitDNS service
|
|
|
|
// provided by exit nodes, processing DNS clients from queries. We don't want to
|
|
|
|
// waste bytes with security headers to non-browser clients. But if there's any
|
|
|
|
// hint that the request is from a browser, then we do.
|
|
|
|
func peerAPIRequestShouldGetSecurityHeaders(r *http.Request) bool {
|
|
|
|
// Accept-Encoding is a forbidden header
|
|
|
|
// (https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name)
|
|
|
|
// that Chrome, Firefox, Safari, etc send, but Go does not. So if we see it,
|
|
|
|
// it's probably a browser and not a Tailscale PeerAPI (Go) client.
|
|
|
|
if httpguts.HeaderValuesContainsToken(r.Header["Accept-Encoding"], "deflate") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Clients can mess with their User-Agent, but if they say Mozilla or have a bunch
|
|
|
|
// of components (spaces) they're likely a browser.
|
|
|
|
if ua := r.Header.Get("User-Agent"); strings.HasPrefix(ua, "Mozilla/") || strings.Count(ua, " ") > 2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Tailscale/PeerAPI/Go clients don't have an Accept-Language.
|
|
|
|
if r.Header.Get("Accept-Language") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-27 04:24:02 +00:00
|
|
|
func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2022-11-16 16:53:51 +00:00
|
|
|
if err := h.validatePeerAPIRequest(r); err != nil {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricInvalidRequests.Add(1)
|
2022-11-16 16:53:51 +00:00
|
|
|
h.logf("invalid request from %v: %v", h.remoteAddr, err)
|
|
|
|
http.Error(w, "invalid peerapi request", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 23:29:15 +00:00
|
|
|
if peerAPIRequestShouldGetSecurityHeaders(r) {
|
2023-02-23 19:48:03 +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'; style-src 'unsafe-inline'`)
|
2022-11-19 23:29:15 +00:00
|
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
}
|
2021-03-29 17:42:33 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/v0/put/") {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricPutCalls.Add(1)
|
2021-04-20 03:21:48 +00:00
|
|
|
h.handlePeerPut(w, r)
|
2021-03-29 17:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-23 05:45:34 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/dns-query") {
|
2022-12-09 22:21:53 +00:00
|
|
|
metricDNSCalls.Add(1)
|
2021-11-23 05:45:34 +00:00
|
|
|
h.handleDNSQuery(w, r)
|
|
|
|
return
|
|
|
|
}
|
2021-11-18 15:54:24 +00:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/v0/goroutines":
|
2021-04-22 20:01:42 +00:00
|
|
|
h.handleServeGoroutines(w, r)
|
|
|
|
return
|
2021-11-18 15:54:24 +00:00
|
|
|
case "/v0/env":
|
2021-11-03 18:17:56 +00:00
|
|
|
h.handleServeEnv(w, r)
|
|
|
|
return
|
2021-11-18 15:54:24 +00:00
|
|
|
case "/v0/metrics":
|
|
|
|
h.handleServeMetrics(w, r)
|
|
|
|
return
|
2021-12-21 18:26:13 +00:00
|
|
|
case "/v0/magicsock":
|
|
|
|
h.handleServeMagicsock(w, r)
|
|
|
|
return
|
2021-12-21 21:52:50 +00:00
|
|
|
case "/v0/dnsfwd":
|
|
|
|
h.handleServeDNSFwd(w, r)
|
|
|
|
return
|
2022-04-26 19:29:41 +00:00
|
|
|
case "/v0/wol":
|
2022-12-09 22:21:53 +00:00
|
|
|
metricWakeOnLANCalls.Add(1)
|
2022-04-26 19:29:41 +00:00
|
|
|
h.handleWakeOnLAN(w, r)
|
|
|
|
return
|
2022-04-26 22:25:48 +00:00
|
|
|
case "/v0/interfaces":
|
|
|
|
h.handleServeInterfaces(w, r)
|
|
|
|
return
|
2023-02-09 22:58:20 +00:00
|
|
|
case "/v0/doctor":
|
|
|
|
h.handleServeDoctor(w, r)
|
2023-02-03 20:07:58 +00:00
|
|
|
case "/v0/sockstats":
|
|
|
|
h.handleServeSockStats(w, r)
|
2023-02-09 22:58:20 +00:00
|
|
|
return
|
2022-11-07 15:46:42 +00:00
|
|
|
case "/v0/ingress":
|
2022-12-09 22:21:53 +00:00
|
|
|
metricIngressCalls.Add(1)
|
2022-11-07 15:46:42 +00:00
|
|
|
h.handleServeIngress(w, r)
|
|
|
|
return
|
2021-11-03 18:17:56 +00:00
|
|
|
}
|
2021-03-27 04:24:02 +00:00
|
|
|
who := h.peerUser.DisplayName
|
2021-03-25 22:38:40 +00:00
|
|
|
fmt.Fprintf(w, `<html>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<body>
|
|
|
|
<h1>Hello, %s (%v)</h1>
|
|
|
|
This is my Tailscale device. Your device is %v.
|
2022-07-25 03:08:42 +00:00
|
|
|
`, html.EscapeString(who), h.remoteAddr.Addr(), html.EscapeString(h.peerNode.ComputedName))
|
2021-03-25 22:38:40 +00:00
|
|
|
|
2021-03-27 04:24:02 +00:00
|
|
|
if h.isSelf {
|
|
|
|
fmt.Fprintf(w, "<p>You are the owner of this node.\n")
|
|
|
|
}
|
2021-03-25 22:38:40 +00:00
|
|
|
}
|
2021-03-29 17:42:33 +00:00
|
|
|
|
2022-11-07 15:46:42 +00:00
|
|
|
func (h *peerAPIHandler) handleServeIngress(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// http.Errors only useful if hitting endpoint manually
|
|
|
|
// otherwise rely on log lines when debugging ingress connections
|
|
|
|
// as connection is hijacked for bidi and is encrypted tls
|
|
|
|
if !h.canIngress() {
|
|
|
|
h.logf("ingress: denied; no ingress cap from %v", h.remoteAddr)
|
|
|
|
http.Error(w, "denied; no ingress cap", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logAndError := func(code int, publicMsg string) {
|
|
|
|
h.logf("ingress: bad request from %v: %s", h.remoteAddr, publicMsg)
|
|
|
|
http.Error(w, publicMsg, http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
bad := func(publicMsg string) {
|
|
|
|
logAndError(http.StatusBadRequest, publicMsg)
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
logAndError(http.StatusMethodNotAllowed, "only POST allowed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
srcAddrStr := r.Header.Get("Tailscale-Ingress-Src")
|
|
|
|
if srcAddrStr == "" {
|
|
|
|
bad("Tailscale-Ingress-Src header not set")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
srcAddr, err := netip.ParseAddrPort(srcAddrStr)
|
|
|
|
if err != nil {
|
|
|
|
bad("Tailscale-Ingress-Src header invalid; want ip:port")
|
|
|
|
return
|
|
|
|
}
|
2023-03-08 20:36:41 +00:00
|
|
|
target := ipn.HostPort(r.Header.Get("Tailscale-Ingress-Target"))
|
2022-11-07 15:46:42 +00:00
|
|
|
if target == "" {
|
2022-11-13 20:36:33 +00:00
|
|
|
bad("Tailscale-Ingress-Target header not set")
|
2022-11-07 15:46:42 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-08 20:36:41 +00:00
|
|
|
if _, _, err := net.SplitHostPort(string(target)); err != nil {
|
2022-11-13 20:36:33 +00:00
|
|
|
bad("Tailscale-Ingress-Target header invalid; want host:port")
|
2022-11-07 15:46:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
getConn := func() (net.Conn, bool) {
|
|
|
|
conn, _, err := w.(http.Hijacker).Hijack()
|
|
|
|
if err != nil {
|
|
|
|
h.logf("ingress: failed hijacking conn")
|
|
|
|
http.Error(w, "failed hijacking conn", http.StatusInternalServerError)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
io.WriteString(conn, "HTTP/1.1 101 Switching Protocols\r\n\r\n")
|
2023-03-08 20:36:41 +00:00
|
|
|
return &ipn.FunnelConn{
|
|
|
|
Conn: conn,
|
|
|
|
Src: srcAddr,
|
|
|
|
Target: target,
|
|
|
|
}, true
|
2022-11-07 15:46:42 +00:00
|
|
|
}
|
|
|
|
sendRST := func() {
|
|
|
|
http.Error(w, "denied", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
2023-03-08 20:36:41 +00:00
|
|
|
h.ps.b.HandleIngressTCPConn(h.peerNode, target, srcAddr, getConn, sendRST)
|
2022-11-07 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 22:25:48 +00:00
|
|
|
func (h *peerAPIHandler) handleServeInterfaces(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-12-01 01:06:21 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
fmt.Fprintln(w, "<h1>Interfaces</h1>")
|
|
|
|
|
|
|
|
if dr, err := interfaces.DefaultRoute(); err == nil {
|
|
|
|
fmt.Fprintf(w, "<h3>Default route is %q(%d)</h3>\n", html.EscapeString(dr.InterfaceName), dr.InterfaceIndex)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "<h3>Could not get the default route: %s</h3>\n", html.EscapeString(err.Error()))
|
2022-04-26 22:25:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 19:48:03 +00:00
|
|
|
if hasCGNATInterface, err := interfaces.HasCGNATInterface(); hasCGNATInterface {
|
|
|
|
fmt.Fprintln(w, "<p>There is another interface using the CGNAT range.</p>")
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Fprintf(w, "<p>Could not check for CGNAT interfaces: %s</p>\n", html.EscapeString(err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-12-01 01:06:21 +00:00
|
|
|
i, err := interfaces.GetList()
|
2022-04-26 22:25:48 +00:00
|
|
|
if err != nil {
|
2022-12-01 01:06:21 +00:00
|
|
|
fmt.Fprintf(w, "Could not get interfaces: %s\n", html.EscapeString(err.Error()))
|
|
|
|
return
|
2022-04-26 22:25:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 19:48:03 +00:00
|
|
|
fmt.Fprintln(w, "<table style='border-collapse: collapse' border=1 cellspacing=0 cellpadding=2>")
|
2022-04-26 22:25:48 +00:00
|
|
|
fmt.Fprint(w, "<tr>")
|
2023-02-23 19:48:03 +00:00
|
|
|
for _, v := range []any{"Index", "Name", "MTU", "Flags", "Addrs", "Extra"} {
|
2022-04-26 22:25:48 +00:00
|
|
|
fmt.Fprintf(w, "<th>%v</th> ", v)
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "</tr>\n")
|
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 04:14:09 +00:00
|
|
|
i.ForeachInterface(func(iface interfaces.Interface, ipps []netip.Prefix) {
|
2022-04-26 22:25:48 +00:00
|
|
|
fmt.Fprint(w, "<tr>")
|
|
|
|
for _, v := range []any{iface.Index, iface.Name, iface.MTU, iface.Flags, ipps} {
|
2022-12-01 01:06:21 +00:00
|
|
|
fmt.Fprintf(w, "<td>%s</td> ", html.EscapeString(fmt.Sprintf("%v", v)))
|
2022-04-26 22:25:48 +00:00
|
|
|
}
|
2023-02-23 19:48:03 +00:00
|
|
|
if extras, err := interfaces.InterfaceDebugExtras(iface.Index); err == nil && extras != "" {
|
|
|
|
fmt.Fprintf(w, "<td>%s</td> ", html.EscapeString(extras))
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Fprintf(w, "<td>%s</td> ", html.EscapeString(err.Error()))
|
|
|
|
}
|
2022-04-26 22:25:48 +00:00
|
|
|
fmt.Fprint(w, "</tr>\n")
|
|
|
|
})
|
|
|
|
fmt.Fprintln(w, "</table>")
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:58:20 +00:00
|
|
|
func (h *peerAPIHandler) handleServeDoctor(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
fmt.Fprintln(w, "<h1>Doctor Output</h1>")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "<pre>")
|
|
|
|
|
|
|
|
h.ps.b.Doctor(r.Context(), func(format string, args ...any) {
|
2023-02-10 23:40:28 +00:00
|
|
|
line := fmt.Sprintf(format, args...)
|
2023-02-09 22:58:20 +00:00
|
|
|
fmt.Fprintln(w, html.EscapeString(line))
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "</pre>")
|
|
|
|
}
|
|
|
|
|
2023-02-03 20:07:58 +00:00
|
|
|
func (h *peerAPIHandler) handleServeSockStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
fmt.Fprintln(w, "<!DOCTYPE html><h1>Socket Stats</h1>")
|
|
|
|
|
2023-04-10 21:25:22 +00:00
|
|
|
if !sockstats.IsAvailable {
|
|
|
|
fmt.Fprintln(w, "Socket stats are not available for this client")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-14 20:34:04 +00:00
|
|
|
stats, interfaceStats, validation := sockstats.Get(), sockstats.GetInterfaces(), sockstats.GetValidation()
|
2023-02-03 20:07:58 +00:00
|
|
|
if stats == nil {
|
|
|
|
fmt.Fprintln(w, "No socket stats available")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "<table border='1' cellspacing='0' style='border-collapse: collapse;'>")
|
|
|
|
fmt.Fprintln(w, "<thead>")
|
|
|
|
fmt.Fprintln(w, "<th>Label</th>")
|
|
|
|
fmt.Fprintln(w, "<th>Tx</th>")
|
|
|
|
fmt.Fprintln(w, "<th>Rx</th>")
|
2023-03-14 20:34:04 +00:00
|
|
|
for _, iface := range interfaceStats.Interfaces {
|
2023-02-03 20:07:58 +00:00
|
|
|
fmt.Fprintf(w, "<th>Tx (%s)</th>", html.EscapeString(iface))
|
|
|
|
fmt.Fprintf(w, "<th>Rx (%s)</th>", html.EscapeString(iface))
|
|
|
|
}
|
2023-03-08 00:29:41 +00:00
|
|
|
fmt.Fprintln(w, "<th>Validation</th>")
|
2023-02-03 20:07:58 +00:00
|
|
|
fmt.Fprintln(w, "</thead>")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "<tbody>")
|
2023-03-06 23:35:50 +00:00
|
|
|
labels := make([]sockstats.Label, 0, len(stats.Stats))
|
2023-02-03 20:07:58 +00:00
|
|
|
for label := range stats.Stats {
|
|
|
|
labels = append(labels, label)
|
|
|
|
}
|
2023-03-06 23:35:50 +00:00
|
|
|
slices.SortFunc(labels, func(a, b sockstats.Label) bool {
|
|
|
|
return a.String() < b.String()
|
|
|
|
})
|
2023-02-03 20:07:58 +00:00
|
|
|
|
2023-03-08 00:29:41 +00:00
|
|
|
txTotal := uint64(0)
|
|
|
|
rxTotal := uint64(0)
|
|
|
|
txTotalByInterface := map[string]uint64{}
|
|
|
|
rxTotalByInterface := map[string]uint64{}
|
2023-02-03 20:07:58 +00:00
|
|
|
|
|
|
|
for _, label := range labels {
|
|
|
|
stat := stats.Stats[label]
|
|
|
|
fmt.Fprintln(w, "<tr>")
|
2023-03-06 23:35:50 +00:00
|
|
|
fmt.Fprintf(w, "<td>%s</td>", html.EscapeString(label.String()))
|
2023-02-03 20:07:58 +00:00
|
|
|
fmt.Fprintf(w, "<td align=right>%d</td>", stat.TxBytes)
|
|
|
|
fmt.Fprintf(w, "<td align=right>%d</td>", stat.RxBytes)
|
|
|
|
|
|
|
|
txTotal += stat.TxBytes
|
|
|
|
rxTotal += stat.RxBytes
|
|
|
|
|
2023-03-14 20:34:04 +00:00
|
|
|
if interfaceStat, ok := interfaceStats.Stats[label]; ok {
|
|
|
|
for _, iface := range interfaceStats.Interfaces {
|
|
|
|
fmt.Fprintf(w, "<td align=right>%d</td>", interfaceStat.TxBytesByInterface[iface])
|
|
|
|
fmt.Fprintf(w, "<td align=right>%d</td>", interfaceStat.RxBytesByInterface[iface])
|
|
|
|
txTotalByInterface[iface] += interfaceStat.TxBytesByInterface[iface]
|
|
|
|
rxTotalByInterface[iface] += interfaceStat.RxBytesByInterface[iface]
|
|
|
|
}
|
2023-02-03 20:07:58 +00:00
|
|
|
}
|
2023-03-08 00:29:41 +00:00
|
|
|
|
2023-03-09 19:40:07 +00:00
|
|
|
if validationStat, ok := validation.Stats[label]; ok && (validationStat.RxBytes > 0 || validationStat.TxBytes > 0) {
|
2023-03-08 00:29:41 +00:00
|
|
|
fmt.Fprintf(w, "<td>Tx=%d (%+d) Rx=%d (%+d)</td>",
|
2023-03-09 19:40:07 +00:00
|
|
|
validationStat.TxBytes,
|
|
|
|
int64(validationStat.TxBytes)-int64(stat.TxBytes),
|
|
|
|
validationStat.RxBytes,
|
|
|
|
int64(validationStat.RxBytes)-int64(stat.RxBytes))
|
2023-03-08 00:29:41 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "<td></td>")
|
|
|
|
}
|
|
|
|
|
2023-02-03 20:07:58 +00:00
|
|
|
fmt.Fprintln(w, "</tr>")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "</tbody>")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "<tfoot>")
|
|
|
|
fmt.Fprintln(w, "<th>Total</th>")
|
|
|
|
fmt.Fprintf(w, "<th>%d</th>", txTotal)
|
|
|
|
fmt.Fprintf(w, "<th>%d</th>", rxTotal)
|
2023-03-14 20:34:04 +00:00
|
|
|
for _, iface := range interfaceStats.Interfaces {
|
2023-02-03 20:07:58 +00:00
|
|
|
fmt.Fprintf(w, "<th>%d</th>", txTotalByInterface[iface])
|
|
|
|
fmt.Fprintf(w, "<th>%d</th>", rxTotalByInterface[iface])
|
|
|
|
}
|
2023-03-08 00:29:41 +00:00
|
|
|
fmt.Fprintln(w, "<th></th>")
|
2023-02-03 20:07:58 +00:00
|
|
|
fmt.Fprintln(w, "</tfoot>")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "</table>")
|
2023-04-19 20:45:02 +00:00
|
|
|
|
|
|
|
fmt.Fprintln(w, "<h2>Debug Info</h2>")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "<pre>")
|
|
|
|
fmt.Fprintln(w, html.EscapeString(sockstats.DebugInfo()))
|
|
|
|
fmt.Fprintln(w, "</pre>")
|
2023-02-03 20:07:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 21:54:25 +00:00
|
|
|
type incomingFile struct {
|
2021-04-16 19:33:04 +00:00
|
|
|
name string // "foo.jpg"
|
|
|
|
started time.Time
|
|
|
|
size int64 // or -1 if unknown; never 0
|
|
|
|
w io.Writer // underlying writer
|
|
|
|
ph *peerAPIHandler
|
|
|
|
partialPath string // non-empty in direct mode
|
2021-04-08 21:54:25 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
copied int64
|
2021-04-16 19:33:04 +00:00
|
|
|
done bool
|
2021-04-08 21:54:25 +00:00
|
|
|
lastNotify time.Time
|
2021-04-12 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:33:04 +00:00
|
|
|
func (f *incomingFile) markAndNotifyDone() {
|
2021-04-12 21:05:44 +00:00
|
|
|
f.mu.Lock()
|
2021-04-16 19:33:04 +00:00
|
|
|
f.done = true
|
2021-04-12 21:05:44 +00:00
|
|
|
f.mu.Unlock()
|
|
|
|
b := f.ph.ps.b
|
|
|
|
b.sendFileNotify()
|
2021-04-08 21:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *incomingFile) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = f.w.Write(p)
|
|
|
|
|
|
|
|
b := f.ph.ps.b
|
|
|
|
var needNotify bool
|
|
|
|
defer func() {
|
|
|
|
if needNotify {
|
|
|
|
b.sendFileNotify()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if n > 0 {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.copied += int64(n)
|
|
|
|
now := time.Now()
|
|
|
|
if f.lastNotify.IsZero() || now.Sub(f.lastNotify) > time.Second {
|
|
|
|
f.lastNotify = now
|
|
|
|
needNotify = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *incomingFile) PartialFile() ipn.PartialFile {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
return ipn.PartialFile{
|
|
|
|
Name: f.name,
|
|
|
|
Started: f.started,
|
|
|
|
DeclaredSize: f.size,
|
|
|
|
Received: f.copied,
|
2021-04-16 19:33:04 +00:00
|
|
|
PartialPath: f.partialPath,
|
|
|
|
Done: f.done,
|
2021-04-08 21:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 15:45:49 +00:00
|
|
|
// canPutFile reports whether h can put a file ("Taildrop") to this node.
|
|
|
|
func (h *peerAPIHandler) canPutFile() bool {
|
2022-11-23 23:14:25 +00:00
|
|
|
if h.peerNode.UnsignedPeerAPIOnly {
|
|
|
|
// Unsigned peers can't send files.
|
|
|
|
return false
|
|
|
|
}
|
2022-04-18 17:06:41 +00:00
|
|
|
return h.isSelf || h.peerHasCap(tailcfg.CapabilityFileSharingSend)
|
|
|
|
}
|
|
|
|
|
|
|
|
// canDebug reports whether h can debug this node (goroutines, metrics,
|
|
|
|
// magicsock internal state, etc).
|
|
|
|
func (h *peerAPIHandler) canDebug() bool {
|
2022-11-16 19:07:21 +00:00
|
|
|
if !slices.Contains(h.selfNode.Capabilities, tailcfg.CapabilityDebug) {
|
2022-11-16 18:36:01 +00:00
|
|
|
// This node does not expose debug info.
|
|
|
|
return false
|
|
|
|
}
|
2022-11-23 23:14:25 +00:00
|
|
|
if h.peerNode.UnsignedPeerAPIOnly {
|
|
|
|
// Unsigned peers can't debug.
|
|
|
|
return false
|
|
|
|
}
|
2022-04-18 17:06:41 +00:00
|
|
|
return h.isSelf || h.peerHasCap(tailcfg.CapabilityDebugPeer)
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:29:41 +00:00
|
|
|
// canWakeOnLAN reports whether h can send a Wake-on-LAN packet from this node.
|
|
|
|
func (h *peerAPIHandler) canWakeOnLAN() bool {
|
2023-01-10 23:40:07 +00:00
|
|
|
if h.peerNode.UnsignedPeerAPIOnly {
|
|
|
|
return false
|
|
|
|
}
|
2022-04-26 19:29:41 +00:00
|
|
|
return h.isSelf || h.peerHasCap(tailcfg.CapabilityWakeOnLAN)
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:46:42 +00:00
|
|
|
var allowSelfIngress = envknob.RegisterBool("TS_ALLOW_SELF_INGRESS")
|
|
|
|
|
|
|
|
// canIngress reports whether h can send ingress requests to this node.
|
|
|
|
func (h *peerAPIHandler) canIngress() bool {
|
|
|
|
return h.peerHasCap(tailcfg.CapabilityIngress) || (allowSelfIngress() && h.isSelf)
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:06:41 +00:00
|
|
|
func (h *peerAPIHandler) peerHasCap(wantCap string) bool {
|
2022-07-25 03:08:42 +00:00
|
|
|
for _, hasCap := range h.ps.b.PeerCaps(h.remoteAddr.Addr()) {
|
2022-04-18 17:06:41 +00:00
|
|
|
if hasCap == wantCap {
|
|
|
|
return true
|
2022-04-17 15:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-20 03:21:48 +00:00
|
|
|
func (h *peerAPIHandler) handlePeerPut(w http.ResponseWriter, r *http.Request) {
|
2022-11-24 04:16:31 +00:00
|
|
|
if !envknob.CanTaildrop() {
|
|
|
|
http.Error(w, "Taildrop disabled on device", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2022-04-17 15:45:49 +00:00
|
|
|
if !h.canPutFile() {
|
|
|
|
http.Error(w, "Taildrop access denied", http.StatusForbidden)
|
2021-03-29 17:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-16 17:57:46 +00:00
|
|
|
if !h.ps.b.hasCapFileSharing() {
|
|
|
|
http.Error(w, "file sharing not enabled by Tailscale admin", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-03-29 17:42:33 +00:00
|
|
|
if r.Method != "PUT" {
|
2021-04-20 04:00:25 +00:00
|
|
|
http.Error(w, "expected method PUT", http.StatusMethodNotAllowed)
|
2021-03-29 17:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if h.ps.rootDir == "" {
|
2021-12-01 23:00:23 +00:00
|
|
|
http.Error(w, errNoTaildrop.Error(), http.StatusInternalServerError)
|
2021-03-29 17:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-03 03:57:52 +00:00
|
|
|
if distro.Get() == distro.Unraid && !h.ps.directFileMode {
|
|
|
|
http.Error(w, "Taildrop folder not configured or accessible", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-04-20 04:57:08 +00:00
|
|
|
rawPath := r.URL.EscapedPath()
|
2023-02-01 21:43:06 +00:00
|
|
|
suffix, ok := strings.CutPrefix(rawPath, "/v0/put/")
|
2022-09-16 05:08:45 +00:00
|
|
|
if !ok {
|
2021-04-20 04:57:08 +00:00
|
|
|
http.Error(w, "misconfigured internals", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if suffix == "" {
|
|
|
|
http.Error(w, "empty filename", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.Contains(suffix, "/") {
|
|
|
|
http.Error(w, "directories not supported", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
baseName, err := url.PathUnescape(suffix)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bad path encoding", 400)
|
|
|
|
return
|
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
dstFile, ok := h.ps.diskPath(baseName)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "bad filename", 400)
|
2021-03-29 17:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-04 18:09:00 +00:00
|
|
|
t0 := time.Now()
|
2021-04-26 18:28:27 +00:00
|
|
|
// TODO(bradfitz): prevent same filename being sent by two peers at once
|
|
|
|
partialFile := dstFile + partialSuffix
|
|
|
|
f, err := os.Create(partialFile)
|
2021-03-29 17:42:33 +00:00
|
|
|
if err != nil {
|
2021-04-26 18:28:27 +00:00
|
|
|
h.logf("put Create error: %v", redactErr(err))
|
2021-03-29 17:42:33 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var success bool
|
|
|
|
defer func() {
|
|
|
|
if !success {
|
2021-04-26 18:28:27 +00:00
|
|
|
os.Remove(partialFile)
|
2021-03-29 17:42:33 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-04-08 21:54:25 +00:00
|
|
|
var finalSize int64
|
2021-04-12 21:05:44 +00:00
|
|
|
var inFile *incomingFile
|
2021-04-08 21:54:25 +00:00
|
|
|
if r.ContentLength != 0 {
|
2021-04-12 21:05:44 +00:00
|
|
|
inFile = &incomingFile{
|
2021-04-08 21:54:25 +00:00
|
|
|
name: baseName,
|
|
|
|
started: time.Now(),
|
|
|
|
size: r.ContentLength,
|
|
|
|
w: f,
|
|
|
|
ph: h,
|
|
|
|
}
|
2021-04-16 19:33:04 +00:00
|
|
|
if h.ps.directFileMode {
|
2021-04-26 18:28:27 +00:00
|
|
|
inFile.partialPath = partialFile
|
2021-04-16 19:33:04 +00:00
|
|
|
}
|
2021-04-08 21:54:25 +00:00
|
|
|
h.ps.b.registerIncomingFile(inFile, true)
|
|
|
|
defer h.ps.b.registerIncomingFile(inFile, false)
|
|
|
|
n, err := io.Copy(inFile, r.Body)
|
|
|
|
if err != nil {
|
2021-05-04 16:53:37 +00:00
|
|
|
err = redactErr(err)
|
2021-04-08 21:54:25 +00:00
|
|
|
f.Close()
|
|
|
|
h.logf("put Copy error: %v", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
finalSize = n
|
2021-03-29 17:42:33 +00:00
|
|
|
}
|
2021-05-04 16:53:37 +00:00
|
|
|
if err := redactErr(f.Close()); err != nil {
|
2021-03-29 17:42:33 +00:00
|
|
|
h.logf("put Close error: %v", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-12-06 20:24:25 +00:00
|
|
|
if h.ps.directFileMode && !h.ps.directFileDoFinalRename {
|
2021-04-12 21:05:44 +00:00
|
|
|
if inFile != nil { // non-zero length; TODO: notify even for zero length
|
2021-04-16 19:33:04 +00:00
|
|
|
inFile.markAndNotifyDone()
|
2021-04-12 21:05:44 +00:00
|
|
|
}
|
2021-04-26 18:28:27 +00:00
|
|
|
} else {
|
|
|
|
if err := os.Rename(partialFile, dstFile); err != nil {
|
|
|
|
err = redactErr(err)
|
|
|
|
h.logf("put final rename: %v", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-04-12 21:05:44 +00:00
|
|
|
}
|
2021-03-29 17:42:33 +00:00
|
|
|
|
2021-05-04 18:09:00 +00:00
|
|
|
d := time.Since(t0).Round(time.Second / 10)
|
2022-07-25 03:08:42 +00:00
|
|
|
h.logf("got put of %s in %v from %v/%v", approxSize(finalSize), d, h.remoteAddr.Addr(), h.peerNode.ComputedName)
|
2021-03-29 17:42:33 +00:00
|
|
|
|
|
|
|
// TODO: set modtime
|
|
|
|
// TODO: some real response
|
|
|
|
success = true
|
|
|
|
io.WriteString(w, "{}\n")
|
2022-08-04 04:51:02 +00:00
|
|
|
h.ps.knownEmpty.Store(false)
|
2021-04-08 21:54:25 +00:00
|
|
|
h.ps.b.sendFileNotify()
|
2021-03-29 17:42:33 +00:00
|
|
|
}
|
2021-03-30 19:56:00 +00:00
|
|
|
|
|
|
|
func approxSize(n int64) string {
|
|
|
|
if n <= 1<<10 {
|
|
|
|
return "<=1KB"
|
|
|
|
}
|
|
|
|
if n <= 1<<20 {
|
|
|
|
return "<=1MB"
|
|
|
|
}
|
2021-04-22 15:43:52 +00:00
|
|
|
return fmt.Sprintf("~%dMB", n>>20)
|
2021-03-30 19:56:00 +00:00
|
|
|
}
|
2021-04-22 20:01:42 +00:00
|
|
|
|
|
|
|
func (h *peerAPIHandler) handleServeGoroutines(w http.ResponseWriter, r *http.Request) {
|
2022-04-18 17:06:41 +00:00
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
2021-04-22 20:01:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf []byte
|
|
|
|
for size := 4 << 10; size <= 2<<20; size *= 2 {
|
|
|
|
buf = make([]byte, size)
|
|
|
|
buf = buf[:runtime.Stack(buf, true)]
|
|
|
|
if len(buf) < size {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
2021-11-03 18:17:56 +00:00
|
|
|
|
|
|
|
func (h *peerAPIHandler) handleServeEnv(w http.ResponseWriter, r *http.Request) {
|
2022-04-18 17:06:41 +00:00
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
2021-11-03 18:17:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var data struct {
|
|
|
|
Hostinfo *tailcfg.Hostinfo
|
|
|
|
Uid int
|
|
|
|
Args []string
|
|
|
|
Env []string
|
|
|
|
}
|
|
|
|
data.Hostinfo = hostinfo.New()
|
|
|
|
data.Uid = os.Getuid()
|
|
|
|
data.Args = os.Args
|
|
|
|
data.Env = os.Environ()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(data)
|
|
|
|
}
|
2021-11-18 15:54:24 +00:00
|
|
|
|
2021-12-21 18:26:13 +00:00
|
|
|
func (h *peerAPIHandler) handleServeMagicsock(w http.ResponseWriter, r *http.Request) {
|
2022-04-18 17:06:41 +00:00
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
2021-12-21 18:26:13 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-03 20:57:17 +00:00
|
|
|
if mc, ok := h.ps.b.sys.MagicSock.GetOK(); ok {
|
|
|
|
mc.ServeHTTPDebug(w, r)
|
|
|
|
return
|
2021-12-21 18:26:13 +00:00
|
|
|
}
|
|
|
|
http.Error(w, "miswired", 500)
|
|
|
|
}
|
|
|
|
|
2021-11-18 15:54:24 +00:00
|
|
|
func (h *peerAPIHandler) handleServeMetrics(w http.ResponseWriter, r *http.Request) {
|
2022-04-18 17:06:41 +00:00
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
2021-11-18 15:54:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
clientmetric.WritePrometheusExpositionFormat(w)
|
|
|
|
}
|
2021-11-23 05:45:34 +00:00
|
|
|
|
2021-12-21 21:52:50 +00:00
|
|
|
func (h *peerAPIHandler) handleServeDNSFwd(w http.ResponseWriter, r *http.Request) {
|
2022-04-26 19:29:41 +00:00
|
|
|
if !h.canDebug() {
|
|
|
|
http.Error(w, "denied; no debug access", http.StatusForbidden)
|
2021-12-21 21:52:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dh := health.DebugHandler("dnsfwd")
|
|
|
|
if dh == nil {
|
|
|
|
http.Error(w, "not wired up", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dh.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:29:41 +00:00
|
|
|
func (h *peerAPIHandler) handleWakeOnLAN(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !h.canWakeOnLAN() {
|
|
|
|
http.Error(w, "no WoL access", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "bad method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
macStr := r.FormValue("mac")
|
|
|
|
if macStr == "" {
|
|
|
|
http.Error(w, "missing 'mac' param", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mac, err := net.ParseMAC(macStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bad 'mac' param", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var password []byte // TODO(bradfitz): support?
|
|
|
|
st, err := interfaces.GetState()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed to get interfaces state", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var res struct {
|
|
|
|
SentTo []string
|
|
|
|
Errors []string
|
|
|
|
}
|
|
|
|
for ifName, ips := range st.InterfaceIPs {
|
|
|
|
for _, ip := range ips {
|
2022-07-25 03:08:42 +00:00
|
|
|
if ip.Addr().IsLoopback() || ip.Addr().Is6() {
|
2022-04-26 19:29:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
local := &net.UDPAddr{
|
2022-07-25 03:08:42 +00:00
|
|
|
IP: ip.Addr().AsSlice(),
|
2022-04-26 19:29:41 +00:00
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
remote := &net.UDPAddr{
|
|
|
|
IP: net.IPv4bcast,
|
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
if err := wol.Wake(mac, password, local, remote); err != nil {
|
|
|
|
res.Errors = append(res.Errors, err.Error())
|
|
|
|
} else {
|
|
|
|
res.SentTo = append(res.SentTo, ifName)
|
|
|
|
}
|
|
|
|
break // one per interface is enough
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(res.SentTo)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2021-11-23 05:45:34 +00:00
|
|
|
func (h *peerAPIHandler) replyToDNSQueries() bool {
|
2021-11-25 17:43:39 +00:00
|
|
|
if h.isSelf {
|
|
|
|
// If the peer is owned by the same user, just allow it
|
|
|
|
// without further checks.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
b := h.ps.b
|
|
|
|
if !b.OfferingExitNode() {
|
|
|
|
// If we're not an exit node, there's no point to
|
|
|
|
// being a DNS server for somebody.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !h.remoteAddr.IsValid() {
|
|
|
|
// This should never be the case if the peerAPIHandler
|
|
|
|
// was wired up correctly, but just in case.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Otherwise, we're an exit node but the peer is not us, so
|
|
|
|
// we need to check if they're allowed access to the internet.
|
|
|
|
// As peerapi bypasses wgengine/filter checks, we need to check
|
|
|
|
// ourselves. As a proxy for autogroup:internet access, we see
|
|
|
|
// if we would've accepted a packet to 0.0.0.0:53. We treat
|
|
|
|
// the IP 0.0.0.0 as being "the internet".
|
2022-08-04 04:31:40 +00:00
|
|
|
f := b.filterAtomic.Load()
|
|
|
|
if f == nil {
|
2021-11-25 17:43:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Note: we check TCP here because the Filter type already had
|
|
|
|
// a CheckTCP method (for unit tests), but it's pretty
|
|
|
|
// arbitrary. DNS runs over TCP and UDP, so sure... we check
|
|
|
|
// TCP.
|
|
|
|
dstIP := netaddr.IPv4(0, 0, 0, 0)
|
2022-07-25 03:08:42 +00:00
|
|
|
remoteIP := h.remoteAddr.Addr()
|
2021-11-25 17:43:39 +00:00
|
|
|
if remoteIP.Is6() {
|
|
|
|
// autogroup:internet for IPv6 is defined to start with 2000::/3,
|
|
|
|
// so use 2000::0 as the probe "the internet" address.
|
2022-07-26 03:55:44 +00:00
|
|
|
dstIP = netip.MustParseAddr("2000::")
|
2021-11-25 17:43:39 +00:00
|
|
|
}
|
|
|
|
verdict := f.CheckTCP(remoteIP, dstIP, 53)
|
|
|
|
return verdict == filter.Accept
|
2021-11-23 05:45:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 17:58:34 +00:00
|
|
|
// handleDNSQuery implements a DoH server (RFC 8484) over the peerapi.
|
|
|
|
// It's not over HTTPS as the spec dictates, but rather HTTP-over-WireGuard.
|
2021-11-23 05:45:34 +00:00
|
|
|
func (h *peerAPIHandler) handleDNSQuery(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.ps.resolver == nil {
|
|
|
|
http.Error(w, "DNS not wired up", http.StatusNotImplemented)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
http.Error(w, "DNS access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-11-23 17:58:34 +00:00
|
|
|
pretty := false // non-DoH debug mode for humans
|
2021-11-23 05:45:34 +00:00
|
|
|
q, publicError := dohQuery(r)
|
2021-11-23 17:58:34 +00:00
|
|
|
if publicError != "" && r.Method == "GET" {
|
|
|
|
if name := r.FormValue("q"); name != "" {
|
|
|
|
pretty = true
|
|
|
|
publicError = ""
|
|
|
|
q = dnsQueryForName(name, r.FormValue("t"))
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 05:45:34 +00:00
|
|
|
if publicError != "" {
|
|
|
|
http.Error(w, publicError, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-11-23 17:58:34 +00:00
|
|
|
|
|
|
|
// Some timeout that's short enough to be noticed by humans
|
|
|
|
// but long enough that it's longer than real DNS timeouts.
|
|
|
|
const arbitraryTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), arbitraryTimeout)
|
|
|
|
defer cancel()
|
2021-11-29 22:18:09 +00:00
|
|
|
res, err := h.ps.resolver.HandleExitNodeDNSQuery(ctx, q, h.remoteAddr, h.ps.b.allowExitNodeDNSProxyToServeName)
|
2021-11-23 17:58:34 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logf("handleDNS fwd error: %v", err)
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "DNS forwarding error", 500)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pretty {
|
|
|
|
// Non-standard response for interactive debugging.
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
writePrettyDNSReply(w, res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/dns-message")
|
2021-11-30 22:18:48 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(res)))
|
2021-11-23 17:58:34 +00:00
|
|
|
w.Write(res)
|
2021-11-23 05:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dohQuery(r *http.Request) (dnsQuery []byte, publicErr string) {
|
|
|
|
const maxQueryLen = 256 << 10
|
|
|
|
switch r.Method {
|
|
|
|
default:
|
|
|
|
return nil, "bad HTTP method"
|
|
|
|
case "GET":
|
|
|
|
q64 := r.FormValue("dns")
|
|
|
|
if q64 == "" {
|
|
|
|
return nil, "missing 'dns' parameter"
|
|
|
|
}
|
|
|
|
if base64.RawURLEncoding.DecodedLen(len(q64)) > maxQueryLen {
|
|
|
|
return nil, "query too large"
|
|
|
|
}
|
|
|
|
q, err := base64.RawURLEncoding.DecodeString(q64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "invalid 'dns' base64 encoding"
|
|
|
|
}
|
|
|
|
return q, ""
|
|
|
|
case "POST":
|
|
|
|
if r.Header.Get("Content-Type") != "application/dns-message" {
|
|
|
|
return nil, "unexpected Content-Type"
|
|
|
|
}
|
|
|
|
q, err := io.ReadAll(io.LimitReader(r.Body, maxQueryLen+1))
|
|
|
|
if err != nil {
|
|
|
|
return nil, "error reading post body with DNS query"
|
|
|
|
}
|
|
|
|
if len(q) > maxQueryLen {
|
|
|
|
return nil, "query too large"
|
|
|
|
}
|
|
|
|
return q, ""
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 17:58:34 +00:00
|
|
|
|
|
|
|
func dnsQueryForName(name, typStr string) []byte {
|
|
|
|
typ := dnsmessage.TypeA
|
|
|
|
switch strings.ToLower(typStr) {
|
|
|
|
case "aaaa":
|
|
|
|
typ = dnsmessage.TypeAAAA
|
|
|
|
case "txt":
|
|
|
|
typ = dnsmessage.TypeTXT
|
|
|
|
}
|
|
|
|
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{
|
|
|
|
OpCode: 0, // query
|
|
|
|
RecursionDesired: true,
|
|
|
|
ID: 0,
|
|
|
|
})
|
|
|
|
if !strings.HasSuffix(name, ".") {
|
|
|
|
name += "."
|
|
|
|
}
|
|
|
|
b.StartQuestions()
|
|
|
|
b.Question(dnsmessage.Question{
|
|
|
|
Name: dnsmessage.MustNewName(name),
|
|
|
|
Type: typ,
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
})
|
|
|
|
msg, _ := b.Finish()
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePrettyDNSReply(w io.Writer, res []byte) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
j, _ := json.Marshal(struct {
|
|
|
|
Error string
|
|
|
|
}{err.Error()})
|
2021-11-29 22:18:09 +00:00
|
|
|
j = append(j, '\n')
|
2021-11-23 17:58:34 +00:00
|
|
|
w.Write(j)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var p dnsmessage.Parser
|
2021-11-29 22:18:09 +00:00
|
|
|
hdr, err := p.Start(res)
|
|
|
|
if err != nil {
|
2021-11-23 17:58:34 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-29 22:18:09 +00:00
|
|
|
if hdr.RCode != dnsmessage.RCodeSuccess {
|
|
|
|
return fmt.Errorf("DNS RCode = %v", hdr.RCode)
|
|
|
|
}
|
2021-11-23 17:58:34 +00:00
|
|
|
if err := p.SkipAllQuestions(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotIPs []string
|
|
|
|
for {
|
|
|
|
h, err := p.AnswerHeader()
|
|
|
|
if err == dnsmessage.ErrSectionDone {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if h.Class != dnsmessage.ClassINET {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch h.Type {
|
|
|
|
case dnsmessage.TypeA:
|
|
|
|
r, err := p.AResource()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gotIPs = append(gotIPs, net.IP(r.A[:]).String())
|
|
|
|
case dnsmessage.TypeAAAA:
|
|
|
|
r, err := p.AAAAResource()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gotIPs = append(gotIPs, net.IP(r.AAAA[:]).String())
|
|
|
|
case dnsmessage.TypeTXT:
|
|
|
|
r, err := p.TXTResource()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gotIPs = append(gotIPs, r.TXT...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j, _ := json.Marshal(gotIPs)
|
|
|
|
j = append(j, '\n')
|
|
|
|
w.Write(j)
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-20 18:57:34 +00:00
|
|
|
|
|
|
|
// newFakePeerAPIListener creates a new net.Listener that acts like
|
|
|
|
// it's listening on the provided IP address and on TCP port 1.
|
|
|
|
//
|
|
|
|
// See docs on fakePeerAPIListener.
|
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 04:14:09 +00:00
|
|
|
func newFakePeerAPIListener(ip netip.Addr) net.Listener {
|
2022-04-20 18:57:34 +00:00
|
|
|
return &fakePeerAPIListener{
|
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 04:14:09 +00:00
|
|
|
addr: net.TCPAddrFromAddrPort(netip.AddrPortFrom(ip, 1)),
|
2022-04-20 18:57:34 +00:00
|
|
|
closed: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fakePeerAPIListener is a net.Listener that has an Addr method returning a TCPAddr
|
|
|
|
// for a given IP on port 1 (arbitrary) and can be Closed, but otherwise Accept
|
|
|
|
// just blocks forever until closed. The purpose of this is to let the rest
|
|
|
|
// of the LocalBackend/PeerAPI code run and think it's talking to the kernel,
|
|
|
|
// even if the kernel isn't cooperating (like on Android: Issue 4449, 4293, etc)
|
|
|
|
// or we lack permission to listen on a port. It's okay to not actually listen via
|
|
|
|
// the kernel because on almost all platforms (except iOS as of 2022-04-20) we
|
2022-09-25 18:29:55 +00:00
|
|
|
// also intercept incoming netstack TCP requests to our peerapi port and hand them over
|
2022-04-20 18:57:34 +00:00
|
|
|
// directly to peerapi, without involving the kernel. So this doesn't need to be
|
|
|
|
// real. But the port number we return (1, in this case) is the port number we advertise
|
|
|
|
// to peers and they connect to. 1 seems pretty safe to use. Even if the kernel's
|
|
|
|
// using it, it doesn't matter, as we intercept it first in netstack and the kernel
|
|
|
|
// never notices.
|
|
|
|
//
|
|
|
|
// Eventually we'll remove this code and do this on all platforms, when iOS also uses
|
|
|
|
// netstack.
|
|
|
|
type fakePeerAPIListener struct {
|
|
|
|
addr net.Addr
|
|
|
|
|
|
|
|
closeOnce sync.Once
|
|
|
|
closed chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl *fakePeerAPIListener) Close() error {
|
|
|
|
fl.closeOnce.Do(func() { close(fl.closed) })
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl *fakePeerAPIListener) Accept() (net.Conn, error) {
|
|
|
|
<-fl.closed
|
2022-04-25 18:02:59 +00:00
|
|
|
return nil, net.ErrClosed
|
2022-04-20 18:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fl *fakePeerAPIListener) Addr() net.Addr { return fl.addr }
|
2022-12-09 22:21:53 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
metricInvalidRequests = clientmetric.NewCounter("peerapi_invalid_requests")
|
|
|
|
|
|
|
|
// Non-debug PeerAPI endpoints.
|
|
|
|
metricPutCalls = clientmetric.NewCounter("peerapi_put")
|
|
|
|
metricDNSCalls = clientmetric.NewCounter("peerapi_dns")
|
|
|
|
metricWakeOnLANCalls = clientmetric.NewCounter("peerapi_wol")
|
|
|
|
metricIngressCalls = clientmetric.NewCounter("peerapi_ingress")
|
|
|
|
)
|