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
clipboard.go · 5.7 KB · 222 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "log" |
| 9 | "os/exec" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "time" |
| 14 | "unicode/utf8" |
| 15 | |
| 16 | "github.com/pion/webrtc/v4" |
| 17 | |
| 18 | "github.com/askrobots/network-computer/internal/proto" |
| 19 | ) |
| 20 | |
| 21 | // Clipboard sync, text only, on the reliable "control" channel: |
| 22 | // - client to desk: {"t":"clip","text":...} pieces, then the paste keystroke. |
| 23 | // Messages on one channel are handled in order, so the desk clipboard is |
| 24 | // set before the keystroke arrives. |
| 25 | // - desk to client: the desk clipboard is watched while a client is |
| 26 | // connected and new text is sent the same way. {"t":"clipget"} (the client |
| 27 | // just pressed copy) watches closely for a second and answers |
| 28 | // {"t":"clipnone"} if nothing new was copied. {"t":"clipnow"} (a button) |
| 29 | // asks for whatever the desk clipboard holds, new or not. |
| 30 | const ( |
| 31 | clipPiece = 8 << 10 // bytes of text per message, well under SCTP limits even when JSON-escaped |
| 32 | clipMax = 4 << 20 |
| 33 | clipIdle = 500 * time.Millisecond |
| 34 | clipFast = 50 * time.Millisecond |
| 35 | clipWait = time.Second |
| 36 | ) |
| 37 | |
| 38 | type clipSync struct { |
| 39 | mu sync.Mutex |
| 40 | last string // what the desk clipboard holds as far as both sides know |
| 41 | known bool // last has been set, by the first look or by the client |
| 42 | dc *webrtc.DataChannel |
| 43 | out sync.Mutex // one text's pieces at a time |
| 44 | in strings.Builder |
| 45 | poke chan struct{} |
| 46 | } |
| 47 | |
| 48 | func newClipSync() *clipSync { return &clipSync{poke: make(chan struct{}, 1)} } |
| 49 | |
| 50 | // receive handles a piece of client clipboard text; on the last piece it sets |
| 51 | // the desk clipboard. |
| 52 | func (c *clipSync) receive(ev proto.InputEvent) { |
| 53 | if c.in.Len()+len(ev.Text) > clipMax { |
| 54 | log.Printf("clipboard: client text over %d bytes, dropped", clipMax) |
| 55 | c.in.Reset() |
| 56 | return |
| 57 | } |
| 58 | c.in.WriteString(ev.Text) |
| 59 | if ev.More { |
| 60 | return |
| 61 | } |
| 62 | text := c.in.String() |
| 63 | c.in.Reset() |
| 64 | c.mu.Lock() |
| 65 | c.last, c.known = text, true |
| 66 | c.mu.Unlock() |
| 67 | if err := clipWrite(text); err != nil { |
| 68 | log.Printf("clipboard: set: %v", err) |
| 69 | return |
| 70 | } |
| 71 | log.Printf("clipboard: client pasted %d bytes", len(text)) |
| 72 | } |
| 73 | |
| 74 | // get is the client asking for what the user is about to copy. |
| 75 | func (c *clipSync) get() { |
| 76 | select { |
| 77 | case c.poke <- struct{}{}: |
| 78 | default: |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // now sends the desk clipboard as it is, for the client's "get" button. |
| 83 | func (c *clipSync) now() { |
| 84 | t, err := clipRead() |
| 85 | if err != nil || t == "" || len(t) > clipMax { |
| 86 | c.sendEvent(proto.InputEvent{T: "clipnone"}) |
| 87 | return |
| 88 | } |
| 89 | c.mu.Lock() |
| 90 | c.last = t |
| 91 | c.mu.Unlock() |
| 92 | log.Printf("clipboard: client asked, sent %d bytes", len(t)) |
| 93 | c.send(t) |
| 94 | } |
| 95 | |
| 96 | func (c *clipSync) run(ctx context.Context) { |
| 97 | if !clipSupported() { |
| 98 | return |
| 99 | } |
| 100 | if t, err := clipRead(); err == nil { |
| 101 | c.mu.Lock() |
| 102 | if !c.known { // what was there before this session is not news (unless the client already pasted) |
| 103 | c.last, c.known = t, true |
| 104 | } |
| 105 | c.mu.Unlock() |
| 106 | } |
| 107 | var waitUntil time.Time |
| 108 | for { |
| 109 | every := clipIdle |
| 110 | if !waitUntil.IsZero() { |
| 111 | every = clipFast |
| 112 | } |
| 113 | select { |
| 114 | case <-ctx.Done(): |
| 115 | return |
| 116 | case <-c.poke: |
| 117 | waitUntil = time.Now().Add(clipWait) |
| 118 | continue |
| 119 | case <-time.After(every): |
| 120 | } |
| 121 | t, err := clipRead() |
| 122 | c.mu.Lock() |
| 123 | changed := err == nil && t != "" && t != c.last && len(t) <= clipMax |
| 124 | if changed { |
| 125 | c.last = t |
| 126 | } |
| 127 | c.mu.Unlock() |
| 128 | switch { |
| 129 | case changed: |
| 130 | log.Printf("clipboard: desk copied %d bytes, sent to the client", len(t)) |
| 131 | c.send(t) |
| 132 | waitUntil = time.Time{} |
| 133 | case !waitUntil.IsZero() && time.Now().After(waitUntil): |
| 134 | c.sendEvent(proto.InputEvent{T: "clipnone"}) |
| 135 | waitUntil = time.Time{} |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func (c *clipSync) send(text string) { |
| 141 | c.out.Lock() |
| 142 | defer c.out.Unlock() |
| 143 | for { |
| 144 | n := len(text) |
| 145 | if n > clipPiece { |
| 146 | n = clipPiece |
| 147 | for n > 0 && !utf8.RuneStart(text[n]) { |
| 148 | n-- |
| 149 | } |
| 150 | } |
| 151 | if !c.sendEvent(proto.InputEvent{T: "clip", Text: text[:n], More: n < len(text)}) { |
| 152 | return |
| 153 | } |
| 154 | text = text[n:] |
| 155 | if text == "" { |
| 156 | return |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func (c *clipSync) sendEvent(ev proto.InputEvent) bool { |
| 162 | b, _ := json.Marshal(ev) |
| 163 | if err := c.dc.SendText(string(b)); err != nil { |
| 164 | log.Printf("clipboard: send: %v", err) |
| 165 | return false |
| 166 | } |
| 167 | return true |
| 168 | } |
| 169 | |
| 170 | // The desk's clipboard. Linux uses xclip on the session's X display (the |
| 171 | // service sets DISPLAY); macOS uses pbcopy/pbpaste. Other hosts: not yet. |
| 172 | func clipSupported() bool { |
| 173 | switch runtime.GOOS { |
| 174 | case "linux": |
| 175 | _, err := exec.LookPath("xclip") |
| 176 | return err == nil |
| 177 | case "darwin": |
| 178 | return true |
| 179 | } |
| 180 | return false |
| 181 | } |
| 182 | |
| 183 | func clipRead() (string, error) { |
| 184 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 185 | defer cancel() |
| 186 | var cmd *exec.Cmd |
| 187 | switch runtime.GOOS { |
| 188 | case "linux": |
| 189 | cmd = exec.CommandContext(ctx, "xclip", "-selection", "clipboard", "-o", "-t", "UTF8_STRING") |
| 190 | case "darwin": |
| 191 | cmd = exec.CommandContext(ctx, "pbpaste", "-Prefer", "txt") |
| 192 | default: |
| 193 | return "", errors.New("no clipboard on " + runtime.GOOS) |
| 194 | } |
| 195 | out, err := cmd.Output() // fails when the clipboard holds no text (an image, or nothing) |
| 196 | return string(out), err |
| 197 | } |
| 198 | |
| 199 | func clipWrite(text string) error { |
| 200 | switch runtime.GOOS { |
| 201 | case "linux": |
| 202 | // xclip keeps running in the background to serve the text; its output |
| 203 | // must not be a pipe, or Run would wait for it to exit. |
| 204 | cmd := exec.Command("xclip", "-selection", "clipboard", "-i") |
| 205 | cmd.Stdin = strings.NewReader(text) |
| 206 | if err := cmd.Run(); err != nil { |
| 207 | return err |
| 208 | } |
| 209 | // it takes ownership after forking: wait until the desk sees the text |
| 210 | for deadline := time.Now().Add(500 * time.Millisecond); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { |
| 211 | if t, err := clipRead(); err == nil && t == text { |
| 212 | return nil |
| 213 | } |
| 214 | } |
| 215 | return errors.New("xclip did not take the clipboard") |
| 216 | case "darwin": |
| 217 | cmd := exec.Command("pbcopy") |
| 218 | cmd.Stdin = bytes.NewBufferString(text) |
| 219 | return cmd.Run() |
| 220 | } |
| 221 | return errors.New("no clipboard on " + runtime.GOOS) |
| 222 | } |