From 9f67bb5e5369aac476cd2f87f0dcb8dc904e0739 Mon Sep 17 00:00:00 2001 From: Felix Kauselmann Date: Fri, 7 Apr 2023 15:01:10 +0200 Subject: [PATCH 1/5] Server: Port IP detection and filtering to QNetworkInterface --- YACReader/YACReader.pro | 5 - YACReaderLibrary/YACReaderLibrary.pro | 5 - YACReaderLibrary/server_config_dialog.cpp | 98 ++----------------- .../YACReaderLibraryServer.pro | 6 +- 4 files changed, 11 insertions(+), 103 deletions(-) diff --git a/YACReader/YACReader.pro b/YACReader/YACReader.pro index 49684d37..64c639d8 100644 --- a/YACReader/YACReader.pro +++ b/YACReader/YACReader.pro @@ -12,11 +12,6 @@ DEFINES += YACREADER include (../config.pri) include (../dependencies/pdf_backend.pri) -unix:haiku { - DEFINES += _BSD_SOURCE - LIBS += -lnetwork -lbsd -} - CONFIG(force_angle) { contains(QMAKE_TARGET.arch, x86_64) { Release:DESTDIR = ../release64_angle diff --git a/YACReaderLibrary/YACReaderLibrary.pro b/YACReaderLibrary/YACReaderLibrary.pro index 70e558f8..b496b89e 100644 --- a/YACReaderLibrary/YACReaderLibrary.pro +++ b/YACReaderLibrary/YACReaderLibrary.pro @@ -18,11 +18,6 @@ DEFINES += SERVER_RELEASE YACREADER_LIBRARY include (../config.pri) include (../dependencies/pdf_backend.pri) -unix:haiku { - DEFINES += _BSD_SOURCE - LIBS += -lnetwork -lbsd -} - INCLUDEPATH += ../common/gl # there are two builds for Windows, Desktop OpenGL based and ANGLE OpenGL ES based diff --git a/YACReaderLibrary/server_config_dialog.cpp b/YACReaderLibrary/server_config_dialog.cpp index bab2be0b..e80c4510 100644 --- a/YACReaderLibrary/server_config_dialog.cpp +++ b/YACReaderLibrary/server_config_dialog.cpp @@ -1,25 +1,18 @@ -#include "server_config_dialog.h" -#include #include #include -#include -#include #include #include -#include -#include #include #include #include +#include "server_config_dialog.h" #include "yacreader_http_server.h" #include "yacreader_global_gui.h" #include "qnaturalsorting.h" #include "qrcodegen.hpp" -#include - // 192.168 (most comon local subnet for ips are always put first) // IPs are sorted using natoral sorting bool ipComparator(const QString &ip1, const QString &ip2) @@ -36,50 +29,6 @@ bool ipComparator(const QString &ip1, const QString &ip2) return naturalSortLessThanCI(ip1, ip2); } -#ifndef Q_OS_WIN32 - -#include -#include -#include -#include -#include -#include - -QList addresses() -{ - struct ifaddrs *ifAddrStruct = NULL; - struct ifaddrs *ifa = NULL; - void *tmpAddrPtr = NULL; - - QList localAddreses; - - getifaddrs(&ifAddrStruct); - - for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr) { - if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4 - // is a valid IP4 Address - tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; - char addressBuffer[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); - localAddreses.push_back(QString(addressBuffer)); - // printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); - } else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6 - // is a valid IP6 Address - tmpAddrPtr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; - char addressBuffer[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); - // printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); - } - } - } - if (ifAddrStruct != NULL) - freeifaddrs(ifAddrStruct); - return localAddreses; -} - -#endif - extern YACReaderHttpServer *httpServer; ServerConfigDialog::ServerConfigDialog(QWidget *parent) @@ -224,47 +173,20 @@ void ServerConfigDialog::enableperformanceWorkaround(int status) void ServerConfigDialog::generateQR() { ip->clear(); - QString dir; -#ifdef Q_OS_WIN32 - QList list = QHostInfo::fromName(QHostInfo::localHostName()).addresses(); - - QList otherAddresses; - foreach (QHostAddress add, list) { - QString tmp = add.toString(); - if (tmp.contains(".") && !tmp.startsWith("127")) { - otherAddresses.push_back(tmp); + QList addresses; + for (auto add : QNetworkInterface::allAddresses()) { + // Exclude loopback, local, multicast + if (add.isGlobal()) { + addresses.push_back(add.toString()); } } -#else - QList list = addresses(); + std::sort(addresses.begin(), addresses.end(), ipComparator); - QList otherAddresses; - foreach (QString add, list) { - QString tmp = add; - if (tmp.contains(".") && !tmp.startsWith("127")) { - otherAddresses.push_back(tmp); - } - } -#endif - - std::sort(otherAddresses.begin(), otherAddresses.end(), ipComparator); - - if (!otherAddresses.isEmpty()) { - dir = otherAddresses.first(); - otherAddresses.pop_front(); - } - - if (otherAddresses.length() > 0 || !dir.isEmpty()) { - if (!dir.isEmpty()) { - generateQR(dir + ":" + httpServer->getPort()); - - ip->addItem(dir); - } else { - generateQR(otherAddresses.first() + ":" + httpServer->getPort()); - } - ip->addItems(otherAddresses); + if (addresses.length() > 0) { + generateQR(addresses.first() + ":" + httpServer->getPort()); + ip->addItems(addresses); port->setText(httpServer->getPort()); } } diff --git a/YACReaderLibraryServer/YACReaderLibraryServer.pro b/YACReaderLibraryServer/YACReaderLibraryServer.pro index bebe2be7..b93a783d 100644 --- a/YACReaderLibraryServer/YACReaderLibraryServer.pro +++ b/YACReaderLibraryServer/YACReaderLibraryServer.pro @@ -15,6 +15,7 @@ DEFINES += SERVER_RELEASE YACREADER_LIBRARY # do a basic dependency check include(headless_config.pri) include(../dependencies/pdf_backend.pri) +include(../third_party/QrCode/QrCode.pri) greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat @@ -32,11 +33,6 @@ macx { CONFIG += objective_c } -unix:haiku { - DEFINES += _BSD_SOURCE - LIBS += -lnetwork -lbsd -} - #CONFIG += release CONFIG -= flat QT += core sql network From c100edfcd0b68d6ea1325f8d752892d41eb66024 Mon Sep 17 00:00:00 2001 From: Felix Kauselmann Date: Fri, 7 Apr 2023 20:19:11 +0200 Subject: [PATCH 2/5] Move network interface filtering to separate file --- YACReaderLibrary/YACReaderLibrary.pro | 6 +++-- YACReaderLibrary/ip_config_helper.cpp | 33 +++++++++++++++++++++++ YACReaderLibrary/ip_config_helper.h | 4 +++ YACReaderLibrary/server_config_dialog.cpp | 30 ++------------------- 4 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 YACReaderLibrary/ip_config_helper.cpp create mode 100644 YACReaderLibrary/ip_config_helper.h diff --git a/YACReaderLibrary/YACReaderLibrary.pro b/YACReaderLibrary/YACReaderLibrary.pro index b496b89e..d454d218 100644 --- a/YACReaderLibrary/YACReaderLibrary.pro +++ b/YACReaderLibrary/YACReaderLibrary.pro @@ -150,7 +150,8 @@ HEADERS += comic_flow.h \ yacreader_comic_info_helper.h \ db/reading_list.h \ db/query_parser.h \ - current_comic_view_helper.h + current_comic_view_helper.h \ + ip_config_helper.h !CONFIG(no_opengl) { HEADERS += ../common/gl/yacreader_flow_gl.h @@ -232,7 +233,8 @@ SOURCES += comic_flow.cpp \ yacreader_comic_info_helper.cpp\ db/reading_list.cpp \ current_comic_view_helper.cpp \ - db/query_parser.cpp + db/query_parser.cpp \ + ip_config_helper.cpp !CONFIG(no_opengl) { SOURCES += ../common/gl/yacreader_flow_gl.cpp diff --git a/YACReaderLibrary/ip_config_helper.cpp b/YACReaderLibrary/ip_config_helper.cpp new file mode 100644 index 00000000..38c58188 --- /dev/null +++ b/YACReaderLibrary/ip_config_helper.cpp @@ -0,0 +1,33 @@ +#include +#include "ip_config_helper.h" +#include "qnaturalsorting.h" + +// 192.168 (most comon local subnet for ips are always put first) +// IPs are sorted using natoral sorting + +QList getIpAddresses() +{ + auto ipComparator = [](const QString &ip1, const QString &ip2) { + if (ip1.startsWith("192.168") && ip2.startsWith("192.168")) + return naturalSortLessThanCI(ip1, ip2); + + if (ip1.startsWith("192.168")) + return true; + + if (ip2.startsWith("192.168")) + return false; + + return naturalSortLessThanCI(ip1, ip2); + }; + + QList addresses; + for (auto add : QNetworkInterface::allAddresses()) { + // Exclude loopback, local, multicast + if (add.isGlobal()) { + addresses.push_back(add.toString()); + } + } + + std::sort(addresses.begin(), addresses.end(), ipComparator); + return addresses; +} diff --git a/YACReaderLibrary/ip_config_helper.h b/YACReaderLibrary/ip_config_helper.h new file mode 100644 index 00000000..8cabe1d3 --- /dev/null +++ b/YACReaderLibrary/ip_config_helper.h @@ -0,0 +1,4 @@ +#ifndef YR_IP_CONFIG_HELPER +#define YR_IP_CONFIG_HELPER +QList getIpAddresses(); +#endif diff --git a/YACReaderLibrary/server_config_dialog.cpp b/YACReaderLibrary/server_config_dialog.cpp index e80c4510..c15902bc 100644 --- a/YACReaderLibrary/server_config_dialog.cpp +++ b/YACReaderLibrary/server_config_dialog.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -10,25 +9,9 @@ #include "yacreader_http_server.h" #include "yacreader_global_gui.h" -#include "qnaturalsorting.h" +#include "ip_config_helper.h" #include "qrcodegen.hpp" -// 192.168 (most comon local subnet for ips are always put first) -// IPs are sorted using natoral sorting -bool ipComparator(const QString &ip1, const QString &ip2) -{ - if (ip1.startsWith("192.168") && ip2.startsWith("192.168")) - return naturalSortLessThanCI(ip1, ip2); - - if (ip1.startsWith("192.168")) - return true; - - if (ip2.startsWith("192.168")) - return false; - - return naturalSortLessThanCI(ip1, ip2); -} - extern YACReaderHttpServer *httpServer; ServerConfigDialog::ServerConfigDialog(QWidget *parent) @@ -174,16 +157,7 @@ void ServerConfigDialog::generateQR() { ip->clear(); - QList addresses; - for (auto add : QNetworkInterface::allAddresses()) { - // Exclude loopback, local, multicast - if (add.isGlobal()) { - addresses.push_back(add.toString()); - } - } - - std::sort(addresses.begin(), addresses.end(), ipComparator); - + auto addresses = getIpAddresses(); if (addresses.length() > 0) { generateQR(addresses.first() + ":" + httpServer->getPort()); ip->addItems(addresses); From f8f25c396f0a6d5d3c6ebad89cd2c0b3852adfce Mon Sep 17 00:00:00 2001 From: Felix Kauselmann Date: Fri, 7 Apr 2023 20:21:33 +0200 Subject: [PATCH 3/5] YACReaderLibraryServer: Print QR code and basic server info on start --- .../YACReaderLibraryServer.pro | 2 + YACReaderLibraryServer/main.cpp | 41 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/YACReaderLibraryServer/YACReaderLibraryServer.pro b/YACReaderLibraryServer/YACReaderLibraryServer.pro index b93a783d..c48960d5 100644 --- a/YACReaderLibraryServer/YACReaderLibraryServer.pro +++ b/YACReaderLibraryServer/YACReaderLibraryServer.pro @@ -63,6 +63,7 @@ HEADERS += ../YACReaderLibrary/library_creator.h \ ../YACReaderLibrary/comic_files_manager.h \ console_ui_library_creator.h \ libraries_updater.h \ + ../YACReaderLibrary/ip_config_helper.h ../YACReaderLibrary/db/query_lexer.h \ ../YACReaderLibrary/db/query_parser.h \ ../YACReaderLibrary/db/search_query.h @@ -91,6 +92,7 @@ SOURCES += ../YACReaderLibrary/library_creator.cpp \ console_ui_library_creator.cpp \ main.cpp \ libraries_updater.cpp \ + ../YACReaderLibrary/ip_config_helper.cpp ../YACReaderLibrary/db/query_lexer.cpp \ ../YACReaderLibrary/db/query_parser.cpp \ ../YACReaderLibrary/db/search_query.cpp \ diff --git a/YACReaderLibraryServer/main.cpp b/YACReaderLibraryServer/main.cpp index bda5e416..24c0fa7f 100644 --- a/YACReaderLibraryServer/main.cpp +++ b/YACReaderLibraryServer/main.cpp @@ -1,4 +1,3 @@ -// #include #include #include #include @@ -20,6 +19,8 @@ #include "QsLog.h" #include "QsLogDest.h" +#include "qrcodegen.hpp" +#include "ip_config_helper.h" using namespace QsLogging; // Returns false in case of a parse error (unknown option or missing value); returns true otherwise. @@ -29,17 +30,6 @@ void logSystemAndConfig() QLOG_INFO() << "---------- System & configuration ----------"; QLOG_INFO() << "OS:" << QSysInfo::prettyProductName() << "Version: " << QSysInfo::productVersion(); QLOG_INFO() << "Kernel:" << QSysInfo::kernelType() << QSysInfo::kernelVersion() << "Architecture:" << QSysInfo::currentCpuArchitecture(); - /* TODO: qrencode could be helpfull for showing a qr code in the web client for client devices -#if defined Q_OS_UNIX && !defined Q_OS_MAC - if(QFileInfo(QString(BINDIR)+"/qrencode").exists()) -#else - if(QFileInfo(QCoreApplication::applicationDirPath()+"/utils/qrencode.exe").exists() || QFileInfo("./util/qrencode").exists()) -#endif - QLOG_INFO() << "qrencode : found"; - else - QLOG_INFO() << "qrencode : not found"; - */ - QLOG_INFO() << "Libraries: " << DBHelper::getLibraries().getLibraries(); QLOG_INFO() << "--------------------------------------------"; } @@ -76,6 +66,31 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt } } +void printServerInfo(YACReaderHttpServer *httpServer) +{ + auto addresses = getIpAddresses(); + QLOG_INFO() << "Running on" << addresses.first() + ":" + httpServer->getPort().toLocal8Bit() << "\n"; + + qrcodegen::QrCode code = qrcodegen::QrCode::encodeText( + (addresses.first() + ":" + httpServer->getPort()).toLocal8Bit(), + qrcodegen::QrCode::Ecc::LOW); + int border = 4; + for (int y = -border; y < code.getSize() + border; y += 2) { + QString QRCodeString; + for (int x = -border - 1; x < code.getSize() + border + 1; x++) { + QRCodeString.append((code.getModule(x, y) && code.getModule(x, y + 1)) + ? " " + : code.getModule(x, y + 1) ? "\u2580" + : code.getModule(x, y) ? "\u2584" + : "\u2588"); + } + QLOG_INFO() << QRCodeString; + } + if (addresses.length() > 1) { + QLOG_INFO() << addresses.length() - 1 << "more network interfaces detected"; + } +} + int main(int argc, char **argv) { qInstallMessageHandler(messageHandler); @@ -215,7 +230,7 @@ int main(int argc, char **argv) httpServer->start(); } - QLOG_INFO() << "Running on port" << httpServer->getPort(); + printServerInfo(httpServer); // Update libraries to new versions LibrariesUpdater updater; From c0cb792ecaa482e3fc51c698234dc8b679959804 Mon Sep 17 00:00:00 2001 From: Felix Kauselmann Date: Fri, 7 Apr 2023 20:34:07 +0200 Subject: [PATCH 4/5] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6f8055a..c9fd1bb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,11 @@ Version counting is based on semantic versioning (Major.Feature.Patch) ### YACReaderLibrary * Fix scroll in grid views when using Qt6 builds. * Fix deleting metadata from comics also deleted the number of pages info. +* Use https://github.com/nayuki/QR-Code-generator instead of libqrencode for QR code generation ### Server * New search API that exposes the search engine. +* Print scannable QR code at server start ## 9.11 From ea38f60c7c0543e755353623831907c4ade41c06 Mon Sep 17 00:00:00 2001 From: Felix Kauselmann Date: Fri, 7 Apr 2023 20:58:38 +0200 Subject: [PATCH 5/5] Fix build --- YACReaderLibraryServer/YACReaderLibraryServer.pro | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/YACReaderLibraryServer/YACReaderLibraryServer.pro b/YACReaderLibraryServer/YACReaderLibraryServer.pro index c48960d5..c88875df 100644 --- a/YACReaderLibraryServer/YACReaderLibraryServer.pro +++ b/YACReaderLibraryServer/YACReaderLibraryServer.pro @@ -63,7 +63,7 @@ HEADERS += ../YACReaderLibrary/library_creator.h \ ../YACReaderLibrary/comic_files_manager.h \ console_ui_library_creator.h \ libraries_updater.h \ - ../YACReaderLibrary/ip_config_helper.h + ../YACReaderLibrary/ip_config_helper.h \ ../YACReaderLibrary/db/query_lexer.h \ ../YACReaderLibrary/db/query_parser.h \ ../YACReaderLibrary/db/search_query.h @@ -92,7 +92,7 @@ SOURCES += ../YACReaderLibrary/library_creator.cpp \ console_ui_library_creator.cpp \ main.cpp \ libraries_updater.cpp \ - ../YACReaderLibrary/ip_config_helper.cpp + ../YACReaderLibrary/ip_config_helper.cpp \ ../YACReaderLibrary/db/query_lexer.cpp \ ../YACReaderLibrary/db/query_parser.cpp \ ../YACReaderLibrary/db/search_query.cpp \