Browse Source

rendezvous_servers

open-trade 5 years ago
parent
commit
33accfb2ca
5 changed files with 118 additions and 16 deletions
  1. 46 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 1 1
      libs/hbb_common
  4. 49 11
      src/main.rs
  5. 21 4
      src/rendezvous_server.rs

+ 46 - 0
Cargo.lock

@@ -1,5 +1,10 @@
1 1
 # This file is automatically @generated by Cargo.
2 2
 # It is not intended for manual editing.
3
+[[package]]
4
+name = "ahash"
5
+version = "0.3.8"
6
+source = "registry+https://github.com/rust-lang/crates.io-index"
7
+
3 8
 [[package]]
4 9
 name = "aho-corasick"
5 10
 version = "0.7.13"
@@ -298,6 +303,14 @@ dependencies = [
298 303
  "libloading 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
299 304
 ]
300 305
 
306
+[[package]]
307
+name = "dlv-list"
308
+version = "0.2.2"
309
+source = "registry+https://github.com/rust-lang/crates.io-index"
310
+dependencies = [
311
+ "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
312
+]
313
+
301 314
 [[package]]
302 315
 name = "downcast-rs"
303 316
 version = "1.2.0"
@@ -481,6 +494,15 @@ name = "glob"
481 494
 version = "0.3.0"
482 495
 source = "registry+https://github.com/rust-lang/crates.io-index"
483 496
 
497
+[[package]]
498
+name = "hashbrown"
499
+version = "0.7.2"
500
+source = "registry+https://github.com/rust-lang/crates.io-index"
501
+dependencies = [
502
+ "ahash 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
503
+ "autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
504
+]
505
+
484 506
 [[package]]
485 507
 name = "hbb_common"
486 508
 version = "0.1.0"
@@ -521,6 +543,7 @@ dependencies = [
521 543
  "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
522 544
  "hbb_common 0.1.0",
523 545
  "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
546
+ "rust-ini 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)",
524 547
  "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)",
525 548
  "serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)",
526 549
  "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -840,6 +863,15 @@ dependencies = [
840 863
  "num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
841 864
 ]
842 865
 
866
+[[package]]
867
+name = "ordered-multimap"
868
+version = "0.2.4"
869
+source = "registry+https://github.com/rust-lang/crates.io-index"
870
+dependencies = [
871
+ "dlv-list 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
872
+ "hashbrown 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
873
+]
874
+
843 875
 [[package]]
844 876
 name = "parking_lot"
845 877
 version = "0.10.2"
@@ -1118,6 +1150,15 @@ dependencies = [
1118 1150
  "crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
1119 1151
 ]
1120 1152
 
1153
+[[package]]
1154
+name = "rust-ini"
1155
+version = "0.15.3"
1156
+source = "registry+https://github.com/rust-lang/crates.io-index"
1157
+dependencies = [
1158
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
1159
+ "ordered-multimap 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
1160
+]
1161
+
1121 1162
 [[package]]
1122 1163
 name = "rustls"
1123 1164
 version = "0.17.0"
@@ -1748,6 +1789,7 @@ dependencies = [
1748 1789
 ]
1749 1790
 
1750 1791
 [metadata]
1792
+"checksum ahash 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217"
1751 1793
 "checksum aho-corasick 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)" = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
1752 1794
 "checksum andrew 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7f09f89872c2b6b29e319377b1fbe91c6f5947df19a25596e121cf19a7b35e"
1753 1795
 "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
@@ -1786,6 +1828,7 @@ dependencies = [
1786 1828
 "checksum dirs-sys 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
1787 1829
 "checksum dirs-sys-next 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c60f7b8a8953926148223260454befb50c751d3c50e1c178c4fd1ace4083c9a"
1788 1830
 "checksum dlib 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b11f15d1e3268f140f68d390637d5e76d849782d971ae7063e0da69fe9709a76"
1831
+"checksum dlv-list 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1b391911b9a786312a10cb9d2b3d0735adfd5a8113eb3648de26a75e91b0826c"
1789 1832
 "checksum downcast-rs 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
1790 1833
 "checksum either 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f"
1791 1834
 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
@@ -1807,6 +1850,7 @@ dependencies = [
1807 1850
 "checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
1808 1851
 "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
1809 1852
 "checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
1853
+"checksum hashbrown 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96282e96bfcd3da0d3aa9938bedf1e50df3269b6db08b4876d2da0bb1a0841cf"
1810 1854
 "checksum hermit-abi 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
1811 1855
 "checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
1812 1856
 "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
@@ -1844,6 +1888,7 @@ dependencies = [
1844 1888
 "checksum once_cell 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
1845 1889
 "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
1846 1890
 "checksum ordered-float 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3741934be594d77de1c8461ebcbbe866f585ea616a9753aa78f2bdc69f0e4579"
1891
+"checksum ordered-multimap 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e88f947c6799d5eff50e6cf8a2365c17ac4aa8f8f43aceeedc29b616d872a358"
1847 1892
 "checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e"
1848 1893
 "checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3"
1849 1894
 "checksum pin-project 0.4.23 (registry+https://github.com/rust-lang/crates.io-index)" = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa"
@@ -1876,6 +1921,7 @@ dependencies = [
1876 1921
 "checksum regex-syntax 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)" = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
1877 1922
 "checksum ring 0.16.15 (registry+https://github.com/rust-lang/crates.io-index)" = "952cd6b98c85bbc30efa1ba5783b8abf12fec8b3287ffa52605b9432313e34e4"
1878 1923
 "checksum rust-argon2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9dab61250775933275e84053ac235621dfb739556d5c54a2f2e9313b7cf43a19"
1924
+"checksum rust-ini 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7a3679dd538c876a7b606f3bb951c8a20fc281a0ff7795f59f7cb490e3f979e1"
1879 1925
 "checksum rustls 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0d4a31f5d68413404705d6982529b0e11a9aacd4839d1d6222ee3b8cb4015e1"
1880 1926
 "checksum rustls-native-certs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a75ffeb84a6bd9d014713119542ce415db3a3e4748f0bfce1e1416cd224a23a5"
1881 1927
 "checksum rusttype 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)" = "310942406a39981bed7e12b09182a221a29e0990f3e7e0c971f131922ed135d5"

+ 1 - 0
Cargo.toml

@@ -18,6 +18,7 @@ serde = "1.0"
18 18
 serde_json = "1.0"
19 19
 lazy_static = "1.4"
20 20
 clap = "2.33"
21
+rust-ini = "0.15"
21 22
 
22 23
 [workspace]
23 24
 members = ["libs/hbb_common"]

+ 1 - 1
libs/hbb_common

@@ -1 +1 @@
1
-Subproject commit 4b0de359f90425ce9c72903b7a9e5fcbe505a5cc
1
+Subproject commit 0a95f37bb329b43ab6ac7c1b1753e39e10bc3c74

+ 49 - 11
src/main.rs

@@ -4,14 +4,18 @@
4 4
 use clap::App;
5 5
 use hbb_common::{env_logger::*, log, tokio, ResultType};
6 6
 use hbbs::*;
7
+use ini::Ini;
7 8
 const DEFAULT_PORT: &'static str = "21116";
8 9
 
9 10
 #[tokio::main]
10 11
 async fn main() -> ResultType<()> {
11 12
     init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
12 13
     let args = format!(
13
-        "-p, --port=[default={}] 'Sets the listening port'
14
-    -r, --relay-server=[] 'Sets the default relay server'",
14
+        "-c --config=[FILE] +takes_value 'Sets a custom config file'
15
+        -p, --port=[NUMBER(default={})] 'Sets the listening port'
16
+        -s, --serial=[NUMBER(default={0})] 'Sets configure update serial number'
17
+        -R, --rendezvous-servers=[HOSTS] 'Sets rendezvous servers, seperated by colon'
18
+    -r, --relay-server=[HOST] 'Sets the default relay server'",
15 19
         DEFAULT_PORT
16 20
     );
17 21
     let matches = App::new("hbbs")
@@ -20,15 +24,49 @@ async fn main() -> ResultType<()> {
20 24
         .about("RustDesk Rendezvous Server")
21 25
         .args_from_usage(&args)
22 26
         .get_matches();
23
-    let addr = format!(
24
-        "0.0.0.0:{}",
25
-        matches.value_of("port").unwrap_or(DEFAULT_PORT)
26
-    );
27
+    let mut section = None;
28
+    let conf; // for holding section
29
+    if let Some(config) = matches.value_of("config") {
30
+        if let Ok(v) = Ini::load_from_file(config) {
31
+            conf = v;
32
+            section = conf.section(None::<String>);
33
+        }
34
+    }
35
+    let get_arg = |name: &str, default: &str| -> String {
36
+        if let Some(v) = matches.value_of(name) {
37
+            return v.to_owned();
38
+        } else if let Some(section) = section {
39
+            if let Some(v) = section.get(name) {
40
+                return v.to_owned();
41
+            }
42
+        }
43
+        return default.to_owned();
44
+    };
45
+    let port = get_arg("port", DEFAULT_PORT);
46
+    let mut relay_server = get_arg("relay-server", "");
47
+    if !relay_server.contains(":") {
48
+        relay_server = format!("{}:21117", relay_server);
49
+    }
50
+    if !relay_server.parse::<std::net::SocketAddr>().is_ok() {
51
+        relay_server = "".to_owned();
52
+    }
53
+    let serial: i32 = get_arg("serial", "").parse().unwrap_or(0);
54
+    let rendezvous_servers: Vec<String> = get_arg("rendezvous-servers", "")
55
+        .split(",")
56
+        .map(|x| {
57
+            if !x.contains(":") {
58
+                format!("{}:21116", x)
59
+            } else {
60
+                x.to_owned()
61
+            }
62
+        })
63
+        .filter(|x| x.parse::<std::net::SocketAddr>().is_ok())
64
+        .collect();
65
+    let addr = format!("0.0.0.0:{}", port);
27 66
     log::info!("Listening on {}", addr);
28
-    RendezvousServer::start(
29
-        &addr,
30
-        matches.value_of("relay-server").unwrap_or("").to_owned(),
31
-    )
32
-    .await?;
67
+    log::info!("relay-server={}", relay_server);
68
+    log::info!("serial={}", serial);
69
+    log::info!("rendzvous-servers={:?}", rendezvous_servers);
70
+    RendezvousServer::start(&addr, relay_server, serial, rendezvous_servers).await?;
33 71
     Ok(())
34 72
 }

+ 21 - 4
src/rendezvous_server.rs

@@ -126,10 +126,17 @@ pub struct RendezvousServer {
126 126
     pm: PeerMap,
127 127
     tx: Sender,
128 128
     relay_server: String,
129
+    serial: i32,
130
+    rendezvous_servers: Vec<String>,
129 131
 }
130 132
 
131 133
 impl RendezvousServer {
132
-    pub async fn start(addr: &str, relay_server: String) -> ResultType<()> {
134
+    pub async fn start(
135
+        addr: &str,
136
+        relay_server: String,
137
+        serial: i32,
138
+        rendezvous_servers: Vec<String>,
139
+    ) -> ResultType<()> {
133 140
         let mut socket = FramedSocket::new(addr).await?;
134 141
         let (tx, mut rx) = mpsc::unbounded_channel::<(RendezvousMessage, SocketAddr)>();
135 142
         let mut rs = Self {
@@ -137,6 +144,8 @@ impl RendezvousServer {
137 144
             pm: PeerMap::new()?,
138 145
             tx: tx.clone(),
139 146
             relay_server,
147
+            serial,
148
+            rendezvous_servers,
140 149
         };
141 150
         let mut listener = new_listener(addr, true).await?;
142 151
         loop {
@@ -219,6 +228,17 @@ impl RendezvousServer {
219 228
                     if rp.id.len() > 0 {
220 229
                         log::debug!("New peer registered: {:?} {:?}", &rp.id, &addr);
221 230
                         self.update_addr(rp.id, addr, socket).await?;
231
+                        if self.serial != rp.serial {
232
+                            let mut msg_out = RendezvousMessage::new();
233
+                            let mut mi = MiscInfo::new();
234
+                            mi.set_configure_update(ConfigUpdate {
235
+                                serial: self.serial,
236
+                                rendezvous_servers: self.rendezvous_servers.clone(),
237
+                                ..Default::default()
238
+                            });
239
+                            msg_out.set_misc_info(mi);
240
+                            socket.send(&msg_out, addr).await?;
241
+                        }
222 242
                     }
223 243
                 }
224 244
                 Some(rendezvous_message::Union::register_pk(rk)) => {
@@ -267,9 +287,6 @@ impl RendezvousServer {
267 287
                 Some(rendezvous_message::Union::local_addr(la)) => {
268 288
                     self.handle_local_addr(&la, addr, Some(socket)).await?;
269 289
                 }
270
-                Some(rendezvous_message::Union::system_info(info)) => {
271
-                    log::info!("{}", info.value);
272
-                }
273 290
                 _ => {}
274 291
             }
275 292
         }