Created
June 20, 2024 11:21
-
-
Save afpro/7982b6426d03617c3154d220d92639ee to your computer and use it in GitHub Desktop.
socks5 protocol abstraction, based on deku.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use deku::prelude::*; | |
pub const SOCKS5_VERSION: u8 = 0x05; | |
#[derive(Default, Copy, Clone, DekuRead, DekuWrite)] | |
#[deku(id_type = "u8")] | |
pub enum Version { | |
#[default] | |
#[deku(id = 0x05)] | |
Socks5, | |
} | |
#[derive(Default, Copy, Clone, DekuRead, DekuWrite)] | |
#[deku(id_type = "u8")] | |
pub enum ReservedZero { | |
#[default] | |
#[deku(id = 0x00)] | |
Correct, | |
} | |
#[derive(Copy, Clone, PartialEq, Eq, DekuRead, DekuWrite)] | |
#[deku(id_type = "u8")] | |
pub enum Method { | |
#[deku(id = 0x00)] | |
NoAuthRequired, | |
#[deku(id = 0x01)] | |
GSSApi, | |
#[deku(id = 0x02)] | |
Basic, | |
#[deku(id_pat = "0x03 ..= 0x7F")] | |
INNAAssigned(u8), | |
#[deku(id_pat = "0x80 ..= 0xFE")] | |
Reserved(u8), | |
#[deku(id = 0xFF)] | |
NoAcceptable, | |
} | |
#[derive(DekuRead, DekuWrite)] | |
pub struct ClientHello { | |
pub version: Version, | |
pub methods_count: u8, | |
#[deku(count = "methods_count")] | |
pub methods: Vec<Method>, | |
} | |
#[derive(DekuRead, DekuWrite)] | |
pub struct ServerHello { | |
pub version: Version, | |
pub method: Method, | |
} | |
#[derive(Copy, Clone, PartialEq, Eq, DekuRead, DekuWrite)] | |
#[deku(id_type = "u8")] | |
pub enum ClientCommand { | |
#[deku(id = 0x01)] | |
Connect, | |
#[deku(id = 0x02)] | |
Bind, | |
#[deku(id = 0x03)] | |
UdpAssociate, | |
} | |
#[derive(Clone, PartialEq, Eq, DekuRead, DekuWrite)] | |
#[deku(id_type = "u8")] | |
pub enum Addr { | |
#[deku(id = 0x01)] | |
V4([u8; 4]), | |
#[deku(id = 0x03)] | |
Domain { | |
count: u8, | |
#[deku(count = "count")] | |
domain: Vec<u8>, | |
}, | |
#[deku(id = 0x04)] | |
V6([u8; 16]), | |
} | |
#[derive(DekuRead, DekuWrite)] | |
pub struct ClientRequest { | |
pub version: Version, | |
pub command: ClientCommand, | |
pub reserved: ReservedZero, | |
pub addr: Addr, | |
#[deku(endian = "big")] | |
pub port: u16, | |
} | |
#[derive(Default, Copy, Clone, PartialEq, Eq, DekuRead, DekuWrite)] | |
#[deku(id_type = "u8")] | |
pub enum ServerReply { | |
#[default] | |
#[deku(id = 0x00)] | |
Succeed, | |
#[deku(id = 0x01)] | |
GeneralSocksServerFailure, | |
#[deku(id = 0x02)] | |
ConnectionNotAllowedByRuleset, | |
#[deku(id = 0x03)] | |
NetworkUnreachable, | |
#[deku(id = 0x04)] | |
HostUnreachable, | |
#[deku(id = 0x05)] | |
ConnectionRefused, | |
#[deku(id = 0x06)] | |
TTLExpired, | |
#[deku(id = 0x07)] | |
CommandNotSupported, | |
#[deku(id = 0x08)] | |
AddressTypeNotSupported, | |
} | |
#[derive(DekuRead, DekuWrite)] | |
pub struct ServerResponse { | |
pub version: Version, | |
pub reply: ServerReply, | |
pub reserved: ReservedZero, | |
pub bind_addr: Addr, | |
#[deku(endian = "big")] | |
pub bind_port: u16, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment