Skip to main content

gittorrentd/
gittorrentd.rs

1//! GitTorrent daemon binary
2
3use clap::Parser;
4use hyprstream_p2p::{
5    daemon::{run_daemon, DaemonConfig},
6    init_tracing,
7};
8use std::path::PathBuf;
9use tracing::info;
10
11#[derive(Parser)]
12#[command(name = "gittorrentd")]
13#[command(about = "GitTorrent daemon for sharing git repositories over P2P")]
14struct Cli {
15    /// Configuration file path
16    #[arg(short, long)]
17    config: Option<String>,
18
19    /// Repositories directory
20    #[arg(short, long)]
21    repositories: Option<PathBuf>,
22
23    /// Git protocol port
24    #[arg(short, long, default_value = "9418")]
25    git_port: u16,
26
27    /// P2P/DHT listen port (0 = random port)
28    #[arg(short = 'p', long, default_value = "0")]
29    p2p_port: u16,
30
31    /// Bind address
32    #[arg(short, long, default_value = "127.0.0.1")]
33    bind_address: String,
34
35    /// Disable Git server
36    #[arg(long)]
37    no_git_server: bool,
38
39    /// Verbose logging
40    #[arg(short, long)]
41    verbose: bool,
42
43    /// Bootstrap nodes for P2P network (comma-separated)
44    #[arg(long)]
45    bootstrap: Option<String>,
46
47    /// Run in standalone mode (don't attempt to connect to existing network)
48    #[arg(long)]
49    standalone: bool,
50}
51
52#[tokio::main]
53async fn main() -> hyprstream_p2p::Result<()> {
54    let cli = Cli::parse();
55
56    // Initialize tracing
57    init_tracing()?;
58
59    info!("Starting GitTorrent daemon v{}", hyprstream_p2p::VERSION);
60
61    // Create daemon configuration
62    let mut config = DaemonConfig::default();
63
64    if let Some(repos_dir) = cli.repositories {
65        config.repositories_dir = repos_dir;
66    }
67
68    config.git_port = cli.git_port;
69    config.service.bind_address = cli.bind_address;
70    config.service.p2p_port = cli.p2p_port;
71    config.enable_git_server = !cli.no_git_server;
72
73    // Parse bootstrap nodes if provided (but not in standalone mode)
74    if cli.standalone {
75        config.service.bootstrap_nodes.clear();
76        info!("Standalone mode enabled - no bootstrap nodes will be used");
77    } else if let Some(bootstrap_str) = cli.bootstrap {
78        config.service.bootstrap_nodes = bootstrap_str
79            .split(',')
80            .map(|s| s.trim().to_owned())
81            .collect();
82    }
83
84    info!("Daemon configuration:");
85    info!("  Repositories: {}", config.repositories_dir.display());
86    info!(
87        "  Git server: {} (port {})",
88        if config.enable_git_server {
89            "enabled"
90        } else {
91            "disabled"
92        },
93        config.git_port
94    );
95    info!("  Bind address: {}", config.service.bind_address);
96    info!(
97        "  P2P port: {} ({})",
98        config.service.p2p_port,
99        if config.service.p2p_port == 0 {
100            "random"
101        } else {
102            "fixed"
103        }
104    );
105    info!("  Bootstrap nodes: {:?}", config.service.bootstrap_nodes);
106
107    if cli.standalone {
108        info!("  Mode: Standalone (first node in network)");
109        info!("  Other nodes can connect using this node's listen addresses as bootstrap");
110    } else if config.service.bootstrap_nodes.is_empty() {
111        info!("  Note: No bootstrap nodes provided, running in standalone mode");
112        info!("  Use --bootstrap to specify P2P bootstrap nodes or --standalone for first node");
113    } else {
114        info!(
115            "  P2P networking enabled with {} bootstrap nodes",
116            config.service.bootstrap_nodes.len()
117        );
118    }
119
120    // Run the daemon
121    run_daemon(config).await?;
122
123    Ok(())
124}