Просмотр исходного кода

Fix: clippy warning in rust 1.66.0

Bo Zhang лет назад: 3
Родитель
Сommit
60a6d672c5

+ 1 - 1
libs/hbb_common/build.rs

@@ -3,7 +3,7 @@ fn main() {
3
     protobuf_codegen::Codegen::new()
3
     protobuf_codegen::Codegen::new()
4
         .pure()
4
         .pure()
5
         .out_dir("src/protos")
5
         .out_dir("src/protos")
6
-        .inputs(&["protos/rendezvous.proto", "protos/message.proto"])
6
+        .inputs(["protos/rendezvous.proto", "protos/message.proto"])
7
         .include("protos")
7
         .include("protos")
8
         .customize(protobuf_codegen::Customize::default().tokio_bytes(true))
8
         .customize(protobuf_codegen::Customize::default().tokio_bytes(true))
9
         .run()
9
         .run()

+ 7 - 1
libs/hbb_common/src/bytes_codec.rs

@@ -15,6 +15,12 @@ enum DecodeState {
15
     Data(usize),
15
     Data(usize),
16
 }
16
 }
17
 
17
 
18
+impl Default for BytesCodec {
19
+    fn default() -> Self {
20
+        Self::new()
21
+    }
22
+}
23
+
18
 impl BytesCodec {
24
 impl BytesCodec {
19
     pub fn new() -> Self {
25
     pub fn new() -> Self {
20
         Self {
26
         Self {
@@ -56,7 +62,7 @@ impl BytesCodec {
56
         }
62
         }
57
         src.advance(head_len);
63
         src.advance(head_len);
58
         src.reserve(n);
64
         src.reserve(n);
59
-        return Ok(Some(n));
65
+        Ok(Some(n))
60
     }
66
     }
61
 
67
 
62
     fn decode_data(&self, n: usize, src: &mut BytesMut) -> io::Result<Option<BytesMut>> {
68
     fn decode_data(&self, n: usize, src: &mut BytesMut) -> io::Result<Option<BytesMut>> {

+ 1 - 6
libs/hbb_common/src/compress.rs

@@ -32,12 +32,7 @@ pub fn decompress(data: &[u8]) -> Vec<u8> {
32
             const MAX: usize = 1024 * 1024 * 64;
32
             const MAX: usize = 1024 * 1024 * 64;
33
             const MIN: usize = 1024 * 1024;
33
             const MIN: usize = 1024 * 1024;
34
             let mut n = 30 * data.len();
34
             let mut n = 30 * data.len();
35
-            if n > MAX {
36
-                n = MAX;
37
-            }
38
-            if n < MIN {
39
-                n = MIN;
40
-            }
35
+            n = n.clamp(MIN, MAX);
41
             match d.decompress(data, n) {
36
             match d.decompress(data, n) {
42
                 Ok(res) => out = res,
37
                 Ok(res) => out = res,
43
                 Err(err) => {
38
                 Err(err) => {

Разница между файлами не показана из-за своего большого размера
+ 56 - 70
libs/hbb_common/src/config.rs


+ 86 - 97
libs/hbb_common/src/fs.rs

@@ -13,13 +13,13 @@ use crate::{
13
     config::{Config, COMPRESS_LEVEL},
13
     config::{Config, COMPRESS_LEVEL},
14
 };
14
 };
15
 
15
 
16
-pub fn read_dir(path: &PathBuf, include_hidden: bool) -> ResultType<FileDirectory> {
16
+pub fn read_dir(path: &Path, include_hidden: bool) -> ResultType<FileDirectory> {
17
     let mut dir = FileDirectory {
17
     let mut dir = FileDirectory {
18
-        path: get_string(&path),
18
+        path: get_string(path),
19
         ..Default::default()
19
         ..Default::default()
20
     };
20
     };
21
     #[cfg(windows)]
21
     #[cfg(windows)]
22
-    if "/" == &get_string(&path) {
22
+    if "/" == &get_string(path) {
23
         let drives = unsafe { winapi::um::fileapi::GetLogicalDrives() };
23
         let drives = unsafe { winapi::um::fileapi::GetLogicalDrives() };
24
         for i in 0..32 {
24
         for i in 0..32 {
25
             if drives & (1 << i) != 0 {
25
             if drives & (1 << i) != 0 {
@@ -36,74 +36,70 @@ pub fn read_dir(path: &PathBuf, include_hidden: bool) -> ResultType<FileDirector
36
         }
36
         }
37
         return Ok(dir);
37
         return Ok(dir);
38
     }
38
     }
39
-    for entry in path.read_dir()? {
40
-        if let Ok(entry) = entry {
41
-            let p = entry.path();
42
-            let name = p
43
-                .file_name()
44
-                .map(|p| p.to_str().unwrap_or(""))
45
-                .unwrap_or("")
46
-                .to_owned();
47
-            if name.is_empty() {
48
-                continue;
49
-            }
50
-            let mut is_hidden = false;
51
-            let meta;
52
-            if let Ok(tmp) = std::fs::symlink_metadata(&p) {
53
-                meta = tmp;
54
-            } else {
55
-                continue;
56
-            }
57
-            // docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
58
-            #[cfg(windows)]
59
-            if meta.file_attributes() & 0x2 != 0 {
60
-                is_hidden = true;
61
-            }
62
-            #[cfg(not(windows))]
63
-            if name.find('.').unwrap_or(usize::MAX) == 0 {
64
-                is_hidden = true;
65
-            }
66
-            if is_hidden && !include_hidden {
67
-                continue;
68
-            }
69
-            let (entry_type, size) = {
70
-                if p.is_dir() {
71
-                    if meta.file_type().is_symlink() {
72
-                        (FileType::DirLink.into(), 0)
73
-                    } else {
74
-                        (FileType::Dir.into(), 0)
75
-                    }
39
+    for entry in path.read_dir()?.flatten() {
40
+        let p = entry.path();
41
+        let name = p
42
+            .file_name()
43
+            .map(|p| p.to_str().unwrap_or(""))
44
+            .unwrap_or("")
45
+            .to_owned();
46
+        if name.is_empty() {
47
+            continue;
48
+        }
49
+        let mut is_hidden = false;
50
+        let meta;
51
+        if let Ok(tmp) = std::fs::symlink_metadata(&p) {
52
+            meta = tmp;
53
+        } else {
54
+            continue;
55
+        }
56
+        // docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
57
+        #[cfg(windows)]
58
+        if meta.file_attributes() & 0x2 != 0 {
59
+            is_hidden = true;
60
+        }
61
+        #[cfg(not(windows))]
62
+        if name.find('.').unwrap_or(usize::MAX) == 0 {
63
+            is_hidden = true;
64
+        }
65
+        if is_hidden && !include_hidden {
66
+            continue;
67
+        }
68
+        let (entry_type, size) = {
69
+            if p.is_dir() {
70
+                if meta.file_type().is_symlink() {
71
+                    (FileType::DirLink.into(), 0)
76
                 } else {
72
                 } else {
77
-                    if meta.file_type().is_symlink() {
78
-                        (FileType::FileLink.into(), 0)
79
-                    } else {
80
-                        (FileType::File.into(), meta.len())
81
-                    }
73
+                    (FileType::Dir.into(), 0)
82
                 }
74
                 }
83
-            };
84
-            let modified_time = meta
85
-                .modified()
86
-                .map(|x| {
87
-                    x.duration_since(std::time::SystemTime::UNIX_EPOCH)
88
-                        .map(|x| x.as_secs())
89
-                        .unwrap_or(0)
90
-                })
91
-                .unwrap_or(0) as u64;
92
-            dir.entries.push(FileEntry {
93
-                name: get_file_name(&p),
94
-                entry_type,
95
-                is_hidden,
96
-                size,
97
-                modified_time,
98
-                ..Default::default()
99
-            });
100
-        }
75
+            } else if meta.file_type().is_symlink() {
76
+                (FileType::FileLink.into(), 0)
77
+            } else {
78
+                (FileType::File.into(), meta.len())
79
+            }
80
+        };
81
+        let modified_time = meta
82
+            .modified()
83
+            .map(|x| {
84
+                x.duration_since(std::time::SystemTime::UNIX_EPOCH)
85
+                    .map(|x| x.as_secs())
86
+                    .unwrap_or(0)
87
+            })
88
+            .unwrap_or(0);
89
+        dir.entries.push(FileEntry {
90
+            name: get_file_name(&p),
91
+            entry_type,
92
+            is_hidden,
93
+            size,
94
+            modified_time,
95
+            ..Default::default()
96
+        });
101
     }
97
     }
102
     Ok(dir)
98
     Ok(dir)
103
 }
99
 }
104
 
100
 
105
 #[inline]
101
 #[inline]
106
-pub fn get_file_name(p: &PathBuf) -> String {
102
+pub fn get_file_name(p: &Path) -> String {
107
     p.file_name()
103
     p.file_name()
108
         .map(|p| p.to_str().unwrap_or(""))
104
         .map(|p| p.to_str().unwrap_or(""))
109
         .unwrap_or("")
105
         .unwrap_or("")
@@ -111,7 +107,7 @@ pub fn get_file_name(p: &PathBuf) -> String {
111
 }
107
 }
112
 
108
 
113
 #[inline]
109
 #[inline]
114
-pub fn get_string(path: &PathBuf) -> String {
110
+pub fn get_string(path: &Path) -> String {
115
     path.to_str().unwrap_or("").to_owned()
111
     path.to_str().unwrap_or("").to_owned()
116
 }
112
 }
117
 
113
 
@@ -127,14 +123,14 @@ pub fn get_home_as_string() -> String {
127
 
123
 
128
 fn read_dir_recursive(
124
 fn read_dir_recursive(
129
     path: &PathBuf,
125
     path: &PathBuf,
130
-    prefix: &PathBuf,
126
+    prefix: &Path,
131
     include_hidden: bool,
127
     include_hidden: bool,
132
 ) -> ResultType<Vec<FileEntry>> {
128
 ) -> ResultType<Vec<FileEntry>> {
133
     let mut files = Vec::new();
129
     let mut files = Vec::new();
134
     if path.is_dir() {
130
     if path.is_dir() {
135
         // to-do: symbol link handling, cp the link rather than the content
131
         // to-do: symbol link handling, cp the link rather than the content
136
         // to-do: file mode, for unix
132
         // to-do: file mode, for unix
137
-        let fd = read_dir(&path, include_hidden)?;
133
+        let fd = read_dir(path, include_hidden)?;
138
         for entry in fd.entries.iter() {
134
         for entry in fd.entries.iter() {
139
             match entry.entry_type.enum_value() {
135
             match entry.entry_type.enum_value() {
140
                 Ok(FileType::File) => {
136
                 Ok(FileType::File) => {
@@ -158,7 +154,7 @@ fn read_dir_recursive(
158
         }
154
         }
159
         Ok(files)
155
         Ok(files)
160
     } else if path.is_file() {
156
     } else if path.is_file() {
161
-        let (size, modified_time) = if let Ok(meta) = std::fs::metadata(&path) {
157
+        let (size, modified_time) = if let Ok(meta) = std::fs::metadata(path) {
162
             (
158
             (
163
                 meta.len(),
159
                 meta.len(),
164
                 meta.modified()
160
                 meta.modified()
@@ -167,7 +163,7 @@ fn read_dir_recursive(
167
                             .map(|x| x.as_secs())
163
                             .map(|x| x.as_secs())
168
                             .unwrap_or(0)
164
                             .unwrap_or(0)
169
                     })
165
                     })
170
-                    .unwrap_or(0) as u64,
166
+                    .unwrap_or(0),
171
             )
167
             )
172
         } else {
168
         } else {
173
             (0, 0)
169
             (0, 0)
@@ -249,7 +245,7 @@ pub struct RemoveJobMeta {
249
 
245
 
250
 #[inline]
246
 #[inline]
251
 fn get_ext(name: &str) -> &str {
247
 fn get_ext(name: &str) -> &str {
252
-    if let Some(i) = name.rfind(".") {
248
+    if let Some(i) = name.rfind('.') {
253
         return &name[i + 1..];
249
         return &name[i + 1..];
254
     }
250
     }
255
     ""
251
     ""
@@ -270,6 +266,7 @@ fn is_compressed_file(name: &str) -> bool {
270
 }
266
 }
271
 
267
 
272
 impl TransferJob {
268
 impl TransferJob {
269
+    #[allow(clippy::too_many_arguments)]
273
     pub fn new_write(
270
     pub fn new_write(
274
         id: i32,
271
         id: i32,
275
         remote: String,
272
         remote: String,
@@ -281,7 +278,7 @@ impl TransferJob {
281
         enable_overwrite_detection: bool,
278
         enable_overwrite_detection: bool,
282
     ) -> Self {
279
     ) -> Self {
283
         log::info!("new write {}", path);
280
         log::info!("new write {}", path);
284
-        let total_size = files.iter().map(|x| x.size as u64).sum();
281
+        let total_size = files.iter().map(|x| x.size).sum();
285
         Self {
282
         Self {
286
             id,
283
             id,
287
             remote,
284
             remote,
@@ -307,7 +304,7 @@ impl TransferJob {
307
     ) -> ResultType<Self> {
304
     ) -> ResultType<Self> {
308
         log::info!("new read {}", path);
305
         log::info!("new read {}", path);
309
         let files = get_recursive_files(&path, show_hidden)?;
306
         let files = get_recursive_files(&path, show_hidden)?;
310
-        let total_size = files.iter().map(|x| x.size as u64).sum();
307
+        let total_size = files.iter().map(|x| x.size).sum();
311
         Ok(Self {
308
         Ok(Self {
312
             id,
309
             id,
313
             remote,
310
             remote,
@@ -363,7 +360,7 @@ impl TransferJob {
363
             let entry = &self.files[file_num];
360
             let entry = &self.files[file_num];
364
             let path = self.join(&entry.name);
361
             let path = self.join(&entry.name);
365
             let download_path = format!("{}.download", get_string(&path));
362
             let download_path = format!("{}.download", get_string(&path));
366
-            std::fs::rename(&download_path, &path).ok();
363
+            std::fs::rename(download_path, &path).ok();
367
             filetime::set_file_mtime(
364
             filetime::set_file_mtime(
368
                 &path,
365
                 &path,
369
                 filetime::FileTime::from_unix_time(entry.modified_time as _, 0),
366
                 filetime::FileTime::from_unix_time(entry.modified_time as _, 0),
@@ -378,7 +375,7 @@ impl TransferJob {
378
             let entry = &self.files[file_num];
375
             let entry = &self.files[file_num];
379
             let path = self.join(&entry.name);
376
             let path = self.join(&entry.name);
380
             let download_path = format!("{}.download", get_string(&path));
377
             let download_path = format!("{}.download", get_string(&path));
381
-            std::fs::remove_file(&download_path).ok();
378
+            std::fs::remove_file(download_path).ok();
382
         }
379
         }
383
     }
380
     }
384
 
381
 
@@ -433,7 +430,7 @@ impl TransferJob {
433
         }
430
         }
434
         let name = &self.files[file_num].name;
431
         let name = &self.files[file_num].name;
435
         if self.file.is_none() {
432
         if self.file.is_none() {
436
-            match File::open(self.join(&name)).await {
433
+            match File::open(self.join(name)).await {
437
                 Ok(file) => {
434
                 Ok(file) => {
438
                     self.file = Some(file);
435
                     self.file = Some(file);
439
                     self.file_confirmed = false;
436
                     self.file_confirmed = false;
@@ -447,20 +444,15 @@ impl TransferJob {
447
                 }
444
                 }
448
             }
445
             }
449
         }
446
         }
450
-        if self.enable_overwrite_detection {
451
-            if !self.file_confirmed() {
452
-                if !self.file_is_waiting() {
453
-                    self.send_current_digest(stream).await?;
454
-                    self.set_file_is_waiting(true);
455
-                }
456
-                return Ok(None);
447
+        if self.enable_overwrite_detection && !self.file_confirmed() {
448
+            if !self.file_is_waiting() {
449
+                self.send_current_digest(stream).await?;
450
+                self.set_file_is_waiting(true);
457
             }
451
             }
452
+            return Ok(None);
458
         }
453
         }
459
         const BUF_SIZE: usize = 128 * 1024;
454
         const BUF_SIZE: usize = 128 * 1024;
460
-        let mut buf: Vec<u8> = Vec::with_capacity(BUF_SIZE);
461
-        unsafe {
462
-            buf.set_len(BUF_SIZE);
463
-        }
455
+        let mut buf: Vec<u8> = vec![0; BUF_SIZE];
464
         let mut compressed = false;
456
         let mut compressed = false;
465
         let mut offset: usize = 0;
457
         let mut offset: usize = 0;
466
         loop {
458
         loop {
@@ -582,10 +574,7 @@ impl TransferJob {
582
     #[inline]
574
     #[inline]
583
     pub fn job_completed(&self) -> bool {
575
     pub fn job_completed(&self) -> bool {
584
         // has no error, Condition 2
576
         // has no error, Condition 2
585
-        if !self.enable_overwrite_detection || (!self.file_confirmed && !self.file_is_waiting) {
586
-            return true;
587
-        }
588
-        return false;
577
+        !self.enable_overwrite_detection || (!self.file_confirmed && !self.file_is_waiting)
589
     }
578
     }
590
 
579
 
591
     /// Get job error message, useful for getting status when job had finished
580
     /// Get job error message, useful for getting status when job had finished
@@ -660,7 +649,7 @@ pub fn new_dir(id: i32, path: String, files: Vec<FileEntry>) -> Message {
660
     resp.set_dir(FileDirectory {
649
     resp.set_dir(FileDirectory {
661
         id,
650
         id,
662
         path,
651
         path,
663
-        entries: files.into(),
652
+        entries: files,
664
         ..Default::default()
653
         ..Default::default()
665
     });
654
     });
666
     let mut msg_out = Message::new();
655
     let mut msg_out = Message::new();
@@ -692,7 +681,7 @@ pub fn new_receive(id: i32, path: String, file_num: i32, files: Vec<FileEntry>)
692
     action.set_receive(FileTransferReceiveRequest {
681
     action.set_receive(FileTransferReceiveRequest {
693
         id,
682
         id,
694
         path,
683
         path,
695
-        files: files.into(),
684
+        files,
696
         file_num,
685
         file_num,
697
         ..Default::default()
686
         ..Default::default()
698
     });
687
     });
@@ -736,8 +725,8 @@ pub fn remove_job(id: i32, jobs: &mut Vec<TransferJob>) {
736
 }
725
 }
737
 
726
 
738
 #[inline]
727
 #[inline]
739
-pub fn get_job(id: i32, jobs: &mut Vec<TransferJob>) -> Option<&mut TransferJob> {
740
-    jobs.iter_mut().filter(|x| x.id() == id).next()
728
+pub fn get_job(id: i32, jobs: &mut [TransferJob]) -> Option<&mut TransferJob> {
729
+    jobs.iter_mut().find(|x| x.id() == id)
741
 }
730
 }
742
 
731
 
743
 pub async fn handle_read_jobs(
732
 pub async fn handle_read_jobs(
@@ -789,7 +778,7 @@ pub fn remove_all_empty_dir(path: &PathBuf) -> ResultType<()> {
789
                 remove_all_empty_dir(&path.join(&entry.name)).ok();
778
                 remove_all_empty_dir(&path.join(&entry.name)).ok();
790
             }
779
             }
791
             Ok(FileType::DirLink) | Ok(FileType::FileLink) => {
780
             Ok(FileType::DirLink) | Ok(FileType::FileLink) => {
792
-                std::fs::remove_file(&path.join(&entry.name)).ok();
781
+                std::fs::remove_file(path.join(&entry.name)).ok();
793
             }
782
             }
794
             _ => {}
783
             _ => {}
795
         }
784
         }
@@ -813,7 +802,7 @@ pub fn create_dir(dir: &str) -> ResultType<()> {
813
 #[inline]
802
 #[inline]
814
 pub fn transform_windows_path(entries: &mut Vec<FileEntry>) {
803
 pub fn transform_windows_path(entries: &mut Vec<FileEntry>) {
815
     for entry in entries {
804
     for entry in entries {
816
-        entry.name = entry.name.replace("\\", "/");
805
+        entry.name = entry.name.replace('\\', "/");
817
     }
806
     }
818
 }
807
 }
819
 
808
 
@@ -847,4 +836,4 @@ pub fn is_write_need_confirmation(
847
     } else {
836
     } else {
848
         Ok(DigestCheckResult::NoSuchFile)
837
         Ok(DigestCheckResult::NoSuchFile)
849
     }
838
     }
850
-}
839
+}

+ 24 - 34
libs/hbb_common/src/lib.rs

@@ -128,22 +128,20 @@ impl AddrMangle {
128
     }
128
     }
129
 
129
 
130
     pub fn decode(bytes: &[u8]) -> SocketAddr {
130
     pub fn decode(bytes: &[u8]) -> SocketAddr {
131
+        use std::convert::TryInto;
132
+
131
         if bytes.len() > 16 {
133
         if bytes.len() > 16 {
132
             if bytes.len() != 18 {
134
             if bytes.len() != 18 {
133
                 return Config::get_any_listen_addr(false);
135
                 return Config::get_any_listen_addr(false);
134
             }
136
             }
135
-            #[allow(invalid_value)]
136
-            let mut tmp: [u8; 2] = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
137
-            tmp.copy_from_slice(&bytes[16..]);
137
+            let tmp: [u8; 2] = bytes[16..].try_into().unwrap();
138
             let port = u16::from_le_bytes(tmp);
138
             let port = u16::from_le_bytes(tmp);
139
-            #[allow(invalid_value)]
140
-            let mut tmp: [u8; 16] = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
141
-            tmp.copy_from_slice(&bytes[..16]);
139
+            let tmp: [u8; 16] = bytes[..16].try_into().unwrap();
142
             let ip = std::net::Ipv6Addr::from(tmp);
140
             let ip = std::net::Ipv6Addr::from(tmp);
143
             return SocketAddr::new(IpAddr::V6(ip), port);
141
             return SocketAddr::new(IpAddr::V6(ip), port);
144
         }
142
         }
145
         let mut padded = [0u8; 16];
143
         let mut padded = [0u8; 16];
146
-        padded[..bytes.len()].copy_from_slice(&bytes);
144
+        padded[..bytes.len()].copy_from_slice(bytes);
147
         let number = u128::from_le_bytes(padded);
145
         let number = u128::from_le_bytes(padded);
148
         let tm = (number >> 17) & (u32::max_value() as u128);
146
         let tm = (number >> 17) & (u32::max_value() as u128);
149
         let ip = (((number >> 49) - tm) as u32).to_le_bytes();
147
         let ip = (((number >> 49) - tm) as u32).to_le_bytes();
@@ -157,21 +155,9 @@ impl AddrMangle {
157
 
155
 
158
 pub fn get_version_from_url(url: &str) -> String {
156
 pub fn get_version_from_url(url: &str) -> String {
159
     let n = url.chars().count();
157
     let n = url.chars().count();
160
-    let a = url
161
-        .chars()
162
-        .rev()
163
-        .enumerate()
164
-        .filter(|(_, x)| x == &'-')
165
-        .next()
166
-        .map(|(i, _)| i);
158
+    let a = url.chars().rev().position(|x| x == '-');
167
     if let Some(a) = a {
159
     if let Some(a) = a {
168
-        let b = url
169
-            .chars()
170
-            .rev()
171
-            .enumerate()
172
-            .filter(|(_, x)| x == &'.')
173
-            .next()
174
-            .map(|(i, _)| i);
160
+        let b = url.chars().rev().position(|x| x == '.');
175
         if let Some(b) = b {
161
         if let Some(b) = b {
176
             if a > b {
162
             if a > b {
177
                 if url
163
                 if url
@@ -196,20 +182,24 @@ pub fn get_version_from_url(url: &str) -> String {
196
 pub fn gen_version() {
182
 pub fn gen_version() {
197
     use std::io::prelude::*;
183
     use std::io::prelude::*;
198
     let mut file = File::create("./src/version.rs").unwrap();
184
     let mut file = File::create("./src/version.rs").unwrap();
199
-    for line in read_lines("Cargo.toml").unwrap() {
200
-        if let Ok(line) = line {
201
-            let ab: Vec<&str> = line.split("=").map(|x| x.trim()).collect();
202
-            if ab.len() == 2 && ab[0] == "version" {
203
-                file.write_all(format!("pub const VERSION: &str = {};\n", ab[1]).as_bytes())
204
-                    .ok();
205
-                break;
206
-            }
185
+    for line in read_lines("Cargo.toml").unwrap().flatten() {
186
+        let ab: Vec<&str> = line.split('=').map(|x| x.trim()).collect();
187
+        if ab.len() == 2 && ab[0] == "version" {
188
+            file.write_all(format!("pub const VERSION: &str = {};\n", ab[1]).as_bytes())
189
+                .ok();
190
+            break;
207
         }
191
         }
208
     }
192
     }
209
     // generate build date
193
     // generate build date
210
     let build_date = format!("{}", chrono::Local::now().format("%Y-%m-%d %H:%M"));
194
     let build_date = format!("{}", chrono::Local::now().format("%Y-%m-%d %H:%M"));
211
-    file.write_all(format!("pub const BUILD_DATE: &str = \"{}\";", build_date).as_bytes())
212
-        .ok();
195
+    file.write_all(
196
+        format!(
197
+            "#[allow(dead_code)]\npub const BUILD_DATE: &str = \"{}\";",
198
+            build_date
199
+        )
200
+        .as_bytes(),
201
+    )
202
+    .ok();
213
     file.sync_all().ok();
203
     file.sync_all().ok();
214
 }
204
 }
215
 
205
 
@@ -229,20 +219,20 @@ pub fn is_valid_custom_id(id: &str) -> bool {
229
 
219
 
230
 pub fn get_version_number(v: &str) -> i64 {
220
 pub fn get_version_number(v: &str) -> i64 {
231
     let mut n = 0;
221
     let mut n = 0;
232
-    for x in v.split(".") {
222
+    for x in v.split('.') {
233
         n = n * 1000 + x.parse::<i64>().unwrap_or(0);
223
         n = n * 1000 + x.parse::<i64>().unwrap_or(0);
234
     }
224
     }
235
     n
225
     n
236
 }
226
 }
237
 
227
 
238
 pub fn get_modified_time(path: &std::path::Path) -> SystemTime {
228
 pub fn get_modified_time(path: &std::path::Path) -> SystemTime {
239
-    std::fs::metadata(&path)
229
+    std::fs::metadata(path)
240
         .map(|m| m.modified().unwrap_or(UNIX_EPOCH))
230
         .map(|m| m.modified().unwrap_or(UNIX_EPOCH))
241
         .unwrap_or(UNIX_EPOCH)
231
         .unwrap_or(UNIX_EPOCH)
242
 }
232
 }
243
 
233
 
244
 pub fn get_created_time(path: &std::path::Path) -> SystemTime {
234
 pub fn get_created_time(path: &std::path::Path) -> SystemTime {
245
-    std::fs::metadata(&path)
235
+    std::fs::metadata(path)
246
         .map(|m| m.created().unwrap_or(UNIX_EPOCH))
236
         .map(|m| m.created().unwrap_or(UNIX_EPOCH))
247
         .unwrap_or(UNIX_EPOCH)
237
         .unwrap_or(UNIX_EPOCH)
248
 }
238
 }

+ 3 - 3
libs/hbb_common/src/password_security.rs

@@ -104,7 +104,7 @@ pub fn decrypt_str_or_original(s: &str, current_version: &str) -> (String, bool,
104
     if s.len() > VERSION_LEN {
104
     if s.len() > VERSION_LEN {
105
         let version = &s[..VERSION_LEN];
105
         let version = &s[..VERSION_LEN];
106
         if version == "00" {
106
         if version == "00" {
107
-            if let Ok(v) = decrypt(&s[VERSION_LEN..].as_bytes()) {
107
+            if let Ok(v) = decrypt(s[VERSION_LEN..].as_bytes()) {
108
                 return (
108
                 return (
109
                     String::from_utf8_lossy(&v).to_string(),
109
                     String::from_utf8_lossy(&v).to_string(),
110
                     true,
110
                     true,
@@ -149,7 +149,7 @@ pub fn decrypt_vec_or_original(v: &[u8], current_version: &str) -> (Vec<u8>, boo
149
 }
149
 }
150
 
150
 
151
 fn encrypt(v: &[u8]) -> Result<String, ()> {
151
 fn encrypt(v: &[u8]) -> Result<String, ()> {
152
-    if v.len() > 0 {
152
+    if !v.is_empty() {
153
         symmetric_crypt(v, true).map(|v| base64::encode(v, base64::Variant::Original))
153
         symmetric_crypt(v, true).map(|v| base64::encode(v, base64::Variant::Original))
154
     } else {
154
     } else {
155
         Err(())
155
         Err(())
@@ -157,7 +157,7 @@ fn encrypt(v: &[u8]) -> Result<String, ()> {
157
 }
157
 }
158
 
158
 
159
 fn decrypt(v: &[u8]) -> Result<Vec<u8>, ()> {
159
 fn decrypt(v: &[u8]) -> Result<Vec<u8>, ()> {
160
-    if v.len() > 0 {
160
+    if !v.is_empty() {
161
         base64::decode(v, base64::Variant::Original).and_then(|v| symmetric_crypt(&v, false))
161
         base64::decode(v, base64::Variant::Original).and_then(|v| symmetric_crypt(&v, false))
162
     } else {
162
     } else {
163
         Err(())
163
         Err(())

+ 4 - 4
libs/hbb_common/src/platform/linux.rs

@@ -32,7 +32,7 @@ pub fn get_display_server() -> String {
32
         // loginctl has not given the expected output.  try something else.
32
         // loginctl has not given the expected output.  try something else.
33
         if let Ok(sid) = std::env::var("XDG_SESSION_ID") {
33
         if let Ok(sid) = std::env::var("XDG_SESSION_ID") {
34
             // could also execute "cat /proc/self/sessionid"
34
             // could also execute "cat /proc/self/sessionid"
35
-            session = sid.to_owned();
35
+            session = sid;
36
         }
36
         }
37
         if session.is_empty() {
37
         if session.is_empty() {
38
             session = run_cmds("cat /proc/self/sessionid".to_owned()).unwrap_or_default();
38
             session = run_cmds("cat /proc/self/sessionid".to_owned()).unwrap_or_default();
@@ -63,7 +63,7 @@ fn get_display_server_of_session(session: &str) -> String {
63
                 if let Ok(xorg_results) = run_cmds(format!("ps -e | grep \"{}.\\\\+Xorg\"", tty))
63
                 if let Ok(xorg_results) = run_cmds(format!("ps -e | grep \"{}.\\\\+Xorg\"", tty))
64
                 // And check if Xorg is running on that tty
64
                 // And check if Xorg is running on that tty
65
                 {
65
                 {
66
-                    if xorg_results.trim_end().to_string() != "" {
66
+                    if xorg_results.trim_end() != "" {
67
                         // If it is, manually return "x11", otherwise return tty
67
                         // If it is, manually return "x11", otherwise return tty
68
                         return "x11".to_owned();
68
                         return "x11".to_owned();
69
                     }
69
                     }
@@ -88,7 +88,7 @@ pub fn get_values_of_seat0(indices: Vec<usize>) -> Vec<String> {
88
     if let Ok(output) = run_loginctl(None) {
88
     if let Ok(output) = run_loginctl(None) {
89
         for line in String::from_utf8_lossy(&output.stdout).lines() {
89
         for line in String::from_utf8_lossy(&output.stdout).lines() {
90
             if line.contains("seat0") {
90
             if line.contains("seat0") {
91
-                if let Some(sid) = line.split_whitespace().nth(0) {
91
+                if let Some(sid) = line.split_whitespace().next() {
92
                     if is_active(sid) {
92
                     if is_active(sid) {
93
                         return indices
93
                         return indices
94
                             .into_iter()
94
                             .into_iter()
@@ -103,7 +103,7 @@ pub fn get_values_of_seat0(indices: Vec<usize>) -> Vec<String> {
103
     // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73
103
     // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73
104
     if let Ok(output) = run_loginctl(None) {
104
     if let Ok(output) = run_loginctl(None) {
105
         for line in String::from_utf8_lossy(&output.stdout).lines() {
105
         for line in String::from_utf8_lossy(&output.stdout).lines() {
106
-            if let Some(sid) = line.split_whitespace().nth(0) {
106
+            if let Some(sid) = line.split_whitespace().next() {
107
                 let d = get_display_server_of_session(sid);
107
                 let d = get_display_server_of_session(sid);
108
                 if is_active(sid) && d != "tty" {
108
                 if is_active(sid) && d != "tty" {
109
                     return indices
109
                     return indices

+ 7 - 8
libs/hbb_common/src/socket_client.rs

@@ -11,7 +11,7 @@ use tokio_socks::{IntoTargetAddr, TargetAddr};
11
 
11
 
12
 pub fn test_if_valid_server(host: &str) -> String {
12
 pub fn test_if_valid_server(host: &str) -> String {
13
     let mut host = host.to_owned();
13
     let mut host = host.to_owned();
14
-    if !host.contains(":") {
14
+    if !host.contains(':') {
15
         host = format!("{}:{}", host, 0);
15
         host = format!("{}:{}", host, 0);
16
     }
16
     }
17
 
17
 
@@ -34,7 +34,7 @@ pub trait IsResolvedSocketAddr {
34
 
34
 
35
 impl IsResolvedSocketAddr for SocketAddr {
35
 impl IsResolvedSocketAddr for SocketAddr {
36
     fn resolve(&self) -> Option<&SocketAddr> {
36
     fn resolve(&self) -> Option<&SocketAddr> {
37
-        Some(&self)
37
+        Some(self)
38
     }
38
     }
39
 }
39
 }
40
 
40
 
@@ -83,12 +83,12 @@ pub async fn connect_tcp_local<
83
     if let Some(target) = target.resolve() {
83
     if let Some(target) = target.resolve() {
84
         if let Some(local) = local {
84
         if let Some(local) = local {
85
             if local.is_ipv6() && target.is_ipv4() {
85
             if local.is_ipv6() && target.is_ipv4() {
86
-                let target = query_nip_io(&target).await?;
87
-                return Ok(FramedStream::new(target, Some(local), ms_timeout).await?);
86
+                let target = query_nip_io(target).await?;
87
+                return FramedStream::new(target, Some(local), ms_timeout).await;
88
             }
88
             }
89
         }
89
         }
90
     }
90
     }
91
-    Ok(FramedStream::new(target, local, ms_timeout).await?)
91
+    FramedStream::new(target, local, ms_timeout).await
92
 }
92
 }
93
 
93
 
94
 #[inline]
94
 #[inline]
@@ -103,15 +103,14 @@ pub fn is_ipv4(target: &TargetAddr<'_>) -> bool {
103
 pub async fn query_nip_io(addr: &SocketAddr) -> ResultType<SocketAddr> {
103
 pub async fn query_nip_io(addr: &SocketAddr) -> ResultType<SocketAddr> {
104
     tokio::net::lookup_host(format!("{}.nip.io:{}", addr.ip(), addr.port()))
104
     tokio::net::lookup_host(format!("{}.nip.io:{}", addr.ip(), addr.port()))
105
         .await?
105
         .await?
106
-        .filter(|x| x.is_ipv6())
107
-        .next()
106
+        .find(|x| x.is_ipv6())
108
         .context("Failed to get ipv6 from nip.io")
107
         .context("Failed to get ipv6 from nip.io")
109
 }
108
 }
110
 
109
 
111
 #[inline]
110
 #[inline]
112
 pub fn ipv4_to_ipv6(addr: String, ipv4: bool) -> String {
111
 pub fn ipv4_to_ipv6(addr: String, ipv4: bool) -> String {
113
     if !ipv4 && crate::is_ipv4_str(&addr) {
112
     if !ipv4 && crate::is_ipv4_str(&addr) {
114
-        if let Some(ip) = addr.split(":").next() {
113
+        if let Some(ip) = addr.split(':').next() {
115
             return addr.replace(ip, &format!("{}.nip.io", ip));
114
             return addr.replace(ip, &format!("{}.nip.io", ip));
116
         }
115
         }
117
     }
116
     }

+ 9 - 7
libs/hbb_common/src/tcp.rs

@@ -1,4 +1,5 @@
1
 use crate::{bail, bytes_codec::BytesCodec, ResultType};
1
 use crate::{bail, bytes_codec::BytesCodec, ResultType};
2
+use anyhow::Context as AnyhowCtx;
2
 use bytes::{BufMut, Bytes, BytesMut};
3
 use bytes::{BufMut, Bytes, BytesMut};
3
 use futures::{SinkExt, StreamExt};
4
 use futures::{SinkExt, StreamExt};
4
 use protobuf::Message;
5
 use protobuf::Message;
@@ -209,7 +210,7 @@ impl FramedStream {
209
             if let Some(Ok(bytes)) = res.as_mut() {
210
             if let Some(Ok(bytes)) = res.as_mut() {
210
                 key.2 += 1;
211
                 key.2 += 1;
211
                 let nonce = Self::get_nonce(key.2);
212
                 let nonce = Self::get_nonce(key.2);
212
-                match secretbox::open(&bytes, &nonce, &key.0) {
213
+                match secretbox::open(bytes, &nonce, &key.0) {
213
                     Ok(res) => {
214
                     Ok(res) => {
214
                         bytes.clear();
215
                         bytes.clear();
215
                         bytes.put_slice(&res);
216
                         bytes.put_slice(&res);
@@ -245,16 +246,17 @@ impl FramedStream {
245
 
246
 
246
 const DEFAULT_BACKLOG: u32 = 128;
247
 const DEFAULT_BACKLOG: u32 = 128;
247
 
248
 
248
-#[allow(clippy::never_loop)]
249
 pub async fn new_listener<T: ToSocketAddrs>(addr: T, reuse: bool) -> ResultType<TcpListener> {
249
 pub async fn new_listener<T: ToSocketAddrs>(addr: T, reuse: bool) -> ResultType<TcpListener> {
250
     if !reuse {
250
     if !reuse {
251
         Ok(TcpListener::bind(addr).await?)
251
         Ok(TcpListener::bind(addr).await?)
252
     } else {
252
     } else {
253
-        for addr in lookup_host(&addr).await? {
254
-            let socket = new_socket(addr, true)?;
255
-            return Ok(socket.listen(DEFAULT_BACKLOG)?);
256
-        }
257
-        bail!("could not resolve to any address");
253
+        let addr = lookup_host(&addr)
254
+            .await?
255
+            .next()
256
+            .context("could not resolve to any address")?;
257
+        new_socket(addr, true)?
258
+            .listen(DEFAULT_BACKLOG)
259
+            .map_err(anyhow::Error::msg)
258
     }
260
     }
259
 }
261
 }
260
 
262
 

+ 31 - 28
libs/hbb_common/src/udp.rs

@@ -1,5 +1,5 @@
1
-use crate::{bail, ResultType};
2
-use anyhow::anyhow;
1
+use crate::ResultType;
2
+use anyhow::{anyhow, Context};
3
 use bytes::{Bytes, BytesMut};
3
 use bytes::{Bytes, BytesMut};
4
 use futures::{SinkExt, StreamExt};
4
 use futures::{SinkExt, StreamExt};
5
 use protobuf::Message;
5
 use protobuf::Message;
@@ -47,29 +47,30 @@ impl FramedSocket {
47
         Ok(Self::Direct(UdpFramed::new(socket, BytesCodec::new())))
47
         Ok(Self::Direct(UdpFramed::new(socket, BytesCodec::new())))
48
     }
48
     }
49
 
49
 
50
-    #[allow(clippy::never_loop)]
51
     pub async fn new_reuse<T: std::net::ToSocketAddrs>(addr: T) -> ResultType<Self> {
50
     pub async fn new_reuse<T: std::net::ToSocketAddrs>(addr: T) -> ResultType<Self> {
52
-        for addr in addr.to_socket_addrs()? {
53
-            let socket = new_socket(addr, true, 0)?.into_udp_socket();
54
-            return Ok(Self::Direct(UdpFramed::new(
55
-                UdpSocket::from_std(socket)?,
56
-                BytesCodec::new(),
57
-            )));
58
-        }
59
-        bail!("could not resolve to any address");
51
+        let addr = addr
52
+            .to_socket_addrs()?
53
+            .next()
54
+            .context("could not resolve to any address")?;
55
+        let socket = new_socket(addr, true, 0)?.into_udp_socket();
56
+        Ok(Self::Direct(UdpFramed::new(
57
+            UdpSocket::from_std(socket)?,
58
+            BytesCodec::new(),
59
+        )))
60
     }
60
     }
61
 
61
 
62
     pub async fn new_with_buf_size<T: std::net::ToSocketAddrs>(
62
     pub async fn new_with_buf_size<T: std::net::ToSocketAddrs>(
63
         addr: T,
63
         addr: T,
64
         buf_size: usize,
64
         buf_size: usize,
65
     ) -> ResultType<Self> {
65
     ) -> ResultType<Self> {
66
-        for addr in addr.to_socket_addrs()? {
67
-            return Ok(Self::Direct(UdpFramed::new(
68
-                UdpSocket::from_std(new_socket(addr, false, buf_size)?.into_udp_socket())?,
69
-                BytesCodec::new(),
70
-            )));
71
-        }
72
-        bail!("could not resolve to any address");
66
+        let addr = addr
67
+            .to_socket_addrs()?
68
+            .next()
69
+            .context("could not resolve to any address")?;
70
+        Ok(Self::Direct(UdpFramed::new(
71
+            UdpSocket::from_std(new_socket(addr, false, buf_size)?.into_udp_socket())?,
72
+            BytesCodec::new(),
73
+        )))
73
     }
74
     }
74
 
75
 
75
     pub async fn new_proxy<'a, 't, P: ToProxyAddrs, T: ToSocketAddrs>(
76
     pub async fn new_proxy<'a, 't, P: ToProxyAddrs, T: ToSocketAddrs>(
@@ -104,11 +105,12 @@ impl FramedSocket {
104
     ) -> ResultType<()> {
105
     ) -> ResultType<()> {
105
         let addr = addr.into_target_addr()?.to_owned();
106
         let addr = addr.into_target_addr()?.to_owned();
106
         let send_data = Bytes::from(msg.write_to_bytes()?);
107
         let send_data = Bytes::from(msg.write_to_bytes()?);
107
-        let _ = match self {
108
-            Self::Direct(f) => match addr {
109
-                TargetAddr::Ip(addr) => f.send((send_data, addr)).await?,
110
-                _ => {}
111
-            },
108
+        match self {
109
+            Self::Direct(f) => {
110
+                if let TargetAddr::Ip(addr) = addr {
111
+                    f.send((send_data, addr)).await?
112
+                }
113
+            }
112
             Self::ProxySocks(f) => f.send((send_data, addr)).await?,
114
             Self::ProxySocks(f) => f.send((send_data, addr)).await?,
113
         };
115
         };
114
         Ok(())
116
         Ok(())
@@ -123,11 +125,12 @@ impl FramedSocket {
123
     ) -> ResultType<()> {
125
     ) -> ResultType<()> {
124
         let addr = addr.into_target_addr()?.to_owned();
126
         let addr = addr.into_target_addr()?.to_owned();
125
 
127
 
126
-        let _ = match self {
127
-            Self::Direct(f) => match addr {
128
-                TargetAddr::Ip(addr) => f.send((Bytes::from(msg), addr)).await?,
129
-                _ => {}
130
-            },
128
+        match self {
129
+            Self::Direct(f) => {
130
+                if let TargetAddr::Ip(addr) = addr {
131
+                    f.send((Bytes::from(msg), addr)).await?
132
+                }
133
+            }
131
             Self::ProxySocks(f) => f.send((Bytes::from(msg), addr)).await?,
134
             Self::ProxySocks(f) => f.send((Bytes::from(msg), addr)).await?,
132
         };
135
         };
133
         Ok(())
136
         Ok(())

+ 13 - 5
src/common.rs

@@ -12,15 +12,17 @@ use std::{
12
     time::{Instant, SystemTime},
12
     time::{Instant, SystemTime},
13
 };
13
 };
14
 
14
 
15
+#[allow(dead_code)]
15
 pub(crate) fn get_expired_time() -> Instant {
16
 pub(crate) fn get_expired_time() -> Instant {
16
     let now = Instant::now();
17
     let now = Instant::now();
17
     now.checked_sub(std::time::Duration::from_secs(3600))
18
     now.checked_sub(std::time::Duration::from_secs(3600))
18
         .unwrap_or(now)
19
         .unwrap_or(now)
19
 }
20
 }
20
 
21
 
22
+#[allow(dead_code)]
21
 pub(crate) fn test_if_valid_server(host: &str, name: &str) -> ResultType<SocketAddr> {
23
 pub(crate) fn test_if_valid_server(host: &str, name: &str) -> ResultType<SocketAddr> {
22
     use std::net::ToSocketAddrs;
24
     use std::net::ToSocketAddrs;
23
-    let res = if host.contains(":") {
25
+    let res = if host.contains(':') {
24
         host.to_socket_addrs()?.next().context("")
26
         host.to_socket_addrs()?.next().context("")
25
     } else {
27
     } else {
26
         format!("{}:{}", host, 0)
28
         format!("{}:{}", host, 0)
@@ -34,9 +36,10 @@ pub(crate) fn test_if_valid_server(host: &str, name: &str) -> ResultType<SocketA
34
     res
36
     res
35
 }
37
 }
36
 
38
 
39
+#[allow(dead_code)]
37
 pub(crate) fn get_servers(s: &str, tag: &str) -> Vec<String> {
40
 pub(crate) fn get_servers(s: &str, tag: &str) -> Vec<String> {
38
     let servers: Vec<String> = s
41
     let servers: Vec<String> = s
39
-        .split(",")
42
+        .split(',')
40
         .filter(|x| !x.is_empty() && test_if_valid_server(x, tag).is_ok())
43
         .filter(|x| !x.is_empty() && test_if_valid_server(x, tag).is_ok())
41
         .map(|x| x.to_owned())
44
         .map(|x| x.to_owned())
42
         .collect();
45
         .collect();
@@ -44,17 +47,19 @@ pub(crate) fn get_servers(s: &str, tag: &str) -> Vec<String> {
44
     servers
47
     servers
45
 }
48
 }
46
 
49
 
50
+#[allow(dead_code)]
47
 #[inline]
51
 #[inline]
48
 fn arg_name(name: &str) -> String {
52
 fn arg_name(name: &str) -> String {
49
-    name.to_uppercase().replace("_", "-")
53
+    name.to_uppercase().replace('_', "-")
50
 }
54
 }
51
 
55
 
56
+#[allow(dead_code)]
52
 pub fn init_args(args: &str, name: &str, about: &str) {
57
 pub fn init_args(args: &str, name: &str, about: &str) {
53
     let matches = App::new(name)
58
     let matches = App::new(name)
54
         .version(crate::version::VERSION)
59
         .version(crate::version::VERSION)
55
         .author("Purslane Ltd. <info@rustdesk.com>")
60
         .author("Purslane Ltd. <info@rustdesk.com>")
56
         .about(about)
61
         .about(about)
57
-        .args_from_usage(&args)
62
+        .args_from_usage(args)
58
         .get_matches();
63
         .get_matches();
59
     if let Ok(v) = Ini::load_from_file(".env") {
64
     if let Ok(v) = Ini::load_from_file(".env") {
60
         if let Some(section) = v.section(None::<String>) {
65
         if let Some(section) = v.section(None::<String>) {
@@ -79,16 +84,19 @@ pub fn init_args(args: &str, name: &str, about: &str) {
79
     }
84
     }
80
 }
85
 }
81
 
86
 
87
+#[allow(dead_code)]
82
 #[inline]
88
 #[inline]
83
 pub fn get_arg(name: &str) -> String {
89
 pub fn get_arg(name: &str) -> String {
84
     get_arg_or(name, "".to_owned())
90
     get_arg_or(name, "".to_owned())
85
 }
91
 }
86
 
92
 
93
+#[allow(dead_code)]
87
 #[inline]
94
 #[inline]
88
 pub fn get_arg_or(name: &str, default: String) -> String {
95
 pub fn get_arg_or(name: &str, default: String) -> String {
89
     std::env::var(arg_name(name)).unwrap_or(default)
96
     std::env::var(arg_name(name)).unwrap_or(default)
90
 }
97
 }
91
 
98
 
99
+#[allow(dead_code)]
92
 #[inline]
100
 #[inline]
93
 pub fn now() -> u64 {
101
 pub fn now() -> u64 {
94
     SystemTime::now()
102
     SystemTime::now()
@@ -121,7 +129,7 @@ pub fn gen_sk(wait: u64) -> (String, Option<sign::SecretKey>) {
121
         };
129
         };
122
         let (mut pk, mut sk) = gen_func();
130
         let (mut pk, mut sk) = gen_func();
123
         for _ in 0..300 {
131
         for _ in 0..300 {
124
-            if !pk.contains("/") && !pk.contains(":") {
132
+            if !pk.contains('/') && !pk.contains(':') {
125
                 break;
133
                 break;
126
             }
134
             }
127
             (pk, sk) = gen_func();
135
             (pk, sk) = gen_func();

+ 1 - 35
src/database.rs

@@ -1,6 +1,5 @@
1
 use async_trait::async_trait;
1
 use async_trait::async_trait;
2
 use hbb_common::{log, ResultType};
2
 use hbb_common::{log, ResultType};
3
-use serde_json::value::Value;
4
 use sqlx::{
3
 use sqlx::{
5
     sqlite::SqliteConnectOptions, ConnectOptions, Connection, Error as SqlxError, SqliteConnection,
4
     sqlite::SqliteConnectOptions, ConnectOptions, Connection, Error as SqlxError, SqliteConnection,
6
 };
5
 };
@@ -8,7 +7,6 @@ use std::{ops::DerefMut, str::FromStr};
8
 //use sqlx::postgres::PgPoolOptions;
7
 //use sqlx::postgres::PgPoolOptions;
9
 //use sqlx::mysql::MySqlPoolOptions;
8
 //use sqlx::mysql::MySqlPoolOptions;
10
 
9
 
11
-pub(crate) type MapValue = serde_json::map::Map<String, Value>;
12
 type Pool = deadpool::managed::Pool<DbPool>;
10
 type Pool = deadpool::managed::Pool<DbPool>;
13
 
11
 
14
 pub struct DbPool {
12
 pub struct DbPool {
@@ -54,7 +52,7 @@ impl Database {
54
             std::fs::File::create(url).ok();
52
             std::fs::File::create(url).ok();
55
         }
53
         }
56
         let n: usize = std::env::var("MAX_DATABASE_CONNECTIONS")
54
         let n: usize = std::env::var("MAX_DATABASE_CONNECTIONS")
57
-            .unwrap_or("1".to_owned())
55
+            .unwrap_or_else(|_| "1".to_owned())
58
             .parse()
56
             .parse()
59
             .unwrap_or(1);
57
             .unwrap_or(1);
60
         log::debug!("MAX_DATABASE_CONNECTIONS={}", n);
58
         log::debug!("MAX_DATABASE_CONNECTIONS={}", n);
@@ -105,24 +103,6 @@ impl Database {
105
         .await?)
103
         .await?)
106
     }
104
     }
107
 
105
 
108
-    #[inline]
109
-    pub async fn get_conn(&self) -> ResultType<deadpool::managed::Object<DbPool>> {
110
-        Ok(self.pool.get().await?)
111
-    }
112
-
113
-    pub async fn update_peer(&self, payload: MapValue, guid: &[u8]) -> ResultType<()> {
114
-        let mut conn = self.get_conn().await?;
115
-        let mut tx = conn.begin().await?;
116
-        if let Some(v) = payload.get("note") {
117
-            let v = get_str(v);
118
-            sqlx::query!("update peer set note = ? where guid = ?", v, guid)
119
-                .execute(&mut tx)
120
-                .await?;
121
-        }
122
-        tx.commit().await?;
123
-        Ok(())
124
-    }
125
-
126
     pub async fn insert_peer(
106
     pub async fn insert_peer(
127
         &self,
107
         &self,
128
         id: &str,
108
         id: &str,
@@ -199,17 +179,3 @@ mod tests {
199
         hbb_common::futures::future::join_all(jobs).await;
179
         hbb_common::futures::future::join_all(jobs).await;
200
     }
180
     }
201
 }
181
 }
202
-
203
-pub(crate) fn get_str(v: &Value) -> Option<&str> {
204
-    match v {
205
-        Value::String(v) => {
206
-            let v = v.trim();
207
-            if v.is_empty() {
208
-                None
209
-            } else {
210
-                Some(v)
211
-            }
212
-        }
213
-        _ => None,
214
-    }
215
-}

+ 25 - 25
src/peer.rs

@@ -10,10 +10,13 @@ use hbb_common::{
10
 use serde_derive::{Deserialize, Serialize};
10
 use serde_derive::{Deserialize, Serialize};
11
 use std::{collections::HashMap, collections::HashSet, net::SocketAddr, sync::Arc, time::Instant};
11
 use std::{collections::HashMap, collections::HashSet, net::SocketAddr, sync::Arc, time::Instant};
12
 
12
 
13
+type IpBlockMap = HashMap<String, ((u32, Instant), (HashSet<String>, Instant))>;
14
+type UserStatusMap = HashMap<Vec<u8>, Arc<(Option<Vec<u8>>, bool)>>;
15
+type IpChangesMap = HashMap<String, (Instant, HashMap<String, i32>)>;
13
 lazy_static::lazy_static! {
16
 lazy_static::lazy_static! {
14
-    pub(crate) static ref IP_BLOCKER: Mutex<HashMap<String, ((u32, Instant), (HashSet<String>, Instant))>> = Default::default();
15
-    pub(crate) static ref USER_STATUS: RwLock<HashMap<Vec<u8>, Arc<(Option<Vec<u8>>, bool)>>> = Default::default();
16
-    pub(crate) static ref IP_CHANGES: Mutex<HashMap<String, (Instant, HashMap<String, i32>)>> = Default::default();
17
+    pub(crate) static ref IP_BLOCKER: Mutex<IpBlockMap> = Default::default();
18
+    pub(crate) static ref USER_STATUS: RwLock<UserStatusMap> = Default::default();
19
+    pub(crate) static ref IP_CHANGES: Mutex<IpChangesMap> = Default::default();
17
 }
20
 }
18
 pub static IP_CHANGE_DUR: u64 = 180;
21
 pub static IP_CHANGE_DUR: u64 = 180;
19
 pub static IP_CHANGE_DUR_X2: u64 = IP_CHANGE_DUR * 2;
22
 pub static IP_CHANGE_DUR_X2: u64 = IP_CHANGE_DUR * 2;
@@ -32,9 +35,9 @@ pub(crate) struct Peer {
32
     pub(crate) guid: Vec<u8>,
35
     pub(crate) guid: Vec<u8>,
33
     pub(crate) uuid: Bytes,
36
     pub(crate) uuid: Bytes,
34
     pub(crate) pk: Bytes,
37
     pub(crate) pk: Bytes,
35
-    pub(crate) user: Option<Vec<u8>>,
38
+    // pub(crate) user: Option<Vec<u8>>,
36
     pub(crate) info: PeerInfo,
39
     pub(crate) info: PeerInfo,
37
-    pub(crate) disabled: bool,
40
+    // pub(crate) disabled: bool,
38
     pub(crate) reg_pk: (u32, Instant), // how often register_pk
41
     pub(crate) reg_pk: (u32, Instant), // how often register_pk
39
 }
42
 }
40
 
43
 
@@ -47,8 +50,8 @@ impl Default for Peer {
47
             uuid: Bytes::new(),
50
             uuid: Bytes::new(),
48
             pk: Bytes::new(),
51
             pk: Bytes::new(),
49
             info: Default::default(),
52
             info: Default::default(),
50
-            user: None,
51
-            disabled: false,
53
+            // user: None,
54
+            // disabled: false,
52
             reg_pk: (0, get_expired_time()),
55
             reg_pk: (0, get_expired_time()),
53
         }
56
         }
54
     }
57
     }
@@ -65,7 +68,6 @@ pub(crate) struct PeerMap {
65
 impl PeerMap {
68
 impl PeerMap {
66
     pub(crate) async fn new() -> ResultType<Self> {
69
     pub(crate) async fn new() -> ResultType<Self> {
67
         let db = std::env::var("DB_URL").unwrap_or({
70
         let db = std::env::var("DB_URL").unwrap_or({
68
-            #[allow(unused_mut)]
69
             let mut db = "db_v2.sqlite3".to_owned();
71
             let mut db = "db_v2.sqlite3".to_owned();
70
             #[cfg(all(windows, not(debug_assertions)))]
72
             #[cfg(all(windows, not(debug_assertions)))]
71
             {
73
             {
@@ -132,24 +134,22 @@ impl PeerMap {
132
 
134
 
133
     #[inline]
135
     #[inline]
134
     pub(crate) async fn get(&self, id: &str) -> Option<LockPeer> {
136
     pub(crate) async fn get(&self, id: &str) -> Option<LockPeer> {
135
-        let p = self.map.read().await.get(id).map(|x| x.clone());
137
+        let p = self.map.read().await.get(id).cloned();
136
         if p.is_some() {
138
         if p.is_some() {
137
             return p;
139
             return p;
138
-        } else {
139
-            if let Ok(Some(v)) = self.db.get_peer(id).await {
140
-                let peer = Peer {
141
-                    guid: v.guid,
142
-                    uuid: v.uuid.into(),
143
-                    pk: v.pk.into(),
144
-                    user: v.user,
145
-                    info: serde_json::from_str::<PeerInfo>(&v.info).unwrap_or_default(),
146
-                    disabled: v.status == Some(0),
147
-                    ..Default::default()
148
-                };
149
-                let peer = Arc::new(RwLock::new(peer));
150
-                self.map.write().await.insert(id.to_owned(), peer.clone());
151
-                return Some(peer);
152
-            }
140
+        } else if let Ok(Some(v)) = self.db.get_peer(id).await {
141
+            let peer = Peer {
142
+                guid: v.guid,
143
+                uuid: v.uuid.into(),
144
+                pk: v.pk.into(),
145
+                // user: v.user,
146
+                info: serde_json::from_str::<PeerInfo>(&v.info).unwrap_or_default(),
147
+                // disabled: v.status == Some(0),
148
+                ..Default::default()
149
+            };
150
+            let peer = Arc::new(RwLock::new(peer));
151
+            self.map.write().await.insert(id.to_owned(), peer.clone());
152
+            return Some(peer);
153
         }
153
         }
154
         None
154
         None
155
     }
155
     }
@@ -170,7 +170,7 @@ impl PeerMap {
170
 
170
 
171
     #[inline]
171
     #[inline]
172
     pub(crate) async fn get_in_memory(&self, id: &str) -> Option<LockPeer> {
172
     pub(crate) async fn get_in_memory(&self, id: &str) -> Option<LockPeer> {
173
-        self.map.read().await.get(id).map(|x| x.clone())
173
+        self.map.read().await.get(id).cloned()
174
     }
174
     }
175
 
175
 
176
     #[inline]
176
     #[inline]

+ 35 - 31
src/relay_server.rs

@@ -37,12 +37,12 @@ lazy_static::lazy_static! {
37
 }
37
 }
38
 
38
 
39
 static mut DOWNGRADE_THRESHOLD: f64 = 0.66;
39
 static mut DOWNGRADE_THRESHOLD: f64 = 0.66;
40
-static mut DOWNGRADE_START_CHECK: usize = 1800_000; // in ms
40
+static mut DOWNGRADE_START_CHECK: usize = 1_800_000; // in ms
41
 static mut LIMIT_SPEED: usize = 4 * 1024 * 1024; // in bit/s
41
 static mut LIMIT_SPEED: usize = 4 * 1024 * 1024; // in bit/s
42
 static mut TOTAL_BANDWIDTH: usize = 1024 * 1024 * 1024; // in bit/s
42
 static mut TOTAL_BANDWIDTH: usize = 1024 * 1024 * 1024; // in bit/s
43
 static mut SINGLE_BANDWIDTH: usize = 16 * 1024 * 1024; // in bit/s
43
 static mut SINGLE_BANDWIDTH: usize = 16 * 1024 * 1024; // in bit/s
44
-const BLACKLIST_FILE: &'static str = "blacklist.txt";
45
-const BLOCKLIST_FILE: &'static str = "blocklist.txt";
44
+const BLACKLIST_FILE: &str = "blacklist.txt";
45
+const BLOCKLIST_FILE: &str = "blocklist.txt";
46
 
46
 
47
 #[tokio::main(flavor = "multi_thread")]
47
 #[tokio::main(flavor = "multi_thread")]
48
 pub async fn start(port: &str, key: &str) -> ResultType<()> {
48
 pub async fn start(port: &str, key: &str) -> ResultType<()> {
@@ -50,8 +50,8 @@ pub async fn start(port: &str, key: &str) -> ResultType<()> {
50
     if let Ok(mut file) = std::fs::File::open(BLACKLIST_FILE) {
50
     if let Ok(mut file) = std::fs::File::open(BLACKLIST_FILE) {
51
         let mut contents = String::new();
51
         let mut contents = String::new();
52
         if file.read_to_string(&mut contents).is_ok() {
52
         if file.read_to_string(&mut contents).is_ok() {
53
-            for x in contents.split("\n") {
54
-                if let Some(ip) = x.trim().split(' ').nth(0) {
53
+            for x in contents.split('\n') {
54
+                if let Some(ip) = x.trim().split(' ').next() {
55
                     BLACKLIST.write().await.insert(ip.to_owned());
55
                     BLACKLIST.write().await.insert(ip.to_owned());
56
                 }
56
                 }
57
             }
57
             }
@@ -65,8 +65,8 @@ pub async fn start(port: &str, key: &str) -> ResultType<()> {
65
     if let Ok(mut file) = std::fs::File::open(BLOCKLIST_FILE) {
65
     if let Ok(mut file) = std::fs::File::open(BLOCKLIST_FILE) {
66
         let mut contents = String::new();
66
         let mut contents = String::new();
67
         if file.read_to_string(&mut contents).is_ok() {
67
         if file.read_to_string(&mut contents).is_ok() {
68
-            for x in contents.split("\n") {
69
-                if let Some(ip) = x.trim().split(' ').nth(0) {
68
+            for x in contents.split('\n') {
69
+                if let Some(ip) = x.trim().split(' ').next() {
70
                     BLOCKLIST.write().await.insert(ip.to_owned());
70
                     BLOCKLIST.write().await.insert(ip.to_owned());
71
                 }
71
                 }
72
             }
72
             }
@@ -158,8 +158,10 @@ fn check_params() {
158
 }
158
 }
159
 
159
 
160
 async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
160
 async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
161
+    use std::fmt::Write;
162
+
161
     let mut res = "".to_owned();
163
     let mut res = "".to_owned();
162
-    let mut fds = cmd.trim().split(" ");
164
+    let mut fds = cmd.trim().split(' ');
163
     match fds.next() {
165
     match fds.next() {
164
         Some("h") => {
166
         Some("h") => {
165
             res = format!(
167
             res = format!(
@@ -180,7 +182,7 @@ async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
180
         }
182
         }
181
         Some("blacklist-add" | "ba") => {
183
         Some("blacklist-add" | "ba") => {
182
             if let Some(ip) = fds.next() {
184
             if let Some(ip) = fds.next() {
183
-                for ip in ip.split("|") {
185
+                for ip in ip.split('|') {
184
                     BLACKLIST.write().await.insert(ip.to_owned());
186
                     BLACKLIST.write().await.insert(ip.to_owned());
185
                 }
187
                 }
186
             }
188
             }
@@ -190,7 +192,7 @@ async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
190
                 if ip == "all" {
192
                 if ip == "all" {
191
                     BLACKLIST.write().await.clear();
193
                     BLACKLIST.write().await.clear();
192
                 } else {
194
                 } else {
193
-                    for ip in ip.split("|") {
195
+                    for ip in ip.split('|') {
194
                         BLACKLIST.write().await.remove(ip);
196
                         BLACKLIST.write().await.remove(ip);
195
                     }
197
                     }
196
                 }
198
                 }
@@ -201,13 +203,13 @@ async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
201
                 res = format!("{}\n", BLACKLIST.read().await.get(ip).is_some());
203
                 res = format!("{}\n", BLACKLIST.read().await.get(ip).is_some());
202
             } else {
204
             } else {
203
                 for ip in BLACKLIST.read().await.clone().into_iter() {
205
                 for ip in BLACKLIST.read().await.clone().into_iter() {
204
-                    res += &format!("{}\n", ip);
206
+                    let _ = writeln!(res, "{ip}");
205
                 }
207
                 }
206
             }
208
             }
207
         }
209
         }
208
         Some("blocklist-add" | "Ba") => {
210
         Some("blocklist-add" | "Ba") => {
209
             if let Some(ip) = fds.next() {
211
             if let Some(ip) = fds.next() {
210
-                for ip in ip.split("|") {
212
+                for ip in ip.split('|') {
211
                     BLOCKLIST.write().await.insert(ip.to_owned());
213
                     BLOCKLIST.write().await.insert(ip.to_owned());
212
                 }
214
                 }
213
             }
215
             }
@@ -217,7 +219,7 @@ async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
217
                 if ip == "all" {
219
                 if ip == "all" {
218
                     BLOCKLIST.write().await.clear();
220
                     BLOCKLIST.write().await.clear();
219
                 } else {
221
                 } else {
220
-                    for ip in ip.split("|") {
222
+                    for ip in ip.split('|') {
221
                         BLOCKLIST.write().await.remove(ip);
223
                         BLOCKLIST.write().await.remove(ip);
222
                     }
224
                     }
223
                 }
225
                 }
@@ -228,7 +230,7 @@ async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
228
                 res = format!("{}\n", BLOCKLIST.read().await.get(ip).is_some());
230
                 res = format!("{}\n", BLOCKLIST.read().await.get(ip).is_some());
229
             } else {
231
             } else {
230
                 for ip in BLOCKLIST.read().await.clone().into_iter() {
232
                 for ip in BLOCKLIST.read().await.clone().into_iter() {
231
-                    res += &format!("{}\n", ip);
233
+                    let _ = writeln!(res, "{ip}");
232
                 }
234
                 }
233
             }
235
             }
234
         }
236
         }
@@ -313,15 +315,16 @@ async fn check_cmd(cmd: &str, limiter: Limiter) -> String {
313
                 .read()
315
                 .read()
314
                 .await
316
                 .await
315
                 .iter()
317
                 .iter()
316
-                .map(|x| (x.0.clone(), x.1.clone()))
318
+                .map(|x| (x.0.clone(), *x.1))
317
                 .collect();
319
                 .collect();
318
             tmp.sort_by(|a, b| ((b.1).1).partial_cmp(&(a.1).1).unwrap());
320
             tmp.sort_by(|a, b| ((b.1).1).partial_cmp(&(a.1).1).unwrap());
319
             for (ip, (elapsed, total, highest, speed)) in tmp {
321
             for (ip, (elapsed, total, highest, speed)) in tmp {
320
-                if elapsed <= 0 {
322
+                if elapsed == 0 {
321
                     continue;
323
                     continue;
322
                 }
324
                 }
323
-                res += &format!(
324
-                    "{}: {}s {:.2}MB {}kb/s {}kb/s {}kb/s\n",
325
+                let _ = writeln!(
326
+                    res,
327
+                    "{}: {}s {:.2}MB {}kb/s {}kb/s {}kb/s",
325
                     ip,
328
                     ip,
326
                     elapsed / 1000,
329
                     elapsed / 1000,
327
                     total as f64 / 1024. / 1024. / 8.,
330
                     total as f64 / 1024. / 1024. / 8.,
@@ -496,7 +499,7 @@ async fn relay(
496
                     total_limiter.consume(nb).await;
499
                     total_limiter.consume(nb).await;
497
                     total += nb;
500
                     total += nb;
498
                     total_s += nb;
501
                     total_s += nb;
499
-                    if bytes.len() > 0 {
502
+                    if !bytes.is_empty() {
500
                         stream.send_raw(bytes.into()).await?;
503
                         stream.send_raw(bytes.into()).await?;
501
                     }
504
                     }
502
                 } else {
505
                 } else {
@@ -515,7 +518,7 @@ async fn relay(
515
                     total_limiter.consume(nb).await;
518
                     total_limiter.consume(nb).await;
516
                     total += nb;
519
                     total += nb;
517
                     total_s += nb;
520
                     total_s += nb;
518
-                    if bytes.len() > 0 {
521
+                    if !bytes.is_empty() {
519
                         peer.send_raw(bytes.into()).await?;
522
                         peer.send_raw(bytes.into()).await?;
520
                     }
523
                     }
521
                 } else {
524
                 } else {
@@ -537,7 +540,7 @@ async fn relay(
537
             }
540
             }
538
             blacked = BLACKLIST.read().await.get(&ip).is_some();
541
             blacked = BLACKLIST.read().await.get(&ip).is_some();
539
             tm = std::time::Instant::now();
542
             tm = std::time::Instant::now();
540
-            let speed = total_s / (n as usize);
543
+            let speed = total_s / n;
541
             if speed > highest_s {
544
             if speed > highest_s {
542
                 highest_s = speed;
545
                 highest_s = speed;
543
             }
546
             }
@@ -547,16 +550,17 @@ async fn relay(
547
                 (elapsed as _, total as _, highest_s as _, speed as _),
550
                 (elapsed as _, total as _, highest_s as _, speed as _),
548
             );
551
             );
549
             total_s = 0;
552
             total_s = 0;
550
-            if elapsed > unsafe { DOWNGRADE_START_CHECK } && !downgrade {
551
-                if total > elapsed * downgrade_threshold {
552
-                    downgrade = true;
553
-                    log::info!(
554
-                        "Downgrade {}, exceed downgrade threshold {}bit/ms in {}ms",
555
-                        id,
556
-                        downgrade_threshold,
557
-                        elapsed
558
-                    );
559
-                }
553
+            if elapsed > unsafe { DOWNGRADE_START_CHECK }
554
+                && !downgrade
555
+                && total > elapsed * downgrade_threshold
556
+            {
557
+                downgrade = true;
558
+                log::info!(
559
+                    "Downgrade {}, exceed downgrade threshold {}bit/ms in {}ms",
560
+                    id,
561
+                    downgrade_threshold,
562
+                    elapsed
563
+                );
560
             }
564
             }
561
         }
565
         }
562
     }
566
     }

+ 37 - 38
src/rendezvous_server.rs

@@ -40,7 +40,7 @@ const ADDR_127: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
40
 
40
 
41
 #[derive(Clone, Debug)]
41
 #[derive(Clone, Debug)]
42
 enum Data {
42
 enum Data {
43
-    Msg(RendezvousMessage, SocketAddr),
43
+    Msg(Box<RendezvousMessage>, SocketAddr),
44
     RelayServers0(String),
44
     RelayServers0(String),
45
     RelayServers(RelayServers),
45
     RelayServers(RelayServers),
46
 }
46
 }
@@ -233,7 +233,7 @@ impl RendezvousServer {
233
                 }
233
                 }
234
                 Some(data) = rx.recv() => {
234
                 Some(data) = rx.recv() => {
235
                     match data {
235
                     match data {
236
-                        Data::Msg(msg, addr) => { allow_err!(socket.send(&msg, addr).await); }
236
+                        Data::Msg(msg, addr) => { allow_err!(socket.send(msg.as_ref(), addr).await); }
237
                         Data::RelayServers0(rs) => { self.parse_relay_servers(&rs); }
237
                         Data::RelayServers0(rs) => { self.parse_relay_servers(&rs); }
238
                         Data::RelayServers(rs) => { self.relay_servers = Arc::new(rs); }
238
                         Data::RelayServers(rs) => { self.relay_servers = Arc::new(rs); }
239
                     }
239
                     }
@@ -303,11 +303,11 @@ impl RendezvousServer {
303
         socket: &mut FramedSocket,
303
         socket: &mut FramedSocket,
304
         key: &str,
304
         key: &str,
305
     ) -> ResultType<()> {
305
     ) -> ResultType<()> {
306
-        if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
306
+        if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(bytes) {
307
             match msg_in.union {
307
             match msg_in.union {
308
                 Some(rendezvous_message::Union::RegisterPeer(rp)) => {
308
                 Some(rendezvous_message::Union::RegisterPeer(rp)) => {
309
                     // B registered
309
                     // B registered
310
-                    if rp.id.len() > 0 {
310
+                    if !rp.id.is_empty() {
311
                         log::trace!("New peer registered: {:?} {:?}", &rp.id, &addr);
311
                         log::trace!("New peer registered: {:?} {:?}", &rp.id, &addr);
312
                         self.update_addr(rp.id, addr, socket).await?;
312
                         self.update_addr(rp.id, addr, socket).await?;
313
                         if self.inner.serial > rp.serial {
313
                         if self.inner.serial > rp.serial {
@@ -384,12 +384,10 @@ impl RendezvousServer {
384
                                 *tm = Instant::now();
384
                                 *tm = Instant::now();
385
                                 ips.clear();
385
                                 ips.clear();
386
                                 ips.insert(ip.clone(), 1);
386
                                 ips.insert(ip.clone(), 1);
387
+                            } else if let Some(v) = ips.get_mut(&ip) {
388
+                                *v += 1;
387
                             } else {
389
                             } else {
388
-                                if let Some(v) = ips.get_mut(&ip) {
389
-                                    *v += 1;
390
-                                } else {
391
-                                    ips.insert(ip.clone(), 1);
392
-                                }
390
+                                ips.insert(ip.clone(), 1);
393
                             }
391
                             }
394
                         } else {
392
                         } else {
395
                             lock.insert(
393
                             lock.insert(
@@ -472,14 +470,14 @@ impl RendezvousServer {
472
         key: &str,
470
         key: &str,
473
         ws: bool,
471
         ws: bool,
474
     ) -> bool {
472
     ) -> bool {
475
-        if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
473
+        if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(bytes) {
476
             match msg_in.union {
474
             match msg_in.union {
477
                 Some(rendezvous_message::Union::PunchHoleRequest(ph)) => {
475
                 Some(rendezvous_message::Union::PunchHoleRequest(ph)) => {
478
                     // there maybe several attempt, so sink can be none
476
                     // there maybe several attempt, so sink can be none
479
                     if let Some(sink) = sink.take() {
477
                     if let Some(sink) = sink.take() {
480
                         self.tcp_punch.lock().await.insert(addr, sink);
478
                         self.tcp_punch.lock().await.insert(addr, sink);
481
                     }
479
                     }
482
-                    allow_err!(self.handle_tcp_punch_hole_request(addr, ph, &key, ws).await);
480
+                    allow_err!(self.handle_tcp_punch_hole_request(addr, ph, key, ws).await);
483
                     return true;
481
                     return true;
484
                 }
482
                 }
485
                 Some(rendezvous_message::Union::RequestRelay(mut rf)) => {
483
                 Some(rendezvous_message::Union::RequestRelay(mut rf)) => {
@@ -492,7 +490,7 @@ impl RendezvousServer {
492
                         rf.socket_addr = AddrMangle::encode(addr).into();
490
                         rf.socket_addr = AddrMangle::encode(addr).into();
493
                         msg_out.set_request_relay(rf);
491
                         msg_out.set_request_relay(rf);
494
                         let peer_addr = peer.read().await.socket_addr;
492
                         let peer_addr = peer.read().await.socket_addr;
495
-                        self.tx.send(Data::Msg(msg_out, peer_addr)).ok();
493
+                        self.tx.send(Data::Msg(msg_out.into(), peer_addr)).ok();
496
                     }
494
                     }
497
                     return true;
495
                     return true;
498
                 }
496
                 }
@@ -747,14 +745,14 @@ impl RendezvousServer {
747
                     ..Default::default()
745
                     ..Default::default()
748
                 });
746
                 });
749
             }
747
             }
750
-            return Ok((msg_out, Some(peer_addr)));
748
+            Ok((msg_out, Some(peer_addr)))
751
         } else {
749
         } else {
752
             let mut msg_out = RendezvousMessage::new();
750
             let mut msg_out = RendezvousMessage::new();
753
             msg_out.set_punch_hole_response(PunchHoleResponse {
751
             msg_out.set_punch_hole_response(PunchHoleResponse {
754
                 failure: punch_hole_response::Failure::ID_NOT_EXIST.into(),
752
                 failure: punch_hole_response::Failure::ID_NOT_EXIST.into(),
755
                 ..Default::default()
753
                 ..Default::default()
756
             });
754
             });
757
-            return Ok((msg_out, None));
755
+            Ok((msg_out, None))
758
         }
756
         }
759
     }
757
     }
760
 
758
 
@@ -765,8 +763,8 @@ impl RendezvousServer {
765
         peers: Vec<String>,
763
         peers: Vec<String>,
766
     ) -> ResultType<()> {
764
     ) -> ResultType<()> {
767
         let mut states = BytesMut::zeroed((peers.len() + 7) / 8);
765
         let mut states = BytesMut::zeroed((peers.len() + 7) / 8);
768
-        for i in 0..peers.len() {
769
-            if let Some(peer) = self.pm.get_in_memory(&peers[i]).await {
766
+        for (i, peer_id) in peers.iter().enumerate() {
767
+            if let Some(peer) = self.pm.get_in_memory(peer_id).await {
770
                 let elapsed = peer.read().await.last_reg_time.elapsed().as_millis() as i32;
768
                 let elapsed = peer.read().await.last_reg_time.elapsed().as_millis() as i32;
771
                 // bytes index from left to right
769
                 // bytes index from left to right
772
                 let states_idx = i / 8;
770
                 let states_idx = i / 8;
@@ -832,7 +830,7 @@ impl RendezvousServer {
832
     ) -> ResultType<()> {
830
     ) -> ResultType<()> {
833
         let (msg, to_addr) = self.handle_punch_hole_request(addr, ph, key, ws).await?;
831
         let (msg, to_addr) = self.handle_punch_hole_request(addr, ph, key, ws).await?;
834
         if let Some(addr) = to_addr {
832
         if let Some(addr) = to_addr {
835
-            self.tx.send(Data::Msg(msg, addr))?;
833
+            self.tx.send(Data::Msg(msg.into(), addr))?;
836
         } else {
834
         } else {
837
             self.send_to_tcp_sync(msg, addr).await?;
835
             self.send_to_tcp_sync(msg, addr).await?;
838
         }
836
         }
@@ -848,7 +846,7 @@ impl RendezvousServer {
848
     ) -> ResultType<()> {
846
     ) -> ResultType<()> {
849
         let (msg, to_addr) = self.handle_punch_hole_request(addr, ph, key, false).await?;
847
         let (msg, to_addr) = self.handle_punch_hole_request(addr, ph, key, false).await?;
850
         self.tx.send(Data::Msg(
848
         self.tx.send(Data::Msg(
851
-            msg,
849
+            msg.into(),
852
             match to_addr {
850
             match to_addr {
853
                 Some(addr) => addr,
851
                 Some(addr) => addr,
854
                 None => addr,
852
                 None => addr,
@@ -907,8 +905,10 @@ impl RendezvousServer {
907
     }
905
     }
908
 
906
 
909
     async fn check_cmd(&self, cmd: &str) -> String {
907
     async fn check_cmd(&self, cmd: &str) -> String {
908
+        use std::fmt::Write as _;
909
+
910
         let mut res = "".to_owned();
910
         let mut res = "".to_owned();
911
-        let mut fds = cmd.trim().split(" ");
911
+        let mut fds = cmd.trim().split(' ');
912
         match fds.next() {
912
         match fds.next() {
913
             Some("h") => {
913
             Some("h") => {
914
                 res = format!(
914
                 res = format!(
@@ -926,7 +926,7 @@ impl RendezvousServer {
926
                     self.tx.send(Data::RelayServers0(rs.to_owned())).ok();
926
                     self.tx.send(Data::RelayServers0(rs.to_owned())).ok();
927
                 } else {
927
                 } else {
928
                     for ip in self.relay_servers.iter() {
928
                     for ip in self.relay_servers.iter() {
929
-                        res += &format!("{}\n", ip);
929
+                        let _ = writeln!(res, "{ip}");
930
                     }
930
                     }
931
                 }
931
                 }
932
             }
932
             }
@@ -942,8 +942,9 @@ impl RendezvousServer {
942
                 if start < 0 {
942
                 if start < 0 {
943
                     if let Some(ip) = ip {
943
                     if let Some(ip) = ip {
944
                         if let Some((a, b)) = lock.get(ip) {
944
                         if let Some((a, b)) = lock.get(ip) {
945
-                            res += &format!(
946
-                                "{}/{}s {}/{}s\n",
945
+                            let _ = writeln!(
946
+                                res,
947
+                                "{}/{}s {}/{}s",
947
                                 a.0,
948
                                 a.0,
948
                                 a.1.elapsed().as_secs(),
949
                                 a.1.elapsed().as_secs(),
949
                                 b.0.len(),
950
                                 b.0.len(),
@@ -968,8 +969,9 @@ impl RendezvousServer {
968
                             continue;
969
                             continue;
969
                         }
970
                         }
970
                         if let Some((ip, (a, b))) = x {
971
                         if let Some((ip, (a, b))) = x {
971
-                            res += &format!(
972
-                                "{}: {}/{}s {}/{}s\n",
972
+                            let _ = writeln!(
973
+                                res,
974
+                                "{}: {}/{}s {}/{}s",
973
                                 ip,
975
                                 ip,
974
                                 a.0,
976
                                 a.0,
975
                                 a.1.elapsed().as_secs(),
977
                                 a.1.elapsed().as_secs(),
@@ -986,10 +988,10 @@ impl RendezvousServer {
986
                 res = format!("{}\n", lock.len());
988
                 res = format!("{}\n", lock.len());
987
                 let id = fds.next();
989
                 let id = fds.next();
988
                 let mut start = id.map(|x| x.parse::<i32>().unwrap_or(-1)).unwrap_or(-1);
990
                 let mut start = id.map(|x| x.parse::<i32>().unwrap_or(-1)).unwrap_or(-1);
989
-                if start < 0 || start > 10_000_000 {
991
+                if !(0..=10_000_000).contains(&start) {
990
                     if let Some(id) = id {
992
                     if let Some(id) = id {
991
                         if let Some((tm, ips)) = lock.get(id) {
993
                         if let Some((tm, ips)) = lock.get(id) {
992
-                            res += &format!("{}s {:?}\n", tm.elapsed().as_secs(), ips);
994
+                            let _ = writeln!(res, "{}s {:?}", tm.elapsed().as_secs(), ips);
993
                         }
995
                         }
994
                         if fds.next() == Some("-") {
996
                         if fds.next() == Some("-") {
995
                             lock.remove(id);
997
                             lock.remove(id);
@@ -1009,7 +1011,7 @@ impl RendezvousServer {
1009
                             continue;
1011
                             continue;
1010
                         }
1012
                         }
1011
                         if let Some((id, (tm, ips))) = x {
1013
                         if let Some((id, (tm, ips))) = x {
1012
-                            res += &format!("{}: {}s {:?}\n", id, tm.elapsed().as_secs(), ips,);
1014
+                            let _ = writeln!(res, "{}: {}s {:?}", id, tm.elapsed().as_secs(), ips,);
1013
                         }
1015
                         }
1014
                     }
1016
                     }
1015
                 }
1017
                 }
@@ -1023,7 +1025,7 @@ impl RendezvousServer {
1023
                     }
1025
                     }
1024
                     self.tx.send(Data::RelayServers0(rs.to_owned())).ok();
1026
                     self.tx.send(Data::RelayServers0(rs.to_owned())).ok();
1025
                 } else {
1027
                 } else {
1026
-                    res += &format!("ALWAYS_USE_RELAY: {:?}\n", unsafe { ALWAYS_USE_RELAY });
1028
+                    let _ = writeln!(res, "ALWAYS_USE_RELAY: {:?}", unsafe { ALWAYS_USE_RELAY });
1027
                 }
1029
                 }
1028
             }
1030
             }
1029
             Some("test-geo" | "tg") => {
1031
             Some("test-geo" | "tg") => {
@@ -1106,13 +1108,10 @@ impl RendezvousServer {
1106
             let (a, mut b) = ws_stream.split();
1108
             let (a, mut b) = ws_stream.split();
1107
             sink = Some(Sink::Ws(a));
1109
             sink = Some(Sink::Ws(a));
1108
             while let Ok(Some(Ok(msg))) = timeout(30_000, b.next()).await {
1110
             while let Ok(Some(Ok(msg))) = timeout(30_000, b.next()).await {
1109
-                match msg {
1110
-                    tungstenite::Message::Binary(bytes) => {
1111
-                        if !self.handle_tcp(&bytes, &mut sink, addr, key, ws).await {
1112
-                            break;
1113
-                        }
1111
+                if let tungstenite::Message::Binary(bytes) = msg {
1112
+                    if !self.handle_tcp(&bytes, &mut sink, addr, key, ws).await {
1113
+                        break;
1114
                     }
1114
                     }
1115
-                    _ => {}
1116
                 }
1115
                 }
1117
             }
1116
             }
1118
         } else {
1117
         } else {
@@ -1138,7 +1137,7 @@ impl RendezvousServer {
1138
         } else {
1137
         } else {
1139
             match self.pm.get(&id).await {
1138
             match self.pm.get(&id).await {
1140
                 Some(peer) => {
1139
                 Some(peer) => {
1141
-                    let pk = peer.read().await.pk.clone().into();
1140
+                    let pk = peer.read().await.pk.clone();
1142
                     sign::sign(
1141
                     sign::sign(
1143
                         &hbb_common::message_proto::IdPk {
1142
                         &hbb_common::message_proto::IdPk {
1144
                             id,
1143
                             id,
@@ -1147,7 +1146,7 @@ impl RendezvousServer {
1147
                         }
1146
                         }
1148
                         .write_to_bytes()
1147
                         .write_to_bytes()
1149
                         .unwrap_or_default(),
1148
                         .unwrap_or_default(),
1150
-                        &self.inner.sk.as_ref().unwrap(),
1149
+                        self.inner.sk.as_ref().unwrap(),
1151
                     )
1150
                     )
1152
                     .into()
1151
                     .into()
1153
                 }
1152
                 }
@@ -1203,7 +1202,7 @@ async fn check_relay_servers(rs0: Arc<RelayServers>, tx: Sender) {
1203
     let rs = Arc::new(Mutex::new(Vec::new()));
1202
     let rs = Arc::new(Mutex::new(Vec::new()));
1204
     for x in rs0.iter() {
1203
     for x in rs0.iter() {
1205
         let mut host = x.to_owned();
1204
         let mut host = x.to_owned();
1206
-        if !host.contains(":") {
1205
+        if !host.contains(':') {
1207
             host = format!("{}:{}", host, hbb_common::config::RELAY_PORT);
1206
             host = format!("{}:{}", host, hbb_common::config::RELAY_PORT);
1208
         }
1207
         }
1209
         let rs = rs.clone();
1208
         let rs = rs.clone();
@@ -1219,7 +1218,7 @@ async fn check_relay_servers(rs0: Arc<RelayServers>, tx: Sender) {
1219
     }
1218
     }
1220
     join_all(futs).await;
1219
     join_all(futs).await;
1221
     log::debug!("check_relay_servers");
1220
     log::debug!("check_relay_servers");
1222
-    let rs = std::mem::replace(&mut *rs.lock().await, Default::default());
1221
+    let rs = std::mem::take(&mut *rs.lock().await);
1223
     if !rs.is_empty() {
1222
     if !rs.is_empty() {
1224
         tx.send(Data::RelayServers(rs)).ok();
1223
         tx.send(Data::RelayServers(rs)).ok();
1225
     }
1224
     }

+ 12 - 14
src/utils.rs

@@ -33,7 +33,7 @@ fn gen_keypair() {
33
 }
33
 }
34
 
34
 
35
 fn validate_keypair(pk: &str, sk: &str) -> ResultType<()> {
35
 fn validate_keypair(pk: &str, sk: &str) -> ResultType<()> {
36
-    let sk1 = base64::decode(&sk);
36
+    let sk1 = base64::decode(sk);
37
     if sk1.is_err() {
37
     if sk1.is_err() {
38
         bail!("Invalid secret key");
38
         bail!("Invalid secret key");
39
     }
39
     }
@@ -45,7 +45,7 @@ fn validate_keypair(pk: &str, sk: &str) -> ResultType<()> {
45
     }
45
     }
46
     let secret_key = secret_key.unwrap();
46
     let secret_key = secret_key.unwrap();
47
 
47
 
48
-    let pk1 = base64::decode(&pk);
48
+    let pk1 = base64::decode(pk);
49
     if pk1.is_err() {
49
     if pk1.is_err() {
50
         bail!("Invalid public key");
50
         bail!("Invalid public key");
51
     }
51
     }
@@ -96,14 +96,13 @@ fn doctor_ip(server_ip_address: std::net::IpAddr, server_address: Option<&str>)
96
     // reverse dns lookup
96
     // reverse dns lookup
97
     // TODO: (check) doesn't seem to do reverse lookup on OSX...
97
     // TODO: (check) doesn't seem to do reverse lookup on OSX...
98
     let reverse = lookup_addr(&server_ip_address).unwrap();
98
     let reverse = lookup_addr(&server_ip_address).unwrap();
99
-    if server_address.is_some() {
100
-        if reverse == server_address.unwrap() {
99
+    if let Some(server_address) = server_address {
100
+        if reverse == server_address {
101
             println!("Reverse DNS lookup: '{}' MATCHES server address", reverse);
101
             println!("Reverse DNS lookup: '{}' MATCHES server address", reverse);
102
         } else {
102
         } else {
103
             println!(
103
             println!(
104
                 "Reverse DNS lookup: '{}' DOESN'T MATCH server address '{}'",
104
                 "Reverse DNS lookup: '{}' DOESN'T MATCH server address '{}'",
105
-                reverse,
106
-                server_address.unwrap()
105
+                reverse, server_address
107
             );
106
             );
108
         }
107
         }
109
     }
108
     }
@@ -126,19 +125,18 @@ fn doctor(server_address_unclean: &str) {
126
     let server_address2 = server_address3.to_lowercase();
125
     let server_address2 = server_address3.to_lowercase();
127
     let server_address = server_address2.as_str();
126
     let server_address = server_address2.as_str();
128
     println!("Checking server:  {}\n", server_address);
127
     println!("Checking server:  {}\n", server_address);
129
-    let server_ipaddr = server_address.parse::<IpAddr>();
130
-    if server_ipaddr.is_err() {
128
+    if let Ok(server_ipaddr) = server_address.parse::<IpAddr>() {
129
+        // user requested an ip address
130
+        doctor_ip(server_ipaddr, None);
131
+    } else {
131
         // the passed string is not an ip address
132
         // the passed string is not an ip address
132
         let ips: Vec<std::net::IpAddr> = lookup_host(server_address).unwrap();
133
         let ips: Vec<std::net::IpAddr> = lookup_host(server_address).unwrap();
133
-        println!("Found {} IP addresses: ", ips.iter().count());
134
+        println!("Found {} IP addresses: ", ips.len());
134
 
135
 
135
         ips.iter().for_each(|ip| println!(" - {ip}"));
136
         ips.iter().for_each(|ip| println!(" - {ip}"));
136
 
137
 
137
-        ips.iter().for_each(|ip| doctor_ip(*ip, Some(server_address)));
138
-
139
-    } else {
140
-        // user requested an ip address
141
-        doctor_ip(server_ipaddr.unwrap(), None);
138
+        ips.iter()
139
+            .for_each(|ip| doctor_ip(*ip, Some(server_address)));
142
     }
140
     }
143
 }
141
 }
144
 
142
 

+ 2 - 1
src/version.rs

@@ -1,2 +1,3 @@
1
 pub const VERSION: &str = "1.1.6";
1
 pub const VERSION: &str = "1.1.6";
2
-pub const BUILD_DATE: &str = "2023-01-06 11:03";
2
+#[allow(dead_code)]
3
+pub const BUILD_DATE: &str = "2023-01-06 18:48";