PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.
Dart airdrop-alternativeclipboardclipboard-sharingclipboard-synccopy-pastecross-platformend-to-end-encryptionfile-syncfile-transferfluttergolanglinuxmacosnat-traversalp2pself-hostedtailscaleuniversal-clipboardwebrtcwindows
fix(core): record a copy once when an app writes the clipboard twice
baiyuze3a201d9be1fc
diff --git a/client-core/internal/clipboard/watcher.go b/client-core/internal/clipboard/watcher.goindex 50eaa17..68df916 100644--- a/client-core/internal/clipboard/watcher.go+++ b/client-core/internal/clipboard/watcher.go@@ -80,11 +80,40 @@ func (w *Watcher) Run(ctx context.Context) {case <-ticker.C:w.poll(ctx)case <-notify:+ settle(ctx, notify, settleDelay, settleMax)w.poll(ctx)}}}+// 系统通知剪贴板变化后,等通知停下来再读。有的程序一次复制会分几次写剪贴板+// (PowerShell 的 Set-Clipboard、先放 OLE 数据对象再 OleFlushClipboard 的程序),+// 几毫秒内连着来好几个通知;每个都读一次的话,一次复制会记成好几条。+const (+ settleDelay = 100 * time.Millisecond+ settleMax = 500 * time.Millisecond+)++// settle 等到 delay 内没有新通知为止,最多等 max:一直有通知也不能一直不读。+func settle(ctx context.Context, notify <-chan struct{}, delay, max time.Duration) {+ limit := time.NewTimer(max)+ defer limit.Stop()+ quiet := time.NewTimer(delay)+ defer quiet.Stop()+ for {+ select {+ case <-ctx.Done():+ return+ case <-limit.C:+ return+ case <-quiet.C:+ return+ case <-notify:+ quiet.Reset(delay)+ }+ }+}+func (w *Watcher) poll(ctx context.Context) {// 给足超时:万一主线程正卡在授权弹窗上,这里要能干净退出而不是堆积ctx, cancel := context.WithTimeout(ctx, 5*time.Second)diff --git a/client-core/internal/clipboard/watcher_test.go b/client-core/internal/clipboard/watcher_test.goindex 8cdf815..78b920e 100644--- a/client-core/internal/clipboard/watcher_test.go+++ b/client-core/internal/clipboard/watcher_test.go@@ -4,6 +4,7 @@ import ("context""errors""testing"+ "time")type testClipboard struct {@@ -65,3 +66,45 @@ func TestWatcherConditionalWriteAndErrors(t *testing.T) {t.Fatalf("failed probe authorized a write: %v %v", ok, err)}}++func TestSettleWaitsForNotificationsToStop(t *testing.T) {+ notify := make(chan struct{}, 1)+ // 同一次复制连着来的几个通知:最后一个之后再等 delay+ go func() {+ for range 3 {+ notify <- struct{}{}+ time.Sleep(20 * time.Millisecond)+ }+ }()+ start := time.Now()+ settle(context.Background(), notify, 80*time.Millisecond, time.Second)+ if took := time.Since(start); took < 100*time.Millisecond || took > 600*time.Millisecond {+ t.Fatalf("settle took %v, want the notifications plus the 80ms delay", took)+ }+ select {+ case <-notify:+ t.Fatal("a notification was left over; settle should have drained it")+ default:+ }+}++func TestSettleGivesUpAfterMax(t *testing.T) {+ notify := make(chan struct{}, 1)+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()+ // 一直有通知+ go func() {+ for ctx.Err() == nil {+ select {+ case notify <- struct{}{}:+ default:+ }+ time.Sleep(10 * time.Millisecond)+ }+ }()+ start := time.Now()+ settle(ctx, notify, 50*time.Millisecond, 200*time.Millisecond)+ if took := time.Since(start); took < 190*time.Millisecond || took > 600*time.Millisecond {+ t.Fatalf("settle took %v, want it to stop at the 200ms limit", took)+ }+}diff --git a/client-core/internal/sync/engine.go b/client-core/internal/sync/engine.goindex cce3ab6..0a061f8 100644--- a/client-core/internal/sync/engine.go+++ b/client-core/internal/sync/engine.go@@ -90,6 +90,8 @@ type Engine struct {// 没送达对端、等连接恢复后补发的内容,见 backlog.gobacklog backlog+ // 上一次本机复制,用来认出同一次复制引起的重复变化,见 repeat.go+ recentCopy repeatGuard// 每次传输上一次上报进度的时间,用于节流,见 emitProgressprogressMu sync.Mutex@@ -175,6 +177,10 @@ func (e *Engine) HandleClipboardChange(ctx context.Context, snap clipboard.Snapse.log.Debug("已读取剪贴板内容","kind", content.Kind, "文本长度", len(content.Text),"HTML长度", len(content.HTML), "图片字节", len(content.Image), "文件", content.Files)+ if e.recentCopy.seen(content, time.Now()) {+ e.log.Debug("刚复制过同样的内容,跳过")+ return+ }clip, err := e.recordOutgoing(content, cfg)if errors.Is(err, errRemotePlaceholder) {diff --git a/client-core/internal/sync/repeat.go b/client-core/internal/sync/repeat.gonew file mode 100644index 0000000..a288e23--- /dev/null+++ b/client-core/internal/sync/repeat.go@@ -0,0 +1,42 @@+package sync++import (+ "crypto/sha256"+ "fmt"+ "sync"+ "time"++ "github.com/baiyuze/pasteferry/client-core/internal/clipboard"+)++// repeatWindow 内又出现在剪贴板上的同一份内容不再记一条、也不再发一遍。+// 有的程序一次复制会让剪贴板变好几次(PowerShell 的 Set-Clipboard、先放 OLE 数据对象再 OleFlushClipboard 的程序),+// 剪贴板监听会把挨得很近的通知合并(见 clipboard.Watcher),隔得远一点的在这里按内容认出来。+const repeatWindow = 2 * time.Second++// repeatGuard 记着上一次本机复制的内容摘要与时间。+type repeatGuard struct {+ mu sync.Mutex+ sum [32]byte+ at time.Time+}++// seen 记下这次复制,返回它是不是 repeatWindow 内刚出现过的同一份内容。+func (g *repeatGuard) seen(c clipboard.Content, now time.Time) bool {+ sum := contentDigest(c)+ g.mu.Lock()+ defer g.mu.Unlock()+ repeat := !g.at.IsZero() && sum == g.sum && now.Sub(g.at) < repeatWindow+ g.sum, g.at = sum, now+ return repeat+}++func contentDigest(c clipboard.Content) [32]byte {+ h := sha256.New()+ fmt.Fprintf(h, "%d\x00%d:%s\x00%d:%s\x00%d:", c.Kind, len(c.Text), c.Text, len(c.HTML), c.HTML, len(c.Image))+ h.Write(c.Image)+ for _, f := range c.Files {+ fmt.Fprintf(h, "\x00%d:%s", len(f), f)+ }+ return [32]byte(h.Sum(nil))+}diff --git a/client-core/internal/sync/repeat_test.go b/client-core/internal/sync/repeat_test.gonew file mode 100644index 0000000..f1aaba6--- /dev/null+++ b/client-core/internal/sync/repeat_test.go@@ -0,0 +1,43 @@+package sync++import (+ "testing"+ "time"++ "github.com/baiyuze/pasteferry/client-core/internal/clipboard"+)++func TestRepeatGuard(t *testing.T) {+ var g repeatGuard+ t0 := time.Unix(1_700_000_000, 0)+ text := clipboard.Content{Kind: clipboard.KindText, Text: "pnpm install"}++ if g.seen(text, t0) {+ t.Fatal("first copy reported as a repeat")+ }+ // 同一次复制让剪贴板变了两次+ if !g.seen(text, t0.Add(300*time.Millisecond)) {+ t.Fatal("same content right after was not a repeat")+ }+ // 过了一会儿再复制同样的内容:照常记录、同步+ if g.seen(text, t0.Add(300*time.Millisecond+repeatWindow)) {+ t.Fatal("same content after the window was a repeat")+ }++ // 内容不同就不算,哪怕只差格式或文件+ cases := []clipboard.Content{+ {Kind: clipboard.KindHTML, Text: "pnpm install", HTML: "<b>pnpm install</b>"},+ {Kind: clipboard.KindText, Text: "pnpm install "},+ {Kind: clipboard.KindFile, Files: []string{"/tmp/a"}},+ {Kind: clipboard.KindFile, Files: []string{"/tmp/a", "/tmp/b"}},+ {Kind: clipboard.KindImage, Image: []byte{1, 2, 3}},+ {Kind: clipboard.KindImage, Image: []byte{1, 2, 4}},+ }+ now := t0.Add(10 * time.Second)+ for i, c := range cases {+ now = now.Add(100 * time.Millisecond)+ if g.seen(c, now) {+ t.Fatalf("case %d reported as a repeat of the previous content", i)+ }+ }+}