rqbit/crates/librqbit/src/http_api.rs

458 lines
14 KiB
Rust
Raw Normal View History

2021-10-10 09:57:21 +01:00
use anyhow::Context;
2022-12-04 12:53:55 +00:00
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
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};
2022-12-04 12:53:55 +00:00
use axum::{response, routing, Router};
2021-06-30 10:14:33 +01:00
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;
2022-12-04 12:53:55 +00:00
pub 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
}
2022-12-08 09:28:01 +00:00
#[derive(Debug)]
struct Error {
status: Option<StatusCode>,
kind: ErrorKind,
}
impl Error {
const fn torrent_not_found(torrent_id: usize) -> Self {
Self {
status: Some(StatusCode::NOT_FOUND),
kind: ErrorKind::TorrentNotFound(torrent_id),
}
}
const fn dht_disabled() -> Self {
Self {
status: Some(StatusCode::NOT_FOUND),
kind: ErrorKind::DhtDisabled,
}
}
fn with_status(self, status: StatusCode) -> Self {
Self {
status: Some(status),
kind: self.kind,
}
}
}
#[derive(Debug)]
enum ErrorKind {
TorrentNotFound(usize),
DhtDisabled,
Other(anyhow::Error),
}
impl From<anyhow::Error> for Error {
fn from(value: anyhow::Error) -> Self {
Self {
status: None,
kind: ErrorKind::Other(value),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.kind {
ErrorKind::Other(err) => err.source(),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.kind {
ErrorKind::TorrentNotFound(idx) => write!(f, "torrent {idx} not found"),
ErrorKind::Other(err) => err.fmt(f),
ErrorKind::DhtDisabled => write!(f, "DHT is disabled"),
}
}
}
impl IntoResponse for Error {
fn into_response(self) -> response::Response {
let response_body = format!("{self}");
let mut response = response_body.into_response();
*response.status_mut() = match self.status {
Some(s) => s,
None => StatusCode::INTERNAL_SERVER_ERROR,
};
response
}
}
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,
2022-12-04 12:53:55 +00:00
human_readable: format!("{mbps:.2} MiB/s"),
2021-07-08 23:03:58 +01:00
}
}
}
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]>,
2022-12-08 09:28:01 +00:00
) -> Result<TorrentDetailsResponse, Error> {
2021-10-23 09:37:37 +01:00
let files = info
2022-12-08 09:28:01 +00:00
.iter_filenames_and_lengths()?
2021-10-23 09:37:37 +01:00
.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();
2022-12-08 09:28:01 +00:00
Ok(TorrentDetailsResponse {
2021-10-23 09:37:37 +01:00
info_hash: info_hash.as_string(),
files,
2022-12-08 09:28:01 +00:00
})
2021-10-23 09:37:37 +01:00
}
2021-07-13 14:59:44 +01:00
impl ApiInternal {
2022-12-08 09:28:01 +00:00
fn mgr_handle(&self, idx: usize) -> Result<TorrentManagerHandle, Error> {
self.torrent_managers
.read()
.get(idx)
.cloned()
.ok_or(Error::torrent_not_found(idx))
2021-07-08 00:09:00 +01:00
}
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(),
}
}
2022-12-08 09:28:01 +00:00
fn api_torrent_details(&self, idx: usize) -> Result<TorrentDetailsResponse, Error> {
2021-07-08 23:49:25 +01:00
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();
2022-12-08 09:28:01 +00:00
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>,
2022-12-08 09:28:01 +00:00
) -> Result<ApiAddTorrentResponse, Error> {
2021-10-23 09:37:37 +01:00
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
{
2022-12-08 09:28:01 +00:00
AddTorrentResponse::AlreadyManaged(managed) => {
return Err(Error::from(anyhow::anyhow!(
"{:?} is already managed, downloaded to {:?}",
managed.info_hash,
managed.output_folder
))
.with_status(StatusCode::CONFLICT));
}
2021-10-23 09:37:37 +01:00
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,
2022-12-08 09:28:01 +00:00
details: make_torrent_details(&info_hash, &info, only_files.as_deref())
.context("error making torrent details")?,
2021-10-23 09:37:37 +01:00
},
AddTorrentResponse::Added(handle) => {
let details = make_torrent_details(
&handle.torrent_state().info_hash(),
handle.torrent_state().info(),
handle.only_files(),
2022-12-08 09:28:01 +00:00
)
.context("error making torrent details")?;
2021-10-23 09:37:37 +01:00
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())
}
2022-12-08 09:28:01 +00:00
fn api_stats(&self, idx: usize) -> Result<StatsResponse, Error> {
2021-07-08 23:03:58 +01:00
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;
2022-12-08 09:28:01 +00:00
Ok(StatsResponse {
2021-07-08 23:03:58 +01:00
average_piece_download_time: snapshot.average_piece_download_time(),
snapshot,
2021-10-25 18:11:46 +08:00
all_time_download_speed: (downloaded_mb / elapsed.as_secs_f64()).into(),
2021-07-08 23:03:58 +01:00
download_speed: estimator.download_mbps().into(),
time_remaining: estimator.time_remaining(),
})
}
2022-12-08 09:28:01 +00:00
fn api_dump_haves(&self, idx: usize) -> Result<String, Error> {
2021-07-08 23:03:58 +01:00
let mgr = self.mgr_handle(idx)?;
2022-12-08 09:28:01 +00:00
Ok(format!(
2021-07-08 23:03:58 +01:00
"{:?}",
mgr.torrent_state().lock_read().chunks.get_have_pieces(),
))
}
2021-07-08 00:09:00 +01:00
}
2022-12-04 12:53:55 +00:00
type ApiState = Arc<ApiInternal>;
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
}
#[derive(Serialize, Deserialize)]
pub struct TorrentAddQueryParams {
pub overwrite: Option<bool>,
pub output_folder: Option<String>,
pub sub_folder: Option<String>,
pub only_files_regex: Option<String>,
2021-10-23 09:37:37 +01:00
pub list_only: Option<bool>,
}
2022-12-04 12:53:55 +00:00
async fn post_torrent(
State(inner): State<ApiState>,
Query(params): Query<TorrentAddQueryParams>,
url: String,
) -> Result<axum::Json<impl Serialize>, impl IntoResponse> {
let opts = AddTorrentOptions {
overwrite: params.overwrite.unwrap_or(false),
only_files_regex: params.only_files_regex,
output_folder: params.output_folder,
sub_folder: params.sub_folder,
list_only: params.list_only.unwrap_or(false),
..Default::default()
};
match inner
.api_add_torrent(url, Some(opts))
.await
.context("error calling HttpApi::api_add_torrent")
{
Ok(response) => Ok(axum::Json(response)),
Err(err) => Err((StatusCode::BAD_REQUEST, format!("{err:#?}"))),
}
}
2022-12-08 09:28:01 +00:00
async fn get_torrent(
State(state): State<ApiState>,
axum::extract::Path(idx): axum::extract::Path<usize>,
) -> Result<impl IntoResponse, Error> {
Ok(axum::Json(state.api_torrent_details(idx)?))
}
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<()> {
2022-12-04 12:53:55 +00:00
let state = self.inner;
let app = Router::new()
2022-12-08 09:28:01 +00:00
.route("/", routing::get({
let body = serde_json::json!({
2022-12-04 12:53:55 +00:00
"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",
"GET /torrents/{index}/stats": "Torrent stats",
// 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.
"POST /torrents": "Add a torrent here. magnet: or http:// or a local file."
},
"server": "rqbit",
2022-12-08 09:28:01 +00:00
});
|| async move {
axum::Json(body)
}
2022-12-04 12:53:55 +00:00
}))
.route(
"/dht/stats",
routing::get({
let state = state.clone();
move || async move {
2022-12-08 09:28:01 +00:00
let dht_stats = state.api_dht_stats().ok_or(Error::dht_disabled())?;
Ok::<_, Error>(axum::Json(dht_stats))
2021-10-10 09:57:21 +01:00
}
2022-12-04 12:53:55 +00:00
}),
)
.route(
"/dht/table",
routing::get({
let state = state.clone();
move || async move {
2022-12-08 09:28:01 +00:00
let dht = state.dht.as_ref().ok_or(Error::dht_disabled())?;
Ok::<_, Error>(dht.with_routing_table(|r| axum::Json(r.clone())))
2022-12-04 12:53:55 +00:00
}
}),
)
.route(
"/torrents",
routing::get({
let state = state.clone();
2022-12-08 09:28:01 +00:00
move || async move { axum::Json(state.api_torrent_list()) }
2022-12-04 12:53:55 +00:00
}),
)
.route("/torrents", routing::post(post_torrent))
.route(
"/torrents/:id",
2022-12-08 09:28:01 +00:00
routing::get(get_torrent),
2022-12-04 12:53:55 +00:00
)
.route(
"/torrents/:id/haves",
routing::get({
let state = state.clone();
move |axum::extract::Path(idx): axum::extract::Path<usize>| async move {
2022-12-08 09:28:01 +00:00
state.api_dump_haves(idx)
2022-12-04 12:53:55 +00:00
}
}),
)
.route(
"/torrents/:id/stats",
routing::get({
let state = state.clone();
move |axum::extract::Path(idx): axum::extract::Path<usize>| async move {
2022-12-08 09:28:01 +00:00
state.api_stats(idx).map(axum::Json)
2022-12-04 12:53:55 +00:00
}
}),
)
.with_state(state);
log::info!("starting HTTP server on {}", addr);
axum::Server::try_bind(&addr)
.with_context(|| format!("error binding to {addr}"))?
.serve(app.into_make_service())
.await?;
2021-07-08 00:09:00 +01:00
Ok(())
}
2021-06-30 10:14:33 +01:00
}