librqbit 9.0.0-rc.0

The main library used by rqbit torrent client. The binary is just a small wrapper on top of it.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::{net::SocketAddr, str::FromStr};

use anyhow::Context;
use axum::{
    extract::{Path, Query, State},
    response::IntoResponse,
};
use bytes::Bytes;
use http::{
    HeaderMap, HeaderName, HeaderValue, StatusCode,
    header::{CONTENT_DISPOSITION, CONTENT_TYPE},
};
use librqbit_core::magnet::Magnet;
use serde::{Deserialize, Serialize};

use super::ApiState;
use crate::{
    AddTorrent, ApiError, CreateTorrentOptions, SUPPORTED_SCHEMES,
    api::{ApiTorrentListOpts, Result, TorrentIdOrHash},
    api_error::WithStatusError,
    http_api::timeout::Timeout,
    http_api_types::TorrentAddQueryParams,
    torrent_state::peer::stats::snapshot::{PeerStatsFilter, PeerStatsFilterState},
    type_aliases::BF,
};

pub async fn h_torrents_list(
    State(state): State<ApiState>,
    Query(opts): Query<ApiTorrentListOpts>,
) -> impl IntoResponse {
    axum::Json(state.api.api_torrent_list_ext(opts))
}

pub async fn h_torrents_post(
    State(state): State<ApiState>,
    Query(params): Query<TorrentAddQueryParams>,
    Timeout(timeout): Timeout<600_000, 3_600_000>,
    data: Bytes,
) -> Result<impl IntoResponse> {
    let is_url = params.is_url;
    let opts = params.into_add_torrent_options();
    let data = data.to_vec();
    let maybe_magnet = |data: &[u8]| -> bool {
        std::str::from_utf8(data)
            .ok()
            .and_then(|s| Magnet::parse(s).ok())
            .is_some()
    };
    let add = match is_url {
        Some(true) => AddTorrent::Url(
            String::from_utf8(data)
                .context("invalid utf-8 for passed URL")?
                .into(),
        ),
        Some(false) => AddTorrent::TorrentFileBytes(data.into()),

        // Guess the format.
        None if SUPPORTED_SCHEMES
            .iter()
            .any(|s| data.starts_with(s.as_bytes()))
            || maybe_magnet(&data) =>
        {
            AddTorrent::Url(
                String::from_utf8(data)
                    .context("invalid utf-8 for passed URL")?
                    .into(),
            )
        }
        _ => AddTorrent::TorrentFileBytes(data.into()),
    };
    tokio::time::timeout(timeout, state.api.api_add_torrent(add, Some(opts)))
        .await
        .context("timeout")?
        .map(axum::Json)
}

pub async fn h_torrent_details(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state.api.api_torrent_details(idx).map(axum::Json)
}

pub async fn h_torrent_haves(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
    headers: HeaderMap,
) -> Result<impl IntoResponse> {
    fn generate_svg(bits: &BF, len: u32) -> String {
        if len == 0 {
            return r#"<svg width="100%" height="100" xmlns="http://www.w3.org/2000/svg"></svg>"#
                .to_string();
        }

        const HAVE_COLOR: &str = "#22c55e";
        const MISSING_COLOR: &str = "#374151";

        let bit_width = 100.0 / len as f64;
        let mut svg_segments = String::new();

        let mut bits_iter = bits.iter().map(|b| *b).enumerate().peekable();

        while let Some((i, value)) = bits_iter.next() {
            let mut count = 1;

            // Peek ahead to find how many subsequent bits have the same value
            while let Some((_, next_value)) = bits_iter.peek() {
                if *next_value == value {
                    count += 1;
                    bits_iter.next();
                } else {
                    break;
                }
            }

            let color = if value { HAVE_COLOR } else { MISSING_COLOR };
            let x_pos = i as f64 * bit_width;
            let segment_width = count as f64 * bit_width;

            svg_segments.push_str(&format!(
                r#"<rect x="{:.4}%" y="0" width="{:.4}%" height="100%" fill="{}" />"#,
                x_pos, segment_width, color
            ));
        }

        format!(
            r#"<svg width="100%" height="20" viewBox="0 0 100 100" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
                {}
            </svg>"#,
            svg_segments
        )
    }

    let (bf, len) = state.api.api_dump_haves(idx)?;

    // Check if binary format is requested
    let wants_binary = headers
        .get(http::header::ACCEPT)
        .and_then(|v| v.to_str().ok())
        .is_some_and(|s| s.contains("application/octet-stream"));

    if wants_binary {
        let bytes = bf.into_boxed_slice();
        Ok((
            [
                (
                    CONTENT_TYPE,
                    HeaderValue::from_static("application/octet-stream"),
                ),
                (
                    HeaderName::from_static("x-bitfield-len"),
                    HeaderValue::from_str(&len.to_string()).unwrap(),
                ),
            ],
            bytes,
        )
            .into_response())
    } else {
        let svg = generate_svg(&bf, len);
        Ok((
            [(CONTENT_TYPE, HeaderValue::from_static("image/svg+xml"))],
            svg,
        )
            .into_response())
    }
}

pub async fn h_torrent_stats_v0(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state.api.api_stats_v0(idx).map(axum::Json)
}

pub async fn h_torrent_stats_v1(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state.api.api_stats_v1(idx).map(axum::Json)
}

pub async fn h_peer_stats(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
    Query(filter): Query<PeerStatsFilter>,
) -> Result<impl IntoResponse> {
    state.api.api_peer_stats(idx, filter).map(axum::Json)
}

pub async fn h_torrent_action_pause(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state
        .api
        .api_torrent_action_pause(idx)
        .await
        .map(axum::Json)
}

pub async fn h_torrent_action_start(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state
        .api
        .api_torrent_action_start(idx)
        .await
        .map(axum::Json)
}

pub async fn h_torrent_action_forget(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state
        .api
        .api_torrent_action_forget(idx)
        .await
        .map(axum::Json)
}

pub async fn h_torrent_action_delete(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    state
        .api
        .api_torrent_action_delete(idx)
        .await
        .map(axum::Json)
}

#[derive(Deserialize)]
pub struct UpdateOnlyFilesRequest {
    only_files: Vec<usize>,
}

pub async fn h_torrent_action_update_only_files(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
    axum::Json(req): axum::Json<UpdateOnlyFilesRequest>,
) -> Result<impl IntoResponse> {
    state
        .api
        .api_torrent_action_update_only_files(idx, &req.only_files.into_iter().collect())
        .await
        .map(axum::Json)
}

pub async fn h_session_stats(State(state): State<ApiState>) -> impl IntoResponse {
    axum::Json(state.api.api_session_stats())
}

pub async fn h_peer_stats_prometheus(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    let handle = state.api.mgr_handle(idx)?;

    let live = handle
        .live()
        .with_status_error(StatusCode::PRECONDITION_FAILED, "torrent is not live")?;

    let peer_stats = live.per_peer_stats_snapshot(PeerStatsFilter {
        state: PeerStatsFilterState::Live,
    });

    let mut buf = String::new();

    const NAME: &str = "rqbit_peer_fetched_bytes";

    use core::fmt::Write;
    writeln!(&mut buf, "# TYPE {NAME} counter").unwrap();
    for (addr, stats) in peer_stats.peers.iter() {
        // Filter out useless peers that never sent us much.
        const THRESHOLD: u64 = 1024 * 1024;
        if stats.counters.fetched_bytes >= THRESHOLD {
            writeln!(
                &mut buf,
                "{NAME}{{addr=\"{addr}\"}} {}",
                stats.counters.fetched_bytes - THRESHOLD
            )
            .unwrap();
        }
    }

    Ok(buf)
}

pub async fn h_metadata(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
) -> Result<impl IntoResponse> {
    let handle = state.api.mgr_handle(idx)?;

    let (filename, bytes) = handle
        .with_metadata(|meta| {
            (
                meta.info
                    .name_or_else(|| format!("torrent_{idx}"))
                    .into_owned(),
                meta.torrent_bytes.clone(),
            )
        })
        .map_err(ApiError::from)?;

    Ok((
        [(
            http::header::CONTENT_DISPOSITION,
            format!("attachment; filename=\"{filename}.torrent\""),
        )],
        bytes,
    ))
}

#[derive(Serialize)]
struct AddPeersResult {
    added: usize,
}

pub async fn h_add_peers(
    State(state): State<ApiState>,
    Path(idx): Path<TorrentIdOrHash>,
    body: Bytes,
) -> Result<impl IntoResponse> {
    let handle = state.api.mgr_handle(idx)?;
    let live = handle.live().ok_or(crate::Error::TorrentIsNotLive)?;

    let body =
        std::str::from_utf8(&body).with_status_error(StatusCode::BAD_REQUEST, "invalid utf-8")?;

    let addrs = body
        .split('\n')
        .filter_map(|s| SocketAddr::from_str(s).ok());

    let mut count = 0;
    for addr in addrs {
        if live.add_peer_if_not_seen(addr)? {
            count += 1;
        }
    }

    Ok(axum::Json(AddPeersResult { added: count }))
}

#[derive(Default, Deserialize, Debug)]
enum CreateTorrentOutput {
    #[default]
    #[serde(rename = "magnet")]
    Magnet,
    #[serde(rename = "torrent")]
    Torrent,
}

#[derive(Default, Deserialize, Debug)]
pub struct HttpCreateTorrentOptions {
    #[serde(default)]
    output: CreateTorrentOutput,
    #[serde(default)]
    trackers: Vec<String>,
    name: Option<String>,
}

pub async fn h_create_torrent(
    State(state): State<ApiState>,
    axum_extra::extract::Query(opts): axum_extra::extract::Query<HttpCreateTorrentOptions>,
    body: Bytes,
) -> Result<impl IntoResponse> {
    if !state.opts.allow_create {
        return Err((
            StatusCode::FORBIDDEN,
            "creating torrents not allowed. Enable through CLI options",
        )
            .into());
    }

    let path = std::path::Path::new(
        std::str::from_utf8(body.as_ref())
            .with_status_error(StatusCode::BAD_REQUEST, "invalid utf-8")?,
    );

    let create_opts = CreateTorrentOptions {
        name: opts.name.as_deref(),
        trackers: opts.trackers,
        piece_length: None,
    };

    let (torrent, handle) = state
        .api
        .session()
        .create_and_serve_torrent(path, create_opts)
        .await?;

    let mut headers = HeaderMap::new();
    if let Ok(v) = HeaderValue::from_str(&handle.id().to_string()) {
        headers.insert("torrent-id", v);
    }
    if let Ok(v) = HeaderValue::from_str(&torrent.info_hash().as_string()) {
        headers.insert("torrent-info-hash", v);
    }

    match opts.output {
        CreateTorrentOutput::Magnet => {
            let magnet = torrent.as_magnet();
            Ok(magnet.to_string().into_response())
        }
        CreateTorrentOutput::Torrent => {
            let name = torrent
                .as_info()
                .info
                .data
                .name
                .as_ref()
                .map(|n| String::from_utf8_lossy(n.as_ref()))
                .unwrap_or("torrent".into());

            headers.insert(
                CONTENT_TYPE,
                HeaderValue::from_static("application/x-bittorrent"),
            );

            if let Ok(h) =
                HeaderValue::from_str(&format!("attachment; filename=\"{name}.torrent\""))
            {
                headers.insert(CONTENT_DISPOSITION, h);
            }

            Ok((headers, torrent.as_bytes()?).into_response())
        }
    }
}