@mmmbuto/masix 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ use anyhow::Result;
2
+ use reqwest::{Client, Proxy};
3
+ use std::time::Duration;
4
+ use tokio::{net::TcpStream, time::timeout};
5
+
6
+ const TOR_DETECT_TIMEOUT_SECS: u64 = 2;
7
+ const TOR_SOCKS_PORTS: &[u16] = &[9050, 9150];
8
+
9
+ #[derive(Debug, Clone)]
10
+ pub struct TransportLayer {
11
+ clearnet: Client,
12
+ tor: Option<Client>,
13
+ tor_available: bool,
14
+ tor_port: Option<u16>,
15
+ }
16
+
17
+ impl TransportLayer {
18
+ pub async fn new(
19
+ user_agent: &str,
20
+ clearnet_timeout_secs: u64,
21
+ tor_timeout_secs: u64,
22
+ ) -> Result<Self> {
23
+ let clearnet = Client::builder()
24
+ .user_agent(user_agent)
25
+ .timeout(Duration::from_secs(clearnet_timeout_secs))
26
+ .build()?;
27
+
28
+ let mut tor_port = std::env::var("MASIX_TOR_SOCKS_PORT")
29
+ .ok()
30
+ .and_then(|value| value.trim().parse::<u16>().ok());
31
+ let candidates = if let Some(port) = tor_port {
32
+ vec![port]
33
+ } else {
34
+ TOR_SOCKS_PORTS.to_vec()
35
+ };
36
+
37
+ for port in candidates {
38
+ if detect_tor_port(port).await {
39
+ tor_port = Some(port);
40
+ break;
41
+ }
42
+ }
43
+
44
+ let tor = if let Some(port) = tor_port {
45
+ Some(
46
+ Client::builder()
47
+ .user_agent(user_agent)
48
+ .timeout(Duration::from_secs(tor_timeout_secs))
49
+ .proxy(Proxy::all(format!("socks5h://127.0.0.1:{port}"))?)
50
+ .build()?,
51
+ )
52
+ } else {
53
+ None
54
+ };
55
+
56
+ Ok(Self {
57
+ clearnet,
58
+ tor_available: tor.is_some(),
59
+ tor_port,
60
+ tor,
61
+ })
62
+ }
63
+
64
+ pub fn client_for(&self, url: &str) -> (&Client, &'static str) {
65
+ if url.contains(".onion") {
66
+ if let Some(tor) = self.tor.as_ref() {
67
+ return (tor, "tor");
68
+ }
69
+ }
70
+ (&self.clearnet, "clearnet")
71
+ }
72
+
73
+ pub fn client_for_preference(&self, prefer_tor: bool) -> (&Client, &'static str) {
74
+ if prefer_tor {
75
+ if let Some(tor) = self.tor.as_ref() {
76
+ return (tor, "tor");
77
+ }
78
+ }
79
+ (&self.clearnet, "clearnet")
80
+ }
81
+
82
+ pub fn client_for_ahmia(&self, prefer_tor: bool) -> (&Client, &'static str, &'static str) {
83
+ if prefer_tor {
84
+ if let Some(tor) = self.tor.as_ref() {
85
+ return (
86
+ tor,
87
+ "tor",
88
+ "http://juhanurmihxlp77nkq76byazcldy2hlmovfu2epvl5ankdibsot4csyd.onion/search/",
89
+ );
90
+ }
91
+ }
92
+ (&self.clearnet, "clearnet", "https://ahmia.fi/search/")
93
+ }
94
+
95
+ pub fn tor_available(&self) -> bool {
96
+ self.tor_available
97
+ }
98
+
99
+ pub fn tor_port(&self) -> Option<u16> {
100
+ self.tor_port
101
+ }
102
+ }
103
+
104
+ async fn detect_tor_port(port: u16) -> bool {
105
+ timeout(
106
+ Duration::from_secs(TOR_DETECT_TIMEOUT_SECS),
107
+ TcpStream::connect(("127.0.0.1", port)),
108
+ )
109
+ .await
110
+ .map(|result| result.is_ok())
111
+ .unwrap_or(false)
112
+ }
package/prebuilt/masix CHANGED
Binary file