mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
tokio1, windows not test yet
This commit is contained in:
@@ -8,19 +8,19 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
protobuf = { version = "3.0.0-pre", git = "https://github.com/stepancheg/rust-protobuf" }
|
||||
tokio = { version = "0.2", features = ["full"] }
|
||||
tokio-util = { version = "0.3", features = ["full"] }
|
||||
tokio = { version = "1.7", features = ["full"] }
|
||||
tokio-util = { version = "0.6", features = ["full"] }
|
||||
futures = "0.3"
|
||||
bytes = "0.5"
|
||||
bytes = "1.0"
|
||||
log = "0.4"
|
||||
env_logger = "0.8"
|
||||
socket2 = { version = "0.3", features = ["reuseport"] }
|
||||
zstd = "0.5"
|
||||
zstd = "0.9"
|
||||
quinn = {version = "0.6", optional = true }
|
||||
anyhow = "1.0"
|
||||
futures-util = "0.3"
|
||||
directories-next = "2.0"
|
||||
rand = "0.7"
|
||||
rand = "0.8"
|
||||
serde_derive = "1.0"
|
||||
serde = "1.0"
|
||||
lazy_static = "1.4"
|
||||
|
||||
@@ -548,7 +548,7 @@ impl Config {
|
||||
// to-do: how about if one ip register a lot of ids?
|
||||
let id = Self::get_id();
|
||||
let mut rng = rand::thread_rng();
|
||||
let new_id = rng.gen_range(1_000_000_000, 2_000_000_000).to_string();
|
||||
let new_id = rng.gen_range(1_000_000_000..2_000_000_000).to_string();
|
||||
Config::set_id(&new_id);
|
||||
log::info!("id updated from {} to {}", id, new_id);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::{
|
||||
};
|
||||
#[cfg(windows)]
|
||||
use std::os::windows::prelude::*;
|
||||
use tokio::{fs::File, prelude::*};
|
||||
use tokio::{fs::File, io::*};
|
||||
|
||||
pub fn read_dir(path: &PathBuf, include_hidden: bool) -> ResultType<FileDirectory> {
|
||||
let mut dir = FileDirectory {
|
||||
|
||||
@@ -6,7 +6,6 @@ pub mod rendezvous_proto;
|
||||
pub use bytes;
|
||||
pub use futures;
|
||||
pub use protobuf;
|
||||
use socket2::{Domain, Socket, Type};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead},
|
||||
@@ -36,7 +35,7 @@ pub type Stream = tcp::FramedStream;
|
||||
|
||||
#[inline]
|
||||
pub async fn sleep(sec: f32) {
|
||||
tokio::time::delay_for(time::Duration::from_secs_f32(sec)).await;
|
||||
tokio::time::sleep(time::Duration::from_secs_f32(sec)).await;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
@@ -61,30 +60,6 @@ pub fn timeout<T: std::future::Future>(ms: u64, future: T) -> tokio::time::Timeo
|
||||
tokio::time::timeout(std::time::Duration::from_millis(ms), future)
|
||||
}
|
||||
|
||||
fn new_socket(addr: SocketAddr, tcp: bool, reuse: bool) -> Result<Socket, std::io::Error> {
|
||||
let stype = {
|
||||
if tcp {
|
||||
Type::stream()
|
||||
} else {
|
||||
Type::dgram()
|
||||
}
|
||||
};
|
||||
let socket = match addr {
|
||||
SocketAddr::V4(..) => Socket::new(Domain::ipv4(), stype, None),
|
||||
SocketAddr::V6(..) => Socket::new(Domain::ipv6(), stype, None),
|
||||
}?;
|
||||
if reuse {
|
||||
// windows has no reuse_port, but it's reuse_address
|
||||
// almost equals to unix's reuse_port + reuse_address,
|
||||
// though may introduce nondeterministic bahavior
|
||||
#[cfg(unix)]
|
||||
socket.set_reuse_port(true)?;
|
||||
socket.set_reuse_address(true)?;
|
||||
}
|
||||
socket.bind(&addr.into())?;
|
||||
Ok(socket)
|
||||
}
|
||||
|
||||
pub type ResultType<F, E = anyhow::Error> = anyhow::Result<F, E>;
|
||||
|
||||
/// Certain router and firewalls scan the packet and if they
|
||||
@@ -100,10 +75,10 @@ impl AddrMangle {
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_micros() as u32) as u128;
|
||||
let ip = u32::from_ne_bytes(addr_v4.ip().octets()) as u128;
|
||||
let ip = u32::from_le_bytes(addr_v4.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 bytes = v.to_le_bytes();
|
||||
let mut n_padding = 0;
|
||||
for i in bytes.iter().rev() {
|
||||
if i == &0u8 {
|
||||
@@ -123,9 +98,9 @@ impl AddrMangle {
|
||||
pub fn decode(bytes: &[u8]) -> SocketAddr {
|
||||
let mut padded = [0u8; 16];
|
||||
padded[..bytes.len()].copy_from_slice(&bytes);
|
||||
let number = u128::from_ne_bytes(padded);
|
||||
let number = u128::from_le_bytes(padded);
|
||||
let tm = (number >> 17) & (u32::max_value() as u128);
|
||||
let ip = (((number >> 49) - tm) as u32).to_ne_bytes();
|
||||
let ip = (((number >> 49) - tm) as u32).to_le_bytes();
|
||||
let port = (number & 0xFFFFFF) - (tm & 0xFFFF);
|
||||
SocketAddr::V4(SocketAddrV4::new(
|
||||
Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
use crate::{bail, bytes_codec::BytesCodec, ResultType};
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use futures::SinkExt;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use protobuf::Message;
|
||||
use sodiumoxide::crypto::secretbox::{self, Key, Nonce};
|
||||
use std::{
|
||||
io::{Error, ErrorKind},
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
use tokio::{
|
||||
net::{TcpListener, TcpStream, ToSocketAddrs},
|
||||
stream::StreamExt,
|
||||
};
|
||||
use tokio::net::{lookup_host, TcpListener, TcpSocket, TcpStream, ToSocketAddrs};
|
||||
use tokio_util::codec::Framed;
|
||||
|
||||
pub struct FramedStream(Framed<TcpStream, BytesCodec>, Option<(Key, u64, u64)>);
|
||||
@@ -29,25 +26,37 @@ impl DerefMut for FramedStream {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_socket(addr: std::net::SocketAddr, reuse: bool) -> Result<TcpSocket, std::io::Error> {
|
||||
let socket = match addr {
|
||||
std::net::SocketAddr::V4(..) => TcpSocket::new_v4()?,
|
||||
std::net::SocketAddr::V6(..) => TcpSocket::new_v6()?,
|
||||
};
|
||||
if reuse {
|
||||
// windows has no reuse_port, but it's reuse_address
|
||||
// almost equals to unix's reuse_port + reuse_address,
|
||||
// though may introduce nondeterministic bahavior
|
||||
#[cfg(unix)]
|
||||
socket.set_reuseport(true)?;
|
||||
socket.set_reuseaddr(true)?;
|
||||
}
|
||||
socket.bind(addr)?;
|
||||
Ok(socket)
|
||||
}
|
||||
|
||||
impl FramedStream {
|
||||
pub async fn new<T: ToSocketAddrs, T2: ToSocketAddrs>(
|
||||
remote_addr: T,
|
||||
local_addr: T2,
|
||||
ms_timeout: u64,
|
||||
) -> ResultType<Self> {
|
||||
for local_addr in local_addr.to_socket_addrs().await? {
|
||||
for remote_addr in remote_addr.to_socket_addrs().await? {
|
||||
if let Ok(stream) = super::timeout(
|
||||
for local_addr in lookup_host(&local_addr).await? {
|
||||
for remote_addr in lookup_host(&remote_addr).await? {
|
||||
let stream = super::timeout(
|
||||
ms_timeout,
|
||||
TcpStream::connect_std(
|
||||
super::new_socket(local_addr, true, true)?.into_tcp_stream(),
|
||||
&remote_addr,
|
||||
),
|
||||
new_socket(local_addr, true)?.connect(remote_addr),
|
||||
)
|
||||
.await?
|
||||
{
|
||||
return Ok(Self(Framed::new(stream, BytesCodec::new()), None));
|
||||
}
|
||||
.await??;
|
||||
return Ok(Self(Framed::new(stream, BytesCodec::new()), None));
|
||||
}
|
||||
}
|
||||
bail!("could not resolve to any address");
|
||||
@@ -124,22 +133,21 @@ impl FramedStream {
|
||||
|
||||
fn get_nonce(seqnum: u64) -> Nonce {
|
||||
let mut nonce = Nonce([0u8; secretbox::NONCEBYTES]);
|
||||
nonce.0[..std::mem::size_of_val(&seqnum)].copy_from_slice(&seqnum.to_ne_bytes());
|
||||
nonce.0[..std::mem::size_of_val(&seqnum)].copy_from_slice(&seqnum.to_le_bytes());
|
||||
nonce
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_BACKLOG: i32 = 128;
|
||||
const DEFAULT_BACKLOG: u32 = 128;
|
||||
|
||||
#[allow(clippy::never_loop)]
|
||||
pub async fn new_listener<T: ToSocketAddrs>(addr: T, reuse: bool) -> ResultType<TcpListener> {
|
||||
if !reuse {
|
||||
Ok(TcpListener::bind(addr).await?)
|
||||
} else {
|
||||
for addr in addr.to_socket_addrs().await? {
|
||||
let socket = super::new_socket(addr, true, true)?;
|
||||
socket.listen(DEFAULT_BACKLOG)?;
|
||||
return Ok(TcpListener::from_std(socket.into_tcp_listener())?);
|
||||
for addr in lookup_host(&addr).await? {
|
||||
let socket = new_socket(addr, true)?;
|
||||
return Ok(socket.listen(DEFAULT_BACKLOG)?);
|
||||
}
|
||||
bail!("could not resolve to any address");
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
use crate::{bail, ResultType};
|
||||
use bytes::BytesMut;
|
||||
use futures::SinkExt;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use protobuf::Message;
|
||||
use socket2::{Domain, Socket, Type};
|
||||
use std::{
|
||||
io::Error,
|
||||
net::SocketAddr,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
use tokio::{net::ToSocketAddrs, net::UdpSocket, stream::StreamExt};
|
||||
use tokio::{net::ToSocketAddrs, net::UdpSocket};
|
||||
use tokio_util::{codec::BytesCodec, udp::UdpFramed};
|
||||
|
||||
pub struct FramedSocket(UdpFramed<BytesCodec>);
|
||||
@@ -20,6 +21,23 @@ impl Deref for FramedSocket {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_socket(addr: SocketAddr, reuse: bool) -> Result<Socket, std::io::Error> {
|
||||
let socket = match addr {
|
||||
SocketAddr::V4(..) => Socket::new(Domain::ipv4(), Type::dgram(), None),
|
||||
SocketAddr::V6(..) => Socket::new(Domain::ipv6(), Type::dgram(), None),
|
||||
}?;
|
||||
if reuse {
|
||||
// windows has no reuse_port, but it's reuse_address
|
||||
// almost equals to unix's reuse_port + reuse_address,
|
||||
// though may introduce nondeterministic bahavior
|
||||
#[cfg(unix)]
|
||||
socket.set_reuse_port(true)?;
|
||||
socket.set_reuse_address(true)?;
|
||||
}
|
||||
socket.bind(&addr.into())?;
|
||||
Ok(socket)
|
||||
}
|
||||
|
||||
impl DerefMut for FramedSocket {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
@@ -33,10 +51,10 @@ impl FramedSocket {
|
||||
}
|
||||
|
||||
#[allow(clippy::never_loop)]
|
||||
pub async fn new_reuse<T: ToSocketAddrs>(addr: T) -> ResultType<Self> {
|
||||
for addr in addr.to_socket_addrs().await? {
|
||||
pub async fn new_reuse<T: std::net::ToSocketAddrs>(addr: T) -> ResultType<Self> {
|
||||
for addr in addr.to_socket_addrs()? {
|
||||
return Ok(Self(UdpFramed::new(
|
||||
UdpSocket::from_std(super::new_socket(addr, false, true)?.into_udp_socket())?,
|
||||
UdpSocket::from_std(new_socket(addr, true)?.into_udp_socket())?,
|
||||
BytesCodec::new(),
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ futures = "0.3"
|
||||
log = "0.4"
|
||||
mio-named-pipes = "0.1"
|
||||
miow = "0.3"
|
||||
rand = "0.7"
|
||||
tokio = { version = "0.2", features = ["io-driver", "io-util", "uds", "stream", "rt-core", "macros", "time"] }
|
||||
rand = "0.8"
|
||||
tokio = { version = "1.7", features = ["full"] }
|
||||
libc = "0.2"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use libc::chmod;
|
||||
use std::ffi::CString;
|
||||
use std::io::{self, Error};
|
||||
use tokio::prelude::*;
|
||||
use tokio::io::*;
|
||||
use tokio::net::{UnixListener, UnixStream};
|
||||
use std::path::Path;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
/// Socket permissions and ownership on UNIX
|
||||
pub struct SecurityAttributes {
|
||||
@@ -127,15 +126,11 @@ impl Connection {
|
||||
}
|
||||
|
||||
impl AsyncRead for Connection {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
|
||||
self.inner.prepare_uninitialized_buffer(buf)
|
||||
}
|
||||
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
ctx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
buf: &mut tokio::io::ReadBuf<'_>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_read(ctx, buf)
|
||||
}
|
||||
@@ -146,17 +141,17 @@ impl AsyncWrite for Connection {
|
||||
self: Pin<&mut Self>,
|
||||
ctx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize, io::Error>> {
|
||||
) -> Poll<Result<usize>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_write(ctx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
fn poll_flush(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_flush(ctx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
fn poll_shutdown(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_shutdown(ctx)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ use tokio::prelude::*;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::path::Path;
|
||||
use std::mem::MaybeUninit;
|
||||
use tokio::io::PollEvented;
|
||||
|
||||
type NamedPipe = PollEvented<mio_named_pipes::NamedPipe>;
|
||||
@@ -178,15 +177,11 @@ impl Connection {
|
||||
}
|
||||
|
||||
impl AsyncRead for Connection {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
|
||||
self.inner.prepare_uninitialized_buffer(buf)
|
||||
}
|
||||
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
ctx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
buf: &mut tokio::io::ReadBuf<'_>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_read(ctx, buf)
|
||||
}
|
||||
@@ -197,17 +192,17 @@ impl AsyncWrite for Connection {
|
||||
self: Pin<&mut Self>,
|
||||
ctx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize, io::Error>> {
|
||||
) -> Poll<Result<usize>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_write(ctx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
fn poll_flush(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_flush(ctx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||
fn poll_shutdown(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
let this = Pin::into_inner(self);
|
||||
Pin::new(&mut this.inner).poll_shutdown(ctx)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ windowless = []
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
lazy_static = "1.0"
|
||||
lazy_static = "1.4"
|
||||
|
||||
[target.x86_64-apple-darwin.dependencies]
|
||||
objc = "0.2"
|
||||
|
||||
Reference in New Issue
Block a user