Skip to content

Commit

Permalink
client: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
EAimTY committed Mar 8, 2022
1 parent 930106a commit 729145d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 52 deletions.
24 changes: 14 additions & 10 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ use tuic_protocol::{Address as TuicAddress, Command as TuicCommand, Response as
pub struct TuicClient {
endpoint: Endpoint,
server_addr: ServerAddr,
token: Hash,
token_digest: Hash,
req_rx: MpscReceiver<Request>,
}

impl TuicClient {
pub fn init(
server_addr: ServerAddr,
certificate: Option<Certificate>,
token: Hash,
token_digest: Hash,
) -> Result<(Self, MpscSender<Request>)> {
let quinn_config = if let Some(cert) = certificate {
let mut root_cert_store = RootCertStore::empty();
Expand All @@ -47,7 +47,7 @@ impl TuicClient {
Self {
endpoint,
server_addr,
token,
token_digest,
req_rx,
},
req_tx,
Expand Down Expand Up @@ -84,10 +84,7 @@ impl TuicClient {

async fn establish_conn(&self) -> Connection {
let (mut addrs, server_name) = match &self.server_addr {
ServerAddr::HostnameAddr {
hostname,
server_port,
} => (
ServerAddr::HostnameAddr { hostname, .. } => (
unsafe { mem::transmute(MaybeUninit::<IntoIter<SocketAddr>>::uninit()) },
hostname,
),
Expand Down Expand Up @@ -120,7 +117,14 @@ impl TuicClient {
uni_streams,
datagrams,
..
}) => return Connection::new(&self.token, conn, uni_streams, datagrams),
}) => {
return Connection::new(
&self.token_digest,
conn,
uni_streams,
datagrams,
)
}
Err(err) => eprintln!("{err}"),
},
Err(err) => eprintln!("{err}"),
Expand All @@ -138,12 +142,12 @@ pub struct Connection {

impl Connection {
fn new(
token: &Hash,
token_digest: &Hash,
conn: QuinnConnection,
uni_streams: QuinnUniStreams,
datagrams: QuinnDatagrams,
) -> Self {
let token = TuicCommand::new_authenticate(token.as_bytes().to_owned());
let token = TuicCommand::new_authenticate(token_digest.as_bytes().to_owned());
let uni = conn.open_uni();

tokio::spawn(async move {
Expand Down
4 changes: 2 additions & 2 deletions client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl<'cfg> ConfigBuilder<'cfg> {
};

let token_digest = {
let token_digest = unsafe { matches.opt_str("t").unwrap_unchecked() };
blake3::hash(&token_digest.into_bytes())
let token = unsafe { matches.opt_str("t").unwrap_unchecked() };
blake3::hash(&token.into_bytes())
};

let local_addr = {
Expand Down
2 changes: 0 additions & 2 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(unused)]

use crate::{client::TuicClient, config::ConfigBuilder, socks5::Socks5Server};
use std::{env, process};

Expand Down
6 changes: 3 additions & 3 deletions client/src/socks5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Connection {
Authentication::Password { username, password } => {
let req = PasswordAuthRequest::read_from(&mut self.stream).await?;

if matches!((&req.username, &req.password), (username, password)) {
if (&req.username, &req.password) == (username, password) {
let resp = PasswordAuthResponse::new(true);
resp.write_to(&mut self.stream).await?;
} else {
Expand Down Expand Up @@ -155,11 +155,11 @@ impl Connection {
Ok(())
}

async fn handle_bind(mut self, addr: Address) -> Result<()> {
async fn handle_bind(self, _addr: Address) -> Result<()> {
Ok(())
}

async fn handle_associate(mut self, addr: Address) -> Result<()> {
async fn handle_associate(self, _addr: Address) -> Result<()> {
Ok(())
}
}
14 changes: 2 additions & 12 deletions client/src/socks5/protocol/address.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::Error;
use bytes::{BufMut, BytesMut};
use bytes::BufMut;
use std::{
io::Result as IoResult,
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
vec,
};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::io::{AsyncRead, AsyncReadExt};

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Address {
Expand Down Expand Up @@ -72,15 +71,6 @@ impl Address {
}
}

pub async fn write_to<W>(&self, writer: &mut W) -> IoResult<()>
where
W: AsyncWrite + Unpin,
{
let mut buf = BytesMut::with_capacity(self.serialized_len());
self.write_to_buf(&mut buf);
writer.write_all(&buf).await
}

pub fn write_to_buf<B: BufMut>(&self, buf: &mut B) {
match self {
Self::SocketAddress(addr) => match addr {
Expand Down
8 changes: 0 additions & 8 deletions client/src/socks5/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ impl Command {
const CMD_BIND: u8 = 0x02;
const CMD_ASSOCIATE: u8 = 0x03;

pub fn as_u8(self) -> u8 {
match self {
Self::Connect => Self::CMD_CONNECT,
Self::Bind => Self::CMD_BIND,
Self::Associate => Self::CMD_ASSOCIATE,
}
}

pub fn from_u8(code: u8) -> Option<Self> {
match code {
Self::CMD_CONNECT => Some(Command::Connect),
Expand Down
15 changes: 0 additions & 15 deletions client/src/socks5/protocol/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,4 @@ impl Reply {
Self::Other(c) => c,
}
}

pub fn from_u8(code: u8) -> Self {
match code {
Self::REPLY_SUCCEEDED => Self::Succeeded,
Self::REPLY_GENERAL_FAILURE => Self::GeneralFailure,
Self::REPLY_CONNECTION_NOT_ALLOWED => Self::ConnectionNotAllowed,
Self::REPLY_NETWORK_UNREACHABLE => Self::NetworkUnreachable,
Self::REPLY_HOST_UNREACHABLE => Self::HostUnreachable,
Self::REPLY_CONNECTION_REFUSED => Self::ConnectionRefused,
Self::REPLY_TTL_EXPIRED => Self::TtlExpired,
Self::REPLY_COMMAND_NOT_SUPPORTED => Self::CommandNotSupported,
Self::REPLY_ADDRESS_TYPE_NOT_SUPPORTED => Self::AddressTypeNotSupported,
_ => Self::Other(code),
}
}
}

0 comments on commit 729145d

Please sign in to comment.