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
use crate::generator::config::group::group_generate;
use crate::generator::config::remark::process_remark;
use crate::generator::ruleconvert::ruleset_to_surge::ruleset_to_surge;
use crate::models::{
    BalanceStrategy, ExtraSettings, Proxy, ProxyGroupConfigs, ProxyGroupType, ProxyType,
    RulesetContent,
};
use crate::utils::ini_reader::IniReader;
use crate::utils::string::join;
use log::error;
use std::collections::HashMap;

/// Convert proxies to Loon format
///
/// This function converts a list of proxies to the Loon configuration format,
/// using a base configuration as a template and applying rules from ruleset_content_array.
///
/// # Arguments
/// * `nodes` - List of proxy nodes to convert
/// * `base_conf` - Base Loon 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 async fn proxy_to_loon(
    nodes: &mut Vec<Proxy>,
    base_conf: &str,
    ruleset_content_array: &mut Vec<RulesetContent>,
    extra_proxy_group: &ProxyGroupConfigs,
    ext: &mut ExtraSettings,
) -> String {
    let mut ini = IniReader::new();
    let mut output_nodelist = String::new();
    let mut nodelist = Vec::new();
    let mut remarks_list = Vec::new();

    // Configure INI reader
    ini.store_any_line = true;
    ini.add_direct_save_section("Plugin");

    // Parse base configuration
    if ini.parse(base_conf).is_err() && !ext.nodelist {
        error!(
            "Loon base loader failed with error: {}",
            ini.get_last_error()
        );
        return String::new();
    }

    // Prepare Proxy section
    ini.set_current_section("Proxy");
    ini.erase_section();

    // 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);

        // Extract node properties for easier access
        let hostname = &node.hostname;
        let port = node.port.to_string();
        let username = node.username.as_deref().unwrap_or("");
        let password = node.password.as_deref().unwrap_or("");
        let method = node.encrypt_method.as_deref().unwrap_or("");
        let id = node.user_id.as_deref().unwrap_or("");
        let transproto = node.transfer_protocol.as_deref().unwrap_or("");
        let host = node.host.as_deref().unwrap_or("");
        let path = node.path.as_deref().unwrap_or("");
        let protocol = node.protocol.as_deref().unwrap_or("");
        let protoparam = node.protocol_param.as_deref().unwrap_or("");
        let obfs = node.obfs.as_deref().unwrap_or("");
        let obfsparam = node.obfs_param.as_deref().unwrap_or("");
        let plugin = node.plugin.as_deref().unwrap_or("");
        let pluginopts = node.plugin_option.as_deref().unwrap_or("");
        let tls_secure = node.tls_secure;

        // Define tribool values with defaults from ext and override with node-specific values
        let scv = ext.skip_cert_verify;
        let scv = node.allow_insecure.as_ref().map_or(scv, |val| Some(*val));

        let mut proxy;

        // Build proxy string based on type
        match node.proxy_type {
            ProxyType::Shadowsocks => {
                proxy = format!(
                    "Shadowsocks,{},{},{},\"{}\"",
                    hostname, port, method, password
                );

                if plugin == "simple-obfs" || plugin == "obfs-local" {
                    if !pluginopts.is_empty() {
                        // Replace obfs=xxx;obfs-host=yyy with xxx,yyy
                        let plugin_opts =
                            pluginopts.replace(";obfs-host=", ",").replace("obfs=", "");
                        proxy.push_str(&format!(",{}", plugin_opts));
                    }
                } else if !plugin.is_empty() {
                    continue;
                }
            }
            ProxyType::VMess => {
                let actual_method = if method == "auto" {
                    "chacha20-ietf-poly1305"
                } else {
                    method
                };

                proxy = format!(
                    "vmess,{},{},{},\"{}\",over-tls={}",
                    hostname,
                    port,
                    actual_method,
                    id,
                    if tls_secure { "true" } else { "false" }
                );

                if tls_secure {
                    proxy.push_str(&format!(",tls-name={}", host));
                }

                match transproto {
                    "tcp" => {
                        proxy.push_str(",transport=tcp");
                    }
                    "ws" => {
                        proxy.push_str(&format!(",transport=ws,path={},host={}", path, host));
                    }
                    _ => continue,
                }

                if scv.is_some() {
                    proxy.push_str(&format!(
                        ",skip-cert-verify={}",
                        if scv.unwrap_or(false) {
                            "true"
                        } else {
                            "false"
                        }
                    ));
                }
            }
            ProxyType::ShadowsocksR => {
                proxy = format!(
                    "ShadowsocksR,{},{},{},\"{}\",protocol={},protocol-param={},obfs={},obfs-param={}",
                    hostname, port, method, password, protocol, protoparam, obfs, obfsparam
                );
            }
            ProxyType::HTTP => {
                proxy = format!("http,{},{}", hostname, port);

                if !username.is_empty() {
                    proxy.push_str(&format!(",{}", username));

                    if !password.is_empty() {
                        proxy.push_str(&format!(",\"{}\"", password));
                    }
                }
            }
            ProxyType::HTTPS => {
                proxy = format!("https,{},{}", hostname, port);

                if !username.is_empty() {
                    proxy.push_str(&format!(",{}", username));

                    if !password.is_empty() {
                        proxy.push_str(&format!(",\"{}\"", password));
                    }
                }

                if !host.is_empty() {
                    proxy.push_str(&format!(",tls-name={}", host));
                }

                if scv.is_some() {
                    proxy.push_str(&format!(
                        ",skip-cert-verify={}",
                        if scv.unwrap_or(false) {
                            "true"
                        } else {
                            "false"
                        }
                    ));
                }
            }
            ProxyType::Trojan => {
                proxy = format!("trojan,{},{},\"{}\"", hostname, port, password);

                if !host.is_empty() {
                    proxy.push_str(&format!(",tls-name={}", host));
                }

                if scv.is_some() {
                    proxy.push_str(&format!(
                        ",skip-cert-verify={}",
                        if scv.unwrap_or(false) {
                            "true"
                        } else {
                            "false"
                        }
                    ));
                }
            }
            ProxyType::Socks5 => {
                proxy = format!("socks5,{},{}", hostname, port);

                if !username.is_empty() && !password.is_empty() {
                    proxy.push_str(&format!(",{},\"{}\"", username, password));
                }

                proxy.push_str(&format!(
                    ",over-tls={}",
                    if tls_secure { "true" } else { "false" }
                ));

                if tls_secure {
                    if !host.is_empty() {
                        proxy.push_str(&format!(",tls-name={}", host));
                    }

                    if scv.is_some() {
                        proxy.push_str(&format!(
                            ",skip-cert-verify={}",
                            if scv.unwrap_or(false) {
                                "true"
                            } else {
                                "false"
                            }
                        ));
                    }
                }
            }
            ProxyType::WireGuard => {
                proxy = format!(
                    "wireguard, interface-ip={}",
                    node.self_ip.as_deref().unwrap_or("")
                );

                if let Some(ipv6) = &node.self_ipv6 {
                    if !ipv6.is_empty() {
                        proxy.push_str(&format!(", interface-ipv6={}", ipv6));
                    }
                }

                if let Some(private_key) = &node.private_key {
                    proxy.push_str(&format!(", private-key={}", private_key));
                }

                // Add DNS servers
                for server in &node.dns_servers {
                    // Check if IPv4 or IPv6
                    if server.contains('.') {
                        proxy.push_str(&format!(", dns={}", server));
                    } else if server.contains(':') {
                        proxy.push_str(&format!(", dnsv6={}", server));
                    }
                }

                if node.mtu > 0 {
                    proxy.push_str(&format!(", mtu={}", node.mtu));
                }

                if node.keep_alive > 0 {
                    proxy.push_str(&format!(", keepalive={}", node.keep_alive));
                }

                // Add peer info
                proxy.push_str(&format!(", peers=[{{{}}}]", generate_peer(node, true)));
            }
            _ => continue,
        }

        // Add fast-open option if enabled
        if ext.tfo.unwrap_or(false) {
            proxy.push_str(",fast-open=true");
        }

        // Add UDP option if enabled
        if ext.udp.unwrap_or(false) {
            proxy.push_str(",udp=true");
        }

        // Add to nodelist or INI
        if ext.nodelist {
            output_nodelist.push_str(&format!("{} = {}\n", remark, proxy));
        } else {
            ini.set("{NONAME}", &format!("{} = {}", remark, proxy), "")
                .unwrap_or(());
            nodelist.push(node.clone());
            remarks_list.push(remark);
        }
    }

    if ext.nodelist {
        return output_nodelist;
    }

    // Process proxy groups
    let original_groups: HashMap<String, Vec<String>> = HashMap::new();
    ini.set_current_section("Proxy Group");
    ini.erase_section();

    for group in extra_proxy_group {
        let mut filtered_nodelist = Vec::new();
        let mut group_str;

        match group.group_type {
            ProxyGroupType::Select
            | ProxyGroupType::LoadBalance
            | ProxyGroupType::URLTest
            | ProxyGroupType::Fallback => {
                // These types are supported
            }
            ProxyGroupType::SSID => {
                if group.proxies.len() < 2 {
                    continue;
                }
                group_str = format!("{},default={},", group.type_str(), group.proxies[0]);
                group_str.push_str(&join(&group.proxies[1..], ","));
                ini.set("{NONAME}", &format!("{} = {}", group.name, group_str), "")
                    .unwrap_or(());
                continue;
            }
            _ => continue,
        }

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

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

        // Update original_groups handling to extract image URLs
        // In C++ original_groups is loaded from the INI and checked for image-url values
        // For each group, get the items from original_groups and check for img-url:
        let mut img_url = String::new();
        if let Some(values) = original_groups.get(&group.name) {
            if !values.is_empty() {
                // Check if the last element contains "img-url"
                let last_item = &values[values.len() - 1];
                if last_item.contains("img-url") {
                    img_url = last_item.clone();
                }
            }
        }

        // Build group string
        group_str = format!("{},", group.type_str());
        group_str.push_str(&join(&filtered_nodelist, ","));

        // Ensure proper order for group fields to match C++ implementation
        if group.group_type != ProxyGroupType::Select {
            // C++ adds these fields in this specific order
            group_str.push_str(&format!(",url={},interval={}", group.url, group.interval));

            // The additional fields are added based on group type
            if group.group_type == ProxyGroupType::LoadBalance {
                let algorithm = match group.strategy {
                    BalanceStrategy::RoundRobin => "round-robin",
                    BalanceStrategy::ConsistentHashing => "pcc",
                };
                group_str.push_str(&format!(",algorithm={}", algorithm));

                if group.timeout > 0 {
                    group_str.push_str(&format!(",max-timeout={}", group.timeout));
                }
            } else if group.group_type == ProxyGroupType::URLTest {
                // For URL-Test, add tolerance
                if group.tolerance > 0 {
                    group_str.push_str(&format!(",tolerance={}", group.tolerance));
                }
            } else if group.group_type == ProxyGroupType::Fallback {
                // For Fallback, add max-timeout
                group_str.push_str(&format!(",max-timeout={}", group.timeout));
            }
        }

        // Add image URL if found
        if !img_url.is_empty() {
            group_str.push_str(&format!(",{}", img_url));
        }

        ini.set("{NONAME}", &format!("{} = {}", group.name, group_str), "")
            .unwrap_or(());
    }

    // Generate rules if enabled
    if ext.enable_rule_generator {
        ruleset_to_loon(
            &mut ini,
            ruleset_content_array,
            ext.overwrite_original_rules,
            &ext.managed_config_prefix,
        )
        .await;
    }

    ini.to_string()
}

/// Generate a WireGuard peer configuration string
///
/// # Arguments
/// * `node` - Proxy node with WireGuard configuration
/// * `client_id_as_reserved` - Whether to use client_id as reserved field
///
/// # Returns
/// * Peer configuration string
fn generate_peer(node: &Proxy, client_id_as_reserved: bool) -> String {
    let mut peer = String::new();

    if let Some(public_key) = &node.public_key {
        peer.push_str(&format!("public-key={}", public_key));
    }

    peer.push_str(&format!(", endpoint={}:{}", node.hostname, node.port));

    if !node.allowed_ips.is_empty() {
        peer.push_str(&format!(", allowed-ips={}", node.allowed_ips));
    }

    if let Some(client_id) = &node.client_id {
        if !client_id.is_empty() {
            if client_id_as_reserved {
                peer.push_str(&format!(", reserved={}", client_id));
            } else {
                peer.push_str(&format!(", client-id={}", client_id));
            }
        }
    }

    peer
}

async fn ruleset_to_loon(
    ini: &mut IniReader,
    ruleset_content_array: &mut Vec<RulesetContent>,
    overwrite_original_rules: bool,
    managed_config_prefix: &str,
) {
    ruleset_to_surge(
        ini,
        ruleset_content_array,
        -4,
        overwrite_original_rules,
        managed_config_prefix,
    )
    .await;
}