forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rs
452 lines (422 loc) · 13.7 KB
/
common.rs
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
pub use arboard::Clipboard as ClipboardContext;
use hbb_common::{
allow_err,
compress::{compress as compress_func, decompress},
config::{Config, COMPRESS_LEVEL, RENDEZVOUS_TIMEOUT},
log,
message_proto::*,
protobuf::Message as _,
protobuf::ProtobufEnum,
rendezvous_proto::*,
sleep,
tcp::FramedStream,
tokio, ResultType,
};
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
use hbb_common::{config::RENDEZVOUS_PORT, futures::future::join_all};
use std::sync::{Arc, Mutex};
pub const CLIPBOARD_NAME: &'static str = "clipboard";
pub const CLIPBOARD_INTERVAL: u64 = 333;
lazy_static::lazy_static! {
pub static ref CONTENT: Arc<Mutex<String>> = Default::default();
pub static ref SOFTWARE_UPDATE_URL: Arc<Mutex<String>> = Default::default();
}
#[cfg(any(target_os = "android", target_os = "ios"))]
lazy_static::lazy_static! {
pub static ref MOBILE_INFO1: Arc<Mutex<String>> = Default::default();
pub static ref MOBILE_INFO2: Arc<Mutex<String>> = Default::default();
}
#[inline]
pub fn valid_for_numlock(evt: &KeyEvent) -> bool {
if let Some(key_event::Union::control_key(ck)) = evt.union {
let v = ck.value();
(v >= ControlKey::Numpad0.value() && v <= ControlKey::Numpad9.value())
|| v == ControlKey::Decimal.value()
} else {
false
}
}
#[inline]
pub fn valid_for_capslock(evt: &KeyEvent) -> bool {
if let Some(key_event::Union::chr(ch)) = evt.union {
ch >= 'a' as u32 && ch <= 'z' as u32
} else {
false
}
}
pub fn create_clipboard_msg(content: String) -> Message {
let bytes = content.into_bytes();
let compressed = compress_func(&bytes, COMPRESS_LEVEL);
let compress = compressed.len() < bytes.len();
let content = if compress { compressed } else { bytes };
let mut msg = Message::new();
msg.set_clipboard(Clipboard {
compress,
content,
..Default::default()
});
msg
}
pub fn check_clipboard(
ctx: &mut ClipboardContext,
old: Option<&Arc<Mutex<String>>>,
) -> Option<Message> {
let side = if old.is_none() { "host" } else { "client" };
let old = if let Some(old) = old { old } else { &CONTENT };
if let Ok(content) = ctx.get_text() {
if content.len() < 2_000_000 && !content.is_empty() {
let changed = content != *old.lock().unwrap();
if changed {
log::info!("{} update found on {}", CLIPBOARD_NAME, side);
*old.lock().unwrap() = content.clone();
return Some(create_clipboard_msg(content));
}
}
}
None
}
pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc<Mutex<String>>>) {
let content = if clipboard.compress {
decompress(&clipboard.content)
} else {
clipboard.content
};
if let Ok(content) = String::from_utf8(content) {
match ClipboardContext::new() {
Ok(mut ctx) => {
let side = if old.is_none() { "host" } else { "client" };
let old = if let Some(old) = old { old } else { &CONTENT };
*old.lock().unwrap() = content.clone();
allow_err!(ctx.set_text(content));
log::debug!("{} updated on {}", CLIPBOARD_NAME, side);
}
Err(err) => {
log::error!("Failed to create clipboard context: {}", err);
}
}
}
}
#[cfg(feature = "use_rubato")]
pub fn resample_channels(
data: &[f32],
sample_rate0: u32,
sample_rate: u32,
channels: u16,
) -> Vec<f32> {
use rubato::{
InterpolationParameters, InterpolationType, Resampler, SincFixedIn, WindowFunction,
};
let params = InterpolationParameters {
sinc_len: 256,
f_cutoff: 0.95,
interpolation: InterpolationType::Nearest,
oversampling_factor: 160,
window: WindowFunction::BlackmanHarris2,
};
let mut resampler = SincFixedIn::<f64>::new(
sample_rate as f64 / sample_rate0 as f64,
params,
data.len() / (channels as usize),
channels as _,
);
let mut waves_in = Vec::new();
if channels == 2 {
waves_in.push(
data.iter()
.step_by(2)
.map(|x| *x as f64)
.collect::<Vec<_>>(),
);
waves_in.push(
data.iter()
.skip(1)
.step_by(2)
.map(|x| *x as f64)
.collect::<Vec<_>>(),
);
} else {
waves_in.push(data.iter().map(|x| *x as f64).collect::<Vec<_>>());
}
if let Ok(x) = resampler.process(&waves_in) {
if x.is_empty() {
Vec::new()
} else if x.len() == 2 {
x[0].chunks(1)
.zip(x[1].chunks(1))
.flat_map(|(a, b)| a.into_iter().chain(b))
.map(|x| *x as f32)
.collect()
} else {
x[0].iter().map(|x| *x as f32).collect()
}
} else {
Vec::new()
}
}
#[cfg(feature = "use_dasp")]
pub fn resample_channels(
data: &[f32],
sample_rate0: u32,
sample_rate: u32,
channels: u16,
) -> Vec<f32> {
use dasp::{interpolate::linear::Linear, signal, Signal};
let n = data.len() / (channels as usize);
let n = n * sample_rate as usize / sample_rate0 as usize;
if channels == 2 {
let mut source = signal::from_interleaved_samples_iter::<_, [_; 2]>(data.iter().cloned());
let a = source.next();
let b = source.next();
let interp = Linear::new(a, b);
let mut data = Vec::with_capacity(n << 1);
for x in source
.from_hz_to_hz(interp, sample_rate0 as _, sample_rate as _)
.take(n)
{
data.push(x[0]);
data.push(x[1]);
}
data
} else {
let mut source = signal::from_iter(data.iter().cloned());
let a = source.next();
let b = source.next();
let interp = Linear::new(a, b);
source
.from_hz_to_hz(interp, sample_rate0 as _, sample_rate as _)
.take(n)
.collect()
}
}
#[cfg(feature = "use_samplerate")]
pub fn resample_channels(
data: &[f32],
sample_rate0: u32,
sample_rate: u32,
channels: u16,
) -> Vec<f32> {
use samplerate::{convert, ConverterType};
convert(
sample_rate0 as _,
sample_rate as _,
channels as _,
ConverterType::SincBestQuality,
data,
)
.unwrap_or_default()
}
pub fn test_nat_type() {
std::thread::spawn(move || loop {
match test_nat_type_() {
Ok(true) => break,
Err(err) => {
log::error!("test nat: {}", err);
}
_ => {}
}
if Config::get_nat_type() != 0 {
break;
}
std::thread::sleep(std::time::Duration::from_secs(12));
});
}
#[tokio::main(flavor = "current_thread")]
async fn test_nat_type_() -> ResultType<bool> {
log::info!("Testing nat ...");
let start = std::time::Instant::now();
let rendezvous_server = get_rendezvous_server(100).await;
let server1 = rendezvous_server;
let mut server2 = server1;
server2.set_port(server1.port() - 1);
let mut msg_out = RendezvousMessage::new();
let serial = Config::get_serial();
msg_out.set_test_nat_request(TestNatRequest {
serial,
..Default::default()
});
let mut port1 = 0;
let mut port2 = 0;
let mut addr = Config::get_any_listen_addr();
for i in 0..2 {
let mut socket = FramedStream::new(
if i == 0 { &server1 } else { &server2 },
addr,
RENDEZVOUS_TIMEOUT,
)
.await?;
addr = socket.get_ref().local_addr()?;
socket.send(&msg_out).await?;
if let Some(Ok(bytes)) = socket.next_timeout(3000).await {
if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
if let Some(rendezvous_message::Union::test_nat_response(tnr)) = msg_in.union {
if i == 0 {
port1 = tnr.port;
} else {
port2 = tnr.port;
}
if let Some(cu) = tnr.cu.as_ref() {
Config::set_option(
"rendezvous-servers".to_owned(),
cu.rendezvous_servers.join(","),
);
Config::set_serial(cu.serial);
}
}
}
} else {
break;
}
}
let ok = port1 > 0 && port2 > 0;
if ok {
let t = if port1 == port2 {
NatType::ASYMMETRIC
} else {
NatType::SYMMETRIC
};
Config::set_nat_type(t as _);
log::info!("Tested nat type: {:?} in {:?}", t, start.elapsed());
}
Ok(ok)
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub async fn get_rendezvous_server(_ms_timeout: u64) -> std::net::SocketAddr {
Config::get_rendezvous_server()
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub async fn get_rendezvous_server(ms_timeout: u64) -> std::net::SocketAddr {
crate::ipc::get_rendezvous_server(ms_timeout).await
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub async fn get_nat_type(_ms_timeout: u64) -> i32 {
Config::get_nat_type()
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub async fn get_nat_type(ms_timeout: u64) -> i32 {
crate::ipc::get_nat_type(ms_timeout).await
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
#[tokio::main(flavor = "current_thread")]
async fn test_rendezvous_server_() {
let servers = Config::get_rendezvous_servers();
hbb_common::config::ONLINE.lock().unwrap().clear();
let mut futs = Vec::new();
for host in servers {
futs.push(tokio::spawn(async move {
let tm = std::time::Instant::now();
if FramedStream::new(
&crate::check_port(&host, RENDEZVOUS_PORT),
Config::get_any_listen_addr(),
RENDEZVOUS_TIMEOUT,
)
.await
.is_ok()
{
let elapsed = tm.elapsed().as_micros();
Config::update_latency(&host, elapsed as _);
} else {
Config::update_latency(&host, -1);
}
}));
}
join_all(futs).await;
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
pub fn test_rendezvous_server() {
std::thread::spawn(test_rendezvous_server_);
}
#[inline]
pub fn get_time() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or(0) as _
}
pub fn run_me<T: AsRef<std::ffi::OsStr>>(args: Vec<T>) -> std::io::Result<std::process::Child> {
let cmd = std::env::current_exe()?;
return std::process::Command::new(cmd).args(&args).spawn();
}
pub fn username() -> String {
// fix bug of whoami
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return whoami::username().trim_end_matches('\0').to_owned();
#[cfg(any(target_os = "android", target_os = "ios"))]
return MOBILE_INFO2.lock().unwrap().clone();
}
#[inline]
pub fn check_port<T: std::string::ToString>(host: T, port: i32) -> String {
let host = host.to_string();
if !host.contains(":") {
return format!("{}:{}", host, port);
}
return host;
}
pub const POSTFIX_SERVICE: &'static str = "_service";
#[inline]
pub fn is_control_key(evt: &KeyEvent, key: &ControlKey) -> bool {
if let Some(key_event::Union::control_key(ck)) = evt.union {
ck.value() == key.value()
} else {
false
}
}
#[inline]
pub fn is_modifier(evt: &KeyEvent) -> bool {
if let Some(key_event::Union::control_key(ck)) = evt.union {
let v = ck.value();
v == ControlKey::Alt.value()
|| v == ControlKey::Shift.value()
|| v == ControlKey::Control.value()
|| v == ControlKey::Meta.value()
|| v == ControlKey::RAlt.value()
|| v == ControlKey::RShift.value()
|| v == ControlKey::RControl.value()
|| v == ControlKey::RWin.value()
} else {
false
}
}
pub fn test_if_valid_server(host: String) -> String {
let mut host = host;
if !host.contains(":") {
host = format!("{}:{}", host, 0);
}
match hbb_common::to_socket_addr(&host) {
Err(err) => err.to_string(),
Ok(_) => "".to_owned(),
}
}
pub fn get_version_number(v: &str) -> i64 {
let mut n = 0;
for x in v.split(".") {
n = n * 1000 + x.parse::<i64>().unwrap_or(0);
}
n
}
pub fn check_software_update() {
std::thread::spawn(move || allow_err!(_check_software_update()));
}
#[tokio::main(flavor = "current_thread")]
async fn _check_software_update() -> hbb_common::ResultType<()> {
sleep(3.).await;
let rendezvous_server = get_rendezvous_server(1_000).await;
let mut socket = hbb_common::udp::FramedSocket::new(Config::get_any_listen_addr()).await?;
let mut msg_out = RendezvousMessage::new();
msg_out.set_software_update(SoftwareUpdate {
url: crate::VERSION.to_owned(),
..Default::default()
});
socket.send(&msg_out, rendezvous_server).await?;
use hbb_common::protobuf::Message;
if let Some(Ok((bytes, _))) = socket.next_timeout(30_000).await {
if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
if let Some(rendezvous_message::Union::software_update(su)) = msg_in.union {
let version = hbb_common::get_version_from_url(&su.url);
if get_version_number(&version) > get_version_number(crate::VERSION) {
*SOFTWARE_UPDATE_URL.lock().unwrap() = su.url;
}
}
}
}
Ok(())
}