fix: refactored Qv2ray Proxy settings

This commit is contained in:
Qv2ray-dev 2020-04-24 15:29:29 +08:00
parent 73b030af08
commit 0480c63b3a
7 changed files with 181 additions and 121 deletions

View File

@ -1 +1 @@
5311
5312

View File

@ -251,13 +251,24 @@ namespace Qv2ray::base::config
struct Qv2rayNetworkConfig
{
bool useCustomProxy;
enum Qv2rayProxyType
{
QVPROXY_NONE,
QVPROXY_SYSTEM,
QVPROXY_CUSTOM
} proxyType;
QString address;
QString type;
int port;
QString userAgent;
Qv2rayNetworkConfig() : address(""), type("http"), port(8000), userAgent("Qv2ray/$VERSION WebRequestHelper"){};
XTOSTRUCT(O(useCustomProxy, type, address, port, userAgent))
Qv2rayNetworkConfig()
: proxyType(QVPROXY_NONE), //
address("127.0.0.1"), //
type("http"), //
port(8000), //
userAgent("Qv2ray/$VERSION WebRequestHelper"){};
XTOSTRUCT(O(proxyType, type, address, port, userAgent))
};
struct Qv2rayConfig

View File

@ -22,47 +22,22 @@ namespace Qv2ray::common
request.setRawHeader(key, value);
}
QByteArray QvHttpRequestHelper::Get(const QString &url)
void QvHttpRequestHelper::setAccessManagerAttributes(QNetworkAccessManager &accessManager)
{
request.setUrl({ url });
QNetworkProxy p;
if (GlobalConfig.networkConfig.useCustomProxy)
switch (GlobalConfig.networkConfig.proxyType)
{
auto type = GlobalConfig.networkConfig.type == "http" ? QNetworkProxy::HttpProxy : QNetworkProxy::Socks5Proxy;
p = QNetworkProxy{ type, GlobalConfig.networkConfig.address, quint16(GlobalConfig.networkConfig.port) };
case Qv2rayNetworkConfig::QVPROXY_NONE:
{
DEBUG(MODULE_NETWORK, "Get without proxy.")
accessManager.setProxy(QNetworkProxy(QNetworkProxy::ProxyType::NoProxy));
break;
}
else
case Qv2rayNetworkConfig::QVPROXY_SYSTEM:
{
p = QNetworkProxyFactory::systemProxyForQuery().first();
accessManager.setProxy(QNetworkProxyFactory::systemProxyForQuery().first());
break;
}
if (p.type() == QNetworkProxy::Socks5Proxy)
{
DEBUG(MODULE_NETWORK, "Adding HostNameLookupCapability to proxy.")
p.setCapabilities(accessManager.proxy().capabilities() | QNetworkProxy::HostNameLookupCapability);
}
accessManager.setProxy(p);
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true);
auto ua = GlobalConfig.networkConfig.userAgent;
ua.replace("$VERSION", QV2RAY_VERSION_STRING);
request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, ua);
reply = accessManager.get(request);
//
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
//
// Data or timeout?
auto data = reply->readAll();
return data;
}
void QvHttpRequestHelper::AsyncGet(const QString &url)
{
request.setUrl({ url });
if (GlobalConfig.networkConfig.useCustomProxy)
case Qv2rayNetworkConfig::QVPROXY_CUSTOM:
{
QNetworkProxy p{
GlobalConfig.networkConfig.type == "http" ? QNetworkProxy::HttpProxy : QNetworkProxy::Socks5Proxy, //
@ -70,20 +45,42 @@ namespace Qv2ray::common
quint16(GlobalConfig.networkConfig.port) //
};
accessManager.setProxy(p);
break;
}
else
{
DEBUG(MODULE_NETWORK, "Get without proxy.")
accessManager.setProxy(QNetworkProxy(QNetworkProxy::ProxyType::NoProxy));
}
if (accessManager.proxy().type() == QNetworkProxy::Socks5Proxy)
{
DEBUG(MODULE_NETWORK, "Adding HostNameLookupCapability to proxy.")
accessManager.proxy().setCapabilities(accessManager.proxy().capabilities() | QNetworkProxy::HostNameLookupCapability);
}
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true);
request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, GlobalConfig.networkConfig.userAgent);
auto ua = GlobalConfig.networkConfig.userAgent;
ua.replace("$VERSION", QV2RAY_VERSION_STRING);
request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, ua);
}
QByteArray QvHttpRequestHelper::Get(const QString &url)
{
request.setUrl({ url });
setAccessManagerAttributes(accessManager);
auto _reply = accessManager.get(request);
//
QEventLoop loop;
connect(_reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
//
// Data or timeout?
auto data = _reply->readAll();
return data;
}
void QvHttpRequestHelper::AsyncGet(const QString &url)
{
request.setUrl({ url });
setAccessManagerAttributes(accessManager);
reply = accessManager.get(request);
connect(reply, &QNetworkReply::finished, this, &QvHttpRequestHelper::onRequestFinished_p);
connect(reply, &QNetworkReply::readyRead, this, &QvHttpRequestHelper::onReadyRead_p);

View File

@ -42,6 +42,7 @@ namespace Qv2ray::common
void onReadyRead_p();
private:
void setAccessManagerAttributes(QNetworkAccessManager &accessManager);
void setHeader(const QByteArray &key, const QByteArray &value);
QByteArray data;
QUrl url;

View File

@ -149,7 +149,24 @@ PreferencesWindow::PreferencesWindow(QWidget *parent) : QDialog(parent), Current
qvProxyAddressTxt->setText(CurrentConfig.networkConfig.address);
qvProxyTypeCombo->setCurrentText(CurrentConfig.networkConfig.type);
qvNetworkUATxt->setText(CurrentConfig.networkConfig.userAgent);
customProxySettingsGroupBox->setChecked(CurrentConfig.networkConfig.useCustomProxy);
switch (CurrentConfig.networkConfig.proxyType)
{
case Qv2rayNetworkConfig::QVPROXY_NONE:
{
qvProxyNoProxy->setChecked(true);
break;
}
case Qv2rayNetworkConfig::QVPROXY_SYSTEM:
{
qvProxySystemProxy->setChecked(true);
break;
}
case Qv2rayNetworkConfig::QVPROXY_CUSTOM:
{
qvProxyCustomProxy->setChecked(true);
break;
}
}
//
quietModeCB->setChecked(CurrentConfig.uiConfig.quietMode);
//
@ -1248,11 +1265,6 @@ void PreferencesWindow::on_tproxyListenAddr_textEdited(const QString &arg1)
CurrentConfig.inboundConfig.tproxy_ip = arg1;
}
void PreferencesWindow::on_customProxySettingsGroupBox_clicked(bool checked)
{
CurrentConfig.networkConfig.useCustomProxy = checked;
}
void PreferencesWindow::on_jumpListCountSB_valueChanged(int arg1)
{
CurrentConfig.uiConfig.maxJumpListCount = arg1;
@ -1269,3 +1281,18 @@ void PreferencesWindow::on_dnsIntercept_toggled(bool checked)
NEEDRESTART
CurrentConfig.inboundConfig.dnsIntercept = checked;
}
void PreferencesWindow::on_qvProxyCustomProxy_clicked()
{
CurrentConfig.networkConfig.proxyType = Qv2rayNetworkConfig::QVPROXY_CUSTOM;
}
void PreferencesWindow::on_qvProxySystemProxy_clicked()
{
CurrentConfig.networkConfig.proxyType = Qv2rayNetworkConfig::QVPROXY_SYSTEM;
}
void PreferencesWindow::on_qvProxyNoProxy_clicked()
{
CurrentConfig.networkConfig.proxyType = Qv2rayNetworkConfig::QVPROXY_NONE;
}

View File

@ -190,14 +190,18 @@ class PreferencesWindow
void on_tproxyListenAddr_textEdited(const QString &arg1);
void on_customProxySettingsGroupBox_clicked(bool checked);
void on_jumpListCountSB_valueChanged(int arg1);
void on_outboundMark_valueChanged(int arg1);
void on_dnsIntercept_toggled(bool checked);
void on_qvProxyCustomProxy_clicked();
void on_qvProxySystemProxy_clicked();
void on_qvProxyNoProxy_clicked();
private:
//
RouteSettingsMatrixWidget *routeSettingsWidget;

View File

@ -9,8 +9,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>756</width>
<height>572</height>
<width>864</width>
<height>614</height>
</rect>
</property>
<property name="windowTitle">
@ -273,7 +273,7 @@
<property name="title">
<string>Network Settings</string>
</property>
<layout class="QFormLayout" name="formLayout_15">
<layout class="QFormLayout" name="formLayout_13">
<item row="0" column="0">
<widget class="QLabel" name="label_67">
<property name="text">
@ -288,23 +288,46 @@
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QGroupBox" name="customProxySettingsGroupBox">
<property name="title">
<string>Use Custom Proxy Settings</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QFormLayout" name="formLayout_13">
<item row="0" column="0">
<widget class="QLabel" name="label_66">
<item row="1" column="0">
<widget class="QLabel" name="label_71">
<property name="text">
<string>Proxy Type</string>
<string>Qv2ray Proxy</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<widget class="QRadioButton" name="qvProxyNoProxy">
<property name="text">
<string>No Proxy</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="qvProxySystemProxy">
<property name="text">
<string>System Proxy</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="qvProxyCustomProxy">
<property name="text">
<string>Custom Proxy</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_66">
<property name="text">
<string>Curtom Proxy Type</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="qvProxyTypeCombo">
<item>
<property name="text">
@ -318,14 +341,14 @@
</item>
</widget>
</item>
<item row="1" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_64">
<property name="text">
<string>Server</string>
<string>Curtom Proxy Server</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="qvProxyAddressTxt"/>
@ -355,9 +378,6 @@
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QGroupBox" name="groupBox_8">
<property name="title">
@ -705,8 +725,8 @@ Custom DNS Settings</string>
<rect>
<x>0</x>
<y>0</y>
<width>703</width>
<height>524</height>
<width>818</width>
<height>520</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">