rustdesk-server/src/rendezvous_server.rs

156 lines
5.1 KiB
Rust
Raw Normal View History

2020-03-07 22:45:22 +08:00
use bytes::{Bytes, BytesMut};
2020-03-07 02:06:17 +08:00
use futures::SinkExt;
2020-03-07 23:24:58 +08:00
use hbb_common::{
message_proto::*,
protobuf::{parse_from_bytes, Message as _},
V4AddrMangle,
};
2020-03-06 17:18:22 +08:00
use std::{
collections::HashMap,
error::Error,
2020-03-07 23:24:58 +08:00
net::{SocketAddr, SocketAddrV4},
time::Duration,
2020-03-07 02:06:17 +08:00
};
2020-03-07 22:45:22 +08:00
use tokio::{net::UdpSocket, stream::StreamExt, time::delay_for};
2020-03-06 17:18:22 +08:00
use tokio_util::{codec::BytesCodec, udp::UdpFramed};
pub struct Peer {
2020-03-07 00:37:23 +08:00
socket_addr: SocketAddrV4,
2020-03-06 17:18:22 +08:00
}
type PeerMap = HashMap<String, Peer>;
pub struct RendezvousServer {
peer_map: PeerMap,
}
2020-03-07 00:37:23 +08:00
type FramedSocket = UdpFramed<BytesCodec>;
type ResultType = Result<(), Box<dyn Error>>;
2020-03-06 17:18:22 +08:00
impl RendezvousServer {
2020-03-07 00:37:23 +08:00
pub async fn start(addr: &str) -> ResultType {
2020-03-06 17:18:22 +08:00
let socket = UdpSocket::bind(addr).await?;
let mut socket = UdpFramed::new(socket, BytesCodec::new());
2020-03-07 00:37:23 +08:00
let mut rs = Self {
2020-03-06 17:18:22 +08:00
peer_map: PeerMap::new(),
};
while let Some(Ok((bytes, addr))) = socket.next().await {
2020-03-07 22:45:22 +08:00
rs.handle_msg(&bytes, addr, &mut socket).await?;
2020-03-06 17:18:22 +08:00
}
2020-03-07 00:37:23 +08:00
Ok(())
}
2020-03-07 22:45:22 +08:00
pub async fn handle_msg(
&mut self,
bytes: &BytesMut,
2020-03-07 00:37:23 +08:00
addr: SocketAddr,
socket: &mut FramedSocket,
) -> ResultType {
2020-03-07 22:45:22 +08:00
if let Ok(msg_in) = parse_from_bytes::<Message>(&bytes) {
if let SocketAddr::V4(addr_v4) = addr {
match msg_in.union {
Some(Message_oneof_union::register_peer(rp)) => {
if rp.hbb_addr.len() > 0 {
self.peer_map.insert(
rp.hbb_addr,
Peer {
socket_addr: addr_v4,
},
);
}
}
2020-03-09 13:55:12 +08:00
Some(Message_oneof_union::punch_hole_request(ph)) => {
// punch hole request from A, forward to B
if let Some(peer) = self.peer_map.get(&ph.hbb_addr) {
2020-03-07 22:45:22 +08:00
let mut msg_out = Message::new();
2020-03-09 13:55:12 +08:00
msg_out.set_punch_hole(PunchHole {
2020-03-07 22:45:22 +08:00
socket_addr: V4AddrMangle::encode(&peer.socket_addr),
..Default::default()
});
send_to(&msg_out, addr, socket).await?;
}
}
2020-03-09 13:55:12 +08:00
Some(Message_oneof_union::punch_hole_sent(phs)) => {
// punch hole sent from B, tell A that B ready
let addr_a = V4AddrMangle::decode(&phs.socket_addr);
let mut msg_out = Message::new();
msg_out.set_punch_hole_response(PunchHoleResponse {
socket_addr: V4AddrMangle::encode(&addr_v4),
..Default::default()
});
send_to(&msg_out, SocketAddr::V4(addr_a), socket).await?;
}
2020-03-07 22:45:22 +08:00
_ => {}
}
}
2020-03-07 00:37:23 +08:00
}
Ok(())
2020-03-06 17:18:22 +08:00
}
}
2020-03-07 00:37:23 +08:00
#[inline]
pub async fn send_to(msg: &Message, addr: SocketAddr, socket: &mut FramedSocket) -> ResultType {
socket
.send((Bytes::from(msg.write_to_bytes().unwrap()), addr))
.await?;
Ok(())
}
2020-03-09 13:55:12 +08:00
#[inline]
2020-03-07 02:06:17 +08:00
pub async fn sleep(sec: f32) {
delay_for(Duration::from_secs_f32(sec)).await;
}
2020-03-06 17:18:22 +08:00
#[cfg(test)]
mod tests {
use super::*;
2020-03-07 00:37:23 +08:00
#[allow(unused_must_use)]
#[tokio::main]
async fn test_rs_async() {
let server_addr = "0.0.0.0:21116";
let f1 = RendezvousServer::start(server_addr);
let to_addr = server_addr.parse().unwrap();
let f2 = async {
let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
2020-03-07 02:06:17 +08:00
let local_addr = socket.local_addr().unwrap();
2020-03-07 00:37:23 +08:00
let mut socket = UdpFramed::new(socket, BytesCodec::new());
let mut msg_out = Message::new();
2020-03-07 02:06:17 +08:00
msg_out.set_register_peer(RegisterPeer {
hbb_addr: "123".to_string(),
..Default::default()
});
send_to(&msg_out, to_addr, &mut socket).await;
2020-03-09 13:55:12 +08:00
msg_out.set_punch_hole_request(PunchHoleRequest {
2020-03-07 00:37:23 +08:00
hbb_addr: "123".to_string(),
..Default::default()
});
send_to(&msg_out, to_addr, &mut socket).await;
2020-03-07 02:06:17 +08:00
if let Ok(Some(Ok((bytes, _)))) =
2020-03-07 22:45:22 +08:00
tokio::time::timeout(Duration::from_millis(1), socket.next()).await
2020-03-07 02:06:17 +08:00
{
if let Ok(msg_in) = parse_from_bytes::<Message>(&bytes) {
assert_eq!(
local_addr,
SocketAddr::V4(V4AddrMangle::decode(
2020-03-09 13:55:12 +08:00
&msg_in.get_punch_hole_response().socket_addr[..]
2020-03-07 02:06:17 +08:00
))
);
}
}
if true {
Err(Box::new(simple_error::SimpleError::new("done")))
} else {
Ok(())
}
2020-03-07 00:37:23 +08:00
};
2020-03-07 02:06:17 +08:00
tokio::try_join!(f1, f2);
2020-03-07 00:37:23 +08:00
}
#[test]
fn test_rs() {
self::test_rs_async();
2020-03-06 17:18:22 +08:00
}
}