forked from datrs/hyperswarm-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# hyperswarm-bootstrap | ||
|
||
Simple binary to run a bootstrap node. | ||
|
||
``` | ||
$ cargo run -- --help | ||
hyperswarm-bootstrap | ||
Run a Hyperswarm bootstrap node | ||
USAGE: | ||
hyperswarm-bootstrap [OPTIONS] | ||
OPTIONS: | ||
-a, --address <ADDRESS> Local address to bind bootstrap node on [default: 0.0.0.0:49737] | ||
-b, --bootstrap <BOOTSTRAP> Bootstrap addresses | ||
-h, --help Print help information | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,80 @@ | ||
use anyhow::Context; | ||
use async_std::task; | ||
use clap::Clap; | ||
use clap::Parser; | ||
use log::*; | ||
use std::io; | ||
use std::net::SocketAddr; | ||
use std::net::{SocketAddr, ToSocketAddrs}; | ||
|
||
use hyperswarm::run_bootstrap_node; | ||
use hyperswarm::{BootstrapNode, DhtConfig}; | ||
|
||
/// Options for the storage daemon | ||
#[derive(Clap, Debug)] | ||
/// Run a Hyperswarm bootstrap node. | ||
#[derive(Parser, Debug)] | ||
struct Options { | ||
/// Local address to bind bootstrap node on. | ||
#[clap(short, long)] | ||
address: Option<SocketAddr>, | ||
#[clap(short, long, default_value = "0.0.0.0:49737")] | ||
address: SocketAddr, | ||
|
||
/// Bootstrap addresses | ||
#[clap(short, long)] | ||
bootstrap: Vec<SocketAddr>, | ||
bootstrap: Vec<String>, | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
env_logger::init(); | ||
task::block_on(async_main()) | ||
match task::block_on(async_main()) { | ||
Err(e) => { | ||
debug!("{:?}", e); | ||
eprintln!("Error: {}", e); | ||
std::process::exit(1); | ||
} | ||
Ok(()) => Ok(()), | ||
} | ||
} | ||
|
||
async fn async_main() -> io::Result<()> { | ||
async fn async_main() -> anyhow::Result<()> { | ||
let opts: Options = Options::parse(); | ||
let (addr, task) = run_bootstrap_node(opts.address).await?; | ||
println!("listening on {}", addr); | ||
|
||
let config = DhtConfig::default() | ||
.set_bootstrap_nodes(&opts.bootstrap[..]) | ||
.set_ephemeral(false); | ||
|
||
let mut config = if opts.bootstrap.len() > 0 { | ||
let mut addrs = vec![]; | ||
for addr in opts.bootstrap.iter() { | ||
for addr in addr | ||
.to_socket_addrs() | ||
.with_context(|| format!("Invalid socket address \"{}\"", addr))? | ||
{ | ||
if addr.is_ipv4() { | ||
addrs.push(addr); | ||
} | ||
} | ||
} | ||
config.set_bootstrap_nodes(&addrs[..]) | ||
} else { | ||
config | ||
}; | ||
|
||
let bootstrap_addrs: Vec<String> = config | ||
.bootstrap_nodes() | ||
.map(|v| v.clone().iter().map(|s| s.to_string()).collect()) | ||
.unwrap_or_else(|| { | ||
hyperswarm::DEFAULT_BOOTSTRAP | ||
.to_vec() | ||
.iter() | ||
.map(|s| s.to_string()) | ||
.collect() | ||
}); | ||
|
||
let node = BootstrapNode::new(config, Some(opts.address)); | ||
let (addr, task) = node.run().await?; | ||
|
||
println!("DHT listening on {}", addr); | ||
debug!( | ||
"Bootstrapping DHT via nodes: {}", | ||
bootstrap_addrs.join(", ") | ||
); | ||
|
||
task.await?; | ||
Ok(()) | ||
} |