rqbit/crates/librqbit/src/http_api.rs

411 lines
13 KiB
Rust
Raw Normal View History

2021-10-10 09:57:21 +01:00
use anyhow::Context;
2021-10-23 09:37:37 +01:00
use buffers::ByteString;
2021-07-14 13:40:56 +01:00
use dht::{Dht, DhtStats};
2021-10-23 09:37:37 +01:00
use librqbit_core::id20::Id20;
use librqbit_core::torrent_metainfo::TorrentMetaV1Info;
use log::warn;
2021-07-08 00:09:00 +01:00
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
2021-10-10 09:57:21 +01:00
use std::net::SocketAddr;
use std::sync::Arc;
2021-07-08 23:03:58 +01:00
use std::time::{Duration, Instant};
2021-10-10 09:57:21 +01:00
use warp::hyper::body::Bytes;
2021-07-19 09:04:25 +01:00
use warp::hyper::Body;
2021-06-30 10:14:33 +01:00
use warp::Filter;
2021-10-23 09:37:37 +01:00
use crate::session::{AddTorrentOptions, AddTorrentResponse, ListOnlyResponse, Session};
2021-07-08 00:09:00 +01:00
use crate::torrent_manager::TorrentManagerHandle;
2021-07-08 23:03:58 +01:00
use crate::torrent_state::StatsSnapshot;
2021-07-13 14:59:44 +01:00
struct ApiInternal {
2021-07-14 13:40:56 +01:00
dht: Option<Dht>,
2021-07-08 23:03:58 +01:00
startup_time: Instant,
torrent_managers: RwLock<Vec<TorrentManagerHandle>>,
2021-10-10 09:57:21 +01:00
session: Arc<Session>,
2021-07-08 00:09:00 +01:00
}
2021-07-13 14:59:44 +01:00
impl ApiInternal {
2021-10-10 09:57:21 +01:00
fn new(session: Arc<Session>) -> Self {
2021-07-08 23:03:58 +01:00
Self {
2021-10-10 09:57:21 +01:00
dht: session.get_dht(),
2021-07-08 23:03:58 +01:00
startup_time: Instant::now(),
torrent_managers: RwLock::new(Vec::new()),
2021-10-10 09:57:21 +01:00
session,
2021-07-08 00:09:00 +01:00
}
}
2021-10-10 09:57:21 +01:00
fn add_mgr(&self, handle: TorrentManagerHandle) -> usize {
let mut g = self.torrent_managers.write();
let idx = g.len();
g.push(handle);
idx
}
2021-07-08 00:09:00 +01:00
}
2021-07-08 23:03:58 +01:00
#[derive(Serialize)]
struct Speed {
mbps: f64,
human_readable: String,
}
impl Speed {
fn new(mbps: f64) -> Self {
Self {
mbps,
human_readable: format!("{:.2}Mbps", mbps),
}
}
}
impl From<f64> for Speed {
fn from(mbps: f64) -> Self {
Self::new(mbps)
}
}
#[derive(Serialize)]
struct TorrentListResponseItem {
id: usize,
info_hash: String,
}
#[derive(Serialize)]
struct TorrentListResponse {
torrents: Vec<TorrentListResponseItem>,
}
2021-10-23 09:37:37 +01:00
#[derive(Serialize, Deserialize)]
pub struct TorrentDetailsResponseFile {
pub name: String,
pub length: u64,
pub included: bool,
2021-07-08 23:49:25 +01:00
}
2021-10-23 09:37:37 +01:00
#[derive(Serialize, Deserialize)]
pub struct TorrentDetailsResponse {
pub info_hash: String,
pub files: Vec<TorrentDetailsResponseFile>,
2021-07-08 23:49:25 +01:00
}
2021-07-08 23:03:58 +01:00
#[derive(Serialize)]
struct StatsResponse {
snapshot: StatsSnapshot,
average_piece_download_time: Option<Duration>,
download_speed: Speed,
all_time_download_speed: Speed,
time_remaining: Option<Duration>,
2021-07-08 00:09:00 +01:00
}
2021-10-23 09:37:37 +01:00
#[derive(Serialize, Deserialize)]
pub struct ApiAddTorrentResponse {
pub id: Option<usize>,
pub details: TorrentDetailsResponse,
}
fn make_torrent_details(
info_hash: &Id20,
info: &TorrentMetaV1Info<ByteString>,
only_files: Option<&[usize]>,
) -> TorrentDetailsResponse {
let files = info
.iter_filenames_and_lengths()
.unwrap()
.enumerate()
.map(|(idx, (filename_it, length))| {
let name = match filename_it.to_string() {
Ok(s) => s,
Err(err) => {
warn!("error reading filename: {:?}", err);
"<INVALID NAME>".to_string()
}
};
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
TorrentDetailsResponseFile {
name,
length,
included,
}
})
.collect();
TorrentDetailsResponse {
info_hash: info_hash.as_string(),
files,
}
}
2021-07-13 14:59:44 +01:00
impl ApiInternal {
2021-07-08 00:09:00 +01:00
fn mgr_handle(&self, idx: usize) -> Option<TorrentManagerHandle> {
self.torrent_managers.read().get(idx).cloned()
}
2021-07-08 23:03:58 +01:00
fn api_torrent_list(&self) -> TorrentListResponse {
TorrentListResponse {
torrents: self
.torrent_managers
.read()
.iter()
.enumerate()
.map(|(id, mgr)| TorrentListResponseItem {
id,
2021-07-13 09:18:45 +01:00
info_hash: mgr.torrent_state().info_hash().as_string(),
2021-07-08 23:03:58 +01:00
})
.collect(),
}
}
2021-07-08 23:49:25 +01:00
fn api_torrent_details(&self, idx: usize) -> Option<TorrentDetailsResponse> {
let handle = self.mgr_handle(idx)?;
2021-10-23 09:37:37 +01:00
let info_hash = handle.torrent_state().info_hash();
let only_files = handle.only_files();
Some(make_torrent_details(
&info_hash,
handle.torrent_state().info(),
only_files,
))
2021-07-08 23:49:25 +01:00
}
async fn api_add_torrent(
&self,
url: String,
opts: Option<AddTorrentOptions>,
2021-10-23 09:37:37 +01:00
) -> anyhow::Result<ApiAddTorrentResponse> {
let response = match self
2021-10-10 09:57:21 +01:00
.session
.add_torrent(&url, opts)
2021-10-10 09:57:21 +01:00
.await
.context("error adding torrent")?
2021-10-23 09:37:37 +01:00
{
AddTorrentResponse::AlreadyManaged(managed) => anyhow::bail!(
"{:?} is already managed, downloaded to {:?}",
managed.info_hash,
managed.output_folder
),
AddTorrentResponse::ListOnly(ListOnlyResponse {
2021-10-25 16:46:30 +08:00
info_hash,
info,
only_files,
}) => ApiAddTorrentResponse {
2021-10-23 09:37:37 +01:00
id: None,
details: make_torrent_details(&info_hash, &info, only_files.as_deref()),
},
AddTorrentResponse::Added(handle) => {
let details = make_torrent_details(
&handle.torrent_state().info_hash(),
handle.torrent_state().info(),
handle.only_files(),
);
let id = self.add_mgr(handle);
ApiAddTorrentResponse {
id: Some(id),
details,
}
}
};
Ok(response)
2021-10-10 09:57:21 +01:00
}
2021-07-14 13:40:56 +01:00
fn api_dht_stats(&self) -> Option<DhtStats> {
self.dht.as_ref().map(|d| d.stats())
}
2021-07-08 23:03:58 +01:00
fn api_stats(&self, idx: usize) -> Option<StatsResponse> {
let mgr = self.mgr_handle(idx)?;
let snapshot = mgr.torrent_state().stats_snapshot();
let estimator = mgr.speed_estimator();
// Poor mans download speed computation
let elapsed = self.startup_time.elapsed();
let downloaded_bytes = snapshot.downloaded_and_checked_bytes;
let downloaded_mb = downloaded_bytes as f64 / 1024f64 / 1024f64;
Some(StatsResponse {
average_piece_download_time: snapshot.average_piece_download_time(),
snapshot,
2021-10-25 16:39:14 +08:00
all_time_download_speed: (downloaded_mb * 8f64 / elapsed.as_secs_f64()).into(),
2021-07-08 23:03:58 +01:00
download_speed: estimator.download_mbps().into(),
time_remaining: estimator.time_remaining(),
})
}
fn api_dump_haves(&self, idx: usize) -> Option<String> {
let mgr = self.mgr_handle(idx)?;
Some(format!(
"{:?}",
mgr.torrent_state().lock_read().chunks.get_have_pieces(),
))
}
2021-07-08 00:09:00 +01:00
}
2021-07-08 23:03:58 +01:00
#[derive(Clone)]
2021-07-08 00:09:00 +01:00
pub struct HttpApi {
2021-07-13 14:59:44 +01:00
inner: Arc<ApiInternal>,
2021-07-08 00:09:00 +01:00
}
2021-07-08 23:03:58 +01:00
fn json_response<T: Serialize>(v: T) -> warp::reply::Response {
let body = serde_json::to_string_pretty(&v).unwrap();
let mut response = warp::reply::Response::new(body.into());
response.headers_mut().insert(
"content-type",
warp::http::HeaderValue::from_static("application/json"),
);
response
}
2021-07-19 09:04:25 +01:00
fn plaintext_response<B: Into<Body>>(body: B) -> warp::reply::Response {
warp::reply::Response::new(body.into())
}
2021-07-14 13:40:56 +01:00
fn not_found_response(body: String) -> warp::reply::Response {
let mut response = warp::reply::Response::new(body.into());
2021-07-08 23:03:58 +01:00
*response.status_mut() = warp::http::StatusCode::NOT_FOUND;
response
}
2021-07-14 13:40:56 +01:00
fn torrent_not_found_response(idx: usize) -> warp::reply::Response {
not_found_response(format!("torrent {} not found", idx))
}
2021-07-08 23:03:58 +01:00
fn json_or_404<T: Serialize>(idx: usize, v: Option<T>) -> warp::reply::Response {
match v {
Some(v) => json_response(v),
2021-07-14 13:40:56 +01:00
None => torrent_not_found_response(idx),
2021-07-08 23:03:58 +01:00
}
}
#[derive(Serialize, Deserialize)]
pub struct TorrentAddQueryParams {
pub overwrite: Option<bool>,
pub output_folder: Option<String>,
pub only_files_regex: Option<String>,
2021-10-23 09:37:37 +01:00
pub list_only: Option<bool>,
}
2021-07-08 00:09:00 +01:00
impl HttpApi {
2021-10-10 09:57:21 +01:00
pub fn new(session: Arc<Session>) -> Self {
2021-07-08 23:03:58 +01:00
Self {
2021-10-10 09:57:21 +01:00
inner: Arc::new(ApiInternal::new(session)),
2021-07-08 23:03:58 +01:00
}
2021-07-08 00:09:00 +01:00
}
pub fn add_mgr(&self, handle: TorrentManagerHandle) -> usize {
2021-10-10 09:57:21 +01:00
self.inner.add_mgr(handle)
2021-07-08 00:09:00 +01:00
}
pub async fn make_http_api_and_run(self, addr: SocketAddr) -> anyhow::Result<()> {
let inner = self.inner;
2021-07-19 09:04:25 +01:00
let api_list = warp::path::end().map({
let api_list = serde_json::json!({
"apis": {
"GET /": "list all available APIs",
"GET /dht/stats": "DHT stats",
"GET /dht/table": "DHT routing table",
"GET /torrents": "List torrents (default torrent is 0)",
"GET /torrents/{index}": "Torrent details",
"GET /torrents/{index}/haves": "The bitfield of have pieces",
2021-10-09 16:49:55 +01:00
"GET /torrents/{index}/stats": "Torrent stats",
2021-10-10 11:43:44 +01:00
// This is kind of not secure as it just reads any local file that it has access to,
// or any URL, but whatever, ok for our purposes / thread model.
2021-10-09 16:49:55 +01:00
"POST /torrents/": "Add a torrent here. magnet: or http:// or a local file."
},
"server": "rqbit",
2021-07-19 09:04:25 +01:00
});
move || json_response(&api_list)
2021-07-08 00:09:00 +01:00
});
2021-07-14 13:40:56 +01:00
let dht_stats = warp::path!("dht" / "stats").map({
let inner = inner.clone();
move || match inner.api_dht_stats() {
Some(stats) => json_response(stats),
None => not_found_response("DHT is off".into()),
}
});
let dht_routing_table = warp::path!("dht" / "table").map({
let inner = inner.clone();
2021-07-14 18:09:12 +01:00
// clippy suggests something that doesn't work here.
#[allow(clippy::redundant_closure)]
2021-10-25 16:46:30 +08:00
move || match inner.dht.as_ref() {
2021-07-14 13:40:56 +01:00
Some(dht) => dht.with_routing_table(|r| json_response(r)),
None => not_found_response("DHT is off".into()),
}
});
2021-10-10 09:57:21 +01:00
let torrent_list = warp::get().and(warp::path("torrents")).map({
2021-07-19 09:04:25 +01:00
let inner = inner.clone();
move || json_response(inner.api_torrent_list())
});
2021-10-10 09:57:21 +01:00
let torrent_add = warp::post()
.and(warp::path("torrents"))
.and(warp::body::bytes())
.and(warp::query())
2021-10-10 09:57:21 +01:00
.and_then({
let inner = inner.clone();
use warp::http::Response;
2021-10-23 09:37:37 +01:00
fn make_response<T>(status: u16, body: T) -> Response<T> {
2021-10-10 09:57:21 +01:00
Response::builder().status(status).body(body).unwrap()
}
move |body: Bytes, params: TorrentAddQueryParams| {
2021-10-10 09:57:21 +01:00
let inner = inner.clone();
async move {
let url = match String::from_utf8(body.to_vec()) {
Ok(str) => str,
Err(_) => {
return Ok::<_, warp::Rejection>(make_response(
400,
"invalid utf-8".into(),
2021-10-25 16:39:14 +08:00
));
2021-10-10 09:57:21 +01:00
}
};
let opts = AddTorrentOptions {
overwrite: params.overwrite.unwrap_or(false),
only_files_regex: params.only_files_regex,
output_folder: params.output_folder,
2021-10-23 09:37:37 +01:00
list_only: params.list_only.unwrap_or(false),
..Default::default()
};
2021-10-23 09:37:37 +01:00
match inner
.api_add_torrent(url, Some(opts))
2021-10-10 09:57:21 +01:00
.await
2021-10-23 09:37:37 +01:00
.context("error calling HttpApi::api_add_torrent")
{
Ok(response) => Ok(json_response(response)),
Err(err) => Ok(make_response(400, format!("{:#?}", err).into())),
2021-10-10 09:57:21 +01:00
}
}
}
});
2021-07-19 09:04:25 +01:00
let torrent_details = warp::path!("torrents" / usize).map({
2021-07-08 23:49:25 +01:00
let inner = inner.clone();
move |idx| json_or_404(idx, inner.api_torrent_details(idx))
});
2021-07-19 09:04:25 +01:00
let torrent_dump_haves = warp::path!("torrents" / usize / "haves").map({
2021-07-08 00:09:00 +01:00
let inner = inner.clone();
2021-07-19 09:04:25 +01:00
move |idx| match inner.api_dump_haves(idx) {
Some(haves) => plaintext_response(haves),
None => torrent_not_found_response(idx),
}
2021-07-08 00:09:00 +01:00
});
2021-07-19 09:04:25 +01:00
let torrent_dump_stats = warp::path!("torrents" / usize / "stats").map({
2021-07-08 00:09:00 +01:00
let inner = inner.clone();
2021-07-08 23:03:58 +01:00
move |idx| json_or_404(idx, inner.api_stats(idx))
2021-07-08 00:09:00 +01:00
});
2021-06-30 10:31:30 +01:00
2021-07-19 09:04:25 +01:00
let router = api_list
2021-07-14 13:40:56 +01:00
.or(dht_stats)
.or(dht_routing_table)
.or(torrent_details)
2021-07-19 09:04:25 +01:00
.or(torrent_dump_haves)
2021-10-10 09:57:21 +01:00
.or(torrent_dump_stats)
.or(torrent_add)
.or(torrent_list);
2021-06-30 10:31:30 +01:00
2021-07-08 00:09:00 +01:00
warp::serve(router).run(addr).await;
Ok(())
}
2021-06-30 10:14:33 +01:00
}