-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathcodec.rs
More file actions
135 lines (111 loc) · 3.4 KB
/
codec.rs
File metadata and controls
135 lines (111 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::io::ErrorKind;
use bytes::{Buf, BufMut, BytesMut};
use quinn_proto::{coding::Codec, VarInt};
use rand::distr::Alphanumeric;
use tokio_util::codec::{Decoder, Encoder};
use crate::session::SocksAddr;
pub struct Hy2TcpCodec;
/// ### format
///
/// ```text
/// [uint8] Status (0x00 = OK, 0x01 = Error)
/// [varint] Message length
/// [bytes] Message string
/// [varint] Padding length
/// [bytes] Random padding
/// ```
#[derive(Debug)]
pub struct Hy2TcpResp {
pub status: u8,
pub msg: String,
}
impl Decoder for Hy2TcpCodec {
type Error = std::io::Error;
type Item = Hy2TcpResp;
fn decode(
&mut self,
src: &mut BytesMut,
) -> Result<Option<Self::Item>, Self::Error> {
if !src.has_remaining() {
return Err(ErrorKind::UnexpectedEof.into());
}
let status = src.get_u8();
let msg_len = VarInt::decode(src)
.map_err(|_| ErrorKind::InvalidData)?
.into_inner() as usize;
if src.remaining() < msg_len {
return Err(ErrorKind::UnexpectedEof.into());
}
let msg: Vec<u8> = src.split_to(msg_len).into();
let msg: String = String::from_utf8(msg)
.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))?;
let padding_len = VarInt::decode(src)
.map_err(|_| ErrorKind::UnexpectedEof)?
.into_inner() as usize;
if src.remaining() < padding_len {
return Err(ErrorKind::UnexpectedEof.into());
}
src.advance(padding_len);
Ok(Hy2TcpResp { status, msg }.into())
}
}
#[inline]
pub fn padding(range: std::ops::RangeInclusive<u32>) -> Vec<u8> {
use rand::Rng;
let mut rng = rand::rng();
let len = rng.random_range(range) as usize;
rng.sample_iter(Alphanumeric).take(len).collect()
}
impl Encoder<&'_ SocksAddr> for Hy2TcpCodec {
type Error = std::io::Error;
fn encode(
&mut self,
item: &'_ SocksAddr,
buf: &mut BytesMut,
) -> Result<(), Self::Error> {
const REQ_ID: VarInt = VarInt::from_u32(0x401);
let padding = padding(64..=512);
let padding_var = VarInt::from_u32(padding.len() as u32);
let addr = item.to_string().into_bytes();
let addr_var = VarInt::from_u32(addr.len() as u32);
buf.reserve(
var_size(REQ_ID)
+ var_size(padding_var)
+ var_size(addr_var)
+ addr.len()
+ padding.len(),
);
REQ_ID.encode(buf);
addr_var.encode(buf);
buf.put_slice(&addr);
padding_var.encode(buf);
buf.put_slice(&padding);
Ok(())
}
}
/// Compute the number of bytes needed to encode this value
pub fn var_size(var: VarInt) -> usize {
let x = var.into_inner();
if x < 2u64.pow(6) {
1
} else if x < 2u64.pow(14) {
2
} else if x < 2u64.pow(30) {
4
} else if x < 2u64.pow(62) {
8
} else {
unreachable!("malformed VarInt");
}
}
#[test]
fn hy2_resp_parse() {
let mut src = BytesMut::from(&[0x00, 0x03, 0x61, 0x62, 0x63, 0x00][..]);
let msg = Hy2TcpCodec.decode(&mut src).unwrap().unwrap();
assert!(msg.status == 0);
assert!(msg.msg == "abc");
let mut src = BytesMut::from(&[0x01, 0x00, 0x00][..]);
let msg = Hy2TcpCodec.decode(&mut src).unwrap().unwrap();
assert!(msg.status == 0x1);
assert!(msg.msg.is_empty());
}