Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/http/socks_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>

#include <string>
#include <vector>

namespace Socks
{
namespace Network
Expand Down Expand Up @@ -79,14 +82,15 @@ class TcpServerHandlerFactory final : public ServerHandlerFactory
WsHandlerFactory& wsHandlerFactory;
};

SOCKS_INLINE void Server::serve(Context& context, HttpHandlerFactory& httpHandlerFactory, WsHandlerFactory& wsHandlerFactory,
SOCKS_INLINE void Server::serve(std::vector<std::string>& clientTypes, Context& context,
HttpHandlerFactory& httpHandlerFactory, WsHandlerFactory& wsHandlerFactory,
ServerOptions const& options)
{
TcpServerHandlerFactory tcpHandlerFactory(httpHandlerFactory, wsHandlerFactory);
Socks::Network::Tcp::ServerOptions tcpOptions(options.port, options.maxClients);
Socks::Network::Tcp::Server server;

server.serve(context, tcpHandlerFactory, tcpOptions);
server.serve(clientTypes, context, tcpHandlerFactory, tcpOptions);
}
} // namespace Http
} // namespace Network
Expand Down
26 changes: 16 additions & 10 deletions src/http/socks_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <socks_ws_handler.hpp>

#include <cstddef>
#include <string>
#include <vector>


namespace Socks
{
Expand All @@ -24,26 +27,29 @@ struct ServerOptions
class Server final
{
public:
static void serve(Socks::Network::Tcp::Context& context, HttpHandlerFactory& httpHandlerFactory,
WsHandlerFactory& wsHandlerFactory, ServerOptions const& options = ServerOptions());
static inline void serve(Socks::Network::Tcp::Context& context, WsHandlerFactory& wsHandler, std::string const& basePath,
static void serve(std::vector<std::string>& clientTypes, Socks::Network::Tcp::Context& context,
HttpHandlerFactory& httpHandlerFactory, WsHandlerFactory& wsHandlerFactory,
ServerOptions const& options = ServerOptions());

static inline void serve(std::vector<std::string>& clientTypes, Socks::Network::Tcp::Context& context,
WsHandlerFactory& wsHandler, std::string const& basePath,
ServerOptions const& options = ServerOptions())
{
HttpFileHandlerFactory fileHandlerFactory(basePath);
serve(context, fileHandlerFactory, wsHandler, options);
serve(clientTypes, context, fileHandlerFactory, wsHandler, options);
}
static inline void serve(Socks::Network::Tcp::Context& context, HttpHandlerFactory& httpHandlerFactory,
ServerOptions const& options = ServerOptions())
static inline void serve(std::vector<std::string>& clientTypes, Socks::Network::Tcp::Context& context,
HttpHandlerFactory& httpHandlerFactory, ServerOptions const& options = ServerOptions())
{
WsHandlerNullFactory wsHandlerFactory;
serve(context, httpHandlerFactory, wsHandlerFactory, options);
serve(clientTypes, context, httpHandlerFactory, wsHandlerFactory, options);
}
static inline void serve(Socks::Network::Tcp::Context& context, std::string const& basePath,
ServerOptions const& options = ServerOptions())
static inline void serve(std::vector<std::string>& clientTypes, Socks::Network::Tcp::Context& context,
std::string const& basePath, ServerOptions const& options = ServerOptions())
{
HttpFileHandlerFactory fileHandlerFactory(basePath);
WsHandlerNullFactory wsHandlerFactory;
serve(context, fileHandlerFactory, wsHandlerFactory, options);
serve(clientTypes, context, fileHandlerFactory, wsHandlerFactory, options);
}

private:
Expand Down
4 changes: 2 additions & 2 deletions src/http/socks_http_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <socks_http_handler.hpp>

#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>

#include <cstring>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -149,7 +149,7 @@ SOCKS_INLINE void HttpFileHandler::do_GET(HttpConnection* connection, RequestInf
std::fstream in(path, std::ios_base::in | std::ios_base::binary);
if (in.fail())
{
spdlog::error("Could not open file: {}", path);
std::cerr << "Could not open file: " << path << std::endl;
connection->http_404();
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/http/socks_http_states.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <array>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>

Expand All @@ -28,8 +29,8 @@ SOCKS_INLINE void HttpInitState::onReceive(Byte const* buf, std::size_t len)
recvBuf[recvBuf.size() - 1] = '\0';

requestInfo.parse(recvBuf.data(), numRecv);
spdlog::info("Request Type: {}, Path: {}", fsm->requestInfo().requestType() == RequestType::GET ? "GET" : "GET_WS",
fsm->requestInfo().path());
std::cout << "Request Type: " << ((fsm->requestInfo().requestType() == RequestType::GET) ? "GET" : "GET_WS")
<< ", Path: " << fsm->requestInfo().path() << std::endl;
if (fsm->requestInfo().requestType() == RequestType::GET)
{
fsm->setNextState(HttpStateInstance(new HttpGetState(fsm)));
Expand All @@ -43,7 +44,7 @@ SOCKS_INLINE void HttpInitState::onReceive(Byte const* buf, std::size_t len)
}
catch (const std::exception& exc)
{
spdlog::error("HTTP request cannot be parsed completely ({})", exc.what());
std::cerr << "HTTP request cannot be parsed completely (" << exc.what() << ")" << std::endl;
}

fsm->tcpConnection()->close();
Expand Down
2 changes: 2 additions & 0 deletions src/http/socks_ws_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ SOCKS_INLINE void WsConnection::close()
tcpConnection->send(message.data(), message.length());
tcpConnection->close();
}

SOCKS_INLINE void WsConnection::closeTcp() { tcpConnection->close(); }
} // namespace Http
} // namespace Network
} // namespace Socks
30 changes: 30 additions & 0 deletions src/http/socks_ws_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

#include <socks_tcp_handler.hpp>

#include <chrono>
#include <cstring>
#include <memory>
#include <mutex>
#include <string>


namespace Socks
{
namespace Network
Expand All @@ -21,6 +24,7 @@ class WsConnection final
std::size_t send(Byte const* buf, std::size_t len);
std::size_t send(char const* buf);
void close();
void closeTcp();

private:
Socks::Network::Tcp::Connection* tcpConnection;
Expand Down Expand Up @@ -63,6 +67,32 @@ class WsHandlerFactoryDefault : public WsHandlerFactory
}
};


template <class T, class L, class C>
class WsHandlerFactoryMultiClient : public WsHandlerFactory
{
public:
WsHandlerFactoryMultiClient() = default;

WsHandlerFactoryMultiClient(L& lock, C& content) : lock(lock), content(content){};

WsHandlerInstance createWsHandler(Socks::Network::Tcp::Connection* tcpConnection)
{
T* newConnection = new T(tcpConnection);

lock.lock();
content.newClient(newConnection);
lock.unlock();

return WsHandlerInstance(newConnection);
}

private:
L& lock;
C& content;
};


class WsHandlerNull final : public WsHandler
{
public:
Expand Down
10 changes: 8 additions & 2 deletions src/tcp/socks_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include <cstring>
#include <list>

#include <string>
#include <vector>

#include <spdlog/spdlog.h>

namespace Socks
{

Expand All @@ -18,14 +23,15 @@ using ClientWorkers = std::list<ClientWorker>;

static void cleanup(ClientWorkers& workers);

SOCKS_INLINE void Server::serve(Context& context, ServerHandlerFactory& handlerFactory, ServerOptions const& options)
SOCKS_INLINE void Server::serve(std::vector<std::string>& clientTypes, Context& context,
ServerHandlerFactory& handlerFactory, ServerOptions const& options)
{
ClientWorkers workers;

auto listenSocket = context.createListenSocket(options.serverPort);
while (!System::quitCondition())
{
auto clientSocket = listenSocket->accept();

if (!clientSocket.get())
{
continue;
Expand Down
6 changes: 5 additions & 1 deletion src/tcp/socks_tcp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <socks_tcp_handler.hpp>
#include <socks_tcp_options.hpp>

#include <string>
#include <vector>

namespace Socks
{

Expand All @@ -18,7 +21,8 @@ class Server final
{
public:
Server() = default;
void serve(Context& context, ServerHandlerFactory& handlerFactory, ServerOptions const& options = ServerOptions());
void serve(std::vector<std::string>& clientTypes, Context& context, ServerHandlerFactory& handlerFactory,
ServerOptions const& options = ServerOptions());

private:
Server(Server&) = delete;
Expand Down
6 changes: 4 additions & 2 deletions src/tcp/socks_tcp_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <socks_tcp_types.hpp>
#include <socks_tcp_worker.hpp>

#include <iostream>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>

#include <exception>

Expand All @@ -26,7 +26,9 @@ SOCKS_INLINE void ClientWorker::run()
}
catch (std::exception& exc)
{
spdlog::error("{}", exc.what());
handler->onDisconnect();
socket->close();
std::cerr << exc.what() << std::endl;
}
_isActive = false;
}
Expand Down