Add proxy config with Socks5 and HTTP proxy support

Resolve https://invent.kde.org/network/neochat/-/issues/547

This patch attempts to add proxy config support to NeoChat so people could set a HTTP or Socks5 proxy for anonymity or privacy/censorship-circumvention reason.

Currently this will only allows setting proxy type, host and port (which should be usable for most of the cases). Settings in that page needs to be applied by clicking the Apply button so the proxy setting won't accidentally get changed.

Proxy is disabled (use System Default option) by default.
This commit is contained in:
Gary Wang
2022-11-13 15:13:28 +00:00
committed by Tobias Fella
parent 7a2c4f6c71
commit 6ecc18d985
8 changed files with 162 additions and 1 deletions

View File

@@ -18,6 +18,7 @@
#include <QFileInfo>
#include <QGuiApplication>
#include <QImageReader>
#include <QNetworkProxy>
#include <QQuickItem>
#include <QQuickTextDocument>
#include <QQuickWindow>
@@ -65,6 +66,8 @@ Controller::Controller(QObject *parent)
Connection::setRoomType<NeoChatRoom>();
Connection::setUserType<NeoChatUser>();
setApplicationProxy();
#ifndef Q_OS_ANDROID
if (NeoChatConfig::self()->systemTray()) {
m_trayIcon = new TrayIcon(this);
@@ -784,3 +787,29 @@ void Controller::forceRefreshTextDocument(QQuickTextDocument *textDocument, QQui
// HACK: Workaround bug QTBUG 93281
connect(textDocument->textDocument(), SIGNAL(imagesLoaded()), item, SLOT(updateWholeDocument()));
}
void Controller::setApplicationProxy()
{
NeoChatConfig *cfg = NeoChatConfig::self();
QNetworkProxy proxy;
// type match to ProxyType from neochatconfig.kcfg
switch (cfg->proxyType()) {
case 1: // HTTP
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(cfg->proxyHost());
proxy.setPort(cfg->proxyPort());
break;
case 2: // SOCKS 5
proxy.setType(QNetworkProxy::Socks5Proxy);
proxy.setHostName(cfg->proxyHost());
proxy.setPort(cfg->proxyPort());
break;
case 0: // System Default
default:
// do nothing
break;
}
QNetworkProxy::setApplicationProxy(proxy);
}