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:
Chocobo1 2024-05-20 13:50:18 +08:00 committed by GitHub
parent b8a774f1fb
commit a126a7b493
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 8 deletions

View file

@ -32,6 +32,7 @@
#include <algorithm>
#include <QByteArray>
#include <QDateTime>
#include <QDebug>
#include <QNetworkAccessManager>
@ -54,8 +55,27 @@ using namespace std::chrono_literals;
namespace
{
// Disguise as Firefox to avoid web server banning
const char DEFAULT_USER_AGENT[] = "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0";
// Disguise as browser to circumvent website blocking
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
@ -292,7 +312,7 @@ void Net::DownloadManager::processRequest(DownloadHandlerImpl *downloadHandler)
QNetworkRequest request {downloadRequest.url()};
if (downloadRequest.userAgent().isEmpty())
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
request.setRawHeader("User-Agent", getBrowserUserAgent());
else
request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());

View file

@ -1 +0,0 @@
pass

View file

@ -1,4 +1,4 @@
#VERSION: 1.44
#VERSION: 1.45
# Author:
# 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
# POSSIBILITY OF SUCH DAMAGE.
import datetime
import gzip
import html.entities
import io
@ -39,9 +40,25 @@ import urllib.error
import urllib.parse
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'
headers = {'User-Agent': user_agent}
def getBrowserUserAgent():
""" 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
if "sock_proxy" in os.environ and len(os.environ["sock_proxy"].strip()) > 0:
proxy_str = os.environ["sock_proxy"].strip()