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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Trojan {
pub host: String,
pub port: u16,
pub password: String,
pub server_name: String,
pub alpn: Vec<String>,
pub fingerprint: Option<String>,
}
impl Trojan {
pub fn identity(&self) -> (&str, u16, &str) {
(&self.host, self.port, &self.password)
}
}
#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum EntryStatus {
Supported,
Unsupported,
Invalid,
}
impl EntryStatus {
pub fn as_str(self) -> &'static str {
match self {
Self::Supported => "supported",
Self::Unsupported => "unsupported",
Self::Invalid => "invalid",
}
}
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Entry {
pub id: String,
pub name: String,
pub status: EntryStatus,
pub explanation: Option<String>,
pub connection: Option<Trojan>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfigurationSummary {
pub id: String,
pub subscription_id: String,
pub name: String,
pub host: Option<String>,
pub port: Option<u16>,
pub status: String,
pub explanation: Option<String>,
pub selected: bool,
pub ping_ms: Option<u64>,
pub speed_bytes_per_second: Option<f64>,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Subscription {
pub id: String,
pub name: String,
pub url: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SubscriptionSummary {
pub id: String,
pub name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Settings {
pub ping_url: String,
pub speed_url: String,
pub maximum_download_mib: u64,
pub maximum_duration_seconds: u64,
}
impl Default for Settings {
fn default() -> Self {
Self {
ping_url: "https://speed.cloudflare.com/__down?bytes=0".into(),
speed_url: "https://speed.cloudflare.com/__down?bytes=10485760".into(),
maximum_download_mib: 10,
maximum_duration_seconds: 10,
}
}
}
impl Settings {
pub fn validate(&self) -> anyhow::Result<()> {
for value in [&self.ping_url, &self.speed_url] {
let url = url::Url::parse(value)
.map_err(|_| anyhow::anyhow!("Use an absolute HTTPS diagnostic URL"))?;
anyhow::ensure!(
url.scheme() == "https"
&& url.host_str().is_some()
&& url.username().is_empty()
&& url.password().is_none()
&& url.fragment().is_none(),
"Diagnostic URLs require HTTPS without credentials or fragments"
);
}
anyhow::ensure!(
self.maximum_download_mib > 0
&& self.maximum_download_mib.checked_mul(1_048_576).is_some(),
"Download limit must be a positive representable MiB count"
);
anyhow::ensure!(
self.maximum_duration_seconds > 0,
"Duration limit must be positive"
);
anyhow::ensure!(
std::time::Instant::now()
.checked_add(std::time::Duration::from_secs(
self.maximum_duration_seconds
))
.is_some(),
"Duration exceeds the supported clock range"
);
Ok(())
}
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Definitions {
pub version: u32,
#[serde(default)]
pub settings: Settings,
#[serde(default)]
pub subscriptions: Vec<Subscription>,
}
impl Default for Definitions {
fn default() -> Self {
Self {
version: 1,
settings: Settings::default(),
subscriptions: vec![],
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Status {
pub version: u32,
pub requested_enabled: bool,
pub selected_configuration: Option<String>,
pub generation: u64,
pub routing: String,
pub health: String,
pub last_error: Option<String>,
}
pub fn display_text(value: &str) -> String {
value
.chars()
.filter(|c| !c.is_control())
.take(256)
.collect()
}
|