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
|
//! Glue between tunnel-obfuscation and WireGuard configurations
use super::{Error, Result};
use crate::{CloseMsg, config::Config};
#[cfg(target_os = "android")]
use std::sync::{Arc, Mutex};
use std::{
iter,
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
sync::mpsc as sync_mpsc,
};
#[cfg(target_os = "android")]
use talpid_tunnel::tun_provider::TunProvider;
use talpid_types::{
ErrorExt,
net::obfuscation::{ObfuscatorConfig, Obfuscators},
};
use tunnel_obfuscation::{
Settings as ObfuscationSettings, create_obfuscator, lwo, multiplexer, quic, shadowsocks,
udp2tcp,
};
/// Begin running obfuscation machine, if configured. This function will patch `config`'s endpoint
/// to point to an endpoint on localhost
///
/// # Arguments
///
/// * obfuscation_mtu - "MTU" including obfuscation overhead
pub async fn apply_obfuscation_config(
config: &mut Config,
obfuscation_mtu: u16,
close_msg_sender: sync_mpsc::Sender<CloseMsg>,
#[cfg(target_os = "android")] tun_provider: Arc<Mutex<TunProvider>>,
) -> Result<Option<ObfuscatorHandle>> {
let Some(ref obfuscator_config) = config.obfuscator_config else {
return Ok(None);
};
let settings = settings_from_config(
config,
obfuscator_config,
obfuscation_mtu,
#[cfg(target_os = "linux")]
config.fwmark,
);
log::trace!("Obfuscation settings: {settings:?}");
let obfuscator = create_obfuscator(&settings)
.await
.map_err(Error::ObfuscationError)?;
let packet_overhead = obfuscator.packet_overhead();
#[cfg(target_os = "android")]
bypass_vpn(tun_provider, obfuscator.remote_socket_fd()).await;
patch_endpoint(config, obfuscator.endpoint());
let obfuscation_task = tokio::spawn(async move {
match obfuscator.run().await {
Ok(_) => {
let _ = close_msg_sender.send(CloseMsg::ObfuscatorExpired);
}
Err(error) => {
log::error!(
"{}",
error.display_chain_with_msg("Obfuscation controller failed")
);
let _ = close_msg_sender
.send(CloseMsg::ObfuscatorFailed(Error::ObfuscationError(error)));
}
}
});
Ok(Some(ObfuscatorHandle {
obfuscation_task,
packet_overhead,
}))
}
/// Patch the first peer in the WireGuard configuration to use the local proxy endpoint
fn patch_endpoint(config: &mut Config, endpoint: SocketAddr) {
log::trace!("Patching first WireGuard peer to become {endpoint}");
config.entry_peer.endpoint = endpoint;
}
fn settings_from_config(
config: &Config,
obfuscation_config: &Obfuscators,
mtu: u16,
#[cfg(target_os = "linux")] fwmark: Option<u32>,
) -> ObfuscationSettings {
match obfuscation_config {
Obfuscators::Single(obfuscation_config) => settings_from_single_config(
config,
obfuscation_config,
mtu,
#[cfg(target_os = "linux")]
fwmark,
),
Obfuscators::Multiplexer {
direct,
configs: (first_obfs, remaining_obfs),
} => {
let mut transports = vec![];
if let Some(direct) = direct {
transports.push(multiplexer::Transport::Direct(*direct));
}
for obfs_config in iter::once(first_obfs).chain(remaining_obfs) {
let settings = settings_from_single_config(
config,
obfs_config,
mtu,
#[cfg(target_os = "linux")]
fwmark,
);
transports.push(multiplexer::Transport::Obfuscated(settings));
}
ObfuscationSettings::Multiplexer(multiplexer::Settings {
transports,
#[cfg(target_os = "linux")]
fwmark,
})
}
}
}
fn settings_from_single_config(
config: &Config,
obfuscation_config: &ObfuscatorConfig,
mtu: u16,
#[cfg(target_os = "linux")] fwmark: Option<u32>,
) -> ObfuscationSettings {
match obfuscation_config {
ObfuscatorConfig::Udp2Tcp { endpoint } => ObfuscationSettings::Udp2Tcp(udp2tcp::Settings {
peer: *endpoint,
#[cfg(target_os = "linux")]
fwmark,
}),
ObfuscatorConfig::Shadowsocks { endpoint } => {
ObfuscationSettings::Shadowsocks(shadowsocks::Settings {
shadowsocks_endpoint: *endpoint,
wireguard_endpoint: if endpoint.is_ipv4() {
SocketAddr::from((Ipv4Addr::LOCALHOST, 51820))
} else {
SocketAddr::from((Ipv6Addr::LOCALHOST, 51820))
},
#[cfg(target_os = "linux")]
fwmark,
})
}
ObfuscatorConfig::Quic {
hostname,
endpoint,
auth_token,
} => {
let wireguard_endpoint = SocketAddr::from((Ipv4Addr::LOCALHOST, 51820));
let settings = quic::Settings::new(
*endpoint,
hostname.to_owned(),
auth_token.parse().unwrap(),
wireguard_endpoint,
)
.mtu(mtu);
#[cfg(target_os = "linux")]
if let Some(fwmark) = fwmark {
return ObfuscationSettings::Quic(settings.fwmark(fwmark));
}
ObfuscationSettings::Quic(settings)
}
ObfuscatorConfig::Lwo { endpoint } => ObfuscationSettings::Lwo(lwo::Settings {
server_addr: *endpoint,
client_public_key: config.tunnel.private_key.public_key(),
server_public_key: config.entry_peer.public_key.clone(),
#[cfg(target_os = "linux")]
fwmark,
}),
}
}
/// Route socket outside of the VPN on Android
#[cfg(target_os = "android")]
async fn bypass_vpn(
tun_provider: Arc<Mutex<TunProvider>>,
remote_socket_fd: std::os::unix::io::RawFd,
) {
// Exclude remote obfuscation socket or bridge
log::debug!("Excluding remote socket fd from the tunnel");
let _ = tokio::task::spawn_blocking(move || {
if let Err(error) = tun_provider.lock().unwrap().bypass(&remote_socket_fd) {
log::error!("Failed to exclude remote socket fd: {error}");
}
})
.await;
}
/// Simple wrapper that automatically cancels the future which runs an obfuscator.
pub struct ObfuscatorHandle {
obfuscation_task: tokio::task::JoinHandle<()>,
packet_overhead: u16,
}
impl ObfuscatorHandle {
pub fn abort(&self) {
self.obfuscation_task.abort();
}
pub fn packet_overhead(&self) -> u16 {
self.packet_overhead
}
}
impl Drop for ObfuscatorHandle {
fn drop(&mut self) {
self.obfuscation_task.abort();
}
}
|