Skip to main content

reala_subscription/
model.rs

1//! Data modules for subscription parsing
2
3use std::sync::Arc;
4
5use crate::Error;
6
7mod clash;
8mod url;
9
10#[derive(Debug, Clone)]
11/// A proxy configuration.
12pub enum Proxy {
13    /// Shadowsocks 2017 or 2022.
14    Shadowsocks {
15        /// The server address.
16        server: Arc<str>,
17
18        /// The server port.
19        port: u16,
20
21        /// The password
22        password: Arc<str>,
23
24        /// The cipher suite.
25        cipher: Arc<str>,
26    },
27}
28
29impl Proxy {
30    #[inline]
31    /// Parses a Clash-compatible config.
32    ///
33    /// If no compatible proxies are found, returns `Ok(None)`.
34    pub fn parse_clash_compatible(config: &str) -> Result<Vec<Self>, Error> {
35        let parsed = self::clash::Config::parse(config)?;
36
37        let parsed: Vec<_> = parsed
38            .proxies
39            .into_iter()
40            .filter_map(|p| match p {
41                self::clash::Proxy::Shadowsocks {
42                    server,
43                    port,
44                    password,
45                    cipher,
46                } => Some(Self::Shadowsocks {
47                    server,
48                    port,
49                    password,
50                    cipher,
51                }),
52                _ => None,
53            })
54            .collect();
55
56        Ok(parsed)
57    }
58}