Skip to main content

tunneler_core/connections/
destination.rs

1/// Describes a simple external Address consisting of an IP and Port
2#[derive(Clone, Debug)]
3pub struct Destination {
4    ip: String,
5    port: u32,
6    formatted: String,
7}
8
9impl Destination {
10    /// Creates a new Destination from the given data
11    pub fn new(ip: String, port: u32) -> Self {
12        let formatted_ip = format!("{}:{}", ip, port);
13
14        Destination {
15            ip,
16            port,
17            formatted: formatted_ip,
18        }
19    }
20
21    /// Tries to connect to the described Destination and returns
22    /// the TCP-Stream that was established
23    pub async fn connect(&self) -> std::io::Result<tokio::net::TcpStream> {
24        let stream = tokio::net::TcpStream::connect(&self.formatted).await?;
25
26        Ok(stream)
27    }
28
29    /// Returns the full address to connect to
30    pub fn get_full_address(&self) -> &str {
31        &self.formatted
32    }
33
34    /// Returns the Raw-IP of the Destination
35    pub fn get_ip(&self) -> &str {
36        &self.ip
37    }
38    /// Returns the Port of the Destination
39    pub fn get_port(&self) -> u32 {
40        self.port
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn new_dest_ip() {
50        let dest = Destination::new("localhost".to_owned(), 123);
51        assert_eq!("localhost", dest.get_ip());
52    }
53
54    #[test]
55    fn new_dest_port() {
56        let dest = Destination::new("localhost".to_owned(), 123);
57        assert_eq!(123, dest.get_port());
58    }
59
60    #[test]
61    fn new_dest_formatted() {
62        let dest = Destination::new("localhost".to_owned(), 123);
63        assert_eq!("localhost:123", dest.get_full_address());
64    }
65}