From c10e0a17cf5e4da2076ce11dd81db6e5285059d2 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 29 Jul 2025 22:02:07 +0200 Subject: [PATCH] Add Dockerfile, make redis feature optional, add listening address cmdline parameter --- .dockerignore | 5 +++++ Dockerfile | 29 +++++++++++++++++++++++++++++ client.cpp | 4 ++++ less_shitty_proxyjs.pro | 24 +++++++++++++----------- main.cpp | 29 +++++++++++++++++++++++------ webserver.cpp | 10 ++++++++++ webserver.h | 6 ++++++ 7 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cb52423 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +build/ +*.user* +Dockerfile +.git/ +.gitignore diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6152d55 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM registry.brunner.ninja/library/ubuntu:devel AS build + +RUN apt update \ + && DEBIAN_FRONTEND=noninteractive apt install libqt6core6 libqt6httpserver6 libqt6websockets6 -y \ + && rm /var/lib/apt/lists/* /var/log/* -Rf + +RUN apt update \ + && DEBIAN_FRONTEND=noninteractive apt install qmake6 g++ make qt6-base-dev qt6-websockets-dev qt6-httpserver-dev -y \ + && rm /var/lib/apt/lists/* /var/log/* -Rf + +COPY ./ /less_shitty_proxyjs + +RUN mkdir -pv /build \ + && cd /build \ + && qmake6 /less_shitty_proxyjs \ + && make -j4 \ + && ls -lah + +FROM registry.brunner.ninja/library/ubuntu:devel AS runtime + +RUN apt update \ + && DEBIAN_FRONTEND=noninteractive apt install libqt6core6 libqt6httpserver6 libqt6websockets6 -y \ + && rm /var/lib/apt/lists/* /var/log/* -Rf + +COPY --from=build /build/less_shitty_proxyjs /less_shitty_proxyjs + +ENTRYPOINT [ "/less_shitty_proxyjs", "Any", "1234" ] + +EXPOSE 1234 diff --git a/client.cpp b/client.cpp index 5ef64bb..e1516af 100644 --- a/client.cpp +++ b/client.cpp @@ -81,7 +81,11 @@ void Client::socketDisconnected() } if (serialClients.empty()) + { +#ifdef FEATURE_REDIS m_server.destroyTraefikRoute(m_serial); +#endif + } else for (auto &ptr : serialClients) ptr->sendTextMessage(QString{"A client disconnected, number of remaining connected clients: %0"}.arg(serialClients.size())); diff --git a/less_shitty_proxyjs.pro b/less_shitty_proxyjs.pro index 35c5471..7ce9e80 100644 --- a/less_shitty_proxyjs.pro +++ b/less_shitty_proxyjs.pro @@ -4,11 +4,14 @@ CONFIG += c++latest cmdline DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 +HEADERS += \ + client.h \ + webserver.h + SOURCES += \ - client.cpp \ - main.cpp \ - redisqtadapter.cpp \ - webserver.cpp + client.cpp \ + main.cpp \ + webserver.cpp RESOURCES += \ resources.qrc @@ -19,10 +22,9 @@ OTHER_FILES += \ script.js \ style.css -HEADERS += \ - client.h \ - redisqtadapter.h \ - webserver.h - -LIBS += \ - -L/usr/lib -lhiredis +redis { + HEADERS += redisqtadapter.h + SOURCES += redisqtadapter.cpp + LIBS += -L/usr/lib -lhiredis + DEFINES += FEATURE_REDIS +} diff --git a/main.cpp b/main.cpp index 907eab1..5ce90f2 100644 --- a/main.cpp +++ b/main.cpp @@ -23,21 +23,36 @@ int main(int argc, char *argv[]) QCoreApplication::setApplicationName("less_shitty_proxyjs"); QCoreApplication::setApplicationVersion("1.0"); - QString identity = "test"; + QHostAddress listenAddress = QHostAddress::Any; uint16_t port = 8000; + QString identity = "test"; { QCommandLineParser parser; parser.setApplicationDescription("Test helper"); parser.addHelpOption(); parser.addVersionOption(); - parser.addPositionalArgument("identiy", QCoreApplication::translate("main", "The name of this service instance")); + parser.addPositionalArgument("listenAddr", QCoreApplication::translate("main", "The address to listen on (Can be \"Any\" \"Localhost\" or any valid ip address).")); parser.addPositionalArgument("port", QCoreApplication::translate("main", "The port to listen on (will only listen on localhost).")); + parser.addPositionalArgument("identiy", QCoreApplication::translate("main", "The name of this service instance")); parser.process(app); const auto &args = parser.positionalArguments(); if (args.size() > 0) - identity = args.first(); + { + if (const auto &str = args.first(); str == "Any") + listenAddress = QHostAddress::Any; + else if (str == "AnyIPv4") + listenAddress = QHostAddress::AnyIPv4; + else if (str == "AnyIPv6") + listenAddress = QHostAddress::AnyIPv6; + else if (str == "LocalHost") + listenAddress = QHostAddress::LocalHost; + else if (str == "LocalHostIPv6") + listenAddress = QHostAddress::LocalHostIPv6; + else + listenAddress = QHostAddress{str}; + } if (args.size() > 1) { bool ok; @@ -48,6 +63,8 @@ int main(int argc, char *argv[]) return -1; } } + if (args.size() > 2) + identity = args.at(2); } qSetMessagePattern(QString{"%{time dd.MM.yyyy HH:mm:ss.zzz} %0 " @@ -64,9 +81,9 @@ int main(int argc, char *argv[]) WebServer server{identity, QString{"http://localhost:%0"}.arg(port)}; QTcpServer tcpServer; - if (!tcpServer.listen(QHostAddress::LocalHost, port)) + if (!tcpServer.listen(listenAddress, port)) { - qCritical("failed to start listening on port %hu: %s", port, qPrintable(tcpServer.errorString())); + qCritical("failed to start listening on addr %s port %hu: %s", qPrintable(listenAddress.toString()), port, qPrintable(tcpServer.errorString())); return -1; } @@ -76,7 +93,7 @@ int main(int argc, char *argv[]) return -2; } - qDebug() << "server started"; + qDebug() << "server started on" << listenAddress << "port" << port; auto result = app.exec(); qDebug() << "bey bey!"; diff --git a/webserver.cpp b/webserver.cpp index cbfecad..11c9a35 100644 --- a/webserver.cpp +++ b/webserver.cpp @@ -14,6 +14,7 @@ WebServer::WebServer(const QString &identity, const QString &url, QObject *paren QObject{parent}, m_identity{identity} { +#ifdef FEATURE_REDIS m_redis = redisAsyncConnect("localhost", 6379); if (m_redis->err) @@ -25,6 +26,9 @@ WebServer::WebServer(const QString &identity, const QString &url, QObject *paren QString{"traefik/http/services/proxyjs_%0/loadbalancer/servers/0/url"}.arg(m_identity).toUtf8().constData(), url.toUtf8().constData()); + qDebug() << "redis connected"; +#endif + m_server.route("/", [&]() { return serveHtmlWithPlaceholders(":/lspjs/index.html", { {"identity", identity} @@ -65,6 +69,7 @@ void WebServer::newWebSocketConnection() newWebSocketConnection(std::move(socket)); } +#ifdef FEATURE_REDIS void WebServer::createTraefikRoute(const QString &serial) { qDebug() << "create traefik route for" << serial << "to" << m_identity; @@ -109,6 +114,7 @@ void WebServer::destroyTraefikRoute(const QString &serial) redisAsyncCommand(m_redis, NULL, NULL, "DEL %s", QString{"traefik/http/routers/proxyjs_%0/priority"}.arg(serial).toUtf8().constData()); } +#endif QHttpServerWebSocketUpgradeResponse WebServer::verifySocketUpgrade(const QHttpServerRequest &request) { @@ -131,7 +137,11 @@ void WebServer::newWebSocketConnection(std::unique_ptr &&socket) auto &set = m_clients[serial]; if (set.empty()) + { +#ifdef FEATURE_REDIS createTraefikRoute(serial); +#endif + } set.emplace(std::make_unique(*this, std::move(socket), serial, set)); } diff --git a/webserver.h b/webserver.h index f264eec..b71ec9e 100644 --- a/webserver.h +++ b/webserver.h @@ -7,8 +7,10 @@ #include #include +#ifdef FEATURE_REDIS #include #include "redisqtadapter.h" +#endif class QWebSocket; class Client; @@ -28,8 +30,10 @@ private slots: void newWebSocketConnection(); private: +#ifdef FEATURE_REDIS void createTraefikRoute(const QString &serial); void destroyTraefikRoute(const QString &serial); +#endif QHttpServerWebSocketUpgradeResponse verifySocketUpgrade(const QHttpServerRequest &request); void newWebSocketConnection(std::unique_ptr &&socket); @@ -40,6 +44,8 @@ private: std::unordered_map>> m_clients; +#ifdef FEATURE_REDIS redisAsyncContext *m_redis; RedisQtAdapter m_redisAdapter; +#endif };