Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0942c4015 | ||
|
|
228aff5d63 | ||
|
|
5647fd2f69 | ||
|
|
92a290cca7 | ||
|
|
108b9729c7 | ||
|
|
7baaf6aefb | ||
|
|
4793a3f0e3 | ||
|
|
c8cd4066d7 | ||
|
|
33a4dbd0b8 | ||
|
|
01d8b36ca0 | ||
|
|
cd7b985da1 | ||
|
|
80c631f66b | ||
|
|
8a1fcde280 | ||
|
|
51feb98dc3 | ||
|
|
560b5d93cc | ||
|
|
97ee705eec | ||
|
|
8d1ecfc2dc | ||
|
|
b402144e9e | ||
|
|
e7c8cb51cc | ||
|
|
51fd1d47cc | ||
|
|
251b0202e8 | ||
|
|
10e4481376 | ||
|
|
47b56af86e | ||
|
|
1c8d2d610b | ||
|
|
86f2b750a8 |
No files matched your search
@@ -1,4 +1,4 @@
|
||||
name: Build and Release Debian Package
|
||||
name: Build and Release Debian Package - DEV
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -6,7 +6,16 @@ on:
|
||||
- dev
|
||||
paths:
|
||||
- 'core/src/**'
|
||||
- '.github/workflows/**'
|
||||
- 'core/**'
|
||||
- 'nipovpn/**'
|
||||
- '.github/workflows/debian-dev.yml'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 0.1.${{ github.run_number }}
|
||||
PACKAGE_FILE: nipovpn_dev_0.1.${{ github.run_number }}_amd64.deb
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -14,62 +23,74 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y dpkg-dev build-essential lintian fakeroot devscripts cmake libssl-dev libboost-all-dev libyaml-cpp-dev
|
||||
sudo apt-get install -y \
|
||||
dpkg-dev \
|
||||
build-essential \
|
||||
lintian \
|
||||
fakeroot \
|
||||
devscripts \
|
||||
cmake \
|
||||
libssl-dev \
|
||||
libboost-all-dev \
|
||||
libyaml-cpp-dev
|
||||
|
||||
- name: Build the package
|
||||
- name: Build binary
|
||||
run: |
|
||||
rm -rf build
|
||||
mkdir -p build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Debug ..
|
||||
cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
make -j$(nproc)
|
||||
cd ..
|
||||
mkdir -p nipovpn/usr/bin/
|
||||
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
|
||||
dpkg-deb --build nipovpn/ build/nipovpn_staging_v1-1-${{ github.run_number }}.deb
|
||||
|
||||
- name: Upload the Debian nipovpn as an artifact
|
||||
- name: Build Debian package
|
||||
run: |
|
||||
mkdir -p nipovpn/usr/bin
|
||||
|
||||
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
|
||||
chmod 755 nipovpn/usr/bin/nipovpn
|
||||
|
||||
sed -i -E "s/^Version[[:space:]]*:.*/Version: ${PACKAGE_VERSION}/" nipovpn/DEBIAN/control
|
||||
|
||||
echo "Final control file:"
|
||||
cat nipovpn/DEBIAN/control
|
||||
|
||||
grep "^Version: ${PACKAGE_VERSION}$" nipovpn/DEBIAN/control
|
||||
|
||||
dpkg-deb --build nipovpn "build/${PACKAGE_FILE}"
|
||||
|
||||
- name: Show package information
|
||||
run: |
|
||||
dpkg -I "build/${PACKAGE_FILE}"
|
||||
dpkg -c "build/${PACKAGE_FILE}"
|
||||
|
||||
- name: Upload Debian package as artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: nipovpn
|
||||
path: build/nipovpn_staging_v1-1-${{ github.run_number }}.deb
|
||||
|
||||
- name: Show nipovpn information
|
||||
run: |
|
||||
dpkg -I build/nipovpn_staging_v1-1-${{ github.run_number }}.deb
|
||||
name: nipovpn-dev-deb
|
||||
path: build/${{ env.PACKAGE_FILE }}
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Download build artifact
|
||||
- name: Download Debian package artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: nipovpn
|
||||
name: nipovpn-dev-deb
|
||||
path: ./
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
- name: Create DEV GitHub Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tag_name: 'v.0.1.1.${{ github.run_number }}'
|
||||
release_name: Staging v.0.1.1.${{ github.run_number }}
|
||||
draft: false
|
||||
tag_name: dev-v${{ env.PACKAGE_VERSION }}
|
||||
name: DEV Version v${{ env.PACKAGE_VERSION }}
|
||||
prerelease: true
|
||||
|
||||
- name: Upload Debian Package to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
files: ${{ env.PACKAGE_FILE }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./nipovpn_staging_v1-1-${{ github.run_number }}.deb
|
||||
asset_name: nipovpn_staging_v1-1-${{ github.run_number }}.deb
|
||||
asset_content_type: application/octet-stream
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
@@ -1,4 +1,4 @@
|
||||
name: Build and Release Debian Package
|
||||
name: Build and Release Debian Package - MORI
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -6,7 +6,16 @@ on:
|
||||
- mori
|
||||
paths:
|
||||
- 'core/src/**'
|
||||
- '.github/workflows/**'
|
||||
- 'core/**'
|
||||
- 'nipovpn/**'
|
||||
- '.github/workflows/debian-mori.yml'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 0.2.${{ github.run_number }}
|
||||
PACKAGE_FILE: nipovpn_mori_0.2.${{ github.run_number }}_amd64.deb
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -14,62 +23,74 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y dpkg-dev build-essential lintian fakeroot devscripts cmake libssl-dev libboost-all-dev libyaml-cpp-dev
|
||||
sudo apt-get install -y \
|
||||
dpkg-dev \
|
||||
build-essential \
|
||||
lintian \
|
||||
fakeroot \
|
||||
devscripts \
|
||||
cmake \
|
||||
libssl-dev \
|
||||
libboost-all-dev \
|
||||
libyaml-cpp-dev
|
||||
|
||||
- name: Build the package
|
||||
- name: Build binary
|
||||
run: |
|
||||
rm -rf build
|
||||
mkdir -p build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Debug ..
|
||||
cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
make -j$(nproc)
|
||||
cd ..
|
||||
mkdir -p nipovpn/usr/bin/
|
||||
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
|
||||
dpkg-deb --build nipovpn/ build/nipovpn_mori_v1-1-${{ github.run_number }}.deb
|
||||
|
||||
- name: Upload the Debian nipovpn as an artifact
|
||||
- name: Build Debian package
|
||||
run: |
|
||||
mkdir -p nipovpn/usr/bin
|
||||
|
||||
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
|
||||
chmod 755 nipovpn/usr/bin/nipovpn
|
||||
|
||||
sed -i -E "s/^Version[[:space:]]*:.*/Version: ${PACKAGE_VERSION}/" nipovpn/DEBIAN/control
|
||||
|
||||
echo "Final control file:"
|
||||
cat nipovpn/DEBIAN/control
|
||||
|
||||
grep "^Version: ${PACKAGE_VERSION}$" nipovpn/DEBIAN/control
|
||||
|
||||
dpkg-deb --build nipovpn "build/${PACKAGE_FILE}"
|
||||
|
||||
- name: Show package information
|
||||
run: |
|
||||
dpkg -I "build/${PACKAGE_FILE}"
|
||||
dpkg -c "build/${PACKAGE_FILE}"
|
||||
|
||||
- name: Upload Debian package as artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: nipovpn
|
||||
path: build/nipovpn_mori_v1-1-${{ github.run_number }}.deb
|
||||
|
||||
- name: Show nipovpn information
|
||||
run: |
|
||||
dpkg -I build/nipovpn_mori_v1-1-${{ github.run_number }}.deb
|
||||
name: nipovpn-mori-deb
|
||||
path: build/${{ env.PACKAGE_FILE }}
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Download build artifact
|
||||
- name: Download Debian package artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: nipovpn
|
||||
name: nipovpn-mori-deb
|
||||
path: ./
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
- name: Create MORI GitHub Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tag_name: 'v.11.1.1.${{ github.run_number }}'
|
||||
release_name: Mori v.11.1.1.${{ github.run_number }}
|
||||
draft: false
|
||||
tag_name: mori-v${{ env.PACKAGE_VERSION }}
|
||||
name: MORI Version v${{ env.PACKAGE_VERSION }}
|
||||
prerelease: true
|
||||
|
||||
- name: Upload Debian Package to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
files: ${{ env.PACKAGE_FILE }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./nipovpn_mori_v1-1-${{ github.run_number }}.deb
|
||||
asset_name: nipovpn_mori_v1-1-${{ github.run_number }}.deb
|
||||
asset_content_type: application/octet-stream
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
@@ -1,4 +1,4 @@
|
||||
name: Build and Release Debian Package
|
||||
name: Build and Release Debian Package - MAIN
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -6,6 +6,16 @@ on:
|
||||
- main
|
||||
paths:
|
||||
- 'core/src/**'
|
||||
- 'core/**'
|
||||
- 'nipovpn/**'
|
||||
- '.github/workflows/debian.yml'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.${{ github.run_number }}
|
||||
PACKAGE_FILE: nipovpn_1.1.${{ github.run_number }}_amd64.deb
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -13,63 +23,78 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y dpkg-dev build-essential lintian fakeroot devscripts cmake libssl-dev libboost-all-dev libyaml-cpp-dev
|
||||
sudo apt-get install -y \
|
||||
dpkg-dev \
|
||||
build-essential \
|
||||
lintian \
|
||||
fakeroot \
|
||||
devscripts \
|
||||
cmake \
|
||||
libssl-dev \
|
||||
libboost-all-dev \
|
||||
libyaml-cpp-dev
|
||||
|
||||
- name: Build the package
|
||||
- name: Build binary
|
||||
run: |
|
||||
rm -rf build
|
||||
mkdir -p build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Debug ..
|
||||
cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
make -j$(nproc)
|
||||
cd ..
|
||||
mkdir -p nipovpn/usr/bin/
|
||||
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
|
||||
dpkg-deb --build nipovpn/ build/nipovpn_v1-1-${{ github.run_number }}.deb
|
||||
|
||||
- name: Upload the Debian nipovpn as an artifact
|
||||
- name: Build Debian package
|
||||
run: |
|
||||
mkdir -p nipovpn/usr/bin
|
||||
|
||||
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
|
||||
chmod 755 nipovpn/usr/bin/nipovpn
|
||||
|
||||
sed -i -E "s/^Version[[:space:]]*:.*/Version: ${PACKAGE_VERSION}/" nipovpn/DEBIAN/control
|
||||
|
||||
echo "Final control file:"
|
||||
cat nipovpn/DEBIAN/control
|
||||
|
||||
grep "^Version: ${PACKAGE_VERSION}$" nipovpn/DEBIAN/control
|
||||
|
||||
dpkg-deb --build nipovpn "build/${PACKAGE_FILE}"
|
||||
|
||||
- name: Show package information
|
||||
run: |
|
||||
dpkg -I "build/${PACKAGE_FILE}"
|
||||
dpkg -c "build/${PACKAGE_FILE}"
|
||||
|
||||
- name: Upload Debian package as artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: nipovpn
|
||||
path: build/nipovpn_v1-1-${{ github.run_number }}.deb
|
||||
|
||||
- name: Show nipovpn information
|
||||
run: |
|
||||
dpkg -I build/nipovpn_v1-1-${{ github.run_number }}.deb
|
||||
name: nipovpn-main-deb
|
||||
path: build/${{ env.PACKAGE_FILE }}
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Download build artifact
|
||||
- name: Download Debian package artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: nipovpn
|
||||
name: nipovpn-main-deb
|
||||
path: ./
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
- name: Check downloaded files
|
||||
run: |
|
||||
ls -lah
|
||||
|
||||
- name: Create MAIN GitHub Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tag_name: 'v1.1.${{ github.run_number }}'
|
||||
release_name: Version v1.1.${{ github.run_number }}
|
||||
draft: false
|
||||
tag_name: v${{ env.PACKAGE_VERSION }}
|
||||
name: Version v${{ env.PACKAGE_VERSION }}
|
||||
prerelease: false
|
||||
|
||||
- name: Upload Debian Package to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
files: ${{ env.PACKAGE_FILE }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./nipovpn_v1-1-${{ github.run_number }}.deb
|
||||
asset_name: nipovpn_v1-1-${{ github.run_number }}.deb
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
@@ -40,7 +40,7 @@ void AgentHandler::handle() {
|
||||
Log::Level::INFO);
|
||||
}
|
||||
|
||||
if (config_->agent().tlsEnable && !client_->tlsEnabled()) {
|
||||
if (config_->general().tlsEnable && !client_->tlsEnabled()) {
|
||||
if (!client_->enableTlsClient()) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TLS] [ERROR] [Init Client Context] [SRC " +
|
||||
clientConnStr_ + "] [DST " + config_->agent().serverIp + ":" +
|
||||
@@ -86,15 +86,21 @@ void AgentHandler::handle() {
|
||||
request_->httpType() == HTTP::HttpType::connect ? "open" : "request";
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << config_->general().method + " / HTTP/1.1\r\n"
|
||||
const bool directTunnelOpen =
|
||||
config_->general().tunnelEnable &&
|
||||
request_->httpType() == HTTP::HttpType::connect;
|
||||
|
||||
outer << config_->randomMethod() + " /" + config_->randomEndPoint() + " HTTP/" << config_->agent().httpVersion << "\r\n"
|
||||
<< "Host: " << relayHostHeader(config_) << "\r\n"
|
||||
<< "User-Agent: Mozilla/5.0\r\n"
|
||||
<< "User-Agent: " << config_->agent().userAgent << "\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Type: application/text\r\n"
|
||||
<< "X-Nipo-Session: " << to_string(uuid_) << "\r\n"
|
||||
<< "X-Nipo-Action: " << action << "\r\n"
|
||||
<< "Content-Length: " << innerRequest.size() << "\r\n"
|
||||
<< "Connection: close\r\n"
|
||||
<< "Connection: "
|
||||
<< ((config_->general().connectionReuse || directTunnelOpen) ? "keep-alive" : "close")
|
||||
<< "\r\n"
|
||||
<< "\r\n"
|
||||
<< innerRequest;
|
||||
|
||||
@@ -103,7 +109,7 @@ void AgentHandler::handle() {
|
||||
client_->doWrite(readBuffer_);
|
||||
client_->doReadAgent();
|
||||
|
||||
if (client_->readBuffer().size() == 0) {
|
||||
if (client_->readBuffer().size() == 0 && !config_->general().connectionReuse) {
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
@@ -143,7 +149,7 @@ void AgentHandler::handle() {
|
||||
}
|
||||
|
||||
std::string AgentHandler::relayHostHeader(const std::shared_ptr<Config> &config) {
|
||||
std::string hostHeader = config->general().fakeUrl;
|
||||
std::string hostHeader = config->randomFakeUrl();
|
||||
auto pos = hostHeader.find("://");
|
||||
if (pos != std::string::npos) hostHeader = hostHeader.substr(pos + 3);
|
||||
pos = hostHeader.find('/');
|
||||
|
||||
+107
-27
@@ -9,30 +9,29 @@ 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"]["fakeUrl"].as<std::string>(),
|
||||
configYaml_["general"]["method"].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>>(),
|
||||
configYaml_["general"]["timeout"].as<unsigned short>(),
|
||||
configYaml_["general"]["tunnelEnable"].as<bool>(false)}),
|
||||
configYaml_["general"]["tunnelEnable"].as<bool>(false),
|
||||
configYaml_["general"]["connectionReuse"].as<bool>(false),
|
||||
configYaml_["general"]["tlsEnable"].as<bool>(false),
|
||||
configYaml_["general"]["tlsVerifyPeer"].as<bool>(false),
|
||||
configYaml_["general"]["tlsCertFile"].as<std::string>(""),
|
||||
configYaml_["general"]["tlsKeyFile"].as<std::string>(""),
|
||||
configYaml_["general"]["tlsCaFile"].as<std::string>("")}),
|
||||
log_({configYaml_["log"]["logLevel"].as<std::string>(),
|
||||
configYaml_["log"]["logFile"].as<std::string>()}),
|
||||
server_({configYaml_["server"]["threads"].as<unsigned short>(),
|
||||
configYaml_["server"]["listenIp"].as<std::string>(),
|
||||
configYaml_["server"]["listenPort"].as<unsigned short>(),
|
||||
configYaml_["server"]["tlsEnable"].as<bool>(false),
|
||||
configYaml_["server"]["tlsVerifyPeer"].as<bool>(false),
|
||||
configYaml_["server"]["tlsCertFile"].as<std::string>(""),
|
||||
configYaml_["server"]["tlsKeyFile"].as<std::string>(""),
|
||||
configYaml_["server"]["tlsCaFile"].as<std::string>("")}),
|
||||
configYaml_["server"]["listenPort"].as<unsigned short>()}),
|
||||
agent_({configYaml_["agent"]["threads"].as<unsigned short>(),
|
||||
configYaml_["agent"]["listenIp"].as<std::string>(),
|
||||
configYaml_["agent"]["listenPort"].as<unsigned short>(),
|
||||
configYaml_["agent"]["serverIp"].as<std::string>(),
|
||||
configYaml_["agent"]["serverPort"].as<unsigned short>(),
|
||||
configYaml_["agent"]["httpVersion"].as<std::string>(),
|
||||
configYaml_["agent"]["userAgent"].as<std::string>(),
|
||||
configYaml_["agent"]["tlsEnable"].as<bool>(false),
|
||||
configYaml_["agent"]["tlsVerifyPeer"].as<bool>(false),
|
||||
configYaml_["agent"]["tlsCaFile"].as<std::string>("")}) {
|
||||
configYaml_["agent"]["userAgent"].as<std::string>()}) {
|
||||
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
|
||||
@@ -67,38 +66,71 @@ Config::~Config() = default;
|
||||
|
||||
std::string Config::toString() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
ss << "\nConfig :\n"
|
||||
<< " General :\n"
|
||||
<< " token: " << general_.token << "\n"
|
||||
<< " fakeUrl: " << general_.fakeUrl << "\n"
|
||||
<< " method: " << general_.method << "\n"
|
||||
<< " fakeUrls: ";
|
||||
|
||||
for (std::size_t i = 0; i < general_.fakeUrls.size(); ++i) {
|
||||
ss << general_.fakeUrls[i];
|
||||
|
||||
if (i != general_.fakeUrls.size() - 1) {
|
||||
ss << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
ss << "\n"
|
||||
<< " methods: ";
|
||||
|
||||
for (std::size_t i = 0; i < general_.methods.size(); ++i) {
|
||||
ss << general_.methods[i];
|
||||
|
||||
if (i != general_.methods.size() - 1) {
|
||||
ss << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
ss << "\n"
|
||||
<< " endPoints: ";
|
||||
|
||||
for (std::size_t i = 0; i < general_.endPoints.size(); ++i) {
|
||||
ss << general_.endPoints[i];
|
||||
|
||||
if (i != general_.endPoints.size() - 1) {
|
||||
ss << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
ss << "\n"
|
||||
<< " timeout: " << general_.timeout << "\n"
|
||||
<< " tunnelEnable: " << general_.tunnelEnable << "\n"
|
||||
<< " tunnelEnable: " << std::boolalpha << general_.tunnelEnable << "\n"
|
||||
<< " connectionReuse: " << std::boolalpha << general_.connectionReuse << "\n"
|
||||
<< " tlsEnable: " << std::boolalpha << general_.tlsEnable << "\n"
|
||||
<< " tlsVerifyPeer: " << std::boolalpha << general_.tlsVerifyPeer << "\n"
|
||||
<< " tlsCertFile: " << general_.tlsCertFile << "\n"
|
||||
<< " tlsKeyFile: " << general_.tlsKeyFile << "\n"
|
||||
<< " tlsCaFile: " << general_.tlsCaFile << "\n"
|
||||
|
||||
<< " Log :\n"
|
||||
<< " logLevel: " << log_.level << "\n"
|
||||
<< " logFile: " << log_.file << "\n"
|
||||
<< " server :\n"
|
||||
|
||||
<< " Server :\n"
|
||||
<< " threads: " << server_.threads << "\n"
|
||||
<< " listenIp: " << server_.listenIp << "\n"
|
||||
<< " listenPort: " << server_.listenPort << "\n"
|
||||
<< " tlsEnable: " << server_.tlsEnable << "\n"
|
||||
<< " tlsVerifyPeer: " << server_.tlsVerifyPeer << "\n"
|
||||
<< " tlsCertFile: " << server_.tlsCertFile << "\n"
|
||||
<< " tlsKeyFile: " << server_.tlsKeyFile << "\n"
|
||||
<< " tlsCaFile: " << server_.tlsCaFile << "\n"
|
||||
<< " agent :\n"
|
||||
|
||||
<< " Agent :\n"
|
||||
<< " threads: " << agent_.threads << "\n"
|
||||
<< " listenIp: " << agent_.listenIp << "\n"
|
||||
<< " listenPort: " << agent_.listenPort << "\n"
|
||||
<< " serverIp: " << agent_.serverIp << "\n"
|
||||
<< " serverPort: " << agent_.serverPort << "\n"
|
||||
<< " httpVersion: " << agent_.httpVersion << "\n"
|
||||
<< " userAgent: " << agent_.userAgent << "\n"
|
||||
<< " tlsEnable: " << agent_.tlsEnable << "\n"
|
||||
<< " tlsVerifyPeer: " << agent_.tlsVerifyPeer << "\n"
|
||||
<< " tlsCaFile: " << agent_.tlsCaFile << "\n";
|
||||
<< " userAgent: " << agent_.userAgent << "\n";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
@@ -174,4 +206,52 @@ std::string Config::modeToString() const {
|
||||
default:
|
||||
return "UNKNOWN MODE";
|
||||
}
|
||||
}
|
||||
|
||||
std::string Config::randomFakeUrl() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
|
||||
if (general_.fakeUrls.empty()) {
|
||||
return "www.google.com";
|
||||
}
|
||||
|
||||
static thread_local std::mt19937 generator{std::random_device{}()};
|
||||
|
||||
std::uniform_int_distribution<std::size_t> distribution(
|
||||
0,
|
||||
general_.fakeUrls.size() - 1);
|
||||
|
||||
return general_.fakeUrls[distribution(generator)];
|
||||
}
|
||||
|
||||
std::string Config::randomMethod() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
|
||||
if (general_.methods.empty()) {
|
||||
return "GET";
|
||||
}
|
||||
|
||||
static thread_local std::mt19937 generator{std::random_device{}()};
|
||||
|
||||
std::uniform_int_distribution<std::size_t> distribution(
|
||||
0,
|
||||
general_.methods.size() - 1);
|
||||
|
||||
return general_.methods[distribution(generator)];
|
||||
}
|
||||
|
||||
std::string Config::randomEndPoint() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
|
||||
if (general_.endPoints.empty()) {
|
||||
return "api";
|
||||
}
|
||||
|
||||
static thread_local std::mt19937 generator{std::random_device{}()};
|
||||
|
||||
std::uniform_int_distribution<std::size_t> distribution(
|
||||
0,
|
||||
general_.endPoints.size() - 1);
|
||||
|
||||
return general_.endPoints[distribution(generator)];
|
||||
}
|
||||
+17
-12
@@ -4,8 +4,10 @@
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "general.hpp"
|
||||
|
||||
@@ -18,10 +20,17 @@ class Config : private Uncopyable {
|
||||
private:
|
||||
struct General {
|
||||
std::string token;
|
||||
std::string fakeUrl;
|
||||
std::string method;
|
||||
std::vector<std::string> fakeUrls;
|
||||
std::vector<std::string> methods;
|
||||
std::vector<std::string> endPoints;
|
||||
unsigned short timeout;
|
||||
bool tunnelEnable;
|
||||
bool connectionReuse;
|
||||
bool tlsEnable;
|
||||
bool tlsVerifyPeer;
|
||||
std::string tlsCertFile;
|
||||
std::string tlsKeyFile;
|
||||
std::string tlsCaFile;
|
||||
};
|
||||
|
||||
struct Log {
|
||||
@@ -33,12 +42,6 @@ private:
|
||||
unsigned short threads;
|
||||
std::string listenIp;
|
||||
unsigned short listenPort;
|
||||
|
||||
bool tlsEnable;
|
||||
bool tlsVerifyPeer;
|
||||
std::string tlsCertFile;
|
||||
std::string tlsKeyFile;
|
||||
std::string tlsCaFile;
|
||||
};
|
||||
|
||||
struct Agent {
|
||||
@@ -49,10 +52,6 @@ private:
|
||||
unsigned short serverPort;
|
||||
std::string httpVersion;
|
||||
std::string userAgent;
|
||||
|
||||
bool tlsEnable;
|
||||
bool tlsVerifyPeer;
|
||||
std::string tlsCaFile;
|
||||
};
|
||||
|
||||
const RunMode &runMode_;
|
||||
@@ -107,4 +106,10 @@ public:
|
||||
std::string modeToString() const;
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
std::string randomFakeUrl() const;
|
||||
|
||||
std::string randomMethod() const;
|
||||
|
||||
std::string randomEndPoint() const;
|
||||
};
|
||||
@@ -325,8 +325,9 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
|
||||
try {
|
||||
configYaml["general"]["token"].as<std::string>();
|
||||
configYaml["general"]["fakeUrl"].as<std::string>();
|
||||
configYaml["general"]["method"].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>>();
|
||||
configYaml["general"]["timeout"].as<unsigned short>();
|
||||
configYaml["general"]["tunnelEnable"].as<bool>();
|
||||
} catch (const std::exception &e) {
|
||||
|
||||
+8
-6
@@ -156,10 +156,11 @@ bool HTTP::parseTls() {
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
return std::string(config_->general().method + " " +
|
||||
config_->general().fakeUrl + " HTTP/" +
|
||||
auto fakeUrl = config_->randomFakeUrl();
|
||||
return std::string(config_->randomMethod() + " " +
|
||||
fakeUrl + " HTTP/" +
|
||||
config_->agent().httpVersion + "\r\n") +
|
||||
"Host: " + config_->general().fakeUrl + "\r\n" +
|
||||
"Host: " + fakeUrl + "\r\n" +
|
||||
"User-Agent: " + config_->agent().userAgent + "\r\n" +
|
||||
"Accept: */*\r\n" + "Connection: keep-alive\r\n" +
|
||||
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
|
||||
@@ -167,10 +168,11 @@ const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpRestPostReqString() const {
|
||||
return std::string(config_->general().method + " " +
|
||||
config_->general().fakeUrl + " HTTP/" +
|
||||
auto fakeUrl = config_->randomFakeUrl();
|
||||
return std::string(config_->randomMethod() + " " +
|
||||
fakeUrl + " HTTP/" +
|
||||
config_->agent().httpVersion + "\r\n") +
|
||||
"Host: " + config_->general().fakeUrl + "\r\n" +
|
||||
"Host: " + fakeUrl + "\r\n" +
|
||||
"User-Agent: " + config_->agent().userAgent + "\r\n" +
|
||||
"Accept: */*\r\n" + "Connection: keep-alive\r\n" +
|
||||
"Rest: yes\r\n" + "COMP\r\n\r\n";
|
||||
|
||||
+11
-11
@@ -91,7 +91,7 @@ void ServerHandler::makePlainHttpResponse(const std::string &body,
|
||||
std::ostringstream outer;
|
||||
|
||||
outer << "HTTP/1.1 " << status << "\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Type: application/text\r\n"
|
||||
<< "Content-Length: " << body.size() << "\r\n"
|
||||
<< "Connection: " << connection << "\r\n"
|
||||
<< "\r\n"
|
||||
@@ -198,7 +198,7 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
if (!targetClient || !targetClient->isOpen()) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ void ServerHandler::handle() {
|
||||
copyStringToStreambuf(hexToASCII(decryption.message), tunnelBuf);
|
||||
targetClient->doWrite(tunnelBuf);
|
||||
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
if (!targetClient || !targetClient->isOpen()) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ void ServerHandler::handle() {
|
||||
std::size_t available = targetClient->socket().available(ec);
|
||||
|
||||
if (ec || available == 0) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -241,14 +241,14 @@ void ServerHandler::handle() {
|
||||
ec);
|
||||
|
||||
if (ec || n == 0) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
std::string(data.data(), n),
|
||||
"200 OK",
|
||||
"close");
|
||||
(config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -262,7 +262,7 @@ void ServerHandler::handle() {
|
||||
sessions_.erase(it);
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ void ServerHandler::handle() {
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 200 Connection Established\r\n\r\n",
|
||||
"200 OK",
|
||||
"close");
|
||||
"keep-alive");
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
@@ -407,14 +407,14 @@ void ServerHandler::handle() {
|
||||
|
||||
if (client_->readBuffer().size() == 0) {
|
||||
client_->socket().close();
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
streambufToString(client_->readBuffer()),
|
||||
"200 OK",
|
||||
"close");
|
||||
(config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
|
||||
+161
-106
@@ -34,7 +34,6 @@ bool TCPClient::isOpen() const {
|
||||
}
|
||||
|
||||
bool TCPClient::enableTlsClient() {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
try {
|
||||
if (tlsEnabled_) return true;
|
||||
@@ -48,11 +47,11 @@ bool TCPClient::enableTlsClient() {
|
||||
|
||||
SSL_CTX_set_min_proto_version(sslContext_.native_handle(), TLS1_2_VERSION);
|
||||
|
||||
if (config_->agent().tlsVerifyPeer) {
|
||||
if (config_->general().tlsVerifyPeer) {
|
||||
sslContext_.set_verify_mode(boost::asio::ssl::verify_peer);
|
||||
|
||||
if (!config_->agent().tlsCaFile.empty()) {
|
||||
sslContext_.load_verify_file(config_->agent().tlsCaFile);
|
||||
if (!config_->general().tlsCaFile.empty()) {
|
||||
sslContext_.load_verify_file(config_->general().tlsCaFile);
|
||||
}
|
||||
} else {
|
||||
sslContext_.set_verify_mode(boost::asio::ssl::verify_none);
|
||||
@@ -92,16 +91,26 @@ bool TCPClient::enableTlsClient() {
|
||||
}
|
||||
|
||||
bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPort) {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
try {
|
||||
if (tlsEnabled_ && sslSocket_ && sslSocket_->lowest_layer().is_open()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!tlsEnabled_ && socket_.is_open()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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(), std::to_string(dstPort).c_str(), error_code);
|
||||
|
||||
auto endpoint = resolver_.resolve(
|
||||
dstIP.c_str(),
|
||||
std::to_string(dstPort).c_str(),
|
||||
error_code);
|
||||
|
||||
if (error_code) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
@@ -111,9 +120,19 @@ bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPor
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tlsEnabled_ && sslSocket_) {
|
||||
if (tlsEnabled_) {
|
||||
if (!sslSocket_ || closed_ || !sslSocket_->lowest_layer().is_open()) {
|
||||
sslSocket_ = std::make_unique<ssl_stream>(io_context_, sslContext_);
|
||||
closed_ = false;
|
||||
}
|
||||
|
||||
boost::asio::connect(sslSocket_->lowest_layer(), endpoint, error_code);
|
||||
} else {
|
||||
if (!socket_.is_open()) {
|
||||
socket_ = tcp::socket(io_context_);
|
||||
closed_ = false;
|
||||
}
|
||||
|
||||
boost::asio::connect(socket_, endpoint, error_code);
|
||||
}
|
||||
|
||||
@@ -128,7 +147,8 @@ bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPor
|
||||
return true;
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] " +
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doConnect] " +
|
||||
error.what(),
|
||||
Log::Level::ERROR);
|
||||
return false;
|
||||
@@ -136,12 +156,11 @@ bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPor
|
||||
}
|
||||
|
||||
bool TCPClient::doHandshakeClient() {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
try {
|
||||
if (!tlsEnabled_ || !sslSocket_) return true;
|
||||
|
||||
std::string sniHost = config_->general().fakeUrl;
|
||||
std::string sniHost = config_->randomFakeUrl();
|
||||
|
||||
if (!sniHost.empty()) {
|
||||
auto pos = sniHost.find("://");
|
||||
@@ -217,13 +236,11 @@ bool TCPClient::doHandshakeClient() {
|
||||
}
|
||||
|
||||
void TCPClient::writeBuffer(boost::asio::streambuf &buffer) {
|
||||
std::lock_guard lock(mutex_);
|
||||
moveStreambuf(buffer, writeBuffer_);
|
||||
}
|
||||
|
||||
|
||||
void TCPClient::doWrite(boost::asio::streambuf &buffer) {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
try {
|
||||
moveStreambuf(buffer, writeBuffer_);
|
||||
@@ -245,7 +262,7 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) {
|
||||
resetTimeout();
|
||||
|
||||
if (tlsEnabled_ && sslSocket_) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [DST " +
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [SSL] [DST " +
|
||||
sslSocket_->lowest_layer().remote_endpoint().address().to_string() +
|
||||
":" +
|
||||
std::to_string(sslSocket_->lowest_layer().remote_endpoint().port()) +
|
||||
@@ -253,7 +270,7 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) {
|
||||
Log::Level::DEBUG);
|
||||
boost::asio::write(*sslSocket_, writeBuffer_, error);
|
||||
} else {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [DST " +
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [TCP] [DST " +
|
||||
socket_.remote_endpoint().address().to_string() + ":" +
|
||||
std::to_string(socket_.remote_endpoint().port()) +
|
||||
"] [Bytes " + std::to_string(writeBuffer_.size()) + "] ",
|
||||
@@ -281,51 +298,70 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) {
|
||||
|
||||
void TCPClient::doReadAgent() {
|
||||
boost::system::error_code error;
|
||||
std::lock_guard lock(mutex_);
|
||||
readBuffer_.consume(readBuffer_.size());
|
||||
|
||||
try {
|
||||
if (!isOpen()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] Socket is not OPEN",
|
||||
Log::Level::DEBUG);
|
||||
return;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
readBuffer_.consume(readBuffer_.size());
|
||||
|
||||
if (!isOpen()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] Socket is not OPEN",
|
||||
Log::Level::DEBUG);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
resetTimeout();
|
||||
|
||||
bool ok = false;
|
||||
if (tlsEnabled_ && sslSocket_) {
|
||||
if (tlsEnabled_) {
|
||||
if (!sslSocket_) {
|
||||
cancelTimeout();
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] SSL socket is null",
|
||||
Log::Level::DEBUG);
|
||||
socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
ok = HttpUtils::readHttpMessage(*sslSocket_, readBuffer_, error);
|
||||
} else {
|
||||
ok = HttpUtils::readHttpMessage(socket_, readBuffer_, error);
|
||||
}
|
||||
|
||||
cancelTimeout();
|
||||
|
||||
if (!ok) {
|
||||
if (error == boost::asio::error::eof) {
|
||||
if (error == boost::asio::error::operation_aborted) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] [aborted]",
|
||||
Log::Level::DEBUG);
|
||||
} else if (error == boost::asio::error::eof) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] [EOF] Connection closed by peer.",
|
||||
Log::Level::TRACE);
|
||||
} else {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] [error] " + error.message(),
|
||||
"] [TCPClient doReadAgent] [error] " +
|
||||
error.message(),
|
||||
Log::Level::ERROR);
|
||||
}
|
||||
socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
if (readBuffer_.size() > 0) {
|
||||
copyStreambuf(readBuffer_, buffer_);
|
||||
} else {
|
||||
socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] [catch read] " + error.what(),
|
||||
"] [TCPClient doReadAgent] empty read buffer",
|
||||
Log::Level::DEBUG);
|
||||
socketShutdown();
|
||||
} catch (const std::exception &ex) {
|
||||
cancelTimeout();
|
||||
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doReadAgent] [catch read] " +
|
||||
ex.what(),
|
||||
Log::Level::DEBUG);
|
||||
socketShutdown();
|
||||
}
|
||||
@@ -334,7 +370,6 @@ void TCPClient::doReadAgent() {
|
||||
void TCPClient::doReadServer() {
|
||||
boost::system::error_code error;
|
||||
end_ = false;
|
||||
std::lock_guard lock(mutex_);
|
||||
readBuffer_.consume(readBuffer_.size());
|
||||
|
||||
try {
|
||||
@@ -429,26 +464,46 @@ void TCPClient::onTimeout(const boost::system::error_code &error) {
|
||||
}
|
||||
|
||||
void TCPClient::socketShutdown() {
|
||||
try {
|
||||
boost::system::error_code ignored;
|
||||
boost::system::error_code ec;
|
||||
|
||||
if (sslSocket_) {
|
||||
sslSocket_->shutdown(ignored);
|
||||
sslSocket_->lowest_layer().shutdown(
|
||||
boost::asio::ip::tcp::socket::shutdown_both, ignored);
|
||||
sslSocket_->lowest_layer().close(ignored);
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (socket_.is_open()) {
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored);
|
||||
socket_.close(ignored);
|
||||
}
|
||||
if (closed_) return;
|
||||
closed_ = true;
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient socketShutdown] [catch] " + error.what(),
|
||||
cancelTimeout();
|
||||
|
||||
if (sslSocket_) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient socketShutdown] [SSL]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
auto &lowest = sslSocket_->lowest_layer();
|
||||
|
||||
lowest.cancel(ec);
|
||||
ec.clear();
|
||||
|
||||
if (lowest.is_open()) {
|
||||
lowest.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
ec.clear();
|
||||
|
||||
lowest.close(ec);
|
||||
}
|
||||
}
|
||||
|
||||
if (socket_.is_open()) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient socketShutdown] [TCP]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
socket_.cancel(ec);
|
||||
ec.clear();
|
||||
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
ec.clear();
|
||||
|
||||
socket_.close(ec);
|
||||
}
|
||||
|
||||
tlsEnabled_ = false;
|
||||
}
|
||||
|
||||
std::string TCPClient::HttpUtils::toLowerCopy(std::string s) {
|
||||
@@ -553,10 +608,13 @@ bool TCPClient::HttpUtils::readHttpMessage(
|
||||
bool TCPClient::HttpUtils::readHttpMessageImpl(
|
||||
boost::asio::streambuf &out,
|
||||
boost::system::error_code &ec,
|
||||
const std::function<std::size_t(boost::asio::mutable_buffer,
|
||||
boost::system::error_code &)> &readSome,
|
||||
const std::function<bool(boost::asio::streambuf &,
|
||||
boost::system::error_code &)> &readHeaders) {
|
||||
const std::function<std::size_t(
|
||||
boost::asio::mutable_buffer,
|
||||
boost::system::error_code &)> &readSome,
|
||||
const std::function<bool(
|
||||
boost::asio::streambuf &,
|
||||
boost::system::error_code &)> &readHeaders) {
|
||||
|
||||
out.consume(out.size());
|
||||
|
||||
if (!readHeaders(out, ec)) {
|
||||
@@ -568,64 +626,61 @@ bool TCPClient::HttpUtils::readHttpMessageImpl(
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
std::vector<char> tmp(contentLength - body.size());
|
||||
std::size_t total = 0;
|
||||
|
||||
while (total < tmp.size()) {
|
||||
std::size_t n = readSome(
|
||||
boost::asio::buffer(tmp.data() + total, tmp.size() - total),
|
||||
ec);
|
||||
|
||||
if (ec) return false;
|
||||
total += n;
|
||||
while (body.size() < contentLength) {
|
||||
char tmp[8192];
|
||||
std::size_t remaining =
|
||||
contentLength - body.size();
|
||||
std::size_t toRead =
|
||||
std::min<std::size_t>(
|
||||
sizeof(tmp),
|
||||
remaining);
|
||||
std::size_t n =
|
||||
readSome(
|
||||
boost::asio::buffer(
|
||||
tmp,
|
||||
toRead),
|
||||
ec);
|
||||
if (ec) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(tmp.size()));
|
||||
if (n == 0) {
|
||||
ec = boost::asio::error::eof;
|
||||
return false;
|
||||
}
|
||||
boost::asio::buffer_copy(
|
||||
out.prepare(n),
|
||||
boost::asio::buffer(tmp, n));
|
||||
out.commit(n);
|
||||
body.append(tmp, n);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isChunked(headers)) {
|
||||
for (;;) {
|
||||
current = streambufToString(out);
|
||||
|
||||
auto bodyPos = current.find("\r\n\r\n");
|
||||
if (bodyPos == std::string::npos) return false;
|
||||
|
||||
std::string bodyPart = current.substr(bodyPos + 4);
|
||||
|
||||
auto lastChunkPos = bodyPart.find("\r\n0\r\n\r\n");
|
||||
if (lastChunkPos != std::string::npos || bodyPart == "0\r\n\r\n") {
|
||||
return true;
|
||||
while (
|
||||
body.find("\r\n0\r\n\r\n") ==
|
||||
std::string::npos &&
|
||||
body.find("\r\n0\r\n") ==
|
||||
std::string::npos) {
|
||||
char tmp[8192];
|
||||
std::size_t n =
|
||||
readSome(
|
||||
boost::asio::buffer(tmp),
|
||||
ec);
|
||||
if (ec) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = readSome(boost::asio::buffer(tmp), ec);
|
||||
|
||||
if (ec) return false;
|
||||
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
if (n == 0) {
|
||||
ec = boost::asio::error::eof;
|
||||
return false;
|
||||
}
|
||||
boost::asio::buffer_copy(
|
||||
out.prepare(n),
|
||||
boost::asio::buffer(tmp, n));
|
||||
out.commit(n);
|
||||
body.append(tmp, n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = readSome(boost::asio::buffer(tmp), ec);
|
||||
|
||||
if (ec == boost::asio::error::eof) {
|
||||
ec.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ec) return false;
|
||||
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -103,4 +103,5 @@ private:
|
||||
tcp::resolver resolver_;
|
||||
boost::asio::steady_timer timeout_;
|
||||
mutable std::mutex mutex_;
|
||||
bool closed_ = false;
|
||||
};
|
||||
+304
-211
@@ -7,149 +7,6 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBody(
|
||||
boost::asio::ip::tcp::socket &stream,
|
||||
boost::asio::streambuf &buf,
|
||||
boost::system::error_code &ec) {
|
||||
return readRemainingHttpBodyImpl(
|
||||
buf,
|
||||
ec,
|
||||
[&stream](boost::asio::mutable_buffer buffer, boost::system::error_code &error) {
|
||||
return stream.read_some(buffer, error);
|
||||
});
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBody(
|
||||
ssl_stream &stream,
|
||||
boost::asio::streambuf &buf,
|
||||
boost::system::error_code &ec) {
|
||||
return readRemainingHttpBodyImpl(
|
||||
buf,
|
||||
ec,
|
||||
[&stream](boost::asio::mutable_buffer buffer, boost::system::error_code &error) {
|
||||
return stream.read_some(buffer, error);
|
||||
});
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBodyImpl(
|
||||
boost::asio::streambuf &buf,
|
||||
boost::system::error_code &ec,
|
||||
const std::function<std::size_t(boost::asio::mutable_buffer,
|
||||
boost::system::error_code &)> &readSome) {
|
||||
std::string current = streambufToString(buf);
|
||||
std::string headers = extractHeaders(current);
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
std::vector<char> tmp(contentLength - body.size());
|
||||
std::size_t total = 0;
|
||||
|
||||
while (total < tmp.size()) {
|
||||
std::size_t n = readSome(
|
||||
boost::asio::buffer(tmp.data() + total, tmp.size() - total),
|
||||
ec);
|
||||
|
||||
if (ec) return false;
|
||||
total += n;
|
||||
}
|
||||
|
||||
std::ostream os(&buf);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(tmp.size()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isChunked(headers)) {
|
||||
for (;;) {
|
||||
current = streambufToString(buf);
|
||||
|
||||
auto pos = current.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return false;
|
||||
|
||||
auto bodyPart = current.substr(pos + 4);
|
||||
|
||||
if (bodyPart.find("\r\n0\r\n\r\n") != std::string::npos ||
|
||||
bodyPart == "0\r\n\r\n") {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = readSome(boost::asio::buffer(tmp), ec);
|
||||
|
||||
if (ec) return false;
|
||||
|
||||
std::ostream os(&buf);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TCPConnection::TCPConnection(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
@@ -179,16 +36,18 @@ bool TCPConnection::initTlsServerContext() {
|
||||
try {
|
||||
sslContext_ = boost::asio::ssl::context(boost::asio::ssl::context::tls_server);
|
||||
|
||||
sslContext_.set_options(boost::asio::ssl::context::default_workarounds |
|
||||
boost::asio::ssl::context::no_sslv2 |
|
||||
boost::asio::ssl::context::no_sslv3 |
|
||||
boost::asio::ssl::context::single_dh_use);
|
||||
sslContext_.set_options(
|
||||
boost::asio::ssl::context::default_workarounds |
|
||||
boost::asio::ssl::context::no_sslv2 |
|
||||
boost::asio::ssl::context::no_sslv3 |
|
||||
boost::asio::ssl::context::single_dh_use);
|
||||
|
||||
SSL_CTX_set_min_proto_version(sslContext_.native_handle(), TLS1_2_VERSION);
|
||||
|
||||
sslContext_.use_certificate_chain_file(config_->server().tlsCertFile);
|
||||
sslContext_.use_private_key_file(config_->server().tlsKeyFile,
|
||||
boost::asio::ssl::context::pem);
|
||||
sslContext_.use_certificate_chain_file(config_->general().tlsCertFile);
|
||||
sslContext_.use_private_key_file(
|
||||
config_->general().tlsKeyFile,
|
||||
boost::asio::ssl::context::pem);
|
||||
|
||||
if (!SSL_CTX_check_private_key(sslContext_.native_handle())) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
@@ -199,12 +58,13 @@ bool TCPConnection::initTlsServerContext() {
|
||||
|
||||
sslContext_.set_verify_mode(boost::asio::ssl::verify_none);
|
||||
|
||||
if (SSL_CTX_set_cipher_list(sslContext_.native_handle(),
|
||||
"ECDHE-RSA-AES256-GCM-SHA384:"
|
||||
"ECDHE-RSA-AES128-GCM-SHA256:"
|
||||
"ECDHE-RSA-CHACHA20-POLY1305:"
|
||||
"AES256-GCM-SHA384:"
|
||||
"AES128-GCM-SHA256") != 1) {
|
||||
if (SSL_CTX_set_cipher_list(
|
||||
sslContext_.native_handle(),
|
||||
"ECDHE-RSA-AES256-GCM-SHA384:"
|
||||
"ECDHE-RSA-AES128-GCM-SHA256:"
|
||||
"ECDHE-RSA-CHACHA20-POLY1305:"
|
||||
"AES256-GCM-SHA384:"
|
||||
"AES128-GCM-SHA256") != 1) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPConnection initTlsServerContext] failed to set cipher list",
|
||||
Log::Level::ERROR);
|
||||
@@ -212,16 +72,21 @@ bool TCPConnection::initTlsServerContext() {
|
||||
}
|
||||
|
||||
#if defined(TLS1_3_VERSION)
|
||||
SSL_CTX_set_ciphersuites(sslContext_.native_handle(),
|
||||
"TLS_AES_256_GCM_SHA384:"
|
||||
"TLS_AES_128_GCM_SHA256:"
|
||||
"TLS_CHACHA20_POLY1305_SHA256");
|
||||
SSL_CTX_set_ciphersuites(
|
||||
sslContext_.native_handle(),
|
||||
"TLS_AES_256_GCM_SHA384:"
|
||||
"TLS_AES_128_GCM_SHA256:"
|
||||
"TLS_CHACHA20_POLY1305_SHA256");
|
||||
#endif
|
||||
|
||||
tlsSocket_ = std::make_unique<ssl_stream>(io_context_, sslContext_);
|
||||
closed_ = false;
|
||||
|
||||
return true;
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPConnection initTlsServerContext] " + error.what(),
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPConnection initTlsServerContext] " +
|
||||
error.what(),
|
||||
Log::Level::ERROR);
|
||||
return false;
|
||||
}
|
||||
@@ -288,7 +153,7 @@ void TCPConnection::doReadServer() {
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred));
|
||||
|
||||
if (config_->server().tlsEnable) {
|
||||
if (config_->general().tlsEnable) {
|
||||
boost::asio::async_read_until(*tlsSocket_, readBuffer_, "\r\n\r\n", handler);
|
||||
} else {
|
||||
boost::asio::async_read_until(socket_, readBuffer_, "\r\n\r\n", handler);
|
||||
@@ -396,7 +261,9 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
self->relayClientToServer();
|
||||
self->relayServerToClient();
|
||||
} else {
|
||||
self->client_->socketShutdown();
|
||||
if (!self->config_->general().connectionReuse) {
|
||||
self->client_->socketShutdown();
|
||||
}
|
||||
self->startTunnelReadFromClient();
|
||||
self->startTunnelPollServer();
|
||||
}
|
||||
@@ -424,7 +291,7 @@ void TCPConnection::handleReadServer(const boost::system::error_code &error, siz
|
||||
boost::system::error_code bodyError;
|
||||
std::string remotePeer;
|
||||
|
||||
if (config_->server().tlsEnable) {
|
||||
if (config_->general().tlsEnable) {
|
||||
if (!HttpUtils::readRemainingHttpBody(*tlsSocket_, readBuffer_, bodyError)) {
|
||||
socketShutdown();
|
||||
return;
|
||||
@@ -493,10 +360,14 @@ void TCPConnection::handleReadServer(const boost::system::error_code &error, siz
|
||||
return;
|
||||
}
|
||||
|
||||
self->socketShutdown();
|
||||
if (self->config_->general().connectionReuse) {
|
||||
self->doReadServer();
|
||||
} else {
|
||||
self->socketShutdown();
|
||||
}
|
||||
};
|
||||
|
||||
if (config_->server().tlsEnable) {
|
||||
if (config_->general().tlsEnable) {
|
||||
boost::asio::async_write(
|
||||
*tlsSocket_,
|
||||
writeBuffer_.data(),
|
||||
@@ -519,7 +390,7 @@ void TCPConnection::handleReadServer(const boost::system::error_code &error, siz
|
||||
}
|
||||
|
||||
std::string TCPConnection::makeRelayHostHeader() const {
|
||||
std::string hostHeader = config_->general().fakeUrl;
|
||||
std::string hostHeader = config_->randomFakeUrl();
|
||||
|
||||
auto pos = hostHeader.find("://");
|
||||
if (pos != std::string::npos) hostHeader = hostHeader.substr(pos + 3);
|
||||
@@ -530,44 +401,78 @@ std::string TCPConnection::makeRelayHostHeader() const {
|
||||
return hostHeader;
|
||||
}
|
||||
|
||||
std::string TCPConnection::postTunnelAction(const std::string &action, const std::string &rawBody) {
|
||||
TCPClient::pointer c = TCPClient::create(io_context_, config_, log_);
|
||||
c->uuid_ = uuid_;
|
||||
std::string TCPConnection::postTunnelAction(const std::string &action,
|
||||
const std::string &rawBody) {
|
||||
client_->uuid_ = uuid_;
|
||||
|
||||
if (config_->agent().tlsEnable && !c->enableTlsClient()) return "";
|
||||
if (!c->doConnect(config_->agent().serverIp, config_->agent().serverPort)) return "";
|
||||
if (c->tlsEnabled() && !c->doHandshakeClient()) return "";
|
||||
auto sendOnce = [&]() -> std::string {
|
||||
const bool alreadyOpen = client_->isOpen();
|
||||
|
||||
BoolStr enc = aes256Encrypt(
|
||||
hexArrToStr(reinterpret_cast<const unsigned char *>(rawBody.data()), rawBody.size()),
|
||||
config_->general().token);
|
||||
if (!alreadyOpen) {
|
||||
if (config_->general().tlsEnable) {
|
||||
if (!client_->enableTlsClient()) return "";
|
||||
}
|
||||
|
||||
if (!enc.ok) return "";
|
||||
if (!client_->doConnect(config_->agent().serverIp,
|
||||
config_->agent().serverPort)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const std::string encodedBody = encode64(enc.message);
|
||||
if (client_->tlsEnabled()) {
|
||||
if (!client_->doHandshakeClient()) return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::ostringstream req;
|
||||
req << config_->general().method + " / HTTP/1.1\r\n"
|
||||
<< "Host: " << makeRelayHostHeader() << "\r\n"
|
||||
<< "User-Agent: Mozilla/5.0\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "X-Nipo-Session: " << to_string(uuid_) << "\r\n"
|
||||
<< "X-Nipo-Action: " << action << "\r\n"
|
||||
<< "Content-Length: " << encodedBody.size() << "\r\n"
|
||||
<< "Connection: close\r\n"
|
||||
<< "\r\n"
|
||||
<< encodedBody;
|
||||
BoolStr enc = aes256Encrypt(
|
||||
hexArrToStr(
|
||||
reinterpret_cast<const unsigned char *>(rawBody.data()),
|
||||
rawBody.size()),
|
||||
config_->general().token);
|
||||
|
||||
boost::asio::streambuf out;
|
||||
copyStringToStreambuf(req.str(), out);
|
||||
if (!enc.ok) return "";
|
||||
|
||||
c->doWrite(out);
|
||||
c->doReadAgent();
|
||||
const std::string encodedBody = encode64(enc.message);
|
||||
|
||||
const std::string response = streambufToString(c->readBuffer());
|
||||
const bool keepAlive =
|
||||
config_->general().connectionReuse && action != "close";
|
||||
|
||||
c->socketShutdown();
|
||||
std::ostringstream req;
|
||||
req << config_->randomMethod() + " /" + config_->randomEndPoint()
|
||||
<< " HTTP/" << config_->agent().httpVersion << "\r\n"
|
||||
<< "Host: " << makeRelayHostHeader() << "\r\n"
|
||||
<< "User-Agent: " << config_->agent().userAgent << "\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
<< "Content-Type: application/text\r\n"
|
||||
<< "X-Nipo-Session: " << to_string(uuid_) << "\r\n"
|
||||
<< "X-Nipo-Action: " << action << "\r\n"
|
||||
<< "Content-Length: " << encodedBody.size() << "\r\n"
|
||||
<< "Connection: " << (keepAlive ? "keep-alive" : "close") << "\r\n"
|
||||
<< "\r\n"
|
||||
<< encodedBody;
|
||||
|
||||
boost::asio::streambuf out;
|
||||
copyStringToStreambuf(req.str(), out);
|
||||
|
||||
client_->doWrite(out);
|
||||
client_->doReadAgent();
|
||||
|
||||
const std::string response = streambufToString(client_->readBuffer());
|
||||
|
||||
if (!keepAlive) {
|
||||
client_->socketShutdown();
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
std::string response = sendOnce();
|
||||
|
||||
if (response.empty() &&
|
||||
config_->general().connectionReuse &&
|
||||
action != "close") {
|
||||
client_->socketShutdown();
|
||||
response = sendOnce();
|
||||
}
|
||||
|
||||
const auto pos = response.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return "";
|
||||
@@ -651,22 +556,35 @@ void TCPConnection::closeTunnelSession() {
|
||||
|
||||
void TCPConnection::resetTimeout() {
|
||||
if (!config_->general().timeout) return;
|
||||
if (closed_) return;
|
||||
|
||||
timeout_.cancel();
|
||||
|
||||
timeout_.expires_after(std::chrono::seconds(config_->general().timeout));
|
||||
timeout_.async_wait(boost::bind(&TCPConnection::onTimeout,
|
||||
shared_from_this(),
|
||||
boost::asio::placeholders::error));
|
||||
|
||||
timeout_.async_wait(
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
[self = shared_from_this()](const boost::system::error_code &error) {
|
||||
self->onTimeout(error);
|
||||
}));
|
||||
}
|
||||
|
||||
void TCPConnection::cancelTimeout() {
|
||||
if (config_->general().timeout) timeout_.cancel();
|
||||
if (!config_->general().timeout) return;
|
||||
|
||||
boost::system::error_code ignored;
|
||||
timeout_.cancel();
|
||||
}
|
||||
|
||||
void TCPConnection::onTimeout(const boost::system::error_code &error) {
|
||||
if (error || error == boost::asio::error::operation_aborted) return;
|
||||
if (error == boost::asio::error::operation_aborted) return;
|
||||
if (error) return;
|
||||
if (connect_) return;
|
||||
if (closed_) return;
|
||||
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPConnection onTimeout] [expiration] " +
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPConnection onTimeout] [expiration] " +
|
||||
std::to_string(+config_->general().timeout) +
|
||||
" seconds has passed, and the timeout has expired",
|
||||
Log::Level::TRACE);
|
||||
@@ -675,32 +593,64 @@ void TCPConnection::onTimeout(const boost::system::error_code &error) {
|
||||
}
|
||||
|
||||
void TCPConnection::socketShutdown() {
|
||||
try {
|
||||
boost::system::error_code ignored;
|
||||
if (closed_.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
boost::system::error_code ignored;
|
||||
|
||||
try {
|
||||
pollTimer_.cancel();
|
||||
ignored.clear();
|
||||
|
||||
timeout_.cancel();
|
||||
ignored.clear();
|
||||
|
||||
if (tlsSocket_) {
|
||||
tlsSocket_->shutdown(ignored);
|
||||
tlsSocket_->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored);
|
||||
tlsSocket_->lowest_layer().close(ignored);
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPConnection socketShutdown] [SSL]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
auto &lowest = tlsSocket_->lowest_layer();
|
||||
|
||||
lowest.cancel(ignored);
|
||||
ignored.clear();
|
||||
|
||||
if (lowest.is_open()) {
|
||||
lowest.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored);
|
||||
ignored.clear();
|
||||
|
||||
lowest.close(ignored);
|
||||
ignored.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (socket_.is_open()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPConnection socketShutdown] [TCP]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
socket_.cancel(ignored);
|
||||
ignored.clear();
|
||||
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored);
|
||||
ignored.clear();
|
||||
|
||||
socket_.close(ignored);
|
||||
ignored.clear();
|
||||
}
|
||||
|
||||
if (client_) {
|
||||
client_->socketShutdown();
|
||||
}
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPConnection socketShutdown] [catch] " + error.what(),
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPConnection socketShutdown] [catch] " +
|
||||
error.what(),
|
||||
Log::Level::DEBUG);
|
||||
}
|
||||
}
|
||||
|
||||
void TCPConnection::enableDirectTunnelMode() {
|
||||
tunnelMode_ = true;
|
||||
}
|
||||
@@ -709,7 +659,7 @@ void TCPConnection::asyncWriteToAgentConnection(
|
||||
const char *data,
|
||||
std::size_t bytes,
|
||||
std::function<void(const boost::system::error_code &)> done) {
|
||||
if (config_->server().tlsEnable && tlsSocket_) {
|
||||
if (config_->general().tlsEnable && tlsSocket_) {
|
||||
boost::asio::async_write(
|
||||
*tlsSocket_,
|
||||
boost::asio::buffer(data, bytes),
|
||||
@@ -734,7 +684,7 @@ void TCPConnection::asyncWriteToAgentConnection(
|
||||
void TCPConnection::asyncReadFromAgentConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done) {
|
||||
if (config_->server().tlsEnable && tlsSocket_) {
|
||||
if (config_->general().tlsEnable && tlsSocket_) {
|
||||
tlsSocket_->async_read_some(
|
||||
boost::asio::buffer(buffer),
|
||||
boost::asio::bind_executor(strand_, done));
|
||||
@@ -913,4 +863,147 @@ void TCPConnection::relayTargetToAgent() {
|
||||
self->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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBody(
|
||||
boost::asio::ip::tcp::socket &stream,
|
||||
boost::asio::streambuf &buf,
|
||||
boost::system::error_code &ec) {
|
||||
return readRemainingHttpBodyImpl(
|
||||
buf,
|
||||
ec,
|
||||
[&stream](boost::asio::mutable_buffer buffer, boost::system::error_code &error) {
|
||||
return stream.read_some(buffer, error);
|
||||
});
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBody(
|
||||
ssl_stream &stream,
|
||||
boost::asio::streambuf &buf,
|
||||
boost::system::error_code &ec) {
|
||||
return readRemainingHttpBodyImpl(
|
||||
buf,
|
||||
ec,
|
||||
[&stream](boost::asio::mutable_buffer buffer, boost::system::error_code &error) {
|
||||
return stream.read_some(buffer, error);
|
||||
});
|
||||
}
|
||||
|
||||
bool TCPConnection::HttpUtils::readRemainingHttpBodyImpl(
|
||||
boost::asio::streambuf &buf,
|
||||
boost::system::error_code &ec,
|
||||
const std::function<std::size_t(boost::asio::mutable_buffer,
|
||||
boost::system::error_code &)> &readSome) {
|
||||
std::string current = streambufToString(buf);
|
||||
std::string headers = extractHeaders(current);
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
std::vector<char> tmp(contentLength - body.size());
|
||||
std::size_t total = 0;
|
||||
|
||||
while (total < tmp.size()) {
|
||||
std::size_t n = readSome(
|
||||
boost::asio::buffer(tmp.data() + total, tmp.size() - total),
|
||||
ec);
|
||||
|
||||
if (ec) return false;
|
||||
total += n;
|
||||
}
|
||||
|
||||
std::ostream os(&buf);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(tmp.size()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isChunked(headers)) {
|
||||
for (;;) {
|
||||
current = streambufToString(buf);
|
||||
|
||||
auto pos = current.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return false;
|
||||
|
||||
auto bodyPart = current.substr(pos + 4);
|
||||
|
||||
if (bodyPart.find("\r\n0\r\n\r\n") != std::string::npos ||
|
||||
bodyPart == "0\r\n\r\n") {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = readSome(boost::asio::buffer(tmp), ec);
|
||||
|
||||
if (ec) return false;
|
||||
|
||||
std::ostream os(&buf);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
@@ -64,21 +65,21 @@ public:
|
||||
void relayAgentToTarget();
|
||||
void relayTargetToAgent();
|
||||
|
||||
void asyncWriteToAgentConnection(
|
||||
inline void asyncWriteToAgentConnection(
|
||||
const char *data,
|
||||
std::size_t bytes,
|
||||
std::function<void(const boost::system::error_code &)> done);
|
||||
|
||||
void asyncReadFromAgentConnection(
|
||||
inline void asyncReadFromAgentConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done);
|
||||
|
||||
void asyncWriteToAgentServerConnection(
|
||||
inline void asyncWriteToAgentServerConnection(
|
||||
const char *data,
|
||||
std::size_t bytes,
|
||||
std::function<void(const boost::system::error_code &)> done);
|
||||
|
||||
void asyncReadFromAgentServerConnection(
|
||||
inline void asyncReadFromAgentServerConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done);
|
||||
|
||||
@@ -144,6 +145,7 @@ private:
|
||||
bool tunnelMode_;
|
||||
bool tunnelClosed_;
|
||||
bool pollInProgress_;
|
||||
std::atomic_bool closed_{false};
|
||||
|
||||
std::array<char, 8192> downstreamBuf_;
|
||||
std::array<char, 8192> upstreamBuf_;
|
||||
|
||||
@@ -31,7 +31,7 @@ void TCPServer::handleAccept(TCPConnection::pointer connection,
|
||||
try {
|
||||
if (!error) {
|
||||
if (config_->runMode() == RunMode::server) {
|
||||
if (config_->server().tlsEnable) {
|
||||
if (config_->general().tlsEnable) {
|
||||
if (!connection->initTlsServerContext()) {
|
||||
log_->write("[TCPServer handleAccept] TLS server context init failed",
|
||||
Log::Level::ERROR);
|
||||
|
||||
@@ -3,21 +3,54 @@
|
||||
general:
|
||||
# token: 32 bytes of string. this will be used on agent and server side to encryption
|
||||
token: "af445adb-2434-4975-9445-2c1b2231"
|
||||
# fakeUrl: string
|
||||
# Defines the fake url between agent and server
|
||||
fakeUrl: "nipo.ciron.net"
|
||||
# method: "HEAD|GET|POST|PUT|DELETE"
|
||||
# Defines with which method
|
||||
# fakeUrls:
|
||||
# Defines the list of fake urls between agent and server
|
||||
fakeUrls:
|
||||
- www.google.com
|
||||
- nipo.ciron.net
|
||||
- sudoer.net
|
||||
- sudoer.ir
|
||||
# methods:
|
||||
# Defines the list of using methods
|
||||
# you like to send requests from agent to server
|
||||
method: "GET"
|
||||
# Program will choose randomly from this list
|
||||
methods:
|
||||
- HEAD
|
||||
- GET
|
||||
- POST
|
||||
- PUT
|
||||
- DELETE
|
||||
# endPoints:
|
||||
# Defines the list of using endpoints
|
||||
# you like to send requests from agent to server
|
||||
# Program will choose randomly from this list
|
||||
endPoints:
|
||||
- api
|
||||
- login
|
||||
- user
|
||||
- update
|
||||
# timeout: unsigned short
|
||||
# Defines the timeout for I/O Operation in seconds. 0 indicates no timeout.
|
||||
# Useful to automatically close stalled connections.
|
||||
timeout: 10
|
||||
# tunnelEnable: bool
|
||||
# Degines the connection mode between agent and server. If enabled then the traffic
|
||||
# Defines the connection mode between agent and server. If enabled then the traffic
|
||||
# will be TCP/TLS tunnel like.
|
||||
tunnelEnable: false
|
||||
# connectionReuse: bool
|
||||
# If false then it will use same connection between agent and server to handle client request
|
||||
# If true then it will create new connection for each request between agent and server
|
||||
connectionReuse: true
|
||||
# tlsEnable: bool
|
||||
# It enables or disables tls between agent and server
|
||||
tlsEnable: false
|
||||
# tlsVerifyPeer: bool
|
||||
# Use it to controll if the certificate should be validated or not
|
||||
tlsVerifyPeer: false
|
||||
# Use following configurations to specify the key and cert which will be used between agent and server
|
||||
tlsCertFile: "/etc/nipovpn/server.crt"
|
||||
tlsKeyFile: "/etc/nipovpn/server.key"
|
||||
tlsCaFile: ""
|
||||
|
||||
|
||||
# This block is to define log directives
|
||||
@@ -33,14 +66,10 @@ server:
|
||||
# threads: number of threads in server
|
||||
threads: 8
|
||||
# listenIp: which must be IPv4 which is used on server
|
||||
listenIp: "127.0.0.10"
|
||||
listenIp: "0.0.0.0"
|
||||
# listenPort: the port number which will be used on server
|
||||
listenPort: 8081
|
||||
tlsEnable: false
|
||||
tlsVerifyPeer: false
|
||||
tlsCertFile: "/etc/nipovpn/server.crt"
|
||||
tlsKeyFile: "/etc/nipovpn/server.key"
|
||||
tlsCaFile: ""
|
||||
listenPort: 80
|
||||
|
||||
|
||||
# This block is to define agent directives
|
||||
agent:
|
||||
@@ -53,11 +82,8 @@ agent:
|
||||
# serverIp: IPv4 of the nipoServer
|
||||
serverIp: "127.0.0.10"
|
||||
# serverPort: port number of the nipoServer
|
||||
serverPort: 8081
|
||||
serverPort: 80
|
||||
# httpVersion: This version will be used between agent and server
|
||||
httpVersion: "1.1"
|
||||
# userAgent: This is the http user agent which will be used between agent and server
|
||||
userAgent : "NipoAgent"
|
||||
tlsEnable: false
|
||||
tlsVerifyPeer: false
|
||||
tlsCaFile: ""
|
||||
userAgent : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0"
|
||||
Reference in new issue
Block a user