mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
386 lines
10 KiB
Go
386 lines
10 KiB
Go
10 months ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
|
|
||
|
package ipnlocal
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
8 months ago
|
"os"
|
||
10 months ago
|
"regexp"
|
||
8 months ago
|
"slices"
|
||
10 months ago
|
"strings"
|
||
|
|
||
8 months ago
|
"tailscale.com/drive"
|
||
10 months ago
|
"tailscale.com/ipn"
|
||
|
"tailscale.com/tailcfg"
|
||
|
"tailscale.com/types/netmap"
|
||
9 months ago
|
"tailscale.com/types/views"
|
||
10 months ago
|
)
|
||
|
|
||
|
const (
|
||
9 months ago
|
// TailFSLocalPort is the port on which the TailFS listens for location
|
||
10 months ago
|
// connections on quad 100.
|
||
9 months ago
|
TailFSLocalPort = 8080
|
||
10 months ago
|
)
|
||
|
|
||
|
var (
|
||
|
shareNameRegex = regexp.MustCompile(`^[a-z0-9_\(\) ]+$`)
|
||
8 months ago
|
ErrTailFSNotEnabled = errors.New("TailFS not enabled")
|
||
|
ErrInvalidShareName = errors.New("Share names may only contain the letters a-z, underscore _, parentheses (), or spaces")
|
||
10 months ago
|
)
|
||
|
|
||
9 months ago
|
// TailFSSharingEnabled reports whether sharing to remote nodes via tailfs is
|
||
10 months ago
|
// enabled. This is currently based on checking for the tailfs:share node
|
||
|
// attribute.
|
||
9 months ago
|
func (b *LocalBackend) TailFSSharingEnabled() bool {
|
||
10 months ago
|
b.mu.Lock()
|
||
|
defer b.mu.Unlock()
|
||
9 months ago
|
return b.tailFSSharingEnabledLocked()
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
func (b *LocalBackend) tailFSSharingEnabledLocked() bool {
|
||
9 months ago
|
return b.netMap != nil && b.netMap.SelfNode.HasCap(tailcfg.NodeAttrsTailFSShare)
|
||
|
}
|
||
|
|
||
|
// TailFSAccessEnabled reports whether accessing TailFS shares on remote nodes
|
||
|
// is enabled. This is currently based on checking for the tailfs:access node
|
||
|
// attribute.
|
||
|
func (b *LocalBackend) TailFSAccessEnabled() bool {
|
||
|
b.mu.Lock()
|
||
|
defer b.mu.Unlock()
|
||
|
return b.tailFSAccessEnabledLocked()
|
||
|
}
|
||
|
|
||
|
func (b *LocalBackend) tailFSAccessEnabledLocked() bool {
|
||
|
return b.netMap != nil && b.netMap.SelfNode.HasCap(tailcfg.NodeAttrsTailFSAccess)
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
// TailFSSetFileServerAddr tells tailfs to use the given address for connecting
|
||
10 months ago
|
// to the tailfs.FileServer that's exposing local files as an unprivileged
|
||
|
// user.
|
||
9 months ago
|
func (b *LocalBackend) TailFSSetFileServerAddr(addr string) error {
|
||
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
||
|
if !ok {
|
||
8 months ago
|
return ErrTailFSNotEnabled
|
||
10 months ago
|
}
|
||
|
|
||
|
fs.SetFileServerAddr(addr)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
8 months ago
|
// TailFSSetShare adds the given share if no share with that name exists, or
|
||
|
// replaces the existing share if one with the same name already exists. To
|
||
|
// avoid potential incompatibilities across file systems, share names are
|
||
10 months ago
|
// limited to alphanumeric characters and the underscore _.
|
||
8 months ago
|
func (b *LocalBackend) TailFSSetShare(share *drive.Share) error {
|
||
10 months ago
|
var err error
|
||
|
share.Name, err = normalizeShareName(share.Name)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
b.mu.Lock()
|
||
8 months ago
|
shares, err := b.tailFSSetShareLocked(share)
|
||
10 months ago
|
b.mu.Unlock()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
9 months ago
|
b.tailFSNotifyShares(shares)
|
||
10 months ago
|
return nil
|
||
|
}
|
||
|
|
||
|
// normalizeShareName normalizes the given share name and returns an error if
|
||
|
// it contains any disallowed characters.
|
||
|
func normalizeShareName(name string) (string, error) {
|
||
|
// Force all share names to lowercase to avoid potential incompatibilities
|
||
|
// with clients that don't support case-sensitive filenames.
|
||
|
name = strings.ToLower(name)
|
||
|
|
||
|
// Trim whitespace
|
||
|
name = strings.TrimSpace(name)
|
||
|
|
||
|
if !shareNameRegex.MatchString(name) {
|
||
8 months ago
|
return "", ErrInvalidShareName
|
||
10 months ago
|
}
|
||
|
|
||
|
return name, nil
|
||
|
}
|
||
|
|
||
8 months ago
|
func (b *LocalBackend) tailFSSetShareLocked(share *drive.Share) (views.SliceView[*drive.Share, drive.ShareView], error) {
|
||
9 months ago
|
existingShares := b.pm.prefs.TailFSShares()
|
||
|
|
||
9 months ago
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
||
|
if !ok {
|
||
8 months ago
|
return existingShares, ErrTailFSNotEnabled
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
addedShare := false
|
||
8 months ago
|
var shares []*drive.Share
|
||
9 months ago
|
for i := 0; i < existingShares.Len(); i++ {
|
||
|
existing := existingShares.At(i)
|
||
|
if existing.Name() != share.Name {
|
||
|
if !addedShare && existing.Name() > share.Name {
|
||
|
// Add share in order
|
||
|
shares = append(shares, share)
|
||
|
addedShare = true
|
||
|
}
|
||
|
shares = append(shares, existing.AsStruct())
|
||
|
}
|
||
10 months ago
|
}
|
||
9 months ago
|
if !addedShare {
|
||
|
shares = append(shares, share)
|
||
10 months ago
|
}
|
||
9 months ago
|
|
||
|
err := b.tailFSSetSharesLocked(shares)
|
||
10 months ago
|
if err != nil {
|
||
9 months ago
|
return existingShares, err
|
||
10 months ago
|
}
|
||
9 months ago
|
fs.SetShares(shares)
|
||
10 months ago
|
|
||
9 months ago
|
return b.pm.prefs.TailFSShares(), nil
|
||
10 months ago
|
}
|
||
|
|
||
8 months ago
|
// TailFSRenameShare renames the share at old name to new name. To avoid
|
||
|
// potential incompatibilities across file systems, the new share name is
|
||
|
// limited to alphanumeric characters and the underscore _.
|
||
|
// Any of the following will result in an error.
|
||
|
// - no share found under old name
|
||
|
// - new share name contains disallowed characters
|
||
|
// - share already exists under new name
|
||
|
func (b *LocalBackend) TailFSRenameShare(oldName, newName string) error {
|
||
|
var err error
|
||
|
newName, err = normalizeShareName(newName)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
b.mu.Lock()
|
||
|
shares, err := b.tailFSRenameShareLocked(oldName, newName)
|
||
|
b.mu.Unlock()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
b.tailFSNotifyShares(shares)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
8 months ago
|
func (b *LocalBackend) tailFSRenameShareLocked(oldName, newName string) (views.SliceView[*drive.Share, drive.ShareView], error) {
|
||
8 months ago
|
existingShares := b.pm.prefs.TailFSShares()
|
||
|
|
||
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
||
|
if !ok {
|
||
|
return existingShares, ErrTailFSNotEnabled
|
||
|
}
|
||
|
|
||
|
found := false
|
||
8 months ago
|
var shares []*drive.Share
|
||
8 months ago
|
for i := 0; i < existingShares.Len(); i++ {
|
||
|
existing := existingShares.At(i)
|
||
|
if existing.Name() == newName {
|
||
|
return existingShares, os.ErrExist
|
||
|
}
|
||
|
if existing.Name() == oldName {
|
||
|
share := existing.AsStruct()
|
||
|
share.Name = newName
|
||
|
shares = append(shares, share)
|
||
|
found = true
|
||
|
} else {
|
||
|
shares = append(shares, existing.AsStruct())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if !found {
|
||
|
return existingShares, os.ErrNotExist
|
||
|
}
|
||
|
|
||
8 months ago
|
slices.SortFunc(shares, drive.CompareShares)
|
||
8 months ago
|
err := b.tailFSSetSharesLocked(shares)
|
||
|
if err != nil {
|
||
|
return existingShares, err
|
||
|
}
|
||
|
fs.SetShares(shares)
|
||
|
|
||
|
return b.pm.prefs.TailFSShares(), nil
|
||
|
}
|
||
|
|
||
9 months ago
|
// TailFSRemoveShare removes the named share. Share names are forced to
|
||
10 months ago
|
// lowercase.
|
||
9 months ago
|
func (b *LocalBackend) TailFSRemoveShare(name string) error {
|
||
10 months ago
|
// Force all share names to lowercase to avoid potential incompatibilities
|
||
|
// with clients that don't support case-sensitive filenames.
|
||
9 months ago
|
var err error
|
||
|
name, err = normalizeShareName(name)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
10 months ago
|
|
||
|
b.mu.Lock()
|
||
9 months ago
|
shares, err := b.tailFSRemoveShareLocked(name)
|
||
10 months ago
|
b.mu.Unlock()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
9 months ago
|
b.tailFSNotifyShares(shares)
|
||
10 months ago
|
return nil
|
||
|
}
|
||
|
|
||
8 months ago
|
func (b *LocalBackend) tailFSRemoveShareLocked(name string) (views.SliceView[*drive.Share, drive.ShareView], error) {
|
||
9 months ago
|
existingShares := b.pm.prefs.TailFSShares()
|
||
|
|
||
9 months ago
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
||
|
if !ok {
|
||
8 months ago
|
return existingShares, ErrTailFSNotEnabled
|
||
10 months ago
|
}
|
||
|
|
||
8 months ago
|
found := false
|
||
8 months ago
|
var shares []*drive.Share
|
||
9 months ago
|
for i := 0; i < existingShares.Len(); i++ {
|
||
|
existing := existingShares.At(i)
|
||
|
if existing.Name() != name {
|
||
|
shares = append(shares, existing.AsStruct())
|
||
8 months ago
|
} else {
|
||
|
found = true
|
||
9 months ago
|
}
|
||
10 months ago
|
}
|
||
9 months ago
|
|
||
8 months ago
|
if !found {
|
||
|
return existingShares, os.ErrNotExist
|
||
|
}
|
||
|
|
||
9 months ago
|
err := b.tailFSSetSharesLocked(shares)
|
||
10 months ago
|
if err != nil {
|
||
9 months ago
|
return existingShares, err
|
||
10 months ago
|
}
|
||
9 months ago
|
fs.SetShares(shares)
|
||
10 months ago
|
|
||
9 months ago
|
return b.pm.prefs.TailFSShares(), nil
|
||
10 months ago
|
}
|
||
|
|
||
8 months ago
|
func (b *LocalBackend) tailFSSetSharesLocked(shares []*drive.Share) error {
|
||
9 months ago
|
prefs := b.pm.prefs.AsStruct()
|
||
|
prefs.ApplyEdits(&ipn.MaskedPrefs{
|
||
|
Prefs: ipn.Prefs{
|
||
|
TailFSShares: shares,
|
||
|
},
|
||
|
TailFSSharesSet: true,
|
||
|
})
|
||
|
return b.pm.setPrefsLocked(prefs.View())
|
||
|
}
|
||
|
|
||
|
// tailFSNotifyShares notifies IPN bus listeners (e.g. Mac Application process)
|
||
|
// about the latest list of shares.
|
||
8 months ago
|
func (b *LocalBackend) tailFSNotifyShares(shares views.SliceView[*drive.Share, drive.ShareView]) {
|
||
8 months ago
|
// Ensures shares is not nil to distinguish "no shares" from "not notifying shares"
|
||
|
if shares.IsNil() {
|
||
8 months ago
|
shares = views.SliceOfViews(make([]*drive.Share, 0))
|
||
8 months ago
|
}
|
||
9 months ago
|
b.send(ipn.Notify{TailFSShares: shares})
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
// tailFSNotifyCurrentSharesLocked sends an ipn.Notify if the current set of
|
||
|
// shares has changed since the last notification.
|
||
|
func (b *LocalBackend) tailFSNotifyCurrentSharesLocked() {
|
||
8 months ago
|
var shares views.SliceView[*drive.Share, drive.ShareView]
|
||
9 months ago
|
if b.tailFSSharingEnabledLocked() {
|
||
|
// Only populate shares if sharing is enabled.
|
||
|
shares = b.pm.prefs.TailFSShares()
|
||
|
}
|
||
|
|
||
|
lastNotified := b.lastNotifiedTailFSShares.Load()
|
||
|
if lastNotified == nil || !tailFSShareViewsEqual(lastNotified, shares) {
|
||
9 months ago
|
// Do the below on a goroutine to avoid deadlocking on b.mu in b.send().
|
||
9 months ago
|
go b.tailFSNotifyShares(shares)
|
||
|
}
|
||
10 months ago
|
}
|
||
|
|
||
8 months ago
|
func tailFSShareViewsEqual(a *views.SliceView[*drive.Share, drive.ShareView], b views.SliceView[*drive.Share, drive.ShareView]) bool {
|
||
9 months ago
|
if a == nil {
|
||
|
return false
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
if a.Len() != b.Len() {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for i := 0; i < a.Len(); i++ {
|
||
8 months ago
|
if !drive.ShareViewsEqual(a.At(i), b.At(i)) {
|
||
9 months ago
|
return false
|
||
|
}
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
return true
|
||
|
}
|
||
|
|
||
|
// TailFSGetShares() gets the current list of TailFS shares, sorted by name.
|
||
8 months ago
|
func (b *LocalBackend) TailFSGetShares() views.SliceView[*drive.Share, drive.ShareView] {
|
||
9 months ago
|
b.mu.Lock()
|
||
|
defer b.mu.Unlock()
|
||
|
|
||
|
return b.pm.prefs.TailFSShares()
|
||
10 months ago
|
}
|
||
|
|
||
9 months ago
|
// updateTailFSPeersLocked sets all applicable peers from the netmap as tailfs
|
||
10 months ago
|
// remotes.
|
||
9 months ago
|
func (b *LocalBackend) updateTailFSPeersLocked(nm *netmap.NetworkMap) {
|
||
|
fs, ok := b.sys.TailFSForLocal.GetOK()
|
||
10 months ago
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
|
||
8 months ago
|
var tailFSRemotes []*drive.Remote
|
||
9 months ago
|
if b.tailFSAccessEnabledLocked() {
|
||
|
// Only populate peers if access is enabled, otherwise leave blank.
|
||
|
tailFSRemotes = b.tailFSRemotesFromPeers(nm)
|
||
|
}
|
||
|
|
||
|
fs.SetRemotes(b.netMap.Domain, tailFSRemotes, &tailFSTransport{b: b})
|
||
|
}
|
||
|
|
||
8 months ago
|
func (b *LocalBackend) tailFSRemotesFromPeers(nm *netmap.NetworkMap) []*drive.Remote {
|
||
|
tailFSRemotes := make([]*drive.Remote, 0, len(nm.Peers))
|
||
10 months ago
|
for _, p := range nm.Peers {
|
||
9 months ago
|
// Exclude mullvad exit nodes from list of TailFS peers
|
||
|
// TODO(oxtoacart) - once we have a better mechanism for finding only accessible sharers
|
||
|
// (see below) we can remove this logic.
|
||
|
if strings.HasSuffix(p.Name(), ".mullvad.ts.net.") {
|
||
|
continue
|
||
|
}
|
||
|
|
||
10 months ago
|
peerID := p.ID()
|
||
9 months ago
|
url := fmt.Sprintf("%s/%s", peerAPIBase(nm, p), tailFSPrefix[1:])
|
||
8 months ago
|
tailFSRemotes = append(tailFSRemotes, &drive.Remote{
|
||
10 months ago
|
Name: p.DisplayName(false),
|
||
|
URL: url,
|
||
|
Available: func() bool {
|
||
|
// TODO(oxtoacart): need to figure out a performant and reliable way to only
|
||
|
// show the peers that have shares to which we have access
|
||
|
// This will require work on the control server to transmit the inverse
|
||
|
// of the "tailscale.com/cap/tailfs" capability.
|
||
|
// For now, at least limit it only to nodes that are online.
|
||
|
// Note, we have to iterate the latest netmap because the peer we got from the first iteration may not be it
|
||
|
b.mu.Lock()
|
||
|
latestNetMap := b.netMap
|
||
|
b.mu.Unlock()
|
||
|
|
||
|
for _, candidate := range latestNetMap.Peers {
|
||
|
if candidate.ID() == peerID {
|
||
|
online := candidate.Online()
|
||
|
// TODO(oxtoacart): for some reason, this correctly
|
||
|
// catches when a node goes from offline to online,
|
||
|
// but not the other way around...
|
||
|
return online != nil && *online
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// peer not found, must not be available
|
||
|
return false
|
||
|
},
|
||
|
})
|
||
|
}
|
||
9 months ago
|
return tailFSRemotes
|
||
10 months ago
|
}
|