Skip to main content

rinova_proxy_sdk/
types.rs

1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3use serde_yaml::Value;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum ProxyType {
8    #[serde(rename = "ss")]
9    Ss,
10    #[serde(rename = "vmess")]
11    Vmess,
12    #[serde(rename = "trojan")]
13    Trojan,
14    #[serde(rename = "hysteria2")]
15    Hysteria2,
16}
17
18impl ProxyType {
19    pub fn as_str(self) -> &'static str {
20        match self {
21            Self::Ss => "ss",
22            Self::Vmess => "vmess",
23            Self::Trojan => "trojan",
24            Self::Hysteria2 => "hysteria2",
25        }
26    }
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct ProxyNode {
31    pub name: String,
32    #[serde(rename = "type")]
33    pub proxy_type: ProxyType,
34    pub server: String,
35    pub port: u16,
36    #[serde(flatten, skip_serializing_if = "IndexMap::is_empty")]
37    pub extra: IndexMap<String, Value>,
38}
39
40impl ProxyNode {
41    pub fn new(name: String, proxy_type: ProxyType, server: String, port: u16) -> Self {
42        Self {
43            name,
44            proxy_type,
45            server,
46            port,
47            extra: IndexMap::new(),
48        }
49    }
50
51    pub fn set_str(&mut self, key: &str, value: impl Into<String>) {
52        self.extra.insert(key.to_string(), Value::String(value.into()));
53    }
54
55    pub fn set_bool(&mut self, key: &str, value: bool) {
56        self.extra.insert(key.to_string(), Value::Bool(value));
57    }
58
59    pub fn set_u64(&mut self, key: &str, value: u64) {
60        self.extra.insert(key.to_string(), Value::Number(value.into()));
61    }
62
63    pub fn set_value(&mut self, key: &str, value: Value) {
64        self.extra.insert(key.to_string(), value);
65    }
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct ProxyGroup {
70    pub name: String,
71    #[serde(rename = "type")]
72    pub group_type: String,
73    pub proxies: Vec<String>,
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub url: Option<String>,
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub interval: Option<u64>,
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct ClashConfig {
82    pub port: u16,
83    #[serde(rename = "socks-port")]
84    pub socks_port: u16,
85    #[serde(rename = "allow-lan")]
86    pub allow_lan: bool,
87    pub mode: String,
88    #[serde(rename = "log-level")]
89    pub log_level: String,
90    #[serde(rename = "external-controller")]
91    pub external_controller: String,
92    pub proxies: Vec<ProxyNode>,
93    #[serde(rename = "proxy-groups")]
94    pub proxy_groups: Vec<ProxyGroup>,
95    pub rules: Vec<String>,
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
99pub enum RuleMode {
100    #[default]
101    Builtin,
102    External,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub struct ConvertOptions {
107    pub rules: RuleMode,
108    pub deduplicate: bool,
109}
110
111impl Default for ConvertOptions {
112    fn default() -> Self {
113        Self {
114            rules: RuleMode::Builtin,
115            deduplicate: true,
116        }
117    }
118}
119
120#[derive(Debug, Clone)]
121pub struct ConvertResult {
122    pub config: ClashConfig,
123    pub yaml: String,
124    pub nodes: Vec<ProxyNode>,
125}