mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 10:45:30 +00:00
implement binary info-hash api
This commit is contained in:
parent
4b473a63bc
commit
7d00f0750f
2 changed files with 69 additions and 6 deletions
|
|
@ -189,6 +189,13 @@ pub struct StatisticsConfig {
|
||||||
pub json_info_hash_ipv4_file_path: PathBuf,
|
pub json_info_hash_ipv4_file_path: PathBuf,
|
||||||
/// Path to dump JSON info-hash IPv6 to
|
/// Path to dump JSON info-hash IPv6 to
|
||||||
pub json_info_hash_ipv6_file_path: PathBuf,
|
pub json_info_hash_ipv6_file_path: PathBuf,
|
||||||
|
/// Save statistics as binary to a file
|
||||||
|
/// * this option is recommended for SSD as compares previous file before overwrite
|
||||||
|
pub write_bin_to_file: bool,
|
||||||
|
/// Path to dump binary info-hash IPv4 to
|
||||||
|
pub bin_info_hash_ipv4_file_path: PathBuf,
|
||||||
|
/// Path to dump binary info-hash IPv6 to
|
||||||
|
pub bin_info_hash_ipv6_file_path: PathBuf,
|
||||||
/// Run a prometheus endpoint
|
/// Run a prometheus endpoint
|
||||||
#[cfg(feature = "prometheus")]
|
#[cfg(feature = "prometheus")]
|
||||||
pub run_prometheus_endpoint: bool,
|
pub run_prometheus_endpoint: bool,
|
||||||
|
|
@ -232,6 +239,9 @@ impl Default for StatisticsConfig {
|
||||||
write_json_to_file: false,
|
write_json_to_file: false,
|
||||||
json_info_hash_ipv4_file_path: "tmp/info_hash_v4.json".into(),
|
json_info_hash_ipv4_file_path: "tmp/info_hash_v4.json".into(),
|
||||||
json_info_hash_ipv6_file_path: "tmp/info_hash_v6.json".into(),
|
json_info_hash_ipv6_file_path: "tmp/info_hash_v6.json".into(),
|
||||||
|
write_bin_to_file: false,
|
||||||
|
bin_info_hash_ipv4_file_path: "tmp/info_hash_v4.bin".into(),
|
||||||
|
bin_info_hash_ipv6_file_path: "tmp/info_hash_v6.bin".into(),
|
||||||
#[cfg(feature = "prometheus")]
|
#[cfg(feature = "prometheus")]
|
||||||
run_prometheus_endpoint: false,
|
run_prometheus_endpoint: false,
|
||||||
#[cfg(feature = "prometheus")]
|
#[cfg(feature = "prometheus")]
|
||||||
|
|
|
||||||
|
|
@ -136,12 +136,12 @@ impl TorrentMaps {
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.statistics.write_json_to_file {
|
if config.statistics.write_json_to_file {
|
||||||
|
use anyhow::{Context, Result};
|
||||||
fn save_to_file(
|
fn save_to_file(
|
||||||
path: &std::path::PathBuf,
|
path: &std::path::PathBuf,
|
||||||
info_hashes: &Vec<InfoHash>,
|
info_hashes: &Vec<InfoHash>,
|
||||||
) -> anyhow::Result<()> {
|
) -> Result<()> {
|
||||||
let mut file =
|
let mut file = Context::with_context(std::fs::File::create(path), || {
|
||||||
anyhow::Context::with_context(std::fs::File::create(path), || {
|
|
||||||
format!("File path: {}", path.to_string_lossy())
|
format!("File path: {}", path.to_string_lossy())
|
||||||
})?;
|
})?;
|
||||||
write!(file, "[")?;
|
write!(file, "[")?;
|
||||||
|
|
@ -153,7 +153,7 @@ impl TorrentMaps {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
write!(file, "]")?; // @TODO serialize with serde_json?
|
write!(file, "]")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
if config.network.ipv4_active() {
|
if config.network.ipv4_active() {
|
||||||
|
|
@ -171,6 +171,59 @@ impl TorrentMaps {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.statistics.write_bin_to_file {
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use std::{fs::File, io::Read, path::PathBuf};
|
||||||
|
/// Prevent extra write operations by compare the file content is same
|
||||||
|
fn is_same(path: &PathBuf, info_hashes: &Vec<InfoHash>) -> Result<bool> {
|
||||||
|
if !std::fs::exists(path)? {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
let mut t = 0;
|
||||||
|
let mut f = File::open(path)?;
|
||||||
|
loop {
|
||||||
|
let mut b = vec![0, 20];
|
||||||
|
let n = f.read_to_end(&mut b)?;
|
||||||
|
if n == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if info_hashes.iter().any(|i| i.0 != b[..n]) {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
t += 1
|
||||||
|
}
|
||||||
|
Ok(t == info_hashes.len())
|
||||||
|
}
|
||||||
|
/// Dump `InfoHash` index to file
|
||||||
|
fn save_to_file(path: &PathBuf, info_hashes: &Vec<InfoHash>) -> Result<()> {
|
||||||
|
if is_same(path, info_hashes)? {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let mut f = Context::with_context(File::create(path), || {
|
||||||
|
format!("File path: {}", path.to_string_lossy())
|
||||||
|
})?;
|
||||||
|
for i in info_hashes {
|
||||||
|
f.write_all(&i.0)?;
|
||||||
|
f.write_all(b"\n")?
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
if config.network.ipv4_active() {
|
||||||
|
if let Err(err) =
|
||||||
|
save_to_file(&config.statistics.bin_info_hash_ipv4_file_path, &ipv4.3)
|
||||||
|
{
|
||||||
|
::log::error!("Couldn't dump IPv4 info-hash table to file: {:#}", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config.network.ipv6_active() {
|
||||||
|
if let Err(err) =
|
||||||
|
save_to_file(&config.statistics.bin_info_hash_ipv6_file_path, &ipv6.3)
|
||||||
|
{
|
||||||
|
::log::error!("Couldn't dump IPv6 info-hash table to file: {:#}", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue