Browse Source

Prefer IPV4 addresses when choice is available

tags/0.8.7^0
Julien Blanchard 1 year ago
parent
commit
64ae298982
6 changed files with 88 additions and 71 deletions
  1. 1
    1
      Cargo.lock
  2. 1
    1
      Cargo.toml
  3. 32
    24
      src/finger/client.rs
  4. 10
    2
      src/gemini/client.rs
  5. 43
    36
      src/gopher/client.rs
  6. 1
    7
      src/gopher/parser.rs

+ 1
- 1
Cargo.lock View File

@@ -112,7 +112,7 @@ dependencies = [
112 112
 
113 113
 [[package]]
114 114
 name = "castor"
115
-version = "0.8.6"
115
+version = "0.8.7"
116 116
 dependencies = [
117 117
  "ansi-parser",
118 118
  "dirs",

+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
1 1
 [package]
2 2
 name = "castor"
3
-version = "0.8.6"
3
+version = "0.8.7"
4 4
 authors = ["Julien Blanchard <julien@typed-hole.org>"]
5 5
 edition = "2018"
6 6
 

+ 32
- 24
src/finger/client.rs View File

@@ -1,6 +1,5 @@
1 1
 use std::io::{Read, Write};
2
-use std::net::TcpStream;
3
-use std::net::ToSocketAddrs;
2
+use std::net::{SocketAddr::V4, SocketAddr::V6, TcpStream, ToSocketAddrs};
4 3
 use std::thread;
5 4
 use std::time::Duration;
6 5
 
@@ -10,31 +9,40 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin
10 9
     let url = url.get_source_url();
11 10
     let host = url.host_str().unwrap().to_string();
12 11
     let urlf = format!("{}:79", host);
13
-    let socket = match urlf.to_socket_addrs() {
14
-        Ok(iter) => iter.rev().next(),
15
-        Err(_) => None,
16
-    };
17 12
 
18
-    match socket {
19
-        Some(socket) => match TcpStream::connect_timeout(&socket, Duration::new(5, 0)) {
20
-            Ok(mut stream) => thread::spawn(move || {
21
-                let username = if url.username() == "" {
22
-                    url.path().replace("/", "")
23
-                } else {
24
-                    String::from(url.username())
13
+    match urlf.to_socket_addrs() {
14
+        Ok(mut addrs_iter) => match addrs_iter.next() {
15
+            Some(socket_addr) => {
16
+                let socket_addr = match socket_addr {
17
+                    V4(ip) => V4(ip),
18
+                    V6(ip) => match addrs_iter.next() {
19
+                        Some(addr) => addr,
20
+                        None => V6(ip),
21
+                    },
25 22
                 };
26 23
 
27
-                let request = format!("{}\r\n", username);
28
-                stream.write_all(request.as_bytes()).unwrap();
29
-                let mut res = vec![];
30
-                stream.read_to_end(&mut res).unwrap();
24
+                match TcpStream::connect_timeout(&socket_addr, Duration::new(5, 0)) {
25
+                    Ok(mut stream) => thread::spawn(move || {
26
+                        let username = if url.username() == "" {
27
+                            url.path().replace("/", "")
28
+                        } else {
29
+                            String::from(url.username())
30
+                        };
31 31
 
32
-                Ok((None, res))
33
-            })
34
-            .join()
35
-            .unwrap(),
36
-            Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
37
-        },
38
-        None => Err(format!("Could not connect to {}\n", urlf)),
32
+                        let request = format!("{}\r\n", username);
33
+                        stream.write_all(request.as_bytes()).unwrap();
34
+                        let mut res = vec![];
35
+                        stream.read_to_end(&mut res).unwrap();
36
+
37
+                        Ok((None, res))
38
+                    })
39
+                        .join()
40
+                        .unwrap(),
41
+                    Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
42
+                }
43
+            }
44
+            None => Err(format!("Could not connect to {}\n", urlf)),
45
+        }
46
+        Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
39 47
     }
40 48
 }

+ 10
- 2
src/gemini/client.rs View File

@@ -1,6 +1,6 @@
1 1
 use native_tls::TlsConnector;
2 2
 use std::io::{Read, Write};
3
-use std::net::{TcpStream, ToSocketAddrs};
3
+use std::net::{SocketAddr::V4, SocketAddr::V6, TcpStream, ToSocketAddrs};
4 4
 use std::thread;
5 5
 use std::time::Duration;
6 6
 
@@ -24,8 +24,16 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin
24 24
     let connector = builder.build().unwrap();
25 25
 
26 26
     match urlf.to_socket_addrs() {
27
-        Ok(addrs_iter) => match addrs_iter.rev().next() {
27
+        Ok(mut addrs_iter) => match addrs_iter.next() {
28 28
             Some(socket_addr) => {
29
+                let socket_addr = match socket_addr {
30
+                    V4(ip) => V4(ip),
31
+                    V6(ip) => match addrs_iter.next() {
32
+                        Some(addr) => addr,
33
+                        None => V6(ip)
34
+                    }
35
+                };
36
+
29 37
                 let stream = TcpStream::connect_timeout(&socket_addr, Duration::new(5, 0));
30 38
 
31 39
                 match stream {

+ 43
- 36
src/gopher/client.rs View File

@@ -1,7 +1,6 @@
1 1
 use percent_encoding::percent_decode;
2 2
 use std::io::{Read, Write};
3
-use std::net::TcpStream;
4
-use std::net::ToSocketAddrs;
3
+use std::net::{SocketAddr::V4, SocketAddr::V6, TcpStream, ToSocketAddrs};
5 4
 use std::thread;
6 5
 use std::time::Duration;
7 6
 
@@ -16,46 +15,54 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin
16 15
     };
17 16
     let urlf = format!("{}:{}", host, port);
18 17
 
19
-    let socket = match urlf.to_socket_addrs() {
20
-        Ok(iter) => iter.rev().next(),
21
-        Err(_) => None,
22
-    };
18
+    match urlf.to_socket_addrs() {
19
+        Ok(mut addrs_iter) => match addrs_iter.next() {
20
+            Some(socket_addr) => {
21
+                let socket_addr = match socket_addr {
22
+                    V4(ip) => V4(ip),
23
+                    V6(ip) => match addrs_iter.next() {
24
+                        Some(addr) => addr,
25
+                        None => V6(ip),
26
+                    },
27
+                };
23 28
 
24
-    match socket {
25
-        Some(socket) => match TcpStream::connect_timeout(&socket, Duration::new(5, 0)) {
26
-            Ok(mut stream) => thread::spawn(move || {
27
-                let path = url.path().to_string();
29
+                match TcpStream::connect_timeout(&socket_addr, Duration::new(5, 0)) {
30
+                    Ok(mut stream) => thread::spawn(move || {
31
+                        let path = url.path().to_string();
28 32
 
29
-                let mut url = match url.query() {
30
-                    Some(query) => format!("{}?{}\r\n", path, query),
31
-                    None => format!("{}\r\n", path),
32
-                };
33
+                        let mut url = match url.query() {
34
+                            Some(query) => format!("{}?{}\r\n", path, query),
35
+                            None => format!("{}\r\n", path),
36
+                        };
33 37
 
34
-                let url = if url.starts_with("/0")
35
-                    || url.starts_with("/1")
36
-                    || url.starts_with("/g")
37
-                    || url.starts_with("/I")
38
-                    || url.starts_with("/9")
39
-                {
40
-                    url.split_off(2)
41
-                } else if url == "/\n" {
42
-                    String::from("\r\n")
43
-                } else {
44
-                    url
45
-                };
38
+                        let url = if url.starts_with("/0")
39
+                            || url.starts_with("/1")
40
+                            || url.starts_with("/g")
41
+                            || url.starts_with("/I")
42
+                            || url.starts_with("/9")
43
+                        {
44
+                            url.split_off(2)
45
+                        } else if url == "/\n" {
46
+                            String::from("\r\n")
47
+                        } else {
48
+                            url
49
+                        };
46 50
 
47
-                let url = percent_decode(url.as_bytes()).decode_utf8().unwrap();
51
+                        let url = percent_decode(url.as_bytes()).decode_utf8().unwrap();
48 52
 
49
-                stream.write_all(url.as_bytes()).unwrap();
50
-                let mut res = vec![];
51
-                stream.read_to_end(&mut res).unwrap();
53
+                        stream.write_all(url.as_bytes()).unwrap();
54
+                        let mut res = vec![];
55
+                        stream.read_to_end(&mut res).unwrap();
52 56
 
53
-                Ok((None, res))
54
-            })
55
-            .join()
56
-            .unwrap(),
57
-            Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
57
+                        Ok((None, res))
58
+                    })
59
+                    .join()
60
+                    .unwrap(),
61
+                    Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
62
+                }
63
+            }
64
+            None => Err(format!("Could not connect to {}\n", urlf)),
58 65
         },
59
-        None => Err(format!("Could not connect to {}\n", urlf)),
66
+        Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
60 67
     }
61 68
 }

+ 1
- 7
src/gopher/parser.rs View File

@@ -42,13 +42,7 @@ impl FromStr for TextElement {
42 42
             // Text line
43 43
             if line.contains("gopher://") {
44 44
                 Ok(TextElement::LinkItem(String::from(line)))
45
-            } else if line.contains("http://") || line.contains("https://") {
46
-                Ok(TextElement::ExternalLinkItem(String::from(line)))
47
-            } else if line.contains("gemini://") {
48
-                Ok(TextElement::ExternalLinkItem(String::from(line)))
49
-            } else if line.contains("ftp://") {
50
-                Ok(TextElement::ExternalLinkItem(String::from(line)))
51
-            } else if line.contains("finger://") {
45
+            } else if line.contains("http://") || line.contains("https://") || line.contains("gemini://") || line.contains("finger://") || line.contains("ftp://") {
52 46
                 Ok(TextElement::ExternalLinkItem(String::from(line)))
53 47
             } else {
54 48
                 Ok(TextElement::Text(colors::colorize(line)))

Loading…
Cancel
Save