mirror of
https://github.com/Mahdi-zarei/nekoray.git
synced 2025-12-19 13:50:12 +08:00
refactor: improve NetworkRequestHelper
This commit is contained in:
parent
e615045a92
commit
3cdf027dd6
@ -89,12 +89,14 @@ namespace Configs {
|
||||
QString simple_dl_url = "http://cachefly.cachefly.net/1mb.test";
|
||||
bool allow_beta_update = false;
|
||||
|
||||
// Network
|
||||
bool net_use_proxy = false;
|
||||
bool net_insecure = false;
|
||||
|
||||
// Subscription
|
||||
QString user_agent = ""; // set at main.cpp
|
||||
bool sub_use_proxy = false;
|
||||
bool sub_clear = false;
|
||||
bool sub_insecure = false;
|
||||
int sub_auto_update = -30;
|
||||
bool sub_clear = false;
|
||||
bool sub_send_hwid = false;
|
||||
|
||||
// Security
|
||||
|
||||
@ -26,7 +26,7 @@ namespace Configs_network {
|
||||
;
|
||||
|
||||
public:
|
||||
static HTTPResponse HttpGet(const QString &url);
|
||||
static HTTPResponse HttpGet(const QString &url, bool sendHwid = false);
|
||||
|
||||
static QString GetHeader(const QList<QPair<QByteArray, QByteArray>> &header, const QString &name);
|
||||
|
||||
|
||||
@ -490,9 +490,50 @@
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_3">
|
||||
<attribute name="title">
|
||||
<string>Subscription</string>
|
||||
<string>Network</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_6">
|
||||
<item>
|
||||
<widget class="QGroupBox" name="groupBox_3">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="title">
|
||||
<string>Network</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_11">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="net_use_proxy">
|
||||
<property name="text">
|
||||
<string>Use proxy</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="net_insecure">
|
||||
<property name="text">
|
||||
<string>Ignore TLS errors</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QGroupBox" name="groupBox_4">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="title">
|
||||
<string>Subscription</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_10">
|
||||
<item row="0" column="1">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_5">
|
||||
<item>
|
||||
@ -531,27 +572,13 @@
|
||||
<widget class="MyLineEdit" name="user_agent"/>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QCheckBox" name="sub_use_proxy">
|
||||
<property name="text">
|
||||
<string>Use proxy when updating subscription</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QCheckBox" name="sub_insecure">
|
||||
<property name="text">
|
||||
<string>Ignore TLS errors when updating subscription</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QCheckBox" name="sub_clear">
|
||||
<property name="text">
|
||||
<string>Clear servers before updating subscription</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="1">
|
||||
<item row="3" column="1">
|
||||
<widget class="QCheckBox" name="sub_send_hwid">
|
||||
<property name="toolTip">
|
||||
<string><html><head/><body><p>HWID=%1</p><p>OS=%2</p><p>OS Version=%3</p><p>Model=%4</p></body></html></string>
|
||||
@ -583,6 +610,9 @@
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_4">
|
||||
<attribute name="title">
|
||||
<string>Core</string>
|
||||
|
||||
@ -807,7 +807,7 @@ namespace Subscription {
|
||||
auto groupName = group == nullptr ? content : group->name;
|
||||
MW_show_log(">>>>>>>> " + QObject::tr("Requesting subscription: %1").arg(groupName));
|
||||
|
||||
auto resp = NetworkRequestHelper::HttpGet(content);
|
||||
auto resp = NetworkRequestHelper::HttpGet(content, Configs::dataStore->sub_send_hwid);
|
||||
if (!resp.error.isEmpty()) {
|
||||
MW_show_log("<<<<<<<< " + QObject::tr("Requesting subscription %1 error: %2").arg(groupName, resp.error + "\n" + resp.data));
|
||||
return;
|
||||
|
||||
@ -255,7 +255,7 @@ namespace Configs {
|
||||
_add(new configItem("theme", &theme, itemType::string));
|
||||
_add(new configItem("custom_inbound", &custom_inbound, itemType::string));
|
||||
_add(new configItem("custom_route", &custom_route_global, itemType::string));
|
||||
_add(new configItem("sub_use_proxy", &sub_use_proxy, itemType::boolean));
|
||||
_add(new configItem("net_use_proxy", &net_use_proxy, itemType::boolean));
|
||||
_add(new configItem("remember_id", &remember_id, itemType::integer));
|
||||
_add(new configItem("remember_enable", &remember_enable, itemType::boolean));
|
||||
_add(new configItem("language", &language, itemType::integer));
|
||||
@ -277,7 +277,7 @@ namespace Configs {
|
||||
_add(new configItem("vpn_ipv6", &vpn_ipv6, itemType::boolean));
|
||||
_add(new configItem("vpn_strict_route", &vpn_strict_route, itemType::boolean));
|
||||
_add(new configItem("sub_clear", &sub_clear, itemType::boolean));
|
||||
_add(new configItem("sub_insecure", &sub_insecure, itemType::boolean));
|
||||
_add(new configItem("net_insecure", &net_insecure, itemType::boolean));
|
||||
_add(new configItem("sub_auto_update", &sub_auto_update, itemType::integer));
|
||||
_add(new configItem("sub_send_hwid", &sub_send_hwid, itemType::boolean));
|
||||
_add(new configItem("start_minimal", &start_minimal, itemType::boolean));
|
||||
|
||||
@ -14,11 +14,12 @@
|
||||
|
||||
namespace Configs_network {
|
||||
|
||||
HTTPResponse NetworkRequestHelper::HttpGet(const QString &url) {
|
||||
HTTPResponse NetworkRequestHelper::HttpGet(const QString &url, bool sendHwid) {
|
||||
QNetworkRequest request;
|
||||
QNetworkAccessManager accessManager;
|
||||
accessManager.setTransferTimeout(10000);
|
||||
request.setUrl(url);
|
||||
if (Configs::dataStore->sub_use_proxy || Configs::dataStore->spmode_system_proxy) {
|
||||
if (Configs::dataStore->net_use_proxy || Configs::dataStore->spmode_system_proxy) {
|
||||
if (Configs::dataStore->started_id < 0) {
|
||||
return HTTPResponse{QObject::tr("Request with proxy but no profile started.")};
|
||||
}
|
||||
@ -31,13 +32,13 @@ namespace Configs_network {
|
||||
// Set attribute
|
||||
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, Configs::dataStore->GetUserAgent());
|
||||
if (Configs::dataStore->sub_insecure) {
|
||||
if (Configs::dataStore->net_insecure) {
|
||||
QSslConfiguration c;
|
||||
c.setPeerVerifyMode(QSslSocket::PeerVerifyMode::VerifyNone);
|
||||
request.setSslConfiguration(c);
|
||||
}
|
||||
//Attach HWID and device info headers if enabled in settings
|
||||
if (Configs::dataStore->sub_send_hwid && !request.url().toString().contains("/throneproj/")) {
|
||||
if (sendHwid) {
|
||||
auto details = GetDeviceDetails();
|
||||
|
||||
if (!details.hwid.isEmpty()) request.setRawHeader("x-hwid", details.hwid.toUtf8());
|
||||
@ -52,23 +53,13 @@ namespace Configs_network {
|
||||
for (const auto &err: errors) {
|
||||
error_str << err.errorString();
|
||||
}
|
||||
MW_show_log(QString("SSL Errors: %1 %2").arg(error_str.join(","), Configs::dataStore->sub_insecure ? "(Ignored)" : ""));
|
||||
MW_show_log(QString("SSL Errors: %1 %2").arg(error_str.join(","), Configs::dataStore->net_insecure ? "(Ignored)" : ""));
|
||||
});
|
||||
// Wait for response
|
||||
auto abortTimer = new QTimer;
|
||||
abortTimer->setSingleShot(true);
|
||||
abortTimer->setInterval(10000);
|
||||
connect(abortTimer, &QTimer::timeout, _reply, &QNetworkReply::abort);
|
||||
abortTimer->start();
|
||||
{
|
||||
QEventLoop loop;
|
||||
connect(_reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
}
|
||||
if (abortTimer != nullptr) {
|
||||
abortTimer->stop();
|
||||
abortTimer->deleteLater();
|
||||
}
|
||||
|
||||
//
|
||||
auto result = HTTPResponse{_reply->error() == QNetworkReply::NetworkError::NoError ? "" : _reply->errorString(),
|
||||
_reply->readAll(), _reply->rawHeaderPairs()};
|
||||
@ -87,7 +78,7 @@ namespace Configs_network {
|
||||
QNetworkRequest request;
|
||||
QNetworkAccessManager accessManager;
|
||||
request.setUrl(url);
|
||||
if (Configs::dataStore->spmode_system_proxy) {
|
||||
if (Configs::dataStore->net_use_proxy || Configs::dataStore->spmode_system_proxy) {
|
||||
if (Configs::dataStore->started_id < 0) {
|
||||
return QObject::tr("Request with proxy but no profile started.");
|
||||
}
|
||||
@ -97,8 +88,11 @@ namespace Configs_network {
|
||||
p.setPort(Configs::dataStore->inbound_socks_port);
|
||||
accessManager.setProxy(p);
|
||||
}
|
||||
request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, Configs::dataStore->GetUserAgent());
|
||||
request.setRawHeader("Accept", "application/octet-stream");
|
||||
if (Configs::dataStore->net_insecure) {
|
||||
QSslConfiguration c;
|
||||
c.setPeerVerifyMode(QSslSocket::PeerVerifyMode::VerifyNone);
|
||||
request.setSslConfiguration(c);
|
||||
}
|
||||
|
||||
auto _reply = accessManager.get(request);
|
||||
connect(_reply, &QNetworkReply::sslErrors, _reply, [](const QList<QSslError> &errors) {
|
||||
@ -106,7 +100,7 @@ namespace Configs_network {
|
||||
for (const auto &err: errors) {
|
||||
error_str << err.errorString();
|
||||
}
|
||||
MW_show_log(QString("SSL Errors: %1").arg(error_str.join(",")));
|
||||
MW_show_log(QString("SSL Errors: %1 %2").arg(error_str.join(","), Configs::dataStore->net_insecure ? "(Ignored)" : ""));
|
||||
});
|
||||
connect(_reply, &QNetworkReply::downloadProgress, _reply, [&](qint64 bytesReceived, qint64 bytesTotal)
|
||||
{
|
||||
@ -123,6 +117,7 @@ namespace Configs_network {
|
||||
GetMainWindow()->setDownloadReport({}, false);
|
||||
GetMainWindow()->UpdateDataView(true);
|
||||
});
|
||||
_reply->deleteLater();
|
||||
if(_reply->error() != QNetworkReply::NetworkError::NoError) {
|
||||
return _reply->errorString();
|
||||
}
|
||||
@ -137,7 +132,6 @@ namespace Configs_network {
|
||||
}
|
||||
file.write(_reply->readAll());
|
||||
file.close();
|
||||
_reply->deleteLater();
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
@ -114,9 +114,9 @@ DialogBasicSettings::DialogBasicSettings(QWidget *parent)
|
||||
|
||||
ui->user_agent->setText(Configs::dataStore->user_agent);
|
||||
ui->user_agent->setPlaceholderText(Configs::dataStore->GetUserAgent(true));
|
||||
D_LOAD_BOOL(sub_use_proxy)
|
||||
D_LOAD_BOOL(net_use_proxy)
|
||||
D_LOAD_BOOL(sub_clear)
|
||||
D_LOAD_BOOL(sub_insecure)
|
||||
D_LOAD_BOOL(net_insecure)
|
||||
D_LOAD_BOOL(sub_send_hwid)
|
||||
D_LOAD_INT_ENABLE(sub_auto_update, sub_auto_update_enable)
|
||||
auto details = GetDeviceDetails();
|
||||
@ -206,9 +206,9 @@ void DialogBasicSettings::accept() {
|
||||
}
|
||||
|
||||
Configs::dataStore->user_agent = ui->user_agent->text();
|
||||
D_SAVE_BOOL(sub_use_proxy)
|
||||
D_SAVE_BOOL(net_use_proxy)
|
||||
D_SAVE_BOOL(sub_clear)
|
||||
D_SAVE_BOOL(sub_insecure)
|
||||
D_SAVE_BOOL(net_insecure)
|
||||
D_SAVE_BOOL(sub_send_hwid)
|
||||
D_SAVE_INT_ENABLE(sub_auto_update, sub_auto_update_enable)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user