tobaru 0.7.1

Port forwarding CLI utility written in Rust with allowlists, TLS SNI and ALPN routing, round-robin forwarding (load balancing), IP-to-destination mapping, and iptables support.
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
use json::JsonValue;
use log::debug;
use percent_encoding::percent_decode_str;
use url::Url;

use std::collections::{HashMap, HashSet};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};

#[derive(Debug, Clone)]
pub struct ServerConfig {
    pub server_address: SocketAddr,
    pub use_iptables: bool,
    pub target_configs: TargetConfigs,
}

#[derive(Debug, Clone)]
pub enum TargetConfigs {
    Tcp(Vec<TcpTargetConfig>),
    Udp(Vec<UdpTargetConfig>),
}

#[derive(Debug, Clone)]
pub struct TcpTargetAddress {
    // We don't convert to SocketAddr here so that if it's a hostname,
    // it could be updated without restarting the process depending on
    // the system's DNS settings.
    pub address: String,
    pub port: u16,
    pub client_tls_config: Option<ClientTlsConfig>,
}

pub type IpMask = (Ipv6Addr, u32);

#[derive(Debug, Clone)]
pub struct ServerTlsConfig {
    pub sni_hostnames: HashSet<TlsOption>,
    // the alpn protocols to show in the serverhello response
    pub alpn_protocols: HashSet<TlsOption>,

    pub cert_path: String,
    pub key_path: String,
    pub optional: bool,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum TlsOption {
    None,
    Any,
    Specified(String),
}

impl From<&str> for TlsOption {
    fn from(s: &str) -> Self {
        match s {
            "any" => TlsOption::Any,
            "none" => TlsOption::None,
            hostname => TlsOption::Specified(hostname.to_string()),
        }
    }
}

impl TlsOption {
    pub fn is_specified(&self) -> bool {
        match self {
            TlsOption::Specified(_) => true,
            _ => false,
        }
    }
    pub fn unwrap_specified(&self) -> &str {
        match self {
            TlsOption::Specified(s) => s.as_str(),
            _ => panic!("Not a hostname"),
        }
    }
}

#[derive(Debug, Clone)]
pub struct ClientTlsConfig {
    pub verify: bool,
}

#[derive(Debug, Clone)]
pub struct TcpTargetConfig {
    pub allowed_ips: Vec<IpMask>,
    pub target_addresses: Vec<TcpTargetAddress>,
    pub server_tls_config: Option<ServerTlsConfig>,
    pub tcp_nodelay: bool,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct UdpTargetAddress {
    // We don't convert to SocketAddr here so that if it's a hostname,
    // it could be updated without restarting the process depending on
    // the system's DNS settings.
    pub address: String,
    pub port: u16,
}

impl std::fmt::Display for UdpTargetAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        // Use `self.number` to refer to each positional data point.
        write!(f, "{}:{}", self.address, self.port)
    }
}

#[derive(Debug, Clone)]
pub struct UdpTargetConfig {
    pub target_addresses: Vec<UdpTargetAddress>,
    pub allowed_ips: Vec<IpMask>,
    pub association_timeout_secs: Option<u32>,
}

pub async fn load_configs(
    config_paths: Vec<String>,
    config_urls: Vec<String>,
    is_initial: bool,
) -> Vec<ServerConfig> {
    let mut config_objects = vec![];
    for config_path in config_paths {
        let config_str = match tokio::fs::read_to_string(&config_path).await {
            Ok(s) => s,
            Err(e) => {
                println!("Failed to read {}, skipping: {}", config_path, e);
                if is_initial {
                    panic!("Initial load failed.");
                }
                continue;
            }
        };
        let config_str = config_str
            .split('\n')
            .filter(|line| return !line.trim_start().starts_with("//"))
            .collect::<Vec<&str>>()
            .join("\n");
        let config_object = match json::parse(&config_str) {
            Ok(o) => o,
            Err(e) => {
                println!("Failed to read {}, invalid JSON: {}", config_path, e);
                if is_initial {
                    panic!("Initial load failed.");
                }
                continue;
            }
        };
        config_objects.push(config_object);
    }

    for config_url in config_urls {
        let config_object = convert_url_to_obj(&config_url).unwrap();
        config_objects.push(config_object);
    }

    let mut ip_groups = HashMap::new();
    for config_object in config_objects.iter_mut() {
        load_ip_groups(config_object, &mut ip_groups);
    }

    debug!("IP groups: {:#?}", ip_groups);

    let mut all_configs = vec![];
    for config_object in config_objects.into_iter() {
        let configs = load_config(config_object, &ip_groups);
        all_configs.extend(configs.into_iter())
    }

    all_configs
}

fn load_ip_groups(obj: &mut JsonValue, ip_groups: &mut HashMap<String, Vec<IpMask>>) {
    if !obj.has_key("ipGroups") {
        return;
    }

    let mut groups = match obj["ipGroups"].take() {
        JsonValue::Object(o) => o,
        _ => panic!("Invalid ipGroups object"),
    };

    for (group_name, group_val) in groups.iter_mut() {
        let ips = match group_val.take() {
            JsonValue::Array(v) => v,
            _ => panic!("Invalid ipGroups range array"),
        };
        let ip_strs = ips
            .into_iter()
            .map(|obj| {
                let ip_str = obj
                    .as_str()
                    .expect("Invalid ip group entry, expected string");
                lookup_ip_mask(ip_str, ip_groups)
            })
            .collect::<Vec<Vec<IpMask>>>()
            .concat();
        ip_groups.insert(group_name.to_string(), ip_strs);
    }
}

fn convert_ip_mask(mask_str: &str) -> IpMask {
    if mask_str == "all" {
        // Special string meaning all IPs should be accepted.
        return (Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 0);
    }

    let tokens: Vec<&str> = mask_str.split('/').collect();
    let (ip_str, mask_str) = if tokens.len() == 1 {
        (mask_str, "")
    } else if tokens.len() == 2 {
        (tokens[0], tokens[1])
    } else {
        panic!("Invalid ip mask: {}", mask_str);
    };

    match ip_str.parse::<Ipv6Addr>() {
        Ok(i) => {
            let masklen = if mask_str.len() == 0 {
                128
            } else {
                mask_str.parse().expect("Could not parse ipv6 mask")
            };
            (i, masklen)
        }
        Err(_) => match ip_str.parse::<Ipv4Addr>() {
            Ok(i) => {
                let masklen = if mask_str.len() == 0 {
                    128
                } else {
                    mask_str.parse::<u32>().expect("Could not parse ipv4 mask") + 96
                };
                (i.to_ipv6_mapped(), masklen)
            }
            Err(_) => {
                panic!("Invalid ip address: {}", ip_str);
            }
        },
    }
}

fn load_config(mut obj: JsonValue, ip_groups: &HashMap<String, Vec<IpMask>>) -> Vec<ServerConfig> {
    let server_objs = match obj {
        JsonValue::Array(v) => v,
        JsonValue::Object(_) => {
            if obj.has_key("servers") {
                match obj["servers"].take() {
                    JsonValue::Array(v) => v,
                    _ => {
                        panic!("Expected array value for servers.")
                    }
                }
            } else {
                vec![obj]
            }
        }
        _ => vec![obj],
    };

    server_objs
        .into_iter()
        .map(|server_obj| parse_server_object(server_obj, ip_groups))
        .collect()
}

fn parse_server_object(
    mut obj: JsonValue,
    ip_groups: &HashMap<String, Vec<IpMask>>,
) -> ServerConfig {
    let server_address = obj["bindAddress"]
        .take_string()
        .expect("No bind address")
        .to_socket_addrs()
        .expect("Invalid bind address")
        .next()
        .expect("Unable to resolve bind address");

    let use_iptables = is_true_value(&obj["iptables"], false);

    let target_value = take_existing(&mut obj, &["target", "targets", "to"]);

    let target_objs = match target_value {
        JsonValue::String(s) => {
            let mut json_obj = JsonValue::new_object();
            json_obj["address"] = s.into();
            vec![json_obj]
        }
        JsonValue::Object(_) => vec![target_value],
        JsonValue::Array(v) => v,
        _ => panic!("Invalid targets"),
    };

    let target_configs = match obj["protocol"].as_str().unwrap_or("tcp") {
        "tcp" => {
            let target_configs = target_objs
                .into_iter()
                .map(|target_obj| parse_tcp_target_object(target_obj, ip_groups))
                .collect();

            TargetConfigs::Tcp(target_configs)
        }
        "udp" => {
            let target_configs = target_objs
                .into_iter()
                .map(|target_obj| parse_udp_target_object(target_obj, ip_groups))
                .collect();
            TargetConfigs::Udp(target_configs)
        }
        unknown_protocol => {
            panic!("Unknown protocol value: {}", unknown_protocol);
        }
    };

    ServerConfig {
        server_address,
        use_iptables,
        target_configs,
    }
}

fn parse_tcp_target_object(
    mut obj: JsonValue,
    ip_groups: &HashMap<String, Vec<IpMask>>,
) -> TcpTargetConfig {
    let server_tls_config = parse_server_tls_object(obj["serverTls"].take());

    let target_addresses = if obj.has_key("address") {
        vec![parse_tcp_target_address(obj["address"].take())]
    } else {
        let addresses_objs = match obj["addresses"].take() {
            JsonValue::Array(v) => v,
            _ => panic!("Invalid target addresses"),
        };
        addresses_objs
            .into_iter()
            .map(parse_tcp_target_address)
            .collect()
    };

    if target_addresses.is_empty() {
        panic!("No target addresses specified.");
    }

    // support allowlist key for backwards compatibility
    let allowed_ips = parse_allowed_ips(&mut obj, ip_groups);

    let tcp_nodelay = match obj["tcp_nodelay"].take() {
        JsonValue::Boolean(b) => b,
        JsonValue::Null => false,
        invalid => panic!("Invalid tcp_nodelay value: {}", invalid),
    };

    TcpTargetConfig {
        allowed_ips,
        server_tls_config,
        target_addresses,
        tcp_nodelay,
    }
}

fn parse_allowed_ips(obj: &mut JsonValue, ip_groups: &HashMap<String, Vec<IpMask>>) -> Vec<IpMask> {
    let allowed_ips_obj = take_existing(obj, &["allowed_ips", "allowlist"]);

    let allowed_ip_strs = match allowed_ips_obj {
        JsonValue::String(s) => vec![s],
        JsonValue::Short(s) => vec![s.as_str().to_string()],
        JsonValue::Array(v) => v
            .into_iter()
            .map(|v| v.as_str().expect("Invalid allowed_ips entry").to_string())
            .collect::<Vec<_>>(),
        JsonValue::Null => vec!["all".to_string()],
        invalid => panic!("Invalid allowed_ips value: {}", invalid),
    };

    allowed_ip_strs
        .into_iter()
        .map(|s| lookup_ip_mask(&s, ip_groups))
        .collect::<Vec<Vec<IpMask>>>()
        .concat()
}

fn lookup_ip_mask(s: &str, ip_groups: &HashMap<String, Vec<IpMask>>) -> Vec<IpMask> {
    if s.starts_with("@") {
        match ip_groups.get(&s[1..]) {
            Some(ips) => ips.iter().map(|ip| ip.clone()).collect(),
            None => {
                panic!("No such IP group: {}", s)
            }
        }
    } else {
        vec![convert_ip_mask(s)]
    }
}

fn parse_server_tls_object(obj: JsonValue) -> Option<ServerTlsConfig> {
    match obj {
        JsonValue::Null => None,
        JsonValue::Object(mut o) => {
            let sni_hostnames = match o["sni_hostnames"].take() {
                JsonValue::String(s) => HashSet::from([s.as_str().into()]),
                JsonValue::Short(s) => HashSet::from([s.as_str().into()]),
                JsonValue::Array(v) => {
                    let mut hostnames = v
                        .into_iter()
                        .map(|v| v.as_str().expect("Invalid sni_hostnames entry").into())
                        .collect::<HashSet<_>>();
                    if hostnames.is_empty() {
                        hostnames.extend([TlsOption::None, TlsOption::Any]);
                    }
                    hostnames
                }
                JsonValue::Null => HashSet::from([TlsOption::None, TlsOption::Any]),
                invalid => panic!("Invalid sni_hostnames value: {}", invalid),
            };

            let cert_path = o["cert"].take_string().expect("No cert path");
            let key_path = o["key"].take_string().expect("No key path");
            let optional = is_true_value(&o["optional"], false);

            let alpn_protocols = match o["alpn_protocols"].take() {
                JsonValue::String(s) => HashSet::from([s.as_str().into()]),
                JsonValue::Short(s) => HashSet::from([s.as_str().into()]),
                JsonValue::Array(v) => {
                    let mut hostnames = v
                        .into_iter()
                        .map(|v| v.as_str().expect("Invalid alpn_protocols entry").into())
                        .collect::<HashSet<_>>();
                    if hostnames.is_empty() {
                        hostnames.extend([TlsOption::None, TlsOption::Any]);
                    }
                    hostnames
                }
                JsonValue::Null => HashSet::from([TlsOption::None, TlsOption::Any]),
                invalid => panic!("Invalid alpn_protocols value: {}", invalid),
            };

            Some(ServerTlsConfig {
                sni_hostnames,
                alpn_protocols,
                cert_path,
                key_path,
                optional,
            })
        }
        _ => {
            panic!("Unknown server TLS config");
        }
    }
}

fn parse_client_tls_object(obj: JsonValue) -> Option<ClientTlsConfig> {
    match obj {
        JsonValue::Null => None,
        JsonValue::Object(mut o) => {
            let verify = o
                .remove("verify")
                .map(|v| is_true_value(&v, true))
                .unwrap_or(true);
            Some(ClientTlsConfig { verify })
        }
        _ => {
            if is_true_value(&obj, false) {
                Some(ClientTlsConfig { verify: true })
            } else {
                None
            }
        }
    }
}

fn parse_tcp_target_address(mut obj: JsonValue) -> TcpTargetAddress {
    if obj.is_object() {
        TcpTargetAddress {
            address: obj["address"]
                .take_string()
                .expect("Target address is not a string"),
            port: obj["port"]
                .as_u16()
                .expect("Target port is not a valid number"),
            client_tls_config: parse_client_tls_object(obj["tls"].take()),
        }
    } else if obj.is_string() {
        let mut s = obj.take_string().unwrap();
        let i = s.rfind(':').expect("No port separator in address string");
        let mut suffix_str = s.split_off(i + 1);
        // Remove the colon.
        s.pop();

        let (mut port_str, query_str) = match suffix_str.find("/?") {
            Some(i) => {
                let query_str = suffix_str.split_off(i + 2);
                // Remove the question mark and the slash.
                suffix_str.pop();
                suffix_str.pop();
                (suffix_str, query_str)
            }
            None => (suffix_str, String::new()),
        };

        let (port_str, tls) = if port_str.starts_with("+") {
            (port_str.split_off(1), true)
        } else {
            (port_str, false)
        };

        TcpTargetAddress {
            address: s,
            port: port_str.parse().expect("Invalid target port"),
            client_tls_config: if tls {
                Some(ClientTlsConfig {
                    verify: !query_str.find("verify=false").is_some(),
                })
            } else {
                None
            },
        }
    } else {
        panic!("Invalid target address: {}", obj);
    }
}

fn parse_udp_target_object(
    mut obj: JsonValue,
    ip_groups: &HashMap<String, Vec<IpMask>>,
) -> UdpTargetConfig {
    let target_addresses = if obj.has_key("address") {
        vec![parse_udp_target_address(obj["address"].take())]
    } else {
        let addresses_objs = match obj["addresses"].take() {
            JsonValue::Array(v) => v,
            _ => panic!("Invalid target addresses"),
        };
        addresses_objs
            .into_iter()
            .map(parse_udp_target_address)
            .collect()
    };

    if target_addresses.is_empty() {
        panic!("No target addresses specified.");
    }

    let allowed_ips = parse_allowed_ips(&mut obj, ip_groups);

    let association_timeout_secs = obj["association_timeout_secs"].as_u32();

    UdpTargetConfig {
        target_addresses,
        allowed_ips,
        association_timeout_secs,
    }
}

fn parse_udp_target_address(mut obj: JsonValue) -> UdpTargetAddress {
    if obj.is_object() {
        UdpTargetAddress {
            address: obj["address"]
                .take_string()
                .expect("Target address is not a string"),
            port: obj["port"]
                .as_u16()
                .expect("Target port is not a valid number"),
        }
    } else if obj.is_string() {
        let mut s = obj.take_string().unwrap();
        let i = s.rfind(':').expect("No port separator in address string");
        let port_str = s.split_off(i + 1);
        // Remove the colon.
        s.pop();

        UdpTargetAddress {
            address: s,
            port: port_str.parse().expect("Invalid target port"),
        }
    } else {
        panic!("Invalid target address: {}", obj);
    }
}

fn is_true_value(value: &JsonValue, default_value: bool) -> bool {
    if value.is_string() {
        let value_str = value.as_str().unwrap();
        return value_str == "true" || value_str == "1" || value_str == "yes";
    } else if value.is_number() {
        let value = value.as_i32().unwrap();
        return value == 1;
    }
    value.as_bool().unwrap_or(default_value)
}

fn convert_url_to_obj(url_str: &str) -> std::result::Result<JsonValue, String> {
    let url = Url::parse(url_str).map_err(|e| format!("Failed to parse url: {}", e))?;

    let mut json_obj = JsonValue::new_object();

    let protocol = url.scheme();

    json_obj.insert("protocol", protocol.to_string()).unwrap();

    let host_str = match url.host_str() {
        Some(s) => s.to_string(),
        None => {
            return Err(format!("URL missing host: {}", url_str));
        }
    };

    let address = match url.port() {
        Some(port) => format!("{}:{}", host_str, port),
        None => host_str,
    };

    json_obj.insert("bindAddress", address).unwrap();

    for (query_key, query_value) in url.query_pairs().into_owned() {
        let new_value = JsonValue::String(
            percent_decode_str(&query_value)
                .decode_utf8()
                .unwrap()
                .into_owned(),
        );

        let mut key_parts = query_key.split('.').collect::<Vec<_>>();
        let final_part = key_parts.pop().unwrap();

        let mut current_obj = &mut json_obj;
        for key_part in key_parts.into_iter() {
            if !current_obj.has_key(&key_part) {
                current_obj[key_part] = JsonValue::new_object();
            }
            current_obj = &mut current_obj[key_part];
        }

        if current_obj.has_key(final_part) {
            let existing_value = &mut current_obj[final_part];
            if existing_value.is_array() {
                existing_value.push(new_value).unwrap();
            } else {
                let existing_value = current_obj.remove(final_part);
                current_obj
                    .insert(final_part, vec![existing_value, new_value])
                    .unwrap();
            }
        } else {
            current_obj.insert(final_part, new_value).unwrap();
        }
    }

    Ok(json_obj)
}

fn take_existing(obj: &mut JsonValue, keys: &[&str]) -> JsonValue {
    for key in keys {
        if obj.has_key(*key) {
            return obj[*key].take();
        }
    }
    JsonValue::Null
}