Compare commits
3
Commits
dev
...
v.10.1.1.10
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11b2787746 | ||
|
|
0cd2b91412 | ||
|
|
92b348b7ad |
No files matched your search
@@ -102,7 +102,7 @@ void AgentHandler::handle() {
|
||||
if (pos != std::string::npos) hostHeader = hostHeader.substr(0, pos);
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "POST /relay HTTP/1.1\r\n"
|
||||
outer << "POST / HTTP/1.1\r\n"
|
||||
<< "Host: " << hostHeader << "\r\n"
|
||||
<< "User-Agent: Mozilla/5.0\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
|
||||
@@ -365,4 +365,56 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
result.ok = true;
|
||||
result.message = "OK";
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::string toLowerCopy(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string 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);
|
||||
}
|
||||
|
||||
inline std::string extractBody(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(pos + 4);
|
||||
}
|
||||
|
||||
inline bool parseContentLength(const std::string &headers, std::size_t &value) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("content-length:", 0) == 0) {
|
||||
auto raw = line.substr(std::string("Content-Length:").size());
|
||||
raw.erase(0, raw.find_first_not_of(" \t"));
|
||||
try {
|
||||
value = static_cast<std::size_t>(std::stoull(raw));
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isChunked(const std::string &headers) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("transfer-encoding:", 0) == 0 &&
|
||||
lower.find("chunked") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
+21
-13
@@ -163,9 +163,24 @@ const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
config_->agent().httpVersion + "\r\n") +
|
||||
"Host: " + config_->general().fakeUrl + "\r\n" +
|
||||
"User-Agent: " + config_->agent().userAgent + "\r\n" +
|
||||
"Accept: */*\r\n" + "Connection: keep-alive\r\n" +
|
||||
"Accept: */*\r\n" +
|
||||
"Connection: keep-alive\r\n" +
|
||||
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" + "\r\n" + body + "COMP\r\n\r\n";
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"\r\n" +
|
||||
body;
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpOkResString(const std::string &body) const {
|
||||
return std::string("HTTP/1.1 200 OK\r\n") +
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
|
||||
config_->general().chunkHeader + ": " + chunkHeader_ + "\r\n" +
|
||||
"Connection: keep-alive\r\n" +
|
||||
"Cache-Control: no-cache\r\n" +
|
||||
"Pragma: no-cache\r\n" +
|
||||
"\r\n" +
|
||||
body;
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpRestPostReqString() const {
|
||||
@@ -174,17 +189,10 @@ const std::string HTTP::genHttpRestPostReqString() const {
|
||||
config_->agent().httpVersion + "\r\n") +
|
||||
"Host: " + config_->general().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";
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpOkResString(const std::string &body) const {
|
||||
return std::string("HTTP/1.1 200 OK\r\n") +
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
|
||||
config_->general().chunkHeader + ": " + chunkHeader_ + "\r\n" +
|
||||
"Connection: keep-alive\r\n" + "Cache-Control: no-cache\r\n" +
|
||||
"Pragma: no-cache\r\n" + "\r\n" + body + "COMP\r\n\r\n";
|
||||
"Accept: */*\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"Rest: yes\r\n"
|
||||
"\r\n";
|
||||
}
|
||||
|
||||
void HTTP::setIPPort() {
|
||||
|
||||
@@ -38,7 +38,7 @@ void ServerHandler::handle() {
|
||||
const std::string method = std::string(outerReq.method_string());
|
||||
const std::string target = std::string(outerReq.target());
|
||||
|
||||
if (method != "POST" || target != "/relay") {
|
||||
if (method != "POST" || target != "/") {
|
||||
copyStringToStreambuf(
|
||||
"HTTP/1.1 404 Not Found\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
|
||||
+58
-112
@@ -6,118 +6,6 @@
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string 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 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 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 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 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;
|
||||
}
|
||||
|
||||
template<typename SyncReadStream>
|
||||
bool readHttpMessage(SyncReadStream &stream,
|
||||
boost::asio::streambuf &out,
|
||||
boost::system::error_code &ec) {
|
||||
out.consume(out.size());
|
||||
|
||||
boost::asio::read_until(stream, out, "\r\n\r\n", ec);
|
||||
if (ec) return false;
|
||||
|
||||
std::string current = streambufToString(out);
|
||||
std::string headers = extractHeaders(current);
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
boost::asio::read(stream, out,
|
||||
boost::asio::transfer_exactly(contentLength - body.size()),
|
||||
ec);
|
||||
if (ec) return false;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(boost::asio::buffer(tmp), ec);
|
||||
if (ec) return false;
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(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));
|
||||
}
|
||||
}
|
||||
|
||||
}// namespace
|
||||
|
||||
TCPClient::TCPClient(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
@@ -134,10 +22,68 @@ TCPClient::TCPClient(boost::asio::io_context &io_context,
|
||||
end_ = false;
|
||||
}
|
||||
|
||||
|
||||
TCPClient::tcp::socket &TCPClient::socket() { return socket_; }
|
||||
|
||||
TCPClient::ssl_stream &TCPClient::sslSocket() { return *sslSocket_; }
|
||||
|
||||
template<typename SyncReadStream>
|
||||
bool readHttpMessage(SyncReadStream &stream,
|
||||
boost::asio::streambuf &out,
|
||||
boost::system::error_code &ec) {
|
||||
out.consume(out.size());
|
||||
|
||||
boost::asio::read_until(stream, out, "\r\n\r\n", ec);
|
||||
if (ec) return false;
|
||||
|
||||
std::string current = streambufToString(out);
|
||||
std::string headers = extractHeaders(current);
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
boost::asio::read(stream, out,
|
||||
boost::asio::transfer_exactly(contentLength - body.size()),
|
||||
ec);
|
||||
if (ec) return false;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(boost::asio::buffer(tmp), ec);
|
||||
if (ec) return false;
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(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));
|
||||
}
|
||||
}
|
||||
|
||||
bool TCPClient::tlsEnabled() const { return tlsEnabled_; }
|
||||
|
||||
bool TCPClient::isOpen() const {
|
||||
|
||||
+783
-264
File diff suppressed because it is too large.
Load diff
+35
-42
@@ -3,7 +3,6 @@
|
||||
#include <array>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/enable_shared_from_this.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/uuid/random_generator.hpp>
|
||||
@@ -18,94 +17,88 @@
|
||||
class TCPConnection : public boost::enable_shared_from_this<TCPConnection> {
|
||||
public:
|
||||
using pointer = boost::shared_ptr<TCPConnection>;
|
||||
using ssl_stream =
|
||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
|
||||
using ssl_stream = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
|
||||
|
||||
static pointer create(boost::asio::io_context &io_context,
|
||||
static pointer create(boost::asio::io_context &ioContext,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client) {
|
||||
return pointer(new TCPConnection(io_context, config, log, client));
|
||||
}
|
||||
TCPClient::pointer client);
|
||||
|
||||
boost::asio::ip::tcp::socket &socket();
|
||||
|
||||
inline TCPClient::pointer client() { return client_; }
|
||||
|
||||
ssl_stream &tlsSocket();
|
||||
|
||||
bool initTlsServerContext();
|
||||
TCPClient::pointer client() const { return client_; }
|
||||
|
||||
bool initTlsServerContext();
|
||||
bool doHandshakeServer();
|
||||
|
||||
inline void writeBuffer(boost::asio::streambuf &buffer) {
|
||||
moveStreambuf(buffer, writeBuffer_);
|
||||
}
|
||||
void writeBuffer(boost::asio::streambuf &buffer);
|
||||
const boost::asio::streambuf &readBuffer() const & { return readBuffer_; }
|
||||
|
||||
inline const boost::asio::streambuf &readBuffer() & { return readBuffer_; }
|
||||
|
||||
inline const boost::asio::streambuf &&readBuffer() && {
|
||||
return std::move(readBuffer_);
|
||||
}
|
||||
|
||||
inline boost::uuids::uuid uuid() { return uuid_; }
|
||||
boost::uuids::uuid uuid() const { return uuid_; }
|
||||
|
||||
void startAgent();
|
||||
|
||||
void startServer();
|
||||
|
||||
void doReadAgent();
|
||||
|
||||
void doReadServer();
|
||||
|
||||
void handleReadAgent(const boost::system::error_code &error,
|
||||
size_t bytes_transferred);
|
||||
std::size_t bytesTransferred);
|
||||
|
||||
void handleReadServer(const boost::system::error_code &error,
|
||||
size_t bytes_transferred);
|
||||
|
||||
void socketShutdown();
|
||||
std::size_t bytesTransferred);
|
||||
|
||||
void enableTunnelMode();
|
||||
|
||||
void relayClientToRemote();
|
||||
|
||||
void relayRemoteToClient();
|
||||
|
||||
void socketShutdown();
|
||||
|
||||
private:
|
||||
explicit TCPConnection(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client);
|
||||
TCPConnection(boost::asio::io_context &ioContext,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client);
|
||||
|
||||
void resetTimeout();
|
||||
|
||||
void cancelTimeout();
|
||||
|
||||
void onTimeout(const boost::system::error_code &error);
|
||||
|
||||
void clearTunnelBuffers();
|
||||
|
||||
private:
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
boost::asio::ssl::context sslContext_;
|
||||
std::unique_ptr<ssl_stream> tlsSocket_;
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
boost::asio::io_context &io_context_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
boost::asio::io_context &ioContext_;
|
||||
TCPClient::pointer client_;
|
||||
|
||||
boost::asio::streambuf readBuffer_;
|
||||
boost::asio::streambuf writeBuffer_;
|
||||
|
||||
boost::asio::streambuf downstreamHttpBuffer_;
|
||||
boost::asio::streambuf upstreamHttpBuffer_;
|
||||
|
||||
boost::asio::steady_timer timeout_;
|
||||
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
|
||||
|
||||
boost::uuids::uuid uuid_;
|
||||
std::mutex mutex_;
|
||||
|
||||
bool end_;
|
||||
bool connect_;
|
||||
bool tunnelMode_;
|
||||
bool end_{false};
|
||||
bool connect_{false};
|
||||
bool tunnelMode_{false};
|
||||
|
||||
std::array<char, 16384> downstreamBuf_;
|
||||
std::array<char, 16384> upstreamBuf_;
|
||||
bool tunnelRequestHeaderSent_{false};
|
||||
bool tunnelResponseHeaderSent_{false};
|
||||
bool tunnelRequestHeaderReceived_{false};
|
||||
bool tunnelResponseHeaderReceived_{false};
|
||||
|
||||
std::array<char, 16384> downstreamBuf_{};
|
||||
std::array<char, 16384> upstreamBuf_{};
|
||||
};
|
||||
@@ -34,8 +34,8 @@ server:
|
||||
# listenIp: which must be IPv4 which is used on server
|
||||
listenIp: "0.0.0.0"
|
||||
# listenPort: the port number which will be used on server
|
||||
listenPort: 8081
|
||||
tlsEnable: false
|
||||
listenPort: 443
|
||||
tlsEnable: true
|
||||
tlsVerifyPeer: false
|
||||
tlsCertFile: "/etc/nipovpn/server.crt"
|
||||
tlsKeyFile: "/etc/nipovpn/server.key"
|
||||
@@ -52,11 +52,11 @@ agent:
|
||||
# serverIp: IPv4 of the nipoServer
|
||||
serverIp: "127.0.0.10"
|
||||
# serverPort: port number of the nipoServer
|
||||
serverPort: 8081
|
||||
serverPort: 443
|
||||
# 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
|
||||
tlsEnable: true
|
||||
tlsVerifyPeer: false
|
||||
tlsCaFile: ""
|
||||
Reference in new issue
Block a user