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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
use crate::models::{
    Proxy, HTTP_DEFAULT_GROUP, SNELL_DEFAULT_GROUP, SOCKS_DEFAULT_GROUP, SS_DEFAULT_GROUP,
    TROJAN_DEFAULT_GROUP, V2RAY_DEFAULT_GROUP,
};

/// Parse a Surge configuration into a vector of Proxy objects
pub fn explode_surge(content: &str, nodes: &mut Vec<Proxy>) -> bool {
    // Split the content into lines
    let lines: Vec<&str> = content.lines().collect();

    // Track the section we're currently in
    let mut in_proxy_section = false;
    let mut success = false;

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

        // Check section headers
        if line.starts_with('[') && line.ends_with(']') {
            in_proxy_section = line == "[Proxy]";
            continue;
        }

        // Only process lines in the [Proxy] section
        if !in_proxy_section {
            continue;
        }

        // Split by = to get name and configuration
        let parts: Vec<&str> = line.splitn(2, '=').collect();
        if parts.len() != 2 {
            continue;
        }

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

        // Skip direct, reject, and reject-tinygif
        if config.starts_with("direct")
            || config.starts_with("reject")
            || config.starts_with("reject-tinygif")
        {
            continue;
        }

        // Parse the proxy based on the configuration format
        let mut node = Proxy::default();

        if config.starts_with("custom,") {
            // Surge 2 style custom proxy (essentially a shadowsocks proxy)
            if parse_surge_custom_ss(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        } else if config.starts_with("ss,") || config.starts_with("shadowsocks,") {
            // Surge 3 style ss proxy
            if parse_surge_ss(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        } else if config.starts_with("socks5") || config.starts_with("socks5-tls") {
            if parse_surge_socks(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        } else if config.starts_with("vmess,") {
            // Surge 4 style vmess proxy
            if parse_surge_vmess(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        } else if config.starts_with("http") || config.starts_with("https") {
            if parse_surge_http(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        } else if config.starts_with("trojan") {
            if parse_surge_trojan(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        } else if config.starts_with("snell") {
            if parse_surge_snell(config, name, &mut node) {
                nodes.push(node);
                success = true;
            }
        }
    }

    success
}

/// Parse a Surge 2 custom Shadowsocks configuration line
fn parse_surge_custom_ss(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts (custom,server,port,method,password,module)
    if parts.len() < 5 {
        return false;
    }

    // Extract the server, port, method, and password
    let server = parts[1];
    let port_str = parts[2];
    let port = match port_str.parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false;
    }

    let method = parts[3];
    let password = parts[4];

    // Default values
    let mut plugin = String::new();
    let mut plugin_opts = String::new();
    let mut pluginopts_mode = String::new();
    let mut pluginopts_host = String::new();
    let mut udp = None;
    let mut tfo = None;
    let scv = None;

    // Parse additional parameters
    for i in 6..parts.len() {
        if parts[i].contains('=') {
            let param_parts: Vec<&str> = parts[i].split('=').collect();
            if param_parts.len() != 2 {
                continue;
            }
            let key = param_parts[0].trim();
            let value = param_parts[1].trim();

            match key {
                "obfs" => {
                    plugin = "simple-obfs".to_string();
                    pluginopts_mode = value.to_string();
                }
                "obfs-host" => {
                    pluginopts_host = value.to_string();
                }
                "udp-relay" => {
                    udp = Some(value == "true" || value == "1");
                }
                "tfo" => {
                    tfo = Some(value == "true" || value == "1");
                }
                _ => {}
            }
        }
    }

    // Build plugin options if plugin is not empty
    if !plugin.is_empty() {
        plugin_opts = format!("obfs={}", pluginopts_mode);
        if !pluginopts_host.is_empty() {
            plugin_opts.push_str(&format!(";obfs-host={}", pluginopts_host));
        }
    }

    // Create the proxy object
    *node = Proxy::ss_construct(
        SS_DEFAULT_GROUP,
        name,
        server,
        port,
        password,
        method,
        &plugin,
        &plugin_opts,
        udp,
        tfo,
        scv,
        None,
        "",
    );

    true
}

/// Parse a Surge Shadowsocks configuration line
fn parse_surge_ss(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts
    if parts.len() < 3 {
        return false;
    }

    // Extract the server and port
    let server = parts[1];
    let port_str = parts[2];
    let port = match port_str.parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false;
    }

    // Default values
    let mut method = String::new();
    let mut password = String::new();
    let mut plugin = String::new();
    let mut plugin_opts = String::new();
    let mut pluginopts_mode = String::new();
    let mut pluginopts_host = String::new();
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional parameters
    for i in 3..parts.len() {
        if parts[i].contains('=') {
            let param_parts: Vec<&str> = parts[i].split('=').collect();
            if param_parts.len() != 2 {
                continue;
            }
            let key = param_parts[0].trim();
            let value = param_parts[1].trim();

            match key {
                "encrypt-method" => {
                    method = value.to_string();
                }
                "password" => {
                    password = value.to_string();
                }
                "obfs" => {
                    plugin = "simple-obfs".to_string();
                    pluginopts_mode = value.to_string();
                }
                "obfs-host" => {
                    pluginopts_host = value.to_string();
                }
                "udp-relay" => {
                    udp = Some(value == "true" || value == "1");
                }
                "tfo" => {
                    tfo = Some(value == "true" || value == "1");
                }
                "skip-cert-verify" => {
                    scv = Some(value == "true" || value == "1");
                }
                _ => {}
            }
        }
    }

    // Build plugin options if plugin is not empty
    if !plugin.is_empty() {
        plugin_opts = format!("obfs={}", pluginopts_mode);
        if !pluginopts_host.is_empty() {
            plugin_opts.push_str(&format!(";obfs-host={}", pluginopts_host));
        }
    }

    // Create the proxy object
    *node = Proxy::ss_construct(
        SS_DEFAULT_GROUP,
        name,
        server,
        port,
        &password,
        &method,
        &plugin,
        &plugin_opts,
        udp,
        tfo,
        scv,
        None,
        "",
    );

    true
}

/// Parse a Surge HTTP/HTTPS configuration line
fn parse_surge_http(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts
    if parts.len() < 3 {
        return false;
    }

    // Extract the server and port
    let server = parts[1];
    let port_str = parts[2];
    let port = match port_str.parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };

    // Determine if it's HTTPS
    let is_https = parts[0] == "https";

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

    // Parse additional parameters
    for i in 3..parts.len() {
        if parts[i].starts_with("username=") {
            username = &parts[i][9..];
        } else if parts[i].starts_with("password=") {
            password = &parts[i][9..];
        } else if parts[i] == "tfo=true" {
            tfo = Some(true);
        } else if parts[i] == "skip-cert-verify=true" {
            scv = Some(true);
        }
    }

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

    true
}

/// Parse a Surge SOCKS5 configuration line
fn parse_surge_socks(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts
    if parts.len() < 3 {
        return false;
    }

    // Extract the server and port
    let server = parts[1];
    let port_str = parts[2];
    let port = match port_str.parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };

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

    // Parse additional parameters
    if parts.len() >= 5 {
        username = parts[3];
        password = parts[4];
    }

    // Parse additional parameters
    for i in 5..parts.len() {
        if parts[i].contains('=') {
            let param_parts: Vec<&str> = parts[i].split('=').collect();
            if param_parts.len() != 2 {
                continue;
            }
            let key = param_parts[0].trim();
            let value = param_parts[1].trim();

            match key {
                "udp-relay" => {
                    udp = Some(value == "true" || value == "1");
                }
                "tfo" => {
                    tfo = Some(value == "true" || value == "1");
                }
                "skip-cert-verify" => {
                    scv = Some(value == "true" || value == "1");
                }
                _ => {}
            }
        }
    }

    // Create the proxy object
    *node = Proxy::socks_construct(
        SOCKS_DEFAULT_GROUP,
        name,
        server,
        port,
        username,
        password,
        udp,
        tfo,
        scv,
        "",
    );

    true
}

/// Parse a Surge VMess configuration line
fn parse_surge_vmess(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts
    if parts.len() < 3 {
        return false;
    }

    // Extract the server and port
    let server = parts[1];
    let port_str = parts[2];
    let port = match port_str.parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false;
    }

    // Default values
    let mut id = String::new();
    let mut net = "tcp".to_string();
    let method = "auto".to_string();
    let mut path = String::new();
    let mut host = String::new();
    let mut edge = String::new();
    let mut tls = String::new();
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;
    let mut tls13 = None;
    let mut aead = "1".to_string(); // Default to 1 for non-AEAD mode

    // Parse additional parameters
    for i in 3..parts.len() {
        if parts[i].contains('=') {
            let param_parts: Vec<&str> = parts[i].split('=').collect();
            if param_parts.len() != 2 {
                continue;
            }
            let key = param_parts[0].trim();
            let value = param_parts[1].trim();

            match key {
                "username" => {
                    id = value.to_string();
                }
                "ws" => {
                    net = if value == "true" {
                        "ws".to_string()
                    } else {
                        "tcp".to_string()
                    };
                }
                "tls" => {
                    tls = if value == "true" {
                        "tls".to_string()
                    } else {
                        String::new()
                    };
                }
                "ws-path" => {
                    path = value.to_string();
                }
                "obfs-host" => {
                    host = value.to_string();
                }
                "ws-headers" => {
                    // Parse headers in the format "Host:example.com|Edge:example.edge"
                    let headers: Vec<&str> = value.split('|').collect();
                    for header in headers {
                        let header_parts: Vec<&str> = header.split(':').collect();
                        if header_parts.len() == 2 {
                            let header_name = header_parts[0].trim().to_lowercase();
                            let header_value = header_parts[1].trim();
                            if header_name == "host" {
                                host = header_value.trim_matches('"').to_string();
                            } else if header_name == "edge" {
                                edge = header_value.trim_matches('"').to_string();
                            }
                        }
                    }
                }
                "udp-relay" => {
                    udp = Some(value == "true" || value == "1");
                }
                "tfo" => {
                    tfo = Some(value == "true" || value == "1");
                }
                "skip-cert-verify" => {
                    scv = Some(value == "true" || value == "1");
                }
                "tls13" => {
                    tls13 = Some(value == "true" || value == "1");
                }
                "vmess-aead" => {
                    aead = if value == "true" {
                        "0".to_string()
                    } else {
                        "1".to_string()
                    };
                }
                _ => {}
            }
        }
    }

    // Create the proxy object
    *node = Proxy::vmess_construct(
        V2RAY_DEFAULT_GROUP,
        name,
        server,
        port,
        "",
        &id,
        aead.parse::<u16>().unwrap_or(0),
        &net,
        &method,
        &path,
        &host,
        &edge,
        &tls,
        "",
        udp,
        tfo,
        scv,
        tls13,
        "",
    );

    true
}

/// Parse a Surge Trojan configuration line
fn parse_surge_trojan(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts
    if parts.len() < 4 {
        return false;
    }

    // Extract the server and port
    let server = parts[1];
    let port_str = parts[2];
    let port = match port_str.parse::<u16>() {
        Ok(p) => p,
        Err(_) => return false,
    };
    if port == 0 {
        return false;
    }

    // Default values
    let mut password = String::new();
    let mut host = String::new();
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional parameters
    for i in 3..parts.len() {
        if parts[i].contains('=') {
            let param_parts: Vec<&str> = parts[i].split('=').collect();
            if param_parts.len() != 2 {
                continue;
            }
            let key = param_parts[0].trim();
            let value = param_parts[1].trim();

            match key {
                "password" => {
                    password = value.to_string();
                }
                "sni" => {
                    host = value.to_string();
                }
                "udp-relay" => {
                    udp = Some(value == "true" || value == "1");
                }
                "tfo" => {
                    tfo = Some(value == "true" || value == "1");
                }
                "skip-cert-verify" => {
                    scv = Some(value == "true" || value == "1");
                }
                _ => {}
            }
        }
    }

    // If password parameter not found, use the 4th part directly
    if password.is_empty() {
        password = parts[3].to_string();
        // Check if it has password= prefix
        if password.starts_with("password=") {
            password = password[9..].to_string();
        }
    }

    // Create the proxy object
    *node = Proxy::trojan_construct(
        TROJAN_DEFAULT_GROUP.to_string(),
        name.to_string(),
        server.to_string(),
        port,
        password,
        None,
        if host.is_empty() { None } else { Some(host) },
        None,
        None,
        true,
        udp,
        tfo,
        scv,
        None,
        None,
    );

    true
}

/// Parse a Surge Snell configuration line
fn parse_surge_snell(config: &str, name: &str, node: &mut Proxy) -> bool {
    // Split the configuration into parts
    let parts: Vec<&str> = config.split(',').map(|s| s.trim()).collect();

    // Check minimum required parts
    if parts.len() < 3 {
        return false;
    }

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

    // Default values
    let mut password = String::new();
    let mut plugin = String::new();
    let mut host = String::new();
    let mut version = String::new();
    let mut udp = None;
    let mut tfo = None;
    let mut scv = None;

    // Parse additional parameters
    for i in 3..parts.len() {
        // Split by equals sign
        let param_parts: Vec<&str> = parts[i].split('=').collect();
        if param_parts.len() != 2 {
            continue;
        }
        let key = param_parts[0].trim();
        let value = param_parts[1].trim();

        match key {
            "psk" => password = value.to_string(),
            "obfs" => plugin = value.to_string(),
            "obfs-host" => host = value.to_string(),
            "udp-relay" => udp = Some(value == "true" || value == "1"),
            "tfo" => tfo = Some(value == "true" || value == "1"),
            "skip-cert-verify" => scv = Some(value == "true" || value == "1"),
            "version" => version = value.to_string(),
            _ => {}
        }
    }

    if password.is_empty() {
        return false;
    }

    // Create the proxy object
    *node = Proxy::snell_construct(
        SNELL_DEFAULT_GROUP.to_string(),
        name.to_string(),
        server.to_string(),
        port,
        password.to_string(),
        plugin,
        host,
        version.parse::<u16>().unwrap_or(1),
        udp,
        tfo,
        scv,
        None,
    );

    true
}