forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_interface.rs
1521 lines (1380 loc) · 44.8 KB
/
ui_interface.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#[cfg(any(target_os = "android", target_os = "ios"))]
use hbb_common::password_security;
use hbb_common::{
allow_err,
bytes::Bytes,
config::{
self, keys::*, option2bool, Config, LocalConfig, PeerConfig, CONNECT_TIMEOUT,
RENDEZVOUS_PORT,
},
directories_next,
futures::future::join_all,
log,
rendezvous_proto::*,
tokio,
};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::{
sleep,
tokio::{sync::mpsc, time},
};
use serde_derive::Serialize;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use std::process::Child;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use crate::common::SOFTWARE_UPDATE_URL;
#[cfg(feature = "flutter")]
use crate::hbbs_http::account;
#[cfg(not(any(target_os = "ios")))]
use crate::ipc;
type Message = RendezvousMessage;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub type Children = Arc<Mutex<(bool, HashMap<(String, String), Child>)>>;
#[derive(Clone, Debug, Serialize)]
pub struct UiStatus {
pub status_num: i32,
#[cfg(not(feature = "flutter"))]
pub key_confirmed: bool,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub mouse_time: i64,
#[cfg(not(feature = "flutter"))]
pub id: String,
#[cfg(feature = "flutter")]
pub video_conn_count: usize,
}
#[derive(Debug, Clone, Serialize)]
pub struct LoginDeviceInfo {
pub os: String,
pub r#type: String,
pub name: String,
}
lazy_static::lazy_static! {
static ref UI_STATUS : Arc<Mutex<UiStatus>> = Arc::new(Mutex::new(UiStatus{
status_num: 0,
#[cfg(not(feature = "flutter"))]
key_confirmed: false,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
mouse_time: 0,
#[cfg(not(feature = "flutter"))]
id: "".to_owned(),
#[cfg(feature = "flutter")]
video_conn_count: 0,
}));
static ref ASYNC_JOB_STATUS : Arc<Mutex<String>> = Default::default();
static ref ASYNC_HTTP_STATUS : Arc<Mutex<HashMap<String, String>>> = Arc::new(Mutex::new(HashMap::new()));
static ref TEMPORARY_PASSWD : Arc<Mutex<String>> = Arc::new(Mutex::new("".to_owned()));
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
lazy_static::lazy_static! {
static ref OPTION_SYNCED: Arc<Mutex<bool>> = Default::default();
static ref OPTIONS : Arc<Mutex<HashMap<String, String>>> = Arc::new(Mutex::new(Config::get_options()));
pub static ref SENDER : Mutex<mpsc::UnboundedSender<ipc::Data>> = Mutex::new(check_connect_status(true));
static ref CHILDREN : Children = Default::default();
}
const INIT_ASYNC_JOB_STATUS: &str = " ";
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn get_id() -> String {
#[cfg(any(target_os = "android", target_os = "ios"))]
return Config::get_id();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return ipc::get_id();
}
#[inline]
pub fn goto_install() {
allow_err!(crate::run_me(vec!["--install"]));
std::process::exit(0);
}
#[inline]
pub fn install_me(_options: String, _path: String, _silent: bool, _debug: bool) {
#[cfg(windows)]
std::thread::spawn(move || {
allow_err!(crate::platform::windows::install_me(
&_options, _path, _silent, _debug
));
std::process::exit(0);
});
}
#[inline]
pub fn update_me(_path: String) {
goto_install();
}
#[inline]
pub fn run_without_install() {
crate::run_me(vec!["--noinstall"]).ok();
std::process::exit(0);
}
#[inline]
pub fn show_run_without_install() -> bool {
let mut it = std::env::args();
if let Some(tmp) = it.next() {
if crate::is_setup(&tmp) {
return it.next() == None;
}
}
false
}
#[inline]
pub fn get_license() -> String {
#[cfg(windows)]
if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() {
#[cfg(feature = "flutter")]
return format!("Key: {}\nHost: {}\nAPI: {}", lic.key, lic.host, lic.api);
// default license format is html formed (sciter)
#[cfg(not(feature = "flutter"))]
return format!(
"<br /> Key: {} <br /> Host: {} API: {}",
lic.key, lic.host, lic.api
);
}
Default::default()
}
#[inline]
pub fn refresh_options() {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
*OPTIONS.lock().unwrap() = Config::get_options();
}
}
#[inline]
pub fn get_option<T: AsRef<str>>(key: T) -> String {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
let map = OPTIONS.lock().unwrap();
if let Some(v) = map.get(key.as_ref()) {
v.to_owned()
} else {
"".to_owned()
}
}
#[cfg(any(target_os = "android", target_os = "ios"))]
{
Config::get_option(key.as_ref())
}
}
#[inline]
pub fn use_texture_render() -> bool {
#[cfg(target_os = "android")]
return false;
#[cfg(target_os = "ios")]
return false;
#[cfg(target_os = "macos")]
return cfg!(feature = "flutter")
&& LocalConfig::get_option(config::keys::OPTION_TEXTURE_RENDER) == "Y";
#[cfg(target_os = "linux")]
return cfg!(feature = "flutter")
&& LocalConfig::get_option(config::keys::OPTION_TEXTURE_RENDER) != "N";
#[cfg(target_os = "windows")]
{
if !cfg!(feature = "flutter") {
return false;
}
// https://learn.microsoft.com/en-us/windows/win32/sysinfo/targeting-your-application-at-windows-8-1
#[cfg(debug_assertions)]
let default_texture = true;
#[cfg(not(debug_assertions))]
let default_texture = crate::platform::is_win_10_or_greater();
if default_texture {
LocalConfig::get_option(config::keys::OPTION_TEXTURE_RENDER) != "N"
} else {
return LocalConfig::get_option(config::keys::OPTION_TEXTURE_RENDER) == "Y";
}
}
}
#[inline]
pub fn get_local_option(key: String) -> String {
LocalConfig::get_option(&key)
}
#[inline]
#[cfg(feature = "flutter")]
pub fn get_hard_option(key: String) -> String {
config::HARD_SETTINGS
.read()
.unwrap()
.get(&key)
.cloned()
.unwrap_or_default()
}
#[inline]
pub fn get_builtin_option(key: &str) -> String {
crate::get_builtin_option(key)
}
#[inline]
pub fn set_local_option(key: String, value: String) {
LocalConfig::set_option(key.clone(), value.clone());
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn get_local_flutter_option(key: String) -> String {
LocalConfig::get_flutter_option(&key)
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn set_local_flutter_option(key: String, value: String) {
LocalConfig::set_flutter_option(key, value);
}
#[cfg(feature = "flutter")]
#[inline]
pub fn get_kb_layout_type() -> String {
LocalConfig::get_kb_layout_type()
}
#[cfg(feature = "flutter")]
#[inline]
pub fn set_kb_layout_type(kb_layout_type: String) {
LocalConfig::set_kb_layout_type(kb_layout_type);
}
#[inline]
pub fn peer_has_password(id: String) -> bool {
!PeerConfig::load(&id).password.is_empty()
}
#[inline]
pub fn forget_password(id: String) {
let mut c = PeerConfig::load(&id);
c.password.clear();
c.store(&id);
}
#[inline]
pub fn get_peer_option(id: String, name: String) -> String {
let c = PeerConfig::load(&id);
c.options.get(&name).unwrap_or(&"".to_owned()).to_owned()
}
#[inline]
#[cfg(feature = "flutter")]
pub fn get_peer_flutter_option(id: String, name: String) -> String {
let c = PeerConfig::load(&id);
c.ui_flutter.get(&name).unwrap_or(&"".to_owned()).to_owned()
}
#[inline]
#[cfg(feature = "flutter")]
pub fn set_peer_flutter_option(id: String, name: String, value: String) {
let mut c = PeerConfig::load(&id);
if value.is_empty() {
c.ui_flutter.remove(&name);
} else {
c.ui_flutter.insert(name, value);
}
c.store(&id);
}
#[inline]
pub fn set_peer_option(id: String, name: String, value: String) {
let mut c = PeerConfig::load(&id);
if value.is_empty() {
c.options.remove(&name);
} else {
c.options.insert(name, value);
}
c.store(&id);
}
#[inline]
pub fn get_options() -> String {
let options = {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
OPTIONS.lock().unwrap()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
{
Config::get_options()
}
};
let mut m = serde_json::Map::new();
for (k, v) in options.iter() {
m.insert(k.into(), v.to_owned().into());
}
serde_json::to_string(&m).unwrap_or_default()
}
#[inline]
pub fn test_if_valid_server(host: String, test_with_proxy: bool) -> String {
hbb_common::socket_client::test_if_valid_server(&host, test_with_proxy)
}
#[inline]
#[cfg(feature = "flutter")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn get_sound_inputs() -> Vec<String> {
let mut a = Vec::new();
#[cfg(not(target_os = "linux"))]
{
fn get_sound_inputs_() -> Vec<String> {
let mut out = Vec::new();
use cpal::traits::{DeviceTrait, HostTrait};
// Do not use `cpal::host_from_id(cpal::HostId::ScreenCaptureKit)` for feature = "screencapturekit"
// Because we explicitly handle the "System Sound" device.
let host = cpal::default_host();
if let Ok(devices) = host.devices() {
for device in devices {
if device.default_input_config().is_err() {
continue;
}
if let Ok(name) = device.name() {
out.push(name);
}
}
}
out
}
let inputs = Arc::new(Mutex::new(Vec::new()));
let cloned = inputs.clone();
// can not call below in UI thread, because conflict with sciter sound com initialization
std::thread::spawn(move || *cloned.lock().unwrap() = get_sound_inputs_())
.join()
.ok();
for name in inputs.lock().unwrap().drain(..) {
a.push(name);
}
}
#[cfg(target_os = "linux")]
{
let inputs: Vec<String> = crate::platform::linux::get_pa_sources()
.drain(..)
.map(|x| x.1)
.collect();
for name in inputs {
a.push(name);
}
}
a
}
#[inline]
pub fn set_options(m: HashMap<String, String>) {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
*OPTIONS.lock().unwrap() = m.clone();
ipc::set_options(m).ok();
}
#[cfg(any(target_os = "android", target_os = "ios"))]
Config::set_options(m);
}
#[inline]
pub fn set_option(key: String, value: String) {
if &key == "stop-service" {
#[cfg(target_os = "macos")]
{
let is_stop = value == "Y";
if is_stop && crate::platform::uninstall_service(true, false) {
return;
}
}
#[cfg(any(target_os = "windows", target_os = "linux"))]
{
if crate::platform::is_installed() {
if value == "Y" {
if crate::platform::uninstall_service(true, false) {
return;
}
} else {
if crate::platform::install_service() {
return;
}
}
return;
}
}
} else if &key == "audio-input" {
#[cfg(not(target_os = "ios"))]
crate::audio_service::restart();
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
let mut options = OPTIONS.lock().unwrap();
if value.is_empty() {
options.remove(&key);
} else {
options.insert(key.clone(), value.clone());
}
ipc::set_options(options.clone()).ok();
}
#[cfg(any(target_os = "android", target_os = "ios"))]
Config::set_option(key, value);
}
#[inline]
pub fn install_path() -> String {
#[cfg(windows)]
return crate::platform::windows::get_install_info().1;
#[cfg(not(windows))]
return "".to_owned();
}
#[inline]
pub fn install_options() -> String {
#[cfg(windows)]
return crate::platform::windows::get_install_options();
#[cfg(not(windows))]
return "{}".to_owned();
}
#[inline]
pub fn get_socks() -> Vec<String> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let s = ipc::get_socks();
#[cfg(target_os = "android")]
let s = Config::get_socks();
#[cfg(target_os = "ios")]
let s: Option<config::Socks5Server> = None;
match s {
None => Vec::new(),
Some(s) => {
let mut v = Vec::new();
v.push(s.proxy);
v.push(s.username);
v.push(s.password);
v
}
}
}
#[inline]
pub fn set_socks(proxy: String, username: String, password: String) {
let socks = config::Socks5Server {
proxy,
username,
password,
};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
ipc::set_socks(socks).ok();
#[cfg(target_os = "android")]
{
if socks.proxy.is_empty() {
Config::set_socks(None);
} else {
Config::set_socks(Some(socks));
}
crate::common::test_nat_type();
crate::RendezvousMediator::restart();
log::info!("socks updated");
}
}
#[inline]
#[cfg(feature = "flutter")]
pub fn get_proxy_status() -> bool {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return ipc::get_proxy_status();
// Currently, only the desktop version has proxy settings.
#[cfg(any(target_os = "android", target_os = "ios"))]
return false;
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[inline]
pub fn is_installed() -> bool {
crate::platform::is_installed()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
#[inline]
pub fn is_installed() -> bool {
false
}
#[inline]
pub fn is_share_rdp() -> bool {
#[cfg(windows)]
return crate::platform::windows::is_share_rdp();
#[cfg(not(windows))]
return false;
}
#[inline]
pub fn set_share_rdp(_enable: bool) {
#[cfg(windows)]
crate::platform::windows::set_share_rdp(_enable);
}
#[inline]
pub fn is_installed_lower_version() -> bool {
#[cfg(not(windows))]
return false;
#[cfg(windows)]
{
let b = crate::platform::windows::get_reg("BuildDate");
return crate::BUILD_DATE.cmp(&b).is_gt();
}
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn get_mouse_time() -> f64 {
UI_STATUS.lock().unwrap().mouse_time as f64
}
#[inline]
pub fn check_mouse_time() {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
let sender = SENDER.lock().unwrap();
allow_err!(sender.send(ipc::Data::MouseMoveTime(0)));
}
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn get_connect_status() -> UiStatus {
UI_STATUS.lock().unwrap().clone()
}
#[inline]
pub fn temporary_password() -> String {
#[cfg(any(target_os = "android", target_os = "ios"))]
return password_security::temporary_password();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return TEMPORARY_PASSWD.lock().unwrap().clone();
}
#[inline]
pub fn update_temporary_password() {
#[cfg(any(target_os = "android", target_os = "ios"))]
password_security::update_temporary_password();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
allow_err!(ipc::update_temporary_password());
}
#[inline]
pub fn permanent_password() -> String {
#[cfg(any(target_os = "android", target_os = "ios"))]
return Config::get_permanent_password();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return ipc::get_permanent_password();
}
#[inline]
pub fn set_permanent_password(password: String) {
#[cfg(any(target_os = "android", target_os = "ios"))]
Config::set_permanent_password(&password);
#[cfg(not(any(target_os = "android", target_os = "ios")))]
allow_err!(ipc::set_permanent_password(password));
}
#[inline]
pub fn get_peer(id: String) -> PeerConfig {
PeerConfig::load(&id)
}
#[inline]
pub fn get_fav() -> Vec<String> {
LocalConfig::get_fav()
}
#[inline]
pub fn store_fav(fav: Vec<String>) {
LocalConfig::set_fav(fav);
}
#[inline]
pub fn is_process_trusted(_prompt: bool) -> bool {
#[cfg(target_os = "macos")]
return crate::platform::macos::is_process_trusted(_prompt);
#[cfg(not(target_os = "macos"))]
return true;
}
#[inline]
pub fn is_can_screen_recording(_prompt: bool) -> bool {
#[cfg(target_os = "macos")]
return crate::platform::macos::is_can_screen_recording(_prompt);
#[cfg(not(target_os = "macos"))]
return true;
}
#[inline]
pub fn is_installed_daemon(_prompt: bool) -> bool {
#[cfg(target_os = "macos")]
return crate::platform::macos::is_installed_daemon(_prompt);
#[cfg(not(target_os = "macos"))]
return true;
}
#[inline]
#[cfg(feature = "flutter")]
pub fn is_can_input_monitoring(_prompt: bool) -> bool {
#[cfg(target_os = "macos")]
return crate::platform::macos::is_can_input_monitoring(_prompt);
#[cfg(not(target_os = "macos"))]
return true;
}
#[inline]
pub fn get_error() -> String {
#[cfg(not(any(feature = "cli")))]
#[cfg(target_os = "linux")]
{
let dtype = crate::platform::linux::get_display_server();
if crate::platform::linux::DISPLAY_SERVER_WAYLAND == dtype {
return crate::server::wayland::common_get_error();
}
if dtype != crate::platform::linux::DISPLAY_SERVER_X11 {
return format!(
"{} {}, {}",
crate::client::translate("Unsupported display server".to_owned()),
dtype,
crate::client::translate("x11 expected".to_owned()),
);
}
}
return "".to_owned();
}
#[inline]
pub fn is_login_wayland() -> bool {
#[cfg(target_os = "linux")]
return crate::platform::linux::is_login_wayland();
#[cfg(not(target_os = "linux"))]
return false;
}
#[inline]
pub fn current_is_wayland() -> bool {
#[cfg(target_os = "linux")]
return crate::platform::linux::current_is_wayland();
#[cfg(not(target_os = "linux"))]
return false;
}
#[inline]
pub fn get_new_version() -> String {
(*SOFTWARE_UPDATE_URL
.lock()
.unwrap()
.rsplit('/')
.next()
.unwrap_or(""))
.to_string()
}
#[inline]
pub fn get_version() -> String {
crate::VERSION.to_owned()
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn get_app_name() -> String {
crate::get_app_name()
}
#[cfg(windows)]
#[inline]
pub fn create_shortcut(_id: String) {
crate::platform::windows::create_shortcut(&_id).ok();
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn discover() {
std::thread::spawn(move || {
allow_err!(crate::lan::discover());
});
}
#[cfg(feature = "flutter")]
pub fn peer_to_map(id: String, p: PeerConfig) -> HashMap<&'static str, String> {
use hbb_common::sodiumoxide::base64;
HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
(
"hash",
base64::encode(p.password, base64::Variant::Original),
),
])
}
#[cfg(feature = "flutter")]
pub fn peer_exists(id: &str) -> bool {
PeerConfig::exists(id)
}
#[inline]
pub fn get_lan_peers() -> Vec<HashMap<&'static str, String>> {
config::LanPeers::load()
.peers
.iter()
.map(|peer| {
HashMap::<&str, String>::from_iter([
("id", peer.id.clone()),
("username", peer.username.clone()),
("hostname", peer.hostname.clone()),
("platform", peer.platform.clone()),
])
})
.collect()
}
#[inline]
pub fn remove_discovered(id: String) {
let mut peers = config::LanPeers::load().peers;
peers.retain(|x| x.id != id);
config::LanPeers::store(&peers);
}
#[inline]
pub fn get_uuid() -> String {
crate::encode64(hbb_common::get_uuid())
}
#[inline]
pub fn get_init_async_job_status() -> String {
INIT_ASYNC_JOB_STATUS.to_string()
}
#[inline]
pub fn reset_async_job_status() {
*ASYNC_JOB_STATUS.lock().unwrap() = get_init_async_job_status();
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn change_id(id: String) {
reset_async_job_status();
let old_id = get_id();
std::thread::spawn(move || {
change_id_shared(id, old_id);
});
}
#[inline]
pub fn http_request(url: String, method: String, body: Option<String>, header: String) {
// Respond to concurrent requests for resources
let current_request = ASYNC_HTTP_STATUS.clone();
current_request
.lock()
.unwrap()
.insert(url.clone(), " ".to_owned());
std::thread::spawn(move || {
let res = match crate::http_request_sync(url.clone(), method, body, header) {
Err(err) => {
log::error!("{}", err);
err.to_string()
}
Ok(text) => text,
};
current_request.lock().unwrap().insert(url, res);
});
}
#[inline]
pub fn get_async_http_status(url: String) -> Option<String> {
match ASYNC_HTTP_STATUS.lock().unwrap().get(&url) {
None => None,
Some(_str) => Some(_str.to_string()),
}
}
#[inline]
pub fn post_request(url: String, body: String, header: String) {
*ASYNC_JOB_STATUS.lock().unwrap() = " ".to_owned();
std::thread::spawn(move || {
*ASYNC_JOB_STATUS.lock().unwrap() = match crate::post_request_sync(url, body, &header) {
Err(err) => err.to_string(),
Ok(text) => text,
};
});
}
#[inline]
pub fn get_async_job_status() -> String {
ASYNC_JOB_STATUS.lock().unwrap().clone()
}
#[inline]
pub fn get_langs() -> String {
use serde_json::json;
let mut x: Vec<(&str, String)> = crate::lang::LANGS
.iter()
.map(|a| (a.0, format!("{} ({})", a.1, a.0)))
.collect();
x.sort_by(|a, b| a.0.cmp(b.0));
json!(x).to_string()
}
#[inline]
pub fn video_save_directory(root: bool) -> String {
let appname = crate::get_app_name();
// ui process can show it correctly Once vidoe process created it.
let try_create = |path: &std::path::Path| {
if !path.exists() {
std::fs::create_dir_all(path).ok();
}
if path.exists() {
path.to_string_lossy().to_string()
} else {
"".to_string()
}
};
if root {
// Currently, only installed windows run as root
#[cfg(windows)]
{
let drive = std::env::var("SystemDrive").unwrap_or("C:".to_owned());
let dir =
std::path::PathBuf::from(format!("{drive}\\ProgramData\\RustDesk\\recording",));
return dir.to_string_lossy().to_string();
}
}
// Get directory from config file otherwise --server will use the old value from global var.
#[cfg(any(target_os = "linux", target_os = "macos"))]
let dir = LocalConfig::get_option_from_file(OPTION_VIDEO_SAVE_DIRECTORY);
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
let dir = LocalConfig::get_option(OPTION_VIDEO_SAVE_DIRECTORY);
if !dir.is_empty() {
return dir;
}
#[cfg(any(target_os = "android", target_os = "ios"))]
if let Ok(home) = config::APP_HOME_DIR.read() {
let mut path = home.to_owned();
path.push_str("/RustDesk/ScreenRecord");
let dir = try_create(&std::path::Path::new(&path));
if !dir.is_empty() {
return dir;
}
}
if let Some(user) = directories_next::UserDirs::new() {
if let Some(video_dir) = user.video_dir() {
let dir = try_create(&video_dir.join(&appname));
if !dir.is_empty() {
return dir;
}
if video_dir.exists() {
return video_dir.to_string_lossy().to_string();
}
}
if let Some(desktop_dir) = user.desktop_dir() {
if desktop_dir.exists() {
return desktop_dir.to_string_lossy().to_string();
}
}
let home = user.home_dir();
if home.exists() {
return home.to_string_lossy().to_string();
}
}
// same order as above
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Some(home) = crate::platform::get_active_user_home() {
let name = if cfg!(target_os = "macos") {
"Movies"
} else {
"Videos"
};
let video_dir = home.join(name);
let dir = try_create(&video_dir.join(&appname));
if !dir.is_empty() {
return dir;
}
if video_dir.exists() {
return video_dir.to_string_lossy().to_string();
}
let desktop_dir = home.join("Desktop");
if desktop_dir.exists() {
return desktop_dir.to_string_lossy().to_string();
}
if home.exists() {
return home.to_string_lossy().to_string();
}
}
if let Ok(exe) = std::env::current_exe() {
if let Some(parent) = exe.parent() {
let dir = try_create(&parent.join("videos"));
if !dir.is_empty() {
return dir;
}
// basically exist
return parent.to_string_lossy().to_string();
}
}
Default::default()
}
#[inline]
pub fn get_api_server() -> String {
crate::get_api_server(
get_option("api-server"),
get_option("custom-rendezvous-server"),
)
}
#[inline]
pub fn has_hwcodec() -> bool {
// Has real hardware codec using gpu
(cfg!(feature = "hwcodec") && cfg!(not(target_os = "ios"))) || cfg!(feature = "mediacodec")
}
#[inline]
pub fn has_vram() -> bool {
cfg!(feature = "vram")
}
#[cfg(feature = "flutter")]
#[inline]
pub fn supported_hwdecodings() -> (bool, bool) {
let decoding =
scrap::codec::Decoder::supported_decodings(None, use_texture_render(), None, &vec![]);
#[allow(unused_mut)]
let (mut h264, mut h265) = (decoding.ability_h264 > 0, decoding.ability_h265 > 0);
#[cfg(feature = "vram")]
{
// supported_decodings check runtime luid
let vram = scrap::vram::VRamDecoder::possible_available_without_check();
if vram.0 {
h264 = true;
}
if vram.1 {
h265 = true;
}
}
(h264, h265)
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[inline]
pub fn is_root() -> bool {
crate::platform::is_root()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
#[inline]
pub fn is_root() -> bool {
false
}
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
#[inline]
pub fn check_super_user_permission() -> bool {
#[cfg(any(windows, target_os = "linux", target_os = "macos"))]
return crate::platform::check_super_user_permission().unwrap_or(false);