Skip to content

Commit 389c0a4

Browse files
Add UserAgent configuration (#1212)
* feat: Set Lynx as default user agent and improve custom user agent handling * chore: Remove user agent debug logging * Add User Agent wiki link, remove whitespace --------- Co-authored-by: Ben Busby <[email protected]>
1 parent 041c1fb commit 389c0a4

File tree

4 files changed

+83
-49
lines changed

4 files changed

+83
-49
lines changed

app/models/config.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,12 @@ def get_rule_for_selector(stylesheet: CSSStyleSheet,
3737

3838
class Config:
3939
def __init__(self, **kwargs):
40-
app_config = current_app.config
41-
self.url = os.getenv('WHOOGLE_CONFIG_URL', '')
42-
self.lang_search = os.getenv('WHOOGLE_CONFIG_SEARCH_LANGUAGE', '')
43-
self.lang_interface = os.getenv('WHOOGLE_CONFIG_LANGUAGE', '')
44-
self.style_modified = os.getenv(
45-
'WHOOGLE_CONFIG_STYLE', '')
46-
self.block = os.getenv('WHOOGLE_CONFIG_BLOCK', '')
47-
self.block_title = os.getenv('WHOOGLE_CONFIG_BLOCK_TITLE', '')
48-
self.block_url = os.getenv('WHOOGLE_CONFIG_BLOCK_URL', '')
49-
self.country = os.getenv('WHOOGLE_CONFIG_COUNTRY', '')
50-
self.tbs = os.getenv('WHOOGLE_CONFIG_TIME_PERIOD', '')
51-
self.theme = os.getenv('WHOOGLE_CONFIG_THEME', 'system')
52-
self.safe = read_config_bool('WHOOGLE_CONFIG_SAFE')
53-
self.dark = read_config_bool('WHOOGLE_CONFIG_DARK') # deprecated
54-
self.alts = read_config_bool('WHOOGLE_CONFIG_ALTS')
55-
self.nojs = read_config_bool('WHOOGLE_CONFIG_NOJS')
56-
self.tor = read_config_bool('WHOOGLE_CONFIG_TOR')
57-
self.near = os.getenv('WHOOGLE_CONFIG_NEAR', '')
58-
self.new_tab = read_config_bool('WHOOGLE_CONFIG_NEW_TAB')
59-
self.view_image = read_config_bool('WHOOGLE_CONFIG_VIEW_IMAGE')
60-
self.get_only = read_config_bool('WHOOGLE_CONFIG_GET_ONLY')
61-
self.anon_view = read_config_bool('WHOOGLE_CONFIG_ANON_VIEW')
62-
self.preferences_encrypted = read_config_bool('WHOOGLE_CONFIG_PREFERENCES_ENCRYPTED')
63-
self.preferences_key = os.getenv('WHOOGLE_CONFIG_PREFERENCES_KEY', '')
64-
65-
self.accept_language = False
40+
# User agent configuration
41+
self.user_agent = kwargs.get('user_agent', 'LYNX_UA')
42+
self.custom_user_agent = kwargs.get('custom_user_agent', '')
43+
self.use_custom_user_agent = kwargs.get('use_custom_user_agent', False)
6644

45+
# Add user agent related keys to safe_keys
6746
self.safe_keys = [
6847
'lang_search',
6948
'lang_interface',
@@ -77,9 +56,39 @@ def __init__(self, **kwargs):
7756
'nojs',
7857
'anon_view',
7958
'preferences_encrypted',
80-
'tbs'
59+
'tbs',
60+
'user_agent',
61+
'custom_user_agent',
62+
'use_custom_user_agent'
8163
]
8264

65+
app_config = current_app.config
66+
self.url = kwargs.get('url', '')
67+
self.lang_search = kwargs.get('lang_search', '')
68+
self.lang_interface = kwargs.get('lang_interface', '')
69+
self.style_modified = os.getenv(
70+
'WHOOGLE_CONFIG_STYLE', '')
71+
self.block = os.getenv('WHOOGLE_CONFIG_BLOCK', '')
72+
self.block_title = os.getenv('WHOOGLE_CONFIG_BLOCK_TITLE', '')
73+
self.block_url = os.getenv('WHOOGLE_CONFIG_BLOCK_URL', '')
74+
self.country = os.getenv('WHOOGLE_CONFIG_COUNTRY', '')
75+
self.tbs = os.getenv('WHOOGLE_CONFIG_TIME_PERIOD', '')
76+
self.theme = kwargs.get('theme', '')
77+
self.safe = kwargs.get('safe', '')
78+
self.dark = kwargs.get('dark', '')
79+
self.alts = kwargs.get('alts', '')
80+
self.nojs = kwargs.get('nojs', '')
81+
self.tor = kwargs.get('tor', '')
82+
self.near = kwargs.get('near', '')
83+
self.new_tab = kwargs.get('new_tab', '')
84+
self.view_image = kwargs.get('view_image', '')
85+
self.get_only = kwargs.get('get_only', '')
86+
self.anon_view = read_config_bool('WHOOGLE_CONFIG_ANON_VIEW')
87+
self.preferences_encrypted = read_config_bool('WHOOGLE_CONFIG_PREFERENCES_ENCRYPTED')
88+
self.preferences_key = os.getenv('WHOOGLE_CONFIG_PREFERENCES_KEY', '')
89+
90+
self.accept_language = False
91+
8392
# Skip setting custom config if there isn't one
8493
if kwargs:
8594
mutable_attrs = self.get_mutable_attrs()

app/request.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
MOBILE_UA = '{}/5.0 (Android 0; Mobile; rv:54.0) Gecko/54.0 {}/59.0'
2020
DESKTOP_UA = '{}/5.0 (X11; {} x86_64; rv:75.0) Gecko/20100101 {}/75.0'
21-
LYNX_UA = 'Lynx/2.9.2 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/3.4.0'
2221

2322
# Valid query params
2423
VALID_PARAMS = ['tbs', 'tbm', 'start', 'near', 'source', 'nfpr']
@@ -73,20 +72,19 @@ def send_tor_signal(signal: Signal) -> bool:
7372
return False
7473

7574

76-
def gen_user_agent(is_mobile) -> str:
77-
if True:
78-
# Temporary fix while the removal of javascript-free searches by
79-
# Google is being investigated
80-
return LYNX_UA
75+
def gen_user_agent(config, is_mobile) -> str:
76+
# Define the Lynx user agent
77+
LYNX_UA = 'Lynx/2.9.2 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/3.4.0'
8178

82-
user_agent = os.environ.get('WHOOGLE_USER_AGENT', '')
83-
user_agent_mobile = os.environ.get('WHOOGLE_USER_AGENT_MOBILE', '')
84-
if user_agent and not is_mobile:
85-
return user_agent
79+
# If using custom user agent, return the custom string
80+
if config.user_agent == 'custom' and config.custom_user_agent:
81+
return config.custom_user_agent
8682

87-
if user_agent_mobile and is_mobile:
88-
return user_agent_mobile
83+
# If using Lynx user agent
84+
if config.user_agent == 'LYNX_UA':
85+
return LYNX_UA
8986

87+
# If no custom user agent is set, generate a random one
9088
firefox = random.choice(['Choir', 'Squier', 'Higher', 'Wire']) + 'fox'
9189
linux = random.choice(['Win', 'Sin', 'Gin', 'Fin', 'Kin']) + 'ux'
9290

@@ -198,10 +196,7 @@ def __init__(self, normal_ua, root_path, config: Config):
198196
# enable Tor for future requests
199197
send_tor_signal(Signal.HEARTBEAT)
200198

201-
self.language = (
202-
config.lang_search if config.lang_search else ''
203-
)
204-
199+
self.language = config.lang_search if config.lang_search else ''
205200
self.country = config.country if config.country else ''
206201

207202
# For setting Accept-language Header
@@ -211,11 +206,13 @@ def __init__(self, normal_ua, root_path, config: Config):
211206

212207
self.mobile = bool(normal_ua) and ('Android' in normal_ua
213208
or 'iPhone' in normal_ua)
214-
self.modified_user_agent = gen_user_agent(self.mobile)
209+
210+
# Generate user agent based on config
211+
self.modified_user_agent = gen_user_agent(config, self.mobile)
215212
if not self.mobile:
216-
self.modified_user_agent_mobile = gen_user_agent(True)
213+
self.modified_user_agent_mobile = gen_user_agent(config, True)
217214

218-
# Set up proxy, if previously configured
215+
# Set up proxy configuration
219216
proxy_path = os.environ.get('WHOOGLE_PROXY_LOC', '')
220217
if proxy_path:
221218
proxy_type = os.environ.get('WHOOGLE_PROXY_TYPE', '')
@@ -235,6 +232,7 @@ def __init__(self, normal_ua, root_path, config: Config):
235232
'http': 'socks5://127.0.0.1:9050',
236233
'https': 'socks5://127.0.0.1:9050'
237234
} if config.tor else {}
235+
238236
self.tor = config.tor
239237
self.tor_valid = False
240238
self.root_path = root_path

app/routes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ def config():
438438
if name:
439439
config_pkl = os.path.join(app.config['CONFIG_PATH'], name)
440440
session['config'] = (pickle.load(open(config_pkl, 'rb'))
441-
if os.path.exists(config_pkl)
442-
else session['config'])
441+
if os.path.exists(config_pkl)
442+
else session['config'])
443443
return json.dumps(session['config'])
444444
else:
445445
return json.dumps({})
@@ -448,8 +448,20 @@ def config():
448448
if 'url' not in config_data or not config_data['url']:
449449
config_data['url'] = g.user_config.url
450450

451+
# Handle user agent configuration
452+
if 'user_agent' in config_data:
453+
if config_data['user_agent'] == 'custom':
454+
config_data['use_custom_user_agent'] = True
455+
# Keep both the selection and the custom string
456+
if 'custom_user_agent' in config_data:
457+
config_data['custom_user_agent'] = config_data['custom_user_agent']
458+
print(f"Setting custom user agent to: {config_data['custom_user_agent']}") # Debug log
459+
else:
460+
config_data['use_custom_user_agent'] = False
461+
config_data['custom_user_agent'] = ''
462+
451463
# Save config by name to allow a user to easily load later
452-
if 'name' in request.args:
464+
if name:
453465
pickle.dump(
454466
config_data,
455467
open(os.path.join(

app/templates/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,21 @@
228228
<input type="checkbox" name="get_only"
229229
id="config-get-only" {{ 'checked' if config.get_only else '' }}>
230230
</div>
231+
<div class="config-div config-div-user-agent">
232+
<label for="config-user-agent">User Agent: </label>
233+
<select name="user_agent" id="config-user-agent">
234+
<option value="LYNX_UA" {% if not config.user_agent or config.user_agent == 'LYNX_UA' %}selected{% endif %}>Lynx Browser</option>
235+
<option value="" {% if config.user_agent == '' and config.user_agent != 'LYNX_UA' %}selected{% endif %}>Original (Random)</option>
236+
<option value="custom" {% if config.user_agent == 'custom' %}selected{% endif %}>Custom</option>
237+
</select>
238+
</div>
239+
<div class="config-div config-div-custom-user-agent" {% if config.user_agent != 'custom' %}style="display: none;"{% endif %}>
240+
<label for="config-custom-user-agent">Custom User Agent: </label>
241+
<input type="text" name="custom_user_agent" id="config-custom-user-agent"
242+
value="{{ config.custom_user_agent }}"
243+
placeholder="Enter custom user agent string">
244+
<div><span class="info-text"><a href="https://github.com/benbusby/whoogle-search/wiki/User-Agents">User Agent Wiki</a></span></div>
245+
</div>
231246
<div class="config-div config-div-accept-language">
232247
<label for="config-accept-language">Set Accept-Language: </label>
233248
<input type="checkbox" name="accept_language"

0 commit comments

Comments
 (0)