Skip to content

Commit 1c9112d

Browse files
committed
feat: MI0698-1660 use configurator to encrypt/decrypt wirte and read proxy user and password to ini file
1 parent 14edc0b commit 1c9112d

File tree

2 files changed

+165
-31
lines changed

2 files changed

+165
-31
lines changed

src/libs/installer/packagemanagercore_p.cpp

Lines changed: 153 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ PackageManagerCorePrivate::PackageManagerCorePrivate(PackageManagerCore *core)
201201
, m_allowCompressedRepositoryInstall(false)
202202
#endif
203203
, m_connectedOperations(0)
204+
, m_processGetProxyUser(nullptr)
204205
{
205206
}
206207

@@ -1046,22 +1047,6 @@ void PackageManagerCorePrivate::writeMaintenanceConfigFiles()
10461047
writer.writeStartDocument();
10471048

10481049
writer.writeStartElement(QLatin1String("Network"));
1049-
writer.writeTextElement(QLatin1String("ProxyType"), QString::number(m_data.settings().proxyType()));
1050-
writer.writeStartElement(QLatin1String("Ftp"));
1051-
const QNetworkProxy &ftpProxy = m_data.settings().ftpProxy();
1052-
writer.writeTextElement(QLatin1String("Host"), ftpProxy.hostName());
1053-
writer.writeTextElement(QLatin1String("Port"), QString::number(ftpProxy.port()));
1054-
writer.writeTextElement(QLatin1String("Username"), ftpProxy.user());
1055-
writer.writeTextElement(QLatin1String("Password"), ftpProxy.password());
1056-
writer.writeEndElement();
1057-
writer.writeStartElement(QLatin1String("Http"));
1058-
const QNetworkProxy &httpProxy = m_data.settings().httpProxy();
1059-
writer.writeTextElement(QLatin1String("Host"), httpProxy.hostName());
1060-
writer.writeTextElement(QLatin1String("Port"), QString::number(httpProxy.port()));
1061-
writer.writeTextElement(QLatin1String("Username"), httpProxy.user());
1062-
writer.writeTextElement(QLatin1String("Password"), httpProxy.password());
1063-
writer.writeEndElement();
1064-
10651050
writer.writeStartElement(QLatin1String("Repositories"));
10661051
foreach (const Repository &repo, m_data.settings().userRepositories()) {
10671052
writer.writeStartElement(QLatin1String("Repository"));
@@ -1072,17 +1057,75 @@ void PackageManagerCorePrivate::writeMaintenanceConfigFiles()
10721057
writer.writeEndElement();
10731058
}
10741059
writer.writeEndElement();
1075-
writer.writeTextElement(QLatin1String("LocalCachePath"), m_data.settings().localCachePath());
10761060
writer.writeEndElement();
10771061

10781062
file.write(outputStr.toUtf8());
10791063
}
10801064
setDefaultFilePermissions(&file, DefaultFilePermissions::NonExecutable);
1065+
1066+
//directly access disw.ini to write http mode , host and port
1067+
QSettingsWrapper configFile(CONF_PATH,QSettings::IniFormat);
1068+
if(m_data.settings().proxyType() == 0)
1069+
{
1070+
configFile.setValue(QStringLiteral("proxy/mode"),QStringLiteral("disabled"));
1071+
}
1072+
if(m_data.settings().proxyType() == 1)
1073+
{
1074+
configFile.setValue(QStringLiteral("proxy/mode"),QStringLiteral("system"));
1075+
}
1076+
if(m_data.settings().proxyType() == 2)
1077+
{
1078+
configFile.setValue(QStringLiteral("proxy/mode"),QStringLiteral("custom"));
1079+
}
1080+
//httpproxy para
1081+
const QNetworkProxy &httpProxy = m_data.settings().httpProxy();
1082+
configFile.beginGroup(QStringLiteral("proxy"));
1083+
configFile.setValue(QStringLiteral("host"),httpProxy.hostName());
1084+
configFile.setValue(QStringLiteral("port"),httpProxy.port());
1085+
configFile.endGroup();
1086+
//local cache path
1087+
configFile.beginGroup(QStringLiteral("installer-localcache"));
1088+
configFile.setValue(QStringLiteral("path"),m_data.settings().localCachePath());
1089+
configFile.endGroup();
1090+
1091+
//use configurator.exe to write http proxy paras to disw.ini
1092+
const QString configuratorFileArg = targetDir() + QLatin1String("/config/tools/configurator.exe");
1093+
if (!QFileInfo::exists(configuratorFileArg))
1094+
{
1095+
qCWarning(QInstaller::lcInstallerInstallLog) << "configurator path not exist";
1096+
return;
1097+
}
1098+
1099+
QStringList proxyArgList;
1100+
proxyArgList.append(configuratorFileArg);
1101+
proxyArgList << QLatin1String("--proxyuser") << httpProxy.user() << QLatin1String("--proxypwd") << httpProxy.password();
1102+
//use configurator to set proxyuser and proxypwd
1103+
QProcessWrapper *process = new QProcessWrapper();
1104+
QProcessEnvironment penv = QProcessEnvironment::systemEnvironment();
1105+
process->setEnvironment(penv.toStringList());
1106+
QObject::connect(process,&QProcessWrapper::readyRead, this, [=]{
1107+
QString outputStr = QString::fromUtf8(process->readAll());
1108+
qCInfo(QInstaller::lcInstallerInstallLog) << outputStr;
1109+
});
1110+
1111+
bool success = false;
1112+
process->start(proxyArgList.front(), proxyArgList.mid(1));
1113+
if (QThread::currentThread() == qApp->thread()) {
1114+
success = process->waitForStarted();
1115+
} else {
1116+
success = process->waitForFinished(-1);
1117+
}
1118+
if (!success)
1119+
{
1120+
qCWarning(QInstaller::lcInstallerInstallLog) << "configurator set proxy not successful";
1121+
}
1122+
1123+
10811124
}
10821125

1083-
void PackageManagerCorePrivate::readMaintenanceConfigFiles(const QString &targetDir)
1126+
void PackageManagerCorePrivate::readMaintenanceConfigFiles(const QString &targetDirectory)
10841127
{
1085-
QSettingsWrapper cfg(targetDir + QLatin1Char('/') + m_data.settings().maintenanceToolIniFile(),
1128+
QSettingsWrapper cfg(targetDirectory + QLatin1Char('/') + m_data.settings().maintenanceToolIniFile(),
10861129
QSettings::IniFormat);
10871130
const QVariantHash v = cfg.value(QLatin1String("Variables")).toHash(); // Do not change to
10881131
// QVariantMap! Breaks reading from existing .ini files, cause the variant types do not match.
@@ -1093,7 +1136,7 @@ void PackageManagerCorePrivate::readMaintenanceConfigFiles(const QString &target
10931136
if (it.key() != scStartMenuDir)
10941137
continue;
10951138
}
1096-
m_data.setValue(it.key(), replacePath(it.value().toString(), QLatin1String(scRelocatable), targetDir));
1139+
m_data.setValue(it.key(), replacePath(it.value().toString(), QLatin1String(scRelocatable), targetDirectory));
10971140
}
10981141
QSet<Repository> repos;
10991142
const QVariantList variants = cfg.value(QLatin1String("DefaultRepositories"))
@@ -1105,7 +1148,43 @@ void PackageManagerCorePrivate::readMaintenanceConfigFiles(const QString &target
11051148

11061149
m_filesForDelayedDeletion = cfg.value(QLatin1String("FilesForDelayedDeletion")).toStringList();
11071150

1108-
QFile file(targetDir + QLatin1String("/network.xml"));
1151+
1152+
//read from configurator for decryted user and password
1153+
const QString configuratorFileArg = targetDir() + QLatin1String("/config/tools/configurator.exe");
1154+
if (!QFileInfo::exists(configuratorFileArg))
1155+
{
1156+
qCWarning(QInstaller::lcInstallerInstallLog) << "configurator path not exist";
1157+
return;
1158+
}
1159+
else
1160+
{
1161+
qCInfo(QInstaller::lcInstallerInstallLog) << "configurator path :" <<configuratorFileArg;
1162+
}
1163+
1164+
QStringList proxyGetUserArgList;
1165+
proxyGetUserArgList.append(configuratorFileArg);
1166+
proxyGetUserArgList << QLatin1String("--getproxyparam");
1167+
//use configurator to set proxyuser and proxypwd
1168+
m_processGetProxyUser = new QProcessWrapper();
1169+
QProcessEnvironment penv = QProcessEnvironment::systemEnvironment();
1170+
m_processGetProxyUser->setEnvironment(penv.toStringList());
1171+
m_processGetProxyUser->setProcessChannelMode(QProcessWrapper::MergedChannels);
1172+
QObject::connect(m_processGetProxyUser,&QProcessWrapper::readyRead, this, &PackageManagerCorePrivate::changeProxyUsrAndPwd, Qt::DirectConnection);
1173+
bool resForGetUser = false;
1174+
m_processGetProxyUser->start(proxyGetUserArgList.front(), proxyGetUserArgList.mid(1));
1175+
qCWarning(QInstaller::lcInstallerInstallLog) << "configurator started";
1176+
if (QThread::currentThread() == qApp->thread()) {
1177+
resForGetUser = m_processGetProxyUser->waitForStarted();
1178+
} else {
1179+
resForGetUser = m_processGetProxyUser->waitForFinished(-1);
1180+
}
1181+
if (!resForGetUser)
1182+
{
1183+
qCWarning(QInstaller::lcInstallerInstallLog) << "configurator get proxy decryed paras not successful";
1184+
}
1185+
1186+
1187+
QFile file(targetDirectory + QLatin1String("/network.xml"));
11091188
if (!file.open(QIODevice::ReadOnly))
11101189
return;
11111190

@@ -1115,17 +1194,8 @@ void PackageManagerCorePrivate::readMaintenanceConfigFiles(const QString &target
11151194
case QXmlStreamReader::StartElement: {
11161195
if (reader.name() == QLatin1String("Network")) {
11171196
while (reader.readNextStartElement()) {
1118-
const QStringView name = reader.name();
1119-
if (name == QLatin1String("Ftp")) {
1120-
m_data.settings().setFtpProxy(readProxy(reader));
1121-
} else if (name == QLatin1String("Http")) {
1122-
m_data.settings().setHttpProxy(readProxy(reader));
1123-
} else if (reader.name() == QLatin1String("Repositories")) {
1197+
if (reader.name() == QLatin1String("Repositories")) {
11241198
m_data.settings().addUserRepositories(readRepositories(reader, false));
1125-
} else if (name == QLatin1String("ProxyType")) {
1126-
m_data.settings().setProxyType(Settings::ProxyType(reader.readElementText().toInt()));
1127-
} else if (name == QLatin1String("LocalCachePath")) {
1128-
m_data.settings().setLocalCachePath(reader.readElementText());
11291199
} else {
11301200
reader.skipCurrentElement();
11311201
}
@@ -3300,6 +3370,58 @@ void PackageManagerCorePrivate::addPathForDeletion(const QString &path)
33003370
m_tmpPathDeleter.add(path);
33013371
}
33023372

3373+
void PackageManagerCorePrivate::changeProxyUsrAndPwd()
3374+
{
3375+
//directly access disw.ini to read http mode , host and port
3376+
QSettingsWrapper configFile(CONF_PATH,QSettings::IniFormat);
3377+
QString proxyModeStr = configFile.value(QStringLiteral("proxy/mode")).toString();
3378+
if(proxyModeStr == QStringLiteral("disabled"))
3379+
{
3380+
m_data.settings().setProxyType(Settings::ProxyType::NoProxy);
3381+
}
3382+
if(proxyModeStr == QStringLiteral("system"))
3383+
{
3384+
m_data.settings().setProxyType(Settings::ProxyType::SystemProxy);
3385+
}
3386+
if(proxyModeStr == QStringLiteral("custom"))
3387+
{
3388+
m_data.settings().setProxyType(Settings::ProxyType::UserDefinedProxy);
3389+
}
3390+
QString localCachePath = configFile.value(QStringLiteral("installer-localcache/path")).toString();
3391+
m_data.settings().setLocalCachePath(localCachePath);
3392+
QString proxyHostStr = configFile.value(QStringLiteral("proxy/host")).toString();
3393+
int proxyPortNum = configFile.value(QStringLiteral("proxy/port")).toInt();
3394+
3395+
Q_ASSERT(m_processGetProxyUser);
3396+
Q_ASSERT(QThread::currentThread() == m_processGetProxyUser->thread());
3397+
QString retVal = QString::fromLocal8Bit(m_processGetProxyUser->readAll()).simplified();
3398+
if(retVal.isEmpty())
3399+
{
3400+
qCWarning(QInstaller::lcInstallerInstallLog) << "configurator return empty";
3401+
}
3402+
qCDebug(QInstaller::lcInstallerInstallLog) << "configurator return OUTPUT" <<retVal;
3403+
QStringList paraList = retVal.split(QStringLiteral(" "));
3404+
if(paraList.size() < 2)
3405+
{
3406+
qCWarning(QInstaller::lcInstallerInstallLog) << "return value error";
3407+
}
3408+
QString proxyUser = paraList.at(0);
3409+
QString proxyPassword = paraList.at(1);
3410+
qCDebug(QInstaller::lcInstallerInstallLog) << "decrypted proxy USER:"<< proxyUser;
3411+
qCDebug(QInstaller::lcInstallerInstallLog) << "decrypted proxy PASSWORD:"<< proxyPassword;
3412+
3413+
if(!proxyUser.isEmpty() && !proxyPassword.isEmpty())
3414+
{
3415+
m_core->settings().setHttpProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyHostStr, proxyPortNum, proxyUser, proxyPassword));
3416+
}
3417+
else
3418+
{
3419+
qCWarning(QInstaller::lcInstallerInstallLog) << "empty proxyUser or proxyPassword";
3420+
m_core->settings().setHttpProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyHostStr, proxyPortNum));
3421+
}
3422+
3423+
}
3424+
33033425
void PackageManagerCorePrivate::unpackAndInstallComponents(const QList<Component *> &components,
33043426
const double progressOperationSize)
33053427
{

src/libs/installer/packagemanagercore_p.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@
4040

4141
#include "sysinfo.h"
4242
#include "updatefinder.h"
43+
#include "qprocesswrapper.h"
4344

4445
#include <QObject>
46+
#include <QCoreApplication>
47+
#include <QDir>
48+
49+
const QString CONF = QStringLiteral("disw.ini");
50+
#ifdef Q_OS_WINDOWS
51+
const QString CONF_PATH = QDir(qEnvironmentVariable("ProgramData")).filePath(QStringLiteral("Barco/disw/")) + CONF;
52+
#else
53+
const QString CONF_PATH = QStringLiteral("/etc/Barco/disw/") + CONF;
54+
#endif
4555

4656
class Job;
4757

@@ -248,6 +258,7 @@ private slots:
248258

249259
void handleMethodInvocationRequest(const QString &invokableMethodName);
250260
void addPathForDeletion(const QString &path);
261+
void changeProxyUsrAndPwd();
251262

252263
private:
253264
void unpackAndInstallComponents(const QList<Component *> &components,
@@ -345,6 +356,7 @@ private slots:
345356
bool m_allowCompressedRepositoryInstall;
346357
int m_connectedOperations;
347358
QStringList m_componentsToBeInstalled;
359+
QProcessWrapper *m_processGetProxyUser;
348360
};
349361

350362
} // namespace QInstaller

0 commit comments

Comments
 (0)