Skip to main content

proxy_scraper/
hysteria.rs

1use regex::Regex;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Represents a Hysteria proxy.
6#[derive(Debug, Serialize, Deserialize)]
7pub struct Hysteria {
8    /// The version of the Hysteria protocol.
9    pub version: u8,
10    /// The host address of the Hysteria proxy.
11    pub host: String,
12    /// The port number for the Hysteria proxy.
13    pub port: u32,
14    /// The authentication string associated with the Hysteria proxy.
15    pub auth: String,
16    /// Additional parameters associated with the Hysteria proxy.
17    #[serde(flatten)]
18    pub parameters: Option<HashMap<String, String>>,
19}
20
21impl Hysteria {
22    /// Converts the Hysteria proxy information into a Hysteria URL.
23    ///
24    /// # Example
25    ///
26    /// ```
27    /// use proxy_scraper::hysteria::Hysteria;
28    /// use std::collections::HashMap;
29    /// let proxy = Hysteria {
30    ///     version: 1,
31    ///     host: "example.com".to_string(),
32    ///     port: 443,
33    ///     auth: "auth123".to_string(),
34    ///     parameters: Some(HashMap::new()), // Insert additional parameters here
35    /// };
36    /// let url = proxy.to_url();
37    /// assert_eq!(url, "hysteria://auth123@example.com:443?");
38    /// ```
39    pub fn to_url(&self) -> String {
40        let url_encoded_parameters = serde_urlencoded::to_string(&self.parameters).unwrap();
41        let hysteria_version = match self.version {
42            1 => "hysteria",
43            _ => "hy2",
44        };
45
46        format!(
47            "{}://{}@{}:{}?{}",
48            hysteria_version, self.auth, self.host, self.port, url_encoded_parameters
49        )
50    }
51
52    /// Scrapes Hysteria proxy information from the provided source string and returns a vector of Hysteria instances.
53    ///
54    /// # Arguments
55    ///
56    /// * `source` - A string containing the source code or text from which to extract Hysteria proxy information.
57    ///
58    /// # Returns
59    ///
60    /// A vector of `Hysteria` instances parsed from the source string.
61    pub fn scrape(source: &str) -> Vec<Self> {
62        let source = crate::utils::seperate_links(source);
63        let mut proxy_list: Vec<Hysteria> = Vec::new();
64        let regex =
65            Regex::new(r#"(hy2|hysteria2|hysteria)://([A-Za-z0-9\-._~]+)@((.+):(\d+))\?(.+)#"#)
66                .unwrap();
67
68        for captures in regex.captures_iter(&source) {
69            let version = captures.get(1).map(|ver| ver.as_str()).unwrap_or("");
70            let auth = captures.get(2).map(|auth| auth.as_str()).unwrap_or("");
71            let host = captures.get(4).map(|host| host.as_str()).unwrap_or("");
72            let port = captures.get(5).map(|port| port.as_str()).unwrap_or("");
73            let url_parameters = captures.get(6).map(|params| params.as_str()).unwrap_or("");
74
75            if version.is_empty()
76                || auth.is_empty()
77                || host.is_empty()
78                || port.is_empty()
79                || url_parameters.is_empty()
80            {
81                continue;
82            }
83
84            let parameters: HashMap<String, String> =
85                serde_urlencoded::from_str(&url_parameters).unwrap();
86
87            let hysteria_version = match version {
88                "hy2" | "hysteria2" => 2,
89                _ => 1,
90            };
91
92            let hysteria_proxy = Hysteria {
93                version: hysteria_version,
94                host: host.to_string(),
95                port: port.parse::<u32>().unwrap_or(0),
96                auth: auth.to_string(),
97                parameters: Some(parameters),
98            };
99
100            proxy_list.push(hysteria_proxy);
101        }
102
103        proxy_list
104    }
105}