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
use regex::Regex;
use url::Url;
/// Represents an MTProxy, a specific type of proxy.
#[derive(Debug)]
pub struct MTProxy {
/// The host address of the MTProxy.
pub host: String,
/// The port number for the MTProxy.
pub port: u32,
/// The secret associated with the MTProxy.
pub secret: String,
}
impl MTProxy {
/// Converts the MTProxy information into a Telegram proxy URL.
///
/// # Example
///
/// ```
/// use proxy_scraper::mtproxy::MTProxy;
/// let proxy = MTProxy {
/// host: "example.com".to_string(),
/// port: 443,
/// secret: "abcdef0123456789".to_string(),
/// };
/// let url = proxy.to_url();
/// assert_eq!(url, "https://t.me/proxy?server=example.com&port=443&secret=abcdef0123456789");
/// ```
pub fn to_url(&self) -> String {
format!(
"https://t.me/proxy?server={}&port={}&secret={}",
self.host, self.port, self.secret
)
}
/// Scrapes MTProxy information from the provided source string and returns a vector of MTProxy instances.
///
/// # Arguments
///
/// * `source` - A string containing the source code or text from which to extract MTProxy information.
///
/// # Returns
///
/// A vector of `MTProxy` instances parsed from the source string.
pub fn scrape(source: &str) -> Vec<Self> {
let source = crate::utils::seperate_links(source);
let mut proxy_list: Vec<MTProxy> = Vec::new();
let regex = Regex::new(
r#"(\w+:\/\/.*\/proxy\?((server=.+)|(port=.+)|(secret=([A-Fa-f0-9]+|[A-Za-z0-9+\/]+))))+"#,
)
.unwrap();
for captures in regex.captures_iter(&source) {
let proxy = captures.get(1).map(|m| m.as_str()).unwrap_or("");
if proxy.is_empty() {
continue;
}
let proxy_url = Url::parse(proxy).unwrap();
let server = proxy_url
.query_pairs()
.find(|(key, _)| key == "server")
.map(|(_, value)| value);
let port = proxy_url
.query_pairs()
.find(|(key, _)| key == "port")
.map(|(_, value)| value);
let secret = proxy_url
.query_pairs()
.find(|(key, _)| key == "secret")
.map(|(_, value)| value);
match (server, port, secret) {
(Some(server), Some(port), Some(secret)) => proxy_list.push(MTProxy {
host: server.to_string(),
port: port.parse().unwrap_or(443),
secret: secret.to_string(),
}),
_ => continue,
};
}
proxy_list
}
}