Compare commits
28
Commits
v.11.1.1.2
..
v
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92a290cca7 | ||
|
|
108b9729c7 | ||
|
|
7baaf6aefb | ||
|
|
4793a3f0e3 | ||
|
|
c8cd4066d7 | ||
|
|
33a4dbd0b8 | ||
|
|
01d8b36ca0 | ||
|
|
cd7b985da1 | ||
|
|
80c631f66b | ||
|
|
8a1fcde280 | ||
|
|
51feb98dc3 | ||
|
|
560b5d93cc | ||
|
|
97ee705eec | ||
|
|
8d1ecfc2dc | ||
|
|
b402144e9e | ||
|
|
e7c8cb51cc | ||
|
|
51fd1d47cc | ||
|
|
251b0202e8 | ||
|
|
10e4481376 | ||
|
|
47b56af86e | ||
|
|
1c8d2d610b | ||
|
|
86f2b750a8 | ||
|
|
0e4c1bf00d | ||
|
|
1452c1bd82 | ||
|
|
09d5e25509 | ||
|
|
73f9cb01c1 | ||
|
|
f70ef566d7 | ||
|
|
064fe1614a |
No files matched your search
@@ -3,10 +3,11 @@ name: Build and Release Debian Package
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- dev
|
||||
- main
|
||||
paths:
|
||||
- 'core/src/**'
|
||||
- '.github/workflows/**'
|
||||
- '.github/workflows/debian-dev.yml'
|
||||
- 'nipovpn/**'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -14,39 +15,70 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set package version
|
||||
run: echo "PACKAGE_VERSION=0.1.${{ github.run_number }}" >> "$GITHUB_ENV"
|
||||
|
||||
- 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
|
||||
run: |
|
||||
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
|
||||
rm -rf package
|
||||
mkdir -p package/DEBIAN
|
||||
mkdir -p package/usr/bin
|
||||
|
||||
cp build/core/nipovpn package/usr/bin/nipovpn
|
||||
chmod 755 package/usr/bin/nipovpn
|
||||
|
||||
cat > package/DEBIAN/control <<EOF
|
||||
Package: nipovpn
|
||||
Version: ${PACKAGE_VERSION}
|
||||
Maintainer: Morteza Bashsiz
|
||||
Architecture: amd64
|
||||
Description: Powerful and Secure HTTP/S Proxy
|
||||
Depends: libyaml-cpp0.8
|
||||
Homepage: https://github.com/MortezaBashsiz/nipovpn
|
||||
EOF
|
||||
|
||||
dpkg-deb --build package "build/nipovpn_dev_${PACKAGE_VERSION}_amd64.deb"
|
||||
|
||||
- name: Show package information
|
||||
run: |
|
||||
dpkg -I "build/nipovpn_dev_${PACKAGE_VERSION}_amd64.deb"
|
||||
|
||||
- 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
|
||||
path: build/nipovpn_dev_${{ env.PACKAGE_VERSION }}_amd64.deb
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Set package version
|
||||
run: echo "PACKAGE_VERSION=0.1.${{ github.run_number }}" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Download build artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
@@ -54,22 +86,10 @@ jobs:
|
||||
path: ./
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
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
|
||||
prerelease: true
|
||||
|
||||
- name: Upload Debian Package to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
tag_name: v${{ env.PACKAGE_VERSION }}
|
||||
name: Version v${{ env.PACKAGE_VERSION }}
|
||||
files: nipovpn_dev_${{ env.PACKAGE_VERSION }}_amd64.deb
|
||||
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: ${{ secrets.ACCESS_TOKEN }}
|
||||
@@ -3,10 +3,11 @@ name: Build and Release Debian Package
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- mori
|
||||
- main
|
||||
paths:
|
||||
- 'core/src/**'
|
||||
- '.github/workflows/**'
|
||||
- '.github/workflows/debian-mori.yml'
|
||||
- 'nipovpn/**'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -14,39 +15,70 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set package version
|
||||
run: echo "PACKAGE_VERSION=0.0.1.${{ github.run_number }}" >> "$GITHUB_ENV"
|
||||
|
||||
- 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
|
||||
run: |
|
||||
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
|
||||
rm -rf package
|
||||
mkdir -p package/DEBIAN
|
||||
mkdir -p package/usr/bin
|
||||
|
||||
cp build/core/nipovpn package/usr/bin/nipovpn
|
||||
chmod 755 package/usr/bin/nipovpn
|
||||
|
||||
cat > package/DEBIAN/control <<EOF
|
||||
Package: nipovpn
|
||||
Version: ${PACKAGE_VERSION}
|
||||
Maintainer: Morteza Bashsiz
|
||||
Architecture: amd64
|
||||
Description: Powerful and Secure HTTP/S Proxy
|
||||
Depends: libyaml-cpp0.8
|
||||
Homepage: https://github.com/MortezaBashsiz/nipovpn
|
||||
EOF
|
||||
|
||||
dpkg-deb --build package "build/nipovpn_mori_${PACKAGE_VERSION}_amd64.deb"
|
||||
|
||||
- name: Show package information
|
||||
run: |
|
||||
dpkg -I "build/nipovpn_mori_${PACKAGE_VERSION}_amd64.deb"
|
||||
|
||||
- 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
|
||||
path: build/nipovpn_mori_${{ env.PACKAGE_VERSION }}_amd64.deb
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Set package version
|
||||
run: echo "PACKAGE_VERSION=0.0.1.${{ github.run_number }}" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Download build artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
@@ -54,22 +86,10 @@ jobs:
|
||||
path: ./
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
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
|
||||
prerelease: true
|
||||
|
||||
- name: Upload Debian Package to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
tag_name: v${{ env.PACKAGE_VERSION }}
|
||||
name: Version v${{ env.PACKAGE_VERSION }}
|
||||
files: nipovpn_mori_${{ env.PACKAGE_VERSION }}_amd64.deb
|
||||
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: ${{ secrets.ACCESS_TOKEN }}
|
||||
@@ -6,6 +6,8 @@ on:
|
||||
- main
|
||||
paths:
|
||||
- 'core/src/**'
|
||||
- '.github/workflows/debian.yml'
|
||||
- 'nipovpn/**'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -13,39 +15,70 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set package version
|
||||
run: echo "PACKAGE_VERSION=1.1.${{ github.run_number }}" >> "$GITHUB_ENV"
|
||||
|
||||
- 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
|
||||
run: |
|
||||
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
|
||||
rm -rf package
|
||||
mkdir -p package/DEBIAN
|
||||
mkdir -p package/usr/bin
|
||||
|
||||
cp build/core/nipovpn package/usr/bin/nipovpn
|
||||
chmod 755 package/usr/bin/nipovpn
|
||||
|
||||
cat > package/DEBIAN/control <<EOF
|
||||
Package: nipovpn
|
||||
Version: ${PACKAGE_VERSION}
|
||||
Maintainer: Morteza Bashsiz
|
||||
Architecture: amd64
|
||||
Description: Powerful and Secure HTTP/S Proxy
|
||||
Depends: libyaml-cpp0.8
|
||||
Homepage: https://github.com/MortezaBashsiz/nipovpn
|
||||
EOF
|
||||
|
||||
dpkg-deb --build package "build/nipovpn_${PACKAGE_VERSION}_amd64.deb"
|
||||
|
||||
- name: Show package information
|
||||
run: |
|
||||
dpkg -I "build/nipovpn_${PACKAGE_VERSION}_amd64.deb"
|
||||
|
||||
- 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
|
||||
path: build/nipovpn_${{ env.PACKAGE_VERSION }}_amd64.deb
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Set package version
|
||||
run: echo "PACKAGE_VERSION=1.1.${{ github.run_number }}" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Download build artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
@@ -53,23 +86,10 @@ jobs:
|
||||
path: ./
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tag_name: 'v1.1.${{ github.run_number }}'
|
||||
release_name: Version v1.1.${{ github.run_number }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
- name: Upload Debian Package to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
tag_name: v${{ env.PACKAGE_VERSION }}
|
||||
name: Version v${{ env.PACKAGE_VERSION }}
|
||||
files: nipovpn_${{ env.PACKAGE_VERSION }}_amd64.deb
|
||||
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: ${{ secrets.ACCESS_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;
|
||||
}
|
||||
|
||||
+79
-25
@@ -10,29 +10,28 @@ Config::Config(const RunMode &mode, const std::string &filePath)
|
||||
listenPort_(0),
|
||||
general_({configYaml_["general"]["token"].as<std::string>(),
|
||||
configYaml_["general"]["fakeUrl"].as<std::string>(),
|
||||
configYaml_["general"]["method"].as<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,61 @@ 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"
|
||||
<< " 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 +196,36 @@ std::string Config::modeToString() const {
|
||||
default:
|
||||
return "UNKNOWN MODE";
|
||||
}
|
||||
}
|
||||
|
||||
std::string Config::randomMethod() const {
|
||||
std::lock_guard<std::mutex> lock(configMutex_);
|
||||
|
||||
if (general_.methods.empty()) {
|
||||
return "GET";// fallback
|
||||
}
|
||||
|
||||
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";// fallback
|
||||
}
|
||||
|
||||
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)];
|
||||
}
|
||||
+14
-11
@@ -4,8 +4,10 @@
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "general.hpp"
|
||||
|
||||
@@ -19,9 +21,16 @@ private:
|
||||
struct General {
|
||||
std::string token;
|
||||
std::string fakeUrl;
|
||||
std::string method;
|
||||
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,8 @@ public:
|
||||
std::string modeToString() const;
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
std::string randomMethod() const;
|
||||
|
||||
std::string randomEndPoint() const;
|
||||
};
|
||||
@@ -326,7 +326,8 @@ 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"]["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) {
|
||||
|
||||
+2
-2
@@ -156,7 +156,7 @@ bool HTTP::parseTls() {
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
return std::string(config_->general().method + " " +
|
||||
return std::string(config_->randomMethod() + " " +
|
||||
config_->general().fakeUrl + " HTTP/" +
|
||||
config_->agent().httpVersion + "\r\n") +
|
||||
"Host: " + config_->general().fakeUrl + "\r\n" +
|
||||
@@ -167,7 +167,7 @@ const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpRestPostReqString() const {
|
||||
return std::string(config_->general().method + " " +
|
||||
return std::string(config_->randomMethod() + " " +
|
||||
config_->general().fakeUrl + " HTTP/" +
|
||||
config_->agent().httpVersion + "\r\n") +
|
||||
"Host: " + config_->general().fakeUrl + "\r\n" +
|
||||
|
||||
+121
-61
@@ -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"
|
||||
@@ -120,32 +120,33 @@ void ServerHandler::makeEncryptedHttpResponse(const std::string &plainBody,
|
||||
}
|
||||
|
||||
void ServerHandler::handle() {
|
||||
std::lock_guard lock(mutex_);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (!request_->detectType()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [NOT HTTP Request From Agent] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
Log::Level::DEBUG);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [NOT HTTP Request From Agent] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
client_->socket().close();
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &outerReq = request_->parsedHttpRequest();
|
||||
const std::string method = std::string(outerReq.method_string());
|
||||
const std::string target = std::string(outerReq.target());
|
||||
|
||||
const std::string outerRaw = streambufToString(readBuffer_);
|
||||
const auto outerHeaderEnd = outerRaw.find("\r\n\r\n");
|
||||
|
||||
const std::string outerHeaders =
|
||||
outerHeaderEnd == std::string::npos
|
||||
? ""
|
||||
: outerRaw.substr(0, outerHeaderEnd + 2);
|
||||
|
||||
const std::string session = HttpUtils::getRawHeader(outerHeaders, "X-Nipo-Session");
|
||||
const std::string action = HttpUtils::getRawHeader(outerHeaders, "X-Nipo-Action");
|
||||
const std::string session =
|
||||
HttpUtils::getRawHeader(outerHeaders, "X-Nipo-Session");
|
||||
|
||||
const std::string action =
|
||||
HttpUtils::getRawHeader(outerHeaders, "X-Nipo-Action");
|
||||
|
||||
std::string requestBody = std::string(outerReq.body());
|
||||
|
||||
@@ -156,50 +157,56 @@ void ServerHandler::handle() {
|
||||
BoolStr decryption{false, std::string("FAILED")};
|
||||
|
||||
try {
|
||||
decryption = aes256Decrypt(decode64(requestBody), config_->general().token);
|
||||
decryption = aes256Decrypt(
|
||||
decode64(requestBody),
|
||||
config_->general().token);
|
||||
} catch (const std::exception &e) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Base64 Decode Failed] " + e.what(),
|
||||
Log::Level::DEBUG);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [Base64 Decode Failed] " + e.what(),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
makePlainHttpResponse("", "400 Bad Request", "close");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!decryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Failed] : [ " +
|
||||
decryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [Decryption Failed] : [ " +
|
||||
decryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
makePlainHttpResponse("", "400 Bad Request", "close");
|
||||
return;
|
||||
}
|
||||
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Done]",
|
||||
Log::Level::DEBUG);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Done]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
if (action == "send") {
|
||||
TCPClient::pointer targetClient;
|
||||
|
||||
{
|
||||
std::lock_guard sessionLock(sessionsMutex_);
|
||||
|
||||
std::lock_guard<std::mutex> sessionLock(sessionsMutex_);
|
||||
auto it = sessions_.find(session);
|
||||
|
||||
if (it != sessions_.end()) {
|
||||
targetClient = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if (!targetClient || !targetClient->isOpen()) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
boost::asio::streambuf tunnelBuf;
|
||||
copyStringToStreambuf(hexToASCII(decryption.message), tunnelBuf);
|
||||
|
||||
targetClient->doWrite(tunnelBuf);
|
||||
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -207,16 +214,16 @@ void ServerHandler::handle() {
|
||||
TCPClient::pointer targetClient;
|
||||
|
||||
{
|
||||
std::lock_guard sessionLock(sessionsMutex_);
|
||||
|
||||
std::lock_guard<std::mutex> sessionLock(sessionsMutex_);
|
||||
auto it = sessions_.find(session);
|
||||
|
||||
if (it != sessions_.end()) {
|
||||
targetClient = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if (!targetClient || !targetClient->isOpen()) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -224,26 +231,30 @@ 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;
|
||||
}
|
||||
|
||||
std::vector<char> data(std::min<std::size_t>(available, 65536));
|
||||
|
||||
std::size_t n = targetClient->socket().read_some(boost::asio::buffer(data), ec);
|
||||
std::size_t n = targetClient->socket().read_some(
|
||||
boost::asio::buffer(data),
|
||||
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");
|
||||
makeEncryptedHttpResponse(
|
||||
std::string(data.data(), n),
|
||||
"200 OK",
|
||||
(config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (action == "close") {
|
||||
std::lock_guard sessionLock(sessionsMutex_);
|
||||
|
||||
std::lock_guard<std::mutex> sessionLock(sessionsMutex_);
|
||||
auto it = sessions_.find(session);
|
||||
|
||||
if (it != sessions_.end()) {
|
||||
@@ -251,17 +262,19 @@ void ServerHandler::handle() {
|
||||
sessions_.erase(it);
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse("", "200 OK", (config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string innerRequest = hexToASCII(decryption.message);
|
||||
|
||||
const std::size_t headerEnd = innerRequest.find("\r\n\r\n");
|
||||
|
||||
if (headerEnd == std::string::npos) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [INNER REQUEST MISSING HEADER END]",
|
||||
Log::Level::DEBUG);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [INNER REQUEST MISSING HEADER END]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
makeEncryptedHttpResponse("", "400 Bad Request", "close");
|
||||
return;
|
||||
@@ -275,10 +288,11 @@ void ServerHandler::handle() {
|
||||
HTTP::pointer inner = HTTP::create(config_, log_, readBuffer_, uuid_);
|
||||
|
||||
if (!inner->detectType()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [NOT INNER HTTP Request] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
Log::Level::DEBUG);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [NOT INNER HTTP Request] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
makeEncryptedHttpResponse("", "400 Bad Request", "close");
|
||||
return;
|
||||
@@ -286,6 +300,42 @@ void ServerHandler::handle() {
|
||||
|
||||
switch (inner->httpType()) {
|
||||
case HTTP::HttpType::connect: {
|
||||
if (config_->general().tunnelEnable) {
|
||||
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()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 502 Bad Gateway\r\n\r\n",
|
||||
"502 Bad Gateway",
|
||||
"close");
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [TUNNEL] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 200 Connection Established\r\n\r\n",
|
||||
"200 OK",
|
||||
"keep-alive");
|
||||
|
||||
connect_ = true;
|
||||
end_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
TCPClient::pointer targetClient = TCPClient::create(
|
||||
static_cast<boost::asio::io_context &>(
|
||||
client_->socket().get_executor().context()),
|
||||
@@ -295,20 +345,29 @@ void ServerHandler::handle() {
|
||||
targetClient->uuid_ = uuid_;
|
||||
|
||||
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()) + "]",
|
||||
Log::Level::INFO);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [ERROR] [Resolving Host] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse("HTTP/1.1 502 Bad Gateway\r\n\r\n",
|
||||
"502 Bad Gateway",
|
||||
"close");
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 502 Bad Gateway\r\n\r\n",
|
||||
"502 Bad Gateway",
|
||||
"close");
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [CONNECT] [SRC " +
|
||||
clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
if (!pendingTunnelData.empty()) {
|
||||
boost::asio::streambuf pendingBuf;
|
||||
@@ -317,14 +376,14 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard sessionLock(sessionsMutex_);
|
||||
|
||||
std::lock_guard<std::mutex> sessionLock(sessionsMutex_);
|
||||
sessions_[session.empty() ? to_string(uuid_) : session] = targetClient;
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse("HTTP/1.1 200 Connection Established\r\n\r\n",
|
||||
"200 OK",
|
||||
"close");
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 200 Connection Established\r\n\r\n",
|
||||
"200 OK",
|
||||
"keep-alive");
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
@@ -348,13 +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");
|
||||
makeEncryptedHttpResponse(
|
||||
streambufToString(client_->readBuffer()),
|
||||
"200 OK",
|
||||
(config_->general().connectionReuse ? "keep-alive" : "close"));
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
|
||||
+160
-105
@@ -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,7 +156,6 @@ 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;
|
||||
@@ -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;
|
||||
};
|
||||
+858
-514
File diff suppressed because it is too large.
Load diff
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
@@ -56,6 +57,32 @@ public:
|
||||
|
||||
void socketShutdown();
|
||||
|
||||
void enableDirectTunnelMode();
|
||||
|
||||
void relayClientToServer();
|
||||
void relayServerToClient();
|
||||
|
||||
void relayAgentToTarget();
|
||||
void relayTargetToAgent();
|
||||
|
||||
inline void asyncWriteToAgentConnection(
|
||||
const char *data,
|
||||
std::size_t bytes,
|
||||
std::function<void(const boost::system::error_code &)> done);
|
||||
|
||||
inline void asyncReadFromAgentConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done);
|
||||
|
||||
inline void asyncWriteToAgentServerConnection(
|
||||
const char *data,
|
||||
std::size_t bytes,
|
||||
std::function<void(const boost::system::error_code &)> done);
|
||||
|
||||
inline void asyncReadFromAgentServerConnection(
|
||||
std::array<char, 8192> &buffer,
|
||||
std::function<void(const boost::system::error_code &, std::size_t)> done);
|
||||
|
||||
private:
|
||||
struct HttpUtils {
|
||||
static std::string toLowerCopy(std::string s);
|
||||
@@ -88,7 +115,7 @@ private:
|
||||
void cancelTimeout();
|
||||
void onTimeout(const boost::system::error_code &error);
|
||||
|
||||
void startTunnelReadFromBrowser();
|
||||
void startTunnelReadFromClient();
|
||||
void startTunnelPollServer();
|
||||
std::string postTunnelAction(const std::string &action, const std::string &rawBody);
|
||||
std::string makeRelayHostHeader() const;
|
||||
@@ -118,7 +145,8 @@ private:
|
||||
bool tunnelMode_;
|
||||
bool tunnelClosed_;
|
||||
bool pollInProgress_;
|
||||
std::atomic_bool closed_{false};
|
||||
|
||||
std::array<char, 65536> downstreamBuf_;
|
||||
std::array<char, 65536> upstreamBuf_;
|
||||
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);
|
||||
|
||||
@@ -5,19 +5,48 @@ general:
|
||||
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
|
||||
fakeUrl: "www.google.com"
|
||||
# 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: false
|
||||
# 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 +62,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:
|
||||
@@ -51,13 +76,10 @@ agent:
|
||||
# listenPort: the port number which will be used on agent
|
||||
listenPort: 8080
|
||||
# serverIp: IPv4 of the nipoServer
|
||||
serverIp: "127.0.0.10"
|
||||
serverIp: "127.0.0.1"
|
||||
# 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