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 struct Qv2rayNetworkConfig
{ {
bool useCustomProxy; enum Qv2rayProxyType
{
QVPROXY_NONE,
QVPROXY_SYSTEM,
QVPROXY_CUSTOM
} proxyType;
QString address; QString address;
QString type; QString type;
int port; int port;
QString userAgent; QString userAgent;
Qv2rayNetworkConfig() : address(""), type("http"), port(8000), userAgent("Qv2ray/$VERSION WebRequestHelper"){}; Qv2rayNetworkConfig()
XTOSTRUCT(O(useCustomProxy, type, address, port, userAgent)) : 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 struct Qv2rayConfig

View File

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

View File

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

View File

@ -149,7 +149,24 @@ PreferencesWindow::PreferencesWindow(QWidget *parent) : QDialog(parent), Current
qvProxyAddressTxt->setText(CurrentConfig.networkConfig.address); qvProxyAddressTxt->setText(CurrentConfig.networkConfig.address);
qvProxyTypeCombo->setCurrentText(CurrentConfig.networkConfig.type); qvProxyTypeCombo->setCurrentText(CurrentConfig.networkConfig.type);
qvNetworkUATxt->setText(CurrentConfig.networkConfig.userAgent); 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); quietModeCB->setChecked(CurrentConfig.uiConfig.quietMode);
// //
@ -1248,11 +1265,6 @@ void PreferencesWindow::on_tproxyListenAddr_textEdited(const QString &arg1)
CurrentConfig.inboundConfig.tproxy_ip = arg1; CurrentConfig.inboundConfig.tproxy_ip = arg1;
} }
void PreferencesWindow::on_customProxySettingsGroupBox_clicked(bool checked)
{
CurrentConfig.networkConfig.useCustomProxy = checked;
}
void PreferencesWindow::on_jumpListCountSB_valueChanged(int arg1) void PreferencesWindow::on_jumpListCountSB_valueChanged(int arg1)
{ {
CurrentConfig.uiConfig.maxJumpListCount = arg1; CurrentConfig.uiConfig.maxJumpListCount = arg1;
@ -1269,3 +1281,18 @@ void PreferencesWindow::on_dnsIntercept_toggled(bool checked)
NEEDRESTART NEEDRESTART
CurrentConfig.inboundConfig.dnsIntercept = checked; 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_tproxyListenAddr_textEdited(const QString &arg1);
void on_customProxySettingsGroupBox_clicked(bool checked);
void on_jumpListCountSB_valueChanged(int arg1); void on_jumpListCountSB_valueChanged(int arg1);
void on_outboundMark_valueChanged(int arg1); void on_outboundMark_valueChanged(int arg1);
void on_dnsIntercept_toggled(bool checked); void on_dnsIntercept_toggled(bool checked);
void on_qvProxyCustomProxy_clicked();
void on_qvProxySystemProxy_clicked();
void on_qvProxyNoProxy_clicked();
private: private:
// //
RouteSettingsMatrixWidget *routeSettingsWidget; RouteSettingsMatrixWidget *routeSettingsWidget;

View File

@ -9,8 +9,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>756</width> <width>864</width>
<height>572</height> <height>614</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -273,7 +273,7 @@
<property name="title"> <property name="title">
<string>Network Settings</string> <string>Network Settings</string>
</property> </property>
<layout class="QFormLayout" name="formLayout_15"> <layout class="QFormLayout" name="formLayout_13">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label_67"> <widget class="QLabel" name="label_67">
<property name="text"> <property name="text">
@ -288,73 +288,93 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0" colspan="2"> <item row="1" column="0">
<widget class="QGroupBox" name="customProxySettingsGroupBox"> <widget class="QLabel" name="label_71">
<property name="title"> <property name="text">
<string>Use Custom Proxy Settings</string> <string>Qv2ray Proxy</string>
</property> </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">
<property name="text">
<string>Proxy Type</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="qvProxyTypeCombo">
<item>
<property name="text">
<string notr="true">http</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">socks</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_64">
<property name="text">
<string>Server</string>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="qvProxyAddressTxt"/>
</item>
<item>
<widget class="QLabel" name="label_74">
<property name="text">
<string>:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="qvProxyPortCB">
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget> </widget>
</item> </item>
<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">
<string notr="true">http</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">socks</string>
</property>
</item>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_64">
<property name="text">
<string>Curtom Proxy Server</string>
</property>
</widget>
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="qvProxyAddressTxt"/>
</item>
<item>
<widget class="QLabel" name="label_74">
<property name="text">
<string>:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="qvProxyPortCB">
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
@ -705,8 +725,8 @@ Custom DNS Settings</string>
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>703</width> <width>818</width>
<height>524</height> <height>520</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">