Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackarain committed Nov 20, 2023
1 parent 2365174 commit 20f1158
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
7 changes: 7 additions & 0 deletions proxy/include/proxy/proxy_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3406,6 +3406,10 @@ R"x*x(<html>
continue;
}

static std::set<uint8_t> filter = {
0x04, 0x05, 0x16, 0x47, 0x50, 0x43
};

// plain socks4/5 protocol.
if (detect[0] == 0x05 || detect[0] == 0x04)
{
Expand Down Expand Up @@ -3489,6 +3493,9 @@ R"x*x(<html>

new_session->start();
}
else {
// 进入噪声过滤协议, 同时返回一段噪声给客户端.
}
}

XLOG_WARN << "start_proxy_listen exit ...";
Expand Down
138 changes: 138 additions & 0 deletions server/proxy_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,141 @@ int main(int argc, char** argv)

return EXIT_SUCCESS;
}


#include <random>
#include <vector>
#include <cstdint>
#include <climits>
#include <iostream>
#include <iomanip>

int start_position(std::mt19937& gen)
{
const static int pos[] =
{ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };

static std::normal_distribution<> dis(5, 4);

int num = static_cast<int>(dis(gen));
if (num < 0) num = 0;
if (num > 10) num = 10;

return pos[num];
}

inline std::vector<uint8_t>
generate_noise(uint16_t max_len = 0x7FFF, std::set<uint8_t> bfilter = {})
{
if (max_len <= 4)
return {};

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 255);

std::vector<uint8_t> data;

uint8_t bhalf = static_cast<uint8_t>(max_len >> 8);
uint8_t ehalf = static_cast<uint8_t>(max_len & 0xFF);

int start_pos = start_position(gen);
if (start_pos > max_len)
start_pos = (max_len / 2) * 2;

bool flip = false;
for (int i = 0; i < start_pos; i++)
{
uint8_t c = static_cast<uint8_t>(dis(gen));

if (flip)
c |= bhalf;
else
c |= ehalf;

if (i == 0 && !bfilter.empty())
{
while (bfilter.find(c) != bfilter.end())
{
c = static_cast<uint8_t>(dis(gen));
if (flip)
c |= bhalf;
else
c |= ehalf;
}
}

flip = !flip;

data.push_back(c);
}

uint16_t min_len = std::min<uint16_t>(max_len, static_cast<uint16_t>(start_pos));
if (min_len >= max_len)
min_len = max_len - 1;

auto length = std::uniform_int_distribution<>(min_len, max_len - 1)(gen);

data[start_pos - 2] = static_cast<uint8_t>(length >> 8);
data[start_pos - 1] = static_cast<uint8_t>(length & 0xFF);

data[start_pos - 4] |= static_cast<uint8_t>(min_len >> 8);
data[start_pos - 3] |= static_cast<uint8_t>(min_len & 0xFF);

uint16_t a = data[start_pos - 3] | (data[start_pos - 4] << CHAR_BIT);
uint16_t b = data[start_pos - 1] | (data[start_pos - 2] << CHAR_BIT);

length = a & b;

while (data.size() < length) {
data.push_back(static_cast<uint8_t>(dis(gen)));
}

return data;
}

void printVectorAsUint16(const std::vector<uint8_t>& data, uint16_t target_len = 0x7FFF)
{
if (data.size() < 4) {
std::cerr << "数据长度小于4,无法转换为 uint16_t。" << std::endl;
return;
}

std::cout << "print len: " << data.size() << "\n";

uint16_t p1 = 0;
p1 = data[0] | (data[1] << 8);
bool found = false;
int count = 0;
// 从第二个字节开始,确保每个 uint16_t 的前一个字节是前一个 uint16_t 的后一个字节
for (size_t i = 1; i < data.size(); i += 2) {
// 组合当前字节和前一个字节为 uint16_t
uint16_t value = static_cast<uint16_t>(data[i]) | (static_cast<uint16_t>(data[i - 1]) << 8);

// 设置字段宽度和左对齐
std::cout << std::left << std::setw(10) << value;

if ((p1 & value) < target_len && !found && i != 1) {
found = true;
std::cout << "decode len: " << (p1 & value) << "\n";
}

p1 = value;

count++;
if (count == 8) { // 每输出8个数值后换行
std::cout << std::endl;
count = 0;
}
}

if (count > 0) {
std::cout << std::endl; // 保证最后一行在输出后有换行
}
}

int main2() {
uint16_t max_len = 0x001F;
auto result = generate_noise(max_len);
printVectorAsUint16(result, max_len);
}

0 comments on commit 20f1158

Please sign in to comment.