Skip to content

Commit

Permalink
feat: pt filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ztelliot committed Apr 12, 2024
1 parent c4922eb commit 1ca7b18
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env_logger = "0.11.3"
log = "0.4.21"
async-trait = "0.1.79"
clap = { version = "4.5.4", features = ["derive"] }
regex = "1.10.4"

[profile.opt]
inherits = "release"
Expand Down
1 change: 1 addition & 0 deletions src/backend/qb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl Backend for QBitBackend {
name: t.get("name").unwrap().as_str().unwrap().to_string(),
hash: t.get("hash").unwrap().as_str().unwrap().to_string(),
size: t.get("size").unwrap().as_u64().unwrap(),
tracker: t.get("tracker").unwrap().as_str().unwrap().to_string(),
}).collect())
}

Expand Down
21 changes: 19 additions & 2 deletions src/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use regex::Regex;
use std::time::Instant;

use anyhow::Result;
Expand All @@ -8,6 +9,8 @@ use crate::peer::BannedPeer;
use crate::rules::preload::PREDEFINED_RULES;
use crate::rules::Rule;

const PT_KEYWORDS: [&str; 5] = ["?passkey=", "?authkey=", "?secure=", "?credential=", "private"];

struct Statistic {
pub torrents: u64,
pub peers: u64,
Expand All @@ -19,17 +22,19 @@ pub struct Daemon {
banned: Vec<BannedPeer>,
rules: Vec<Rule>,
scan_time: u64,
pt: bool,
clear: bool,
}

impl Daemon {
pub fn new(backend: Box<dyn Backend>, scan: u64, clear: bool) -> Self {
pub fn new(backend: Box<dyn Backend>, scan: u64, pt: bool, clear: bool) -> Self {
let rules = PREDEFINED_RULES.clone();
Daemon {
backend,
banned: Vec::new(),
rules,
scan_time: scan,
pt,
clear,
}
}
Expand All @@ -46,13 +51,25 @@ impl Daemon {
self.backend.ban_clear().await?;
info!("[startup] jail cleared.");
}
let re = Regex::new(r"([a-zA-Z0-9]{32})").unwrap();
loop {
let mut flag = false;
let torrents = self.backend.get_uploading_torrents().await?;
stat.torrents = torrents.len() as u64;
stat.peers = 0;
for torrent in torrents {
debug!("Torrent: {}({})", torrent.name, torrent.hash);
if !self.pt && !torrent.tracker.is_empty() {
let lower_tracker = torrent.tracker.to_lowercase();
if PT_KEYWORDS.iter().any(|&keyword| lower_tracker.contains(keyword)) || re.is_match(&lower_tracker) {
debug!("Private tracker torrent: {}({})", torrent.name, torrent.hash);
continue;
} else {
debug!("Torrent: {}({})", torrent.name, torrent.hash);
}
} else {
debug!("Torrent: {}({})", torrent.name, torrent.hash);
}

let peers = self.backend.get_peers(&torrent.hash).await?;
stat.peers += peers.len() as u64;
for peer in peers {
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct Args {
auth: String,
#[arg(short, long, default_value = "5", help = "Scan interval in seconds.")]
scan: u64,
#[arg(short, long, default_value = "false", help = "Handle private tracker torrents.")]
pt: bool,
#[arg(short, long, default_value = "false", help = "Clear all bans before start.")]
clear: bool,
}
Expand All @@ -39,7 +41,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("PeerBan/{} started.", env!("CARGO_PKG_VERSION"));

let qb = QBitBackend::new(args.endpoint, args.auth);
let mut daemon = Daemon::new(Box::new(qb), args.scan, args.clear);
let mut daemon = Daemon::new(Box::new(qb), args.scan, args.pt, args.clear);
loop {
match daemon.run().await {
Ok(_) => (),
Expand Down
1 change: 1 addition & 0 deletions src/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub struct Torrent {
pub name: String,
pub hash: String,
pub size: u64,
pub tracker: String,
}

0 comments on commit 1ca7b18

Please sign in to comment.