ipn/localapi: add support for multipart POST to file-put

This allows sending multiple files via Taildrop in one request.
Progress is tracked via ipn.Notify.

Updates tailscale/corp#18202

Signed-off-by: Percy Wegmann <percy@tailscale.com>
This commit is contained in:
Percy Wegmann 2024-03-26 07:27:58 -05:00 committed by Percy Wegmann
parent bed818a978
commit 66e4d843c1
5 changed files with 22 additions and 27 deletions

View File

@ -102,7 +102,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
L github.com/google/nftables/expr from github.com/google/nftables+
L github.com/google/nftables/internal/parseexprfunc from github.com/google/nftables+
L github.com/google/nftables/xt from github.com/google/nftables/expr+
github.com/google/uuid from tailscale.com/clientupdate
github.com/google/uuid from tailscale.com/clientupdate+
github.com/gorilla/csrf from tailscale.com/client/web
github.com/gorilla/securecookie from github.com/gorilla/csrf
github.com/hdevalence/ed25519consensus from tailscale.com/clientupdate/distsign+
@ -376,6 +376,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
W 💣 tailscale.com/util/osdiag/internal/wsc from tailscale.com/util/osdiag
tailscale.com/util/osshare from tailscale.com/cmd/tailscaled+
tailscale.com/util/osuser from tailscale.com/ipn/localapi+
tailscale.com/util/progresstracking from tailscale.com/ipn/localapi
tailscale.com/util/race from tailscale.com/net/dns/resolver
tailscale.com/util/racebuild from tailscale.com/logpolicy
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
@ -522,7 +523,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
math/rand from github.com/mdlayher/netlink+
math/rand/v2 from tailscale.com/util/rands
mime from github.com/tailscale/xnet/webdav+
mime/multipart from net/http
mime/multipart from net/http+
mime/quotedprintable from mime/multipart
net from crypto/tls+
net/http from expvar+

View File

@ -202,8 +202,8 @@ type PartialFile struct {
// OutgoingFile represents an in-progress outgoing file transfer.
type OutgoingFile struct {
ID string `json:"-"` // unique identifier for this transfer (a type 4 UUID)
PeerID tailcfg.StableNodeID // identifier for the peer to which this is being transferred
ID string `json:",omitempty"` // unique identifier for this transfer (a type 4 UUID)
PeerID tailcfg.StableNodeID `json:",omitempty"` // identifier for the peer to which this is being transferred
Name string `json:",omitempty"` // e.g. "foo.jpg"
Started time.Time // time transfer started
DeclaredSize int64 // or -1 if unknown

View File

@ -320,7 +320,7 @@ type LocalBackend struct {
// notified about.
lastNotifiedTailFSShares atomic.Pointer[views.SliceView[*tailfs.Share, tailfs.ShareView]]
// outgoingFiles keeps track of Taildrop outgoing files
// outgoingFiles keeps track of Taildrop outgoing files keyed to their OutgoingFile.ID
outgoingFiles map[string]*ipn.OutgoingFile
}

View File

@ -4,20 +4,21 @@
package ipnlocal
import (
"maps"
"slices"
"strings"
"tailscale.com/ipn"
)
func (b *LocalBackend) UpdateOutgoingFiles(updates map[string]ipn.OutgoingFile) {
// UpdateOutgoingFiles updates b.outgoingFiles to reflect the given updates and
// sends an ipn.Notify with the full list of outgoingFiles.
func (b *LocalBackend) UpdateOutgoingFiles(updates map[string]*ipn.OutgoingFile) {
b.mu.Lock()
if b.outgoingFiles == nil {
b.outgoingFiles = make(map[string]*ipn.OutgoingFile, len(updates))
}
for id, file := range updates {
b.outgoingFiles[id] = &file
}
maps.Copy(b.outgoingFiles, updates)
outgoingFiles := make([]*ipn.OutgoingFile, 0, len(b.outgoingFiles))
for _, file := range b.outgoingFiles {
outgoingFiles = append(outgoingFiles, file)

View File

@ -1535,11 +1535,10 @@ func (h *Handler) serveFileTargets(w http.ResponseWriter, r *http.Request) {
// directly, as the Windows GUI always runs in tun mode anyway.
//
// In addition to single file PUTs, this endpoint accepts multipart file
// POSTS encoded as multipart/form-data. Each part must include a
// "Content-Length" in the MIME header indicating the size of the file.
// The first part should be an application/json file that contains a JSON map
// of filename -> length, which we can use for tracking progress even before
// reading the file parts.
// POSTS encoded as multipart/form-data.The first part should be an
// application/json file that contains a manifest consisting of a JSON array of
// OutgoingFiles which wecan use for tracking progress even before reading the
// file parts.
//
// URL format:
//
@ -1599,9 +1598,9 @@ func (h *Handler) serveFilePut(w http.ResponseWriter, r *http.Request) {
return
}
// Report progress on outgoing files every 5 seconds
outgoingFiles := make(map[string]ipn.OutgoingFile)
t := time.NewTicker(5 * time.Second)
// Periodically report progress of outgoing files.
outgoingFiles := make(map[string]*ipn.OutgoingFile)
t := time.NewTicker(1 * time.Second)
progressUpdates := make(chan ipn.OutgoingFile)
defer close(progressUpdates)
@ -1614,7 +1613,7 @@ func (h *Handler) serveFilePut(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
outgoingFiles[u.ID] = u
outgoingFiles[u.ID] = &u
case <-t.C:
h.b.UpdateOutgoingFiles(outgoingFiles)
}
@ -1672,21 +1671,15 @@ func (h *Handler) multiFilePost(progressUpdates chan (ipn.OutgoingFile), w http.
return
}
var manifest map[string]int64
var manifest []ipn.OutgoingFile
err := json.NewDecoder(part).Decode(&manifest)
if err != nil {
http.Error(ww, fmt.Sprintf("invalid manifest: %s", err), http.StatusBadRequest)
return
}
for filename, size := range manifest {
file := ipn.OutgoingFile{
ID: uuid.Must(uuid.NewRandom()).String(),
Name: filename,
PeerID: peerID,
DeclaredSize: size,
}
outgoingFilesByName[filename] = file
for _, file := range manifest {
outgoingFilesByName[file.Name] = file
progressUpdates <- file
}