Add support for redis with password, introduced new arguments with -

This commit is contained in:
2025-07-29 23:01:33 +02:00
parent c10e0a17cf
commit 22862a2795
4 changed files with 76 additions and 33 deletions

View File

@@ -13,8 +13,7 @@ COPY ./ /less_shitty_proxyjs
RUN mkdir -pv /build \ RUN mkdir -pv /build \
&& cd /build \ && cd /build \
&& qmake6 /less_shitty_proxyjs \ && qmake6 /less_shitty_proxyjs \
&& make -j4 \ && make -j4
&& ls -lah
FROM registry.brunner.ninja/library/ubuntu:devel AS runtime FROM registry.brunner.ninja/library/ubuntu:devel AS runtime
@@ -24,6 +23,6 @@ RUN apt update \
COPY --from=build /build/less_shitty_proxyjs /less_shitty_proxyjs COPY --from=build /build/less_shitty_proxyjs /less_shitty_proxyjs
ENTRYPOINT [ "/less_shitty_proxyjs", "Any", "1234" ] ENTRYPOINT [ "/less_shitty_proxyjs", "-l", "Any", "-p", "1234" ]
EXPOSE 1234 EXPOSE 1234

View File

@@ -2,6 +2,10 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include "webserver.h" #include "webserver.h"
#ifdef FEATURE_REDIS
#include <hiredis/async.h>
#include "redisqtadapter.h"
#endif
#include <QTcpServer> #include <QTcpServer>
#include <QDebug> #include <QDebug>
@@ -26,21 +30,39 @@ int main(int argc, char *argv[])
QHostAddress listenAddress = QHostAddress::Any; QHostAddress listenAddress = QHostAddress::Any;
uint16_t port = 8000; uint16_t port = 8000;
QString identity = "test"; QString identity = "test";
QString redisPassword;
{ {
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription("Test helper"); parser.setApplicationDescription("Test helper");
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
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).")); QCommandLineOption listenAddrOption{QStringList{ "l", "listenAddress" },
parser.addPositionalArgument("identiy", QCoreApplication::translate("main", "The name of this service instance")); QCoreApplication::translate("main", "The address to listen on (Can be \"Any\" \"Localhost\" or any valid ip address)."),
QCoreApplication::translate("main", "listenAddress")};
parser.addOption(listenAddrOption);
QCommandLineOption portOption{QStringList{ "p", "port" },
QCoreApplication::translate("main", "The port to listen on (will only listen on localhost)."),
QCoreApplication::translate("main", "port")};
parser.addOption(portOption);
QCommandLineOption identityOption{QStringList{ "i", "identity" },
QCoreApplication::translate("main", "The name of this service instance"),
QCoreApplication::translate("main", "identity")};
parser.addOption(identityOption);
QCommandLineOption redisPasswordOption{QStringList{ "r", "redisPassword" },
QCoreApplication::translate("main", "The redis password"),
QCoreApplication::translate("main", "redisPassword")};
parser.addOption(redisPasswordOption);
parser.process(app); parser.process(app);
const auto &args = parser.positionalArguments(); if (parser.isSet(listenAddrOption))
if (args.size() > 0)
{ {
if (const auto &str = args.first(); str == "Any") if (const auto &str = parser.value(listenAddrOption); str == "Any")
listenAddress = QHostAddress::Any; listenAddress = QHostAddress::Any;
else if (str == "AnyIPv4") else if (str == "AnyIPv4")
listenAddress = QHostAddress::AnyIPv4; listenAddress = QHostAddress::AnyIPv4;
@@ -53,18 +75,21 @@ int main(int argc, char *argv[])
else else
listenAddress = QHostAddress{str}; listenAddress = QHostAddress{str};
} }
if (args.size() > 1) if (parser.isSet(portOption))
{ {
const auto &str = parser.value(portOption);
bool ok; bool ok;
port = args.at(1).toInt(&ok); port = str.toInt(&ok);
if (!ok) if (!ok)
{ {
qCritical("could not parse port: %s", qPrintable(args.at(1))); qCritical("could not parse port: %s", qPrintable(str));
return -1; return -1;
} }
} }
if (args.size() > 2) if (parser.isSet(identityOption))
identity = args.at(2); identity = parser.value(identityOption);
if (parser.isSet(redisPasswordOption))
redisPassword = parser.value(redisPasswordOption);
} }
qSetMessagePattern(QString{"%{time dd.MM.yyyy HH:mm:ss.zzz} %0 " qSetMessagePattern(QString{"%{time dd.MM.yyyy HH:mm:ss.zzz} %0 "
@@ -78,7 +103,32 @@ int main(int argc, char *argv[])
"%{function}(): " "%{function}(): "
"%{message}"}.arg(identity)); "%{message}"}.arg(identity));
WebServer server{identity, QString{"http://localhost:%0"}.arg(port)}; #ifdef FEATURE_REDIS
redisAsyncContext * const redis = redisAsyncConnect("localhost", 6379);
if (redis->err)
qFatal("error with redis connection: %s", redis->errstr);
RedisQtAdapter redisAdapter;
redisAdapter.setContext(redis);
qDebug() << "redis AUTH" << redisPassword;
redisAsyncCommand(redis, NULL, NULL, "AUTH %s",
redisPassword.toUtf8().constData());
redisAsyncCommand(redis, NULL, NULL, "SET %s %s",
QString{"traefik/http/services/proxyjs_%0/loadbalancer/servers/0/url"}.arg(identity).toUtf8().constData(),
QString{"http://localhost:%0"}.arg(port).toUtf8().constData());
qDebug() << "redis connected";
#endif
WebServer server{identity
#ifdef FEATURE_REDIS
, redis
#endif
};
QTcpServer tcpServer; QTcpServer tcpServer;
if (!tcpServer.listen(listenAddress, port)) if (!tcpServer.listen(listenAddress, port))

View File

@@ -10,25 +10,17 @@ namespace {
QHttpServerResponse serveHtmlWithPlaceholders(const QString &filePath, const QMap<QString, QString> &placeholders); QHttpServerResponse serveHtmlWithPlaceholders(const QString &filePath, const QMap<QString, QString> &placeholders);
} // namespace } // namespace
WebServer::WebServer(const QString &identity, const QString &url, QObject *parent) : WebServer::WebServer(const QString &identity,
#ifdef FEATURE_REDIS
redisAsyncContext *redis,
#endif
QObject *parent) :
QObject{parent}, QObject{parent},
m_identity{identity} m_identity{identity}
{
#ifdef FEATURE_REDIS #ifdef FEATURE_REDIS
m_redis = redisAsyncConnect("localhost", 6379); , m_redis{redis}
if (m_redis->err)
qFatal("error with redis: %s", m_redis->errstr);
m_redisAdapter.setContext(m_redis);
redisAsyncCommand(m_redis, NULL, NULL, "SET %s %s",
QString{"traefik/http/services/proxyjs_%0/loadbalancer/servers/0/url"}.arg(m_identity).toUtf8().constData(),
url.toUtf8().constData());
qDebug() << "redis connected";
#endif #endif
{
m_server.route("/", [&]() { m_server.route("/", [&]() {
return serveHtmlWithPlaceholders(":/lspjs/index.html", { return serveHtmlWithPlaceholders(":/lspjs/index.html", {
{"identity", identity} {"identity", identity}

View File

@@ -9,7 +9,6 @@
#ifdef FEATURE_REDIS #ifdef FEATURE_REDIS
#include <hiredis/async.h> #include <hiredis/async.h>
#include "redisqtadapter.h"
#endif #endif
class QWebSocket; class QWebSocket;
@@ -21,7 +20,11 @@ class WebServer : public QObject
friend class Client; friend class Client;
public: public:
explicit WebServer(const QString &identity, const QString &url, QObject *parent = nullptr); explicit WebServer(const QString &identity,
#ifdef FEATURE_REDIS
redisAsyncContext *redis,
#endif
QObject *parent = nullptr);
~WebServer() override; ~WebServer() override;
bool bind(QTcpServer *server); bool bind(QTcpServer *server);
@@ -46,6 +49,5 @@ private:
#ifdef FEATURE_REDIS #ifdef FEATURE_REDIS
redisAsyncContext *m_redis; redisAsyncContext *m_redis;
RedisQtAdapter m_redisAdapter;
#endif #endif
}; };