adtp 1.5.0-pre2

Alula's Data Transfer Protocol
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use crate::aead::OsRng;
use crate::aes::Aes256;
use aes_gcm::aead::Aead;
pub use aes_gcm::*;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use chrono::{DateTime, Utc};
use rsa::pkcs8::DecodePublicKey;
use rsa::{Pkcs1v15Encrypt, RsaPublicKey};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::io::{Read, Write};
use std::net::TcpStream;

const ADTP_VERSION: &str = "ADTP/1.5";

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn requests_test() {
        let request = Request::create(
            Method::Create,
            "buss://napture.web".to_string(),
            "test/0.0".to_string(),
        )
            .with_content_string(ContentType::Text, "Content".to_string())
            .with_language("us-english".to_string());
        println!("{}", request.clone().build());
        assert_eq!(
            request.clone().build().as_str(),
            Request::try_from(request.build()).unwrap().build()
        );
    }

    #[test]
    fn responses_test() {
        let response = Response::create(Status::Ok, "test/0.0".to_string())
            .with_language("us-english".to_string())
            .with_content_string(ContentType::Text, "Content".to_string());
        println!("{}", response.clone().build());
        assert_eq!(
            response.clone().build().as_str(),
            Response::try_from(response.build())
                .unwrap()
                .build()
        );
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Cookie {
    #[serde(rename = "key")]
    pub key: String,
    #[serde(rename = "value")]
    pub value: String,
}
impl Cookie {
    pub fn new(key: String, value: String) -> Cookie {
        Cookie { key, value }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum Method {
    /// Used to read existing data
    #[serde(rename = "read")]
    Read,
    /// Used to create new data
    #[serde(rename = "create")]
    Create,
    /// Used to update existing data
    #[serde(rename = "update")]
    Update,
    /// Used to delete existing data
    #[serde(rename = "destroy")]
    Destroy,
    /// Used to check if data exists. If uri is *, this acts as a server health check
    #[serde(rename = "check")]
    Check,
    /// Used to authorize with a server
    #[serde(rename = "auth")]
    Auth,
    /// Used to get this sessions encryption key. assume unencrypted data unless this is sent
    #[serde(rename = "key")]
    Key
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum ContentType {
    /// AAC Audio
    #[serde(rename = "aac")]
    AAC,
    /// AVIF Image
    #[serde(rename = "avif")]
    AVIF,
    /// Audio Video Interleaf
    #[serde(rename = "avi")]
    AVI,
    /// Raw Binary Data
    #[serde(rename = "binary")]
    Binary,
    /// Bitmap Image
    #[serde(rename = "bmp")]
    BMP,
    /// Cascading Style Sheets in UTF-8
    #[serde(rename = "css")]
    CSS,
    /// Comma Seperated Values in UTF-8
    #[serde(rename = "csv")]
    CSV,
    /// Microsoft Word Document
    #[serde(rename = "docx")]
    DOCX,
    /// EPUB Book
    #[serde(rename = "epub")]
    EPUB,
    /// GIF Image
    #[serde(rename = "gif")]
    GIF,
    /// HTML in UTF-8
    #[serde(rename = "html")]
    HTML,
    /// Icon File
    #[serde(rename = "ico")]
    ICO,
    /// JPEG Image File
    #[serde(rename = "jpeg")]
    JPEG,
    /// JavaScript in UTF-8
    #[serde(rename = "javascript")]
    JavaScript,
    /// JavaScript Object Notation in UTF-8
    #[serde(rename = "json")]
    JSON,
    /// Markdown in UTF-8
    #[serde(rename = "markdown")]
    MarkDown,
    /// MIDI
    #[serde(rename = "midi")]
    MIDI,
    /// MP3 Audio
    #[serde(rename = "mp3")]
    MP3,
    /// MP4 Video
    #[serde(rename = "mp4")]
    MP4,
    /// MPEG Video
    #[serde(rename = "mpeg")]
    MPEG,
    /// OpenDocument Presentation
    #[serde(rename = "odp")]
    ODP,
    /// OpenDocument Spreadsheet
    #[serde(rename = "ods")]
    ODS,
    /// OpenDocument Text
    #[serde(rename = "odt")]
    ODT,
    /// Ogg Audio
    #[serde(rename = "oga")]
    OGA,
    /// Ogg Video
    #[serde(rename = "ogv")]
    OGV,
    /// Ogg
    #[serde(rename = "ogx")]
    OGX,
    /// Opus audio in Ogg Container
    #[serde(rename = "opus")]
    Opus,
    /// OpenType Font
    #[serde(rename = "otf")]
    OTF,
    /// PNG Image
    #[serde(rename = "png")]
    PNG,
    /// PDF Document
    #[serde(rename = "pdf")]
    PDF,
    /// PHP Page
    #[serde(rename = "php")]
    PHP,
    /// Microsoft PowerPoint Presentation
    #[serde(rename = "pptx")]
    PPTX,
    /// RAR Archive
    #[serde(rename = "rar")]
    RAR,
    /// SVG Graphics
    #[serde(rename = "svg")]
    SVG,
    /// TAR Archive
    #[serde(rename = "tar")]
    TAR,
    /// TIFF Image
    #[serde(rename = "tiff")]
    TIFF,
    /// TrueType Font
    #[serde(rename = "ttf")]
    TTF,
    /// Plain Text (UTF-8)
    #[serde(rename = "text")]
    Text,
    /// Waveform Audio
    #[serde(rename = "wav")]
    WAV,
    /// Webm Audio
    #[serde(rename = "weba")]
    WEBA,
    /// Webm Video
    #[serde(rename = "webm")]
    WEBM,
    /// Webm Image
    #[serde(rename = "webp")]
    WEBP,
    /// Woff font (WOFF1 or WOFF2)
    #[serde(rename = "woff")]
    WOFF,
    /// Microsoft Excel Spreadsheet
    #[serde(rename = "xlsx")]
    XLSX,
    /// XML (UTF-8)
    #[serde(rename = "xml")]
    XML,
    /// Zip Archive
    #[serde(rename = "zip")]
    ZIP,
    /// 7z archive
    #[serde(rename = "7z")]
    SevenZ,
    #[serde(rename = "none")]
    None,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Request {
    #[serde(rename = "version")]
    version: String,
    #[serde(rename = "uri")]
    pub uri: String,
    #[serde(rename = "sent")]
    pub sent: String,
    #[serde(rename = "agent")]
    pub agent: String,
    #[serde(rename = "method")]
    pub method: Method,
    #[serde(rename = "lang")]
    pub language: String,
    #[serde(rename = "cookies")] // Cookies requested by the server
    pub cookies: Vec<Cookie>,
    #[serde(rename = "nonce")]
    pub nonce: String,
    #[serde(rename = "contentType")]
    pub content_type: ContentType,
    #[serde(rename = "content")]
    pub content: Vec<u8>,
}

pub fn date_time_string(date_time: DateTime<Utc>) -> String {
    date_time.to_rfc3339()
}

impl Request {
    pub fn create(method: Method, uri: String, agent: String) -> Request {
        Request {
            version: ADTP_VERSION.to_string(),
            uri,
            sent: date_time_string(Utc::now()),
            agent,
            language: "none".to_string(),
            cookies: vec![],
            nonce: "".to_string(),
            method,
            content_type: ContentType::None,
            content: vec![],
        }
    }

    pub fn with_time(mut self, sent: DateTime<Utc>) -> Request {
        self.sent = date_time_string(sent);
        self
    }

    pub fn with_language(mut self, language: String) -> Request {
        self.language = language;
        self
    }

    pub fn with_cookie(mut self, cookie: Cookie) -> Request {
        self.cookies.push(cookie);
        self
    }

    pub fn with_content(mut self, content_type: ContentType, content: Vec<u8>) -> Request {
        self.content_type = content_type;
        self.content = content;
        self
    }

    pub fn with_content_string(mut self, content_type: ContentType, content: String) -> Request {
        self.content_type = content_type;
        self.content = content.into_bytes();
        self
    }

    pub fn build(self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}
impl TryFrom<String> for Request {
    type Error = String;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        match serde_json::from_str(&value) {
            Ok(r) => Ok(r),
            Err(e) => Err(format!("Failed to parse JSON: {}", e)),
        }
    }
}
impl TryFrom<Vec<u8>> for Request {
    type Error = String;
    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        match String::from_utf8(value) {
            Ok(r) => match serde_json::from_str(r.as_str()) {
                Ok(r) => Ok(r),
                Err(e) => Err(format!("Failed to parse JSON: {}", e)),
            },
            Err(e) => Err(format!("Failed to parse JSON: {}", e)),
        }
    }
}
impl Display for Request {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum Status {
    #[serde(rename = "switch-protocols")]
    SwitchProtocols, // Server is switching protocol. Check content for protocol type
    #[serde(rename = "ok")]
    Ok, // server has handled your request
    #[serde(rename = "pending")]
    Pending, // Request is pending. Wait for follow up.
    #[serde(rename = "redirect")]
    Redirect, // Content is no longer at requested location. Check content for new location.
    #[serde(rename = "denied")]
    Denied, // Server has denied request.
    #[serde(rename = "bad-request")]
    BadRequest, // Request is malformed.
    #[serde(rename = "unauthorized")]
    Unauthorized, // You are not authorized to view the content
    #[serde(rename = "not-found")]
    NotFound, // Server could not find the requested content.
    #[serde(rename = "too-many-requests")]
    TooManyRequests, // Server has received too many requests from this client
    #[serde(rename = "internal-error")]
    InternalError, // Server has encountered an error
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Response {
    #[serde(rename = "version")]
    version: String,
    #[serde(rename = "status")]
    pub status: Status,
    #[serde(rename = "sent")]
    pub sent: String,
    #[serde(rename = "agent")]
    pub agent: String,
    #[serde(rename = "language")]
    pub language: String,
    #[serde(rename = "cookies")] // Cookies for the client to save
    pub cookies: Vec<Cookie>,
    #[serde(rename = "nonce")]
    pub nonce: String,
    #[serde(rename = "contentType")]
    pub content_type: ContentType,
    #[serde(rename = "content")]
    pub content: Vec<u8>,
}
impl Response {
    pub fn create(status: Status, agent: String) -> Response {
        Response {
            version: ADTP_VERSION.to_string(),
            status,
            sent: date_time_string(Utc::now()),
            agent,
            language: "none".to_string(),
            nonce: "".to_string(),
            cookies: vec![],
            content_type: ContentType::None,
            content: vec![],
        }
    }

    pub fn with_time(mut self, sent: DateTime<Utc>) -> Response {
        self.sent = date_time_string(sent);
        self
    }

    pub fn with_language(mut self, language: String) -> Response {
        self.language = language;
        self
    }

    pub fn with_cookie(mut self, cookie: Cookie) -> Response {
        self.cookies.push(cookie);
        self
    }

    pub fn with_content(mut self, content_type: ContentType, content: Vec<u8>) -> Response {
        self.content_type = content_type;
        self.content = content;
        self
    }

    pub fn decrypt_content(&mut self, key: Key<Aes256>) -> Vec<u8> {
        let cipher = Aes256Gcm::new(Key::<Aes256>::from_slice(key.as_slice()));
        cipher.decrypt(Nonce::from_slice(STANDARD.decode(self.nonce.clone()).unwrap().as_slice()), self.content.as_slice()).unwrap()
    }

    pub fn with_content_string(mut self, content_type: ContentType, content: String) -> Response {
        self.content_type = content_type;
        self.content = content.into_bytes();
        self
    }

    pub fn build(self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}

impl TryFrom<String> for Response {
    type Error = String;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        match serde_json::from_str(&value) {
            Ok(r) => Ok(r),
            Err(e) => Err(format!("Failed to parse JSON: {}", e)),
        }
    }
}
impl TryFrom<Vec<u8>> for Response {
    type Error = String;
    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        match String::from_utf8(value) {
            Ok(r) => match serde_json::from_str(r.as_str()) {
                Ok(r) => Ok(r),
                Err(e) => Err(format!("Failed to parse JSON: {}", e)),
            },
            Err(e) => Err(format!("Failed to parse JSON: {}", e)),
        }
    }
}

impl Display for Response {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap())
    }
}

pub struct Connection {
    socket: TcpStream,
    secured: bool,
    aes_key: Option<Key<Aes256Gcm>>,
    server_public_key: Option<RsaPublicKey>,
}
impl Connection {
    pub fn connect(address: &str) -> Result<Connection, String> {
        let socket: TcpStream = match TcpStream::connect(address) {
            Ok(s) => s,
            Err(e) => return Err(format!("Failed to connect to server: {}", e)),
        };

        let conn = Connection {
            socket,
            secured: false,
            aes_key: None,
            server_public_key: None,
        };

        Ok(conn)
    }

    pub fn connect_secure(address: &str) -> Result<Connection, String> {
        let mut conn = Self::connect(address)?;

        let _ = conn.send_request(Request::create(Method::Key, "".to_string(), "ADTP/INTERNAL/1.5".to_string()));

        let mut resp = conn.get_response()?;

        conn.server_public_key = Some(RsaPublicKey::from_public_key_pem(String::from_utf8_lossy(resp.content.as_slice()).to_string().trim()).unwrap());

        conn.aes_key = Some(Aes256Gcm::generate_key(&mut OsRng));

        let mut payload = Vec::new();

        payload.extend_from_slice(&conn.aes_key.unwrap());

        let encrypted = conn.server_public_key.clone().unwrap().encrypt(&mut OsRng, Pkcs1v15Encrypt, &payload).unwrap();

        let req = Request::create(Method::Key, "".to_string(), "ADTP/INTERNAL/1.5".to_string()).with_content(ContentType::Binary, encrypted);

        conn.send_request(req)?;

        resp = conn.get_response()?;

        if resp.status == Status::Ok {
            conn.secured = true;
        }

        Ok(conn)
    }

    pub fn send_request(&mut self, mut request: Request) -> Result<(), String> {
        if self.secured {
            let cipher = Aes256Gcm::new(&self.aes_key.unwrap());
            let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
            request.nonce = STANDARD.encode(nonce.as_slice());
            let payload = cipher.encrypt(&nonce, request.content.as_slice()).unwrap();
            request.content = payload;
            let _ = self.socket.write(request.build().as_bytes());
            Ok(())
        }
        else {
            let _ = self.socket.write(request.build().as_bytes());

            Ok(())
        }
    }

    pub fn get_response(&mut self) -> Result<Response, String> {
        let mut buffer: Vec<u8> = vec![0; 2048];
        if self.secured {
            let cipher = Aes256Gcm::new(&self.aes_key.unwrap());
            let n = self.socket.read(&mut buffer).unwrap();

            let mut resp = Response::try_from(String::from_utf8_lossy(&buffer[0..n]).to_string())?;
            let payload = cipher.decrypt(Nonce::from_slice(STANDARD.decode(resp.nonce.clone()).unwrap().as_slice()), resp.content.as_slice()).unwrap();
            resp.content = payload;

            Ok(resp)
        }
        else {
            let n = self.socket.read(buffer.as_mut_slice()).unwrap();

            Ok(Response::try_from(String::from_utf8_lossy(&buffer[0..n]).to_string())?)
        }
    }
}