refactor: system-proxy/linux/set

This commit is contained in:
DuckSoft 2020-05-06 22:03:52 +08:00
parent 5c66b29879
commit b5ec43c0df
No known key found for this signature in database
GPG Key ID: 7A3A9FA6E4FD4A8D

View File

@ -233,46 +233,62 @@ namespace Qv2ray::components::proxy
actions << QString("gsettings set org.gnome.system.proxy mode '%1'").arg("manual"); actions << QString("gsettings set org.gnome.system.proxy mode '%1'").arg("manual");
bool isKDE = qEnvironmentVariable("XDG_SESSION_DESKTOP") == "KDE"; bool isKDE = qEnvironmentVariable("XDG_SESSION_DESKTOP") == "KDE";
const auto configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); const auto configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
// Setting Proxy Mode to Manual
{
// for GNOME:
{
actions << "gsettings set org.gnome.system.proxy mode 'manual'";
}
// for KDE:
if (isKDE) if (isKDE)
{ {
LOG(MODULE_PROXY, "KDE detected") LOG(MODULE_PROXY, "KDE detected")
actions << QString("kwriteconfig5 --file " + configPath + "/kioslaverc --group \"Proxy Settings\" --key ProxyType 1"); actions << QString("kwriteconfig5 --file %1/kioslaverc --group 'Proxy Settings' --key ProxyType 1").arg(configPath);
} }
}
// Configure HTTP Proxies for HTTP, FTP and HTTPS
if (hasHTTP) if (hasHTTP)
{ {
actions << QString("gsettings set org.gnome.system.proxy.http host '%1'").arg(address); // iterate over protocols...
actions << QString("gsettings set org.gnome.system.proxy.http port %1").arg(httpPort); for (const auto protocol : { "http", "ftp", "https" })
// {
actions << QString("gsettings set org.gnome.system.proxy.https host '%1'").arg(address); // for GNOME:
actions << QString("gsettings set org.gnome.system.proxy.https port %1").arg(httpPort); {
actions << QString("gsettings set org.gnome.system.proxy.%1 host '%2'").arg(protocol, address);
actions << QString("gsettings set org.gnome.system.proxy.%1 port %2").arg(protocol, QSTRN(httpPort));
}
// for KDE:
if (isKDE) if (isKDE)
{ {
// FTP here should be scheme: ftp:// actions << QString("kwriteconfig5 --file %1/kioslaverc --group 'Proxy Settings' --key %2Proxy 'http://%3 %4'")
for (auto protocol : { "http", "ftp", "https" }) .arg(configPath, protocol, address, QSTRN(httpPort));
{
auto str =
QString("kwriteconfig5 --file " + configPath + "/kioslaverc --group \"Proxy Settings\" --key %1Proxy \"http://%2 %3\"")
.arg(protocol)
.arg(address)
.arg(QSTRN(httpPort));
actions << str;
} }
} }
} }
// Configure SOCKS5 Proxies
if (hasSOCKS) if (hasSOCKS)
{
// for GNOME:
{ {
actions << QString("gsettings set org.gnome.system.proxy.socks host '%1'").arg(address); actions << QString("gsettings set org.gnome.system.proxy.socks host '%1'").arg(address);
actions << QString("gsettings set org.gnome.system.proxy.socks port %1").arg(socksPort); actions << QString("gsettings set org.gnome.system.proxy.socks port %1").arg(socksPort);
}
// for KDE:
if (isKDE) if (isKDE)
{ {
actions << QString("kwriteconfig5 --file " + configPath + actions << QString("kwriteconfig5 --file %1/kioslaverc --group 'Proxy Settings' --key socksProxy 'socks://%2 %3'")
"/kioslaverc --group \"Proxy Settings\" --key socksProxy \"socks://%1 %2\"") .arg(configPath, address, QSTRN(socksPort));
.arg(address)
.arg(QSTRN(socksPort));
} }
} }
// Execute them all!
//
// note: do not use std::all_of / any_of / none_of, // note: do not use std::all_of / any_of / none_of,
// because those are short-circuit and cannot guarantee atomicity. // because those are short-circuit and cannot guarantee atomicity.
auto result = std::count_if(actions.cbegin(), actions.cend(), [](const QString &action) { auto result = std::count_if(actions.cbegin(), actions.cend(), [](const QString &action) {
@ -286,7 +302,8 @@ namespace Qv2ray::components::proxy
LOG(MODULE_PROXY, "It may happen if you are using KDE with no gsettings support.") LOG(MODULE_PROXY, "It may happen if you are using KDE with no gsettings support.")
} }
Q_UNUSED(result); // TODO: Post-settings for DDE
#else #else
for (auto service : macOSgetNetworkServices()) for (auto service : macOSgetNetworkServices())