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): pair with a firewalled Windows that shows the code
baiyuze767018844f8c
diff --git a/client-core/internal/transport/lan/lan.go b/client-core/internal/transport/lan/lan.goindex 4f0e85e..82cd65d 100644--- a/client-core/internal/transport/lan/lan.go+++ b/client-core/internal/transport/lan/lan.go@@ -148,6 +148,10 @@ type Transport struct {pairing *pairingStateresponses map[string]cachedResponsewaiters map[string]*pairWaiter+ // seeking 是正在进行的 Pair 调用数:大于零时广播里带 s=1,+ // 显示配对码的一方据此先往本机发一个空包,见 found+ seeking int+ pokedAt map[string]time.Time// reach 是各设备可达状态的副本,由叶子锁保护:peers 层会在持有自己的锁时查询它,// 不能让它去等 t.mu(t.mu 持有期间可能回调 peers 层)@@ -182,6 +186,7 @@ func New(opts Options) *Transport {rate: make(map[netip.Addr]int),responses: make(map[string]cachedResponse),waiters: make(map[string]*pairWaiter),+ pokedAt: make(map[string]time.Time),reach: make(map[string]bool),}}@@ -632,6 +637,11 @@ func (t *Transport) tick() {delete(t.responses, k)}}+ for k, at := range t.pokedAt {+ if now.Sub(at) > instanceTTL {+ delete(t.pokedAt, k)+ }+ }for k, si := range t.instances {if now.Sub(si.seen) > instanceTTL {delete(t.instances, k)@@ -783,6 +793,9 @@ func (t *Transport) advertTextLocked() map[string]string {if t.pairing != nil {text["p"] = "1"}+ if t.seeking > 0 {+ text["s"] = "1"+ }return text}@@ -803,6 +816,19 @@ func (t *Transport) found(inst Instance) {fresh := !had || prev.inst.Addr != inst.Addr || prev.inst.Text["t"] != inst.Text["t"] ||now.Sub(prev.seen) > 30*time.Second+ // 本机在显示配对码,对方在找人配对:先往它发一个空包。本机若是没放行防火墙的 Windows,+ // 对方主动发来的配对请求会被挡住;本机先发过包,有状态的防火墙就会把它当回包放行+ // 同一个实例可能有好几个地址(多块网卡),每个地址都要敲到+ var poke netip.AddrPort+ pokeKey := inst.Name + "|" + inst.Addr.String()+ if t.pairing != nil && inst.Text["s"] == "1" && now.Sub(t.pokedAt[pokeKey]) > 2*time.Second {+ t.pokedAt[pokeKey] = now+ poke = inst.Addr+ }+ if fresh && (inst.Text["p"] == "1" || inst.Text["s"] == "1") {+ t.log.Debug("发现正在配对的实例", "实例", inst.Name, "地址", inst.Addr, "开放配对码", inst.Text["p"] == "1", "在找人配对", inst.Text["s"] == "1")+ }+var hello stringif id := t.tokens[inst.Text["t"]]; id != "" {st := t.peerLocked(id)@@ -819,6 +845,10 @@ func (t *Transport) found(inst Instance) {}t.mu.Unlock()+ if poke.IsValid() {+ t.log.Debug("向在找人配对的实例敲门", "地址", poke)+ _ = t.write(poke, []byte(magic))+ }if hello != "" {t.sendHello(hello, []netip.AddrPort{inst.Addr}, true, t.intervalFor(hello))}diff --git a/client-core/internal/transport/lan/lan_test.go b/client-core/internal/transport/lan/lan_test.goindex 6ebe39b..0dba366 100644--- a/client-core/internal/transport/lan/lan_test.go+++ b/client-core/internal/transport/lan/lan_test.go@@ -4,6 +4,7 @@ import ("context""crypto/ed25519""errors"+ netpkg "net""net/netip""path/filepath""sync"@@ -332,6 +333,74 @@ func TestPresenceTokenRotatesDaily(t *testing.T) {}}+// 显示配对码的一方看到有人在找人配对(s=1),先往它发一个空包,好让本机的防火墙放行对方的请求。+func TestShowerPokesSeekers(t *testing.T) {+ net := NewMemoryNetwork()+ shower := newNode(t, net)+ shower.t.OpenPairing("K7MP2X", time.Now().Add(time.Minute))++ seeker, err := netpkg.ListenUDP("udp4", &netpkg.UDPAddr{IP: netpkg.IPv4(127, 0, 0, 1)})+ if err != nil {+ t.Fatal(err)+ }+ defer seeker.Close()+ port := seeker.LocalAddr().(*netpkg.UDPAddr).Port+ d := net.Discovery()+ if err := d.Start(context.Background(), "seeker", port, map[string]string{"v": "1", "s": "1"}, func(Instance) {}); err != nil {+ t.Fatal(err)+ }+ defer d.Stop()++ _ = seeker.SetReadDeadline(time.Now().Add(3 * time.Second))+ buf := make([]byte, 64)+ n, _, err := seeker.ReadFromUDP(buf)+ if err != nil {+ t.Fatalf("没收到敲门的空包: %v", err)+ }+ if string(buf[:n]) != magic {+ t.Fatalf("收到的不是空包: %q", buf[:n])+ }+}++// 正在配对的一方在广播里带 s=1,配对结束后撤下。+func TestJoinerAdvertisesSeeking(t *testing.T) {+ net := NewMemoryNetwork()+ joiner := newNode(t, net)+ watcher := net.Discovery()+ seen := make(chan map[string]string, 16)+ if err := watcher.Start(context.Background(), "watcher", 1, map[string]string{"v": "1"}, func(i Instance) { seen <- i.Text }); err != nil {+ t.Fatal(err)+ }+ defer watcher.Stop()++ ctx, cancel := context.WithTimeout(context.Background(), 700*time.Millisecond)+ defer cancel()+ done := make(chan struct{})+ go func() { _, _ = joiner.t.Pair(ctx, "K7MP2X", nil); close(done) }()++ sawSeeking := false+ for !sawSeeking {+ select {+ case txt := <-seen:+ sawSeeking = txt["s"] == "1"+ case <-time.After(2 * time.Second):+ t.Fatal("配对时广播里没有 s=1")+ }+ }+ <-done+ deadline := time.After(2 * time.Second)+ for {+ select {+ case txt := <-seen:+ if txt["s"] == "" {+ return+ }+ case <-deadline:+ t.Fatal("配对结束后 s=1 没有撤下")+ }+ }+}+// 对方重启后带着新的启动标识打招呼:本端要知道旧连接已经失效。func TestPeerRestartIsNoticed(t *testing.T) {net := NewMemoryNetwork()diff --git a/client-core/internal/transport/lan/pairing.go b/client-core/internal/transport/lan/pairing.goindex 98ff19b..3ff6e49 100644--- a/client-core/internal/transport/lan/pairing.go+++ b/client-core/internal/transport/lan/pairing.go@@ -99,17 +99,25 @@ func (t *Transport) Pair(ctx context.Context, code string, extra []netip.AddrPorw := &pairWaiter{resp: make(chan pairResult, 1), rejects: make(map[string]bool)}t.mu.Lock()t.waiters[key] = w+ t.seeking++t.mu.Unlock()+ t.updateAdvert() // 广播里带上 s=1,显示码的一方会先往本机发包,见 founddefer func() {t.mu.Lock()delete(t.waiters, key)+ t.seeking--t.mu.Unlock()+ t.updateAdvert()}()tk := time.NewTicker(500 * time.Millisecond)defer tk.Stop()- for {- for _, dst := range t.pairTargets(extra) {+ for round := 0; ; round++ {+ targets := t.pairTargets(extra)+ if round%4 == 0 {+ t.log.Debug("发送配对请求", "目标", targets)+ }+ for _, dst := range targets {_ = t.write(dst, pkt)}select {diff --git a/proto/copysync/v1/wire.proto b/proto/copysync/v1/wire.protoindex 50f36d4..98af059 100644--- a/proto/copysync/v1/wire.proto+++ b/proto/copysync/v1/wire.proto@@ -169,6 +169,8 @@ message ServerError {// 每个包前面加 4 字节魔数 "CSL1"。设备靠 mDNS(_copysync._udp)互相发现。//// 已配对设备之间发的是与服务器转发时完全相同的 Signed,信任模型不变。+// 只有魔数、没有内容的空包是「敲门」:显示配对码的一方看到有人在找人配对(mDNS 里带 s=1),+// 先往对方发一个,让本机的有状态防火墙放行对方随后的配对请求。收到的一方直接忽略。// 配对请求与回复各自用发送方自带的公钥签名:这只能证明发送方持有那把私钥,// 防中间人仍然靠双方肉眼核对指纹——与经服务器配对时一样。