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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
use crate::generator::config::group::group_generate;
use crate::generator::config::remark::process_remark;
use crate::generator::ruleconvert::ruleset_to_sing_box::ruleset_to_sing_box;
use crate::models::{
    ExtraSettings, Proxy, ProxyGroupConfigs, ProxyGroupType, ProxyType, RulesetContent,
};
use crate::utils::base64::base64_encode;
use crate::Settings;
use log::error;
use serde_json::{json, Map, Value as JsonValue};

/// Format SingBox interval from seconds
///
/// # Arguments
/// * `interval` - Interval in seconds
///
/// # Returns
/// * Formatted interval string
fn format_singbox_interval(interval: u32) -> String {
    let mut result = String::new();
    let mut remaining_seconds = interval;

    if remaining_seconds >= 3600 {
        result.push_str(&format!("{}h", remaining_seconds / 3600));
        remaining_seconds %= 3600;
    }

    if remaining_seconds >= 60 {
        result.push_str(&format!("{}m", remaining_seconds / 60));
        remaining_seconds %= 60;
    }

    if remaining_seconds > 0 {
        result.push_str(&format!("{}s", remaining_seconds));
    }

    result
}

/// Build SingBox transport configuration
///
/// # Arguments
/// * `proxy` - The proxy node
///
/// # Returns
/// * Transport configuration as JSON
fn build_singbox_transport(proxy: &Proxy) -> JsonValue {
    let mut transport = Map::new();

    // Extract transport protocol
    let transproto = proxy.transfer_protocol.as_deref().unwrap_or("");

    match transproto {
        "http" => {
            if let Some(host) = &proxy.host {
                if !host.is_empty() {
                    transport.insert("host".to_string(), JsonValue::String(host.clone()));
                }
            }
            // Fall through to WS handler for common settings
            transport.insert("type".to_string(), JsonValue::String("http".to_string()));
        }
        "ws" => {
            transport.insert("type".to_string(), JsonValue::String("ws".to_string()));

            // Set path or default to "/"
            if let Some(path) = &proxy.path {
                transport.insert("path".to_string(), JsonValue::String(path.clone()));
            } else {
                transport.insert("path".to_string(), JsonValue::String("/".to_string()));
            }

            // Add headers
            let mut headers = Map::new();
            if let Some(host) = &proxy.host {
                if !host.is_empty() {
                    headers.insert("Host".to_string(), JsonValue::String(host.clone()));
                }
            }

            if let Some(edge) = &proxy.edge {
                if !edge.is_empty() {
                    headers.insert("Edge".to_string(), JsonValue::String(edge.clone()));
                }
            }

            if !headers.is_empty() {
                transport.insert("headers".to_string(), JsonValue::Object(headers));
            }
        }
        "grpc" => {
            transport.insert("type".to_string(), JsonValue::String("grpc".to_string()));

            if let Some(path) = &proxy.path {
                if !path.is_empty() {
                    transport.insert("service_name".to_string(), JsonValue::String(path.clone()));
                }
            }
        }
        _ => {} // Default empty transport
    }

    JsonValue::Object(transport)
}

/// Add common members to a SingBox proxy configuration
///
/// # Arguments
/// * `proxy_obj` - Proxy object to add members to
/// * `proxy` - Source proxy data
/// * `proxy_type` - Type of proxy
///
/// # Returns
/// * Updated proxy object
fn add_singbox_common_members(
    proxy_obj: &mut Map<String, JsonValue>,
    proxy: &Proxy,
    proxy_type: &str,
) {
    proxy_obj.insert(
        "type".to_string(),
        JsonValue::String(proxy_type.to_string()),
    );
    proxy_obj.insert("tag".to_string(), JsonValue::String(proxy.remark.clone()));
    proxy_obj.insert(
        "server".to_string(),
        JsonValue::String(proxy.hostname.clone()),
    );
    proxy_obj.insert(
        "server_port".to_string(),
        JsonValue::Number(proxy.port.into()),
    );
}

/// Convert string array to JSON array
///
/// # Arguments
/// * `array` - String containing values separated by delimiter
/// * `delimiter` - Delimiter separating values
///
/// # Returns
/// * Array of values as JSON
fn string_array_to_json_array(array: &str, delimiter: &str) -> JsonValue {
    let values: Vec<JsonValue> = array
        .split(delimiter)
        .map(|s| JsonValue::String(s.trim().to_string()))
        .collect();

    JsonValue::Array(values)
}

/// Convert proxies to SingBox format
///
/// # Arguments
/// * `nodes` - List of proxy nodes to convert
/// * `base_conf` - Base SingBox configuration as a string
/// * `ruleset_content_array` - Array of ruleset contents to apply
/// * `extra_proxy_group` - Extra proxy group configurations
/// * `ext` - Extra settings for conversion
///
/// # Returns
/// * Converted configuration as a string
pub fn proxy_to_singbox(
    nodes: &mut Vec<Proxy>,
    base_conf: &str,
    ruleset_content_array: &mut Vec<RulesetContent>,
    extra_proxy_group: &ProxyGroupConfigs,
    ext: &mut ExtraSettings,
) -> String {
    // Parse the base configuration
    let mut json: JsonValue = if ext.nodelist {
        json!({})
    } else {
        match serde_json::from_str(base_conf) {
            Ok(json) => json,
            Err(e) => {
                error!(
                    "SingBox base loader failed with error: {}, base_conf: {}",
                    e, base_conf
                );
                return String::new();
            }
        }
    };

    // Convert nodes to outbounds
    let mut outbounds = Vec::new();
    let mut nodelist = Vec::new();
    let mut remarks_list = Vec::new();

    // Add default outbounds if not in nodelist mode
    if !ext.nodelist {
        // Direct outbound
        outbounds.push(json!({
            "type": "direct",
            "tag": "DIRECT"
        }));

        // Reject outbound
        outbounds.push(json!({
            "type": "block",
            "tag": "REJECT"
        }));

        // DNS outbound
        outbounds.push(json!({
            "type": "dns",
            "tag": "dns-out"
        }));
    }

    // Process each proxy node
    for node in nodes.iter_mut() {
        // Add proxy type prefix if enabled
        if ext.append_proxy_type {
            let proxy_type = node.proxy_type.to_string();
            node.remark = format!("[{}] {}", proxy_type, node.remark);
        }

        // Process remark
        let mut remark = node.remark.clone();
        process_remark(&mut remark, &remarks_list, false);
        node.remark = remark;

        // Define tribool values with defaults from ext and override with node-specific values
        let mut udp = ext.udp;
        let mut tfo = ext.tfo;
        let mut scv = ext.skip_cert_verify;

        udp = node.udp.as_ref().map_or(udp, |val| Some(*val));
        tfo = node.tcp_fast_open.as_ref().map_or(tfo, |val| Some(*val));
        scv = node.allow_insecure.as_ref().map_or(scv, |val| Some(*val));

        // Create proxy object based on type
        let mut proxy_obj = match node.proxy_type {
            ProxyType::Shadowsocks => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "shadowsocks");

                // Add encryption method and password
                if let Some(method) = &node.encrypt_method {
                    obj.insert("method".to_string(), JsonValue::String(method.clone()));
                }

                if let Some(password) = &node.password {
                    obj.insert("password".to_string(), JsonValue::String(password.clone()));
                }

                // Handle plugin if present
                if let (Some(plugin), Some(plugin_opts)) = (&node.plugin, &node.plugin_option) {
                    if !plugin.is_empty() && !plugin_opts.is_empty() {
                        let plugin_name = if plugin == "simple-obfs" {
                            "obfs-local"
                        } else {
                            plugin
                        };

                        obj.insert(
                            "plugin".to_string(),
                            JsonValue::String(plugin_name.to_string()),
                        );
                        obj.insert(
                            "plugin_opts".to_string(),
                            JsonValue::String(plugin_opts.clone()),
                        );
                    }
                }

                obj
            }
            ProxyType::ShadowsocksR => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "shadowsocksr");

                // Add shadowsocksr specific fields
                if let Some(method) = &node.encrypt_method {
                    obj.insert("method".to_string(), JsonValue::String(method.clone()));
                }

                if let Some(password) = &node.password {
                    obj.insert("password".to_string(), JsonValue::String(password.clone()));
                }

                if let Some(protocol) = &node.protocol {
                    obj.insert("protocol".to_string(), JsonValue::String(protocol.clone()));
                }

                if let Some(protocol_param) = &node.protocol_param {
                    obj.insert(
                        "protocol_param".to_string(),
                        JsonValue::String(protocol_param.clone()),
                    );
                }

                if let Some(obfs) = &node.obfs {
                    obj.insert("obfs".to_string(), JsonValue::String(obfs.clone()));
                }

                if let Some(obfs_param) = &node.obfs_param {
                    obj.insert(
                        "obfs_param".to_string(),
                        JsonValue::String(obfs_param.clone()),
                    );
                }

                obj
            }
            ProxyType::VMess => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "vmess");

                // Add VMess specific fields
                if let Some(user_id) = &node.user_id {
                    obj.insert("uuid".to_string(), JsonValue::String(user_id.clone()));
                }

                obj.insert(
                    "alter_id".to_string(),
                    JsonValue::Number(node.alter_id.into()),
                );

                if let Some(method) = &node.encrypt_method {
                    obj.insert("security".to_string(), JsonValue::String(method.clone()));
                }

                // Add transport settings if any
                let transport = build_singbox_transport(node);
                if !transport.as_object().unwrap().is_empty() {
                    obj.insert("transport".to_string(), transport);
                }

                obj
            }
            ProxyType::Trojan => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "trojan");

                // Add Trojan specific fields
                if let Some(password) = &node.password {
                    obj.insert("password".to_string(), JsonValue::String(password.clone()));
                }

                // Add transport settings if any
                let transport = build_singbox_transport(node);
                if !transport.as_object().unwrap().is_empty() {
                    obj.insert("transport".to_string(), transport);
                }

                obj
            }
            ProxyType::WireGuard => {
                let mut obj = Map::new();
                obj.insert(
                    "type".to_string(),
                    JsonValue::String("wireguard".to_string()),
                );
                obj.insert("tag".to_string(), JsonValue::String(node.remark.clone()));

                // Add WireGuard specific fields
                let mut addresses = Vec::new();
                if let Some(self_ip) = &node.self_ip {
                    addresses.push(JsonValue::String(self_ip.clone()));
                }

                if let Some(self_ipv6) = &node.self_ipv6 {
                    if !self_ipv6.is_empty() {
                        addresses.push(JsonValue::String(self_ipv6.clone()));
                    }
                }

                obj.insert("local_address".to_string(), JsonValue::Array(addresses));

                if let Some(private_key) = &node.private_key {
                    obj.insert(
                        "private_key".to_string(),
                        JsonValue::String(private_key.clone()),
                    );
                }

                // Create peer
                let mut peer = Map::new();
                peer.insert(
                    "server".to_string(),
                    JsonValue::String(node.hostname.clone()),
                );
                peer.insert(
                    "server_port".to_string(),
                    JsonValue::Number(node.port.into()),
                );

                if let Some(public_key) = &node.public_key {
                    peer.insert(
                        "public_key".to_string(),
                        JsonValue::String(public_key.clone()),
                    );
                }

                if let Some(pre_shared_key) = &node.pre_shared_key {
                    if !pre_shared_key.is_empty() {
                        peer.insert(
                            "pre_shared_key".to_string(),
                            JsonValue::String(pre_shared_key.clone()),
                        );
                    }
                }

                if !node.allowed_ips.is_empty() {
                    let allowed_ips = string_array_to_json_array(&node.allowed_ips, ",");
                    peer.insert("allowed_ips".to_string(), allowed_ips);
                }

                if let Some(client_id) = &node.client_id {
                    if !client_id.is_empty() {
                        let reserved = string_array_to_json_array(client_id, ",");
                        peer.insert("reserved".to_string(), reserved);
                    }
                }

                // Add peer to peers array
                let peers = vec![JsonValue::Object(peer)];
                obj.insert("peers".to_string(), JsonValue::Array(peers));

                // Add MTU if present
                if node.mtu > 0 {
                    obj.insert("mtu".to_string(), JsonValue::Number(node.mtu.into()));
                }

                obj
            }
            ProxyType::Hysteria => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "hysteria");

                // Add Hysteria specific fields
                if node.up_speed > 0 {
                    obj.insert(
                        "up_mbps".to_string(),
                        JsonValue::Number(node.up_speed.into()),
                    );
                }

                if node.down_speed > 0 {
                    obj.insert(
                        "down_mbps".to_string(),
                        JsonValue::Number(node.down_speed.into()),
                    );
                }

                if let Some(obfs) = &node.obfs {
                    if !obfs.is_empty() {
                        obj.insert("obfs".to_string(), JsonValue::String(obfs.clone()));
                    }
                }

                if let Some(auth_str) = &node.auth_str {
                    if !auth_str.is_empty() {
                        obj.insert("auth_str".to_string(), JsonValue::String(auth_str.clone()));

                        // Create a temporary String
                        let auth_str_value = auth_str.clone();
                        obj.insert(
                            "auth".to_string(),
                            JsonValue::String(base64_encode(&auth_str_value)),
                        );
                    }
                }

                if node.recv_window_conn > 0 {
                    obj.insert(
                        "recv_window_conn".to_string(),
                        JsonValue::Number(node.recv_window_conn.into()),
                    );
                }

                if node.recv_window > 0 {
                    obj.insert(
                        "recv_window".to_string(),
                        JsonValue::Number(node.recv_window.into()),
                    );
                }

                if let Some(disable_mtu_discovery) = node.disable_mtu_discovery {
                    obj.insert(
                        "disable_mtu_discovery".to_string(),
                        JsonValue::Bool(disable_mtu_discovery),
                    );
                }

                // Add TLS settings
                let mut tls = Map::new();
                tls.insert("enabled".to_string(), JsonValue::Bool(true));

                if let Some(allow_insecure) = scv {
                    tls.insert("insecure".to_string(), JsonValue::Bool(allow_insecure));
                }

                if !node.alpn.is_empty() && node.alpn.len() > 0 {
                    let alpn = vec![JsonValue::String(node.alpn.iter().next().unwrap().clone())];
                    tls.insert("alpn".to_string(), JsonValue::Array(alpn));
                }

                if let Some(ca) = &node.ca {
                    if !ca.is_empty() {
                        tls.insert("certificate".to_string(), JsonValue::String(ca.to_string()));
                    }
                }

                if let Some(ca_str) = &node.ca_str {
                    if !ca_str.is_empty() {
                        tls.insert("certificate".to_string(), JsonValue::String(ca_str.clone()));
                    }
                }

                obj.insert("tls".to_string(), JsonValue::Object(tls));
                obj
            }
            ProxyType::Hysteria2 => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "hysteria2");

                // Add Hysteria2 specific fields
                if node.up_speed > 0 {
                    obj.insert(
                        "up_mbps".to_string(),
                        JsonValue::Number(node.up_speed.into()),
                    );
                }

                if node.down_speed > 0 {
                    obj.insert(
                        "down_mbps".to_string(),
                        JsonValue::Number(node.down_speed.into()),
                    );
                }

                if let Some(obfs) = &node.obfs {
                    if !obfs.is_empty() {
                        let mut obfs_obj = Map::new();
                        obfs_obj.insert("type".to_string(), JsonValue::String(obfs.clone()));

                        if let Some(obfs_param) = &node.obfs_param {
                            if !obfs_param.is_empty() {
                                obfs_obj.insert(
                                    "password".to_string(),
                                    JsonValue::String(obfs_param.clone()),
                                );
                            }
                        }

                        obj.insert("obfs".to_string(), JsonValue::Object(obfs_obj));
                    }
                }

                if let Some(password) = &node.password {
                    if !password.is_empty() {
                        obj.insert("password".to_string(), JsonValue::String(password.clone()));
                    }
                }

                // Add TLS settings
                let mut tls = Map::new();
                tls.insert("enabled".to_string(), JsonValue::Bool(true));

                if let Some(allow_insecure) = scv {
                    tls.insert("insecure".to_string(), JsonValue::Bool(allow_insecure));
                }

                if !node.alpn.is_empty() && node.alpn.len() > 0 {
                    let alpn = vec![JsonValue::String(node.alpn.iter().next().unwrap().clone())];
                    tls.insert("alpn".to_string(), JsonValue::Array(alpn));
                }

                if let Some(ca) = &node.ca {
                    if !ca.is_empty() {
                        tls.insert("certificate".to_string(), JsonValue::String(ca.to_string()));
                    }
                }

                if let Some(ca_str) = &node.ca_str {
                    if !ca_str.is_empty() {
                        tls.insert("certificate".to_string(), JsonValue::String(ca_str.clone()));
                    }
                }

                obj.insert("tls".to_string(), JsonValue::Object(tls));
                obj
            }
            ProxyType::HTTP | ProxyType::HTTPS => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "http");

                // Add HTTP/HTTPS specific fields
                if let Some(username) = &node.username {
                    obj.insert("username".to_string(), JsonValue::String(username.clone()));
                }

                if let Some(password) = &node.password {
                    obj.insert("password".to_string(), JsonValue::String(password.clone()));
                }

                obj
            }
            ProxyType::Socks5 => {
                let mut obj = Map::new();
                add_singbox_common_members(&mut obj, node, "socks");

                // Add Socks5 specific fields
                obj.insert("version".to_string(), JsonValue::String("5".to_string()));

                if let Some(username) = &node.username {
                    obj.insert("username".to_string(), JsonValue::String(username.clone()));
                }

                if let Some(password) = &node.password {
                    obj.insert("password".to_string(), JsonValue::String(password.clone()));
                }

                obj
            }
            _ => continue, // Skip unsupported types
        };

        // Add TLS settings for protocols that need it
        if node.tls_secure {
            let mut tls = Map::new();
            tls.insert("enabled".to_string(), JsonValue::Bool(true));

            // Set server_name from ServerName or Host
            if let Some(server_name) = &node.server_name {
                if !server_name.is_empty() {
                    tls.insert(
                        "server_name".to_string(),
                        JsonValue::String(server_name.clone()),
                    );
                }
            } else if let Some(host) = &node.host {
                if !host.is_empty() {
                    tls.insert("server_name".to_string(), JsonValue::String(host.clone()));
                }
            }

            // Add insecure option
            if let Some(allow_insecure) = scv {
                tls.insert("insecure".to_string(), JsonValue::Bool(allow_insecure));
            }

            proxy_obj.insert("tls".to_string(), JsonValue::Object(tls));
        }

        // Add UDP and TFO settings
        if let Some(udp_enabled) = udp {
            if !udp_enabled {
                proxy_obj.insert("network".to_string(), JsonValue::String("tcp".to_string()));
            }
        }

        if let Some(tfo_enabled) = tfo {
            proxy_obj.insert("tcp_fast_open".to_string(), JsonValue::Bool(tfo_enabled));
        }

        // Add to node list and outbounds
        nodelist.push(node.clone());
        remarks_list.push(node.remark.clone());
        outbounds.push(JsonValue::Object(proxy_obj));
    }

    // If nodelist mode, just return outbounds
    if ext.nodelist {
        if let JsonValue::Object(obj) = &mut json {
            obj.insert("outbounds".to_string(), JsonValue::Array(outbounds));
        }

        return serde_json::to_string_pretty(&json).unwrap_or_default();
    }

    // Process proxy groups
    for group in extra_proxy_group {
        let mut filtered_nodelist = Vec::new();

        // Determine group type
        let group_type = match group.group_type {
            ProxyGroupType::Select => "selector",
            ProxyGroupType::URLTest | ProxyGroupType::Fallback | ProxyGroupType::LoadBalance => {
                "urltest"
            }
            _ => continue, // Skip unsupported types
        };

        // Generate filtered proxy list
        for proxy_name in &group.proxies {
            group_generate(proxy_name, &nodelist, &mut filtered_nodelist, true, ext);
        }

        // Add DIRECT if empty
        if filtered_nodelist.is_empty() {
            filtered_nodelist.push("DIRECT".to_string());
        }

        // Create group object
        let mut group_obj = Map::new();
        group_obj.insert(
            "type".to_string(),
            JsonValue::String(group_type.to_string()),
        );
        group_obj.insert("tag".to_string(), JsonValue::String(group.name.clone()));

        // Add outbounds
        let group_outbounds: Vec<JsonValue> = filtered_nodelist
            .iter()
            .map(|name| JsonValue::String(name.clone()))
            .collect();

        group_obj.insert("outbounds".to_string(), JsonValue::Array(group_outbounds));

        // Add URL Test specific settings
        if group.group_type == ProxyGroupType::URLTest {
            group_obj.insert("url".to_string(), JsonValue::String(group.url.clone()));
            group_obj.insert(
                "interval".to_string(),
                JsonValue::String(format_singbox_interval(group.interval as u32)),
            );

            if group.tolerance > 0 {
                group_obj.insert(
                    "tolerance".to_string(),
                    JsonValue::Number(group.tolerance.into()),
                );
            }
        }

        outbounds.push(JsonValue::Object(group_obj));
    }

    let global = Settings::current();

    // Add global group if enabled
    if global.singbox_add_clash_modes {
        let mut global_group = Map::new();
        global_group.insert(
            "type".to_string(),
            JsonValue::String("selector".to_string()),
        );
        global_group.insert("tag".to_string(), JsonValue::String("GLOBAL".to_string()));

        let mut global_outbounds = vec![JsonValue::String("DIRECT".to_string())];

        // Add all remarks
        for remark in &remarks_list {
            global_outbounds.push(JsonValue::String(remark.clone()));
        }

        global_group.insert("outbounds".to_string(), JsonValue::Array(global_outbounds));
        outbounds.push(JsonValue::Object(global_group));
    }

    // Add outbounds to JSON
    if let JsonValue::Object(obj) = &mut json {
        obj.insert("outbounds".to_string(), JsonValue::Array(outbounds));
    }

    // Handle rule generation
    if !ext.enable_rule_generator {
        return serde_json::to_string_pretty(&json).unwrap_or_default();
    }

    // Generate rules
    ruleset_to_sing_box(
        &mut json,
        ruleset_content_array,
        ext.overwrite_original_rules,
    );

    serde_json::to_string_pretty(&json).unwrap_or_default()
}

/// Generate a Sing-Box configuration
pub fn generate_singbox(
    _proxies: &[Proxy],
    _group_config: &ProxyGroupConfigs,
    _ruleset_content_array: &mut Vec<RulesetContent>,
    _config: &str,
    _extra_settings: &ExtraSettings,
) -> String {
    String::new()
}