forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.rs
More file actions
186 lines (160 loc) · 6.51 KB
/
Copy pathfilters.rs
File metadata and controls
186 lines (160 loc) · 6.51 KB
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
use std::convert::Infallible;
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use std::sync::Arc;
use warp::{reject, Filter, Rejection};
use super::error::Error;
use super::{request, WebResult};
use crate::protocol::common::MAX_SCRAPE_TORRENTS;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::{self, auth, peer};
/// Pass Arc<tracker::TorrentTracker> along
#[must_use]
pub fn with_tracker(
tracker: Arc<tracker::Tracker>,
) -> impl Filter<Extract = (Arc<tracker::Tracker>,), Error = Infallible> + Clone {
warp::any().map(move || tracker.clone())
}
/// Check for infoHash
#[must_use]
pub fn with_info_hash() -> impl Filter<Extract = (Vec<InfoHash>,), Error = Rejection> + Clone {
warp::filters::query::raw().and_then(|q| async move { info_hashes(&q) })
}
/// Check for `PeerId`
#[must_use]
pub fn with_peer_id() -> impl Filter<Extract = (peer::Id,), Error = Rejection> + Clone {
warp::filters::query::raw().and_then(|q| async move { peer_id(&q) })
}
/// Pass Arc<tracker::TorrentTracker> along
#[must_use]
pub fn with_auth_key() -> impl Filter<Extract = (Option<auth::Key>,), Error = Infallible> + Clone {
warp::path::param::<String>()
.map(|key: String| auth::Key::from_string(&key))
.or_else(|_| async { Ok::<(Option<auth::Key>,), Infallible>((None,)) })
}
/// Check for `PeerAddress`
#[must_use]
pub fn with_peer_addr(on_reverse_proxy: bool) -> impl Filter<Extract = (IpAddr,), Error = Rejection> + Clone {
warp::addr::remote()
.and(warp::header::optional::<String>("X-Forwarded-For"))
.map(move |remote_addr: Option<SocketAddr>, x_forwarded_for: Option<String>| {
(on_reverse_proxy, remote_addr, x_forwarded_for)
})
.and_then(|q| async move { peer_addr(q) })
}
/// Check for `request::Announce`
#[must_use]
pub fn with_announce_request(on_reverse_proxy: bool) -> impl Filter<Extract = (request::Announce,), Error = Rejection> + Clone {
warp::filters::query::query::<request::AnnounceQuery>()
.and(with_info_hash())
.and(with_peer_id())
.and(with_peer_addr(on_reverse_proxy))
.and_then(|q, r, s, t| async move { announce_request(q, &r, s, t) })
}
/// Check for `ScrapeRequest`
#[must_use]
pub fn with_scrape_request(on_reverse_proxy: bool) -> impl Filter<Extract = (request::Scrape,), Error = Rejection> + Clone {
warp::any()
.and(with_info_hash())
.and(with_peer_addr(on_reverse_proxy))
.and_then(|q, r| async move { scrape_request(q, r) })
}
/// Parse `InfoHash` from raw query string
#[allow(clippy::ptr_arg)]
fn info_hashes(raw_query: &String) -> WebResult<Vec<InfoHash>> {
let split_raw_query: Vec<&str> = raw_query.split('&').collect();
let mut info_hashes: Vec<InfoHash> = Vec::new();
for v in split_raw_query {
if v.contains("info_hash") {
let raw_info_hash = v.split('=').collect::<Vec<&str>>()[1];
let info_hash_bytes = percent_encoding::percent_decode_str(raw_info_hash).collect::<Vec<u8>>();
let info_hash = InfoHash::from_str(&hex::encode(info_hash_bytes));
if let Ok(ih) = info_hash {
info_hashes.push(ih);
}
}
}
if info_hashes.len() > MAX_SCRAPE_TORRENTS as usize {
Err(reject::custom(Error::ExceededInfoHashLimit))
} else if info_hashes.is_empty() {
Err(reject::custom(Error::InvalidInfo))
} else {
Ok(info_hashes)
}
}
/// Parse `PeerId` from raw query string
#[allow(clippy::ptr_arg)]
fn peer_id(raw_query: &String) -> WebResult<peer::Id> {
// put all query params in a vec
let split_raw_query: Vec<&str> = raw_query.split('&').collect();
let mut peer_id: Option<peer::Id> = None;
for v in split_raw_query {
// look for the peer_id param
if v.contains("peer_id") {
// get raw percent_encoded peer_id
let raw_peer_id = v.split('=').collect::<Vec<&str>>()[1];
// decode peer_id
let peer_id_bytes = percent_encoding::percent_decode_str(raw_peer_id).collect::<Vec<u8>>();
// peer_id must be 20 bytes
if peer_id_bytes.len() != 20 {
return Err(reject::custom(Error::InvalidPeerId));
}
// clone peer_id_bytes into fixed length array
let mut byte_arr: [u8; 20] = Default::default();
byte_arr.clone_from_slice(peer_id_bytes.as_slice());
peer_id = Some(peer::Id(byte_arr));
break;
}
}
match peer_id {
Some(id) => Ok(id),
None => Err(reject::custom(Error::InvalidPeerId)),
}
}
/// Get `PeerAddress` from `RemoteAddress` or Forwarded
fn peer_addr((on_reverse_proxy, remote_addr, x_forwarded_for): (bool, Option<SocketAddr>, Option<String>)) -> WebResult<IpAddr> {
if !on_reverse_proxy && remote_addr.is_none() {
return Err(reject::custom(Error::AddressNotFound));
}
if on_reverse_proxy && x_forwarded_for.is_none() {
return Err(reject::custom(Error::AddressNotFound));
}
if on_reverse_proxy {
let mut x_forwarded_for_raw = x_forwarded_for.unwrap();
// remove whitespace chars
x_forwarded_for_raw.retain(|c| !c.is_whitespace());
// get all forwarded ip's in a vec
let x_forwarded_ips: Vec<&str> = x_forwarded_for_raw.split(',').collect();
// set client ip to last forwarded ip
let x_forwarded_ip = *x_forwarded_ips.last().unwrap();
IpAddr::from_str(x_forwarded_ip).map_err(|_| reject::custom(Error::AddressNotFound))
} else {
Ok(remote_addr.unwrap().ip())
}
}
/// Parse `AnnounceRequest` from raw `AnnounceRequestQuery`, `InfoHash` and Option<SocketAddr>
#[allow(clippy::unnecessary_wraps)]
#[allow(clippy::ptr_arg)]
fn announce_request(
announce_request_query: request::AnnounceQuery,
info_hashes: &Vec<InfoHash>,
peer_id: peer::Id,
peer_addr: IpAddr,
) -> WebResult<request::Announce> {
Ok(request::Announce {
info_hash: info_hashes[0],
peer_addr,
downloaded: announce_request_query.downloaded.unwrap_or(0),
uploaded: announce_request_query.uploaded.unwrap_or(0),
peer_id,
port: announce_request_query.port,
left: announce_request_query.left.unwrap_or(0),
event: announce_request_query.event,
compact: announce_request_query.compact,
})
}
/// Parse `ScrapeRequest` from `InfoHash`
#[allow(clippy::unnecessary_wraps)]
fn scrape_request(info_hashes: Vec<InfoHash>, peer_addr: IpAddr) -> WebResult<request::Scrape> {
Ok(request::Scrape { info_hashes, peer_addr })
}