rustdesk-server/src/rendezvous_server.rs

135 lines
5.9 KiB
Rust
Raw Normal View History

2020-03-07 23:24:58 +08:00
use hbb_common::{
2020-03-12 22:06:09 +08:00
allow_err, bytes::BytesMut, log, protobuf::parse_from_bytes, rendezvous_proto::*,
tcp::new_listener, tokio, udp::FramedSocket, AddrMangle, ResultType,
2020-03-07 02:06:17 +08:00
};
2020-03-09 20:52:54 +08:00
use std::{collections::HashMap, net::SocketAddr};
2020-03-06 17:18:22 +08:00
pub struct Peer {
2020-03-09 16:16:21 +08:00
socket_addr: SocketAddr,
2020-03-06 17:18:22 +08:00
}
type PeerMap = HashMap<String, Peer>;
pub struct RendezvousServer {
peer_map: PeerMap,
}
impl RendezvousServer {
2020-03-09 20:52:54 +08:00
pub async fn start(addr: &str) -> ResultType<()> {
2020-03-10 00:24:07 +08:00
let mut socket = FramedSocket::new(addr).await?;
2020-03-07 00:37:23 +08:00
let mut rs = Self {
2020-03-06 17:18:22 +08:00
peer_map: PeerMap::new(),
};
2020-03-12 22:06:09 +08:00
// tcp listener used to test if udp/tcp share the same NAT port, yes in my test.
// also be used to help client to get local ip.
2020-03-12 22:06:09 +08:00
let mut listener = new_listener(addr, true).await.unwrap();
loop {
tokio::select! {
Some(Ok((bytes, addr))) = socket.next() => {
allow_err!(rs.handle_msg(&bytes, addr, &mut socket).await);
}
Ok((_, addr)) = listener.accept() => {
log::debug!("Tcp connection from {:?}", addr);
}
}
2020-03-06 17:18:22 +08:00
}
2020-03-07 00:37:23 +08:00
}
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,
2020-03-09 20:52:54 +08:00
) -> ResultType<()> {
2020-03-12 19:25:37 +08:00
if let Ok(msg_in) = parse_from_bytes::<RendezvousMessage>(&bytes) {
2020-03-09 16:16:21 +08:00
match msg_in.union {
2020-04-21 23:49:31 +08:00
Some(rendezvous_message::Union::register_peer(rp)) => {
2020-03-12 19:25:37 +08:00
// B registered
if rp.id.len() > 0 {
log::debug!("New peer registered: {:?} {:?}", &rp.id, &addr);
self.peer_map.insert(rp.id, Peer { socket_addr: addr });
let mut msg_out = RendezvousMessage::new();
msg_out.set_register_peer_response(RegisterPeerResponse::default());
socket.send(&msg_out, addr).await?
2020-03-07 22:45:22 +08:00
}
2020-03-09 16:16:21 +08:00
}
2020-04-21 23:49:31 +08:00
Some(rendezvous_message::Union::punch_hole_request(ph)) => {
2020-03-12 19:25:37 +08:00
// punch hole request from A, forward to B,
// check if in same intranet first,
// fetch local addrs if in same intranet.
// because punch hole won't work if in the same intranet,
// all routers will drop such self-connections.
if let Some(peer) = self.peer_map.get(&ph.id) {
2020-03-12 19:25:37 +08:00
let mut msg_out = RendezvousMessage::new();
let same_intranet = match peer.socket_addr {
SocketAddr::V4(a) => match addr {
SocketAddr::V4(b) => a.ip() == b.ip(),
_ => false,
},
SocketAddr::V6(a) => match addr {
SocketAddr::V6(b) => a.ip() == b.ip(),
_ => false,
},
};
let socket_addr = AddrMangle::encode(&addr);
if same_intranet {
log::debug!(
"Fetch local addr {:?} {:?} request from {:?}",
&ph.id,
2020-03-12 19:25:37 +08:00
&peer.socket_addr,
&addr
);
msg_out.set_fetch_local_addr(FetchLocalAddr {
2020-03-12 19:25:37 +08:00
socket_addr,
..Default::default()
});
} else {
log::debug!(
"Punch hole {:?} {:?} request from {:?}",
&ph.id,
2020-03-12 19:25:37 +08:00
&peer.socket_addr,
&addr
);
msg_out.set_punch_hole(PunchHole {
socket_addr,
..Default::default()
});
}
2020-03-09 20:52:54 +08:00
socket.send(&msg_out, peer.socket_addr).await?;
} else {
let mut msg_out = RendezvousMessage::new();
msg_out.set_punch_hole_response(PunchHoleResponse {
2020-04-21 23:49:31 +08:00
failure: punch_hole_response::Failure::ID_NOT_EXIST.into(),
..Default::default()
});
socket.send(&msg_out, addr).await?
2020-03-09 13:55:12 +08:00
}
2020-03-07 22:45:22 +08:00
}
2020-04-21 23:49:31 +08:00
Some(rendezvous_message::Union::punch_hole_sent(phs)) => {
2020-03-12 19:25:37 +08:00
// punch hole sent from B, tell A that B is ready to be connected
2020-03-09 16:16:21 +08:00
let addr_a = AddrMangle::decode(&phs.socket_addr);
2020-03-09 19:35:57 +08:00
log::debug!("Punch hole response to {:?} from {:?}", &addr_a, &addr);
2020-03-12 19:25:37 +08:00
let mut msg_out = RendezvousMessage::new();
2020-03-09 16:16:21 +08:00
msg_out.set_punch_hole_response(PunchHoleResponse {
socket_addr: AddrMangle::encode(&addr),
..Default::default()
});
2020-03-09 20:52:54 +08:00
socket.send(&msg_out, addr_a).await?;
2020-03-09 16:16:21 +08:00
}
2020-04-21 23:49:31 +08:00
Some(rendezvous_message::Union::local_addr(la)) => {
2020-03-12 19:25:37 +08:00
// forward local addrs of B to A
let addr_a = AddrMangle::decode(&la.socket_addr);
log::debug!("Local addrs response to {:?} from {:?}", &addr_a, &addr);
let mut msg_out = RendezvousMessage::new();
msg_out.set_punch_hole_response(PunchHoleResponse {
socket_addr: la.local_addr,
2020-03-12 19:25:37 +08:00
..Default::default()
});
socket.send(&msg_out, addr_a).await?;
}
2020-03-09 16:16:21 +08:00
_ => {}
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-04-24 18:15:41 +08:00
}