summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 17a20beeea87183d4fde7da1688f37da7cf7247b (plain)
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
use clap::Parser;
use tokio::net::UdpSocket;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// The address and port to listen and accept cleartext UDP datagrams from
    #[arg(short, long)]
    bind: String,

    /// The local endpoint to read cleartext datagrams from & write to
    #[arg(short, long)]
    local: String,

    /// The destination to send obfuscated UDP datagrams to
    #[arg(short, long)]
    sendto: String,

    /// The hex representation of XOR key to use
    #[arg(short, long)]
    key: String,
}

#[tokio::main]
async fn main() {
    let args = Args::parse();
    let key = hex::decode(args.key)
        .expect("failed to decode key hex string");
    let sock = UdpSocket::bind(&args.bind).await
        .expect("failed to bind UDP endpoint");
    let mut buf = [0; 65536];
    let local = args.local.parse().expect("failed to parse local address");
    loop {
        if let Ok((len, addr)) = sock.recv_from(&mut buf).await {
            let mut i = 0;
            let n = key.len();
            for j in 0..len {
                buf[j] ^= key[i];
                i += 1;
                if i == n {
                    i = 0;
                }
            }
            let target = if addr == local {
                // packet comes from local endpoint
                // send to target
                &args.sendto
            } else {
                // packets coming from target
                // send to local
                &args.local
            };
            let _ = sock.send_to(&buf[..len], target).await;
        }
    }
}