diff --git a/Cargo.toml b/Cargo.toml index 3a12307..8aa2d23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ serde = "1.0.80" bincode = "1.0.1" serde_derive = "1.0.80" actix-web = "0.7.13" -binascii = "0.1.2" \ No newline at end of file +binascii = "0.1.2" +toml = "0.4.8" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..423a6d2 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,71 @@ +use std; +use std::collections::HashMap; +use toml; +use serde; +pub use tracker::TrackerMode; + +#[derive(Deserialize)] +pub struct UDPConfig { + bind_address: String, + mode: TrackerMode, +} + +#[derive(Deserialize)] +pub struct HTTPConfig { + bind_address: String, + access_tokens: HashMap, +} + +#[derive(Deserialize)] +pub struct Configuration { + udp: UDPConfig, + http: Option, +} + +#[derive(Debug)] +pub enum ConfigError { + IOError(std::io::Error), + ParseError(toml::de::Error), +} + +impl std::fmt::Display for ConfigError { + fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + use std::fmt::Display; + match self { + ConfigError::IOError(e) => e.fmt(formatter), + ConfigError::ParseError(e) => e.fmt(formatter), + } + } +} +impl std::error::Error for ConfigError {} + + +impl Configuration { + pub fn load(data: &[u8]) -> Result { + toml::from_slice(data) + } + + pub fn load_file(path: &str) -> Result { + match std::fs::read(path) { + Err(e) => Err(ConfigError::IOError(e)), + Ok(data) => { + match Self::load(data.as_slice()) { + Ok(cfg) => Ok(cfg), + Err(e) => Err(ConfigError::ParseError(e)), + } + } + } + } +} + +impl Default for Configuration { + fn default() -> Configuration { + Configuration{ + udp: UDPConfig{ + bind_address: String::from("0.0.0.0:6969"), + mode: TrackerMode::DynamicMode, + }, + http: None, + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7137b2d..33adca5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,25 @@ extern crate serde; #[macro_use] extern crate serde_derive; extern crate actix_web; extern crate binascii; +extern crate toml; mod server; mod tracker; mod stackvec; mod webserver; +mod config; +use config::Configuration; fn main() { + let cfg = match Configuration::load_file("udpt.toml") { + Ok(v) => v, + Err(e) => { + eprintln!("failed to open configuration: {}", e); + return; + } + }; + + let tracker = std::sync::Arc::new(tracker::TorrentTracker::new()); // start http server: diff --git a/src/tracker.rs b/src/tracker.rs index 79ebaa5..ac84042 100644 --- a/src/tracker.rs +++ b/src/tracker.rs @@ -2,15 +2,19 @@ use std; use server::Events; +#[derive(Deserialize)] pub enum TrackerMode { /// In static mode torrents are tracked only if they were added ahead of time. + #[serde(rename="static")] StaticMode, /// In dynamic mode, torrents are tracked being added ahead of time. + #[serde(rename="dynamic")] DynamicMode, /// Tracker will only serve authenticated peers. + #[serde(rename="private")] PrivateMode, }