vpncloud/src/main.rs

93 lines
2.6 KiB
Rust
Raw Normal View History

2015-11-19 15:34:20 +00:00
#[macro_use] extern crate log;
extern crate time;
extern crate docopt;
extern crate rustc_serialize;
2015-11-19 18:29:42 +00:00
mod util;
2015-11-19 15:34:20 +00:00
mod udpmessage;
mod tapdev;
mod ethernet;
mod ethcloud;
use time::Duration;
use docopt::Docopt;
2015-11-19 18:29:42 +00:00
use ethcloud::{Error, Token, EthCloud};
2015-11-19 15:34:20 +00:00
2015-11-19 18:29:42 +00:00
//FIXME: Send peer list in several packets when too large. The current behaviour panics at about
// 10000 peers.
//TODO: Implement IPv6
2015-11-19 19:51:53 +00:00
//TODO: Encryption
//TODO: Call close
2015-11-19 18:29:42 +00:00
2015-11-19 15:34:20 +00:00
struct SimpleLogger;
impl log::Log for SimpleLogger {
fn enabled(&self, _metadata: &log::LogMetadata) -> bool {
true
}
fn log(&self, record: &log::LogRecord) {
if self.enabled(record.metadata()) {
println!("{} - {}", record.level(), record.args());
}
}
}
static USAGE: &'static str = "
Usage:
2015-11-19 16:23:36 +00:00
ethcloud [options]
2015-11-19 15:34:20 +00:00
Options:
2015-11-19 16:23:36 +00:00
-d <device>, --device <device> Name of the tap device [default: ethcloud%d]
-l <listen>, --listen <listen> Address to listen on [default: 0.0.0.0:3210]
-t <token>, --token <token> Token that identifies the network [default: 0]
-c <connect>, --connect <connect> List of peers (addr:port) to connect to
--peer-timeout <peer_timeout> Peer timeout in seconds [default: 300]
--mac-timeout <mac_timeout> Mac table entry timeout in seconds [default: 60]
-v, --verbose Log verbosely
-q, --quiet Only print error messages
2015-11-19 15:34:20 +00:00
";
#[derive(RustcDecodable, Debug)]
struct Args {
2015-11-19 16:23:36 +00:00
flag_device: String,
flag_listen: String,
flag_token: Token,
flag_connect: Vec<String>,
flag_peer_timeout: usize,
flag_mac_timeout: usize,
flag_verbose: bool,
flag_quiet: bool
2015-11-19 15:34:20 +00:00
}
fn main() {
let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
log::set_logger(|max_log_level| {
2015-11-19 16:23:36 +00:00
assert!(!args.flag_verbose || !args.flag_quiet);
2015-11-19 15:34:20 +00:00
if args.flag_verbose {
max_log_level.set(log::LogLevelFilter::Debug);
2015-11-19 16:23:36 +00:00
} else if args.flag_quiet {
max_log_level.set(log::LogLevelFilter::Error);
2015-11-19 15:34:20 +00:00
} else {
max_log_level.set(log::LogLevelFilter::Info);
}
Box::new(SimpleLogger)
}).unwrap();
debug!("Args: {:?}", args);
2015-11-19 16:11:59 +00:00
let tapcloud = EthCloud::new(
2015-11-19 15:34:20 +00:00
&args.flag_device,
args.flag_listen,
args.flag_token,
Duration::seconds(args.flag_mac_timeout as i64),
Duration::seconds(args.flag_peer_timeout as i64)
);
for addr in args.flag_connect {
tapcloud.connect(&addr as &str).expect("Failed to send");
}
2015-11-19 19:51:53 +00:00
tapcloud.run()
2015-11-19 15:34:20 +00:00
}