mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-31 03:50:20 -07:00
Adjust user agent version automatically
The version calculation is an estimation and it will drift off after some time. Hopefully the drift offset won't be noticeable within a few years. Also switched the user agent to Windows 10 which has the largest portion of users to avoid standing out from the crowd. PR #20864.
This commit is contained in:
parent
b8a774f1fb
commit
a126a7b493
3 changed files with 44 additions and 8 deletions
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <QByteArray>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
|
@ -54,8 +55,27 @@ using namespace std::chrono_literals;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// Disguise as Firefox to avoid web server banning
|
// Disguise as browser to circumvent website blocking
|
||||||
const char DEFAULT_USER_AGENT[] = "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0";
|
QByteArray getBrowserUserAgent()
|
||||||
|
{
|
||||||
|
// Firefox release calendar
|
||||||
|
// https://whattrainisitnow.com/calendar/
|
||||||
|
// https://wiki.mozilla.org/index.php?title=Release_Management/Calendar&redirect=no
|
||||||
|
|
||||||
|
static QByteArray ret;
|
||||||
|
if (ret.isEmpty())
|
||||||
|
{
|
||||||
|
const std::chrono::time_point baseDate = std::chrono::sys_days(2024y / 04 / 16);
|
||||||
|
const int baseVersion = 125;
|
||||||
|
|
||||||
|
const std::chrono::time_point nowDate = std::chrono::system_clock::now();
|
||||||
|
const int nowVersion = baseVersion + std::chrono::duration_cast<std::chrono::months>(nowDate - baseDate).count();
|
||||||
|
|
||||||
|
QByteArray userAgentTemplate = QByteArrayLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:%1.0) Gecko/20100101 Firefox/%1.0");
|
||||||
|
ret = userAgentTemplate.replace("%1", QByteArray::number(nowVersion));
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Net::DownloadManager::NetworkCookieJar final : public QNetworkCookieJar
|
class Net::DownloadManager::NetworkCookieJar final : public QNetworkCookieJar
|
||||||
|
@ -292,7 +312,7 @@ void Net::DownloadManager::processRequest(DownloadHandlerImpl *downloadHandler)
|
||||||
QNetworkRequest request {downloadRequest.url()};
|
QNetworkRequest request {downloadRequest.url()};
|
||||||
|
|
||||||
if (downloadRequest.userAgent().isEmpty())
|
if (downloadRequest.userAgent().isEmpty())
|
||||||
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
|
request.setRawHeader("User-Agent", getBrowserUserAgent());
|
||||||
else
|
else
|
||||||
request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());
|
request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());
|
||||||
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
pass
|
|
|
@ -1,4 +1,4 @@
|
||||||
#VERSION: 1.44
|
#VERSION: 1.45
|
||||||
|
|
||||||
# Author:
|
# Author:
|
||||||
# Christophe DUMEZ (chris@qbittorrent.org)
|
# Christophe DUMEZ (chris@qbittorrent.org)
|
||||||
|
@ -27,6 +27,7 @@
|
||||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
import datetime
|
||||||
import gzip
|
import gzip
|
||||||
import html.entities
|
import html.entities
|
||||||
import io
|
import io
|
||||||
|
@ -39,9 +40,25 @@ import urllib.error
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
# Some sites blocks default python User-agent
|
|
||||||
user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0'
|
def getBrowserUserAgent():
|
||||||
headers = {'User-Agent': user_agent}
|
""" Disguise as browser to circumvent website blocking """
|
||||||
|
|
||||||
|
# Firefox release calendar
|
||||||
|
# https://whattrainisitnow.com/calendar/
|
||||||
|
# https://wiki.mozilla.org/index.php?title=Release_Management/Calendar&redirect=no
|
||||||
|
|
||||||
|
baseDate = datetime.date(2024, 4, 16)
|
||||||
|
baseVersion = 125
|
||||||
|
|
||||||
|
nowDate = datetime.date.today()
|
||||||
|
nowVersion = baseVersion + ((nowDate - baseDate).days // 30)
|
||||||
|
|
||||||
|
return f"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:{nowVersion}.0) Gecko/20100101 Firefox/{nowVersion}.0"
|
||||||
|
|
||||||
|
|
||||||
|
headers = {'User-Agent': getBrowserUserAgent()}
|
||||||
|
|
||||||
# SOCKS5 Proxy support
|
# SOCKS5 Proxy support
|
||||||
if "sock_proxy" in os.environ and len(os.environ["sock_proxy"].strip()) > 0:
|
if "sock_proxy" in os.environ and len(os.environ["sock_proxy"].strip()) > 0:
|
||||||
proxy_str = os.environ["sock_proxy"].strip()
|
proxy_str = os.environ["sock_proxy"].strip()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue