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): find the LAN address for Send Files from the route to the peer
baiyuze0dcda1272edd
diff --git a/client-core/cmd/pasteferryd/send_lan.go b/client-core/cmd/pasteferryd/send_lan.goindex 431ab68..5a226a9 100644--- a/client-core/cmd/pasteferryd/send_lan.go+++ b/client-core/cmd/pasteferryd/send_lan.go@@ -25,10 +25,12 @@ func lanHooks(id *identity.Identity, peerMgr *peers.Manager) (listenLANFunc, dia}listen := func(deviceID string) (string, func(context.Context) (io.ReadCloser, string, error), bool) {if !peerMgr.HasFeature(deviceID, peers.FeatureSendFilesTCP) {+ slog.Info("对方不支持局域网快速通道,用数据通道", "device", deviceID)return "", nil, false}ip, ok := peerMgr.P2P().LANAddr(deviceID)if !ok {+ slog.Info("不是局域网直连,用数据通道", "device", deviceID)return "", nil, false}ln, err := link.Listen(ip, deviceID)diff --git a/client-core/internal/transport/p2p/lanaddr_test.go b/client-core/internal/transport/p2p/lanaddr_test.gonew file mode 100644index 0000000..c85afa4--- /dev/null+++ b/client-core/internal/transport/p2p/lanaddr_test.go@@ -0,0 +1,31 @@+package p2p++import (+ "net"+ "testing"+)++// 本机两端直连(host 对 host):LANAddr 给出一个本机网卡上的真实地址,能在上面监听+func TestLANAddrOnDirectConnection(t *testing.T) {+ alice, bob := connectPair(t)+ for _, c := range []struct {+ n *testNode+ peer string+ }{{alice, "bob"}, {bob, "alice"}} {+ addr, ok := c.n.m.LANAddr(c.peer)+ if !ok {+ t.Fatalf("对 %s 应当是局域网直连", c.peer)+ }+ if addr.IsLoopback() || addr.IsUnspecified() {+ t.Fatalf("地址不能是 %v", addr)+ }+ ln, err := net.Listen("tcp", net.JoinHostPort(addr.String(), "0"))+ if err != nil {+ t.Fatalf("在 %v 上监听: %v", addr, err)+ }+ ln.Close()+ }+ if _, ok := alice.m.LANAddr("nobody"); ok {+ t.Fatal("没有连接的设备不该有地址")+ }+}diff --git a/client-core/internal/transport/p2p/peer.go b/client-core/internal/transport/p2p/peer.goindex 0ee8e9f..d43015e 100644--- a/client-core/internal/transport/p2p/peer.go+++ b/client-core/internal/transport/p2p/peer.go@@ -11,6 +11,7 @@ import ("errors""fmt""log/slog"+ "net""net/netip""strings""sync"@@ -223,6 +224,9 @@ func (p *Peer) State() ConnState {}// LANAddr 见 Manager.LANAddr。对端是 prflx 也算:局域网里对端的地址常常先从收到的包里认出来。+//+// 本端地址不取 candidate 对里记的那个:多条连接共用一个 UDP 端口时,它可能是监听地址(实测出现过 127.0.0.1)。+// 改为问系统发往对端地址时用哪个源地址,这正是对端连得到的那个。func (p *Peer) LANAddr() (netip.Addr, bool) {p.mu.Lock()pc := p.pc@@ -236,16 +240,39 @@ func (p *Peer) LANAddr() (netip.Addr, bool) {return netip.Addr{}, false}pair, err := sctp.Transport().ICETransport().GetSelectedCandidatePair()- if err != nil || pair == nil || pair.Local == nil || pair.Remote == nil ||- pair.Local.Typ != webrtc.ICECandidateTypeHost ||+ if err != nil || pair == nil || pair.Local == nil || pair.Remote == nil {+ return netip.Addr{}, false+ }+ if pair.Local.Typ != webrtc.ICECandidateTypeHost ||(pair.Remote.Typ != webrtc.ICECandidateTypeHost && pair.Remote.Typ != webrtc.ICECandidateTypePrflx) {+ p.log.Info("不是局域网直连,隔空传送不走快速通道", "local", pair.Local.Typ.String(), "remote", pair.Remote.Typ.String())return netip.Addr{}, false}- addr, err := netip.ParseAddr(pair.Local.Address)- if err != nil || addr.IsLoopback() || addr.IsUnspecified() {+ remote, err := netip.ParseAddr(pair.Remote.Address)+ if err != nil || remote.IsLoopback() || remote.IsUnspecified() {+ p.log.Info("对端地址不能用来开快速通道", "remote", pair.Remote.Address)+ return netip.Addr{}, false+ }+ local, err := sourceAddrFor(remote.Unmap())+ if err != nil {+ p.log.Info("找不到发往对端用的本机地址", "remote", remote, "err", err)return netip.Addr{}, false}- return addr.Unmap(), true+ return local, true+}++// sourceAddrFor 返回系统发往 dst 时用的本机地址。UDP 的 connect 只查路由,不发包。+func sourceAddrFor(dst netip.Addr) (netip.Addr, error) {+ c, err := net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(netip.AddrPortFrom(dst, 9)))+ if err != nil {+ return netip.Addr{}, err+ }+ defer c.Close()+ local := c.LocalAddr().(*net.UDPAddr).AddrPort().Addr().Unmap()+ if !local.IsValid() || local.IsLoopback() || local.IsUnspecified() {+ return netip.Addr{}, errors.New("没有可用的本机地址")+ }+ return local, nil}func (p *Peer) DirectAttemptReady() bool {