Compare commits
6
Commits
dev-v0.2.58
...
v1.1.54
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e6ae56753 | ||
|
|
deae50f9d4 | ||
|
|
daa21a57db | ||
|
|
d8f09b9fe2 | ||
|
|
aeeea2a527 | ||
|
|
55004368e4 |
No files matched your search
@@ -100,8 +100,8 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_exec.so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_exec.so
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_dev_"${{ env.PACKAGE_VERSION }}".so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_dev_"${{ env.PACKAGE_VERSION }}".so
|
||||
|
||||
find "$VCPKG_ROOT/installed/arm64-android" -type f -name "*.so*" -exec cp -v {} out/android/arm64-v8a/ \; || true
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.53
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
|
||||
jobs:
|
||||
build-android:
|
||||
@@ -100,8 +100,8 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_exec.so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_exec.so
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_"${{ env.PACKAGE_VERSION }}".so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_"${{ env.PACKAGE_VERSION }}".so
|
||||
|
||||
find "$VCPKG_ROOT/installed/arm64-android" -type f -name "*.so*" -exec cp -v {} out/android/arm64-v8a/ \; || true
|
||||
|
||||
|
||||
@@ -100,8 +100,8 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_exec.so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_exec.so
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_mori_"${{ env.PACKAGE_VERSION }}".so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_mori_"${{ env.PACKAGE_VERSION }}".so
|
||||
|
||||
find "$VCPKG_ROOT/installed/arm64-android" -type f -name "*.so*" -exec cp -v {} out/android/arm64-v8a/ \; || true
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.53
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
@@ -36,13 +36,13 @@ jobs:
|
||||
update: true
|
||||
install: >-
|
||||
git
|
||||
zip
|
||||
mingw-w64-x86_64-gcc
|
||||
mingw-w64-x86_64-cmake
|
||||
mingw-w64-x86_64-ninja
|
||||
mingw-w64-x86_64-boost
|
||||
mingw-w64-x86_64-openssl
|
||||
mingw-w64-x86_64-yaml-cpp
|
||||
mingw-w64-x86_64-zip
|
||||
|
||||
- name: Build nipovpn.exe
|
||||
run: |
|
||||
@@ -51,7 +51,8 @@ jobs:
|
||||
|
||||
cmake -S . -B build \
|
||||
-G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Release
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_CXX_FLAGS="-DNOMINMAX -DWIN32_LEAN_AND_MEAN -DNOGDI -DOUT=NIPO_OUT"
|
||||
|
||||
cmake --build build -j$(nproc)
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.53
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
|
||||
jobs:
|
||||
build-windows:
|
||||
@@ -36,13 +36,13 @@ jobs:
|
||||
update: true
|
||||
install: >-
|
||||
git
|
||||
zip
|
||||
mingw-w64-x86_64-gcc
|
||||
mingw-w64-x86_64-cmake
|
||||
mingw-w64-x86_64-ninja
|
||||
mingw-w64-x86_64-boost
|
||||
mingw-w64-x86_64-openssl
|
||||
mingw-w64-x86_64-yaml-cpp
|
||||
mingw-w64-x86_64-zip
|
||||
|
||||
- name: Build nipovpn.exe
|
||||
run: |
|
||||
@@ -51,7 +51,8 @@ jobs:
|
||||
|
||||
cmake -S . -B build \
|
||||
-G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Release
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_CXX_FLAGS="-DNOMINMAX -DWIN32_LEAN_AND_MEAN -DNOGDI -DOUT=NIPO_OUT"
|
||||
|
||||
cmake --build build -j$(nproc)
|
||||
|
||||
@@ -86,7 +87,7 @@ jobs:
|
||||
name: nipovpn-windows-x86_64
|
||||
path: nipovpn_${{ env.PACKAGE_VERSION }}_windows_x86_64.zip
|
||||
|
||||
- name: Create main Windows Release
|
||||
- name: Create Windows Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tag_name: windows-v${{ env.PACKAGE_VERSION }}
|
||||
@@ -94,4 +95,4 @@ jobs:
|
||||
prerelease: true
|
||||
files: nipovpn_${{ env.PACKAGE_VERSION }}_windows_x86_64.zip
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
@@ -10,6 +10,10 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -ggdb")
|
||||
# Set the preprocessor flags
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -MMD -MP")
|
||||
|
||||
if(POLICY CMP0167)
|
||||
cmake_policy(SET CMP0167 NEW)
|
||||
endif()
|
||||
|
||||
# https://cmake.org/cmake/help/latest/module/FindBoost.html
|
||||
find_package(Boost REQUIRED COMPONENTS regex OPTIONAL_COMPONENTS system)
|
||||
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
#include "agenthandler.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
AgentHandler::AgentHandler(boost::asio::streambuf &readBuffer,
|
||||
boost::asio::streambuf &writeBuffer,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
const TCPClient::pointer &client,
|
||||
const std::string &clientConnStr,
|
||||
TCPClient::pointer client,
|
||||
std::string clientConnStr,
|
||||
boost::uuids::uuid uuid)
|
||||
: config_(config),
|
||||
log_(log),
|
||||
client_(client),
|
||||
client_(std::move(client)),
|
||||
readBuffer_(readBuffer),
|
||||
writeBuffer_(writeBuffer),
|
||||
request_(HTTP::create(config, log, readBuffer, uuid)),
|
||||
clientConnStr_(clientConnStr),
|
||||
clientConnStr_(std::move(clientConnStr)),
|
||||
uuid_(uuid) {
|
||||
end_ = false;
|
||||
connect_ = false;
|
||||
|
||||
@@ -18,8 +18,8 @@ public:
|
||||
boost::asio::streambuf &writeBuffer,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
const TCPClient::pointer &client,
|
||||
const std::string &clientConnStr,
|
||||
TCPClient::pointer client,
|
||||
std::string clientConnStr,
|
||||
boost::uuids::uuid uuid) {
|
||||
return pointer(new AgentHandler(readBuffer, writeBuffer, config, log,
|
||||
client, clientConnStr, uuid));
|
||||
@@ -29,9 +29,7 @@ public:
|
||||
|
||||
void handle();
|
||||
|
||||
inline const HTTP::pointer &request() & { return request_; }
|
||||
|
||||
inline const HTTP::pointer &&request() && { return std::move(request_); }
|
||||
inline const HTTP::pointer &request() const { return request_; }
|
||||
|
||||
bool end_;
|
||||
bool connect_;
|
||||
@@ -41,18 +39,18 @@ private:
|
||||
boost::asio::streambuf &writeBuffer,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
const TCPClient::pointer &client,
|
||||
const std::string &clientConnStr,
|
||||
TCPClient::pointer client,
|
||||
std::string clientConnStr,
|
||||
boost::uuids::uuid uuid);
|
||||
|
||||
std::string relayHostHeader(const std::shared_ptr<Config> &config);
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
const TCPClient::pointer &client_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
TCPClient::pointer client_;
|
||||
boost::asio::streambuf &readBuffer_;
|
||||
boost::asio::streambuf &writeBuffer_;
|
||||
HTTP::pointer request_;
|
||||
const std::string &clientConnStr_;
|
||||
std::string clientConnStr_;
|
||||
|
||||
boost::uuids::uuid uuid_;
|
||||
|
||||
|
||||
+6
-4
@@ -1,7 +1,7 @@
|
||||
#include "config.hpp"
|
||||
|
||||
|
||||
Config::Config(const RunMode &mode, const std::string &filePath)
|
||||
Config::Config(RunMode mode, const std::string &filePath)
|
||||
: runMode_(mode),
|
||||
filePath_(filePath),
|
||||
configYaml_(YAML::LoadFile(filePath)),
|
||||
@@ -9,6 +9,7 @@ Config::Config(const RunMode &mode, const std::string &filePath)
|
||||
listenIp_("127.0.0.1"),
|
||||
listenPort_(0),
|
||||
general_({configYaml_["general"]["token"].as<std::string>(),
|
||||
configYaml_["general"]["protocol"].as<std::string>(""),
|
||||
configYaml_["general"]["fakeUrls"].as<std::vector<std::string>>(),
|
||||
configYaml_["general"]["methods"].as<std::vector<std::string>>(),
|
||||
configYaml_["general"]["endPoints"].as<std::vector<std::string>>(),
|
||||
@@ -73,6 +74,7 @@ std::string Config::toString() const {
|
||||
ss << "\nConfig :\n"
|
||||
<< " General :\n"
|
||||
<< " token: " << general_.token << "\n"
|
||||
<< " protocol: " << general_.protocol << "\n"
|
||||
<< " fakeUrls: ";
|
||||
|
||||
for (std::size_t i = 0; i < general_.fakeUrls.size(); ++i) {
|
||||
@@ -157,7 +159,7 @@ const Config::Agent &Config::agent() const {
|
||||
return agent_;
|
||||
}
|
||||
|
||||
const unsigned short &Config::threads() const {
|
||||
unsigned short Config::threads() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
return threads_;
|
||||
}
|
||||
@@ -177,7 +179,7 @@ void Config::listenIp(const std::string &ip) {
|
||||
listenIp_ = ip;
|
||||
}
|
||||
|
||||
const unsigned short &Config::listenPort() const {
|
||||
unsigned short Config::listenPort() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
return listenPort_;
|
||||
}
|
||||
@@ -187,7 +189,7 @@ void Config::listenPort(unsigned short port) {
|
||||
listenPort_ = port;
|
||||
}
|
||||
|
||||
const RunMode &Config::runMode() const {
|
||||
RunMode Config::runMode() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
return runMode_;
|
||||
}
|
||||
|
||||
+8
-7
@@ -20,6 +20,7 @@ class Config : private Uncopyable {
|
||||
private:
|
||||
struct General {
|
||||
std::string token;
|
||||
std::string protocol;
|
||||
std::vector<std::string> fakeUrls;
|
||||
std::vector<std::string> methods;
|
||||
std::vector<std::string> endPoints;
|
||||
@@ -55,9 +56,9 @@ private:
|
||||
std::string userAgent;
|
||||
};
|
||||
|
||||
const RunMode &runMode_;
|
||||
RunMode runMode_;
|
||||
std::string filePath_;
|
||||
const YAML::Node configYaml_;
|
||||
YAML::Node configYaml_;
|
||||
|
||||
unsigned short threads_;
|
||||
std::string listenIp_;
|
||||
@@ -65,12 +66,12 @@ private:
|
||||
|
||||
mutable std::mutex configMutex_;
|
||||
|
||||
explicit Config(const RunMode &mode, const std::string &filePath);
|
||||
explicit Config(RunMode mode, const std::string &filePath);
|
||||
|
||||
public:
|
||||
using pointer = std::shared_ptr<Config>;
|
||||
|
||||
static pointer create(const RunMode &mode, const std::string &filePath) {
|
||||
static pointer create(RunMode mode, const std::string &filePath) {
|
||||
return pointer(new Config(mode, filePath));
|
||||
}
|
||||
|
||||
@@ -88,7 +89,7 @@ public:
|
||||
const Server &server() const;
|
||||
const Agent &agent() const;
|
||||
|
||||
const unsigned short &threads() const;
|
||||
unsigned short threads() const;
|
||||
|
||||
void threads(unsigned short threads);
|
||||
|
||||
@@ -96,11 +97,11 @@ public:
|
||||
|
||||
void listenIp(const std::string &ip);
|
||||
|
||||
const unsigned short &listenPort() const;
|
||||
unsigned short listenPort() const;
|
||||
|
||||
void listenPort(unsigned short port);
|
||||
|
||||
const RunMode &runMode() const;
|
||||
RunMode runMode() const;
|
||||
|
||||
const std::string &filePath() const;
|
||||
|
||||
|
||||
+20
-19
@@ -19,10 +19,13 @@
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static constexpr std::size_t BufferSize = 8192;
|
||||
|
||||
class Uncopyable {
|
||||
public:
|
||||
Uncopyable() = default;
|
||||
@@ -86,7 +89,7 @@ inline std::string hexArrToStr(const unsigned char *data, std::size_t size) {
|
||||
}
|
||||
|
||||
inline unsigned int hexToInt(const std::string &hexString) {
|
||||
unsigned short result;
|
||||
unsigned int result{};
|
||||
std::stringstream hexStr(hexString);
|
||||
hexStr >> std::hex >> result;
|
||||
return result;
|
||||
@@ -195,40 +198,36 @@ inline BoolStr aes256Encrypt(const std::string &plaintext,
|
||||
plaintext.size() + EVP_CIPHER_block_size(EVP_aes_256_cbc());
|
||||
std::vector<unsigned char> ciphertext(ciphertext_len);
|
||||
|
||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
||||
using EvpCipherCtxPtr = std::unique_ptr<EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)>;
|
||||
EvpCipherCtxPtr ctx(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free);
|
||||
if (!ctx) {
|
||||
result.message = "Context initialization failed";
|
||||
return result;
|
||||
}
|
||||
|
||||
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
|
||||
if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL,
|
||||
reinterpret_cast<const unsigned char *>(key.c_str()),
|
||||
iv) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
result.message = "Encryption initialization failed";
|
||||
return result;
|
||||
}
|
||||
|
||||
int len;
|
||||
if (EVP_EncryptUpdate(
|
||||
ctx, ciphertext.data(), &len,
|
||||
ctx.get(), ciphertext.data(), &len,
|
||||
reinterpret_cast<const unsigned char *>(plaintext.c_str()),
|
||||
plaintext.size()) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
result.message = "Encryption update failed";
|
||||
return result;
|
||||
}
|
||||
ciphertext_len = len;
|
||||
|
||||
if (EVP_EncryptFinal_ex(ctx, ciphertext.data() + len, &len) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
if (EVP_EncryptFinal_ex(ctx.get(), ciphertext.data() + len, &len) != 1) {
|
||||
result.message = "Encryption finalization failed";
|
||||
return result;
|
||||
}
|
||||
ciphertext_len += len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
std::string final_result(reinterpret_cast<char *>(iv), sizeof(iv));
|
||||
final_result.append(reinterpret_cast<char *>(ciphertext.data()),
|
||||
ciphertext_len);
|
||||
@@ -252,40 +251,36 @@ inline BoolStr aes256Decrypt(const std::string &ciphertext_with_iv,
|
||||
int max_plaintext_len = ciphertext.size();
|
||||
std::vector<unsigned char> plaintext(max_plaintext_len);
|
||||
|
||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
||||
using EvpCipherCtxPtr = std::unique_ptr<EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)>;
|
||||
EvpCipherCtxPtr ctx(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free);
|
||||
if (!ctx) {
|
||||
result.message = "Context initialization failed";
|
||||
return result;
|
||||
}
|
||||
|
||||
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
|
||||
if (EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL,
|
||||
reinterpret_cast<const unsigned char *>(key.c_str()),
|
||||
iv) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
result.message = "Decryption initialization failed";
|
||||
return result;
|
||||
}
|
||||
|
||||
int len;
|
||||
if (EVP_DecryptUpdate(
|
||||
ctx, plaintext.data(), &len,
|
||||
ctx.get(), plaintext.data(), &len,
|
||||
reinterpret_cast<const unsigned char *>(ciphertext.c_str()),
|
||||
ciphertext.size()) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
result.message = "Decryption update failed";
|
||||
return result;
|
||||
}
|
||||
int plaintext_len = len;
|
||||
|
||||
if (EVP_DecryptFinal_ex(ctx, plaintext.data() + len, &len) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
if (EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len) != 1) {
|
||||
result.message = "Decryption finalization failed";
|
||||
return result;
|
||||
}
|
||||
plaintext_len += len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
result.ok = true;
|
||||
result.message =
|
||||
std::string(reinterpret_cast<char *>(plaintext.data()), plaintext_len);
|
||||
@@ -325,6 +320,7 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
|
||||
try {
|
||||
configYaml["general"]["token"].as<std::string>();
|
||||
configYaml["general"]["protocol"].as<std::string>();
|
||||
configYaml["general"]["fakeUrls"].as<std::vector<std::string>>();
|
||||
configYaml["general"]["methods"].as<std::vector<std::string>>();
|
||||
configYaml["general"]["endPoints"].as<std::vector<std::string>>();
|
||||
@@ -364,6 +360,11 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (configYaml["general"]["protocol"].as<std::string>() != "http" && configYaml["general"]["protocol"].as<std::string>() != "socks5") {
|
||||
result.message = "First argument must be one of [http|socks5]\n";
|
||||
return result;
|
||||
}
|
||||
|
||||
result.ok = true;
|
||||
result.message = "OK";
|
||||
return result;
|
||||
|
||||
+13
-13
@@ -155,7 +155,7 @@ bool HTTP::parseTls() {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
auto fakeUrl = config_->randomFakeUrl();
|
||||
return std::string(config_->randomMethod() + " " +
|
||||
fakeUrl + " HTTP/" +
|
||||
@@ -167,7 +167,7 @@ const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" + "\r\n" + body + "COMP\r\n\r\n";
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpRestPostReqString() const {
|
||||
std::string HTTP::genHttpRestPostReqString() const {
|
||||
auto fakeUrl = config_->randomFakeUrl();
|
||||
return std::string(config_->randomMethod() + " " +
|
||||
fakeUrl + " HTTP/" +
|
||||
@@ -178,7 +178,7 @@ const std::string HTTP::genHttpRestPostReqString() const {
|
||||
"Rest: yes\r\n" + "COMP\r\n\r\n";
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpOkResString(const std::string &body) const {
|
||||
std::string HTTP::genHttpOkResString(const std::string &body) const {
|
||||
return std::string("HTTP/1.1 200 OK\r\n") +
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
|
||||
@@ -196,14 +196,14 @@ void HTTP::setIPPort() {
|
||||
splitted = splitString(target, ":");
|
||||
|
||||
if (!splitted.empty()) {
|
||||
dstIP_ = splitted[0];
|
||||
dstIp_ = splitted[0];
|
||||
if (splitted.size() > 1 && !splitted[1].empty()) {
|
||||
dstPort_ = std::stoi(splitted[1]);
|
||||
} else {
|
||||
dstPort_ = 443;
|
||||
}
|
||||
} else {
|
||||
dstIP_.clear();
|
||||
dstIp_.clear();
|
||||
dstPort_ = 443;
|
||||
}
|
||||
break;
|
||||
@@ -234,7 +234,7 @@ void HTTP::setIPPort() {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [HTTP setIPPort] missing Host header",
|
||||
Log::Level::DEBUG);
|
||||
dstIP_.clear();
|
||||
dstIp_.clear();
|
||||
dstPort_ = 80;
|
||||
break;
|
||||
}
|
||||
@@ -242,7 +242,7 @@ void HTTP::setIPPort() {
|
||||
|
||||
splitted = splitString(hostPort, ":");
|
||||
if (!splitted.empty()) {
|
||||
dstIP_ = splitted[0];
|
||||
dstIp_ = splitted[0];
|
||||
if (splitted.size() > 1 && !splitted[1].empty()) {
|
||||
dstPort_ = std::stoi(splitted[1]);
|
||||
} else {
|
||||
@@ -252,7 +252,7 @@ void HTTP::setIPPort() {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [HTTP setIPPort] wrong request",
|
||||
Log::Level::DEBUG);
|
||||
dstIP_.clear();
|
||||
dstIp_.clear();
|
||||
dstPort_ = 80;
|
||||
}
|
||||
break;
|
||||
@@ -263,14 +263,14 @@ void HTTP::setIPPort() {
|
||||
splitted = splitString(target, ":");
|
||||
|
||||
if (!splitted.empty()) {
|
||||
dstIP_ = splitted[0];
|
||||
dstIp_ = splitted[0];
|
||||
if (splitted.size() > 1 && !splitted[1].empty()) {
|
||||
dstPort_ = std::stoi(splitted[1]);
|
||||
} else {
|
||||
dstPort_ = 443;
|
||||
}
|
||||
} else {
|
||||
dstIP_.clear();
|
||||
dstIp_.clear();
|
||||
dstPort_ = 443;
|
||||
}
|
||||
break;
|
||||
@@ -278,7 +278,7 @@ void HTTP::setIPPort() {
|
||||
}
|
||||
}
|
||||
|
||||
const std::string HTTP::tlsTypeToString() const {
|
||||
std::string HTTP::tlsTypeToString() const {
|
||||
switch (parsedTlsRequest_.type) {
|
||||
case TlsTypes::TLSHandshake:
|
||||
return "TLSHandshake";
|
||||
@@ -291,7 +291,7 @@ const std::string HTTP::tlsTypeToString() const {
|
||||
}
|
||||
}
|
||||
|
||||
const std::string HTTP::toString() const {
|
||||
std::string HTTP::toString() const {
|
||||
switch (httpType()) {
|
||||
case HTTP::HttpType::https:
|
||||
return std::string("\n") + "TLS Type : " + tlsTypeToString() + "\n" +
|
||||
@@ -327,7 +327,7 @@ const std::string HTTP::toString() const {
|
||||
}
|
||||
}
|
||||
|
||||
const std::string HTTP::restoString() const {
|
||||
std::string HTTP::restoString() const {
|
||||
return std::string("\n") +
|
||||
boost::lexical_cast<std::string>(parsedHttpResponse_.base()) + "\n" +
|
||||
"Body Size : " +
|
||||
|
||||
+12
-12
@@ -43,7 +43,7 @@ public:
|
||||
|
||||
~HTTP();
|
||||
|
||||
inline const HTTP::HttpType &httpType() const { return httpType_; }
|
||||
inline HTTP::HttpType httpType() const { return httpType_; }
|
||||
|
||||
inline void httpType(const HTTP::HttpType &httpType) { httpType_ = httpType; }
|
||||
|
||||
@@ -55,11 +55,11 @@ public:
|
||||
|
||||
bool parseTls();
|
||||
|
||||
const std::string genHttpPostReqString(const std::string &body) const;
|
||||
std::string genHttpPostReqString(const std::string &body) const;
|
||||
|
||||
const std::string genHttpRestPostReqString() const;
|
||||
std::string genHttpRestPostReqString() const;
|
||||
|
||||
const std::string genHttpOkResString(const std::string &body) const;
|
||||
std::string genHttpOkResString(const std::string &body) const;
|
||||
|
||||
inline const boost::beast::http::request<boost::beast::http::string_body> &
|
||||
parsedHttpRequest() & {
|
||||
@@ -73,15 +73,15 @@ public:
|
||||
|
||||
inline const TlsRequest &parsedTlsRequest() & { return parsedTlsRequest_; }
|
||||
|
||||
const std::string tlsTypeToString() const;
|
||||
std::string tlsTypeToString() const;
|
||||
|
||||
inline const std::string &dstIP() { return dstIP_; }
|
||||
inline const std::string &dstIp() const { return dstIp_; }
|
||||
|
||||
inline const unsigned short &dstPort() { return dstPort_; }
|
||||
inline unsigned short dstPort() const { return dstPort_; }
|
||||
|
||||
const std::string toString() const;
|
||||
std::string toString() const;
|
||||
|
||||
const std::string restoString() const;
|
||||
std::string restoString() const;
|
||||
|
||||
private:
|
||||
explicit HTTP(const std::shared_ptr<Config> &config,
|
||||
@@ -91,8 +91,8 @@ private:
|
||||
|
||||
void setIPPort();
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
boost::asio::streambuf &buffer_;
|
||||
|
||||
boost::beast::http::parser<false, boost::beast::http::string_body> parser_;
|
||||
@@ -100,7 +100,7 @@ private:
|
||||
boost::beast::http::response<boost::beast::http::string_body> parsedHttpResponse_;
|
||||
|
||||
HttpType httpType_;
|
||||
std::string dstIP_;
|
||||
std::string dstIp_;
|
||||
unsigned short dstPort_;
|
||||
TlsRequest parsedTlsRequest_;
|
||||
boost::uuids::uuid uuid_;
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace http_utils {
|
||||
|
||||
inline std::string toLowerCopy(std::string value) {
|
||||
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
|
||||
return static_cast<char>(std::tolower(c));
|
||||
});
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
inline std::string trimCopy(std::string value) {
|
||||
while (!value.empty() && (value.front() == ' ' || value.front() == '\t')) {
|
||||
value.erase(value.begin());
|
||||
}
|
||||
|
||||
while (!value.empty() &&
|
||||
(value.back() == ' ' ||
|
||||
value.back() == '\t' ||
|
||||
value.back() == '\r' ||
|
||||
value.back() == '\n')) {
|
||||
value.pop_back();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
inline std::string extractHeaders(const std::string &message) {
|
||||
const auto pos = message.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return message.substr(0, pos + 4);
|
||||
}
|
||||
|
||||
inline std::string extractBody(const std::string &message) {
|
||||
const auto pos = message.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return message.substr(pos + 4);
|
||||
}
|
||||
|
||||
inline bool parseContentLength(const std::string &headers, std::size_t &value) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
|
||||
const auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("content-length:", 0) == 0) {
|
||||
auto raw = line.substr(std::string("Content-Length:").size());
|
||||
raw.erase(0, raw.find_first_not_of(" \t"));
|
||||
|
||||
try {
|
||||
value = static_cast<std::size_t>(std::stoull(raw));
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isChunked(const std::string &headers) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
|
||||
const auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("transfer-encoding:", 0) == 0 &&
|
||||
lower.find("chunked") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
inline std::string getRawHeader(const std::string &headers, const std::string &name) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
const std::string prefix = toLowerCopy(name) + ":";
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
|
||||
const std::string lower = toLowerCopy(line);
|
||||
if (lower.rfind(prefix, 0) == 0) {
|
||||
return trimCopy(line.substr(name.size() + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}// namespace http_utils
|
||||
+2
-4
@@ -10,7 +10,6 @@
|
||||
#include "config.hpp"
|
||||
#include "general.hpp"
|
||||
|
||||
|
||||
class Log : private Uncopyable {
|
||||
public:
|
||||
enum class Level {
|
||||
@@ -28,10 +27,9 @@ public:
|
||||
|
||||
explicit Log(const std::shared_ptr<Log> &log);
|
||||
|
||||
|
||||
~Log();
|
||||
|
||||
const std::shared_ptr<Config> &config() const { return config_; }
|
||||
std::shared_ptr<Config> config() const { return config_; }
|
||||
|
||||
Level level() const { return level_; }
|
||||
|
||||
@@ -42,7 +40,7 @@ private:
|
||||
|
||||
static std::string levelToString(Level level);
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
std::shared_ptr<Config> config_;
|
||||
Level level_;
|
||||
std::string mode_;
|
||||
|
||||
|
||||
+2
-2
@@ -31,8 +31,8 @@ private:
|
||||
|
||||
void stop();
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
|
||||
boost::asio::io_context io_context_;
|
||||
|
||||
|
||||
+18
-56
@@ -1,83 +1,45 @@
|
||||
#include "serverhandler.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "http_utils.hpp"
|
||||
|
||||
std::mutex ServerHandler::sessionsMutex_;
|
||||
std::unordered_map<std::string, TCPClient::pointer> ServerHandler::sessions_;
|
||||
|
||||
std::string ServerHandler::HttpUtils::lowerCopy(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
|
||||
return static_cast<char>(std::tolower(c));
|
||||
});
|
||||
|
||||
return s;
|
||||
return http_utils::toLowerCopy(std::move(s));
|
||||
}
|
||||
|
||||
std::string ServerHandler::HttpUtils::trimCopy(std::string s) {
|
||||
while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) {
|
||||
s.erase(s.begin());
|
||||
}
|
||||
|
||||
while (!s.empty() &&
|
||||
(s.back() == ' ' ||
|
||||
s.back() == '\t' ||
|
||||
s.back() == '\r' ||
|
||||
s.back() == '\n')) {
|
||||
s.pop_back();
|
||||
}
|
||||
|
||||
return s;
|
||||
return http_utils::trimCopy(std::move(s));
|
||||
}
|
||||
|
||||
std::string ServerHandler::HttpUtils::getRawHeader(const std::string &headers,
|
||||
const std::string &name) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
const std::string prefix = lowerCopy(name) + ":";
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
|
||||
std::string lower = lowerCopy(line);
|
||||
|
||||
if (lower.rfind(prefix, 0) == 0) {
|
||||
return trimCopy(line.substr(name.size() + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
return http_utils::getRawHeader(headers, name);
|
||||
}
|
||||
|
||||
std::string ServerHandler::HttpUtils::extractHttpBody(const std::string &msg) {
|
||||
const auto pos = msg.find("\r\n\r\n");
|
||||
|
||||
if (pos == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return msg.substr(pos + 4);
|
||||
return http_utils::extractBody(msg);
|
||||
}
|
||||
|
||||
ServerHandler::ServerHandler(boost::asio::streambuf &readBuffer,
|
||||
boost::asio::streambuf &writeBuffer,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
const TCPClient::pointer &client,
|
||||
const std::string &clientConnStr,
|
||||
TCPClient::pointer client,
|
||||
std::string clientConnStr,
|
||||
boost::uuids::uuid uuid)
|
||||
: config_(config),
|
||||
log_(log),
|
||||
client_(client),
|
||||
client_(std::move(client)),
|
||||
readBuffer_(readBuffer),
|
||||
writeBuffer_(writeBuffer),
|
||||
request_(HTTP::create(config, log, readBuffer, uuid)),
|
||||
clientConnStr_(clientConnStr),
|
||||
clientConnStr_(std::move(clientConnStr)),
|
||||
uuid_(uuid) {
|
||||
end_ = false;
|
||||
connect_ = false;
|
||||
@@ -301,12 +263,12 @@ void ServerHandler::handle() {
|
||||
switch (inner->httpType()) {
|
||||
case HTTP::HttpType::connect: {
|
||||
if (config_->general().tunnelEnable) {
|
||||
if (!client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
if (!client_->doConnect(inner->dstIp(), inner->dstPort())) {
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [ERROR] [Resolving Host] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
inner->dstIp() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
@@ -323,7 +285,7 @@ void ServerHandler::handle() {
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [TUNNEL] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
inner->dstIp() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
@@ -344,12 +306,12 @@ void ServerHandler::handle() {
|
||||
|
||||
targetClient->uuid_ = uuid_;
|
||||
|
||||
if (!targetClient->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
if (!targetClient->doConnect(inner->dstIp(), inner->dstPort())) {
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [ERROR] [Resolving Host] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
inner->dstIp() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
@@ -366,7 +328,7 @@ void ServerHandler::handle() {
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
inner->dstIp() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
if (!pendingTunnelData.empty()) {
|
||||
@@ -395,7 +357,7 @@ void ServerHandler::handle() {
|
||||
copyStringToStreambuf(innerRequest, readBuffer_);
|
||||
|
||||
if (!client_->socket().is_open()) {
|
||||
if (!client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
if (!client_->doConnect(inner->dstIp(), inner->dstPort())) {
|
||||
client_->socket().close();
|
||||
makeEncryptedHttpResponse("", "502 Bad Gateway", "close");
|
||||
return;
|
||||
|
||||
@@ -19,8 +19,8 @@ public:
|
||||
boost::asio::streambuf &writeBuffer,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
const TCPClient::pointer &client,
|
||||
const std::string &clientConnStr,
|
||||
TCPClient::pointer client,
|
||||
std::string clientConnStr,
|
||||
boost::uuids::uuid uuid) {
|
||||
return pointer(new ServerHandler(readBuffer, writeBuffer, config, log, client, clientConnStr, uuid));
|
||||
}
|
||||
@@ -29,8 +29,7 @@ public:
|
||||
|
||||
void handle();
|
||||
|
||||
inline const HTTP::pointer &request() & { return request_; }
|
||||
inline const HTTP::pointer &&request() && { return std::move(request_); }
|
||||
inline const HTTP::pointer &request() const { return request_; }
|
||||
|
||||
bool end_;
|
||||
bool connect_;
|
||||
@@ -47,8 +46,8 @@ private:
|
||||
boost::asio::streambuf &writeBuffer,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
const TCPClient::pointer &client,
|
||||
const std::string &clientConnStr,
|
||||
TCPClient::pointer client,
|
||||
std::string clientConnStr,
|
||||
boost::uuids::uuid uuid);
|
||||
|
||||
void makeEncryptedHttpResponse(const std::string &plainBody,
|
||||
@@ -62,13 +61,13 @@ private:
|
||||
static std::mutex sessionsMutex_;
|
||||
static std::unordered_map<std::string, TCPClient::pointer> sessions_;
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
const TCPClient::pointer &client_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
TCPClient::pointer client_;
|
||||
boost::asio::streambuf &readBuffer_;
|
||||
boost::asio::streambuf &writeBuffer_;
|
||||
HTTP::pointer request_;
|
||||
const std::string &clientConnStr_;
|
||||
std::string clientConnStr_;
|
||||
boost::uuids::uuid uuid_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
+11
-51
@@ -5,8 +5,11 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "http_utils.hpp"
|
||||
|
||||
TCPClient::TCPClient(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log)
|
||||
@@ -90,7 +93,7 @@ bool TCPClient::enableTlsClient() {
|
||||
}
|
||||
}
|
||||
|
||||
bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPort) {
|
||||
bool TCPClient::doConnect(const std::string &dstIp, unsigned short dstPort) {
|
||||
|
||||
try {
|
||||
if (tlsEnabled_ && sslSocket_ && sslSocket_->lowest_layer().is_open()) {
|
||||
@@ -101,14 +104,14 @@ bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPor
|
||||
return true;
|
||||
}
|
||||
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] [DST " + dstIP +
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] [DST " + dstIp +
|
||||
":" + std::to_string(dstPort) + "]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
boost::system::error_code error_code;
|
||||
|
||||
auto endpoint = resolver_.resolve(
|
||||
dstIP.c_str(),
|
||||
dstIp.c_str(),
|
||||
std::to_string(dstPort).c_str(),
|
||||
error_code);
|
||||
|
||||
@@ -507,66 +510,23 @@ void TCPClient::socketShutdown() {
|
||||
}
|
||||
|
||||
std::string TCPClient::HttpUtils::toLowerCopy(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) {
|
||||
return static_cast<char>(std::tolower(c));
|
||||
});
|
||||
return s;
|
||||
return http_utils::toLowerCopy(std::move(s));
|
||||
}
|
||||
|
||||
std::string TCPClient::HttpUtils::extractHeaders(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(0, pos + 4);
|
||||
return http_utils::extractHeaders(msg);
|
||||
}
|
||||
|
||||
std::string TCPClient::HttpUtils::extractBody(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(pos + 4);
|
||||
return http_utils::extractBody(msg);
|
||||
}
|
||||
|
||||
bool TCPClient::HttpUtils::parseContentLength(const std::string &headers, std::size_t &value) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
|
||||
auto lower = toLowerCopy(line);
|
||||
|
||||
if (lower.rfind("content-length:", 0) == 0) {
|
||||
auto raw = line.substr(std::string("Content-Length:").size());
|
||||
raw.erase(0, raw.find_first_not_of(" \t"));
|
||||
|
||||
try {
|
||||
value = static_cast<std::size_t>(std::stoull(raw));
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return http_utils::parseContentLength(headers, value);
|
||||
}
|
||||
|
||||
bool TCPClient::HttpUtils::isChunked(const std::string &headers) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
|
||||
auto lower = toLowerCopy(line);
|
||||
|
||||
if (lower.rfind("transfer-encoding:", 0) == 0 &&
|
||||
lower.find("chunked") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return http_utils::isChunked(headers);
|
||||
}
|
||||
|
||||
bool TCPClient::HttpUtils::readHttpMessage(
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/enable_shared_from_this.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -15,9 +13,9 @@
|
||||
#include "http.hpp"
|
||||
#include "log.hpp"
|
||||
|
||||
class TCPClient : public boost::enable_shared_from_this<TCPClient> {
|
||||
class TCPClient : public std::enable_shared_from_this<TCPClient> {
|
||||
public:
|
||||
using pointer = boost::shared_ptr<TCPClient>;
|
||||
using pointer = std::shared_ptr<TCPClient>;
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
using ssl_stream = boost::asio::ssl::stream<tcp::socket>;
|
||||
|
||||
@@ -35,15 +33,13 @@ public:
|
||||
|
||||
bool enableTlsClient();
|
||||
bool doHandshakeClient();
|
||||
bool doConnect(const std::string &dstIP, const unsigned short &dstPort);
|
||||
bool doConnect(const std::string &dstIp, unsigned short dstPort);
|
||||
|
||||
void writeBuffer(boost::asio::streambuf &buffer);
|
||||
|
||||
inline boost::asio::streambuf &writeBuffer() & { return writeBuffer_; }
|
||||
inline boost::asio::streambuf &&writeBuffer() && { return std::move(writeBuffer_); }
|
||||
|
||||
inline boost::asio::streambuf &readBuffer() & { return readBuffer_; }
|
||||
inline boost::asio::streambuf &&readBuffer() && { return std::move(readBuffer_); }
|
||||
|
||||
void doWrite(boost::asio::streambuf &buffer);
|
||||
void doReadAgent();
|
||||
@@ -51,6 +47,9 @@ public:
|
||||
|
||||
void socketShutdown();
|
||||
|
||||
boost::uuids::uuid uuid() const { return uuid_; }
|
||||
bool ended() const { return end_; }
|
||||
|
||||
boost::uuids::uuid uuid_;
|
||||
bool end_;
|
||||
|
||||
@@ -87,8 +86,8 @@ private:
|
||||
void cancelTimeout();
|
||||
void onTimeout(const boost::system::error_code &error);
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
boost::asio::io_context &io_context_;
|
||||
|
||||
tcp::socket socket_;
|
||||
|
||||
+177
-56
@@ -5,8 +5,11 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "http_utils.hpp"
|
||||
|
||||
TCPConnection::TCPConnection(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
@@ -123,20 +126,30 @@ void TCPConnection::doReadAgent() {
|
||||
try {
|
||||
readBuffer_.consume(readBuffer_.size());
|
||||
writeBuffer_.consume(writeBuffer_.size());
|
||||
|
||||
resetTimeout();
|
||||
|
||||
boost::asio::async_read(socket_,
|
||||
readBuffer_,
|
||||
boost::asio::transfer_at_least(1),
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
boost::bind(&TCPConnection::handleReadAgent,
|
||||
shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
socket_.async_read_some(
|
||||
readBuffer_.prepare(8192),
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
[self = shared_from_this()](
|
||||
const boost::system::error_code &ec,
|
||||
std::size_t bytes) {
|
||||
if (!ec) {
|
||||
self->readBuffer_.commit(bytes);
|
||||
}
|
||||
|
||||
self->handleReadAgent(ec, bytes);
|
||||
}));
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPConnection doReadAgent] [catch] " + error.what(),
|
||||
Log::Level::ERROR);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [TCPConnection doReadAgent] [catch] " +
|
||||
error.what(),
|
||||
Log::Level::ERROR);
|
||||
|
||||
socketShutdown();
|
||||
}
|
||||
}
|
||||
@@ -177,7 +190,15 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
|
||||
boost::system::error_code errorIn;
|
||||
|
||||
if (config_->general().protocol == "socks5") {
|
||||
if (!handleSocks5Handshake()) {
|
||||
socketShutdown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string current = streambufToString(readBuffer_);
|
||||
|
||||
if (current.find("\r\n\r\n") == std::string::npos) {
|
||||
boost::asio::read_until(socket_, readBuffer_, "\r\n\r\n", errorIn);
|
||||
}
|
||||
@@ -234,6 +255,18 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
end_ = agentHandler_->end_;
|
||||
connect_ = agentHandler_->connect_;
|
||||
|
||||
if (config_->general().protocol == "socks5" && connect_) {
|
||||
writeBuffer_.consume(writeBuffer_.size());
|
||||
|
||||
const unsigned char ok[10] = {
|
||||
0x05, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00};
|
||||
|
||||
std::ostream os(&writeBuffer_);
|
||||
os.write(reinterpret_cast<const char *>(ok), sizeof(ok));
|
||||
}
|
||||
|
||||
if (writeBuffer_.size() == 0) {
|
||||
socketShutdown();
|
||||
return;
|
||||
@@ -265,12 +298,14 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
if (!self->config_->general().connectionReuse) {
|
||||
self->client_->socketShutdown();
|
||||
}
|
||||
|
||||
self->startTunnelReadFromClient();
|
||||
self->startTunnelPollServer();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self->doReadAgent();
|
||||
}));
|
||||
} catch (std::exception &error) {
|
||||
@@ -487,6 +522,132 @@ std::string TCPConnection::postTunnelAction(const std::string &action,
|
||||
return dec.message;
|
||||
}
|
||||
|
||||
bool TCPConnection::readExactFromClient(void *data, std::size_t size) {
|
||||
auto *out = static_cast<char *>(data);
|
||||
std::size_t copied = 0;
|
||||
|
||||
while (copied < size && readBuffer_.size() > 0) {
|
||||
std::size_t n = std::min(size - copied, readBuffer_.size());
|
||||
|
||||
auto buffers = readBuffer_.data();
|
||||
std::size_t copiedNow = boost::asio::buffer_copy(
|
||||
boost::asio::buffer(out + copied, n),
|
||||
buffers);
|
||||
|
||||
readBuffer_.consume(copiedNow);
|
||||
copied += copiedNow;
|
||||
}
|
||||
|
||||
if (copied == size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boost::system::error_code ec;
|
||||
boost::asio::read(
|
||||
socket_,
|
||||
boost::asio::buffer(out + copied, size - copied),
|
||||
ec);
|
||||
|
||||
return !ec;
|
||||
}
|
||||
|
||||
bool TCPConnection::handleSocks5Handshake() {
|
||||
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [TCPConnection handleSocks5Handshake] handshake start",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
unsigned char header[2];
|
||||
if (!readExactFromClient(header, 2)) {
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [TCPConnection handleSocks5Handshake] [SOCKS5] version=" +
|
||||
std::to_string(header[0]),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header[0] != 0x05) {
|
||||
log_->write("[" + to_string(uuid_) + "] [SOCKS5] invalid version", Log::Level::DEBUG);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> methods(header[1]);
|
||||
if (!readExactFromClient(methods.data(), methods.size())) return false;
|
||||
|
||||
const unsigned char authReply[2] = {0x05, 0x00};
|
||||
boost::asio::write(socket_, boost::asio::buffer(authReply, 2));
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [TCPConnection handleSocks5Handshake] auth reply sent" +
|
||||
std::to_string(header[0]),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
|
||||
unsigned char req[4];
|
||||
if (!readExactFromClient(req, 4)) return false;
|
||||
|
||||
if (req[0] != 0x05 || req[1] != 0x01) {
|
||||
const unsigned char fail[10] = {0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0};
|
||||
boost::asio::write(socket_, boost::asio::buffer(fail, 10));
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string host;
|
||||
|
||||
if (req[3] == 0x01) {
|
||||
unsigned char ip[4];
|
||||
if (!readExactFromClient(ip, 4)) return false;
|
||||
|
||||
host =
|
||||
std::to_string(ip[0]) + "." +
|
||||
std::to_string(ip[1]) + "." +
|
||||
std::to_string(ip[2]) + "." +
|
||||
std::to_string(ip[3]);
|
||||
} else if (req[3] == 0x03) {
|
||||
unsigned char len;
|
||||
if (!readExactFromClient(&len, 1)) return false;
|
||||
|
||||
std::vector<char> domain(len);
|
||||
if (!readExactFromClient(domain.data(), domain.size())) return false;
|
||||
|
||||
host.assign(domain.begin(), domain.end());
|
||||
} else if (req[3] == 0x04) {
|
||||
unsigned char ip[16];
|
||||
if (!readExactFromClient(ip, 16)) return false;
|
||||
|
||||
boost::asio::ip::address_v6::bytes_type bytes;
|
||||
std::copy(ip, ip + 16, bytes.begin());
|
||||
host = boost::asio::ip::address_v6(bytes).to_string();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned char portBytes[2];
|
||||
if (!readExactFromClient(portBytes, 2)) return false;
|
||||
|
||||
unsigned short port =
|
||||
static_cast<unsigned short>((portBytes[0] << 8) | portBytes[1]);
|
||||
|
||||
std::string target = host + ":" + std::to_string(port);
|
||||
|
||||
std::string connectRequest =
|
||||
"CONNECT " + target +
|
||||
" HTTP/1.1\r\n"
|
||||
"Host: " +
|
||||
target +
|
||||
"\r\n"
|
||||
"\r\n";
|
||||
|
||||
copyStringToStreambuf(connectRequest, readBuffer_);
|
||||
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [SOCKS5] CONNECT " + target,
|
||||
Log::Level::INFO);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TCPConnection::startTunnelReadFromClient() {
|
||||
if (tunnelClosed_ || !socket_.is_open()) return;
|
||||
|
||||
@@ -867,63 +1028,23 @@ void TCPConnection::relayTargetToAgent() {
|
||||
}
|
||||
|
||||
std::string TCPConnection::HttpUtils::toLowerCopy(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
|
||||
return static_cast<char>(std::tolower(c));
|
||||
});
|
||||
return s;
|
||||
return http_utils::toLowerCopy(std::move(s));
|
||||
}
|
||||
|
||||
std::string TCPConnection::HttpUtils::extractHeaders(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(0, pos + 4);
|
||||
return http_utils::extractHeaders(msg);
|
||||
}
|
||||
|
||||
std::string TCPConnection::HttpUtils::extractBody(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(pos + 4);
|
||||
return http_utils::extractBody(msg);
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::parseContentLength(const std::string &headers, std::size_t &value) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("content-length:", 0) == 0) {
|
||||
auto raw = line.substr(std::string("Content-Length:").size());
|
||||
raw.erase(0, raw.find_first_not_of(" \t"));
|
||||
|
||||
try {
|
||||
value = static_cast<std::size_t>(std::stoull(raw));
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return http_utils::parseContentLength(headers, value);
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::isChunked(const std::string &headers) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("transfer-encoding:", 0) == 0 &&
|
||||
lower.find("chunked") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return http_utils::isChunked(headers);
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBody(
|
||||
|
||||
+14
-13
@@ -5,8 +5,6 @@
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/enable_shared_from_this.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <boost/uuid/uuid_generators.hpp>
|
||||
#include <boost/uuid/uuid_io.hpp>
|
||||
@@ -20,9 +18,9 @@
|
||||
#include "general.hpp"
|
||||
#include "serverhandler.hpp"
|
||||
|
||||
class TCPConnection : public boost::enable_shared_from_this<TCPConnection> {
|
||||
class TCPConnection : public std::enable_shared_from_this<TCPConnection> {
|
||||
public:
|
||||
using pointer = boost::shared_ptr<TCPConnection>;
|
||||
using pointer = std::shared_ptr<TCPConnection>;
|
||||
using ssl_stream = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
|
||||
|
||||
static pointer create(boost::asio::io_context &io_context,
|
||||
@@ -33,7 +31,7 @@ public:
|
||||
}
|
||||
|
||||
boost::asio::ip::tcp::socket &socket();
|
||||
inline TCPClient::pointer client() { return client_; }
|
||||
inline TCPClient::pointer client() const { return client_; }
|
||||
ssl_stream &tlsSocket();
|
||||
|
||||
bool initTlsServerContext();
|
||||
@@ -44,8 +42,8 @@ public:
|
||||
}
|
||||
|
||||
inline const boost::asio::streambuf &readBuffer() & { return readBuffer_; }
|
||||
inline const boost::asio::streambuf &&readBuffer() && { return std::move(readBuffer_); }
|
||||
inline boost::uuids::uuid uuid() { return uuid_; }
|
||||
|
||||
inline boost::uuids::uuid uuid() const { return uuid_; }
|
||||
|
||||
void startAgent();
|
||||
void startServer();
|
||||
@@ -71,7 +69,7 @@ public:
|
||||
std::function<void(const boost::system::error_code &)> done);
|
||||
|
||||
inline void asyncReadFromAgentConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::array<char, BufferSize> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done);
|
||||
|
||||
inline void asyncWriteToAgentServerConnection(
|
||||
@@ -80,7 +78,7 @@ public:
|
||||
std::function<void(const boost::system::error_code &)> done);
|
||||
|
||||
inline void asyncReadFromAgentServerConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::array<char, BufferSize> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done);
|
||||
|
||||
private:
|
||||
@@ -111,6 +109,9 @@ private:
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client);
|
||||
|
||||
bool handleSocks5Handshake();
|
||||
bool readExactFromClient(void *data, std::size_t size);
|
||||
|
||||
void resetTimeout();
|
||||
void cancelTimeout();
|
||||
void onTimeout(const boost::system::error_code &error);
|
||||
@@ -125,8 +126,8 @@ private:
|
||||
boost::asio::ssl::context sslContext_;
|
||||
std::unique_ptr<ssl_stream> tlsSocket_;
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
boost::asio::io_context &io_context_;
|
||||
TCPClient::pointer client_;
|
||||
|
||||
@@ -147,6 +148,6 @@ private:
|
||||
bool pollInProgress_;
|
||||
std::atomic_bool closed_{false};
|
||||
|
||||
std::array<char, 8192> downstreamBuf_;
|
||||
std::array<char, 8192> upstreamBuf_;
|
||||
std::array<char, BufferSize> downstreamBuf_;
|
||||
std::array<char, BufferSize> upstreamBuf_;
|
||||
};
|
||||
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "tcpconnection.hpp"
|
||||
|
||||
class TCPServer {
|
||||
public:
|
||||
using pointer = boost::shared_ptr<TCPServer>;
|
||||
using pointer = std::shared_ptr<TCPServer>;
|
||||
|
||||
static pointer create(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
@@ -25,8 +25,8 @@ private:
|
||||
void handleAccept(TCPConnection::pointer connection,
|
||||
const boost::system::error_code &error);
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
boost::asio::io_context &io_context_;
|
||||
boost::asio::ip::tcp::acceptor acceptor_;
|
||||
};
|
||||
@@ -3,6 +3,10 @@
|
||||
general:
|
||||
# token: 32 bytes of string. this will be used on agent and server side to encryption
|
||||
token: "af445adb-2434-4975-9445-2c1b2231"
|
||||
# protocol:
|
||||
# Defines the protocol between client and agent
|
||||
# Choose http or socks5
|
||||
protocol: http
|
||||
# fakeUrls:
|
||||
# Defines the list of fake urls between agent and server
|
||||
fakeUrls:
|
||||
|
||||
Reference in new issue
Block a user