Skip to content

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.

定好打洞时谁先发包,修复对端重启后只能中转,发布 1.1.2

baiyuze5261908faba9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f00d161..6c23af5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,18 @@
# 更新记录 / Changelog
+## 1.1.2 — 2026-10-09
+
+- **修复:家里那台一重启就只能中转,重试也换不回直连。**谁的包先到,谁的路由器就先收到陌生来源的包;
+ 实测公司路由器会为它占住端口,本机随后往外发只好换端口,打洞失败。对方重启、或 ICE 重启时,总是对方
+ 先拿到本机的地址、先发包。现在由规则决定谁先发包:一方先公布出口地址,另一方收到后先往那边发包再公布
+ 自己的;初次握手由发起方等,换直连的重试里两种顺序轮流用。
+ **Fixed: restarting the home Mac always fell back to the relay, and retries never got back to direct.** Some
+ routers lock a port when the other side's packet arrives first. A rule now decides who sends first, and
+ retries alternate the order.
+- NAT 打洞实验室新增两种「路由器会为外来的包占住端口」的场景,并给信令加上延迟,共 11 种。
+ The NAT lab adds two port-locking router scenarios with signaling delay (11 in total).
+- 协议:`SdpOffer` 新增 `answerer_waits` 字段。服务器不需要升级。 Protocol: `SdpOffer.answerer_waits`. No server upgrade needed.
+
## 1.1.1 — 2026-10-09
- **修复:对方断线后,复制的内容发不过去。**连接悄悄断了(对方睡眠、断网、重启),本端却以为还连着,
diff --git a/README.en.md b/README.en.md
index c82d45e..20ef112 100644
--- a/README.en.md
+++ b/README.en.md
@@ -162,7 +162,7 @@ flowchart LR
1. Devices connect to the signaling server over WebSocket and receive STUN and TURN addresses.
2. All connections share one local UDP port. The background service probes several servers from that port to learn its public address, one per uplink on networks with several (see the next section).
-3. During the handshake each side sends its LAN addresses, its public address on every uplink, and its TURN relay address; both sides try each other with WebRTC ICE at the same time.
+3. During the handshake each side sends its LAN addresses, its public address on every uplink, and its TURN relay address; both sides try each other with WebRTC ICE at the same time. One side holds back its uplink addresses until it has received the other's and sent packets to them, because some routers lock a port when the other side's packet arrives first (see the next section).
4. A direct path wins if it works; relay paths must wait 5 seconds before they can be selected.
5. Once connected, each side tells the other whether it is relayed, and both show "relay" if either is.
6. If the connection still lands on the relay, the side that started it runs an ICE restart while idle to punch again, and moves to the direct path if that works (first after 10 seconds, then at intervals growing to 15 minutes).
@@ -191,7 +191,7 @@ What 1.1 does:
A Mac behind that 4-uplink office network now connects directly to a home connection, about 3 seconds after reaching the server. The full design, measurements and validation are in [NAT traversal on multi-uplink networks](design/nat-traversal.md) (Chinese).
-The **NAT lab** (`tools/natlab`) builds real topologies with Linux network namespaces and iptables and runs CopySync's actual connection code through nine scenarios on every commit:
+The **NAT lab** (`tools/natlab`) builds real topologies with Linux network namespaces and iptables and runs CopySync's actual connection code through eleven scenarios on every commit:
| Scenario | Result |
|---|---|
@@ -203,6 +203,8 @@ The **NAT lab** (`tools/natlab`) builds real topologies with Linux network names
| Two uplinks ↔ two uplinks | Direct |
| Two uplinks ↔ NAT that randomizes ports | Relay |
| Blocked at first, network recovers later | Relay first, then direct |
+| Office router locks a port for unsolicited packets | Direct |
+| Home router locks a port for unsolicited packets | Relay, then direct after the first retry |
### Security model
@@ -242,6 +244,7 @@ Each of these was found by testing and has a regression test. The full investiga
- **pion opens a separate port per STUN server**, so the answers don't apply to each other. Adding STUN servers can't fix multi-uplink networks; all connections now share one port and CopySync probes it itself.
- **One side can't tell on its own whether a connection is relayed.** When one side sends through TURN, the other may just see an ordinary address; once one Mac showed "direct" and the other "relay". Both sides now tell each other after connecting.
- **pion never switches paths once it has picked one.** The relay handshake is fast, and if it wins that race the whole connection stays relayed. We saw exactly that after the peer restarted: its addresses arrived after the relay wait had expired. The relay wait is now 5 seconds, probe hostnames resolve in parallel (a probe round went from about 1.6 s to under 0.2 s), and relayed connections retry the direct path with an ICE restart.
+- **Who sends first matters.** Some routers (the office one we measured) keep a connection entry for a packet that arrives before you've sent anything to its source, locking the port; your own packets then leave from a different port and punching fails. That's why restarting the office Mac always connected directly while restarting the home Mac fell back to the relay: a freshly started Mac probes its uplinks first, so its addresses went out late and it ended up sending first. A rule now decides who sends first, and retries alternate the order.
- **A dead connection has to be cleaned up by hand.** When pion reports "failed" it doesn't close the connection; the control channel still looks open and whatever is written to it is lost, and the layer above thinks it's connected and never reconnects. In 1.1.0 this sent six files into a dead connection after the other Mac dropped off, and none arrived.
- **Public STUN domains can be poisoned by DNS**, resolving to `192.0.2.42` in our tests. Reserved ranges and proxy fake-IP ranges are filtered before probing.
- **Simulated routers need a firewall.** When both sides punch at once, a packet let through to the router itself leaves a Linux conntrack entry, the outgoing flow then gets a new port, and punching fails. Real routers drop such packets, and the lab does too.
@@ -268,7 +271,7 @@ During development:
```bash
cd client-core && go test ./... # service tests
-./tools/natlab/docker.sh # NAT lab: direct vs relay across 9 network topologies
+./tools/natlab/docker.sh # NAT lab: direct vs relay across 11 network topologies
cd ui && flutter test # app tests
./scripts/install-macos.sh # install the service from dist/ as a login item
./dist/copysync-cli status # service status
diff --git a/README.md b/README.md
index 83565d0..bc71ba6 100644
--- a/README.md
+++ b/README.md
@@ -160,7 +160,7 @@ flowchart LR
1. 设备通过 WebSocket 连上信令服务器,拿到 STUN 与 TURN 的地址。
2. 所有连接共用一个本地 UDP 端口。后台服务在这个端口上向多台服务器探测本机的公网地址,网络有几个出口就能拿到几个地址(见下一节)。
-3. 握手时把局域网地址、每个出口的公网地址、TURN 中转地址都发给对端,两边用 WebRTC 的 ICE 同时互相尝试。
+3. 握手时把局域网地址、每个出口的公网地址、TURN 中转地址都发给对端,两边用 WebRTC 的 ICE 同时互相尝试。其中一方先不公布出口地址,等收到对方的、先往那边发过包再公布:有的路由器会被对方先到的包占住端口(见下一节)。
4. 直连打通就走直连;中转路径至少等 5 秒才允许被选中,给直连留出时间。
5. 连上后两端互相告知自己是否经过中转,任一端经中转,两边都显示「中转」。
6. 若还是先落到了中转,发起连接的一方会在空闲时用 ICE 重启再打一次洞,打通就换成直连(10 秒后第一次,之后间隔逐步拉长到 15 分钟)。
@@ -189,7 +189,7 @@ flowchart LR
实测公司 4 个出口与家用宽带之间直连,连上服务器后约 3 秒建立。设计细节、测量过程与验证见 [多出口网络的 NAT 打洞](design/nat-traversal.md)。
-**NAT 打洞实验室**(`tools/natlab`)用 Linux 网络命名空间与 iptables 搭出真实拓扑,用 CopySync 真实的连接代码验证 9 种场景,每次提交都在 CI 里运行:
+**NAT 打洞实验室**(`tools/natlab`)用 Linux 网络命名空间与 iptables 搭出真实拓扑,用 CopySync 真实的连接代码验证 11 种场景,每次提交都在 CI 里运行:
| 场景 | 结果 |
|---|---|
@@ -201,6 +201,8 @@ flowchart LR
| 两个出口 ↔ 两个出口 | 直连 |
| 两个出口 ↔ 端口随机分配的 NAT | 中转 |
| 一开始打不通,之后网络恢复 | 先中转,随后换成直连 |
+| 公司路由器会为外来的包占住端口 | 直连 |
+| 家用路由器会为外来的包占住端口 | 先中转,第一次重试后直连 |
### 安全模型
@@ -240,6 +242,7 @@ flowchart LR
- **pion 为每台 STUN 服务器单独开端口**,问到的地址互相用不上。多出口网络里多配几台 STUN 也打不通,所以改为所有连接共用一个端口、由自己探测。
- **单靠一端判断不了是否中转。**一端经 TURN 发送时,另一端看到的可能只是个普通地址,曾出现一台显示直连、一台显示中转。现在连接就绪后两端互相告知。
- **pion 选定线路后就不再更换。**中转握手快,一旦赢了这场赛跑,整个连接期间都走中转。实测对端重启后的第一次握手,出口地址比中转的等待时限晚到,就这样一直中转。现在中转等待放宽到 5 秒、探测时并发解析域名(整轮从约 1.6 秒降到 0.2 秒以内),并在中转时用 ICE 重启再试直连。
+- **谁先发包很重要。**有的路由器(实测公司那台)收到对方先到的包,会留下一条连接记录、占住端口,本端随后往外发只好换端口,打洞就失败了。这解释了为什么本机重启时总能直连、家里那台一重启就中转:本机重启时出口要现探测,地址晚发出,本机反而先发包。现在由规则决定谁先发包,换直连的重试里两种顺序轮流用。
- **连接失效后要自己清理。**pion 报告「失败」后不会关闭连接,控制通道看上去还是打开的,写进去的消息石沉大海;上层以为连接还在,也不会重连。1.1.0 曾因此在对方断线后,把 6 个文件「发」进了一条死连接,对方一个也没收到。
- **公共 STUN 域名可能被 DNS 污染**,实测被解析到 `192.0.2.42`。这类保留地址和代理软件的 fake-ip 网段在探测前就过滤掉。
- **测试用的模拟路由器要有防火墙。**两端同时打洞时,对方的包若被放行到路由器本机,Linux 会为它留下一条连接记录,本端随后往外发时只好换端口,打洞就失败了。真实路由器直接丢弃这类包,实验室也照此配置。
@@ -266,7 +269,7 @@ flowchart LR
```bash
cd client-core && go test ./... # 后台服务的测试
-./tools/natlab/docker.sh # NAT 打洞实验室:9 种网络拓扑下的直连与中转
+./tools/natlab/docker.sh # NAT 打洞实验室:11 种网络拓扑下的直连与中转
cd ui && flutter test # 界面的测试
./scripts/install-macos.sh # 把 dist/ 里的后台服务装成登录项
./dist/copysync-cli status # 查看后台服务状态
diff --git a/client-core/cmd/natlab/main.go b/client-core/cmd/natlab/main.go
index e3b9ddc..6b5b5f8 100644
--- a/client-core/cmd/natlab/main.go
+++ b/client-core/cmd/natlab/main.go
@@ -61,6 +61,7 @@ func runInfra(args []string) error {
httpAddr := fs.String("http", "", "信令转发的 HTTP 监听地址")
stunIPs := fs.String("stun", "", "STUN 服务器的 IP,逗号分隔,每个 IP 各起一台")
turnIP := fs.String("turn", "", "TURN 中转的 IP")
+ delay := fs.Duration("delay", 0, "信令转发的延迟:现实里信令要经服务器绕一道,比两台设备之间的包慢")
_ = fs.Parse(args)
for _, ip := range splitList(*stunIPs) {
@@ -76,7 +77,7 @@ func runInfra(args []string) error {
return fmt.Errorf("启动 TURN: %w", err)
}
- relay := &signalRelay{queues: map[string]chan envelope{}}
+ relay := &signalRelay{queues: map[string]chan envelope{}, delay: *delay}
mux := http.NewServeMux()
mux.HandleFunc("/send", relay.send)
mux.HandleFunc("/recv", relay.recv)
@@ -121,12 +122,14 @@ func serveSTUN(ip string) error {
type envelope struct {
from string
body []byte
+ due time.Time // 到这个时间才交给收件人,见 signalRelay.delay
}
// signalRelay 是最简的信令转发:按收件人排队,收件人长轮询取走。
type signalRelay struct {
mu sync.Mutex
queues map[string]chan envelope
+ delay time.Duration
}
func (r *signalRelay) queue(id string) chan envelope {
@@ -143,12 +146,14 @@ func (r *signalRelay) queue(id string) chan envelope {
func (r *signalRelay) send(w http.ResponseWriter, req *http.Request) {
body, _ := io.ReadAll(req.Body)
q := req.URL.Query()
- r.queue(q.Get("to")) <- envelope{from: q.Get("from"), body: body}
+ r.queue(q.Get("to")) <- envelope{from: q.Get("from"), body: body, due: time.Now().Add(r.delay)}
}
func (r *signalRelay) recv(w http.ResponseWriter, req *http.Request) {
select {
case e := <-r.queue(req.URL.Query().Get("id")):
+ // 按入队顺序逐条等到期,消息的先后不变
+ time.Sleep(time.Until(e.due))
w.Header().Set("X-From", e.from)
_, _ = w.Write(e.body)
case <-time.After(20 * time.Second):
diff --git a/client-core/internal/transport/p2p/egress_integration_test.go b/client-core/internal/transport/p2p/egress_integration_test.go
index 022f636..8147343 100644
--- a/client-core/internal/transport/p2p/egress_integration_test.go
+++ b/client-core/internal/transport/p2p/egress_integration_test.go
@@ -3,6 +3,7 @@ package p2p_test
import (
"context"
"fmt"
+ "slices"
"strings"
"sync"
"testing"
@@ -85,14 +86,20 @@ func TestManagerAdvertisesProbedEgress(t *testing.T) {
waitConnected(t, alice, "bob")
waitConnected(t, bob, "alice")
- // 出口地址必须作为 srflx candidate 发给了对端
+ // 出口地址必须作为 srflx candidate 发给了对端。alice 是发起方,要等收到 bob 的出口地址、
+ // 先往那边发过包才公布自己的(见 Peer.sendExtraCandidates),回环上连通可能比这更早
want := fmt.Sprintf("127.0.0.1 %d typ srflx", egress.LocalPort)
- mu.Lock()
- defer mu.Unlock()
- for _, c := range sentByAlice {
- if strings.Contains(c, want) {
+ deadline = time.Now().Add(5 * time.Second)
+ for time.Now().Before(deadline) {
+ mu.Lock()
+ sent := slices.ContainsFunc(sentByAlice, func(c string) bool { return strings.Contains(c, want) })
+ mu.Unlock()
+ if sent {
return
}
+ time.Sleep(20 * time.Millisecond)
}
+ mu.Lock()
+ defer mu.Unlock()
t.Errorf("alice 没有把出口地址发给对端;发出的 candidate:\n%s", strings.Join(sentByAlice, "\n"))
}
diff --git a/client-core/internal/transport/p2p/peer.go b/client-core/internal/transport/p2p/peer.go
index b5aaa0b..1c5a1fc 100644
--- a/client-core/internal/transport/p2p/peer.go
+++ b/client-core/internal/transport/p2p/peer.go
@@ -66,6 +66,12 @@ const (
relayAcceptanceWait = 5 * time.Second
// restartTimeout 是 ICE 重启后等待恢复的最长时间,超时就重置连接
restartTimeout = 30 * time.Second
+ // remoteEgressWait 是「先等对端的出口地址、再发自己的」最多等多久,见 sendExtraCandidates。
+ // 要给对端探测出口留出时间(Manager.advertisedCandidates 最多等 4 秒,通常不到 1 秒),
+ // 又要短于中转的等待时限
+ remoteEgressWait = 3 * time.Second
+ // punchHeadStart:收到对端的出口地址后,等本端发往那里的包先出去,再公布自己的
+ punchHeadStart = 200 * time.Millisecond
)
var (
@@ -121,6 +127,9 @@ type Peer struct {
dialing bool
forceRelay bool
+ // remoteEgress 在收到本轮协商中对端的出口地址时关闭,见 sendExtraCandidates
+ remoteEgress chan struct{}
+ remoteEgressSeen bool
// 生成并发出 offer/answer 期间,新收集到的 candidate 先攒着,见 holdCandidates
holdCands bool
heldCands []*pb.IceCandidate
@@ -263,6 +272,7 @@ func (p *Peer) Connect(ctx context.Context) error {
p.holdCandidates()
defer p.releaseCandidates()
+ remoteEgress := p.expectRemoteEgress()
offer, err := pc.CreateOffer(nil)
if err != nil {
@@ -275,17 +285,21 @@ func (p *Peer) Connect(ctx context.Context) error {
p.setState(StateConnecting)
p.armConnectTimeout()
- if err := p.sendSignal(ctx, offerMessage(offer)); err != nil {
+ if err := p.sendSignal(ctx, offerMessage(offer, false)); err != nil {
return err
}
- go p.sendExtraCandidates()
+ // 初次握手由发起方等:应答方先公布出口地址,发起方先往那边发包
+ go p.sendExtraCandidates(remoteEgress)
return nil
}
// restartICE 在已连通的连接上做一次 ICE 重启:两端重新收集地址、重新做连通性检查,
// DTLS 与数据通道保持不变。重启期间发出的数据会暂时丢失,由 SCTP 重传补上,
// 所以只停顿、不断开。
-func (p *Peer) restartICE(ctx context.Context) error {
+//
+// offererWaits 决定这一轮谁先发包:为真时本端等对端的出口地址、先往那边发包再公布
+// 自己的;为假时请应答方这样等(见 wire.proto 的 SdpOffer.answerer_waits)。
+func (p *Peer) restartICE(ctx context.Context, offererWaits bool) error {
p.mu.Lock()
pc := p.pc
if pc == nil || p.makingOffer || pc.SignalingState() != webrtc.SignalingStateStable {
@@ -302,6 +316,7 @@ func (p *Peer) restartICE(ctx context.Context) error {
p.holdCandidates()
defer p.releaseCandidates()
+ remoteEgress := p.expectRemoteEgress()
// 先盯住:从 CreateOffer 起本端的 ICE 就已重启,之后任何一步失败都要能恢复
p.watchRestart(pc)
@@ -312,10 +327,13 @@ func (p *Peer) restartICE(ctx context.Context) error {
if err := pc.SetLocalDescription(offer); err != nil {
return fmt.Errorf("设置本地描述: %w", err)
}
- if err := p.sendSignal(ctx, offerMessage(offer)); err != nil {
+ if err := p.sendSignal(ctx, offerMessage(offer, !offererWaits)); err != nil {
return err
}
- go p.sendExtraCandidates()
+ if !offererWaits {
+ remoteEgress = nil
+ }
+ go p.sendExtraCandidates(remoteEgress)
return nil
}
@@ -337,13 +355,42 @@ func (p *Peer) watchRestart(pc *webrtc.PeerConnection) {
})
}
-func offerMessage(offer webrtc.SessionDescription) *pb.SignalMessage {
+func offerMessage(offer webrtc.SessionDescription, answererWaits bool) *pb.SignalMessage {
return &pb.SignalMessage{Payload: &pb.SignalMessage_Offer{Offer: &pb.SdpOffer{
Sdp: offer.SDP,
DtlsFingerprint: fingerprintFromSDP(offer.SDP),
+ AnswererWaits: answererWaits,
}}}
}
+// expectRemoteEgress 开始一轮协商时调用,返回的通道在收到对端这一轮的出口地址时关闭。
+func (p *Peer) expectRemoteEgress() <-chan struct{} {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ p.remoteEgress = make(chan struct{})
+ p.remoteEgressSeen = false
+ return p.remoteEgress
+}
+
+// noteRemoteCandidate 在加入一条对端 candidate 后调用:是出口地址就通知 sendExtraCandidates。
+func (p *Peer) noteRemoteCandidate(candidate string) {
+ if !isEgressCandidate(candidate) {
+ return
+ }
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ if p.remoteEgress != nil && !p.remoteEgressSeen {
+ p.remoteEgressSeen = true
+ close(p.remoteEgress)
+ }
+}
+
+// isEgressCandidate 判断是不是出口探测得到的地址(见 srflxCandidates):类型是 srflx,
+// 且不带 ufrag 扩展。pion 自己收集的 candidate 都带 ufrag。
+func isEgressCandidate(candidate string) bool {
+ return strings.Contains(candidate, " typ srflx") && candidateUfrag(candidate) == ""
+}
+
// holdCandidates 让新收集到的 candidate 先攒着,releaseCandidates 时再发。
//
// 生成 offer/answer 时 pion 就开始收集地址,candidate 可能抢在描述之前发出。
@@ -382,10 +429,23 @@ func (p *Peer) sendCandidate(c *pb.IceCandidate) {
// 出去,那个地址对不上,打洞就失败了。把每个出口的地址都告诉对端,对端逐个检查连通性,
// 总有一个与实际出口吻合。必须在 offer/answer 之后发:对端设好远端描述前收到的
// candidate 会先缓存起来。
-func (p *Peer) sendExtraCandidates() {
+//
+// remoteEgress 不为空时,先等对端的出口地址到达、本端往那里发的包出去之后再发。
+// 打洞时谁的包先到,谁的路由器就先收到「陌生来源」的包。有的路由器(实测公司那台)
+// 会为它留下一条连接记录,占住这个端口;本端随后往外发时源端口只好换掉,事先告诉
+// 对端的地址就失效了。本端先发包,路由器里就已有本端发起的记录,对端的包回来正好对上。
+func (p *Peer) sendExtraCandidates(remoteEgress <-chan struct{}) {
if p.extraCands == nil {
return
}
+ if remoteEgress != nil {
+ select {
+ case <-remoteEgress:
+ time.Sleep(punchHeadStart)
+ case <-time.After(remoteEgressWait):
+ p.log.Debug("没等到对端的出口地址,照常发出本端的")
+ }
+ }
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for _, c := range p.extraCands() {
@@ -483,6 +543,10 @@ func (p *Peer) handleOffer(ctx context.Context, offer *pb.SdpOffer) error {
p.mu.Lock()
p.expectFingerprint = offer.GetDtlsFingerprint()
p.mu.Unlock()
+ remoteEgress := p.expectRemoteEgress()
+ if !offer.GetAnswererWaits() {
+ remoteEgress = nil
+ }
if restart {
// 设置远端描述时本端的 ICE 随之重启,从这里起就要盯住
@@ -517,7 +581,7 @@ func (p *Peer) handleOffer(ctx context.Context, offer *pb.SdpOffer) error {
}); err != nil {
return err
}
- go p.sendExtraCandidates()
+ go p.sendExtraCandidates(remoteEgress)
return nil
}
@@ -568,7 +632,11 @@ func (p *Peer) handleCandidate(c *pb.IceCandidate) error {
}
p.mu.Unlock()
- return pc.AddICECandidate(init)
+ if err := pc.AddICECandidate(init); err != nil {
+ return err
+ }
+ p.noteRemoteCandidate(init.Candidate)
+ return nil
}
// candidateUfrag 取出 candidate 里的 ufrag 扩展。pion 生成的 candidate 都带,
@@ -609,7 +677,9 @@ func (p *Peer) flushPendingCandidates() {
for _, c := range pending {
if err := pc.AddICECandidate(c); err != nil {
p.log.Debug("补加 candidate 失败", "err", err)
+ continue
}
+ p.noteRemoteCandidate(c.Candidate)
}
}
@@ -819,7 +889,10 @@ func (p *Peer) tryUpgrade() {
p.upgradeAttempt++
attempt := p.upgradeAttempt
p.mu.Unlock()
- p.log.Info("当前经中转,尝试换成直连", "attempt", attempt)
+ // 谁先发包轮流来:初次握手是本端(发起方)先发,没打通的话第一次重试换对端先发,
+ // 再下一次换回来。哪一边的路由器会被对方先到的包占住端口,总有一种顺序能避开
+ offererWaits := attempt%2 == 0
+ p.log.Info("当前经中转,尝试换成直连", "attempt", attempt, "本端先发包", offererWaits)
// 先重新探测出口,新发现的线路地址随重启一起发给对端
if p.refreshEgress != nil {
@@ -828,7 +901,7 @@ func (p *Peer) tryUpgrade() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
- if err := p.restartICE(ctx); err != nil {
+ if err := p.restartICE(ctx, offererWaits); err != nil {
p.log.Debug("ICE 重启没有发起", "err", err)
p.mu.Lock()
p.scheduleUpgradeLocked(upgradeDelays[min(p.upgradeAttempt, len(upgradeDelays)-1)])
diff --git a/client-core/internal/transport/p2p/upgrade_test.go b/client-core/internal/transport/p2p/upgrade_test.go
index 28101ed..aff1347 100644
--- a/client-core/internal/transport/p2p/upgrade_test.go
+++ b/client-core/internal/transport/p2p/upgrade_test.go
@@ -161,7 +161,7 @@ func TestICERestartKeepsConnection(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
- if err := ap.restartICE(ctx); err != nil {
+ if err := ap.restartICE(ctx, false); err != nil {
t.Fatalf("ICE 重启: %v", err)
}
// 重启进行中就发:数据暂时丢失,由 SCTP 重传补上
@@ -344,3 +344,110 @@ func TestSendRefusedWhileDown(t *testing.T) {
t.Errorf("断开时发送返回 %v,应为 ErrNotConnected", err)
}
}
+
+// orderLog 记下出口地址的收发顺序,见 TestEgressOrder。
+type orderLog struct {
+ mu sync.Mutex
+ events []string
+ peers map[string]*Peer
+}
+
+func (o *orderLog) add(e string) {
+ o.mu.Lock()
+ o.events = append(o.events, e)
+ o.mu.Unlock()
+}
+
+// index 返回 from 之后第一次出现 e 的位置,没有则为 -1。
+func (o *orderLog) index(e string, from int) int {
+ o.mu.Lock()
+ defer o.mu.Unlock()
+ for i := from; i < len(o.events); i++ {
+ if o.events[i] == e {
+ return i
+ }
+ }
+ return -1
+}
+
+func (o *orderLog) len() int {
+ o.mu.Lock()
+ defer o.mu.Unlock()
+ return len(o.events)
+}
+
+func (o *orderLog) newPeer(t *testing.T, self, other, egressIP string) *Peer {
+ t.Helper()
+ p, err := NewPeer(PeerOptions{
+ DeviceID: other,
+ SelfID: self,
+ ExtraCandidates: func() []string {
+ return []string{"candidate:1 1 udp 1694498815 " + egressIP + " 50000 typ srflx raddr 0.0.0.0 rport 50000"}
+ },
+ SendSignal: func(_ context.Context, msg *pb.SignalMessage) error {
+ egress := isEgressCandidate(msg.GetCandidate().GetCandidate())
+ if egress {
+ o.add(self + " 发出出口地址")
+ }
+ go func() {
+ o.mu.Lock()
+ target := o.peers[other]
+ o.mu.Unlock()
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ _ = target.HandleSignal(ctx, msg)
+ if egress {
+ o.add(other + " 收到出口地址")
+ }
+ }()
+ return nil
+ },
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(p.Close)
+ return p
+}
+
+// TestEgressOrder 验证谁先发包:等待的一方要先收到对端的出口地址,再公布自己的。
+// 初次握手由发起方等;ICE 重启时可以改请应答方等。
+func TestEgressOrder(t *testing.T) {
+ o := &orderLog{peers: map[string]*Peer{}}
+ alice := o.newPeer(t, "alice", "bob", "203.0.113.10") // 字典序小,不礼让,负责发起
+ bob := o.newPeer(t, "bob", "alice", "203.0.113.20")
+ o.peers["alice"], o.peers["bob"] = alice, bob
+
+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
+ defer cancel()
+ if err := alice.Connect(ctx); err != nil {
+ t.Fatal(err)
+ }
+ deadline := time.Now().Add(15 * time.Second)
+ for time.Now().Before(deadline) && (o.index("alice 收到出口地址", 0) < 0 || o.index("bob 收到出口地址", 0) < 0) {
+ time.Sleep(10 * time.Millisecond)
+ }
+ got, sent := o.index("alice 收到出口地址", 0), o.index("alice 发出出口地址", 0)
+ if got < 0 || sent < 0 || sent < got {
+ t.Fatalf("初次握手:发起方应先收到对端的出口地址再发自己的,实际顺序 %v", o.events)
+ }
+
+ for time.Now().Before(deadline) && (alice.State() != StateDirect || bob.State() != StateDirect) {
+ time.Sleep(10 * time.Millisecond)
+ }
+ if alice.State() != StateDirect {
+ t.Fatalf("没有连通:%v", alice.State())
+ }
+
+ mark := o.len()
+ if err := alice.restartICE(ctx, false); err != nil {
+ t.Fatal(err)
+ }
+ for time.Now().Before(deadline) && (o.index("alice 收到出口地址", mark) < 0 || o.index("bob 收到出口地址", mark) < 0) {
+ time.Sleep(10 * time.Millisecond)
+ }
+ got, sent = o.index("bob 收到出口地址", mark), o.index("bob 发出出口地址", mark)
+ if got < 0 || sent < 0 || sent < got {
+ t.Fatalf("请应答方等的重启:应答方应先收到对端的出口地址再发自己的,实际顺序 %v", o.events[mark:])
+ }
+}
diff --git a/design/nat-traversal.md b/design/nat-traversal.md
index bc49fcf..eabeed5 100644
--- a/design/nat-traversal.md
+++ b/design/nat-traversal.md
@@ -1,6 +1,6 @@
# 多出口网络的 NAT 打洞
-> 状态:已实现。多出口探测随 1.1.0 发布(2026-10-08);中转后换直连、连接失效检测与补发随 1.1.1 发布(2026-10-09)
+> 状态:已实现。多出口探测随 1.1.0 发布(2026-10-08);中转后换直连、连接失效检测与补发随 1.1.1 发布(2026-10-09);谁先发包随 1.1.2 发布(2026-10-09)
> 代码:`client-core/internal/transport/p2p/egress*.go`、`manager.go`、`peer.go`;补发在 `client-core/internal/sync/backlog.go`
> 验证:`tools/natlab/`(NAT 打洞实验室,CI 中运行)
@@ -132,6 +132,36 @@ pion 4.4 也带有自动续提名(renomination),能不重启就换线路
- 连接不在直连/中转状态时,发送一律返回「未连接」。缓冲排不空按失败处理。
- **补发**:同步引擎不再「广播一次了事」,而是逐台设备发送。没送达的(连不上、推送中途断开)记进该设备的待补发队列,连接建立或恢复时补发。队列在内存里,每台设备最多 20 条、24 小时内的;已删除的记录跳过。补发的 offer 带 `backlog` 标记,对端只记进复制记录,不写进剪贴板:对方此刻剪贴板里的内容比这些旧内容更新。
+### 10. 谁先发包(1.1.2)
+
+**问题。**1.1.1 装上之后,本机重启时 1~3 秒就直连;家里那台一重启,就落到中转,之后几次 ICE 重启也换不回直连:
+
+```
+00:28:00.185 P2P 连接状态 connecting 家里那台刚重启上线
+00:28:05.947 P2P 连接状态 relay
+00:28:15.947 当前经中转,尝试换成直连 attempt=1
+00:28:51.411 当前经中转,尝试换成直连 attempt=2
+00:30:16.195 信令已连接 本机重启
+00:30:17.363 P2P 连接状态 direct
+```
+
+临时打开 pion 的 ICE 日志对比成功的那次:本机往家里那台的地址发出第一个包,65 毫秒后才收到对方的第一个包,本机先发。失败的几次正好相反:
+
+- 家里那台刚重启时,本机的出口地址早已探测好,随 offer 一起发过去;对方一收到就开始往这些地址发包。本机要等对方的 answer 和出口地址经服务器绕回来,才能往对方发包,晚了一个信令来回。
+- ICE 重启也是这个顺序:本机发出重启的 offer 和出口地址,对方先发包。
+- 本机重启时,出口要现探测(几百毫秒),出口地址晚于 offer 发出,本机反而先发包,所以总能直连。
+
+**根因。**谁的包先到,谁的路由器就先收到一个「陌生来源」的包。公司路由器没有丢弃它,而是留下了一条连接记录,占住了「出口地址:端口 ↔ 对方地址:端口」这一对。本机随后往同一个对方地址发包时,源端口与这条记录冲突,路由器只好换一个端口;事先告诉对方的地址就对不上了,对方的包也一直撞在那条记录上。这条记录要在对方停止发包 30 秒左右后才过期,而每次重启对方又先发包,于是一直打不通。家里的路由器(运营商级 NAT)直接丢弃这类包,不受影响。
+
+NAT 实验室起初没有复现,是因为模拟路由器都丢弃外来的新连接(第六节「踩过的坑」里那条),而且实验室里信令没有延迟,两端几乎同时发包。去掉公司侧路由器的丢弃规则、给信令加上 80 毫秒延迟后,旧代码稳定落到中转,后续的重启也都失败;连探测服务器看到的公网端口都被挤得变了,与实测公司网络里出口 C、D 端口忽变的现象一致。
+
+**做法。**让「先发包」由规则决定,而不是靠运气:
+
+- 等待的一方先不公布出口地址,等收到对方本轮的出口地址、本端发往那里的包已经发出(再留 200 毫秒),才公布自己的。这样本端的路由器先有一条本端发起的记录,对方的包回来正好对上;先收到陌生包的是对方的路由器。最多等 3 秒,对方是没有出口地址的旧版本时照常发出。
+- 初次握手由发起方等。用户这一对设备里发起方恰好是公司那台,初次握手就能打通。
+- 若先收到包的那一侧路由器才是会占端口的,初次握手会失败。所以换直连的重试里两种顺序轮流用:第一次重试请应答方等(`SdpOffer.answerer_waits`),第二次换回发起方等,依此类推。哪一侧的路由器有这个毛病,最多一次重试就能避开;两侧都有就只能中转。
+- 「出口地址」靠格式识别:出口探测生成的 srflx candidate 不带 ufrag 扩展,pion 自己收集的都带。
+
### 代码位置
| 文件 | 内容 |
@@ -139,7 +169,7 @@ pion 4.4 也带有自动续提名(renomination),能不重启就换线路
| `p2p/egress.go` | 探测、地址过滤、srflx candidate 生成 |
| `p2p/egress_history.go` | 出口历史与推算 |
| `p2p/manager.go` | 共享端口、探测时机、把地址交给每个连接 |
-| `p2p/peer.go` | 发送出口地址、中转等待、`LinkInfo`、ICE 重启换直连、失效连接的清理 |
+| `p2p/peer.go` | 发送出口地址(含谁先发包)、中转等待、`LinkInfo`、ICE 重启换直连、失效连接的清理 |
| `sync/backlog.go` | 逐台发送、待补发队列、连接恢复后补发 |
| `server/relay/turn.go` | TURN 可绑定指定 IP(`ListenIP`),实验室需要 |
@@ -164,7 +194,7 @@ pion 4.4 也带有自动续提名(renomination),能不重启就换线路
`p2p/egress_integration_test.go`:起一台本机 STUN 服务,验证管理器在共享端口上完成探测,把出口地址作为 srflx candidate 发给对端,连接照常建立。
-`p2p/upgrade_test.go`(1.1.1):ICE 重启期间两端状态一直是直连、重启中发出的消息照样送达;中转时发起方自己重启并改回直连,礼让方不主动重启;对端换了证书时本端重建连接(去掉这个处理,测试会卡在「连接中」);连接断开时发送报错。信令桩异步、不保序地投递,比真实服务器更苛刻。
+`p2p/upgrade_test.go`(1.1.1):ICE 重启期间两端状态一直是直连、重启中发出的消息照样送达;中转时发起方自己重启并改回直连,礼让方不主动重启;对端换了证书时本端重建连接(去掉这个处理,测试会卡在「连接中」);连接断开时发送报错。信令桩异步、不保序地投递,比真实服务器更苛刻。`TestEgressOrder`(1.1.2)核对出口地址的收发顺序:初次握手发起方先收到对方的再发自己的,请应答方等的重启反过来;去掉等待,这个测试会失败。
`sync/backlog_test.go`(1.1.1):连不上的设备在连接恢复后按复制先后收到补发、带补发标记,已送达的不重复;太旧的、已删除的不补发,补发途中又断开的留到下次;收到补发的文件不写进剪贴板。
@@ -191,6 +221,8 @@ rA 用 SNAT 规则按目标地址选出口,还原公司网络;rB 还原家
| 两个出口 ↔ 两个出口 | 直连 | 通过 |
| 两个出口 ↔ 端口随机分配的 NAT | 中转 | 通过 |
| 一开始打不通,8 秒后网络恢复(1.1.1) | 先中转,随后换成直连 | 通过 |
+| 公司路由器会为外来的包占住端口,信令延迟 80 毫秒(1.1.2) | 直连(初次握手即打通) | 通过;1.1.1 在这里落到中转,重试也失败 |
+| 家用路由器会为外来的包占住端口,信令延迟 80 毫秒(1.1.2) | 先中转,第一次重试换对方先发包后直连 | 通过 |
最后一个场景让家用路由器开头 8 秒不放行发往公司出口的 UDP,第一次握手只能落到中转;放开后,脚本还要求日志里确实出现了「中转 → 尝试换成直连」,排除一开始就碰巧直连的情况。
@@ -226,6 +258,7 @@ rA 用 SNAT 规则按目标地址选出口,还原公司网络;rB 还原家
- **公共 STUN 域名可能被污染。**`stun.syncthing.net` 在测试网络里被解析到 `192.0.2.42`。不过滤的话,要么白等超时,要么被当成一个出口。
- **实验室场景要互相隔离。**出口历史文件跨场景残留,曾让「无历史」场景意外直连。这反过来也说明历史推算确实生效。
- **pion 选定 candidate 对后不再更换。**一次握手输给中转,整个连接期间都是中转。等待时限只能降低输的概率,要补救得靠 ICE 重启(第 8 节)。
+- **模拟路由器太「规矩」会掩盖问题。**实验室里的路由器都丢弃外来的新连接、信令又没有延迟,于是「谁先发包」从来不是问题;真实的公司路由器会为外来包占住端口,信令也要绕服务器几十毫秒。现在实验室两种路由器都有,会占端口的场景给信令加了延迟。
- **pion 报告失败后不会自己关闭连接**,控制通道也仍显示打开。只把状态改成离线是不够的,必须关闭清理,否则上层永远不会重连(第 9 节)。
## 七、局限与后续
diff --git a/docs/en/index.html b/docs/en/index.html
index 87c9d86..f3ec9f4 100644
--- a/docs/en/index.html
+++ b/docs/en/index.html
@@ -40,7 +40,7 @@
"description": "Open-source tool that syncs the clipboard and files between Macs. Copy text, images, files or folders on one Mac and paste them on another. Devices connect directly over WebRTC with DTLS encryption, falling back to a TURN relay on a server the user hosts.",
"applicationCategory": "UtilitiesApplication",
"operatingSystem": "macOS 13 or later (Intel and Apple silicon)",
- "softwareVersion": "1.1.1",
+ "softwareVersion": "1.1.2",
"datePublished": "2026-10-09",
"inLanguage": "en",
"license": "https://opensource.org/licenses/MIT",
@@ -139,7 +139,7 @@
<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 3v10m0 0-4-4m4 4 4-4M4 16h12"/></svg>
Download for macOS
</a>
- <span class="note">Version 1.1.1, free and open source. macOS 13 or later, Intel and Apple silicon.</span>
+ <span class="note">Version 1.1.2, free and open source. macOS 13 or later, Intel and Apple silicon.</span>
</div>
<div class="demo" role="img" aria-label="Demo: a file is selected on a MacBook Pro and copied with Command C, travels over a direct connection to a MacBook Air, and is pasted there with Command V">
@@ -286,6 +286,7 @@
<li><strong>Every uplink probed.</strong> Seven IP-diverse servers are queried from that port at once, yielding one address per uplink. Addresses poisoned by DNS are dropped first.</li>
<li><strong>All of them sent to the peer.</strong> The peer tries each address; the one matching the real uplink gets through.</li>
<li><strong>Uplinks remembered.</strong> If a round misses an uplink that keeps ports unchanged, its address is filled in as uplink IP plus local port.</li>
+ <li><strong>A rule for who sends first.</strong> Some routers lock a port when the other side's packet arrives first, and punching fails. One side now announces its uplink addresses first; the other sends packets to them before announcing its own. Retries alternate the order.</li>
<li><strong>Relay first, direct later.</strong> When the peer has just started, its addresses can arrive a moment late and the first handshake lands on the relay. CopySync punches again while the link is idle and switches to direct when that works.</li>
</ul>
</div>
@@ -305,7 +306,7 @@
</div>
<h3 class="subhead">Checked against real network topologies</h3>
- <p class="aside-p">The NAT lab builds the nine topologies below with Linux network namespaces and iptables and runs CopySync's actual connection code through each. It runs on every commit.</p>
+ <p class="aside-p">The NAT lab builds the eleven topologies below with Linux network namespaces and iptables and runs CopySync's actual connection code through each. It runs on every commit.</p>
<div class="table-scroll">
<table class="lab">
<thead><tr><th scope="col">Scenario</th><th scope="col">Result</th></tr></thead>
@@ -319,6 +320,8 @@
<tr><td>Two uplinks ↔ two uplinks</td><td>Direct</td></tr>
<tr><td>Two uplinks ↔ NAT that randomizes ports</td><td>Relay</td></tr>
<tr><td>Blocked at first, network recovers later</td><td>Relay, then direct</td></tr>
+ <tr><td>Office router locks a port for unsolicited packets</td><td>Direct</td></tr>
+ <tr><td>Home router locks a port for unsolicited packets</td><td>Relay, then direct after one retry</td></tr>
</tbody>
</table>
</div>
@@ -425,7 +428,7 @@ sudo ./install.sh YOUR_PUBLIC_IP</code></pre>
<div class="table-scroll">
<table class="specs">
<tbody>
- <tr><th scope="row">Version</th><td>1.1.1, released <time datetime="2026-10-09">October 9, 2026</time></td></tr>
+ <tr><th scope="row">Version</th><td>1.1.2, released <time datetime="2026-10-09">October 9, 2026</time></td></tr>
<tr><th scope="row">Mac app</th><td>macOS 13 Ventura or later; universal binary for Intel and Apple silicon, no Rosetta; 45 MB download</td></tr>
<tr><th scope="row">Server</th><td>Linux on x86_64 or ARM64 (systemd or Docker), or macOS; about 15 MB of RAM</td></tr>
<tr><th scope="row">Content</th><td>Plain text, rich text, images and screenshots, files, folders; each type can be turned off</td></tr>
diff --git a/docs/index.html b/docs/index.html
index 6207a7e..e31625f 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -40,7 +40,7 @@
"description": "在两台 Mac 之间同步剪贴板与文件的开源工具。复制文本、图片、文件或文件夹后,另一台 Mac 可直接粘贴。设备间 WebRTC 直连、DTLS 端到端加密,打不通时经用户自建服务器的 TURN 中转。",
"applicationCategory": "UtilitiesApplication",
"operatingSystem": "macOS 13 及以上(Intel 与 Apple 芯片)",
- "softwareVersion": "1.1.1",
+ "softwareVersion": "1.1.2",
"datePublished": "2026-10-09",
"inLanguage": "zh-CN",
"license": "https://opensource.org/licenses/MIT",
@@ -139,7 +139,7 @@
<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 3v10m0 0-4-4m4 4 4-4M4 16h12"/></svg>
下载 macOS 版
</a>
- <span class="note">版本 1.1.1,免费开源。macOS 13 及以上,Intel 与 Apple 芯片通用。</span>
+ <span class="note">版本 1.1.2,免费开源。macOS 13 及以上,Intel 与 Apple 芯片通用。</span>
</div>
<div class="demo" role="img" aria-label="演示:在 MacBook Pro 上选中文件并按下 Command C,文件经直连传到 MacBook Air,在那里按下 Command V 粘贴出来">
@@ -286,6 +286,7 @@
<li><strong>探测每个出口。</strong>在这个端口上同时询问 7 台 IP 分散的服务器,网络有几个出口就拿到几个地址。被 DNS 污染的地址先过滤掉。</li>
<li><strong>全部告诉对端。</strong>每个出口的地址都发给对端,由它逐个尝试,与实际出口吻合的那一个能打通。</li>
<li><strong>记住见过的出口。</strong>某次漏掉的出口如果不改端口,就用「出口 IP + 本地端口」推算补上。</li>
+ <li><strong>定好谁先发包。</strong>有的路由器收到对方先到的包会占住端口,打洞就失败了。现在一方先公布出口地址,另一方收到后先往那边发包再公布自己的;重试时两种顺序轮流用。</li>
<li><strong>先中转,再换直连。</strong>对端刚启动时,出口地址可能晚到一步,第一次握手落到了中转。之后会在空闲时再打一次洞,打通就换成直连。</li>
</ul>
</div>
@@ -305,7 +306,7 @@
</div>
<h3 class="subhead">用真实的网络拓扑验证</h3>
- <p class="aside-p">NAT 打洞实验室用 Linux 网络命名空间与 iptables 搭出下面 9 种拓扑,用 CopySync 真实的连接代码各跑一遍。每次提交代码都会自动运行。</p>
+ <p class="aside-p">NAT 打洞实验室用 Linux 网络命名空间与 iptables 搭出下面 11 种拓扑,用 CopySync 真实的连接代码各跑一遍。每次提交代码都会自动运行。</p>
<div class="table-scroll">
<table class="lab">
<thead><tr><th scope="col">场景</th><th scope="col">结果</th></tr></thead>
@@ -319,6 +320,8 @@
<tr><td>两个出口 ↔ 两个出口</td><td>直连</td></tr>
<tr><td>两个出口 ↔ 端口随机分配的 NAT</td><td>中转</td></tr>
<tr><td>一开始打不通,之后网络恢复</td><td>先中转,随后直连</td></tr>
+ <tr><td>公司路由器会为外来的包占住端口</td><td>直连</td></tr>
+ <tr><td>家用路由器会为外来的包占住端口</td><td>先中转,重试一次后直连</td></tr>
</tbody>
</table>
</div>
@@ -425,7 +428,7 @@ sudo ./install.sh 你的公网IP</code></pre>
<div class="table-scroll">
<table class="specs">
<tbody>
- <tr><th scope="row">版本</th><td>1.1.1,<time datetime="2026-10-09">2026 年 10 月 9 日</time>发布</td></tr>
+ <tr><th scope="row">版本</th><td>1.1.2,<time datetime="2026-10-09">2026 年 10 月 9 日</time>发布</td></tr>
<tr><th scope="row">Mac 客户端</th><td>macOS 13 Ventura 及以上;Intel 与 Apple 芯片通用,不需要 Rosetta;安装包 45 MB</td></tr>
<tr><th scope="row">服务器</th><td>Linux x86_64 或 ARM64(systemd 或 Docker),也可以运行在 macOS 上;约 15 MB 内存</td></tr>
<tr><th scope="row">同步的内容</th><td>纯文本、带格式的文本、图片与截图、文件、文件夹,可以按类型关闭</td></tr>
diff --git a/docs/llms.txt b/docs/llms.txt
index afb09a7..08f3cf3 100644
--- a/docs/llms.txt
+++ b/docs/llms.txt
@@ -6,7 +6,7 @@
Key facts:
-- Current version: 1.1.1, released 2026-10-09.
+- Current version: 1.1.2, released 2026-10-09.
- Client: macOS 13 Ventura or later, universal binary for Intel and Apple silicon. Distributed as a DMG; not notarized by Apple, so the first launch needs "Open Anyway" in System Settings → Privacy & Security.
- Server: a single static Go binary for Linux (x86_64, ARM64) or macOS, installed via systemd script or Docker Compose. Ports: 8787/tcp (signaling), 3478/udp (STUN/TURN), 32768–60999/udp (relay).
- Content synced: plain text, rich text (HTML plus a plain-text copy), images, files, folders.
diff --git a/proto/copysync/v1/wire.proto b/proto/copysync/v1/wire.proto
index 21e21c2..18e5440 100644
--- a/proto/copysync/v1/wire.proto
+++ b/proto/copysync/v1/wire.proto
@@ -42,6 +42,10 @@ message SdpOffer {
// DTLS 证书指纹。经签名传输,杜绝信令服务器中间人攻击——
// 这是 P2P 通道真正的安全锚点。
string dtls_fingerprint = 2;
+ // 应答方先别发自己的出口地址:等收到发起方的出口地址、先往那边发过包再发。
+ // 默认(false)是发起方这样等。谁先发包决定了谁的路由器先收到对方的包,
+ // 有的路由器会因此占住端口、打洞失败,所以换直连的重试里两种顺序轮流用。
+ bool answerer_waits = 3;
}
message SdpAnswer {
diff --git a/proto/gen/copysync/v1/wire.pb.go b/proto/gen/copysync/v1/wire.pb.go
index aa9a3f6..1e97830 100644
--- a/proto/gen/copysync/v1/wire.pb.go
+++ b/proto/gen/copysync/v1/wire.pb.go
@@ -332,8 +332,12 @@ type SdpOffer struct {
// DTLS 证书指纹。经签名传输,杜绝信令服务器中间人攻击——
// 这是 P2P 通道真正的安全锚点。
DtlsFingerprint string `protobuf:"bytes,2,opt,name=dtls_fingerprint,json=dtlsFingerprint,proto3" json:"dtls_fingerprint,omitempty"`
- unknownFields protoimpl.UnknownFields
- sizeCache protoimpl.SizeCache
+ // 应答方先别发自己的出口地址:等收到发起方的出口地址、先往那边发过包再发。
+ // 默认(false)是发起方这样等。谁先发包决定了谁的路由器先收到对方的包,
+ // 有的路由器会因此占住端口、打洞失败,所以换直连的重试里两种顺序轮流用。
+ AnswererWaits bool `protobuf:"varint,3,opt,name=answerer_waits,json=answererWaits,proto3" json:"answerer_waits,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
}
func (x *SdpOffer) Reset() {
@@ -380,6 +384,13 @@ func (x *SdpOffer) GetDtlsFingerprint() string {
return ""
}
+func (x *SdpOffer) GetAnswererWaits() bool {
+ if x != nil {
+ return x.AnswererWaits
+ }
+ return false
+}
+
type SdpAnswer struct {
state protoimpl.MessageState `protogen:"open.v1"`
Sdp string `protobuf:"bytes,1,opt,name=sdp,proto3" json:"sdp,omitempty"`
@@ -2077,10 +2088,11 @@ const file_copysync_v1_wire_proto_rawDesc = "" +
"\x06answer\x18\v \x01(\v2\x16.copysync.v1.SdpAnswerH\x00R\x06answer\x129\n" +
"\tcandidate\x18\f \x01(\v2\x19.copysync.v1.IceCandidateH\x00R\tcandidate\x12'\n" +
"\x04ping\x18\r \x01(\v2\x11.copysync.v1.PingH\x00R\x04pingB\t\n" +
- "\apayload\"G\n" +
+ "\apayload\"n\n" +
"\bSdpOffer\x12\x10\n" +
"\x03sdp\x18\x01 \x01(\tR\x03sdp\x12)\n" +
- "\x10dtls_fingerprint\x18\x02 \x01(\tR\x0fdtlsFingerprint\"H\n" +
+ "\x10dtls_fingerprint\x18\x02 \x01(\tR\x0fdtlsFingerprint\x12%\n" +
+ "\x0eanswerer_waits\x18\x03 \x01(\bR\ranswererWaits\"H\n" +
"\tSdpAnswer\x12\x10\n" +
"\x03sdp\x18\x01 \x01(\tR\x03sdp\x12)\n" +
"\x10dtls_fingerprint\x18\x02 \x01(\tR\x0fdtlsFingerprint\"m\n" +
diff --git a/scripts/build.sh b/scripts/build.sh
index 146fb46..a1087da 100755
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -14,7 +14,7 @@ set -euo pipefail
cd "$(dirname "$0")/.."
DIST=dist
-VERSION=${VERSION:-1.1.1}
+VERSION=${VERSION:-1.1.2}
rm -rf "$DIST" && mkdir -p "$DIST"
# 构建 x86_64 + arm64 通用二进制:两台 Mac 的芯片未必相同,单架构的
diff --git a/scripts/package.sh b/scripts/package.sh
index 174b406..7fa2b7e 100755
--- a/scripts/package.sh
+++ b/scripts/package.sh
@@ -15,7 +15,7 @@
set -euo pipefail
cd "$(dirname "$0")/.."
-export VERSION=${VERSION:-1.1.1}
+export VERSION=${VERSION:-1.1.2}
OUT=release
./scripts/build.sh
diff --git a/tools/natlab/run.sh b/tools/natlab/run.sh
index 1114e18..684a914 100755
--- a/tools/natlab/run.sh
+++ b/tools/natlab/run.sh
@@ -25,6 +25,7 @@ PROBE="10.200.1.2:3478,10.200.1.3:3478,10.200.1.4:3478"
ns() { ip netns exec "$@"; }
INFRA_PID=""
+INFRA_ARGS=() # 各场景额外传给 infra 的参数,由 check 设置
# 按进程号结束 infra:用 pkill -f 按路径匹配会连本脚本自己一起杀掉(命令行里也有这个路径)
cleanup() {
@@ -123,7 +124,7 @@ setup() {
esac
ns inet "$BIN" infra -http 10.200.0.2:9000 -turn 10.200.0.3 \
- -stun 10.200.1.1,10.200.1.2,10.200.1.3,10.200.1.4 >"$WORK/infra.log" 2>&1 &
+ -stun 10.200.1.1,10.200.1.2,10.200.1.3,10.200.1.4 "${INFRA_ARGS[@]}" >"$WORK/infra.log" 2>&1 &
INFRA_PID=$!
for _ in $(seq 50); do
ns a bash -c 'exec 3<>/dev/tcp/10.200.0.2/9000' 2>/dev/null && return
@@ -135,12 +136,16 @@ setup() {
PASS=0
FAIL=0
-# check <场景名> <A 侧> <B 侧> <direct|relay> [legacy|history|late]
+# check <场景名> <A 侧> <B 侧> <direct|relay> [legacy|history|late|leaky-a|leaky-b]
check() {
# NATLAB_ONLY 只跑名字里含这段文字的场景,排查问题时用
[[ -n ${NATLAB_ONLY:-} && $1 != *"$NATLAB_ONLY"* ]] && return
local name=$1 expect=$4 flag=${5:-} aflags=() bflags=() state="$WORK/a-state.json"
rm -f "$state" # 每个场景从空白开始:上一个场景的出口历史会让这一个「意外」直连
+ INFRA_ARGS=()
+ # 现实里信令经服务器绕一道,比设备之间的包慢几十毫秒:对端拿到本端地址就开始发包,
+ # 本端要多等一个来回才拿到对端的地址。leaky 场景靠这个时间差复现问题
+ [[ $flag == leaky-* ]] && INFRA_ARGS=(-delay 80ms)
setup "$2" "$3"
case $flag in
legacy) aflags=(-legacy) ;;
@@ -159,6 +164,18 @@ check() {
aflags=(-settle 40s)
bflags=(-settle 40s)
;;
+ leaky-a | leaky-b)
+ # 路由器不丢弃外网主动发来的包:包落到路由器本机,留下一条连接记录。对端的包若先到,
+ # 本端随后往同一地址发包时源端口被占,SNAT 只好换一个端口,事先告诉对端的地址就失效了。
+ # 实测公司路由器就是这样:对端先发包的那几次都没打通。
+ # leaky-a:a(发起方)的路由器这样,初次握手由 a 先发包,应当直接打通;
+ # leaky-b:b(应答方)的路由器这样,初次握手打不通,第一次重试换 b 先发包后打通
+ local r=rA
+ [[ $flag == leaky-b ]] && r=rB
+ ns "$r" iptables -D INPUT -i wan -m conntrack --ctstate NEW -j DROP
+ aflags=(-settle 40s)
+ bflags=(-settle 40s)
+ ;;
esac
local common=(-infra http://10.200.0.2:9000 -stun 10.200.1.1:3478 -timeout 30s)
ns b "$BIN" peer -id b -peer a "${common[@]}" -probe "$PROBE" "${bflags[@]}" >"$WORK/b.json" 2>"$WORK/b.log" &
@@ -186,8 +203,11 @@ check() {
echo " a: $(cat "$WORK/a.json")"
echo " b: $(cat "$WORK/b.json")"
grep -h -E '出口探测完成|P2P|换成直连|ICE 重启|失败|error' "$WORK/a.log" "$WORK/b.log" | head -8 | sed 's/^/ /'
- # NATLAB_KEEP 指定目录时把完整日志留下来
- [[ -n ${NATLAB_KEEP:-} ]] && cp "$WORK"/*.log "$NATLAB_KEEP/" 2>/dev/null || true
+ fi
+ # NATLAB_KEEP 指定目录时把每个场景的完整日志留下来
+ if [[ -n ${NATLAB_KEEP:-} ]]; then
+ local dir="$NATLAB_KEEP/${name// /_}"
+ mkdir -p "$dir" && cp "$WORK"/*.log "$dir/" 2>/dev/null || true
fi
}
@@ -201,6 +221,8 @@ check "两个出口,第二条没探测到,有历史" hidden cone direct hist
check "两个出口 ↔ 两个出口" dual dual direct
check "两个出口 ↔ 端口随机分配的 NAT" dual symmetric relay
check "一开始打不通,先中转后换直连" dual cone direct late
+check "公司路由器会为外来的包占住端口" dual cone direct leaky-a
+check "家用路由器会为外来的包占住端口" dual cone direct leaky-b
echo
echo "通过 $PASS,失败 $FAIL"
diff --git a/ui/lib/gen/copysync/v1/wire.pb.dart b/ui/lib/gen/copysync/v1/wire.pb.dart
index f14bb64..d42a3ea 100644
--- a/ui/lib/gen/copysync/v1/wire.pb.dart
+++ b/ui/lib/gen/copysync/v1/wire.pb.dart
@@ -275,10 +275,12 @@ class SdpOffer extends $pb.GeneratedMessage {
factory SdpOffer({
$core.String? sdp,
$core.String? dtlsFingerprint,
+ $core.bool? answererWaits,
}) {
final result = SdpOffer._();
if (sdp != null) result.sdp = sdp;
if (dtlsFingerprint != null) result.dtlsFingerprint = dtlsFingerprint;
+ if (answererWaits != null) result.answererWaits = answererWaits;
return result;
}
@@ -297,6 +299,7 @@ class SdpOffer extends $pb.GeneratedMessage {
createEmptyInstance: SdpOffer.$_createMessage)
..aOS(1, _omitFieldNames ? '' : 'sdp')
..aOS(2, _omitFieldNames ? '' : 'dtlsFingerprint')
+ ..aOB(3, _omitFieldNames ? '' : 'answererWaits')
..hasRequiredFields = false;
@$core.Deprecated('See https://github.com/google/protobuf.dart/issues/998.')
@@ -338,6 +341,18 @@ class SdpOffer extends $pb.GeneratedMessage {
$core.bool hasDtlsFingerprint() => $_has(1);
@$pb.TagNumber(2)
void clearDtlsFingerprint() => $_clearField(2);
+
+ /// 应答方先别发自己的出口地址:等收到发起方的出口地址、先往那边发过包再发。
+ /// 默认(false)是发起方这样等。谁先发包决定了谁的路由器先收到对方的包,
+ /// 有的路由器会因此占住端口、打洞失败,所以换直连的重试里两种顺序轮流用。
+ @$pb.TagNumber(3)
+ $core.bool get answererWaits => $_getBF(2);
+ @$pb.TagNumber(3)
+ set answererWaits($core.bool value) => $_setBool(2, value);
+ @$pb.TagNumber(3)
+ $core.bool hasAnswererWaits() => $_has(2);
+ @$pb.TagNumber(3)
+ void clearAnswererWaits() => $_clearField(3);
}
class SdpAnswer extends $pb.GeneratedMessage {
diff --git a/ui/lib/gen/copysync/v1/wire.pbjson.dart b/ui/lib/gen/copysync/v1/wire.pbjson.dart
index 4068d20..0081bfc 100644
--- a/ui/lib/gen/copysync/v1/wire.pbjson.dart
+++ b/ui/lib/gen/copysync/v1/wire.pbjson.dart
@@ -124,13 +124,14 @@ const SdpOffer$json = {
'2': [
{'1': 'sdp', '3': 1, '4': 1, '5': 9, '10': 'sdp'},
{'1': 'dtls_fingerprint', '3': 2, '4': 1, '5': 9, '10': 'dtlsFingerprint'},
+ {'1': 'answerer_waits', '3': 3, '4': 1, '5': 8, '10': 'answererWaits'},
],
};
/// Descriptor for `SdpOffer`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List sdpOfferDescriptor = $convert.base64Decode(
'CghTZHBPZmZlchIQCgNzZHAYASABKAlSA3NkcBIpChBkdGxzX2ZpbmdlcnByaW50GAIgASgJUg'
- '9kdGxzRmluZ2VycHJpbnQ=');
+ '9kdGxzRmluZ2VycHJpbnQSJQoOYW5zd2VyZXJfd2FpdHMYAyABKAhSDWFuc3dlcmVyV2FpdHM=');
@$core.Deprecated('Use sdpAnswerDescriptor instead')
const SdpAnswer$json = {
diff --git a/ui/pubspec.yaml b/ui/pubspec.yaml
index 4e1688b..d9a9065 100644
--- a/ui/pubspec.yaml
+++ b/ui/pubspec.yaml
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
-version: 1.1.1+5
+version: 1.1.2+6
environment:
sdk: ^3.12.2