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
refactor: share the pairing code format between server and client
baiyuze54b17f0ccdd3
diff --git a/proto/pairingcode/pairingcode.go b/proto/pairingcode/pairingcode.gonew file mode 100644index 0000000..2833e0a--- /dev/null+++ b/proto/pairingcode/pairingcode.go@@ -0,0 +1,43 @@+// Package pairingcode 定义配对码的格式。+//+// 配对码既可能由信令服务器签发,也可能由设备在局域网里自己生成;+// 两边必须是同一种格式,用户在界面里输入时才不必区分来源。+// 因此与 deviceid 一样放在 proto 模块里,由服务器和客户端共用。+package pairingcode++import (+ "crypto/rand"+ "fmt"+ "math/big"+ "strings"+ "time"+)++// Alphabet 刻意剔除易混字符(0/O、1/I/L),因为用户可能要口头转述。+const Alphabet = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"++const (+ Length = 6+ TTL = 5 * time.Minute+)++// Generate 生成一个新的配对码。+func Generate() (string, error) {+ out := make([]byte, Length)+ max := big.NewInt(int64(len(Alphabet)))+ for i := range out {+ // 用密码学随机源:配对码在有效期内是唯一的准入凭证+ n, err := rand.Int(rand.Reader, max)+ if err != nil {+ return "", fmt.Errorf("生成配对码: %w", err)+ }+ out[i] = Alphabet[n.Int64()]+ }+ return string(out), nil+}++// Normalize 把用户输入的配对码整理成标准形式:去掉空白与连字符,转成大写。+func Normalize(code string) string {+ code = strings.ToUpper(strings.TrimSpace(code))+ return strings.NewReplacer(" ", "", "-", "").Replace(code)+}diff --git a/server/pairing/pairing.go b/server/pairing/pairing.goindex 5fc2846..aabe294 100644--- a/server/pairing/pairing.go+++ b/server/pairing/pairing.go@@ -5,20 +5,17 @@package pairingimport (- "crypto/rand""errors"- "fmt"- "math/big""sync""time"-)-// 配对码字符集刻意剔除易混字符(0/O、1/I/L),因为用户可能要口头转述。-const codeAlphabet = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"+ "github.com/baiyuze/copysync/proto/pairingcode"+)+// 配对码的格式与局域网里设备自己生成的一致,见 proto/pairingcode。const (- CodeLength = 6- CodeTTL = 5 * time.Minute+ CodeLength = pairingcode.Length+ CodeTTL = pairingcode.TTL)var (@@ -66,7 +63,7 @@ func (b *Broker) Create(initiator Peer) (code string, expiresAt time.Time, ch <-// 极小概率撞码,重试几次即可for attempt := 0; attempt < 8; attempt++ {- c, genErr := generateCode()+ c, genErr := pairingcode.Generate()if genErr != nil {return "", time.Time{}, nil, genErr}@@ -135,17 +132,3 @@ func (b *Broker) Len() int {defer b.mu.Unlock()return len(b.codes)}--func generateCode() (string, error) {- out := make([]byte, CodeLength)- max := big.NewInt(int64(len(codeAlphabet)))- for i := range out {- // 用密码学随机源:配对码在有效期内是唯一的准入凭证- n, err := rand.Int(rand.Reader, max)- if err != nil {- return "", fmt.Errorf("生成配对码: %w", err)- }- out[i] = codeAlphabet[n.Int64()]- }- return string(out), nil-}diff --git a/server/pairing/pairing_test.go b/server/pairing/pairing_test.goindex 3edfb5e..1cc1ed5 100644--- a/server/pairing/pairing_test.go+++ b/server/pairing/pairing_test.go@@ -4,6 +4,8 @@ import ("strings""testing""time"++ "github.com/baiyuze/copysync/proto/pairingcode")func peerNamed(id string) Peer {@@ -21,12 +23,12 @@ func TestCodeFormat(t *testing.T) {}// 字符集刻意剔除了易混字符,用户可能要口头转述配对码for _, c := range code {- if !strings.ContainsRune(codeAlphabet, c) {+ if !strings.ContainsRune(pairingcode.Alphabet, c) {t.Errorf("配对码含字符集外的字符 %q(可能与 0/O、1/I 混淆)", c)}}for _, bad := range []string{"0", "O", "1", "I", "L"} {- if strings.Contains(codeAlphabet, bad) {+ if strings.Contains(pairingcode.Alphabet, bad) {t.Errorf("字符集不该包含易混字符 %q", bad)}}