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
|
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};
use std::time::{Duration, SystemTime, SystemTimeError};
use clap::{arg, Parser};
use tokio::net::UdpSocket;
use tracing::{debug, error};
use crate::Destination::{Address, SockAddr};
const TTL: Duration = Duration::from_secs(300);
/// 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() {
tracing_subscriber::fmt::init();
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);
}
let mut addr_sendto: Option<SocketAddr> = None;
let mut addr_sendto_t = SystemTime::UNIX_EPOCH;
loop {
match sock.recv_from(&mut buf).await {
Ok((len, addr)) => {
debug!("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) => {
let now = SystemTime::now();
if now.duration_since(addr_sendto_t)
.or::<SystemTimeError>(Ok(Duration::from_secs(0))).unwrap() > TTL {
// update
debug!("resolving address: {}", &addr);
match resolve_addr(&addr) {
Ok(addr) => {
addr_sendto = Some(addr);
addr_sendto_t = SystemTime::now();
SockAddr(addr)
}
Err(why) => {
error!("Failed to resolve address {}: {}", addr, why);
Destination::None
}
}
} else {
// cache is still valid
match addr_sendto {
None => Destination::None,
Some(addr) => SockAddr(addr),
}
}
}
SockAddr(addr) => SockAddr(addr),
};
let target = match sendto {
Destination::None => None,
Address(_) => panic!("unresolved address"),
SockAddr(sendto_addr) => {
if addr == sendto_addr {
// remote -> local
Some(local)
} else {
Some(sendto_addr)
}
}
};
match target {
None => {
error!("cannot send_to: destination not determined");
continue;
}
Some(target) => {
if let Err(why) = sock.send_to(&buf[..len], target).await {
debug!("send_to failed, dest: {}, error: {}", target, why);
} else {
debug!("send_to OK, len: {}, dest: {}", len, target);
}
}
}
}
Err(why) => {
debug!("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),
}
}
|