Skip to content

Commit

Permalink
update deps and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
naim94a committed Mar 6, 2024
1 parent 94e5c86 commit 3c53840
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 315 deletions.
823 changes: 537 additions & 286 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions Cargo.toml
Expand Up @@ -9,15 +9,24 @@ edition = "2021"
lto = "fat"

[dependencies]
serde = {version = "1.0", features = ["derive"]}
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3"
warp = {version = "0.3", default-features = false}
tokio = {version = "1.19", features = ["macros", "io-util", "net", "time", "rt-multi-thread", "fs", "sync", "signal"]}
warp = { version = "^0.3.6", default-features = false }
tokio = { version = "1.36", features = [
"macros",
"io-util",
"net",
"time",
"rt-multi-thread",
"fs",
"sync",
"signal",
] }
binascii = "0.1"
toml = "0.5"
clap = "3.2"
log = {version = "0.4", features = ["release_max_level_info"]}
toml = "0.8.10"
clap = "4.5"
log = { version = "0.4", features = ["release_max_level_info"] }
fern = "0.6"
serde_json = "1.0"
async-compression = {version = "^0.3.8", features = ["bzip2", "tokio"]}
async-compression = { version = "^0.4.6", features = ["bzip2", "tokio"] }
chrono = "0.4"
2 changes: 1 addition & 1 deletion rustfmt.toml
@@ -1,6 +1,6 @@
edition = "2018"
brace_style = "PreferSameLine"
fn_args_layout = "compressed"
fn_params_layout = "compressed"
force_multiline_blocks = true
max_width = 125
overflow_delimited_expr = true
Expand Down
8 changes: 4 additions & 4 deletions src/config.rs
Expand Up @@ -61,15 +61,15 @@ impl std::fmt::Display for ConfigError {
impl std::error::Error for ConfigError {}

impl Configuration {
pub fn load(data: &[u8]) -> Result<Configuration, toml::de::Error> {
toml::from_slice(data)
pub fn load(data: &str) -> Result<Configuration, toml::de::Error> {
toml::from_str(data)
}

pub fn load_file(path: &str) -> Result<Configuration, ConfigError> {
match std::fs::read(path) {
match std::fs::read_to_string(path) {
Err(e) => Err(ConfigError::IOError(e)),
Ok(data) => {
match Self::load(data.as_slice()) {
match Self::load(&data) {
Ok(cfg) => Ok(cfg),
Err(e) => Err(ConfigError::ParseError(e)),
}
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
@@ -1,3 +1,4 @@
use clap::ValueHint;
use log::{error, info, trace, warn};

mod config;
Expand Down Expand Up @@ -50,20 +51,20 @@ fn setup_logging(cfg: &Configuration) {

#[tokio::main]
async fn main() {
let parser = clap::App::new(env!("CARGO_PKG_NAME"))
let parser = clap::Command::new(env!("CARGO_PKG_NAME"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
.version(env!("CARGO_PKG_VERSION"))
.arg(
clap::Arg::new("config")
.takes_value(true)
.short('c')
.value_hint(ValueHint::FilePath)
.help("Configuration file to load.")
.required(true),
);

let matches = parser.get_matches();
let cfg_path = matches.value_of("config").unwrap();
let cfg_path = matches.get_one::<String>("config").unwrap();

let cfg = match Configuration::load_file(cfg_path) {
Ok(v) => std::sync::Arc::new(v),
Expand Down
10 changes: 7 additions & 3 deletions src/server.rs
Expand Up @@ -363,7 +363,7 @@ impl UDPTracker {
Err(err) => {
debug!("failed to send a packet: {}", err);
Err(err)
},
}
Ok(sz) => Ok(sz),
}
}
Expand All @@ -375,7 +375,9 @@ impl UDPTracker {
if pack_into(&mut payload, &UDPResponseHeader {
transaction_id: header.transaction_id,
action: Actions::Error,
}).is_ok() {
})
.is_ok()
{
let msg_bytes = Vec::from(error_msg.as_bytes());
payload.extend(msg_bytes);

Expand Down Expand Up @@ -414,7 +416,9 @@ mod tests {

assert!(pack_into(&mut payload, &mystruct).is_ok());
assert_eq!(payload.as_slice().len(), 16);
assert_eq!(payload.as_slice(), &[0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0, 0, 0, 0, 0, 1, 47, 203]);
assert_eq!(payload.as_slice(), &[
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0, 0, 0, 0, 0, 1, 47, 203
]);
}

#[test]
Expand Down
16 changes: 6 additions & 10 deletions src/tracker.rs
Expand Up @@ -75,9 +75,7 @@ impl std::convert::From<&[u8]> for InfoHash {

impl From<[u8; 20]> for InfoHash {
fn from(info_hash: [u8; 20]) -> Self {
InfoHash {
info_hash,
}
InfoHash { info_hash }
}
}

Expand Down Expand Up @@ -383,7 +381,7 @@ impl TorrentTracker {
Err(ref err) => {
error!("failed to read lines! {}", err);
continue;
},
}
};
let row: DatabaseRow = match serde_json::from_str(&line) {
Ok(v) => v,
Expand Down Expand Up @@ -411,7 +409,7 @@ impl TorrentTracker {
std::collections::btree_map::Entry::Vacant(ve) => {
ve.insert(TorrentEntry::new());
Ok(())
},
}
std::collections::btree_map::Entry::Occupied(_entry) => Err(()),
}
}
Expand Down Expand Up @@ -453,9 +451,7 @@ impl TorrentTracker {
&self, info_hash: &InfoHash, remote_addr: &std::net::SocketAddr,
) -> Option<Vec<std::net::SocketAddr>> {
let read_lock = self.database.torrent_peers.read().await;
read_lock
.get(info_hash)
.map(|entry| entry.get_peers(remote_addr))
read_lock.get(info_hash).map(|entry| entry.get_peers(remote_addr))
}

pub async fn update_torrent_and_get_stats(
Expand Down Expand Up @@ -503,7 +499,7 @@ impl TorrentTracker {

let db_lock = self.database.torrent_peers.read().await;

let db: &BTreeMap<InfoHash, TorrentEntry> = &*db_lock;
let db: &BTreeMap<InfoHash, TorrentEntry> = &db_lock;
let mut tmp = Vec::with_capacity(4096);

for row in db {
Expand All @@ -525,7 +521,7 @@ impl TorrentTracker {

async fn cleanup(&self) {
let mut lock = self.database.torrent_peers.write().await;
let db: &mut BTreeMap<InfoHash, TorrentEntry> = &mut *lock;
let db: &mut BTreeMap<InfoHash, TorrentEntry> = &mut lock;
let mut torrents_to_remove = Vec::new();

for (k, v) in db.iter_mut() {
Expand Down
2 changes: 1 addition & 1 deletion src/webserver.rs
Expand Up @@ -77,7 +77,7 @@ fn authenticate(tokens: HashMap<String, String>) -> impl Filter<Extract = (), Er
token: Option<String>,
}

let tokens: HashSet<String> = tokens.into_iter().map(|(_, v)| v).collect();
let tokens: HashSet<String> = tokens.into_values().collect();

let tokens = Arc::new(tokens);
warp::filters::any::any()
Expand Down

0 comments on commit 3c53840

Please sign in to comment.