fix wrong bytes order in protocol parsing
This commit is contained in:
parent
15d0211f04
commit
1147c5a7df
@ -12,6 +12,7 @@ license = "GPL-3.0-or-later"
|
|||||||
repository = "https://github.com/EAimTY/tuic"
|
repository = "https://github.com/EAimTY/tuic"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
byteorder = "1.4"
|
||||||
bytes = "1.1"
|
bytes = "1.1"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
tokio = { version = "1.19", features = ["io-util"] }
|
tokio = { version = "1.19", features = ["io-util"] }
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
//! The TUIC protocol
|
//! The TUIC protocol
|
||||||
|
|
||||||
use bytes::{BufMut, BytesMut};
|
use byteorder::{BigEndian, ReadBytesExt};
|
||||||
|
use bytes::BufMut;
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Display, Formatter, Result as FmtResult},
|
fmt::{Display, Formatter, Result as FmtResult},
|
||||||
io::{Error, ErrorKind, Result},
|
io::{Cursor, Error, ErrorKind, Result},
|
||||||
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
|
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
|
||||||
};
|
};
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||||
@ -92,15 +93,19 @@ impl Command {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
match r.read_u8().await? {
|
let cmd = r.read_u8().await?;
|
||||||
Self::TYPE_RESPONSE => match r.read_u8().await? {
|
match cmd {
|
||||||
Self::RESPONSE_SUCCEEDED => Ok(Self::new_response(true)),
|
Self::TYPE_RESPONSE => {
|
||||||
Self::RESPONSE_FAILED => Ok(Self::new_response(false)),
|
let resp = r.read_u8().await?;
|
||||||
resp => Err(Error::new(
|
match resp {
|
||||||
ErrorKind::InvalidInput,
|
Self::RESPONSE_SUCCEEDED => Ok(Self::new_response(true)),
|
||||||
format!("Invalid response code: {resp}"),
|
Self::RESPONSE_FAILED => Ok(Self::new_response(false)),
|
||||||
)),
|
_ => Err(Error::new(
|
||||||
},
|
ErrorKind::InvalidInput,
|
||||||
|
format!("Invalid response code: {resp}"),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
Self::TYPE_AUTHENTICATE => {
|
Self::TYPE_AUTHENTICATE => {
|
||||||
let mut digest = [0; 32];
|
let mut digest = [0; 32];
|
||||||
r.read_exact(&mut digest).await?;
|
r.read_exact(&mut digest).await?;
|
||||||
@ -113,9 +118,10 @@ impl Command {
|
|||||||
Self::TYPE_PACKET => {
|
Self::TYPE_PACKET => {
|
||||||
let mut buf = [0; 6];
|
let mut buf = [0; 6];
|
||||||
r.read_exact(&mut buf).await?;
|
r.read_exact(&mut buf).await?;
|
||||||
|
let mut rdr = Cursor::new(buf);
|
||||||
|
|
||||||
let assoc_id = unsafe { u32::from_be(*(buf.as_ptr() as *const u32)) };
|
let assoc_id = ReadBytesExt::read_u32::<BigEndian>(&mut rdr).unwrap();
|
||||||
let len = unsafe { u16::from_be(*(buf.as_ptr().add(4) as *const u16)) };
|
let len = ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap();
|
||||||
let addr = Address::read_from(r).await?;
|
let addr = Address::read_from(r).await?;
|
||||||
|
|
||||||
Ok(Self::new_packet(assoc_id, len, addr))
|
Ok(Self::new_packet(assoc_id, len, addr))
|
||||||
@ -125,7 +131,7 @@ impl Command {
|
|||||||
Ok(Self::new_dissociate(assoc_id))
|
Ok(Self::new_dissociate(assoc_id))
|
||||||
}
|
}
|
||||||
Self::TYPE_HEARTBEAT => Ok(Self::new_heartbeat()),
|
Self::TYPE_HEARTBEAT => Ok(Self::new_heartbeat()),
|
||||||
cmd => Err(Error::new(
|
_ => Err(Error::new(
|
||||||
ErrorKind::InvalidInput,
|
ErrorKind::InvalidInput,
|
||||||
format!("Invalid command: {cmd}"),
|
format!("Invalid command: {cmd}"),
|
||||||
)),
|
)),
|
||||||
@ -136,7 +142,7 @@ impl Command {
|
|||||||
where
|
where
|
||||||
W: AsyncWrite + Unpin,
|
W: AsyncWrite + Unpin,
|
||||||
{
|
{
|
||||||
let mut buf = BytesMut::with_capacity(self.serialized_len());
|
let mut buf = Vec::with_capacity(self.serialized_len());
|
||||||
self.write_to_buf(&mut buf);
|
self.write_to_buf(&mut buf);
|
||||||
w.write_all(&buf).await
|
w.write_all(&buf).await
|
||||||
}
|
}
|
||||||
@ -235,8 +241,7 @@ impl Address {
|
|||||||
let mut buf = vec![0; len + 2];
|
let mut buf = vec![0; len + 2];
|
||||||
stream.read_exact(&mut buf).await?;
|
stream.read_exact(&mut buf).await?;
|
||||||
|
|
||||||
let port = unsafe { u16::from_be(*(buf.as_ptr().add(len) as *const u16)) };
|
let port = ReadBytesExt::read_u16::<BigEndian>(&mut &buf[len..]).unwrap();
|
||||||
|
|
||||||
buf.truncate(len);
|
buf.truncate(len);
|
||||||
|
|
||||||
let addr = String::from_utf8(buf).map_err(|err| {
|
let addr = String::from_utf8(buf).map_err(|err| {
|
||||||
@ -251,30 +256,37 @@ impl Address {
|
|||||||
Self::TYPE_IPV4 => {
|
Self::TYPE_IPV4 => {
|
||||||
let mut buf = [0; 6];
|
let mut buf = [0; 6];
|
||||||
stream.read_exact(&mut buf).await?;
|
stream.read_exact(&mut buf).await?;
|
||||||
|
let mut rdr = Cursor::new(buf);
|
||||||
|
|
||||||
let port = unsafe { u16::from_be(*(buf.as_ptr().add(4) as *const u16)) };
|
let addr = Ipv4Addr::new(
|
||||||
let addr = Ipv4Addr::new(buf[0], buf[1], buf[2], buf[3]);
|
ReadBytesExt::read_u8(&mut rdr).unwrap(),
|
||||||
|
ReadBytesExt::read_u8(&mut rdr).unwrap(),
|
||||||
|
ReadBytesExt::read_u8(&mut rdr).unwrap(),
|
||||||
|
ReadBytesExt::read_u8(&mut rdr).unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let port = ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap();
|
||||||
|
|
||||||
Ok(Self::SocketAddress(SocketAddr::from((addr, port))))
|
Ok(Self::SocketAddress(SocketAddr::from((addr, port))))
|
||||||
}
|
}
|
||||||
Self::TYPE_IPV6 => {
|
Self::TYPE_IPV6 => {
|
||||||
let mut buf = [0; 18];
|
let mut buf = [0; 18];
|
||||||
stream.read_exact(&mut buf).await?;
|
stream.read_exact(&mut buf).await?;
|
||||||
let buf = unsafe { *(buf.as_ptr() as *const [u16; 9]) };
|
let mut rdr = Cursor::new(buf);
|
||||||
|
|
||||||
let port = buf[8];
|
|
||||||
|
|
||||||
let addr = Ipv6Addr::new(
|
let addr = Ipv6Addr::new(
|
||||||
u16::from_be(buf[0]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[1]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[2]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[3]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[4]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[5]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[6]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
u16::from_be(buf[7]),
|
ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let port = ReadBytesExt::read_u16::<BigEndian>(&mut rdr).unwrap();
|
||||||
|
|
||||||
Ok(Self::SocketAddress(SocketAddr::from((addr, port))))
|
Ok(Self::SocketAddress(SocketAddr::from((addr, port))))
|
||||||
}
|
}
|
||||||
_ => Err(Error::new(
|
_ => Err(Error::new(
|
||||||
@ -288,7 +300,7 @@ impl Address {
|
|||||||
where
|
where
|
||||||
W: AsyncWrite + Unpin,
|
W: AsyncWrite + Unpin,
|
||||||
{
|
{
|
||||||
let mut buf = BytesMut::with_capacity(self.serialized_len());
|
let mut buf = Vec::with_capacity(self.serialized_len());
|
||||||
self.write_to_buf(&mut buf);
|
self.write_to_buf(&mut buf);
|
||||||
writer.write_all(&buf).await
|
writer.write_all(&buf).await
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user