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
|
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};
use clap::{arg, Parser};
use tokio::net::UdpSocket;
use crate::Destination::{Address, SockAddr};
/// 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: Option<String>,
/// The hex representation of XOR key to use
#[arg(short, long)]
key: String,
}
#[derive(Debug, Eq, PartialEq)]
enum Destination<'a> {
None,
Address(&'a String),
SockAddr(SocketAddr),
}
#[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");
let mut sendto = Destination::None;
let args_sendto: String;
if let Some(s) = args.sendto {
args_sendto = s;
sendto = Address(&args_sendto);
}
loop {
match sock.recv_from(&mut buf).await {
Ok((len, addr)) => {
eprintln!("recv_from OK, len {}, addr {}", len, addr);
let mut i = 0;
let n = key.len();
for j in 0..len {
buf[j] ^= key[i];
i += 1;
if i == n {
i = 0;
}
}
if sendto == Destination::None && addr != local {
// assume the packet is coming from target
// update missing sendto parameter
sendto = SockAddr(addr);
}
let sendto = match sendto {
Destination::None => Destination::None,
Address(addr) => match resolve_addr(&addr) {
Ok(addr) => SockAddr(addr),
Err(why) => {
eprintln!("Failed to resolve address {}: {}", addr, why);
Destination::None
}
},
SockAddr(addr) => SockAddr(addr),
};
let target = if let SockAddr(sendto_addr) = sendto {
if addr == sendto_addr {
// remote -> local
Some(local)
} else {
Some(sendto_addr)
}
} else {
None
};
match target {
None => {
eprintln!("cannot send_to: destination not determined");
continue;
}
Some(target) => {
if let Err(why) = sock.send_to(&buf[..len], target).await {
eprintln!("send_to failed, dest: {}, error: {}", target, why);
} else {
eprintln!("send_to OK, len: {}, dest: {}", len, target);
}
}
}
}
Err(why) => {
eprintln!("recv_from failed: {}", why);
}
}
}
}
fn resolve_addr(addr: &String) -> io::Result<SocketAddr> {
match addr.to_socket_addrs() {
Ok(mut addrs) => match addrs.next() {
None => Err(io::Error::new(io::ErrorKind::Other, "no available address")),
Some(addr) => Ok(addr),
},
Err(why) => Err(why),
}
}
|