Skip to content

Commit

Permalink
支持socks5代理通过update.node
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Nov 21, 2024
1 parent 90b5b08 commit 3a07795
Show file tree
Hide file tree
Showing 14 changed files with 921 additions and 98 deletions.
96 changes: 96 additions & 0 deletions base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "base64.h"

const std::string Base64::base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

bool Base64::is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}

std::string Base64::Encode(const std::string& input) {
return Encode(reinterpret_cast<const unsigned char*>(input.c_str()), input.length());
}

std::string Base64::Encode(const unsigned char* input, size_t length) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];

while (length--) {
char_array_3[i++] = *(input++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for (i = 0; i < 4; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}

if (i) {
for (j = i; j < 3; j++)
char_array_3[j] = '\0';

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for (j = 0; j < i + 1; j++)
ret += base64_chars[char_array_4[j]];

while (i++ < 3)
ret += '=';
}

return ret;
}

std::string Base64::Decode(const std::string& encoded_string) {
size_t in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;

while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4) {
for (i = 0; i < 4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (i = 0; i < 3; i++)
ret += char_array_3[i];
i = 0;
}
}

if (i) {
for (j = i; j < 4; j++)
char_array_4[j] = 0;

for (j = 0; j < 4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (j = 0; j < i - 1; j++)
ret += char_array_3[j];
}

return ret;
}
17 changes: 17 additions & 0 deletions base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <vector>

class Base64 {
public:
static std::string Encode(const std::string& input);
static std::string Encode(const unsigned char* input, size_t length);

static std::string Decode(const std::string& input);

private:
static const std::string base64_chars;
static inline bool is_base64(unsigned char c);
};

24 changes: 12 additions & 12 deletions detours/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,7 @@ BYTE CDetourDis::CopyMiscellaneous16(BYTE* pSource, BYTE* pDest)
return sizeof(USHORT); // The source instruction was 16 bits
}

// If that fails, re-encode with 'conditional branch' logic, without using the condition flags
// If that fails, re-Encode with 'conditional branch' logic, without using the condition flags
// For example, cbz r2,+0x56 (0x90432) becomes:
//
// 001df73a b92a cbnz r2,001df748
Expand Down Expand Up @@ -2959,7 +2959,7 @@ BYTE CDetourDis::CopyConditionalBranchOrOther16(BYTE* pSource, BYTE* pDest)
return sizeof(USHORT); // The source instruction was 16 bits
}

// If that fails, re-encode as a sequence of branches
// If that fails, re-Encode as a sequence of branches
// For example, bne +0x6E (0x90452) becomes:
//
// 001df758 d100 bne 001df75c
Expand All @@ -2978,13 +2978,13 @@ BYTE CDetourDis::CopyConditionalBranchOrOther16(BYTE* pSource, BYTE* pDest)
*pDstInst++ = newInstruction;

// Next, prepare to insert an unconditional branch that will be hit if the condition above is not met. This branch will branch over the following 'long branch'
// We can't actually encode this branch yet though, because 'long branches' can vary in size
// We can't actually Encode this branch yet though, because 'long branches' can vary in size
PUSHORT pUnconditionalBranchInstruction = pDstInst++;

// Then, emit a 'long branch' that will be hit if the original condition is met
BYTE longBranchSize = EmitLongBranch(pDstInst, pTarget);

// Finally, encode and emit the unconditional branch that will be used to branch past the 'long branch' if the initial condition was not met
// Finally, Encode and emit the unconditional branch that will be used to branch past the 'long branch' if the initial condition was not met
Branch11 branch11 = { 0x00, 0x1C };
newInstruction = EncodeBranch11(*(DWORD*)(&branch11), longBranchSize - c_PCAdjust + sizeof(USHORT));
ASSERT(newInstruction);
Expand Down Expand Up @@ -3014,7 +3014,7 @@ BYTE CDetourDis::CopyUnConditionalBranch16(BYTE* pSource, BYTE* pDest)
return sizeof(USHORT); // The source instruction was 16 bits
}

// If that fails, re-encode as 32-bit
// If that fails, re-Encode as 32-bit
PUSHORT pDstInst = (PUSHORT)(pDest);
instruction = EncodeBranch24(0xf0009000, newDelta, FALSE);
if (instruction) {
Expand Down Expand Up @@ -3050,7 +3050,7 @@ BYTE CDetourDis::CopyLiteralLoad16(BYTE* pSource, BYTE* pDest)
LONG oldDelta = DecodeLiteralLoad8(instruction);
PBYTE pTarget = CalculateTarget(Align4(pSource), oldDelta);

// Re-encode as a 'long literal load'
// Re-Encode as a 'long literal load'
// For example, ldr r0, [PC + 1E0] (0x905B4) becomes:
//
// 001df72c f85f0008 ldr r0,=0x905B4
Expand Down Expand Up @@ -3164,7 +3164,7 @@ BYTE CDetourDis::CopyBranch24(BYTE* pSource, BYTE* pDest)
PBYTE pTarget = CalculateTarget(pSource, oldDelta);
m_pbTarget = pTarget;

// Re-encode as 32-bit
// Re-Encode as 32-bit
PUSHORT pDstInst = (PUSHORT)(pDest);
LONG newDelta = CalculateNewDelta(pTarget, pDest);
instruction = EncodeBranch24(instruction, newDelta, fLink);
Expand All @@ -3174,7 +3174,7 @@ BYTE CDetourDis::CopyBranch24(BYTE* pSource, BYTE* pDest)
return sizeof(DWORD);
}

// If that fails, re-encode as a 'long branch'
// If that fails, re-Encode as a 'long branch'
EmitLongBranch(pDstInst, pTarget);

// Compute the extra space needed for the instruction
Expand All @@ -3190,7 +3190,7 @@ BYTE CDetourDis::CopyBranchOrMiscellaneous32(BYTE* pSource, BYTE* pDest)
PBYTE pTarget = CalculateTarget(pSource, oldDelta);
m_pbTarget = pTarget;

// Re-encode as 32-bit
// Re-Encode as 32-bit
PUSHORT pDstInst = (PUSHORT)(pDest);
LONG newDelta = CalculateNewDelta(pTarget, pDest);
instruction = EncodeBranch20(instruction, newDelta);
Expand All @@ -3200,7 +3200,7 @@ BYTE CDetourDis::CopyBranchOrMiscellaneous32(BYTE* pSource, BYTE* pDest)
return sizeof(DWORD);
}

// If that fails, re-encode as a sequence of branches
// If that fails, re-Encode as a sequence of branches
// For example, bls.w +0x86 (00090480)| becomes:
//
// 001df788 f2408001 bls.w 001df78e
Expand All @@ -3222,14 +3222,14 @@ BYTE CDetourDis::CopyBranchOrMiscellaneous32(BYTE* pSource, BYTE* pDest)
// Next, prepare to insert an unconditional branch that will be hit
// if the condition above is not met. This branch will branch over
// the following 'long branch'
// We can't actually encode this branch yet though, because
// We can't actually Encode this branch yet though, because
// 'long branches' can vary in size
PUSHORT pUnconditionalBranchInstruction = pDstInst++;

// Then, emit a 'long branch' that will be hit if the original condition is met
BYTE longBranchSize = EmitLongBranch(pDstInst, pTarget);

// Finally, encode and emit the unconditional branch that will be used
// Finally, Encode and emit the unconditional branch that will be used
// to branch past the 'long branch' if the initial condition was not met
Branch11 branch11 = { 0x00, 0x1C };
instruction = EncodeBranch11(*(DWORD*)(&branch11), longBranchSize - c_PCAdjust + sizeof(USHORT));
Expand Down
Loading

0 comments on commit 3a07795

Please sign in to comment.