askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
files.go · 10.2 KB · 384 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "log" |
| 10 | "net" |
| 11 | "net/http" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | "sync" |
| 16 | "sync/atomic" |
| 17 | "time" |
| 18 | "unicode" |
| 19 | |
| 20 | "github.com/pion/webrtc/v4" |
| 21 | ) |
| 22 | |
| 23 | // File transfer, both ways, on reliable data channels labelled "file": a |
| 24 | // JSON header {"name","size"}, binary pieces, then the text "end"; the |
| 25 | // receiver answers "ok <name>" or "error: <why>". A channel may carry several |
| 26 | // files one after another (the Flutter app opens one up front: channels it |
| 27 | // creates later never report open there); the browser opens one per file. |
| 28 | // - client to desk: saved into -files-dir (the desk user's Desktop) under a |
| 29 | // temporary name until complete, owned by that directory's owner. |
| 30 | // - desk to client: nc-send (the file manager's "Send to my device") PUTs |
| 31 | // the file into the -send-socket unix socket; the host passes it to every |
| 32 | // connected client, which offers it as a download. |
| 33 | // - printing: the desk's "My device" printer (a CUPS backend, ncdevice) |
| 34 | // PUTs the job as a PDF with ?print=1; the header then says "print": true |
| 35 | // and the client opens its own print dialog with it (AirPrint on an |
| 36 | // iPhone or iPad). A client that does not know "print" just saves it. |
| 37 | const ( |
| 38 | filePiece = 16 << 10 |
| 39 | fileHighWater = 4 << 20 // bytes queued on a channel before waiting |
| 40 | fileReserve = 50 << 20 |
| 41 | ) |
| 42 | |
| 43 | type fileHeader struct { |
| 44 | Name string `json:"name"` |
| 45 | Size int64 `json:"size"` |
| 46 | Print bool `json:"print,omitempty"` |
| 47 | } |
| 48 | |
| 49 | // receiveFile saves the files a client sends on one channel, one at a time. |
| 50 | func receiveFile(peer string, dc *webrtc.DataChannel, dir string) { |
| 51 | var ( |
| 52 | mu sync.Mutex |
| 53 | hdr fileHeader |
| 54 | f *os.File |
| 55 | tmp, final string |
| 56 | got int64 |
| 57 | ) |
| 58 | finish := func(reply string) { // with mu held |
| 59 | if f != nil { |
| 60 | f.Close() |
| 61 | os.Remove(tmp) |
| 62 | f = nil |
| 63 | } |
| 64 | dc.SendText(reply) |
| 65 | if strings.HasPrefix(reply, "error") { |
| 66 | log.Printf("[%s] file %q: %s", peer, hdr.Name, reply) |
| 67 | } |
| 68 | } |
| 69 | dc.OnMessage(func(m webrtc.DataChannelMessage) { |
| 70 | mu.Lock() |
| 71 | defer mu.Unlock() |
| 72 | switch { |
| 73 | case f == nil && !m.IsString: |
| 74 | // pieces of a file already refused or finished: drop them |
| 75 | case f == nil && m.IsString && string(m.Data) == "end": |
| 76 | // the end of a refused file: already answered |
| 77 | case f == nil && m.IsString: |
| 78 | hdr, got = fileHeader{}, 0 |
| 79 | if err := json.Unmarshal(m.Data, &hdr); err != nil || hdr.Size < 0 { |
| 80 | finish("error: bad header") |
| 81 | return |
| 82 | } |
| 83 | name := safeName(hdr.Name) |
| 84 | switch { |
| 85 | case dir == "": |
| 86 | finish("error: this host does not accept files") |
| 87 | return |
| 88 | case name == "": |
| 89 | finish("error: bad file name") |
| 90 | return |
| 91 | } |
| 92 | if free, err := freeBytes(dir); err == nil && hdr.Size > free-fileReserve { |
| 93 | finish(fmt.Sprintf("error: not enough space on the desk (%s free)", humanBytes(free))) |
| 94 | return |
| 95 | } |
| 96 | final = uniquePath(dir, name) |
| 97 | tmp = filepath.Join(dir, "."+filepath.Base(final)+".part") |
| 98 | var err error |
| 99 | if f, err = os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644); err != nil { |
| 100 | f = nil |
| 101 | finish("error: " + err.Error()) |
| 102 | } |
| 103 | case f != nil && !m.IsString: |
| 104 | got += int64(len(m.Data)) |
| 105 | if got > hdr.Size { |
| 106 | finish("error: more data than announced") |
| 107 | return |
| 108 | } |
| 109 | if _, err := f.Write(m.Data); err != nil { |
| 110 | finish("error: " + err.Error()) |
| 111 | } |
| 112 | case f != nil && string(m.Data) == "end": |
| 113 | if got != hdr.Size { |
| 114 | finish(fmt.Sprintf("error: got %d of %d bytes", got, hdr.Size)) |
| 115 | return |
| 116 | } |
| 117 | err := f.Close() |
| 118 | f = nil |
| 119 | if err == nil { |
| 120 | chownLike(tmp, dir) |
| 121 | final = uniquePath(dir, filepath.Base(final)) // in case the name was taken meanwhile |
| 122 | err = os.Rename(tmp, final) |
| 123 | } |
| 124 | if err != nil { |
| 125 | os.Remove(tmp) |
| 126 | finish("error: " + err.Error()) |
| 127 | return |
| 128 | } |
| 129 | log.Printf("[%s] file saved: %s (%s)", peer, final, humanBytes(got)) |
| 130 | finish("ok " + filepath.Base(final)) |
| 131 | } |
| 132 | }) |
| 133 | dc.OnClose(func() { |
| 134 | mu.Lock() |
| 135 | defer mu.Unlock() |
| 136 | if f != nil { // the client went away mid-file |
| 137 | f.Close() |
| 138 | os.Remove(tmp) |
| 139 | f = nil |
| 140 | } |
| 141 | }) |
| 142 | } |
| 143 | |
| 144 | // fileOut sends files on one channel, one at a time, and reads the answers. |
| 145 | type fileOut struct { |
| 146 | dc *webrtc.DataChannel |
| 147 | mu sync.Mutex |
| 148 | reply chan string |
| 149 | low chan struct{} |
| 150 | } |
| 151 | |
| 152 | func newFileOut(dc *webrtc.DataChannel) *fileOut { |
| 153 | o := &fileOut{dc: dc, reply: make(chan string, 4), low: make(chan struct{}, 1)} |
| 154 | dc.SetBufferedAmountLowThreshold(fileHighWater / 4) |
| 155 | dc.OnBufferedAmountLow(func() { |
| 156 | select { |
| 157 | case o.low <- struct{}{}: |
| 158 | default: |
| 159 | } |
| 160 | }) |
| 161 | dc.OnMessage(func(m webrtc.DataChannelMessage) { |
| 162 | select { |
| 163 | case o.reply <- string(m.Data): |
| 164 | default: |
| 165 | } |
| 166 | }) |
| 167 | return o |
| 168 | } |
| 169 | |
| 170 | // sendFile streams a file to one client on a new channel (the browser's way). |
| 171 | func sendFile(ctx context.Context, pc *webrtc.PeerConnection, hdr fileHeader, r io.Reader) error { |
| 172 | dc, err := pc.CreateDataChannel("file", nil) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | defer dc.Close() |
| 177 | opened := make(chan struct{}) |
| 178 | dc.OnOpen(func() { close(opened) }) |
| 179 | o := newFileOut(dc) |
| 180 | select { |
| 181 | case <-opened: |
| 182 | case <-ctx.Done(): |
| 183 | return ctx.Err() |
| 184 | case <-time.After(10 * time.Second): |
| 185 | return errors.New("the device did not open a channel") |
| 186 | } |
| 187 | return o.send(ctx, hdr, r) |
| 188 | } |
| 189 | |
| 190 | // send streams one file and waits for the client's answer. |
| 191 | func (o *fileOut) send(ctx context.Context, hdr fileHeader, r io.Reader) error { |
| 192 | o.mu.Lock() |
| 193 | defer o.mu.Unlock() |
| 194 | for len(o.reply) > 0 { // answers left from an earlier file |
| 195 | <-o.reply |
| 196 | } |
| 197 | dc := o.dc |
| 198 | b, _ := json.Marshal(hdr) |
| 199 | if err := dc.SendText(string(b)); err != nil { |
| 200 | return err |
| 201 | } |
| 202 | buf := make([]byte, filePiece) |
| 203 | for { |
| 204 | for dc.BufferedAmount() > fileHighWater { |
| 205 | select { |
| 206 | case <-o.low: |
| 207 | case <-time.After(time.Second): |
| 208 | case <-ctx.Done(): |
| 209 | return ctx.Err() |
| 210 | case r := <-o.reply: // refused before the end |
| 211 | return errors.New(r) |
| 212 | } |
| 213 | } |
| 214 | n, err := r.Read(buf) |
| 215 | if n > 0 { |
| 216 | if err := dc.Send(buf[:n]); err != nil { |
| 217 | return err |
| 218 | } |
| 219 | } |
| 220 | if err == io.EOF { |
| 221 | break |
| 222 | } |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | } |
| 227 | if err := dc.SendText("end"); err != nil { |
| 228 | return err |
| 229 | } |
| 230 | select { |
| 231 | case r := <-o.reply: |
| 232 | if !strings.HasPrefix(r, "ok") { |
| 233 | return errors.New(r) |
| 234 | } |
| 235 | return nil |
| 236 | case <-ctx.Done(): |
| 237 | return ctx.Err() |
| 238 | case <-time.After(2 * time.Minute): // a client without file support never answers |
| 239 | return errors.New("the device did not confirm") |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // serveSend accepts files from nc-send on a unix socket and passes them to |
| 244 | // the connected clients. |
| 245 | func (h *host) serveSend(path string) { |
| 246 | os.Remove(path) |
| 247 | l, err := net.Listen("unix", path) |
| 248 | if err != nil { |
| 249 | log.Printf("send socket: %v", err) |
| 250 | return |
| 251 | } |
| 252 | // Only the desk user may use it (nc-send, nc-voice): group = the owner of |
| 253 | // the files folder, mode 0660. Anyone who can write here can push files to |
| 254 | // the connected devices and speak for the voice. |
| 255 | os.Chmod(path, 0o600) |
| 256 | if h.filesDir != "" { |
| 257 | if gid, ok := ownerGroup(h.filesDir); ok { |
| 258 | os.Chown(path, 0, gid) |
| 259 | os.Chmod(path, 0o660) |
| 260 | } |
| 261 | } |
| 262 | log.Printf("send socket: %s", path) |
| 263 | mux := http.NewServeMux() |
| 264 | mux.HandleFunc("/voice/commands", h.handleVoiceCommands) |
| 265 | mux.HandleFunc("/voice/event", h.handleVoiceEvent) |
| 266 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 267 | name := safeName(r.URL.Query().Get("name")) |
| 268 | if r.Method != http.MethodPut || name == "" { |
| 269 | http.Error(w, "PUT /send?name=FILE[&print=1] with the file as the body", http.StatusBadRequest) |
| 270 | return |
| 271 | } |
| 272 | tmp, err := os.CreateTemp("", "nc-send-*") |
| 273 | if err != nil { |
| 274 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 275 | return |
| 276 | } |
| 277 | defer os.Remove(tmp.Name()) |
| 278 | defer tmp.Close() |
| 279 | size, err := io.Copy(tmp, r.Body) |
| 280 | if err != nil { |
| 281 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 282 | return |
| 283 | } |
| 284 | hdr := fileHeader{Name: name, Size: size, Print: r.URL.Query().Get("print") == "1"} |
| 285 | var targets []*session |
| 286 | h.mu.Lock() |
| 287 | for _, s := range h.sessions { |
| 288 | if s.pc.ConnectionState() == webrtc.PeerConnectionStateConnected { |
| 289 | targets = append(targets, s) |
| 290 | } |
| 291 | } |
| 292 | h.mu.Unlock() |
| 293 | if len(targets) == 0 { |
| 294 | http.Error(w, "no device is connected", http.StatusServiceUnavailable) |
| 295 | return |
| 296 | } |
| 297 | ctx, cancel := context.WithTimeout(r.Context(), 30*time.Minute) |
| 298 | defer cancel() |
| 299 | var wg sync.WaitGroup |
| 300 | var ok atomic.Int32 |
| 301 | for _, s := range targets { // in parallel: one slow or old client must not hold up the rest |
| 302 | wg.Add(1) |
| 303 | go func() { |
| 304 | defer wg.Done() |
| 305 | body := io.NewSectionReader(tmp, 0, size) |
| 306 | var err error |
| 307 | if s.fileIn != nil { // the app's own channel, opened at connect |
| 308 | err = s.fileIn.send(ctx, hdr, body) |
| 309 | } else { |
| 310 | err = sendFile(ctx, s.pc, hdr, body) |
| 311 | } |
| 312 | if err != nil { |
| 313 | log.Printf("send %s: %v", name, err) |
| 314 | return |
| 315 | } |
| 316 | ok.Add(1) |
| 317 | }() |
| 318 | } |
| 319 | wg.Wait() |
| 320 | sent := int(ok.Load()) |
| 321 | if sent == 0 { |
| 322 | http.Error(w, "the device did not take it", http.StatusBadGateway) |
| 323 | return |
| 324 | } |
| 325 | verb := "sent" |
| 326 | if hdr.Print { |
| 327 | verb = "sent to print" |
| 328 | } |
| 329 | log.Printf("%s %s (%s) to %d device(s)", verb, name, humanBytes(size), sent) |
| 330 | fmt.Fprintf(w, "%s %s to %d device(s)\n", verb, name, sent) |
| 331 | }) |
| 332 | http.Serve(l, mux) |
| 333 | } |
| 334 | |
| 335 | // safeName keeps only the last element of a client-supplied name and drops |
| 336 | // anything that could escape the directory or hide the file. |
| 337 | func safeName(name string) string { |
| 338 | name = filepath.Base(strings.ReplaceAll(name, "\\", "/")) |
| 339 | name = strings.Map(func(r rune) rune { |
| 340 | if unicode.IsControl(r) || r == '/' { |
| 341 | return -1 |
| 342 | } |
| 343 | return r |
| 344 | }, name) |
| 345 | name = strings.TrimLeft(strings.TrimSpace(name), ".") |
| 346 | if len(name) > 200 { |
| 347 | ext := filepath.Ext(name) |
| 348 | if len(ext) > 20 { |
| 349 | ext = "" |
| 350 | } |
| 351 | name = name[:200-len(ext)] + ext |
| 352 | for !utf8ValidEnd(name) { |
| 353 | name = name[:len(name)-1] |
| 354 | } |
| 355 | } |
| 356 | return name |
| 357 | } |
| 358 | |
| 359 | func utf8ValidEnd(s string) bool { return strings.ToValidUTF8(s, "\x00") == s } |
| 360 | |
| 361 | // uniquePath returns dir/name, or dir/"name (2).ext" and so on if taken. |
| 362 | func uniquePath(dir, name string) string { |
| 363 | p := filepath.Join(dir, name) |
| 364 | ext := filepath.Ext(name) |
| 365 | stem := strings.TrimSuffix(name, ext) |
| 366 | for i := 2; ; i++ { |
| 367 | if _, err := os.Lstat(p); errors.Is(err, os.ErrNotExist) { |
| 368 | return p |
| 369 | } |
| 370 | p = filepath.Join(dir, fmt.Sprintf("%s (%d)%s", stem, i, ext)) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | func humanBytes(n int64) string { |
| 375 | switch { |
| 376 | case n >= 1<<30: |
| 377 | return fmt.Sprintf("%.1f GB", float64(n)/(1<<30)) |
| 378 | case n >= 1<<20: |
| 379 | return fmt.Sprintf("%.1f MB", float64(n)/(1<<20)) |
| 380 | case n >= 1<<10: |
| 381 | return fmt.Sprintf("%.0f KB", float64(n)/(1<<10)) |
| 382 | } |
| 383 | return fmt.Sprintf("%d bytes", n) |
| 384 | } |