diff options
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | build.rs | 10 | ||||
m--------- | libs/hbb_common | 0 | ||||
-rw-r--r-- | protos/message.proto | 22 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/rendezvous_server.rs | 53 |
8 files changed, 23 insertions, 86 deletions
diff --git a/.gitmodules b/.gitmodules index e69de29..b4224d1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/hbb_common"] + path = libs/hbb_common + url = https://github.com/open-trade/hbb_common @@ -152,15 +152,22 @@ dependencies = [ ] [[package]] +name = "hbb_common" +version = "0.1.0" +dependencies = [ + "protobuf 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "protobuf-codegen-pure 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "hbbs" version = "0.1.0" dependencies = [ "bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "hbb_common 0.1.0", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf-codegen-pure 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "simple-error 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-util 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -8,13 +8,14 @@ edition = "2018" [dependencies] tokio = { version = "0.2", features = ["full"] } -protobuf = "2.10" tokio-util = { version = "0.3", features = ["full"] } log = "0.4" env_logger = "0.7" futures = "0.3" bytes = "0.5" simple-error = "0.2" +hbb_common = { path = "libs/hbb_common" } + +[workspace] +members = ['libs/hbb_common'] -[build-dependencies] -protobuf-codegen-pure = "2.10" diff --git a/build.rs b/build.rs deleted file mode 100644 index 5eae926..0000000 --- a/build.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main() { - protobuf_codegen_pure::run(protobuf_codegen_pure::Args { - out_dir: "src/protos", - input: &["protos/message.proto"], - includes: &["protos"], - customize: protobuf_codegen_pure::Customize { - ..Default::default() - }, - }).unwrap(); -} diff --git a/libs/hbb_common b/libs/hbb_common new file mode 160000 +Subproject 938076d06af0efa532ce2372a051ae69f213672 diff --git a/protos/message.proto b/protos/message.proto deleted file mode 100644 index 5d87e1f..0000000 --- a/protos/message.proto +++ /dev/null @@ -1,22 +0,0 @@ -syntax = "proto3"; -package hbb; - -message RegisterPeer { - string hbb_addr = 1; -} - -message PeekPeer { - string hbb_addr = 1; -} - -message PeekPeerResponse { - bytes socket_addr = 1; -} - -message Message { - oneof union { - RegisterPeer register_peer = 6; - PeekPeer peek_peer = 7; - PeekPeerResponse peek_peer_response = 8; - } -} @@ -1,5 +1,2 @@ mod rendezvous_server; pub use rendezvous_server::*; -#[path = "./protos/message.rs"] -mod message_proto; -pub use message_proto::*;
\ No newline at end of file diff --git a/src/rendezvous_server.rs b/src/rendezvous_server.rs index bc36a06..a37c278 100644 --- a/src/rendezvous_server.rs +++ b/src/rendezvous_server.rs @@ -1,53 +1,19 @@ -use super::message_proto::*; use bytes::{Bytes, BytesMut}; use futures::SinkExt; -use protobuf::{parse_from_bytes, Message as _}; +use hbb_common::{ + message_proto::*, + protobuf::{parse_from_bytes, Message as _}, + V4AddrMangle, +}; use std::{ collections::HashMap, error::Error, - net::{Ipv4Addr, SocketAddr, SocketAddrV4}, - time::{Duration, SystemTime, UNIX_EPOCH}, + net::{SocketAddr, SocketAddrV4}, + time::Duration, }; use tokio::{net::UdpSocket, stream::StreamExt, time::delay_for}; use tokio_util::{codec::BytesCodec, udp::UdpFramed}; -/// Certain router and firewalls scan the packet and if they -/// find an IP address belonging to their pool that they use to do the NAT mapping/translation, so here we mangle the ip address - -pub struct V4AddrMangle(); - -impl V4AddrMangle { - pub fn encode(addr: &SocketAddrV4) -> Vec<u8> { - let tm = (SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_micros() as u32) as u128; - let ip = u32::from_ne_bytes(addr.ip().octets()) as u128; - let port = addr.port() as u128; - let v = ((ip + tm) << 49) | (tm << 17) | (port + (tm & 0xFFFF)); - let bytes = v.to_ne_bytes(); - let mut n_padding = 0; - for i in bytes.iter().rev() { - if i == &0u8 { - n_padding += 1; - } else { - break; - } - } - bytes[..(16 - n_padding)].to_vec() - } - - pub fn decode(bytes: &[u8]) -> SocketAddrV4 { - let mut padded = [0u8; 16]; - padded[..bytes.len()].copy_from_slice(&bytes); - let number = u128::from_ne_bytes(padded); - let tm = (number >> 17) & (u32::max_value() as u128); - let ip = (((number >> 49) - tm) as u32).to_ne_bytes(); - let port = (number & 0xFFFFFF) - (tm & 0xFFFF); - SocketAddrV4::new(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]), port as u16) - } -} - pub struct Peer { socket_addr: SocketAddrV4, } @@ -127,11 +93,6 @@ pub async fn sleep(sec: f32) { #[cfg(test)] mod tests { use super::*; - #[test] - fn test_mangle() { - let addr = SocketAddrV4::new(Ipv4Addr::new(192, 168, 16, 32), 21116); - assert_eq!(addr, V4AddrMangle::decode(&V4AddrMangle::encode(&addr)[..])); - } #[allow(unused_must_use)] #[tokio::main] |