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
use crate::models::{
    Proxy, HTTP_DEFAULT_GROUP, SSR_DEFAULT_GROUP, SS_DEFAULT_GROUP, TROJAN_DEFAULT_GROUP,
    V2RAY_DEFAULT_GROUP,
};

/// Parse Quantumult configuration into a vector of Proxy objects
/// Consistent with the C++ implementation in explodeQuan
pub fn explode_quan(content: &str, nodes: &mut Vec<Proxy>) -> bool {
    // Split the content into lines
    let lines: Vec<&str> = content.lines().collect();

    let mut success = false;

    for line in lines {
        // Skip empty lines and comments
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
            continue;
        }

        // Check if this is a proxy line
        let mut node = Proxy::default();
        if parse_quan_line(line, &mut node) {
            nodes.push(node);
            success = true;
        }
    }

    success
}

/// Parse a single line from Quantumult configuration
/// Returns true if parsing was successful
fn parse_quan_line(line: &str, node: &mut Proxy) -> bool {
    // Different formats for Quantumult configuration lines:

    // Format: [name] = [type], [params...]
    let parts: Vec<&str> = line.splitn(2, " = ").collect();
    if parts.len() != 2 {
        return false;
    }

    let name = parts[0].trim();
    let config = parts[1].trim();

    let config_parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();
    if config_parts.is_empty() {
        return false;
    }

    // Determine the proxy type and parse accordingly
    match config_parts[0] {
        "vmess" => parse_quan_vmess(name, config_parts, node),
        "shadowsocks" => parse_quan_ss(name, config_parts, node),
        "shadowsocksr" => parse_quan_ssr(name, config_parts, node),
        "http" => parse_quan_http(name, config_parts, node),
        "trojan" => parse_quan_trojan(name, config_parts, node),
        _ => false,
    }
}

/// Parse a Quantumult Shadowsocks line
fn parse_quan_ss(name: &str, config_parts: Vec<&str>, node: &mut Proxy) -> bool {
    // Format: shadowsocks, [server], [port], [method], [password], [options]
    if config_parts.len() < 5 {
        return false;
    }

    // Extract basic parameters
    let server = config_parts[1];
    let port = match config_parts[2].parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false; // Skip if port is 0
    }
    let method = config_parts[3];
    let password = config_parts[4];

    // Default values
    let mut plugin = "";
    let mut plugin_opts = "";
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional options
    for i in 5..config_parts.len() {
        if config_parts[i].starts_with("obfs=") {
            plugin = "obfs";

            let obfs_parts: Vec<&str> = config_parts[i][5..].split(',').collect();
            if !obfs_parts.is_empty() {
                let mut opts = format!("obfs={}", obfs_parts[0]);

                if obfs_parts.len() > 1 {
                    opts.push_str(&format!(";obfs-host={}", obfs_parts[1]));
                }

                plugin_opts = Box::leak(opts.into_boxed_str());
            }
        } else if config_parts[i] == "udp-relay=true" {
            udp = Some(true);
        } else if config_parts[i] == "fast-open=true" {
            tfo = Some(true);
        } else if config_parts[i] == "tls-verification=false" {
            scv = Some(true);
        }
    }

    *node = Proxy::ss_construct(
        SS_DEFAULT_GROUP,
        name,
        server,
        port,
        password,
        method,
        plugin,
        plugin_opts,
        udp,
        tfo,
        scv,
        None,
        "",
    );

    true
}

/// Parse a Quantumult ShadowsocksR line
fn parse_quan_ssr(name: &str, config_parts: Vec<&str>, node: &mut Proxy) -> bool {
    // Format: shadowsocksr, [server], [port], [method], [password], [protocol], [protocol_param], [obfs], [obfs_param], [options]
    if config_parts.len() < 9 {
        return false;
    }

    // Extract basic parameters
    let server = config_parts[1];
    let port = match config_parts[2].parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false; // Skip if port is 0
    }
    let method = config_parts[3];
    let password = config_parts[4];
    let protocol = config_parts[5];
    let protocol_param = config_parts[6];
    let obfs = config_parts[7];
    let obfs_param = config_parts[8];

    // Default values
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional options
    for i in 9..config_parts.len() {
        if config_parts[i] == "udp-relay=true" {
            udp = Some(true);
        } else if config_parts[i] == "fast-open=true" {
            tfo = Some(true);
        } else if config_parts[i] == "tls-verification=false" {
            scv = Some(true);
        }
    }

    *node = Proxy::ssr_construct(
        SSR_DEFAULT_GROUP,
        name,
        server,
        port,
        protocol,
        method,
        obfs,
        password,
        obfs_param,
        protocol_param,
        udp,
        tfo,
        scv,
        "",
    );

    true
}

/// Parse a Quantumult VMess line
/// This implementation follows the C++ version closely
fn parse_quan_vmess(name: &str, config_parts: Vec<&str>, node: &mut Proxy) -> bool {
    // Format: vmess, [server], [port], [method], [uuid], [options]
    if config_parts.len() < 6 {
        return false;
    }

    // Extract basic parameters
    let server = config_parts[1];
    let port = match config_parts[2].parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false; // Skip if port is 0
    }
    let cipher = config_parts[3];
    let uuid = config_parts[4].replace("\"", ""); // Remove quotes as in C++ replaceAllDistinct

    // Default values
    let mut group = V2RAY_DEFAULT_GROUP.to_string();
    let aid = "0".to_string();
    let mut net = "tcp".to_string();
    let mut path = String::new();
    let mut host = String::new();
    let mut edge = String::new();
    let mut tls = String::new();
    let fake_type = "none".to_string();

    // Parse additional options exactly like the C++ version
    for i in 5..config_parts.len() {
        let option_parts: Vec<&str> = config_parts[i].splitn(2, "=").collect();
        if option_parts.len() < 2 {
            continue;
        }

        let item_name = option_parts[0].trim();
        let item_val = option_parts[1].trim();

        match item_name {
            "group" => group = item_val.to_string(),
            "over-tls" => {
                tls = if item_val == "true" {
                    "tls".to_string()
                } else {
                    String::new()
                }
            }
            "tls-host" => host = item_val.to_string(),
            "obfs-path" => path = item_val.replace("\"", ""), // Remove quotes as in C++ replaceAllDistinct
            "obfs-header" => {
                // Parse headers similar to the C++ implementation
                let processed_val = item_val
                    .replace("\"", "")
                    .replace("\r\n", "|")
                    .replace("\n", "|");
                let headers: Vec<&str> = processed_val.split('|').collect();

                for header in headers {
                    if header.to_lowercase().starts_with("host: ") {
                        host = header[6..].to_string();
                    } else if header.to_lowercase().starts_with("edge: ") {
                        edge = header[6..].to_string();
                    }
                }
            }
            "obfs" => {
                if item_val == "ws" {
                    net = "ws".to_string();
                }
            }
            _ => {}
        }
    }

    // Set default path if empty
    if path.is_empty() {
        path = "/".to_string();
    }

    *node = Proxy::vmess_construct(
        &group,
        name,
        server,
        port,
        &fake_type,
        &uuid,
        aid.parse::<u16>().unwrap_or(0),
        &net,
        cipher,
        &path,
        &host,
        &edge,
        &tls,
        "", // SNI not set in C++ version
        None,
        None,
        None,
        None,
        "",
    );

    true
}

/// Parse a Quantumult HTTP/HTTPS line
fn parse_quan_http(name: &str, config_parts: Vec<&str>, node: &mut Proxy) -> bool {
    // Format: http, [server], [port], [username], [password], [options]
    if config_parts.len() < 3 {
        return false;
    }

    // Extract basic parameters
    let server = config_parts[1];
    let port = match config_parts[2].parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false; // Skip if port is 0
    }

    // Default values
    let mut username = "";
    let mut password = "";
    let mut is_https = false;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional parameters
    if config_parts.len() > 3 {
        username = config_parts[3];
    }

    if config_parts.len() > 4 {
        password = config_parts[4];
    }

    // Parse additional options
    for i in 5..config_parts.len() {
        if config_parts[i] == "over-tls=true" {
            is_https = true;
        } else if config_parts[i] == "fast-open=true" {
            tfo = Some(true);
        } else if config_parts[i] == "tls-verification=false" {
            scv = Some(true);
        }
    }

    *node = Proxy::http_construct(
        HTTP_DEFAULT_GROUP,
        name,
        server,
        port,
        username,
        password,
        is_https,
        tfo,
        scv,
        None,
        "",
    );

    true
}

/// Parse a Quantumult Trojan line
fn parse_quan_trojan(name: &str, config_parts: Vec<&str>, node: &mut Proxy) -> bool {
    // Format: trojan, [server], [port], [password], [options]
    if config_parts.len() < 4 {
        return false;
    }

    // Extract basic parameters
    let server = config_parts[1];
    let port = match config_parts[2].parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false; // Skip if port is 0
    }
    let password = config_parts[3];

    // Default values
    let mut sni = None;
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional options
    for i in 4..config_parts.len() {
        if config_parts[i].starts_with("tls-host=") {
            sni = Some(config_parts[i][9..].to_string());
        } else if config_parts[i] == "udp-relay=true" {
            udp = Some(true);
        } else if config_parts[i] == "fast-open=true" {
            tfo = Some(true);
        } else if config_parts[i] == "tls-verification=false" {
            scv = Some(true);
        }
    }

    *node = Proxy::trojan_construct(
        TROJAN_DEFAULT_GROUP.to_string(),
        name.to_string(),
        server.to_string(),
        port,
        password.to_string(),
        None,
        sni.clone(),
        None,
        sni,
        true,
        udp,
        tfo,
        scv,
        None,
        None,
    );

    true
}