db-sync 0.1.1

A secure and stable database backup synchronization system with automatic file transfer and verification
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use anyhow::Result;
use log::{error, info, warn};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::Infallible;
use std::fs;
use std::path::Path;
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::io::AsyncWriteExt;
use warp::{Filter, Reply, Rejection};
use uuid::Uuid;
use blake3;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    pub listen_addr: String,
    pub data: Vec<SystemConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemConfig {
    pub system_name: String,
    pub system_ip: String,
    pub backup_dir: String,
    pub auth_token: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FileInfo {
    pub filename: String,
    pub blake3: String,
    pub size: u64,
    pub upload_time: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub message: String,
    pub data: Option<T>,
}

pub struct ServerState {
    pub config: ServerConfig,
    pub file_registry: Arc<RwLock<HashMap<String, HashMap<String, FileInfo>>>>, // system_ip -> filename -> FileInfo
    pub chunked_uploads: Arc<RwLock<HashMap<String, ChunkedUploadInfo>>>, // upload_id -> ChunkedUploadInfo
}

#[derive(Debug, Clone)]
pub struct ChunkedUploadInfo {
    pub system_ip: String,
    pub filename: String,
    pub expected_blake3: String,
    pub expected_size: u64,
    pub chunk_size: u64,
    pub temp_file_path: String,
    pub received_chunks: HashMap<usize, u64>, // chunk_number -> chunk_size
    pub total_received: u64,
}

impl ServerState {
    pub fn new(config: ServerConfig) -> Self {
        Self {
            config,
            file_registry: Arc::new(RwLock::new(HashMap::new())),
            chunked_uploads: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn validate_token(&self, token: &str, system_ip: &str) -> bool {
        self.config.data.iter().any(|sys| {
            sys.system_ip == system_ip && sys.auth_token == token
        })
    }

    pub fn get_backup_dir(&self, system_ip: &str) -> Option<String> {
        self.config.data.iter()
            .find(|sys| sys.system_ip == system_ip)
            .map(|sys| sys.backup_dir.clone())
    }
}

async fn load_config(config_path: &str) -> Result<ServerConfig> {
    let config_content = fs::read_to_string(config_path)?;
    let config: ServerConfig = serde_json::from_str(&config_content)?;
    Ok(config)
}

async fn health_check() -> Result<impl Reply, Infallible> {
    Ok(warp::reply::json(&ApiResponse {
        success: true,
        message: "Server is running".to_string(),
        data: None::<String>,
    }))
}

async fn upload_file(
    state: Arc<ServerState>,
    token: String,
    system_ip: String,
    filename: String,
    blake3: String,
    size: u64,
    file_content: bytes::Bytes,
) -> Result<impl Reply, Rejection> {
    // Validate authentication token
    if !state.validate_token(&token, &system_ip) {
        error!("Invalid auth token for system: {}", system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "Invalid authentication token".to_string(),
            data: None,
        }));
    }

    // Get backup directory
    let backup_dir = match state.get_backup_dir(&system_ip) {
        Some(dir) => dir,
        None => {
            error!("Backup directory not found for system: {}", system_ip);
            return Ok(warp::reply::json(&ApiResponse::<String> {
                success: false,
                message: "System not configured".to_string(),
                data: None,
            }));
        }
    };

    // Ensure backup directory exists
    if let Err(e) = fs::create_dir_all(&backup_dir) {
        error!("Failed to create backup directory {}: {}", backup_dir, e);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: format!("Failed to create backup directory: {}", e),
            data: None,
        }));
    }

    // Validate file size
    if file_content.len() as u64 != size {
        error!("File size mismatch for {} from system {}", filename, system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "File size mismatch".to_string(),
            data: None,
        }));
    }

    // Validate BLAKE3
    let computed_blake3 = blake3::hash(&file_content).to_hex().to_string();
    if computed_blake3 != blake3 {
        error!("BLAKE3 mismatch for {} from system {}", filename, system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "BLAKE3 verification failed".to_string(),
            data: None,
        }));
    }

    // Save file
    let file_path = Path::new(&backup_dir).join(&filename);
    if let Err(e) = fs::write(&file_path, &file_content) {
        error!("Failed to save file {}: {}", file_path.display(), e);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: format!("Failed to save file: {}", e),
            data: None,
        }));
    }

    // Update file registry
    let upload_time = chrono::Utc::now().to_rfc3339();
    let file_info = FileInfo {
        filename: filename.clone(),
        blake3: blake3.clone(), // blake3 checksum
        size,
        upload_time,
    };

    {
        let mut registry = state.file_registry.write().await;
        let system_files = registry.entry(system_ip.clone()).or_insert_with(HashMap::new);
        system_files.insert(filename.clone(), file_info);
    }

    info!("File {} uploaded successfully from system {}", filename, system_ip);

    Ok(warp::reply::json(&ApiResponse::<String> {
        success: true,
        message: "File uploaded successfully".to_string(),
        data: None,
    }))
}

async fn start_chunked_upload(
    state: Arc<ServerState>,
    token: String,
    system_ip: String,
    filename: String,
    blake3: String,
    size: u64,
    chunk_size: u64,
) -> Result<impl Reply, Rejection> {
    // Validate authentication token
    if !state.validate_token(&token, &system_ip) {
        error!("Invalid auth token for system: {}", system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "Invalid authentication token".to_string(),
            data: None,
        }));
    }

    // Get backup directory
    let backup_dir = match state.get_backup_dir(&system_ip) {
        Some(dir) => dir,
        None => {
            error!("Backup directory not found for system: {}", system_ip);
            return Ok(warp::reply::json(&ApiResponse::<String> {
                success: false,
                message: "System not configured".to_string(),
                data: None,
            }));
        }
    };

    // Ensure backup directory exists
    if let Err(e) = fs::create_dir_all(&backup_dir) {
        error!("Failed to create backup directory {}: {}", backup_dir, e);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: format!("Failed to create backup directory: {}", e),
            data: None,
        }));
    }

    // Create temporary file
    let upload_id = Uuid::new_v4().to_string();
    let temp_file_path = Path::new(&backup_dir).join(format!("{}.tmp", upload_id));

    let upload_info = ChunkedUploadInfo {
        system_ip: system_ip.clone(),
        filename: filename.clone(),
        expected_blake3: blake3.clone(),
        expected_size: size,
        chunk_size,
        temp_file_path: temp_file_path.to_string_lossy().to_string(),
        received_chunks: HashMap::new(),
        total_received: 0,
    };

    {
        let mut uploads = state.chunked_uploads.write().await;
        uploads.insert(upload_id.clone(), upload_info);
    }

    info!("Started chunked upload session {} for file {} from system {}", upload_id, filename, system_ip);

    Ok(warp::reply::json(&ApiResponse {
        success: true,
        message: "Chunked upload session started".to_string(),
        data: Some(upload_id),
    }))
}

async fn upload_chunk(
    state: Arc<ServerState>,
    token: String,
    upload_id: String,
    filename: String,
    chunk_number: usize,
    chunk_data: bytes::Bytes,
) -> Result<impl Reply, Rejection> {
    // Validate authentication token
    let system_ip = {
        let uploads = state.chunked_uploads.read().await;
        match uploads.get(&upload_id) {
            Some(upload_info) => {
                if upload_info.filename != filename {
                    error!("Filename mismatch for upload {}", upload_id);
                    return Ok(warp::reply::json(&ApiResponse::<String> {
                        success: false,
                        message: "Filename mismatch".to_string(),
                        data: None,
                    }));
                }
                upload_info.system_ip.clone()
            }
            None => {
                error!("Invalid upload ID: {}", upload_id);
                return Ok(warp::reply::json(&ApiResponse::<String> {
                    success: false,
                    message: "Invalid upload ID".to_string(),
                    data: None,
                }));
            }
        }
    };

    if !state.validate_token(&token, &system_ip) {
        error!("Invalid auth token for system: {}", system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "Invalid authentication token".to_string(),
            data: None,
        }));
    }

    // Get upload info and write chunk
    {
        let mut uploads = state.chunked_uploads.write().await;
        if let Some(upload_info) = uploads.get_mut(&upload_id) {
            // Check if this chunk has already been received
            if upload_info.received_chunks.contains_key(&chunk_number) {
                warn!("Chunk {} already received for upload {}", chunk_number, upload_id);
                return Ok(warp::reply::json(&ApiResponse::<String> {
                    success: true,
                    message: "Chunk already received".to_string(),
                    data: None,
                }));
            }

            // Write chunk to temporary file
            let mut file = match tokio::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(&upload_info.temp_file_path)
                .await
            {
                Ok(f) => f,
                Err(e) => {
                    error!("Failed to open temp file for upload {}: {}", upload_id, e);
                    return Ok(warp::reply::json(&ApiResponse::<String> {
                        success: false,
                        message: format!("Failed to open temp file: {}", e),
                        data: None,
                    }));
                }
            };

            if let Err(e) = file.write_all(&chunk_data).await {
                error!("Failed to write chunk {} for upload {}: {}", chunk_number, upload_id, e);
                return Ok(warp::reply::json(&ApiResponse::<String> {
                    success: false,
                    message: format!("Failed to write chunk: {}", e),
                    data: None,
                }));
            }

            // Update received information
            upload_info.received_chunks.insert(chunk_number, chunk_data.len() as u64);
            upload_info.total_received += chunk_data.len() as u64;

            info!("Received chunk {} for upload {} ({} bytes)", chunk_number, upload_id, chunk_data.len());
        } else {
            error!("Upload session not found: {}", upload_id);
            return Ok(warp::reply::json(&ApiResponse::<String> {
                success: false,
                message: "Upload session not found".to_string(),
                data: None,
            }));
        }
    }

    Ok(warp::reply::json(&ApiResponse::<String> {
        success: true,
        message: "Chunk uploaded successfully".to_string(),
        data: None,
    }))
}

async fn complete_upload(
    state: Arc<ServerState>,
    token: String,
    upload_id: String,
    filename: String,
) -> Result<impl Reply, Rejection> {
    // Get upload information
    let upload_info = {
        let uploads = state.chunked_uploads.read().await;
        match uploads.get(&upload_id) {
            Some(info) => {
                if info.filename != filename {
                    error!("Filename mismatch for upload {}", upload_id);
                    return Ok(warp::reply::json(&ApiResponse::<String> {
                        success: false,
                        message: "Filename mismatch".to_string(),
                        data: None,
                    }));
                }
                info.clone()
            }
            None => {
                error!("Upload session not found: {}", upload_id);
                return Ok(warp::reply::json(&ApiResponse::<String> {
                    success: false,
                    message: "Upload session not found".to_string(),
                    data: None,
                }));
            }
        }
    };

    // Validate authentication token
    if !state.validate_token(&token, &upload_info.system_ip) {
        error!("Invalid auth token for system: {}", upload_info.system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "Invalid authentication token".to_string(),
            data: None,
        }));
    }

    // Validate file size
    if upload_info.total_received != upload_info.expected_size {
        error!("File size mismatch for upload {}. Expected: {}, Received: {}", 
               upload_id, upload_info.expected_size, upload_info.total_received);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "File size mismatch".to_string(),
            data: None,
        }));
    }

    // Validate BLAKE3
    let temp_file_content = match tokio::fs::read(&upload_info.temp_file_path).await {
        Ok(content) => content,
        Err(e) => {
            error!("Failed to read temp file for upload {}: {}", upload_id, e);
            return Ok(warp::reply::json(&ApiResponse::<String> {
                success: false,
                message: format!("Failed to read temp file: {}", e),
                data: None,
            }));
        }
    };

    let computed_blake3 = blake3::hash(&temp_file_content).to_hex().to_string();
    if computed_blake3 != upload_info.expected_blake3 {
        error!("BLAKE3 mismatch for upload {}. Expected: {}, Computed: {}", 
               upload_id, upload_info.expected_blake3, computed_blake3);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "BLAKE3 verification failed".to_string(),
            data: None,
        }));
    }

    // Get backup directory
    let backup_dir = match state.get_backup_dir(&upload_info.system_ip) {
        Some(dir) => dir,
        None => {
            error!("Backup directory not found for system: {}", upload_info.system_ip);
            return Ok(warp::reply::json(&ApiResponse::<String> {
                success: false,
                message: "System not configured".to_string(),
                data: None,
            }));
        }
    };

    // Move temporary file to final location
    let final_file_path = Path::new(&backup_dir).join(&filename);
    if let Err(e) = tokio::fs::rename(&upload_info.temp_file_path, &final_file_path).await {
        error!("Failed to move temp file for upload {}: {}", upload_id, e);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: format!("Failed to move temp file: {}", e),
            data: None,
        }));
    }

    // Update file registry
    let upload_time = chrono::Utc::now().to_rfc3339();
    let file_info = FileInfo {
        filename: filename.clone(),
        blake3: upload_info.expected_blake3.clone(), // blake3 checksum
        size: upload_info.expected_size,
        upload_time,
    };

    {
        let mut registry = state.file_registry.write().await;
        let system_files = registry.entry(upload_info.system_ip.clone()).or_insert_with(HashMap::new);
        system_files.insert(filename.clone(), file_info);
    }

    // Clean up upload session
    {
        let mut uploads = state.chunked_uploads.write().await;
        uploads.remove(&upload_id);
    }

    info!("Completed chunked upload {} for file {} from system {}", upload_id, filename, upload_info.system_ip);

    Ok(warp::reply::json(&ApiResponse::<String> {
        success: true,
        message: "File upload completed successfully".to_string(),
        data: None,
    }))
}

async fn query_blake3(
    state: Arc<ServerState>,
    token: String,
    system_ip: String,
    filename: String,
) -> Result<impl Reply, Rejection> {
    // Validate authentication token
    if !state.validate_token(&token, &system_ip) {
        error!("Invalid auth token for system: {}", system_ip);
        return Ok(warp::reply::json(&ApiResponse::<String> {
            success: false,
            message: "Invalid authentication token".to_string(),
            data: None,
        }));
    }

    // Query file information
    let registry = state.file_registry.read().await;
    if let Some(system_files) = registry.get(&system_ip) {
        if let Some(file_info) = system_files.get(&filename) {
            return Ok(warp::reply::json(&ApiResponse {
                success: true,
                message: "File found".to_string(),
                data: Some(file_info.blake3.clone()),
            }));
        }
    }

    // Check if file exists on disk
    if let Some(backup_dir) = state.get_backup_dir(&system_ip) {
        let file_path = Path::new(&backup_dir).join(&filename);
        if file_path.exists() {
            if let Ok(content) = fs::read(&file_path) {
                let blake3_hash = blake3::hash(&content).to_hex().to_string();
                return Ok(warp::reply::json(&ApiResponse {
                    success: true,
                    message: "File found on disk".to_string(),
                    data: Some(blake3_hash),
                }));
            }
        }
    }

    Ok(warp::reply::json(&ApiResponse::<String> {
        success: false,
        message: "File not found".to_string(),
        data: None,
    }))
}

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::Builder::from_default_env()
        .filter_level(log::LevelFilter::Info)
        .init();

    info!("Starting db-sync-server...");

    // Load configuration
    let config = load_config("server.config.json").await?;
    info!("Loaded configuration for {} systems", config.data.len());

    // Create server state
    let state = Arc::new(ServerState::new(config));
    let listen_addr = state.config.listen_addr.clone();

    // Health check endpoint
    let health_route = warp::path("health")
        .and(warp::get())
        .and_then(health_check);

    // File upload endpoint
    let state_clone1 = state.clone();
    let upload_route = warp::path("api")
        .and(warp::path("upload"))
        .and(warp::post())
        .and(warp::header::<String>("authorization"))
        .and(warp::query::<HashMap<String, String>>())
        .and(warp::body::bytes())
        .and(warp::any().map(move || state_clone1.clone()))
        .and_then(|token: String, mut query: HashMap<String, String>, file_content: bytes::Bytes, state: Arc<ServerState>| async move {
            let system_ip = query.remove("system_ip").unwrap_or_default();
            let filename = query.remove("filename").unwrap_or_default();
            let size = query.remove("size").and_then(|s| s.parse().ok()).unwrap_or(0);
            let blake3 = query.remove("blake3").unwrap_or_default();
            
            upload_file(state, token, system_ip, filename, blake3, size, file_content).await
        });

    // BLAKE3 查询接口
    let state_clone2 = state.clone();
    let blake3_route = warp::path("api")
        .and(warp::path("blake3"))
        .and(warp::post())
        .and(warp::header::<String>("authorization"))
        .and(warp::query::<HashMap<String, String>>())
        .and(warp::any().map(move || state_clone2.clone()))
        .and_then(|token: String, mut query: HashMap<String, String>, state: Arc<ServerState>| async move {
            let system_ip = query.remove("system_ip").unwrap_or_default();
            let filename = query.remove("filename").unwrap_or_default();

            query_blake3(state, token, system_ip, filename).await // Keep function name unchanged
        });

    // Start chunked upload endpoint
    let state_clone3 = state.clone();
    let start_chunked_route = warp::path("api")
        .and(warp::path("upload-chunked"))
        .and(warp::post())
        .and(warp::header::<String>("authorization"))
        .and(warp::query::<HashMap<String, String>>())
        .and(warp::any().map(move || state_clone3.clone()))
        .and_then(|token: String, mut query: HashMap<String, String>, state: Arc<ServerState>| async move {
            let system_ip = query.remove("system_ip").unwrap_or_default();
            let filename = query.remove("filename").unwrap_or_default();
            let blake3 = query.remove("blake3").unwrap_or_default();
            let size = query.remove("size").and_then(|s| s.parse().ok()).unwrap_or(0);
            let chunk_size = query.remove("chunk_size").and_then(|s| s.parse().ok()).unwrap_or(0);

            start_chunked_upload(state, token, system_ip, filename, blake3, size, chunk_size).await
        });

    // Upload chunk endpoint
    let state_clone4 = state.clone();
    let upload_chunk_route = warp::path("api")
        .and(warp::path("upload-chunk"))
        .and(warp::post())
        .and(warp::header::<String>("authorization"))
        .and(warp::query::<HashMap<String, String>>())
        .and(warp::body::bytes())
        .and(warp::any().map(move || state_clone4.clone()))
        .and_then(|token: String, mut query: HashMap<String, String>, chunk_data: bytes::Bytes, state: Arc<ServerState>| async move {
            let upload_id = query.remove("upload_id").unwrap_or_default();
            let filename = query.remove("filename").unwrap_or_default();
            let chunk_number = query.remove("chunk_number").and_then(|s| s.parse().ok()).unwrap_or(0);

            upload_chunk(state, token, upload_id, filename, chunk_number, chunk_data).await
        });

    // Complete upload endpoint
    let state_clone5 = state.clone();
    let complete_upload_route = warp::path("api")
        .and(warp::path("complete-upload"))
        .and(warp::post())
        .and(warp::header::<String>("authorization"))
        .and(warp::query::<HashMap<String, String>>())
        .and(warp::any().map(move || state_clone5.clone()))
        .and_then(|token: String, mut query: HashMap<String, String>, state: Arc<ServerState>| async move {
            let upload_id = query.remove("upload_id").unwrap_or_default();
            let filename = query.remove("filename").unwrap_or_default();

            complete_upload(state, token, upload_id, filename).await
        });

    let routes = health_route
        .or(upload_route)
        .or(blake3_route)
        .or(start_chunked_route)
        .or(upload_chunk_route)
        .or(complete_upload_route)
        .with(warp::log("db_sync_server"));

    info!("Server listening on: {}", listen_addr);

    // Start server
    let addr: std::net::SocketAddr = listen_addr.parse()?;
    warp::serve(routes).run(addr).await;

    Ok(())
}