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

move proto and V4Mangle to hbb_common

open-trade лет назад: 5
Родитель
Сommit
d5e55b33d5
8 измененных файлов с 24 добавлено и 86 удалено
  1. 3 0
      .gitmodules
  2. 9 2
      Cargo.lock
  3. 4 3
      Cargo.toml
  4. 0 10
      build.rs
  5. 1 0
      libs/hbb_common
  6. 0 22
      protos/message.proto
  7. 0 3
      src/lib.rs
  8. 7 46
      src/rendezvous_server.rs

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
1
+[submodule "libs/hbb_common"]
2
+	path = libs/hbb_common
3
+	url = https://github.com/open-trade/hbb_common

+ 9 - 2
Cargo.lock

@@ -151,6 +151,14 @@ dependencies = [
151 151
  "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
152 152
 ]
153 153
 
154
+[[package]]
155
+name = "hbb_common"
156
+version = "0.1.0"
157
+dependencies = [
158
+ "protobuf 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
159
+ "protobuf-codegen-pure 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
160
+]
161
+
154 162
 [[package]]
155 163
 name = "hbbs"
156 164
 version = "0.1.0"
@@ -158,9 +166,8 @@ dependencies = [
158 166
  "bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
159 167
  "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
160 168
  "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
169
+ "hbb_common 0.1.0",
161 170
  "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
162
- "protobuf 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
163
- "protobuf-codegen-pure 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
164 171
  "simple-error 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
165 172
  "tokio 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
166 173
  "tokio-util 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",

+ 4 - 3
Cargo.toml

@@ -8,13 +8,14 @@ edition = "2018"
8 8
 
9 9
 [dependencies]
10 10
 tokio = { version = "0.2", features = ["full"] }
11
-protobuf = "2.10"
12 11
 tokio-util = { version = "0.3", features = ["full"] }
13 12
 log = "0.4"
14 13
 env_logger = "0.7"
15 14
 futures = "0.3"
16 15
 bytes = "0.5"
17 16
 simple-error = "0.2"
17
+hbb_common = { path = "libs/hbb_common" }
18
+
19
+[workspace]
20
+members = ['libs/hbb_common']
18 21
 
19
-[build-dependencies]
20
-protobuf-codegen-pure = "2.10"

+ 0 - 10
build.rs

@@ -1,10 +0,0 @@
1
-fn main() {
2
-    protobuf_codegen_pure::run(protobuf_codegen_pure::Args {
3
-        out_dir: "src/protos",
4
-        input: &["protos/message.proto"],
5
-        includes: &["protos"],
6
-        customize: protobuf_codegen_pure::Customize {
7
-            ..Default::default()
8
-        },
9
-    }).unwrap();
10
-}

+ 1 - 0
libs/hbb_common

@@ -0,0 +1 @@
1
+Subproject commit 938076d06af0efa532ce2372a051ae69f2136720

+ 0 - 22
protos/message.proto

@@ -1,22 +0,0 @@
1
-syntax = "proto3";
2
-package hbb;
3
-
4
-message RegisterPeer {
5
-  string hbb_addr = 1;
6
-}
7
-
8
-message PeekPeer {
9
-  string hbb_addr = 1;
10
-}
11
-
12
-message PeekPeerResponse {
13
-  bytes socket_addr = 1;
14
-}
15
-
16
-message Message {
17
-  oneof union {
18
-    RegisterPeer register_peer = 6;
19
-    PeekPeer peek_peer = 7;
20
-    PeekPeerResponse peek_peer_response = 8;
21
-  }
22
-}

+ 0 - 3
src/lib.rs

@@ -1,5 +1,2 @@
1 1
 mod rendezvous_server;
2 2
 pub use rendezvous_server::*;
3
-#[path = "./protos/message.rs"]
4
-mod message_proto;
5
-pub use message_proto::*;

+ 7 - 46
src/rendezvous_server.rs

@@ -1,53 +1,19 @@
1
-use super::message_proto::*;
2 1
 use bytes::{Bytes, BytesMut};
3 2
 use futures::SinkExt;
4
-use protobuf::{parse_from_bytes, Message as _};
3
+use hbb_common::{
4
+    message_proto::*,
5
+    protobuf::{parse_from_bytes, Message as _},
6
+    V4AddrMangle,
7
+};
5 8
 use std::{
6 9
     collections::HashMap,
7 10
     error::Error,
8
-    net::{Ipv4Addr, SocketAddr, SocketAddrV4},
9
-    time::{Duration, SystemTime, UNIX_EPOCH},
11
+    net::{SocketAddr, SocketAddrV4},
12
+    time::Duration,
10 13
 };
11 14
 use tokio::{net::UdpSocket, stream::StreamExt, time::delay_for};
12 15
 use tokio_util::{codec::BytesCodec, udp::UdpFramed};
13 16
 
14
-/// Certain router and firewalls scan the packet and if they
15
-/// find an IP address belonging to their pool that they use to do the NAT mapping/translation, so here we mangle the ip address
16
-
17
-pub struct V4AddrMangle();
18
-
19
-impl V4AddrMangle {
20
-    pub fn encode(addr: &SocketAddrV4) -> Vec<u8> {
21
-        let tm = (SystemTime::now()
22
-            .duration_since(UNIX_EPOCH)
23
-            .unwrap()
24
-            .as_micros() as u32) as u128;
25
-        let ip = u32::from_ne_bytes(addr.ip().octets()) as u128;
26
-        let port = addr.port() as u128;
27
-        let v = ((ip + tm) << 49) | (tm << 17) | (port + (tm & 0xFFFF));
28
-        let bytes = v.to_ne_bytes();
29
-        let mut n_padding = 0;
30
-        for i in bytes.iter().rev() {
31
-            if i == &0u8 {
32
-                n_padding += 1;
33
-            } else {
34
-                break;
35
-            }
36
-        }
37
-        bytes[..(16 - n_padding)].to_vec()
38
-    }
39
-
40
-    pub fn decode(bytes: &[u8]) -> SocketAddrV4 {
41
-        let mut padded = [0u8; 16];
42
-        padded[..bytes.len()].copy_from_slice(&bytes);
43
-        let number = u128::from_ne_bytes(padded);
44
-        let tm = (number >> 17) & (u32::max_value() as u128);
45
-        let ip = (((number >> 49) - tm) as u32).to_ne_bytes();
46
-        let port = (number & 0xFFFFFF) - (tm & 0xFFFF);
47
-        SocketAddrV4::new(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]), port as u16)
48
-    }
49
-}
50
-
51 17
 pub struct Peer {
52 18
     socket_addr: SocketAddrV4,
53 19
 }
@@ -127,11 +93,6 @@ pub async fn sleep(sec: f32) {
127 93
 #[cfg(test)]
128 94
 mod tests {
129 95
     use super::*;
130
-    #[test]
131
-    fn test_mangle() {
132
-        let addr = SocketAddrV4::new(Ipv4Addr::new(192, 168, 16, 32), 21116);
133
-        assert_eq!(addr, V4AddrMangle::decode(&V4AddrMangle::encode(&addr)[..]));
134
-    }
135 96
 
136 97
     #[allow(unused_must_use)]
137 98
     #[tokio::main]