subconverter 0.2.34

A more powerful utility to convert between proxy subscription format
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::{cmp::Ordering, str::FromStr};

use crate::{utils::file_get_async, Settings};

use super::{Proxy, ProxyType, RegexMatchConfig, RegexMatchConfigs};

/// Settings for subscription export operations
pub struct ExtraSettings {
    /// Whether to enable the rule generator
    pub enable_rule_generator: bool,
    /// Whether to overwrite original rules
    pub overwrite_original_rules: bool,
    /// Rename operations to apply
    pub rename_array: RegexMatchConfigs,
    /// Emoji operations to apply
    pub emoji_array: RegexMatchConfigs,
    /// Whether to add emoji
    pub add_emoji: bool,
    /// Whether to remove emoji
    pub remove_emoji: bool,
    /// Whether to append proxy type
    pub append_proxy_type: bool,
    /// Whether to output as node list
    pub nodelist: bool,
    /// Whether to sort nodes
    pub sort_flag: bool,
    /// Whether to filter deprecated nodes
    pub filter_deprecated: bool,
    /// Whether to use new field names in Clash
    pub clash_new_field_name: bool,
    /// Whether to use scripts in Clash
    pub clash_script: bool,
    /// Path to Surge SSR binary
    pub surge_ssr_path: String,
    /// Prefix for managed configs
    pub managed_config_prefix: String,
    /// QuantumultX device ID
    pub quanx_dev_id: String,
    /// UDP support flag
    pub udp: Option<bool>,
    /// TCP Fast Open support flag
    pub tfo: Option<bool>,
    /// Skip certificate verification flag
    pub skip_cert_verify: Option<bool>,
    /// TLS 1.3 support flag
    pub tls13: Option<bool>,
    /// Whether to use classical ruleset in Clash
    pub clash_classical_ruleset: bool,
    /// Script for sorting nodes
    pub sort_script: String,
    /// Style for Clash proxies output
    pub clash_proxies_style: String,
    /// Style for Clash proxy groups output
    pub clash_proxy_groups_style: String,
    /// Whether the export is authorized
    pub authorized: bool,
    /// JavaScript runtime context (not implemented in Rust version)
    #[cfg(feature = "js-runtime")]
    pub js_context: Option<rquickjs::Context>,
    /// JavaScript runtime
    #[cfg(feature = "js-runtime")]
    pub js_runtime: Option<rquickjs::Runtime>,
}

impl std::fmt::Debug for ExtraSettings {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("ExtraSettings")
            .field("enable_rule_generator", &self.enable_rule_generator)
            .field("overwrite_original_rules", &self.overwrite_original_rules)
            .field("rename_array", &self.rename_array)
            .field("emoji_array", &self.emoji_array)
            .field("add_emoji", &self.add_emoji)
            .field("remove_emoji", &self.remove_emoji)
            .field("append_proxy_type", &self.append_proxy_type)
            .field("nodelist", &self.nodelist)
            .field("sort_flag", &self.sort_flag)
            .field("filter_deprecated", &self.filter_deprecated)
            .field("clash_new_field_name", &self.clash_new_field_name)
            .field("clash_script", &self.clash_script)
            .field("surge_ssr_path", &self.surge_ssr_path)
            .field("managed_config_prefix", &self.managed_config_prefix)
            .field("quanx_dev_id", &self.quanx_dev_id)
            .field("udp", &self.udp)
            .field("tfo", &self.tfo)
            .field("skip_cert_verify", &self.skip_cert_verify)
            .field("tls13", &self.tls13)
            .field("clash_classical_ruleset", &self.clash_classical_ruleset)
            .field("sort_script", &self.sort_script)
            .field("clash_proxies_style", &self.clash_proxies_style)
            .field("clash_proxy_groups_style", &self.clash_proxy_groups_style)
            .field("authorized", &self.authorized)
            .finish()
    }
}

impl Default for ExtraSettings {
    fn default() -> Self {
        let global = Settings::current();

        ExtraSettings {
            enable_rule_generator: global.enable_rule_gen,
            overwrite_original_rules: global.overwrite_original_rules,
            rename_array: Vec::new(),
            emoji_array: Vec::new(),
            add_emoji: false,
            remove_emoji: false,
            append_proxy_type: false,
            nodelist: false,
            sort_flag: false,
            filter_deprecated: false,
            clash_new_field_name: true,
            clash_script: false,
            surge_ssr_path: global.surge_ssr_path.clone(),
            managed_config_prefix: String::new(),
            quanx_dev_id: String::new(),
            udp: None,
            tfo: None,
            skip_cert_verify: None,
            tls13: None,
            clash_classical_ruleset: false,
            sort_script: String::new(),
            clash_proxies_style: if global.clash_proxies_style.is_empty() {
                "flow".to_string()
            } else {
                global.clash_proxies_style.clone()
            },
            clash_proxy_groups_style: if global.clash_proxy_groups_style.is_empty() {
                "flow".to_string()
            } else {
                global.clash_proxy_groups_style.clone()
            },
            authorized: false,
            #[cfg(feature = "js-runtime")]
            js_context: None,
            #[cfg(feature = "js-runtime")]
            js_runtime: None,
        }
    }
}

#[cfg(feature = "js-runtime")]
impl ExtraSettings {
    pub fn init_js_context(&mut self) {
        if self.js_runtime.is_none() {
            self.js_runtime = Some(rquickjs::Runtime::new().unwrap());
            self.js_context =
                Some(rquickjs::Context::base(&self.js_runtime.as_ref().unwrap()).unwrap());
        }
    }

    pub fn eval_filter_function(
        &mut self,
        nodes: &mut Vec<Proxy>,
        source_str: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self.init_js_context();
        if let Some(context) = &mut self.js_context {
            let mut error_thrown = None;
            context.with(|ctx| {
                match ctx.eval::<(), &str>(source_str) {
                    Ok(_) => (),
                    Err(e) => {
                        match e {
                            rquickjs::Error::Exception => {
                                log::error!(
                                    "JavaScript eval throw exception: {}",
                                    ctx.catch()
                                        .try_into_string()
                                        .unwrap()
                                        .to_string()
                                        .unwrap_or_default()
                                );
                            }
                            _ => {
                                log::error!("JavaScript eval error: {}", e);
                            }
                        }
                        error_thrown = Some(e);
                        return;
                    }
                };
                let filter_evaluated: rquickjs::Function =
                    match ctx.globals().get::<_, rquickjs::Function>("filter") {
                        Ok(value) => value,
                        Err(e) => {
                            log::error!("JavaScript eval get function error: {}", e);
                            return;
                        }
                    };

                nodes.retain_mut(|node| {
                    match filter_evaluated.call::<(Proxy,), bool>((node.clone(),)) {
                        Ok(value) => value,
                        Err(e) => {
                            log::error!("JavaScript eval call function error: {}", e);
                            false
                        }
                    }
                });
            });
            match error_thrown {
                Some(e) => Err(e.into()),
                None => {
                    log::info!("Filter function evaluated successfully");
                    Ok(())
                }
            }
        } else {
            Err("JavaScript context not initialized".into())
        }
    }

    /// Sorts nodes by a specified criterion
    pub async fn eval_sort_nodes(
        &mut self,
        nodes: &mut Vec<Proxy>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if !self.sort_script.is_empty() {
            let sort_script;
            if self.sort_script.starts_with("path:") {
                sort_script = file_get_async(&self.sort_script[5..], None).await?;
            } else {
                sort_script = self.sort_script.clone();
            }
            self.init_js_context();
            let mut error_thrown = None;
            if let Some(context) = &mut self.js_context {
                context.with(|ctx| {
                    match ctx.eval::<(), &str>(&sort_script) {
                        Ok(_) => (),
                        Err(e) => match e {
                            rquickjs::Error::Exception => {
                                error_thrown = Some(e);
                                return;
                            }
                            _ => {
                                error_thrown = Some(e);
                                return;
                            }
                        },
                    }
                    let compare = match ctx.globals().get::<_, rquickjs::Function>("compare") {
                        Ok(value) => value,
                        Err(e) => {
                            log::error!("JavaScript eval get function error: {}", e);
                            return;
                        }
                    };
                    nodes.sort_by(|a, b| {
                        match compare.call::<(Proxy, Proxy), i32>((a.clone(), b.clone())) {
                            Ok(value) => {
                                if value > 0 {
                                    Ordering::Greater
                                } else if value < 0 {
                                    Ordering::Less
                                } else {
                                    Ordering::Equal
                                }
                            }
                            Err(e) => {
                                log::error!("JavaScript eval call function error: {}", e);
                                return Ordering::Equal;
                            }
                        }
                    });
                });
            }
            if let Some(e) = error_thrown {
                return Err(e.into());
            }
        } else {
            // Default sort by remark
            nodes.sort_by(|a, b| {
                if a.proxy_type == ProxyType::Unknown {
                    return Ordering::Greater;
                }
                if b.proxy_type == ProxyType::Unknown {
                    return Ordering::Less;
                }
                a.remark.cmp(&b.remark)
            });
        }
        Ok(())
    }

    pub async fn eval_get_rename_node_remark(
        &self,
        node: &Proxy,
        match_script: String,
    ) -> Result<String, Box<dyn std::error::Error>> {
        let mut node_name = String::new();
        if !match_script.is_empty() {
            let mut error_thrown = None;
            if let Some(context) = &self.js_context {
                context.with(|ctx| {
                    match ctx.eval::<(), &str>(&match_script) {
                        Ok(_) => (),
                        Err(e) => {
                            error_thrown = Some(e);
                            return;
                        }
                    }
                    let rename = match ctx.globals().get::<_, rquickjs::Function>("rename") {
                        Ok(value) => value,
                        Err(e) => {
                            log::error!("JavaScript eval get function error: {}", e);
                            error_thrown = Some(e);
                            return;
                        }
                    };
                    match rename.call::<(Proxy,), String>((node.clone(),)) {
                        Ok(value) => {
                            if !value.is_empty() {
                                node_name = value;
                            }
                        }
                        Err(e) => {
                            log::error!("JavaScript eval call function error: {}", e);
                            error_thrown = Some(e);
                            return;
                        }
                    }
                })
            }
            if let Some(e) = error_thrown {
                return Err(e.into());
            }
        }
        Ok(node_name)
    }

    pub async fn eval_get_emoji_node_remark(
        &self,
        node: &Proxy,
        match_script: String,
    ) -> Result<String, Box<dyn std::error::Error>> {
        let mut node_emoji = String::new();
        if !match_script.is_empty() {
            let mut error_thrown = None;
            if let Some(context) = &self.js_context {
                context.with(|ctx| {
                    match ctx.eval::<(), &str>(&match_script) {
                        Ok(_) => (),
                        Err(e) => {
                            error_thrown = Some(e);
                            return;
                        }
                    }
                    let get_emoji = match ctx.globals().get::<_, rquickjs::Function>("getEmoji") {
                        Ok(value) => value,
                        Err(e) => {
                            log::error!("JavaScript eval get function error: {}", e);
                            error_thrown = Some(e);
                            return;
                        }
                    };
                    match get_emoji.call::<(Proxy,), String>((node.clone(),)) {
                        Ok(value) => {
                            if !value.is_empty() {
                                node_emoji = value;
                            }
                        }
                        Err(e) => {
                            log::error!("JavaScript eval call function error: {}", e);
                            error_thrown = Some(e);
                            return;
                        }
                    }
                })
            }
            if let Some(e) = error_thrown {
                return Err(e.into());
            }
        }
        Ok(node_emoji)
    }
}

#[cfg(not(feature = "js-runtime"))]
impl ExtraSettings {
    pub fn init_js_context(&mut self) {}
    pub fn eval_filter_function(
        &mut self,
        _nodes: &mut Vec<Proxy>,
        _source_str: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        Err(
            "JavaScript is not supported in this build, please enable js-runtime feature in cargo build"
                .into(),
        )
    }
    pub async fn eval_sort_nodes(
        &mut self,
        _nodes: &mut Vec<Proxy>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        Err(
            "JavaScript is not supported in this build, please enable js-runtime feature in cargo build"
                .into(),
        )
    }
    pub async fn eval_get_rename_node_remark(
        &self,
        _node: &Proxy,
        _match_script: String,
    ) -> Result<String, Box<dyn std::error::Error>> {
        Err(
            "JavaScript is not supported in this build, please enable js-runtime feature in cargo build"
                .into(),
        )
    }
    pub async fn eval_get_emoji_node_remark(
        &self,
        _node: &Proxy,
        _match_script: String,
    ) -> Result<String, Box<dyn std::error::Error>> {
        Err(
            "JavaScript is not supported in this build, please enable js-runtime feature in cargo build"
                .into(),
        )
    }
}