Skip to content

MI0698-1385 Add cli proxy para and parsing #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/libs/installer/commandlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/libs/installer/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
188 changes: 188 additions & 0 deletions src/libs/installer/packagemanagercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,194 @@ QString PackageManagerCore::value(const QString &key, const QString &defaultValu
return d->m_data.value(key, defaultValue, static_cast<QSettings::Format>(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.
Expand Down
21 changes: 21 additions & 0 deletions src/libs/installer/packagemanagercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 12 additions & 1 deletion src/sdk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ")));
Expand Down Expand Up @@ -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);
Expand Down
65 changes: 65 additions & 0 deletions src/sdk/sdkapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
10 changes: 10 additions & 0 deletions src/sdk/tabcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down