style: Format source code and support grouping client
This commit is contained in:
115
common/mask.h
115
common/mask.h
@@ -5,79 +5,92 @@
|
||||
// 数据包协议封装格式
|
||||
// Copy left: 962914132@qq.com & ChatGPT
|
||||
enum PkgMaskType {
|
||||
MaskTypeUnknown = -1,
|
||||
MaskTypeNone,
|
||||
MaskTypeHTTP,
|
||||
MaskTypeNum,
|
||||
MaskTypeUnknown = -1,
|
||||
MaskTypeNone,
|
||||
MaskTypeHTTP,
|
||||
MaskTypeNum,
|
||||
};
|
||||
|
||||
#define DEFAULT_HOST "example.com"
|
||||
|
||||
inline ULONG UnMaskHttp(char* src, ULONG srcSize) {
|
||||
const char* header_end_mark = "\r\n\r\n";
|
||||
const ULONG mark_len = 4;
|
||||
inline ULONG UnMaskHttp(char* src, ULONG srcSize)
|
||||
{
|
||||
const char* header_end_mark = "\r\n\r\n";
|
||||
const ULONG mark_len = 4;
|
||||
|
||||
// 查找 HTTP 头部结束标记
|
||||
for (ULONG i = 0; i + mark_len <= srcSize; ++i) {
|
||||
if (memcmp(src + i, header_end_mark, mark_len) == 0) {
|
||||
return i + mark_len; // 返回 Body 起始位置
|
||||
}
|
||||
}
|
||||
return 0; // 无效数据
|
||||
// 查找 HTTP 头部结束标记
|
||||
for (ULONG i = 0; i + mark_len <= srcSize; ++i) {
|
||||
if (memcmp(src + i, header_end_mark, mark_len) == 0) {
|
||||
return i + mark_len; // 返回 Body 起始位置
|
||||
}
|
||||
}
|
||||
return 0; // 无效数据
|
||||
}
|
||||
|
||||
// TryUnMask 尝试去掉伪装的协议头.
|
||||
inline ULONG TryUnMask(char* src, ULONG srcSize, PkgMaskType& maskHit) {
|
||||
if (srcSize >= 5 && memcmp(src, "POST ", 5) == 0) {
|
||||
maskHit = MaskTypeHTTP;
|
||||
return UnMaskHttp(src, srcSize);
|
||||
}
|
||||
maskHit = MaskTypeNone;
|
||||
return 0;
|
||||
inline ULONG TryUnMask(char* src, ULONG srcSize, PkgMaskType& maskHit)
|
||||
{
|
||||
if (srcSize >= 5 && memcmp(src, "POST ", 5) == 0) {
|
||||
maskHit = MaskTypeHTTP;
|
||||
return UnMaskHttp(src, srcSize);
|
||||
}
|
||||
maskHit = MaskTypeNone;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// PkgMask 针对消息进一步加密、混淆或伪装.
|
||||
class PkgMask {
|
||||
class PkgMask
|
||||
{
|
||||
protected:
|
||||
virtual ~PkgMask() {}
|
||||
public:
|
||||
virtual void Destroy() {
|
||||
virtual void Destroy()
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
virtual void Mask(char*& dst, ULONG& dstSize, char* src, ULONG srcSize, int cmd = -1) {
|
||||
virtual void Mask(char*& dst, ULONG& dstSize, char* src, ULONG srcSize, int cmd = -1)
|
||||
{
|
||||
dst = src;
|
||||
dstSize = srcSize;
|
||||
}
|
||||
virtual ULONG UnMask(char* src, ULONG srcSize) {
|
||||
virtual ULONG UnMask(char* src, ULONG srcSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
virtual PkgMask* SetServer(const std::string& addr) { return this; }
|
||||
virtual PkgMaskType GetMaskType() const {
|
||||
virtual PkgMask* SetServer(const std::string& addr)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
virtual PkgMaskType GetMaskType() const
|
||||
{
|
||||
return MaskTypeNone;
|
||||
}
|
||||
};
|
||||
|
||||
class HttpMask : public PkgMask {
|
||||
class HttpMask : public PkgMask
|
||||
{
|
||||
public:
|
||||
virtual PkgMaskType GetMaskType() const override {
|
||||
return MaskTypeHTTP;
|
||||
}
|
||||
virtual PkgMaskType GetMaskType() const override
|
||||
{
|
||||
return MaskTypeHTTP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param host HTTP Host 头字段
|
||||
*/
|
||||
explicit HttpMask(const std::string& host, const std::map<std::string, std::string>& headers = {}) :
|
||||
product_(GenerateRandomString()), host_(host) {
|
||||
explicit HttpMask(const std::string& host, const std::map<std::string, std::string>& headers = {}) :
|
||||
product_(GenerateRandomString()), host_(host)
|
||||
{
|
||||
// 初始化随机数生成器
|
||||
srand(static_cast<unsigned>(time(nullptr)));
|
||||
char buf[32];
|
||||
sprintf_s(buf, "V%d.%d.%d", rand() % 10, rand() % 10, rand() % 10);
|
||||
version_ = buf;
|
||||
user_agent_ = GetEnhancedSystemUA(product_, version_);
|
||||
for (std::map<std::string, std::string>::const_iterator it = headers.begin(); it != headers.end(); ++it) {
|
||||
for (std::map<std::string, std::string>::const_iterator it = headers.begin(); it != headers.end(); ++it) {
|
||||
headers_ += it->first + ": " + it->second + "\r\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +101,8 @@ public:
|
||||
* @param srcSize 原始数据长度
|
||||
* @param cmd 命令号
|
||||
*/
|
||||
void Mask(char*& dst, ULONG& dstSize, char* src, ULONG srcSize, int cmd = -1) {
|
||||
void Mask(char*& dst, ULONG& dstSize, char* src, ULONG srcSize, int cmd = -1)
|
||||
{
|
||||
// 生成动态 HTTP 头部
|
||||
std::string http_header =
|
||||
"POST " + GeneratePath(cmd) + " HTTP/1.1\r\n"
|
||||
@@ -114,16 +128,19 @@ public:
|
||||
* @param srcSize 数据长度
|
||||
* @return ULONG 原始数据在 src 中的起始偏移量(失败返回 0)
|
||||
*/
|
||||
ULONG UnMask(char* src, ULONG srcSize) {
|
||||
ULONG UnMask(char* src, ULONG srcSize)
|
||||
{
|
||||
return UnMaskHttp(src, srcSize);
|
||||
}
|
||||
|
||||
PkgMask* SetServer(const std::string& addr) override {
|
||||
PkgMask* SetServer(const std::string& addr) override
|
||||
{
|
||||
host_ = addr;
|
||||
return this;
|
||||
}
|
||||
private:
|
||||
static std::string GetEnhancedSystemUA(const std::string& appName, const std::string& appVersion) {
|
||||
static std::string GetEnhancedSystemUA(const std::string& appName, const std::string& appVersion)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
OSVERSIONINFOEX osvi = {};
|
||||
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
||||
@@ -133,15 +150,15 @@ private:
|
||||
SYSTEM_INFO si;
|
||||
GetNativeSystemInfo(&si);
|
||||
std::string arch = (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) ? "Win64; x64" :
|
||||
(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM64) ? "Win64; ARM64" :
|
||||
"WOW64";
|
||||
(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM64) ? "Win64; ARM64" :
|
||||
"WOW64";
|
||||
|
||||
return "Mozilla/5.0 (" +
|
||||
std::string("Windows NT ") +
|
||||
std::to_string(osvi.dwMajorVersion) + "." +
|
||||
std::to_string(osvi.dwMinorVersion) + "; " +
|
||||
arch + ") " +
|
||||
appName + "/" + appVersion;
|
||||
std::string("Windows NT ") +
|
||||
std::to_string(osvi.dwMajorVersion) + "." +
|
||||
std::to_string(osvi.dwMinorVersion) + "; " +
|
||||
arch + ") " +
|
||||
appName + "/" + appVersion;
|
||||
#else
|
||||
return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36";
|
||||
#endif
|
||||
@@ -154,7 +171,8 @@ private:
|
||||
std::string headers_; // 自定义请求头
|
||||
|
||||
/** 生成随机 URL 路径 */
|
||||
std::string GenerateRandomString(int size = 8) const {
|
||||
std::string GenerateRandomString(int size = 8) const
|
||||
{
|
||||
static const char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
char path[32];
|
||||
for (int i = 0; i < size; ++i) {
|
||||
@@ -163,7 +181,8 @@ private:
|
||||
path[size] = 0;
|
||||
return path;
|
||||
}
|
||||
std::string GeneratePath(int cmd) const {
|
||||
std::string GeneratePath(int cmd) const
|
||||
{
|
||||
static std::string root = "/" + product_ + "/" + GenerateRandomString() + "/";
|
||||
return root + (cmd == -1 ? GenerateRandomString() : std::to_string(cmd));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user