This commit is contained in:
Martin Rotter 2022-12-04 10:03:22 +01:00
parent 3169d753fb
commit a3eeb4419e
6 changed files with 47 additions and 7 deletions

View File

@ -118,6 +118,9 @@
#define CLI_SIN_SHORT "s" #define CLI_SIN_SHORT "s"
#define CLI_SIN_LONG "no-single-instance" #define CLI_SIN_LONG "no-single-instance"
#define CLI_USERAGENT_SHORT "u"
#define CLI_USERAGENT_LONG "user-agent"
#define CLI_ADBLOCKPORT_SHORT "p" #define CLI_ADBLOCKPORT_SHORT "p"
#define CLI_ADBLOCKPORT_LONG "adblock-port" #define CLI_ADBLOCKPORT_LONG "adblock-port"

View File

@ -63,7 +63,10 @@
Application::Application(const QString& id, int& argc, char** argv, const QStringList& raw_cli_args) Application::Application(const QString& id, int& argc, char** argv, const QStringList& raw_cli_args)
: SingleApplication(id, argc, argv), m_rawCliArgs(raw_cli_args), m_updateFeedsLock(new Mutex()) { : SingleApplication(id, argc, argv), m_rawCliArgs(raw_cli_args), m_updateFeedsLock(new Mutex()) {
parseCmdArgumentsFromMyInstance(raw_cli_args); QString custom_ua;
parseCmdArgumentsFromMyInstance(raw_cli_args, custom_ua);
qInstallMessageHandler(performLogging); qInstallMessageHandler(performLogging);
m_feedReader = nullptr; m_feedReader = nullptr;
@ -156,6 +159,8 @@ Application::Application(const QString& id, int& argc, char** argv, const QStrin
} }
#endif #endif
m_webFactory->setCustomUserAgent(custom_ua);
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
m_webFactory->urlIinterceptor()->load(); m_webFactory->urlIinterceptor()->load();
@ -164,7 +169,13 @@ Application::Application(const QString& id, int& argc, char** argv, const QStrin
m_webFactory->engineProfile()->setCachePath(web_data_root + QDir::separator() + QSL("cache")); m_webFactory->engineProfile()->setCachePath(web_data_root + QDir::separator() + QSL("cache"));
m_webFactory->engineProfile()->setHttpCacheType(QWebEngineProfile::HttpCacheType::DiskHttpCache); m_webFactory->engineProfile()->setHttpCacheType(QWebEngineProfile::HttpCacheType::DiskHttpCache);
m_webFactory->engineProfile()->setPersistentStoragePath(web_data_root + QDir::separator() + QSL("storage")); m_webFactory->engineProfile()->setPersistentStoragePath(web_data_root + QDir::separator() + QSL("storage"));
m_webFactory->engineProfile()->setHttpUserAgent(QString(HTTP_COMPLETE_USERAGENT));
if (custom_ua.isEmpty()) {
m_webFactory->engineProfile()->setHttpUserAgent(QString(HTTP_COMPLETE_USERAGENT));
}
else {
m_webFactory->engineProfile()->setHttpUserAgent(custom_ua);
}
qDebugNN << LOGSEC_NETWORK << "Persistent web data storage path:" qDebugNN << LOGSEC_NETWORK << "Persistent web data storage path:"
<< QUOTE_W_SPACE_DOT(m_webFactory->engineProfile()->persistentStoragePath()); << QUOTE_W_SPACE_DOT(m_webFactory->engineProfile()->persistentStoragePath());
@ -1004,7 +1015,7 @@ void Application::parseCmdArgumentsFromOtherInstance(const QString& message) {
} }
} }
void Application::parseCmdArgumentsFromMyInstance(const QStringList& raw_cli_args) { void Application::parseCmdArgumentsFromMyInstance(const QStringList& raw_cli_args, QString& custom_ua) {
fillCmdArgumentsParser(m_cmdParser); fillCmdArgumentsParser(m_cmdParser);
m_cmdParser.setApplicationDescription(QSL(APP_NAME)); m_cmdParser.setApplicationDescription(QSL(APP_NAME));
@ -1073,6 +1084,8 @@ void Application::parseCmdArgumentsFromMyInstance(const QStringList& raw_cli_arg
else { else {
m_customAdblockPort = 0; m_customAdblockPort = 0;
} }
custom_ua = m_cmdParser.value(QSL(CLI_USERAGENT_SHORT));
} }
void Application::displayLog() { void Application::displayLog() {
@ -1115,6 +1128,10 @@ void Application::fillCmdArgumentsParser(QCommandLineParser& parser) {
QCommandLineOption forced_style({QSL(CLI_STYLE_SHORT), QSL(CLI_STYLE_LONG)}, QCommandLineOption forced_style({QSL(CLI_STYLE_SHORT), QSL(CLI_STYLE_LONG)},
QSL("Force some application style."), QSL("Force some application style."),
QSL("style-name")); QSL("style-name"));
QCommandLineOption custom_ua({QSL(CLI_USERAGENT_SHORT), QSL(CLI_USERAGENT_LONG)},
QSL("User custom User-Agent HTTP header for all network requests."),
QSL("user-agent"));
QCommandLineOption QCommandLineOption
adblock_port({QSL(CLI_ADBLOCKPORT_SHORT), QSL(CLI_ADBLOCKPORT_LONG)}, adblock_port({QSL(CLI_ADBLOCKPORT_SHORT), QSL(CLI_ADBLOCKPORT_LONG)},
QSL("Use custom port for AdBlock server. It is highly recommended to use values higher than 1024."), QSL("Use custom port for AdBlock server. It is highly recommended to use values higher than 1024."),
@ -1125,7 +1142,7 @@ void Application::fillCmdArgumentsParser(QCommandLineParser& parser) {
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
force_nowebengine, force_nowebengine,
#endif #endif
forced_style, adblock_port forced_style, adblock_port, custom_ua
}); });
parser.addPositionalArgument(QSL("urls"), parser.addPositionalArgument(QSL("urls"),
QSL("List of URL addresses pointing to individual online feeds which should be added."), QSL("List of URL addresses pointing to individual online feeds which should be added."),

View File

@ -187,7 +187,7 @@ class RSSGUARD_DLLSPEC Application : public SingleApplication {
// Processes incoming message from another RSS Guard instance. // Processes incoming message from another RSS Guard instance.
void parseCmdArgumentsFromOtherInstance(const QString& message); void parseCmdArgumentsFromOtherInstance(const QString& message);
void parseCmdArgumentsFromMyInstance(const QStringList& raw_cli_args); void parseCmdArgumentsFromMyInstance(const QStringList& raw_cli_args, QString &custom_ua);
void displayLog(); void displayLog();

View File

@ -62,7 +62,15 @@ QNetworkReply* BaseNetworkAccessManager::createRequest(QNetworkAccessManager::Op
#endif #endif
new_request.setRawHeader(HTTP_HEADERS_COOKIE, QSL("JSESSIONID= ").toLocal8Bit()); new_request.setRawHeader(HTTP_HEADERS_COOKIE, QSL("JSESSIONID= ").toLocal8Bit());
new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, HTTP_COMPLETE_USERAGENT);
auto custom_ua = qApp->web()->customUserAgent();
if (custom_ua.isEmpty()) {
new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, HTTP_COMPLETE_USERAGENT);
}
else {
new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, custom_ua.toLocal8Bit());
}
auto reply = QNetworkAccessManager::createRequest(op, new_request, outgoingData); auto reply = QNetworkAccessManager::createRequest(op, new_request, outgoingData);
return reply; return reply;

View File

@ -28,7 +28,7 @@
#include <QWebEngineUrlScheme> #include <QWebEngineUrlScheme>
#endif #endif
WebFactory::WebFactory(QObject* parent) : QObject(parent) { WebFactory::WebFactory(QObject* parent) : QObject(parent), m_customUserAgent(QString()) {
m_adBlock = new AdBlockManager(this); m_adBlock = new AdBlockManager(this);
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
@ -657,3 +657,11 @@ void WebFactory::generateUnescapes() {
m_htmlNamedEntities[QSL("zwj")] = 0x200d; m_htmlNamedEntities[QSL("zwj")] = 0x200d;
m_htmlNamedEntities[QSL("zwnj")] = 0x200c; m_htmlNamedEntities[QSL("zwnj")] = 0x200c;
} }
QString WebFactory::customUserAgent() const {
return m_customUserAgent;
}
void WebFactory::setCustomUserAgent(const QString& user_agent) {
m_customUserAgent = user_agent;
}

View File

@ -56,6 +56,9 @@ class WebFactory : public QObject {
bool sendMessageViaEmail(const Message& message); bool sendMessageViaEmail(const Message& message);
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
QString customUserAgent() const;
void setCustomUserAgent(const QString& user_agent);
private slots: private slots:
void createMenu(QMenu* menu = nullptr); void createMenu(QMenu* menu = nullptr);
void webEngineSettingChanged(bool enabled); void webEngineSettingChanged(bool enabled);
@ -79,6 +82,7 @@ class WebFactory : public QObject {
CookieJar* m_cookieJar; CookieJar* m_cookieJar;
Readability* m_readability; Readability* m_readability;
QMap<QString, char16_t> m_htmlNamedEntities; QMap<QString, char16_t> m_htmlNamedEntities;
QString m_customUserAgent;
}; };
#endif // WEBFACTORY_H #endif // WEBFACTORY_H