ipn/ipnlocal: add file sharing to windows shell

Updates: tailscale/winmin#33

Signed-off-by: Aleksandar Pesic <peske.nis@gmail.com>
This commit is contained in:
Aleksandar Pesic
2021-04-22 09:25:00 +02:00
committed by Brad Fitzpatrick
parent e41075dd4a
commit 7c985e4944
7 changed files with 160 additions and 33 deletions

View File

@@ -46,6 +46,7 @@ import (
"tailscale.com/types/persist"
"tailscale.com/types/wgkey"
"tailscale.com/util/dnsname"
"tailscale.com/util/osshare"
"tailscale.com/util/systemd"
"tailscale.com/version"
"tailscale.com/wgengine"
@@ -105,6 +106,7 @@ type LocalBackend struct {
inServerMode bool
machinePrivKey wgkey.Private
state ipn.State
capFileSharing bool // whether netMap contains the file sharing capability
// hostinfo is mutated in-place while mu is held.
hostinfo *tailcfg.Hostinfo
// netMap is not mutated in-place once set.
@@ -145,6 +147,8 @@ func NewLocalBackend(logf logger.Logf, logid string, store ipn.StateStore, e wge
panic("ipn.NewLocalBackend: wgengine must not be nil")
}
osshare.SetFileSharingEnabled(false, logf)
// Default filter blocks everything and logs nothing, until Start() is called.
e.SetFilter(filter.NewAllowNone(logf, &netaddr.IPSet{}))
@@ -2256,6 +2260,17 @@ func (b *LocalBackend) setNetInfo(ni *tailcfg.NetInfo) {
cc.SetNetInfo(ni)
}
func hasCapability(nm *netmap.NetworkMap, cap string) bool {
if nm != nil && nm.SelfNode != nil {
for _, c := range nm.SelfNode.Capabilities {
if c == cap {
return true
}
}
}
return false
}
func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) {
var login string
if nm != nil {
@@ -2270,6 +2285,13 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) {
b.activeLogin = login
}
// Determine if file sharing is enabled
fs := hasCapability(nm, tailcfg.CapabilityFileSharing)
if fs != b.capFileSharing {
osshare.SetFileSharingEnabled(fs, b.logf)
}
b.capFileSharing = fs
if nm == nil {
b.nodeByAddr = nil
return
@@ -2378,20 +2400,7 @@ func (b *LocalBackend) OpenFile(name string) (rc io.ReadCloser, size int64, err
func (b *LocalBackend) hasCapFileSharing() bool {
b.mu.Lock()
defer b.mu.Unlock()
return b.hasCapFileSharingLocked()
}
func (b *LocalBackend) hasCapFileSharingLocked() bool {
nm := b.netMap
if nm == nil || nm.SelfNode == nil {
return false
}
for _, c := range nm.SelfNode.Capabilities {
if c == tailcfg.CapabilityFileSharing {
return true
}
}
return false
return b.capFileSharing
}
// FileTargets lists nodes that the current node can send files to.
@@ -2400,7 +2409,7 @@ func (b *LocalBackend) FileTargets() ([]*apitype.FileTarget, error) {
b.mu.Lock()
defer b.mu.Unlock()
if !b.hasCapFileSharingLocked() {
if !b.capFileSharing {
return nil, errors.New("file sharing not enabled by Tailscale admin")
}
nm := b.netMap

View File

@@ -19,7 +19,6 @@ import (
"testing"
"tailscale.com/tailcfg"
"tailscale.com/types/netmap"
)
type peerAPITestEnv struct {
@@ -391,18 +390,10 @@ func TestHandlePeerAPI(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var caps []string
if tt.capSharing {
caps = append(caps, tailcfg.CapabilityFileSharing)
}
var e peerAPITestEnv
lb := &LocalBackend{
netMap: &netmap.NetworkMap{
SelfNode: &tailcfg.Node{
Capabilities: caps,
},
},
logf: e.logf,
logf: e.logf,
capFileSharing: tt.capSharing,
}
e.ph = &peerAPIHandler{
isSelf: tt.isSelf,
@@ -446,12 +437,8 @@ func TestFileDeleteRace(t *testing.T) {
dir := t.TempDir()
ps := &peerAPIServer{
b: &LocalBackend{
logf: t.Logf,
netMap: &netmap.NetworkMap{
SelfNode: &tailcfg.Node{
Capabilities: []string{tailcfg.CapabilityFileSharing},
},
},
logf: t.Logf,
capFileSharing: true,
},
rootDir: dir,
}