forked from S4tvara/Sietch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
351 lines (285 loc) · 10.4 KB
/
sync.go
File metadata and controls
351 lines (285 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
Copyright © 2025 SubstantialCattle5, nilaysharan.com
*/
package cmd
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/spf13/cobra"
"github.com/substantialcattle5/sietch/internal/config"
"github.com/substantialcattle5/sietch/internal/fs"
"github.com/substantialcattle5/sietch/internal/p2p"
"github.com/substantialcattle5/sietch/util"
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync [peer-address]",
Short: "Synchronize with another Sietch vault",
Long: `Synchronize files with another Sietch vault over the network.
This command syncs your vault with another vault, either by auto-discovering
peers on the local network or by connecting to a specified peer address.
Examples:
sietch sync # Auto-discover and sync with peers
sietch sync /ip4/192.168.1.5/tcp/4001/p2p/QmPeerID # Sync with a specific peer`,
RunE: func(cmd *cobra.Command, args []string) error {
// Get verbose flag
verbose, _ := cmd.Flags().GetBool("verbose")
// Create a context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle interrupts gracefully
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
fmt.Println("\nReceived interrupt signal, shutting down...")
cancel()
}()
// Find the vault root
vaultRoot, err := fs.FindVaultRoot()
if err != nil {
return fmt.Errorf("not inside a vault: %v", err)
}
// Load vault configuration
vaultCfg, err := config.LoadVaultConfig(vaultRoot)
if err != nil {
return fmt.Errorf("failed to load vault config: %v", err)
}
// Load RSA keys for secure communication
privateKey, publicKey, err := loadRSAKeys(vaultRoot, vaultCfg)
if err != nil {
return fmt.Errorf("failed to load RSA keys: %v", err)
}
// Convert RSA private key to libp2p format
libp2pPrivKey, err := rsaToLibp2pPrivateKey(privateKey)
if err != nil {
return fmt.Errorf("failed to convert RSA key to libp2p format: %v", err)
}
// Create a libp2p host with our identity key
port, _ := cmd.Flags().GetInt("port")
var opts []libp2p.Option
// Use our RSA key as the node identity
opts = append(opts, libp2p.Identity(libp2pPrivKey))
if port > 0 {
opts = append(opts, libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port)))
} else {
opts = append(opts, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
}
host, err := libp2p.New(opts...)
if err != nil {
return fmt.Errorf("failed to create libp2p host: %v", err)
}
defer host.Close()
fmt.Printf("🔌 Started Sietch node with ID: %s\n", host.ID().String())
// Print our listen addresses only in verbose mode
if verbose {
fmt.Println("📡 Listening on:")
for _, addr := range host.Addrs() {
fmt.Printf(" %s/p2p/%s\n", addr.String(), host.ID().String())
}
}
// Load the vault manager
vaultMgr, err := config.NewManager(vaultRoot)
if err != nil {
return fmt.Errorf("failed to load vault: %v", err)
}
// Create the sync service with RSA key information
syncService, err := p2p.NewSecureSyncService(host, vaultMgr, privateKey, publicKey, vaultCfg.Sync.RSA)
if err != nil {
return fmt.Errorf("failed to create sync service: %v", err)
}
// Set verbose mode on sync service
syncService.SetVerbose(verbose)
// Start secure protocol handlers
syncService.RegisterProtocols(ctx)
// Specific peer address provided
if len(args) > 0 {
peerAddr := args[0]
fmt.Printf("🔄 Connecting to peer: %s\n", peerAddr)
// Parse the multiaddress
maddr, err := multiaddr.NewMultiaddr(peerAddr)
if err != nil {
return fmt.Errorf("invalid peer address: %v", err)
}
// Extract the peer ID from the multiaddress
info, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
return fmt.Errorf("failed to parse peer info: %v", err)
}
// Connect to the peer
if err := host.Connect(ctx, *info); err != nil {
return fmt.Errorf("failed to connect to peer: %v", err)
}
fmt.Printf("✅ Connected to peer: %s\n", info.ID.String())
// Perform secure handshake and key exchange
trusted, err := syncService.VerifyAndExchangeKeys(ctx, info.ID)
if err != nil {
return fmt.Errorf("key exchange failed: %v", err)
}
if !trusted {
// If not automatically trusted, prompt user
fmt.Printf("\n⚠️ New peer detected!\n")
fmt.Printf("Peer ID: %s\n", info.ID.String())
fingerprint, err := syncService.GetPeerFingerprint(info.ID)
if err == nil {
fmt.Printf("Fingerprint: %s\n", fingerprint)
}
if !promptForTrust() {
return fmt.Errorf("sync canceled - peer not trusted")
}
// Add peer to trusted list
err = syncService.AddTrustedPeer(ctx, info.ID)
if err != nil {
return fmt.Errorf("failed to add trusted peer: %v", err)
}
}
fmt.Println("📝 Starting vault synchronization...")
// Sync with the peer
result, err := syncService.SyncWithPeer(ctx, info.ID)
if err != nil {
return fmt.Errorf("sync failed: %v", err)
}
// Display sync results
displaySyncResults(result)
return nil
}
// Auto-discovery mode
fmt.Println("🔍 No peer specified, starting auto-discovery...")
// Create the discovery factory
factory := p2p.NewFactory()
// Create and start mDNS discovery
discovery, err := factory.CreateMDNS(host)
if err != nil {
return fmt.Errorf("failed to create mDNS discovery: %v", err)
}
if err := discovery.Start(ctx); err != nil {
return fmt.Errorf("failed to start mDNS discovery: %v", err)
}
defer func() { _ = discovery.Stop() }()
fmt.Println("📡 Searching for peers on local network...")
// Set timeout for discovery
timeout, _ := cmd.Flags().GetInt("timeout")
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
defer timeoutCancel()
// Wait for peers
select {
case peerInfo := <-discovery.DiscoveredPeers():
// Check if it's our own peer ID
if peerInfo.ID == host.ID() {
fmt.Println("🔄 Found our own peer, continuing discovery...")
// Continue waiting for other peers
select {
case peerInfo = <-discovery.DiscoveredPeers():
if peerInfo.ID == host.ID() {
return fmt.Errorf("only found our own peer, no others on network")
}
case <-timeoutCtx.Done():
return fmt.Errorf("discovery timed out after %d seconds", timeout)
}
}
fmt.Printf("✅ Found peer: %s\n", peerInfo.ID.String())
// Connect to the peer
if err := host.Connect(ctx, peerInfo); err != nil {
return fmt.Errorf("failed to connect to peer: %v", err)
}
// Perform secure handshake and key exchange
trusted, err := syncService.VerifyAndExchangeKeys(ctx, peerInfo.ID)
if err != nil {
return fmt.Errorf("key exchange failed: %v", err)
}
if !trusted {
// If not automatically trusted, prompt user
fmt.Printf("\n⚠️ New peer detected!\n")
fmt.Printf("Peer ID: %s\n", peerInfo.ID.String())
fingerprint, err := syncService.GetPeerFingerprint(peerInfo.ID)
if err == nil {
fmt.Printf("Fingerprint: %s\n", fingerprint)
}
if !promptForTrust() {
return fmt.Errorf("sync canceled - peer not trusted")
}
// Add peer to trusted list
err = syncService.AddTrustedPeer(ctx, peerInfo.ID)
if err != nil {
return fmt.Errorf("failed to add trusted peer: %v", err)
}
}
fmt.Printf("🔄 Starting sync with peer: %s\n", peerInfo.ID.String())
// Sync with the peer
result, err := syncService.SyncWithPeer(ctx, peerInfo.ID)
if err != nil {
return fmt.Errorf("sync failed: %v", err)
}
// Display sync results
displaySyncResults(result)
case <-timeoutCtx.Done():
return fmt.Errorf("discovery timed out after %d seconds, no peers found", timeout)
}
return nil
},
}
// loadRSAKeys loads the RSA key pair from the vault
func loadRSAKeys(vaultRoot string, cfg *config.VaultConfig) (*rsa.PrivateKey, *rsa.PublicKey, error) {
// Get path to private key
privateKeyPath := filepath.Join(vaultRoot, cfg.Sync.RSA.PrivateKeyPath)
// Read private key file
privateKeyData, err := os.ReadFile(privateKeyPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to read private key: %w", err)
}
// Decode PEM block
block, _ := pem.Decode(privateKeyData)
if block == nil || block.Type != "RSA PRIVATE KEY" {
return nil, nil, fmt.Errorf("failed to decode PEM block containing private key")
}
// Parse private key
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse private key: %w", err)
}
// Get public key from private key
publicKey := &privateKey.PublicKey
return privateKey, publicKey, nil
}
// rsaToLibp2pPrivateKey converts a Go RSA private key to libp2p format
func rsaToLibp2pPrivateKey(privateKey *rsa.PrivateKey) (crypto.PrivKey, error) {
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
return crypto.UnmarshalRsaPrivateKey(privateKeyBytes)
}
// promptForTrust asks the user whether to trust a new peer
func promptForTrust() bool {
fmt.Print("\nDo you want to trust this peer? (y/n): ")
var response string
_, _ = fmt.Scanln(&response)
return response == "y" || response == "Y" || response == "yes" || response == "Yes"
}
// displaySyncResults shows the results of a sync operation
func displaySyncResults(result *p2p.SyncResult) {
fmt.Println("\n✅ Synchronization complete!")
fmt.Printf(" Files transferred: %d\n", result.FileCount)
fmt.Printf(" Chunks transferred: %d\n", result.ChunksTransferred)
fmt.Printf(" Chunks deduplicated: %d\n", result.ChunksDeduplicated)
fmt.Printf(" Data transferred: %s\n", util.HumanReadableSize(result.BytesTransferred))
fmt.Printf(" Duration: %s\n", result.Duration.Round(time.Millisecond))
}
func init() {
rootCmd.AddCommand(syncCmd)
// Add command flags
syncCmd.Flags().IntP("port", "p", 0, "Port to use for libp2p (0 for random port)")
syncCmd.Flags().IntP("timeout", "t", 60, "Discovery timeout in seconds (for auto-discovery)")
syncCmd.Flags().BoolP("force-trust", "f", false, "Automatically trust new peers without prompting")
syncCmd.Flags().BoolP("read-only", "r", false, "Only receive files, don't send")
syncCmd.Flags().BoolP("verbose", "v", false, "Enable verbose debug output")
}