mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-06-09 16:17:45 +00:00
Use Option type for credentials
This commit applys the diff by @ssrlive from
3223ca4e22 (commitcomment-105521241)
.
This commit is contained in:
parent
8dd075a7f4
commit
2f295c3fdc
7 changed files with 40 additions and 35 deletions
|
@ -82,7 +82,7 @@ impl SocksConnection {
|
|||
|
||||
fn send_client_hello(&mut self) {
|
||||
let credentials = self.manager.get_credentials();
|
||||
if credentials.authenticate {
|
||||
if credentials.is_some() {
|
||||
self.server_outbuf.extend(&[5u8, 1, 2]);
|
||||
} else {
|
||||
self.server_outbuf.extend(&[5u8, 1, 0]);
|
||||
|
@ -100,8 +100,8 @@ impl SocksConnection {
|
|||
));
|
||||
}
|
||||
|
||||
if self.server_inbuf[1] != 0 && !self.manager.get_credentials().authenticate
|
||||
|| self.server_inbuf[1] != 2 && self.manager.get_credentials().authenticate
|
||||
if self.server_inbuf[1] != 0 && self.manager.get_credentials().is_none()
|
||||
|| self.server_inbuf[1] != 2 && self.manager.get_credentials().is_some()
|
||||
{
|
||||
return Err(ProxyError::new(
|
||||
"SOCKS server requires an unsupported authentication method.".into(),
|
||||
|
@ -110,7 +110,7 @@ impl SocksConnection {
|
|||
|
||||
self.server_inbuf.drain(0..2);
|
||||
|
||||
if self.manager.get_credentials().authenticate {
|
||||
if self.manager.get_credentials().is_some() {
|
||||
self.state = SocksState::SendAuthData;
|
||||
} else {
|
||||
self.state = SocksState::SendRequest;
|
||||
|
@ -119,7 +119,8 @@ impl SocksConnection {
|
|||
}
|
||||
|
||||
fn send_auth_data(&mut self) -> Result<(), ProxyError> {
|
||||
let credentials = self.manager.get_credentials();
|
||||
let tmp = Credentials::default();
|
||||
let credentials = self.manager.get_credentials().as_ref().unwrap_or(&tmp);
|
||||
self.server_outbuf
|
||||
.extend(&[1u8, credentials.username.len() as u8]);
|
||||
self.server_outbuf.extend(&credentials.username);
|
||||
|
@ -285,7 +286,7 @@ impl TcpProxy for SocksConnection {
|
|||
|
||||
pub struct Socks5Manager {
|
||||
server: std::net::SocketAddr,
|
||||
credentials: Credentials,
|
||||
credentials: Option<Credentials>,
|
||||
}
|
||||
|
||||
impl ConnectionManager for Socks5Manager {
|
||||
|
@ -312,13 +313,13 @@ impl ConnectionManager for Socks5Manager {
|
|||
self.server
|
||||
}
|
||||
|
||||
fn get_credentials(&self) -> &Credentials {
|
||||
fn get_credentials(&self) -> &Option<Credentials> {
|
||||
&self.credentials
|
||||
}
|
||||
}
|
||||
|
||||
impl Socks5Manager {
|
||||
pub fn new(server: SocketAddr, credentials: Credentials) -> std::rc::Rc<Self> {
|
||||
pub fn new(server: SocketAddr, credentials: Option<Credentials>) -> std::rc::Rc<Self> {
|
||||
std::rc::Rc::new(Self {
|
||||
server,
|
||||
credentials,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue