From 450030901db1728f6ed8e2c007de1ec3e065bc97 Mon Sep 17 00:00:00 2001 From: JasonSun0593 Date: Wed, 2 Apr 2025 14:42:27 +0800 Subject: [PATCH 1/4] feat: MI0698-1385 add proxy cli para --- src/libs/installer/commandlineparser.cpp | 27 ++++++++++++++++++++++++ src/libs/installer/constants.h | 14 ++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/libs/installer/commandlineparser.cpp b/src/libs/installer/commandlineparser.cpp index f9e1f663e..ee6fa66a7 100644 --- a/src/libs/installer/commandlineparser.cpp +++ b/src/libs/installer/commandlineparser.cpp @@ -120,6 +120,33 @@ CommandLineParser::CommandLineParser() << CommandLineOptions::scNoProxyShort << CommandLineOptions::scNoProxyLong, QLatin1String("Do not use system proxy."))); + // Starting mode options + addOption(QCommandLineOption(QStringList() + << CommandLineOptions::scManualProxyShort << CommandLineOptions::scManualProxyLong, + QLatin1String("Use manual proxy."))); + + // Custom Proxy options + addOption(QCommandLineOption(QStringList() + << CommandLineOptions::scHttpProxyHostNameShort << CommandLineOptions::scHttpProxyHostNameLong, + QLatin1String("Use Http proxy."), + QLatin1String("ProxyName"))); + addOption(QCommandLineOption(QStringList() + << CommandLineOptions::scFtpProxyHostNameShort << CommandLineOptions::scFtpProxyHostNameLong, + QLatin1String("Use Ftp proxy."), + QLatin1String("ProxyName"))); + addOption(QCommandLineOption(QStringList() + << CommandLineOptions::scPortIdShort << CommandLineOptions::scPortIdLong, + QLatin1String("Port id."), + QLatin1String("PortId"))); + addOption(QCommandLineOption(QStringList() + << CommandLineOptions::scProxyUserNameShort << CommandLineOptions::scProxyUserNameLong, + QLatin1String("Proxy Username."), + QLatin1String("Username"))); + addOption(QCommandLineOption(QStringList() + << CommandLineOptions::scProxyPasswordShort << CommandLineOptions::scProxyPasswordLong, + QLatin1String("Proxy password."), + QLatin1String("Password"))); + // Starting mode options addOption(QCommandLineOption(QStringList() << CommandLineOptions::scStartUpdaterShort << CommandLineOptions::scStartUpdaterLong, diff --git a/src/libs/installer/constants.h b/src/libs/installer/constants.h index a8c2a5b7b..34e7b4294 100644 --- a/src/libs/installer/constants.h +++ b/src/libs/installer/constants.h @@ -235,6 +235,20 @@ static const QLatin1String scSystemProxyShort("sp"); static const QLatin1String scSystemProxyLong("system-proxy"); static const QLatin1String scNoProxyShort("np"); static const QLatin1String scNoProxyLong("no-proxy"); +static const QLatin1String scManualProxyShort("mp"); +static const QLatin1String scManualProxyLong("manual-proxy"); + +// Custom Proxy options +static const QLatin1String scHttpProxyHostNameShort("hph"); +static const QLatin1String scHttpProxyHostNameLong("http-proxy-host"); +static const QLatin1String scFtpProxyHostNameShort("fph"); +static const QLatin1String scFtpProxyHostNameLong("ftp-proxy-host"); +static const QLatin1String scPortIdShort("pi"); +static const QLatin1String scPortIdLong("port-id"); +static const QLatin1String scProxyUserNameShort("un"); +static const QLatin1String scProxyUserNameLong("user-name"); +static const QLatin1String scProxyPasswordShort("pp"); +static const QLatin1String scProxyPasswordLong("proxy-password"); // Starting mode options static const QLatin1String scStartUpdaterShort("su"); From 7c5b69ed12f4b1a32639e8fb7105c25c42f2330b Mon Sep 17 00:00:00 2001 From: JasonSun0593 Date: Wed, 2 Apr 2025 14:44:51 +0800 Subject: [PATCH 2/4] feat: MI0698-1385 add cli proxy para parsing --- src/sdk/main.cpp | 13 +++++++++- src/sdk/sdkapp.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/sdk/main.cpp b/src/sdk/main.cpp index abfc9dc5a..720da4686 100644 --- a/src/sdk/main.cpp +++ b/src/sdk/main.cpp @@ -164,6 +164,13 @@ int main(int argc, char *argv[]) << CommandLineOptions::scStartServerLong << CommandLineOptions::scStartClientLong); } + + if (mutually.isEmpty()) { + mutually = QInstaller::checkMutualOptions(parser, QStringList() + << CommandLineOptions::scHttpProxyHostNameLong + << CommandLineOptions::scFtpProxyHostNameLong); + } + if (!mutually.isEmpty()) { sanityMessage = QString::fromLatin1("The following options are mutually exclusive: %1.") .arg(mutually.join(QLatin1String(", "))); @@ -309,8 +316,12 @@ int main(int argc, char *argv[]) // Make sure we honor the system's proxy settings QNetworkProxyFactory::setUseSystemConfiguration(true); } + else //make sure the system config set to false when no proxy para passed otherwise it may interfere with settings read from network.xml + { + QNetworkProxyFactory::setUseSystemConfiguration(false); + } - if (parser.isSet(CommandLineOptions::scNoProxyLong)) + if ((parser.isSet(CommandLineOptions::scNoProxyLong)) || (parser.isSet(CommandLineOptions::scManualProxyLong))) QNetworkProxyFactory::setUseSystemConfiguration(false); const SelfRestarter restarter(argc, argv); diff --git a/src/sdk/sdkapp.h b/src/sdk/sdkapp.h index eef0110ec..0e6b1e119 100644 --- a/src/sdk/sdkapp.h +++ b/src/sdk/sdkapp.h @@ -272,6 +272,71 @@ class SDKApp : public T } else if (QNetworkProxyFactory::usesSystemConfiguration()) { m_core->settings().setProxyType(QInstaller::Settings::SystemProxy); KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory()); + } else if(m_parser.isSet(CommandLineOptions::scManualProxyLong)) { + m_core->settings().setProxyType(QInstaller::Settings::UserDefinedProxy); + // get proxy type, name, port + if ((m_parser.isSet(CommandLineOptions::scHttpProxyHostNameLong)) && (m_parser.isSet(CommandLineOptions::scPortIdLong))) { + const QString httpProxyName = m_parser.value(CommandLineOptions::scHttpProxyHostNameLong); + bool isValid; + const quint16 portId = m_parser.value(CommandLineOptions::scPortIdLong).toInt(&isValid); + if(httpProxyName.isEmpty()) + { + errorMessage = QObject::tr("Manual proxy name empty."); + return false; + } + if(isValid == false) + { + errorMessage = QObject::tr("Manual proxy portId empty."); + return false; + } + m_core->settings().setHttpProxy(QNetworkProxy(QNetworkProxy::HttpProxy, httpProxyName, portId)); + KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory()); + + // get username & password + if ((m_parser.isSet(CommandLineOptions::scProxyUserNameLong)) && (m_parser.isSet(CommandLineOptions::scProxyPasswordLong))) { + const QString userName = m_parser.value(CommandLineOptions::scProxyUserNameLong); + const QString password = m_parser.value(CommandLineOptions::scProxyPasswordLong); + QNetworkProxy setParaProxy = m_core->settings().httpProxy(); + setParaProxy.setUser(userName); + setParaProxy.setPassword(password); + m_core->settings().setHttpProxy(setParaProxy); //have to reset the QNetworkProxy, for the value stored in settings' m_data is a copy,it won't change + m_core->proxyFactory()->setProxyCredentials(m_core->settings().httpProxy(),userName,password); + } + //if username & password not specified, it will require it in AuthenticationRequiredException, so not return false here if username/password not set + + }else if((m_parser.isSet(CommandLineOptions::scFtpProxyHostNameLong))&& (m_parser.isSet(CommandLineOptions::scPortIdLong))){ + const QString ftpProxyName = m_parser.value(CommandLineOptions::scFtpProxyHostNameLong); + bool isValid; + const quint16 portId = m_parser.value(CommandLineOptions::scPortIdLong).toInt(&isValid); + if(ftpProxyName.isEmpty()) + { + errorMessage = QObject::tr("Manual proxy name empty."); + return false; + } + if(isValid == false) + { + errorMessage = QObject::tr("Manual proxy portId empty."); + return false; + } + m_core->settings().setFtpProxy(QNetworkProxy(QNetworkProxy::HttpProxy, ftpProxyName, portId)); + KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory()); + + // get username & password + if ((m_parser.isSet(CommandLineOptions::scProxyUserNameLong)) && (m_parser.isSet(CommandLineOptions::scProxyPasswordLong))) { + const QString userName = m_parser.value(CommandLineOptions::scProxyUserNameLong); + const QString password = m_parser.value(CommandLineOptions::scProxyPasswordLong); + QNetworkProxy setParaProxy = m_core->settings().ftpProxy(); + setParaProxy.setUser(userName); + setParaProxy.setPassword(password); + m_core->settings().setFtpProxy(setParaProxy); //have to reset the QNetworkProxy, for the value stored in settings' m_data is a copy,it won't change + m_core->proxyFactory()->setProxyCredentials(m_core->settings().ftpProxy(),userName,password); + } + + }else { + errorMessage = QObject::tr("Manual proxy name and portId need to be specifed."); + return false; + } + } if (m_parser.isSet(CommandLineOptions::scLocalCachePathLong)) { From a69c01e2fc84ac6686ec06be335d7b99e901151a Mon Sep 17 00:00:00 2001 From: JasonSun0593 Date: Wed, 2 Apr 2025 14:47:38 +0800 Subject: [PATCH 3/4] chore: MI0698-1385 save proxy setting to network.xml for maintainence --- src/sdk/tabcontroller.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/sdk/tabcontroller.cpp b/src/sdk/tabcontroller.cpp index dde91b8c5..3b4cafa31 100644 --- a/src/sdk/tabcontroller.cpp +++ b/src/sdk/tabcontroller.cpp @@ -86,6 +86,16 @@ TabController::TabController(QObject *parent) TabController::~TabController() { + //try writing config file before writeMaintenanceTool 250324 + bool gainedAdminRights = false; + if (!d->m_core->directoryWritable(d->m_core->value(scTargetDir))) { + d->m_core->gainAdminRights(); + gainedAdminRights = true; + } + d->m_core->writeMaintenanceConfigFiles(); + if (gainedAdminRights) + d->m_core->dropAdminRights(); + d->m_core->writeMaintenanceTool(); delete d; } From 6a42624cc8943b4a54f275cc232b6348a7c8a29e Mon Sep 17 00:00:00 2001 From: JasonSun0593 Date: Wed, 2 Apr 2025 14:49:37 +0800 Subject: [PATCH 4/4] feat: MI0698-1385 try add proxy get/set apis to QJSEngine ver 1.0 --- src/libs/installer/packagemanagercore.cpp | 188 ++++++++++++++++++++++ src/libs/installer/packagemanagercore.h | 21 +++ 2 files changed, 209 insertions(+) diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index 72831b0d2..1254a3af6 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -3851,6 +3851,194 @@ QString PackageManagerCore::value(const QString &key, const QString &defaultValu return d->m_data.value(key, defaultValue, static_cast(format)).toString(); } +QString PackageManagerCore::getProxyMode() const +{ + const Settings &settings = d->m_data.settings(); + switch (settings.proxyType()) { + case Settings::NoProxy: + return QStringLiteral("no"); + break; + case Settings::SystemProxy: + return QStringLiteral("system"); + break; + case Settings::UserDefinedProxy: + return QStringLiteral("manual"); + break; + } +} + +void PackageManagerCore::setProxyMode(const QString &proxyType) +{ + if(proxyType == QStringLiteral("no")) + { + d->m_data.settings().setProxyType(QInstaller::Settings::NoProxy); + } + else if(proxyType == QStringLiteral("system")) + { + d->m_data.settings().setProxyType(QInstaller::Settings::SystemProxy); + } + else if(proxyType == QStringLiteral("manual")) + { + d->m_data.settings().setProxyType(QInstaller::Settings::UserDefinedProxy); + } + else + { + qWarning() << "proxy mode does not exist"; + } + +} + +QString PackageManagerCore::getHttpProxyHost() const +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + qInfo() << "installer value: proxy hostname" << proxy.hostName(); + + return proxy.hostName(); +} + +void PackageManagerCore::setHttpProxyHost(const QString &hostName) +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + proxy.setHostName(hostName); + qInfo() << "set proxy hostname" << proxy.hostName(); +} + +QString PackageManagerCore::getHttpProxyPort() const +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + qInfo() << "installer value: proxy port" << proxy.port(); + + return QString::number(proxy.port()); +} + +void PackageManagerCore::setHttpProxyPort(const QString &port) +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + bool boolVal; + proxy.setPort(port.toInt(&boolVal)); + qInfo() << "set proxy port" << proxy.port(); +} + +QString PackageManagerCore::getHttpProxyUser() const +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + qInfo() << "installer value: proxy user" << proxy.user(); + + return proxy.user(); +} + +void PackageManagerCore::setHttpProxyUser(const QString &userName) +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + proxy.setUser(userName); + qInfo() << "set proxy port" << proxy.user(); +} + +QString PackageManagerCore::getHttpProxyPwd() const +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + qInfo() << "installer value: proxy password" << proxy.password(); + + return proxy.password(); +} + +void PackageManagerCore::setHttpProxyPwd(const QString &password) +{ + QNetworkProxy proxy = d->m_data.settings().httpProxy(); + proxy.setPassword(password); + qInfo() << "set proxy port" << proxy.password(); +} + +bool PackageManagerCore::getHttpProxyAuth() const +{ + QString userStr = getHttpProxyUser(); + QString pwdStr = getHttpProxyPwd(); + if(userStr.isEmpty() || pwdStr.isEmpty()) + { + return false; + } + else + { + return true; + } + +} + +QString PackageManagerCore::getFtpProxyHost() const +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + qInfo() << "installer value: proxy hostname" << proxy.hostName(); + + return proxy.hostName(); +} + +void PackageManagerCore::setFtpProxyHost(const QString &hostName) +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + proxy.setHostName(hostName); + qInfo() << "set proxy hostname" << proxy.hostName(); +} + +QString PackageManagerCore::getFtpProxyPort() const +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + qInfo() << "installer value: proxy port" << proxy.port(); + + return QString::number(proxy.port()); +} + +void PackageManagerCore::setFtpProxyPort(const QString &port) +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + bool boolVal; + proxy.setPort(port.toInt(&boolVal)); + qInfo() << "set proxy port" << proxy.port(); +} + +QString PackageManagerCore::getFtpProxyUser() const +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + qInfo() << "installer value: proxy user" << proxy.user(); + + return proxy.user(); +} + +void PackageManagerCore::setFtpProxyUser(const QString &userName) +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + proxy.setUser(userName); + qInfo() << "set proxy port" << proxy.user(); +} + +QString PackageManagerCore::getFtpProxyPwd() const +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + qInfo() << "installer value: proxy password" << proxy.password(); + + return proxy.password(); +} + +void PackageManagerCore::setFtpProxyPwd(const QString &password) +{ + QNetworkProxy proxy = d->m_data.settings().ftpProxy(); + proxy.setPassword(password); + qInfo() << "set proxy port" << proxy.password(); +} + +bool PackageManagerCore::getFtpProxyAuth() const +{ + QString userStr = getFtpProxyUser(); + QString pwdStr = getFtpProxyPwd(); + if(userStr.isEmpty() || pwdStr.isEmpty()) + { + return false; + } + else + { + return true; + } +} + /*! Returns the installer value for \a key. If \a key is not known to the system, \a defaultValue is returned. Additionally, on Windows, \a key can be a registry key. diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index f4ad57ae4..7d7de2a4e 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -190,6 +190,27 @@ class INSTALLER_EXPORT PackageManagerCore : public QObject Q_INVOKABLE bool containsValue(const QString &key) const; Q_INVOKABLE void setValue(const QString &key, const QString &value); Q_INVOKABLE QString value(const QString &key, const QString &defaultValue = QString(), const int &format = QSettings::NativeFormat) const; + Q_INVOKABLE QString getProxyMode() const; + Q_INVOKABLE void setProxyMode(const QString &proxyType); + Q_INVOKABLE QString getHttpProxyHost() const; + Q_INVOKABLE void setHttpProxyHost(const QString &hostName); + Q_INVOKABLE QString getHttpProxyPort() const; + Q_INVOKABLE void setHttpProxyPort(const QString &port); + Q_INVOKABLE QString getHttpProxyUser() const; + Q_INVOKABLE void setHttpProxyUser(const QString &userName); + Q_INVOKABLE QString getHttpProxyPwd() const; + Q_INVOKABLE void setHttpProxyPwd(const QString &password); + Q_INVOKABLE bool getHttpProxyAuth() const; + + Q_INVOKABLE QString getFtpProxyHost() const; + Q_INVOKABLE void setFtpProxyHost(const QString &hostName); + Q_INVOKABLE QString getFtpProxyPort() const; + Q_INVOKABLE void setFtpProxyPort(const QString &port); + Q_INVOKABLE QString getFtpProxyUser() const; + Q_INVOKABLE void setFtpProxyUser(const QString &userName); + Q_INVOKABLE QString getFtpProxyPwd() const; + Q_INVOKABLE void setFtpProxyPwd(const QString &password); + Q_INVOKABLE bool getFtpProxyAuth() const; Q_INVOKABLE QStringList values(const QString &key, const QStringList &defaultValue = QStringList()) const; Q_INVOKABLE QString key(const QString &value) const;