From 73de5616e8f57cea3a53b1ffe0c7388d7cf26ae7 Mon Sep 17 00:00:00 2001 From: Gordon Shumway Date: Sat, 11 Jul 2020 04:25:09 -0400 Subject: [PATCH] Selectable User-Agent for HTTP requests (#1423) https://github.com/ProgVal/Limnoria/issues/1382 --- src/conf.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/conf.py b/src/conf.py index 2ac61b121..9b37ef118 100644 --- a/src/conf.py +++ b/src/conf.py @@ -32,6 +32,7 @@ import os import sys import time import socket +import random from . import ircutils, registry, utils from .utils import minisix @@ -1330,13 +1331,17 @@ def defaultHttpHeaders(network, channel): try: language = supybot.protocols.http.requestLanguage.getSpecific( network, channel)() + agent = random.choice(supybot.protocols.http.userAgents.getSpecific( + network, channel)()) except registry.NonExistentRegistryEntry: - pass # Starting up; language will be set by HttpRequestLanguage later + pass # Starting up; headers will be set by HttpRequestLanguage/UserAgents later else: if language: headers['Accept-Language'] = language elif 'Accept-Language' in headers: del headers['Accept-Language'] + if agent.strip(): + headers['User-agent'] = agent return headers class HttpRequestLanguage(registry.String): @@ -1346,12 +1351,23 @@ class HttpRequestLanguage(registry.String): super(HttpRequestLanguage, self).setValue(v) utils.web.defaultHeaders = defaultHttpHeaders(None, None) +class HttpUserAgents(registry.CommaSeparatedListOfStrings): + """Must be a valid HTTP User-Agent value.""" + __slots__ = () + def setValue(self, v): + super(HttpUserAgent, self).setValue(v) + utils.web.defaultHeaders = defaultHttpHeaders(None, None) + registerChannelValue(supybot.protocols.http, 'requestLanguage', HttpRequestLanguage('', _("""If set, the Accept-Language HTTP header will be set to this value for requests. Useful for overriding the auto-detected language based on the server's location."""))) +registerChannelValue(supybot.protocols.http, 'userAgents', + HttpUserAgents([], _("""If set, the User-Agent HTTP header will be set to a randomly + selected value from this comma-separated list of strings for requests."""))) + ### # supybot.protocols.ssl ###