|
| 1 | +//! The tracker REST API with all its versions. |
| 2 | +//! |
| 3 | +//! > **NOTICE**: This API should not be exposed directly to the internet, it is |
| 4 | +//! intended for internal use only. |
| 5 | +//! |
| 6 | +//! All endpoints require an authorization token which must be set in the |
| 7 | +//! configuration before running the tracker. The default configuration uses |
| 8 | +//! `?token=MyAccessToken`. |
| 9 | +//! |
| 10 | +//! Endpoints for the latest API: [v1](crate::servers::apis::v1). |
| 11 | +//! |
| 12 | +//! # Table of contents |
| 13 | +//! |
| 14 | +//! - [API versioning](#api-versioning) |
| 15 | +//! - [Enable the API](#enable-the-api) |
| 16 | +//! - [Authentication](#authentication) |
| 17 | +//! - [API endpoints](#api-endpoints) |
| 18 | +//! |
| 19 | +//! # API versioning |
| 20 | +//! |
| 21 | +//! The API is versioned and each version has its own module. |
| 22 | +//! The API server runs all the API versions on the same server using |
| 23 | +//! the same port. Currently there is only one API version: [v1](crate::servers::apis::v1) |
| 24 | +//! but a version [`v2`](https://github.com/torrust/torrust-tracker/issues/144) |
| 25 | +//! is planned. |
| 26 | +//! |
| 27 | +//! # Enable the API |
| 28 | +//! |
| 29 | +//! The configuration file has a `http_api` section that can be used to enable |
| 30 | +//! the API. |
| 31 | +//! |
| 32 | +//! ```toml |
| 33 | +//! [http_api] |
| 34 | +//! enabled = true |
| 35 | +//! bind_address = "0.0.0.0:1212" |
| 36 | +//! ssl_enabled = false |
| 37 | +//! ssl_cert_path = "./storage/ssl_certificates/localhost.crt" |
| 38 | +//! ssl_key_path = "./storage/ssl_certificates/localhost.key" |
| 39 | +//! |
| 40 | +//! [http_api.access_tokens] |
| 41 | +//! admin = "MyAccessToken" |
| 42 | +//! ``` |
| 43 | +//! |
| 44 | +//! Refer to [torrust-tracker-configuration](https://docs.rs/torrust-tracker-configuration>) |
| 45 | +//! for more information about the API configuration. |
| 46 | +//! |
| 47 | +//! When you run the tracker with enabled API, you will see the following message: |
| 48 | +//! |
| 49 | +//! ```text |
| 50 | +//! Loading configuration from config file ./config.toml |
| 51 | +//! 023-03-28T12:19:24.963054069+01:00 [torrust_tracker::bootstrap::logging][INFO] logging initialized. |
| 52 | +//! ... |
| 53 | +//! 023-03-28T12:19:24.964138723+01:00 [torrust_tracker::bootstrap::jobs::tracker_apis][INFO] Starting Torrust APIs server on: http://0.0.0.0:1212 |
| 54 | +//! ``` |
| 55 | +//! |
| 56 | +//! The API server will be available on the address specified in the configuration. |
| 57 | +//! |
| 58 | +//! You can test the API by loading the following URL on a browser: |
| 59 | +//! |
| 60 | +//! <http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken> |
| 61 | +//! |
| 62 | +//! Or using `curl`: |
| 63 | +//! |
| 64 | +//! ```bash |
| 65 | +//! $ curl -s http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken | jq |
| 66 | +//! ``` |
| 67 | +//! |
| 68 | +//! The response will be a JSON object. For example, the tracker statistics |
| 69 | +//! endpoint: |
| 70 | +//! |
| 71 | +//! ```json |
| 72 | +//! { |
| 73 | +//! "torrents": 0, |
| 74 | +//! "seeders": 0, |
| 75 | +//! "completed": 0, |
| 76 | +//! "leechers": 0, |
| 77 | +//! "tcp4_connections_handled": 0, |
| 78 | +//! "tcp4_announces_handled": 0, |
| 79 | +//! "tcp4_scrapes_handled": 0, |
| 80 | +//! "tcp6_connections_handled": 0, |
| 81 | +//! "tcp6_announces_handled": 0, |
| 82 | +//! "tcp6_scrapes_handled": 0, |
| 83 | +//! "udp4_connections_handled": 0, |
| 84 | +//! "udp4_announces_handled": 0, |
| 85 | +//! "udp4_scrapes_handled": 0, |
| 86 | +//! "udp6_connections_handled": 0, |
| 87 | +//! "udp6_announces_handled": 0, |
| 88 | +//! "udp6_scrapes_handled": 0 |
| 89 | +//! } |
| 90 | +//! ``` |
| 91 | +//! |
| 92 | +//! # API authentication |
| 93 | +//! |
| 94 | +//! The API supports authentication using a GET parameter token. |
| 95 | +//! |
| 96 | +//! <http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken> |
| 97 | +//! |
| 98 | +//! You can set as many tokens as you want in the configuration file: |
| 99 | +//! |
| 100 | +//! ```toml |
| 101 | +//! [http_api.access_tokens] |
| 102 | +//! admin = "MyAccessToken" |
| 103 | +//! ``` |
| 104 | +//! |
| 105 | +//! The token label is used to identify the token. All tokens have full access |
| 106 | +//! to the API. |
| 107 | +//! |
| 108 | +//! Refer to [torrust-tracker-configuration](https://docs.rs/torrust-tracker-configuration>) |
| 109 | +//! for more information about the API configuration. |
| 110 | +//! |
| 111 | +//! # Setup SSL (optional) |
| 112 | +//! |
| 113 | +//! The API server supports SSL. You can enable it by setting the `ssl_enabled` |
| 114 | +//! option to `true` in the configuration file. |
| 115 | +//! |
| 116 | +//! ```toml |
| 117 | +//! [http_api] |
| 118 | +//! enabled = true |
| 119 | +//! bind_address = "0.0.0.0:1212" |
| 120 | +//! ssl_enabled = true |
| 121 | +//! ssl_cert_path = "./storage/ssl_certificates/localhost.crt" |
| 122 | +//! ssl_key_path = "./storage/ssl_certificates/localhost.key" |
| 123 | +//! |
| 124 | +//! [http_api.access_tokens] |
| 125 | +//! admin = "MyAccessToken" |
| 126 | +//! ``` |
| 127 | +//! |
| 128 | +//! > **NOTICE**: If you are using a reverse proxy like NGINX, you can skip this |
| 129 | +//! step and use NGINX for the SSL instead. See |
| 130 | +//! [other alternatives to Nginx/certbot](https://github.com/torrust/torrust-tracker/discussions/131) |
| 131 | +//! |
| 132 | +//! > **NOTICE**: You can generate a self-signed certificate for localhost using |
| 133 | +//! OpenSSL. See [Let's Encrypt](https://letsencrypt.org/docs/certificates-for-localhost/). |
| 134 | +//! That's particularly useful for testing purposes. Once you have the certificate |
| 135 | +//! you need to set the `ssl_cert_path` and `ssl_key_path` options in the |
| 136 | +//! configuration file with the paths to the certificate (`localhost.crt`) and |
| 137 | +//! key (`localhost.key`) files. |
| 138 | +//! |
| 139 | +//! # API endpoints |
| 140 | +//! |
| 141 | +//! Refer to the [v1](crate::servers::apis::v1) module for the list of available |
| 142 | +//! API endpoints. |
1 | 143 | pub mod routes; |
2 | 144 | pub mod server; |
3 | 145 | pub mod v1; |
4 | 146 |
|
5 | 147 | use serde::Deserialize; |
6 | 148 |
|
| 149 | +/// The info hash URL path parameter. |
| 150 | +/// |
| 151 | +/// Some API endpoints require an info hash as a path parameter. |
| 152 | +/// |
| 153 | +/// For example: `http://localhost:1212/api/v1/torrent/{info_hash}`. |
| 154 | +/// |
| 155 | +/// The info hash represents teh value collected from the URL path parameter. |
| 156 | +/// It does not include validation as this is done by the API endpoint handler, |
| 157 | +/// in order to provide a more specific error message. |
7 | 158 | #[derive(Deserialize)] |
8 | 159 | pub struct InfoHashParam(pub String); |
0 commit comments