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
use crate::util::is_default;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(default)]
pub struct Handshake {
pub peer_id: String,
pub fileserver_port: usize,
pub time: u64,
#[serde(default, skip_serializing_if = "is_default")]
pub crypt: Option<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub crypt_supported: Vec<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub use_bin_type: bool,
#[serde(default, skip_serializing_if = "is_default")]
pub onion: Option<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub protocol: String,
#[serde(default, skip_serializing_if = "is_default")]
pub port_opened: Option<bool>,
#[serde(default, skip_serializing_if = "is_default")]
pub rev: usize,
#[serde(default, skip_serializing_if = "is_default", rename = "target_ip")]
pub target_address: Option<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub version: String,
}
impl Handshake {
pub fn new() -> Handshake {
let now = SystemTime::now();
Handshake {
version: "0.7".to_string(),
rev: 4486,
protocol: "v2".to_string(),
use_bin_type: true,
fileserver_port: 0,
port_opened: Some(false),
crypt_supported: vec![],
time: now.duration_since(UNIX_EPOCH).unwrap().as_secs(),
onion: None,
crypt: None,
target_address: None,
peer_id: String::new(),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
#[serde(default)]
pub struct Announce {
pub port: usize,
#[serde(default, skip_serializing_if = "is_default")]
pub add: Vec<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub need_types: Vec<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub need_num: usize,
#[serde(default, skip_serializing_if = "is_default")]
pub hashes: Vec<ByteBuf>,
#[serde(default, skip_serializing_if = "is_default")]
pub onions: Vec<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub onion_signs: Vec<ByteBuf>,
#[serde(default, skip_serializing_if = "is_default")]
pub onion_sign_this: String,
#[serde(default, skip_serializing_if = "is_default")]
pub delete: bool,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]
pub struct AnnounceResponse {
pub peers: Vec<AnnouncePeers>,
}
#[derive(Serialize, Deserialize, Default)]
#[serde(default)]
pub struct AnnouncePeers {
#[serde(rename = "ipv4", alias = "ip4", skip_serializing_if = "is_default")]
pub ip_v4: Vec<ByteBuf>,
#[serde(rename = "ipv6", skip_serializing_if = "is_default")]
pub ip_v6: Vec<ByteBuf>,
#[serde(rename = "onion", skip_serializing_if = "is_default")]
pub onion_v2: Vec<ByteBuf>,
#[serde(skip_serializing_if = "is_default")]
pub onion_v3: Vec<ByteBuf>,
#[serde(skip_serializing_if = "is_default")]
pub i2p_b32: Vec<ByteBuf>,
#[serde(skip_serializing_if = "is_default")]
pub loki: Vec<ByteBuf>,
}
impl Debug for AnnouncePeers {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let iterator = self
.ip_v4
.iter()
.chain(self.ip_v6.iter())
.chain(self.onion_v2.iter())
.chain(self.onion_v3.iter())
.chain(self.i2p_b32.iter())
.chain(self.loki.iter());
let strings: Vec<String> = iterator
.map(|ip| crate::PeerAddr::unpack(ip).unwrap().to_string())
.collect();
write!(f, "[{}]", strings.join(", "))
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GetFile {
pub site: String,
pub inner_path: String,
pub location: usize,
#[serde(skip_serializing_if = "is_default")]
pub file_size: usize,
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GetFileResponse {
pub body: ByteBuf,
pub location: usize,
pub size: usize,
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct StreamFile {
pub inner_path: String,
pub size: usize,
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct StreamFileResponse {
pub stream_bytes: usize,
}
pub struct Pex {
pub site: String,
pub peers: Vec<ByteBuf>,
pub peers_onion: Vec<ByteBuf>,
pub need: usize,
}
pub struct PexResponse {
pub peers: Vec<ByteBuf>,
pub peers_onion: Vec<ByteBuf>,
}
pub struct UpdateFile {
pub site: String,
pub inner_path: String,
pub body: ByteBuf,
pub diffs: Vec<Diff>,
}
pub struct Diff {
pub opcode: String,
pub diff: String,
}
pub struct UpdateFileResponse {
pub ok: bool,
}
pub struct ListModified {
pub site: String,
pub since: usize,
}
pub struct ListModifiedResponse {
pub modified_files: HashMap<String, usize>,
}
pub struct GetHashfield {
pub site: String,
}
pub struct GetHashfieldResponse {
pub hashfield_raw: ByteBuf,
}
pub struct SetHashfield {
pub site: String,
pub hashfield_raw: ByteBuf,
}
pub struct SetHashfieldResponse {
pub ok: bool,
}
pub struct FindHashIds {
pub site: String,
pub hash_ids: Vec<usize>,
}
pub struct FindHashIdsResponse {
pub peers: HashMap<usize, Vec<ByteBuf>>,
pub peers_onion: HashMap<usize, Vec<ByteBuf>>,
}
pub struct Checkport {
pub port: u16,
}
pub struct CheckportResponse {
pub status: String,
pub ip_external: String,
}
pub struct GetPieceFields {
pub site: String,
}
pub struct GetPieceFieldsResponse {
pub piecefields_packed: ByteBuf,
}
pub struct SetPieceFields {
pub site: String,
pub piecefields_packed: ByteBuf,
}
pub struct SetPieceFieldsResponse {
pub ok: bool,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]
pub struct Error {
pub error: String,
}