ftp-client 0.1.2

A FTP client library
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Implement the Client and most actual functionality
//! for it.
//!
//! Most functions were implemented using the RFC959 as reference
//! and may not work as expected with deviant server implementations.
use crate::status_code::{StatusCode, StatusCodeKind};
use derive_more::From;
use log::warn;
use native_tls::{TlsConnector, TlsStream};
use std::io::prelude::*;
use std::io::BufReader;
use std::io::{Read, Write};
use std::net::TcpStream;

/// Represents a raw server response, with
/// a status code and the message after it.
///
/// Note that usual FTP responses follow the format:
/// STATUS_CODE: MESSAGE.
#[derive(Debug, PartialEq)]
pub struct ServerResponse {
    message: String,
    status_code: StatusCode,
}

impl ServerResponse {
    /// Summarize an error message, when a response is not one of the expect
    /// status codes provided.
    pub fn summarize_error(&self, expected: Vec<StatusCodeKind>) -> String {
        format!(
            "Got {}: {}, expected {:?}",
            self.status_code.code, self.message, expected
        )
    }
}

/// A FTP client can run in two main modes: active and passive.
///
/// The passive mode is the simpler to run, you simply ask the
/// server for a host to connect and connect to it, this is your
/// data connection (for more information about the FTP protocol, check
/// RFC959).
///
/// The active mode is different, you must open a port on your machine
/// and use that to connect to the server. This can be a problem if you
/// have firewalls set on your machine/network, for the common user, the
/// passive mode should work fine.
///
/// The ExtendedPassive mode listed is simply the passive mode with support for
/// IPV6.
#[derive(Debug, Clone, Copy)]
pub enum ClientMode {
    /// The passive mode, using the PASV command
    Passive,
    /// The extended passive mode, using the EPSV command
    ExtendedPassive,
    /// The active mode, not implemented yet
    Active,
}

impl ServerResponse {
    /// Parse a server response from the server text response.
    pub fn parse(text: &str) -> Self {
        let status_code = StatusCode::parse(text);
        let message = text[3..].trim().to_string();

        Self {
            message,
            status_code,
        }
    }

    /// Returns whether the status code returned indicates failure.
    pub fn is_failure_status(&self) -> bool {
        self.status_code.is_failure()
    }
}

#[derive(From)]
enum ClientStream {
    TcpStream(TcpStream),
    TlsStream(TlsStream<TcpStream>),
}

impl ClientStream {
    pub fn peer_addr(&self) -> Result<std::net::SocketAddr, std::io::Error> {
        match self {
            ClientStream::TcpStream(stream) => stream.peer_addr(),
            ClientStream::TlsStream(stream) => stream.get_ref().peer_addr(),
        }
    }
}

impl Read for ClientStream {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
        match self {
            ClientStream::TcpStream(stream) => stream.read(buf),
            ClientStream::TlsStream(stream) => stream.read(buf),
        }
    }
}

impl Write for ClientStream {
    fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
        match self {
            ClientStream::TcpStream(stream) => stream.write(buf),
            ClientStream::TlsStream(stream) => stream.write(buf),
        }
    }

    fn flush(&mut self) -> Result<(), std::io::Error> {
        match self {
            ClientStream::TcpStream(stream) => stream.flush(),
            ClientStream::TlsStream(stream) => stream.flush(),
        }
    }
}

/// The Client is where most of the functionality is, it keeps
/// a control connection open and opens up data connections as
/// commands are issued. This struct is a very thin wrapper over
/// the FTP protocol.
pub struct Client {
    stream: BufReader<ClientStream>,
    buffer: String,
    welcome_string: Option<String>,
    mode: ClientMode,
}

impl Client {
    /// Set the mode for the client.
    pub fn set_mode(&mut self, mode: ClientMode) {
        self.mode = mode
    }

    /// Connect to a new FTP server using plain text (no TLS).
    pub fn connect(
        hostname: &str,
        user: &str,
        password: &str,
    ) -> Result<Self, crate::error::Error> {
        Self::connect_with_port(hostname, 21, user, password)
    }

    /// Connect to a new FTP server using a secure connection (TLS).
    pub fn connect_tls(
        hostname: &str,
        user: &str,
        password: &str,
    ) -> Result<Self, crate::error::Error> {
        Self::connect_tls_with_port(hostname, 21, user, password)
    }

    /// Connect to a new FTP server using a secure connection (TLS) on a specific port.
    pub fn connect_tls_with_port(
        hostname: &str,
        port: u32,
        user: &str,
        password: &str,
    ) -> Result<Self, crate::error::Error> {
        let connector = TlsConnector::new()?;

        let host = format!("{}:{}", hostname, port);
        let raw_stream = TcpStream::connect(&host)?;
        let tls_stream = connector.connect(&host, raw_stream)?;
        let stream: BufReader<ClientStream> = BufReader::new(tls_stream.into());

        let buffer = String::new();
        let mut client = Client {
            stream,
            buffer,
            welcome_string: None,
            mode: ClientMode::ExtendedPassive,
        };
        let response = client.parse_reply_expecting(vec![StatusCodeKind::ReadyForNewUser])?;
        client.welcome_string = Some(response.message);
        client.login(user, password)?;

        Ok(client)
    }

    /// Connect to a new FTP server using plain text (no TLS) on a specific port.
    pub fn connect_with_port(
        hostname: &str,
        port: u32,
        user: &str,
        password: &str,
    ) -> Result<Self, crate::error::Error> {
        let host = format!("{}:{}", hostname, port);
        let raw_stream = TcpStream::connect(&host)?;
        let stream = BufReader::new(raw_stream.into());

        let buffer = String::new();
        let mut client = Client {
            stream,
            buffer,
            welcome_string: None,
            mode: ClientMode::ExtendedPassive,
        };
        let response = client.parse_reply_expecting(vec![StatusCodeKind::ReadyForNewUser])?;
        client.welcome_string = Some(response.message);
        client.login(user, password)?;

        Ok(client)
    }

    /// Get the welcome message sent by the server at the connection establishment.
    pub fn get_welcome(&self) -> Option<&String> {
        self.welcome_string.as_ref()
    }

    /// Login using the given user and password.
    /// Note that many servers require a login with an anonymous user,
    /// such as client.login("anonymous", "anonymous@mail.com").
    pub fn login(&mut self, user: &str, password: &str) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting("USER", user, vec![StatusCodeKind::PasswordRequired])?;
        self.write_unary_command_expecting("PASS", password, vec![StatusCodeKind::UserLoggedIn])?;

        Ok(())
    }

    /// Logout from the current user/password pair.
    pub fn logout(&mut self) -> Result<(), crate::error::Error> {
        self.write_command_expecting("QUIT", vec![StatusCodeKind::ClosingControlConnection])?;

        Ok(())
    }

    /// Change the working directory on the current session.
    pub fn cwd(&mut self, dir: &str) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting(
            "CWD",
            dir,
            vec![StatusCodeKind::RequestFileActionCompleted],
        )?;

        Ok(())
    }

    /// Go up to the parent directory on the current session.
    pub fn cdup(&mut self) -> Result<(), crate::error::Error> {
        self.write_command_expecting("CDUP", vec![StatusCodeKind::RequestFileActionCompleted])?;

        Ok(())
    }

    /// Show server information regarding its implementation status
    /// to the user.
    ///
    /// The help command can also be used with an argument to see detailed
    /// information about a single command, this behaviour is not implemented.
    pub fn help(&mut self) -> Result<(), crate::error::Error> {
        self.write_command_expecting(
            "HELP",
            vec![StatusCodeKind::SystemStatus, StatusCodeKind::HelpMessage],
        )?;
        Ok(())
    }

    /// This command should not do anything other than receiving
    /// an OK response from the server.
    pub fn noop(&mut self) -> Result<(), crate::error::Error> {
        self.write_command_expecting("NOOP", vec![StatusCodeKind::Ok])?;
        Ok(())
    }

    /// Set the transfer type to ascii
    pub fn ascii(&mut self) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting("TYPE", "A", vec![StatusCodeKind::Ok])?;
        Ok(())
    }

    /// Set the transfer type to binary
    pub fn binary(&mut self) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting("TYPE", "I", vec![StatusCodeKind::Ok])?;
        Ok(())
    }

    /// Get the current reported status from the server. This can be used
    /// during transfer and between them. This command can be used with
    /// and argument to get behaviour similar to LIST, this particular
    /// behaviour is not implemented.
    pub fn status(&mut self) -> Result<String, crate::error::Error> {
        let response = self.write_command_expecting("STAT", vec![StatusCodeKind::SystemStatus])?;

        Ok(response.message)
    }

    /// List the provided path in any way the server desires.
    pub fn list(&mut self, path: &str) -> Result<String, crate::error::Error> {
        let mut conn = self.get_data_connection()?;
        self.write_unary_command_expecting(
            "LIST",
            path,
            vec![
                StatusCodeKind::TransferStarted,
                StatusCodeKind::TransferAboutToStart,
            ],
        )?;

        let mut buffer = Vec::with_capacity(1024);
        conn.read_to_end(&mut buffer)?;
        self.parse_reply_expecting(vec![StatusCodeKind::RequestActionCompleted])?;
        let text = String::from_utf8(buffer).map_err(|_| {
            crate::error::Error::SerializationFailed(
                "Invalid ASCII returned on server directory listing.".to_string(),
            )
        })?;
        Ok(text)
    }

    /// List the provided path, providing only name information about files and directories.
    pub fn list_names(&mut self, path: &str) -> Result<Vec<String>, crate::error::Error> {
        let mut conn = self.get_data_connection()?;
        self.write_unary_command_expecting(
            "NLST",
            path,
            vec![
                StatusCodeKind::TransferStarted,
                StatusCodeKind::TransferAboutToStart,
            ],
        )?;

        let mut buffer = Vec::with_capacity(1024);
        conn.read_to_end(&mut buffer)?;
        self.parse_reply_expecting(vec![StatusCodeKind::RequestActionCompleted])?;
        let text = String::from_utf8(buffer).map_err(|_| {
            crate::error::Error::SerializationFailed(
                "Invalid ASCII returned on server directory name listing.".to_string(),
            )
        })?;
        Ok(text.lines().map(|line| line.to_owned()).collect())
    }

    /// Store a new file on a provided path and name.
    pub fn store<B: AsRef<[u8]>>(
        &mut self,
        path: &str,
        data: B,
    ) -> Result<(), crate::error::Error> {
        // Scope connection so it drops before reading server reply.
        {
            let mut conn = self.get_data_connection()?;
            self.write_unary_command_expecting(
                "STOR",
                path,
                vec![
                    StatusCodeKind::TransferStarted,
                    StatusCodeKind::TransferAboutToStart,
                ],
            )?;
            conn.get_mut().write_all(data.as_ref())?;
        }

        self.parse_reply_expecting(vec![StatusCodeKind::RequestActionCompleted])?;

        Ok(())
    }

    /// Store a new file on a provided path using a random unique name.
    pub fn store_unique<B: AsRef<[u8]>>(&mut self, data: B) -> Result<String, crate::error::Error> {
        // Scope connection so it drops before reading server reply.
        {
            let mut conn = self.get_data_connection()?;
            self.write_command_expecting(
                "STOU",
                vec![
                    StatusCodeKind::TransferStarted,
                    StatusCodeKind::TransferAboutToStart,
                ],
            )?;
            conn.get_mut().write_all(data.as_ref())?;
        }

        let reply = self.parse_reply_expecting(vec![StatusCodeKind::RequestActionCompleted])?;

        Ok(reply.message)
    }

    /// Append to a existing file or a create a new one.
    pub fn append<B: AsRef<[u8]>>(
        &mut self,
        path: &str,
        data: B,
    ) -> Result<(), crate::error::Error> {
        // Scope connection so it drops before reading server reply.
        {
            let mut conn = self.get_data_connection()?;
            self.write_unary_command_expecting(
                "APPE",
                path,
                vec![
                    StatusCodeKind::TransferStarted,
                    StatusCodeKind::TransferAboutToStart,
                ],
            )?;
            conn.get_mut().write_all(data.as_ref())?;
        }

        self.parse_reply_expecting(vec![
            StatusCodeKind::RequestActionCompleted,
            StatusCodeKind::RequestFileActionCompleted,
        ])?;

        Ok(())
    }

    /// Restart a file transfer. Unimplemented.
    pub fn restart(&mut self) -> Result<(), crate::error::Error> {
        unimplemented!();
    }

    /// Abort a file transfer. Unimplemented.
    pub fn abort(&mut self) -> Result<(), crate::error::Error> {
        unimplemented!();
    }

    /// Preallocate space on the server. Unimplemented.
    pub fn allocate(
        &mut self,
        _logical_size: usize,
        _logical_page_size: Option<usize>,
    ) -> Result<(), crate::error::Error> {
        unimplemented!();
    }

    /// Move a file from a path to another, essentially renaming it.
    pub fn rename_file(
        &mut self,
        path_from: &str,
        path_to: &str,
    ) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting(
            "RNFR",
            path_from,
            vec![StatusCodeKind::RequestActionPending],
        )?;
        self.write_unary_command_expecting(
            "RNTO",
            path_to,
            vec![StatusCodeKind::RequestFileActionCompleted],
        )?;

        Ok(())
    }

    /// Remove an existing directory.
    pub fn remove_directory(&mut self, dir_path: &str) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting(
            "RMD",
            dir_path,
            vec![StatusCodeKind::RequestFileActionCompleted],
        )?;
        Ok(())
    }

    /// Make a new directory.
    pub fn make_directory(&mut self, dir_path: &str) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting("MKD", dir_path, vec![StatusCodeKind::PathCreated])?;
        Ok(())
    }

    /// Get the current working directory.
    pub fn pwd(&mut self) -> Result<String, crate::error::Error> {
        let response = self.write_command_expecting("PWD", vec![StatusCodeKind::PathCreated])?;
        Ok(response.message)
    }

    /// This command is used by the server to provide services
    /// specific to his system that are essential to file transfer
    /// but not sufficiently universal to be included as commands in
    /// the protocol.
    ///
    /// The nature of these services and the
    /// specification of their syntax can be stated in a reply to
    /// the HELP SITE command.
    ///
    /// Extracted from RFC959.
    pub fn site_parameters(&mut self) -> Result<String, crate::error::Error> {
        let response = self.write_command_expecting(
            "SITE",
            vec![StatusCodeKind::Ok, StatusCodeKind::FeatureNotImplemented],
        )?;

        Ok(response.message)
    }

    /// Get the type of operating system on the server.
    pub fn system(&mut self) -> Result<String, crate::error::Error> {
        let response =
            self.write_command_expecting("SYST", vec![StatusCodeKind::NameSystemType])?;

        Ok(response.message)
    }

    /// Delete a file at a path.
    pub fn delete_file(&mut self, dir_path: &str) -> Result<(), crate::error::Error> {
        self.write_unary_command_expecting(
            "DELE",
            dir_path,
            vec![StatusCodeKind::RequestFileActionCompleted],
        )?;

        Ok(())
    }

    /// Download a file at a path into a byte buffer.
    pub fn retrieve_file(&mut self, path: &str) -> Result<Vec<u8>, crate::error::Error> {
        let mut conn = self.get_data_connection()?;
        self.write_unary_command_expecting(
            "RETR",
            path,
            vec![
                StatusCodeKind::TransferAboutToStart,
                StatusCodeKind::TransferStarted,
            ],
        )?;

        let mut buffer = Vec::with_capacity(1024);
        conn.read_to_end(&mut buffer)?;
        self.parse_reply_expecting(vec![StatusCodeKind::RequestActionCompleted])?;
        Ok(buffer)
    }

    /// Acquire the data connection using the current ClientMode.
    pub fn get_data_connection(&mut self) -> Result<BufReader<TcpStream>, crate::error::Error> {
        match self.mode {
            ClientMode::Active => unimplemented!(),
            ClientMode::Passive => self.passive_mode_connection(),
            ClientMode::ExtendedPassive => self.extended_passive_mode_connection(),
        }
    }

    /// Create a extended passive mode connection.
    pub fn extended_passive_mode_connection(
        &mut self,
    ) -> Result<BufReader<TcpStream>, crate::error::Error> {
        let response =
            self.write_command_expecting("EPSV", vec![StatusCodeKind::EnteredExtendedPassiveMode])?;
        let socket = self.decode_extended_passive_mode_socket(&response.message)?;

        Ok(BufReader::new(TcpStream::connect(socket)?))
    }

    /// Create a passive mode connection.
    pub fn passive_mode_connection(&mut self) -> Result<BufReader<TcpStream>, crate::error::Error> {
        let response =
            self.write_command_expecting("PASV", vec![StatusCodeKind::EnteredPassiveMode])?;
        let socket = self.decode_passive_mode_ip(&response.message)?;

        Ok(BufReader::new(TcpStream::connect(socket)?))
    }

    /// Write a command with one argument to the server expecting a list of positive status codes.
    pub fn write_unary_command_expecting(
        &mut self,
        cmd: &str,
        arg: &str,
        valid_statuses: Vec<StatusCodeKind>,
    ) -> Result<ServerResponse, crate::error::Error> {
        self.write_unary_command(cmd, arg)?;
        self.parse_reply_expecting(valid_statuses)
    }

    /// Write a command with one argument to the server.
    pub fn write_unary_command(&mut self, cmd: &str, arg: &str) -> Result<(), crate::error::Error> {
        let text = format!("{} {}\r\n", cmd, arg);
        self.stream.get_mut().write_all(text.as_bytes())?;

        Ok(())
    }

    /// Write a command to the server expecting a list of positive status codes.
    pub fn write_command_expecting(
        &mut self,
        cmd: &str,
        valid_statuses: Vec<StatusCodeKind>,
    ) -> Result<ServerResponse, crate::error::Error> {
        self.write_command(cmd)?;
        self.parse_reply_expecting(valid_statuses)
    }

    /// Write a command to the server.
    pub fn write_command(&mut self, cmd: &str) -> Result<(), crate::error::Error> {
        let text = format!("{}\r\n", cmd);
        self.stream.get_mut().write_all(text.as_bytes())?;

        Ok(())
    }

    /// Parse the server reply into a ServerResponse expecting a list of status codes.
    pub fn parse_reply_expecting(
        &mut self,
        valid_statuses: Vec<StatusCodeKind>,
    ) -> Result<ServerResponse, crate::error::Error> {
        let response = self.parse_reply()?;

        let is_expected_status = valid_statuses.contains(&response.status_code.kind);
        // We are a bit liberal on what we accept.
        let is_positive_status = response.status_code.is_valid();
        warn!(
            "Unexpected positive status was accepted: {:?}",
            response.status_code
        );

        if is_expected_status || is_positive_status {
            Ok(response)
        } else {
            Err(crate::error::Error::UnexpectedStatusCode(
                response.summarize_error(valid_statuses),
            ))
        }
    }

    /// Parse the server reply into a ServerResponse.
    pub fn parse_reply(&mut self) -> Result<ServerResponse, crate::error::Error> {
        self.buffer.clear();
        self.stream.read_line(&mut self.buffer)?;
        Ok(ServerResponse::parse(&self.buffer))
    }

    /// Read the server reply as a raw string.
    pub fn read_reply(&mut self) -> Result<String, crate::error::Error> {
        self.buffer.clear();
        self.stream.read_line(&mut self.buffer)?;
        Ok(self.buffer.clone())
    }

    fn decode_passive_mode_ip(
        &self,
        message: &str,
    ) -> Result<std::net::SocketAddrV4, crate::error::Error> {
        let first_bracket = message.find('(');
        let second_bracket = message.find(')');
        let cant_parse_error = || {
            crate::error::Error::InvalidSocketPassiveMode(format!(
                "Cannot parse socket sent from server for passive mode: {}.",
                message
            ))
        };

        match (first_bracket, second_bracket) {
            (Some(start), Some(end)) => {
                // We are dealing with ASCII strings only on this point, so +1 is okay.
                let nums: Vec<u8> = message[start + 1..end]
                    .split(',')
                    // Try to parse all digits between ','
                    .flat_map(|val| val.parse())
                    .collect();
                if nums.len() < 4 {
                    Err(cant_parse_error())
                } else {
                    let ip = std::net::Ipv4Addr::new(nums[0], nums[1], nums[2], nums[3]);

                    Ok(std::net::SocketAddrV4::new(
                        ip,
                        256 * nums[4] as u16 + nums[5] as u16,
                    ))
                }
            }
            _ => Err(cant_parse_error()),
        }
    }

    fn decode_extended_passive_mode_socket(
        &self,
        response: &str,
    ) -> Result<std::net::SocketAddr, crate::error::Error> {
        let first_delimiter = response.find("|||");
        let second_delimiter = response.rfind('|');
        let cant_parse_error = || {
            crate::error::Error::InvalidSocketPassiveMode(format!(
                "Cannot parse socket sent from server for passive mode: {}.",
                response
            ))
        };

        match (first_delimiter, second_delimiter) {
            (Some(start), Some(end)) => {
                let port: u16 = response[start + 3..end]
                    .parse()
                    .map_err(move |_| cant_parse_error())?;
                let ip = self
                    .stream
                    .get_ref()
                    .peer_addr()
                    .map_err(move |_| cant_parse_error())?
                    .ip();
                Ok(std::net::SocketAddr::new(ip, port))
            }
            _ => Err(cant_parse_error()),
        }
    }
}