Add socks5 support
This commit is contained in:
1 parent
55004368e4
commit
d8f09b9fe2
11 files changed
+194
-20
No files matched your search
@@ -100,8 +100,8 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_exec.so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_exec.so
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_dev_"${{ env.PACKAGE_VERSION }}".so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_dev_"${{ env.PACKAGE_VERSION }}".so
|
||||
|
||||
find "$VCPKG_ROOT/installed/arm64-android" -type f -name "*.so*" -exec cp -v {} out/android/arm64-v8a/ \; || true
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
PACKAGE_VERSION: 1.1.55
|
||||
|
||||
jobs:
|
||||
build-android:
|
||||
@@ -100,8 +100,8 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_exec.so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_exec.so
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_"${{ env.PACKAGE_VERSION }}".so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_"${{ env.PACKAGE_VERSION }}".so
|
||||
|
||||
find "$VCPKG_ROOT/installed/arm64-android" -type f -name "*.so*" -exec cp -v {} out/android/arm64-v8a/ \; || true
|
||||
|
||||
|
||||
@@ -100,8 +100,8 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_exec.so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_exec.so
|
||||
cp build-android-arm64/core/nipovpn out/android/arm64-v8a/libnipovpn_mori_"${{ env.PACKAGE_VERSION }}".so
|
||||
chmod 755 out/android/arm64-v8a/libnipovpn_mori_"${{ env.PACKAGE_VERSION }}".so
|
||||
|
||||
find "$VCPKG_ROOT/installed/arm64-android" -type f -name "*.so*" -exec cp -v {} out/android/arm64-v8a/ \; || true
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
PACKAGE_VERSION: 1.1.55
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
@@ -15,7 +15,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
PACKAGE_VERSION: 1.1.55
|
||||
|
||||
jobs:
|
||||
build-windows:
|
||||
|
||||
@@ -9,6 +9,7 @@ Config::Config(RunMode mode, const std::string &filePath)
|
||||
listenIp_("127.0.0.1"),
|
||||
listenPort_(0),
|
||||
general_({configYaml_["general"]["token"].as<std::string>(),
|
||||
configYaml_["general"]["protocol"].as<std::string>(""),
|
||||
configYaml_["general"]["fakeUrls"].as<std::vector<std::string>>(),
|
||||
configYaml_["general"]["methods"].as<std::vector<std::string>>(),
|
||||
configYaml_["general"]["endPoints"].as<std::vector<std::string>>(),
|
||||
@@ -73,6 +74,7 @@ std::string Config::toString() const {
|
||||
ss << "\nConfig :\n"
|
||||
<< " General :\n"
|
||||
<< " token: " << general_.token << "\n"
|
||||
<< " protocol: " << general_.protocol << "\n"
|
||||
<< " fakeUrls: ";
|
||||
|
||||
for (std::size_t i = 0; i < general_.fakeUrls.size(); ++i) {
|
||||
|
||||
@@ -20,6 +20,7 @@ class Config : private Uncopyable {
|
||||
private:
|
||||
struct General {
|
||||
std::string token;
|
||||
std::string protocol;
|
||||
std::vector<std::string> fakeUrls;
|
||||
std::vector<std::string> methods;
|
||||
std::vector<std::string> endPoints;
|
||||
|
||||
@@ -320,6 +320,7 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
|
||||
try {
|
||||
configYaml["general"]["token"].as<std::string>();
|
||||
configYaml["general"]["protocol"].as<std::string>();
|
||||
configYaml["general"]["fakeUrls"].as<std::vector<std::string>>();
|
||||
configYaml["general"]["methods"].as<std::vector<std::string>>();
|
||||
configYaml["general"]["endPoints"].as<std::vector<std::string>>();
|
||||
@@ -359,6 +360,11 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (configYaml["general"]["protocol"].as<std::string>() != "http" && configYaml["general"]["protocol"].as<std::string>() != "socks5") {
|
||||
result.message = "First argument must be one of [http|socks5]\n";
|
||||
return result;
|
||||
}
|
||||
|
||||
result.ok = true;
|
||||
result.message = "OK";
|
||||
return result;
|
||||
|
||||
+169
-11
@@ -126,20 +126,30 @@ void TCPConnection::doReadAgent() {
|
||||
try {
|
||||
readBuffer_.consume(readBuffer_.size());
|
||||
writeBuffer_.consume(writeBuffer_.size());
|
||||
|
||||
resetTimeout();
|
||||
|
||||
boost::asio::async_read(socket_,
|
||||
readBuffer_,
|
||||
boost::asio::transfer_at_least(1),
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
boost::bind(&TCPConnection::handleReadAgent,
|
||||
shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
socket_.async_read_some(
|
||||
readBuffer_.prepare(8192),
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
[self = shared_from_this()](
|
||||
const boost::system::error_code &ec,
|
||||
std::size_t bytes) {
|
||||
if (!ec) {
|
||||
self->readBuffer_.commit(bytes);
|
||||
}
|
||||
|
||||
self->handleReadAgent(ec, bytes);
|
||||
}));
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPConnection doReadAgent] [catch] " + error.what(),
|
||||
Log::Level::ERROR);
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) +
|
||||
"] [TCPConnection doReadAgent] [catch] " +
|
||||
error.what(),
|
||||
Log::Level::ERROR);
|
||||
|
||||
socketShutdown();
|
||||
}
|
||||
}
|
||||
@@ -180,7 +190,15 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
|
||||
boost::system::error_code errorIn;
|
||||
|
||||
if (config_->general().protocol == "socks5") {
|
||||
if (!handleSocks5Handshake()) {
|
||||
socketShutdown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string current = streambufToString(readBuffer_);
|
||||
|
||||
if (current.find("\r\n\r\n") == std::string::npos) {
|
||||
boost::asio::read_until(socket_, readBuffer_, "\r\n\r\n", errorIn);
|
||||
}
|
||||
@@ -237,6 +255,18 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
end_ = agentHandler_->end_;
|
||||
connect_ = agentHandler_->connect_;
|
||||
|
||||
if (config_->general().protocol == "socks5" && connect_) {
|
||||
writeBuffer_.consume(writeBuffer_.size());
|
||||
|
||||
const unsigned char ok[10] = {
|
||||
0x05, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00};
|
||||
|
||||
std::ostream os(&writeBuffer_);
|
||||
os.write(reinterpret_cast<const char *>(ok), sizeof(ok));
|
||||
}
|
||||
|
||||
if (writeBuffer_.size() == 0) {
|
||||
socketShutdown();
|
||||
return;
|
||||
@@ -268,12 +298,14 @@ void TCPConnection::handleReadAgent(const boost::system::error_code &error, size
|
||||
if (!self->config_->general().connectionReuse) {
|
||||
self->client_->socketShutdown();
|
||||
}
|
||||
|
||||
self->startTunnelReadFromClient();
|
||||
self->startTunnelPollServer();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self->doReadAgent();
|
||||
}));
|
||||
} catch (std::exception &error) {
|
||||
@@ -490,6 +522,132 @@ std::string TCPConnection::postTunnelAction(const std::string &action,
|
||||
return dec.message;
|
||||
}
|
||||
|
||||
bool TCPConnection::readExactFromClient(void *data, std::size_t size) {
|
||||
auto *out = static_cast<char *>(data);
|
||||
std::size_t copied = 0;
|
||||
|
||||
while (copied < size && readBuffer_.size() > 0) {
|
||||
std::size_t n = std::min(size - copied, readBuffer_.size());
|
||||
|
||||
auto buffers = readBuffer_.data();
|
||||
std::size_t copiedNow = boost::asio::buffer_copy(
|
||||
boost::asio::buffer(out + copied, n),
|
||||
buffers);
|
||||
|
||||
readBuffer_.consume(copiedNow);
|
||||
copied += copiedNow;
|
||||
}
|
||||
|
||||
if (copied == size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boost::system::error_code ec;
|
||||
boost::asio::read(
|
||||
socket_,
|
||||
boost::asio::buffer(out + copied, size - copied),
|
||||
ec);
|
||||
|
||||
return !ec;
|
||||
}
|
||||
|
||||
bool TCPConnection::handleSocks5Handshake() {
|
||||
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [TCPConnection handleSocks5Handshake] handshake start",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
unsigned char header[2];
|
||||
if (!readExactFromClient(header, 2)) {
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [TCPConnection handleSocks5Handshake] [SOCKS5] version=" +
|
||||
std::to_string(header[0]),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header[0] != 0x05) {
|
||||
log_->write("[" + to_string(uuid_) + "] [SOCKS5] invalid version", Log::Level::DEBUG);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> methods(header[1]);
|
||||
if (!readExactFromClient(methods.data(), methods.size())) return false;
|
||||
|
||||
const unsigned char authReply[2] = {0x05, 0x00};
|
||||
boost::asio::write(socket_, boost::asio::buffer(authReply, 2));
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [TCPConnection handleSocks5Handshake] auth reply sent" +
|
||||
std::to_string(header[0]),
|
||||
Log::Level::DEBUG);
|
||||
|
||||
|
||||
unsigned char req[4];
|
||||
if (!readExactFromClient(req, 4)) return false;
|
||||
|
||||
if (req[0] != 0x05 || req[1] != 0x01) {
|
||||
const unsigned char fail[10] = {0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0};
|
||||
boost::asio::write(socket_, boost::asio::buffer(fail, 10));
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string host;
|
||||
|
||||
if (req[3] == 0x01) {
|
||||
unsigned char ip[4];
|
||||
if (!readExactFromClient(ip, 4)) return false;
|
||||
|
||||
host =
|
||||
std::to_string(ip[0]) + "." +
|
||||
std::to_string(ip[1]) + "." +
|
||||
std::to_string(ip[2]) + "." +
|
||||
std::to_string(ip[3]);
|
||||
} else if (req[3] == 0x03) {
|
||||
unsigned char len;
|
||||
if (!readExactFromClient(&len, 1)) return false;
|
||||
|
||||
std::vector<char> domain(len);
|
||||
if (!readExactFromClient(domain.data(), domain.size())) return false;
|
||||
|
||||
host.assign(domain.begin(), domain.end());
|
||||
} else if (req[3] == 0x04) {
|
||||
unsigned char ip[16];
|
||||
if (!readExactFromClient(ip, 16)) return false;
|
||||
|
||||
boost::asio::ip::address_v6::bytes_type bytes;
|
||||
std::copy(ip, ip + 16, bytes.begin());
|
||||
host = boost::asio::ip::address_v6(bytes).to_string();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned char portBytes[2];
|
||||
if (!readExactFromClient(portBytes, 2)) return false;
|
||||
|
||||
unsigned short port =
|
||||
static_cast<unsigned short>((portBytes[0] << 8) | portBytes[1]);
|
||||
|
||||
std::string target = host + ":" + std::to_string(port);
|
||||
|
||||
std::string connectRequest =
|
||||
"CONNECT " + target +
|
||||
" HTTP/1.1\r\n"
|
||||
"Host: " +
|
||||
target +
|
||||
"\r\n"
|
||||
"\r\n";
|
||||
|
||||
copyStringToStreambuf(connectRequest, readBuffer_);
|
||||
|
||||
log_->write(
|
||||
"[" + to_string(uuid_) + "] [SOCKS5] CONNECT " + target,
|
||||
Log::Level::INFO);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TCPConnection::startTunnelReadFromClient() {
|
||||
if (tunnelClosed_ || !socket_.is_open()) return;
|
||||
|
||||
|
||||
@@ -109,6 +109,9 @@ private:
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client);
|
||||
|
||||
bool handleSocks5Handshake();
|
||||
bool readExactFromClient(void *data, std::size_t size);
|
||||
|
||||
void resetTimeout();
|
||||
void cancelTimeout();
|
||||
void onTimeout(const boost::system::error_code &error);
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
general:
|
||||
# token: 32 bytes of string. this will be used on agent and server side to encryption
|
||||
token: "af445adb-2434-4975-9445-2c1b2231"
|
||||
# protocol:
|
||||
# Defines the protocol between client and agent
|
||||
# Choose http or socks5
|
||||
protocol: http
|
||||
# fakeUrls:
|
||||
# Defines the list of fake urls between agent and server
|
||||
fakeUrls:
|
||||
|
||||
Reference in new issue
Block a user