1#include "ProxyServer.h"
3#include "JSONArrayWrapper.h"
4#include "HTTPSNetwork.h"
6#include "Exceptions/SSLException.h"
14 ProxyServer::ProxyData::ProxyData(string_view ip, string_view port, DWORD timeout,
bool isHTTPS) :
15 BaseConnectionData(ip, port, timeout),
21 void ProxyServer::clientConnection(
const string& ip, SOCKET clientSocket, sockaddr addr, function<
void()>& cleanup)
27 ssl = SSL_new(context);
31 throw web::exceptions::SSLException(__LINE__, __FILE__);
34 if (!SSL_set_fd(ssl,
static_cast<int>(clientSocket)))
38 throw web::exceptions::SSLException(__LINE__, __FILE__);
41 if (
int errorCode = SSL_accept(ssl); errorCode != 1)
43 throw web::exceptions::SSLException(__LINE__, __FILE__, ssl, errorCode);
47 streams::IOSocketStream clientStream
50 make_unique<web::HTTPSNetwork>(clientSocket, ssl, context) :
51 make_unique<web::HTTPNetwork>(clientSocket)
57 clientStream >> request;
59 web::HTTPParser parser(request);
61 string route = parser.getParameters();
63 if (route.find(
'?') != string::npos)
65 route.resize(route.find(
'?'));
68 const ProxyData& proxyData = *routes.at(route);
70 streams::IOSocketStream serverStream
73 make_unique<web::HTTPSNetwork>(proxyData.ip, proxyData.port, proxyData.timeout) :
74 make_unique<web::HTTPNetwork>(proxyData.ip, proxyData.port, proxyData.timeout)
77 serverStream << request;
79 serverStream >> response;
81 clientStream << response;
84 ProxyServer::ProxyServer(string_view ip, string_view port, DWORD timeout,
const json::utility::jsonObject& proxySettings) :
95 vector<json::utility::jsonObject> servers = json::utility::JSONArrayWrapper(proxySettings.getArray(
"proxiedServers")).getAsObjectArray();
97 proxyData.reserve(servers.size());
99 for (
const json::utility::jsonObject& proxiedServer : servers)
101 const string& ip = proxiedServer.getString(
"ip");
102 int64_t port = proxiedServer.getInt(
"port");
103 uint64_t timeout = proxiedServer.getUnsignedInt(
"timeout");
104 bool isHTTPS = proxiedServer.getBool(
"isHTTPS");
105 vector<string> serverRoutes = json::utility::JSONArrayWrapper(proxiedServer.getArray(
"routes")).getAsStringArray();
107 const ProxyData& data = proxyData.emplace_back(ip, to_string(port),
static_cast<DWORD
>(timeout), isHTTPS);
109 for (
const string& route : serverRoutes)
111 routes[route] = &data;