mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-06-08 07:37:41 +00:00
Beautify SOCKS implementation
This commit is contained in:
parent
9437308283
commit
f67d8b23a8
1 changed files with 30 additions and 12 deletions
|
@ -1,14 +1,16 @@
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use smoltcp::wire::IpProtocol;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::tun2proxy::{
|
use crate::tun2proxy::{
|
||||||
Connection, ConnectionManager, DestinationHost, Direction, IncomingDataEvent,
|
Connection, ConnectionManager, DestinationHost, Direction, IncomingDataEvent,
|
||||||
IncomingDirection, OutgoingDataEvent, OutgoingDirection, TcpProxy,
|
IncomingDirection, OutgoingDataEvent, OutgoingDirection, TcpProxy,
|
||||||
};
|
};
|
||||||
use crate::Credentials;
|
use crate::Credentials;
|
||||||
use smoltcp::wire::IpProtocol;
|
|
||||||
use std::collections::VecDeque;
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::net::{IpAddr, SocketAddr};
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -48,12 +50,22 @@ impl From<SocksAddressType> for u8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[repr(u8)]
|
||||||
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub enum SocksVersion {
|
pub enum SocksVersion {
|
||||||
V4 = 4,
|
V4 = 4,
|
||||||
V5 = 5,
|
V5 = 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum SocksCommand {
|
||||||
|
Connect = 1,
|
||||||
|
Bind = 2,
|
||||||
|
UdpAssociate = 3,
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
enum SocksAuthentication {
|
enum SocksAuthentication {
|
||||||
None = 0,
|
None = 0,
|
||||||
|
@ -118,8 +130,8 @@ impl SocksConnection {
|
||||||
match self.version {
|
match self.version {
|
||||||
SocksVersion::V4 => {
|
SocksVersion::V4 => {
|
||||||
self.server_outbuf.extend(&[
|
self.server_outbuf.extend(&[
|
||||||
4u8,
|
self.version as u8,
|
||||||
1,
|
SocksCommand::Connect as u8,
|
||||||
(self.connection.dst.port >> 8) as u8,
|
(self.connection.dst.port >> 8) as u8,
|
||||||
(self.connection.dst.port & 0xff) as u8,
|
(self.connection.dst.port & 0xff) as u8,
|
||||||
]);
|
]);
|
||||||
|
@ -152,11 +164,17 @@ impl SocksConnection {
|
||||||
|
|
||||||
SocksVersion::V5 => {
|
SocksVersion::V5 => {
|
||||||
if credentials.is_some() {
|
if credentials.is_some() {
|
||||||
self.server_outbuf
|
self.server_outbuf.extend(&[
|
||||||
.extend(&[5u8, 1, SocksAuthentication::Password as u8]);
|
self.version as u8,
|
||||||
|
SocksCommand::Connect as u8,
|
||||||
|
SocksAuthentication::Password as u8,
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
self.server_outbuf
|
self.server_outbuf.extend(&[
|
||||||
.extend(&[5u8, 1, SocksAuthentication::None as u8]);
|
self.version as u8,
|
||||||
|
SocksCommand::Connect as u8,
|
||||||
|
SocksAuthentication::None as u8,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue