Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
from app.models.config import Config
|
2022-06-16 17:05:41 +00:00
|
|
|
from app.utils.misc import read_config_bool
|
2025-09-21 05:11:54 +00:00
|
|
|
from app.services.provider import get_http_client
|
2025-11-24 02:35:08 +00:00
|
|
|
from app.utils.ua_generator import load_ua_pool, get_random_ua, DEFAULT_FALLBACK_UA
|
2022-03-01 19:54:32 +00:00
|
|
|
from defusedxml import ElementTree as ET
|
2025-09-21 05:11:54 +00:00
|
|
|
import httpx
|
2020-04-24 02:59:43 +00:00
|
|
|
import urllib.parse as urlparse
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
import os
|
|
|
|
|
from stem import Signal, SocketError
|
2022-06-22 16:23:58 +00:00
|
|
|
from stem.connection import AuthenticationFailure
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
from stem.control import Controller
|
2022-06-16 17:05:41 +00:00
|
|
|
from stem.connection import authenticate_cookie, authenticate_password
|
2020-04-24 02:59:43 +00:00
|
|
|
|
2021-05-27 16:01:57 +00:00
|
|
|
MAPS_URL = 'https://maps.google.com/maps'
|
2020-12-17 21:06:47 +00:00
|
|
|
AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/'
|
|
|
|
|
'complete/search?client=toolbar&')
|
2020-04-24 02:59:43 +00:00
|
|
|
|
2020-04-29 00:19:34 +00:00
|
|
|
# Valid query params
|
2020-07-26 17:53:59 +00:00
|
|
|
VALID_PARAMS = ['tbs', 'tbm', 'start', 'near', 'source', 'nfpr']
|
2020-04-29 00:19:34 +00:00
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
class TorError(Exception):
|
|
|
|
|
"""Exception raised for errors in Tor requests.
|
|
|
|
|
|
|
|
|
|
Attributes:
|
2021-03-08 17:22:04 +00:00
|
|
|
message: a message describing the error that occurred
|
|
|
|
|
disable: optionally disables Tor in the user config (note:
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
this should only happen if the connection has been dropped
|
|
|
|
|
altogether).
|
|
|
|
|
"""
|
|
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def __init__(self, message, disable=False) -> None:
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
self.message = message
|
|
|
|
|
self.disable = disable
|
2021-03-24 19:13:52 +00:00
|
|
|
super().__init__(message)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_tor_signal(signal: Signal) -> bool:
|
2022-06-16 17:05:41 +00:00
|
|
|
use_pass = read_config_bool('WHOOGLE_TOR_USE_PASS')
|
|
|
|
|
|
|
|
|
|
confloc = './misc/tor/control.conf'
|
|
|
|
|
# Check that the custom location of conf is real.
|
|
|
|
|
temp = os.getenv('WHOOGLE_TOR_CONF', '')
|
|
|
|
|
if os.path.isfile(temp):
|
|
|
|
|
confloc = temp
|
|
|
|
|
|
|
|
|
|
# Attempt to authenticate and send signal.
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
try:
|
|
|
|
|
with Controller.from_port(port=9051) as c:
|
2022-06-16 17:05:41 +00:00
|
|
|
if use_pass:
|
|
|
|
|
with open(confloc, "r") as conf:
|
|
|
|
|
# Scan for the last line of the file.
|
|
|
|
|
for line in conf:
|
|
|
|
|
pass
|
2022-07-06 15:52:02 +00:00
|
|
|
secret = line.strip('\n')
|
2022-06-16 17:05:41 +00:00
|
|
|
authenticate_password(c, password=secret)
|
|
|
|
|
else:
|
|
|
|
|
cookie_path = '/var/lib/tor/control_auth_cookie'
|
|
|
|
|
authenticate_cookie(c, cookie_path=cookie_path)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
c.signal(signal)
|
|
|
|
|
os.environ['TOR_AVAILABLE'] = '1'
|
|
|
|
|
return True
|
2022-06-22 16:23:58 +00:00
|
|
|
except (SocketError, AuthenticationFailure,
|
|
|
|
|
ConnectionRefusedError, ConnectionError):
|
2022-07-13 16:08:44 +00:00
|
|
|
# TODO: Handle Tor authentication (password and cookie)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
os.environ['TOR_AVAILABLE'] = '0'
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2025-01-17 17:17:50 +00:00
|
|
|
def gen_user_agent(config, is_mobile) -> str:
|
|
|
|
|
# If using custom user agent, return the custom string
|
|
|
|
|
if config.user_agent == 'custom' and config.custom_user_agent:
|
|
|
|
|
return config.custom_user_agent
|
2023-05-19 17:32:05 +00:00
|
|
|
|
2025-09-13 01:24:44 +00:00
|
|
|
# If using environment configuration
|
|
|
|
|
if config.user_agent == 'env_conf':
|
|
|
|
|
if is_mobile:
|
|
|
|
|
env_ua = os.getenv('WHOOGLE_USER_AGENT_MOBILE', '')
|
|
|
|
|
if env_ua:
|
|
|
|
|
return env_ua
|
|
|
|
|
else:
|
|
|
|
|
env_ua = os.getenv('WHOOGLE_USER_AGENT', '')
|
|
|
|
|
if env_ua:
|
|
|
|
|
return env_ua
|
2025-11-24 04:16:53 +00:00
|
|
|
# If env vars are not set, fall back to Opera UA
|
|
|
|
|
return DEFAULT_FALLBACK_UA
|
2025-09-13 01:24:44 +00:00
|
|
|
|
2025-11-24 02:35:08 +00:00
|
|
|
# If using default user agent - use auto-generated Opera UA pool
|
2025-09-13 01:24:44 +00:00
|
|
|
if config.user_agent == 'default':
|
2025-11-24 02:35:08 +00:00
|
|
|
try:
|
|
|
|
|
# Try to load UA pool from cache (lazy loading if not in app.config)
|
|
|
|
|
# First check if we have access to Flask app context
|
|
|
|
|
try:
|
|
|
|
|
from flask import current_app
|
|
|
|
|
if hasattr(current_app, 'config') and 'UA_POOL' in current_app.config:
|
|
|
|
|
ua_pool = current_app.config['UA_POOL']
|
|
|
|
|
else:
|
|
|
|
|
# Fall back to loading from disk
|
2025-11-24 04:16:53 +00:00
|
|
|
raise ImportError("UA_POOL not in app config")
|
2025-11-24 02:35:08 +00:00
|
|
|
except (ImportError, RuntimeError):
|
2025-11-24 04:16:53 +00:00
|
|
|
# No Flask context available or UA_POOL not in config, load from disk
|
2025-11-24 02:35:08 +00:00
|
|
|
config_path = os.environ.get('CONFIG_VOLUME',
|
|
|
|
|
os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
|
'static', 'config'))
|
|
|
|
|
cache_path = os.path.join(config_path, 'ua_cache.json')
|
|
|
|
|
ua_pool = load_ua_pool(cache_path, count=10)
|
|
|
|
|
|
|
|
|
|
return get_random_ua(ua_pool)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# If anything goes wrong, fall back to default Opera UA
|
|
|
|
|
print(f"Warning: Could not load UA pool, using fallback Opera UA: {e}")
|
|
|
|
|
return DEFAULT_FALLBACK_UA
|
2025-09-13 01:24:44 +00:00
|
|
|
|
2025-11-24 04:16:53 +00:00
|
|
|
# Fallback for backwards compatibility (old configs or invalid user_agent values)
|
|
|
|
|
return DEFAULT_FALLBACK_UA
|
2020-04-24 02:59:43 +00:00
|
|
|
|
|
|
|
|
|
2021-12-07 04:39:50 +00:00
|
|
|
def gen_query(query, args, config) -> str:
|
2020-04-30 00:53:58 +00:00
|
|
|
param_dict = {key: '' for key in VALID_PARAMS}
|
2020-06-07 20:06:49 +00:00
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
# Use :past(hour/day/week/month/year) if available
|
|
|
|
|
# example search "new restaurants :past month"
|
2021-03-24 19:13:52 +00:00
|
|
|
lang = ''
|
2020-06-07 20:06:49 +00:00
|
|
|
if ':past' in query and 'tbs' not in args:
|
2020-04-29 00:59:33 +00:00
|
|
|
time_range = str.strip(query.split(':past', 1)[-1])
|
2020-06-07 20:06:49 +00:00
|
|
|
param_dict['tbs'] = '&tbs=' + ('qdr:' + str.lower(time_range[0]))
|
2022-12-21 20:24:27 +00:00
|
|
|
elif 'tbs' in args or 'tbs' in config:
|
|
|
|
|
result_tbs = args.get('tbs') if 'tbs' in args else config['tbs']
|
2020-06-07 20:06:49 +00:00
|
|
|
param_dict['tbs'] = '&tbs=' + result_tbs
|
|
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
# Occasionally the 'tbs' param provided by google also contains a
|
|
|
|
|
# field for 'lr', but formatted strangely. This is a rough solution
|
|
|
|
|
# for this.
|
|
|
|
|
#
|
|
|
|
|
# Example:
|
|
|
|
|
# &tbs=qdr:h,lr:lang_1pl
|
|
|
|
|
# -- the lr param needs to be extracted and remove the leading '1'
|
2021-03-24 19:13:52 +00:00
|
|
|
result_params = [_ for _ in result_tbs.split(',') if 'lr:' in _]
|
|
|
|
|
if len(result_params) > 0:
|
|
|
|
|
result_param = result_params[0]
|
|
|
|
|
lang = result_param[result_param.find('lr:') + 3:len(result_param)]
|
2020-04-24 02:59:43 +00:00
|
|
|
|
|
|
|
|
# Ensure search query is parsable
|
2020-04-29 00:59:33 +00:00
|
|
|
query = urlparse.quote(query)
|
2020-04-24 02:59:43 +00:00
|
|
|
|
|
|
|
|
# Pass along type of results (news, images, books, etc)
|
|
|
|
|
if 'tbm' in args:
|
2020-04-30 00:53:58 +00:00
|
|
|
param_dict['tbm'] = '&tbm=' + args.get('tbm')
|
2025-11-27 04:21:23 +00:00
|
|
|
# Google Images now expects the modern udm=2 layout; force it when
|
|
|
|
|
# requesting images to avoid redirects to the new AI/text layout.
|
|
|
|
|
if args.get('tbm') == 'isch' and 'udm' not in args:
|
|
|
|
|
param_dict['udm'] = '&udm=2'
|
2020-04-24 02:59:43 +00:00
|
|
|
|
|
|
|
|
# Get results page start value (10 per page, ie page 2 start val = 20)
|
|
|
|
|
if 'start' in args:
|
2020-04-30 00:53:58 +00:00
|
|
|
param_dict['start'] = '&start=' + args.get('start')
|
2020-04-24 02:59:43 +00:00
|
|
|
|
|
|
|
|
# Search for results near a particular city, if available
|
2021-12-07 04:39:50 +00:00
|
|
|
if config.near:
|
|
|
|
|
param_dict['near'] = '&near=' + urlparse.quote(config.near)
|
2020-04-24 02:59:43 +00:00
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
# Set language for results (lr) if source isn't set, otherwise use the
|
|
|
|
|
# result language param provided in the results
|
2020-06-07 20:06:49 +00:00
|
|
|
if 'source' in args:
|
|
|
|
|
param_dict['source'] = '&source=' + args.get('source')
|
2020-12-17 21:06:47 +00:00
|
|
|
param_dict['lr'] = ('&lr=' + ''.join(
|
2021-03-24 19:13:52 +00:00
|
|
|
[_ for _ in lang if not _.isdigit()]
|
|
|
|
|
)) if lang else ''
|
2020-06-07 20:06:49 +00:00
|
|
|
else:
|
2021-12-24 00:01:49 +00:00
|
|
|
param_dict['lr'] = (
|
|
|
|
|
'&lr=' + config.lang_search
|
|
|
|
|
) if config.lang_search else ''
|
2020-06-07 20:06:49 +00:00
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
# 'nfpr' defines the exclusion of results from an auto-corrected query
|
2020-07-26 17:53:59 +00:00
|
|
|
if 'nfpr' in args:
|
|
|
|
|
param_dict['nfpr'] = '&nfpr=' + args.get('nfpr')
|
|
|
|
|
|
2021-11-17 23:17:27 +00:00
|
|
|
# 'chips' is used in image tabs to pass the optional 'filter' to add to the
|
|
|
|
|
# given search term
|
|
|
|
|
if 'chips' in args:
|
|
|
|
|
param_dict['chips'] = '&chips=' + args.get('chips')
|
|
|
|
|
|
2021-12-24 00:01:49 +00:00
|
|
|
param_dict['gl'] = (
|
|
|
|
|
'&gl=' + config.country
|
|
|
|
|
) if config.country else ''
|
|
|
|
|
param_dict['hl'] = (
|
|
|
|
|
'&hl=' + config.lang_interface.replace('lang_', '')
|
|
|
|
|
) if config.lang_interface else ''
|
2020-05-23 20:27:23 +00:00
|
|
|
param_dict['safe'] = '&safe=' + ('active' if config.safe else 'off')
|
2020-05-12 23:15:53 +00:00
|
|
|
|
2021-05-07 15:45:53 +00:00
|
|
|
# Block all sites specified in the user config
|
2021-06-04 15:09:30 +00:00
|
|
|
unquoted_query = urlparse.unquote(query)
|
|
|
|
|
for blocked_site in config.block.replace(' ', '').split(','):
|
|
|
|
|
if not blocked_site:
|
|
|
|
|
continue
|
|
|
|
|
block = (' -site:' + blocked_site)
|
|
|
|
|
query += block if block not in unquoted_query else ''
|
2021-05-07 15:45:53 +00:00
|
|
|
|
2020-04-30 00:53:58 +00:00
|
|
|
for val in param_dict.values():
|
2020-06-07 20:06:49 +00:00
|
|
|
if not val:
|
2020-04-29 00:59:33 +00:00
|
|
|
continue
|
|
|
|
|
query += val
|
|
|
|
|
|
|
|
|
|
return query
|
2020-04-24 02:59:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Request:
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
"""Class used for handling all outbound requests, including search queries,
|
|
|
|
|
search suggestions, and loading of external content (images, audio, etc).
|
|
|
|
|
|
|
|
|
|
Attributes:
|
2021-03-08 17:22:04 +00:00
|
|
|
normal_ua: the user's current user agent
|
|
|
|
|
root_path: the root path of the whoogle instance
|
|
|
|
|
config: the user's current whoogle configuration
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
"""
|
2020-12-17 21:06:47 +00:00
|
|
|
|
2025-09-21 05:11:54 +00:00
|
|
|
def __init__(self, normal_ua, root_path, config: Config, http_client=None):
|
2025-12-29 15:43:20 +00:00
|
|
|
self.search_url = 'https://www.google.com/search?gbv=1&q='
|
2025-11-27 04:21:23 +00:00
|
|
|
# Google Images rejects the lightweight gbv=1 interface. Use the
|
|
|
|
|
# modern udm=2 entrypoint specifically for image searches to avoid the
|
|
|
|
|
# "update your browser" interstitial.
|
2025-12-29 15:43:20 +00:00
|
|
|
self.image_search_url = 'https://www.google.com/search?udm=2&q='
|
2025-09-24 03:14:41 +00:00
|
|
|
# Optionally send heartbeat to Tor to determine availability
|
|
|
|
|
# Only when Tor is enabled in config to avoid unnecessary socket usage
|
|
|
|
|
if config.tor:
|
|
|
|
|
send_tor_signal(Signal.HEARTBEAT)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
2025-01-17 17:17:50 +00:00
|
|
|
self.language = config.lang_search if config.lang_search else ''
|
2022-10-26 16:20:21 +00:00
|
|
|
self.country = config.country if config.country else ''
|
|
|
|
|
|
2021-10-25 21:49:09 +00:00
|
|
|
# For setting Accept-language Header
|
|
|
|
|
self.lang_interface = ''
|
|
|
|
|
if config.accept_language:
|
|
|
|
|
self.lang_interface = config.lang_interface
|
|
|
|
|
|
2021-06-16 14:37:33 +00:00
|
|
|
self.mobile = bool(normal_ua) and ('Android' in normal_ua
|
|
|
|
|
or 'iPhone' in normal_ua)
|
2025-01-17 17:17:50 +00:00
|
|
|
|
|
|
|
|
# Generate user agent based on config
|
|
|
|
|
self.modified_user_agent = gen_user_agent(config, self.mobile)
|
2021-04-16 14:16:14 +00:00
|
|
|
if not self.mobile:
|
2025-01-17 17:17:50 +00:00
|
|
|
self.modified_user_agent_mobile = gen_user_agent(config, True)
|
2020-04-24 02:59:43 +00:00
|
|
|
|
2025-11-27 04:21:23 +00:00
|
|
|
# Dedicated modern UA to use when Google rejects legacy ones (e.g. Images)
|
|
|
|
|
self.image_user_agent = (
|
|
|
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
|
|
|
|
|
'AppleWebKit/537.36 (KHTML, like Gecko) '
|
|
|
|
|
'Chrome/127.0.0.0 Safari/537.36'
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-17 17:17:50 +00:00
|
|
|
# Set up proxy configuration
|
2021-06-04 19:30:21 +00:00
|
|
|
proxy_path = os.environ.get('WHOOGLE_PROXY_LOC', '')
|
|
|
|
|
if proxy_path:
|
|
|
|
|
proxy_type = os.environ.get('WHOOGLE_PROXY_TYPE', '')
|
|
|
|
|
proxy_user = os.environ.get('WHOOGLE_PROXY_USER', '')
|
|
|
|
|
proxy_pass = os.environ.get('WHOOGLE_PROXY_PASS', '')
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
auth_str = ''
|
2021-06-04 19:30:21 +00:00
|
|
|
if proxy_user:
|
2022-12-12 23:40:15 +00:00
|
|
|
auth_str = f'{proxy_user}:{proxy_pass}@'
|
|
|
|
|
|
|
|
|
|
proxy_str = f'{proxy_type}://{auth_str}{proxy_path}'
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
self.proxies = {
|
2022-12-12 23:40:15 +00:00
|
|
|
'https': proxy_str,
|
|
|
|
|
'http': proxy_str
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
self.proxies = {
|
|
|
|
|
'http': 'socks5://127.0.0.1:9050',
|
|
|
|
|
'https': 'socks5://127.0.0.1:9050'
|
|
|
|
|
} if config.tor else {}
|
2025-01-17 17:17:50 +00:00
|
|
|
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
self.tor = config.tor
|
|
|
|
|
self.tor_valid = False
|
|
|
|
|
self.root_path = root_path
|
2025-09-21 05:11:54 +00:00
|
|
|
# Initialize HTTP client (shared per proxies)
|
|
|
|
|
self.http_client = http_client or get_http_client(self.proxies)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
def __getitem__(self, name):
|
|
|
|
|
return getattr(self, name)
|
|
|
|
|
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
def autocomplete(self, query) -> list:
|
|
|
|
|
"""Sends a query to Google's search suggestion service
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
query: The in-progress query to send
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list: The list of matches for possible search suggestions
|
|
|
|
|
|
|
|
|
|
"""
|
2025-09-24 02:37:21 +00:00
|
|
|
# Check if autocomplete is disabled via environment variable
|
|
|
|
|
if os.environ.get('WHOOGLE_AUTOCOMPLETE', '1') == '0':
|
2020-12-29 23:43:42 +00:00
|
|
|
return []
|
2025-09-24 02:37:21 +00:00
|
|
|
|
2021-12-28 18:38:18 +00:00
|
|
|
try:
|
2025-09-24 02:37:21 +00:00
|
|
|
ac_query = dict(q=query)
|
|
|
|
|
if self.language:
|
|
|
|
|
ac_query['lr'] = self.language
|
|
|
|
|
if self.country:
|
|
|
|
|
ac_query['gl'] = self.country
|
|
|
|
|
if self.lang_interface:
|
|
|
|
|
ac_query['hl'] = self.lang_interface
|
|
|
|
|
|
|
|
|
|
response = self.send(base_url=AUTOCOMPLETE_URL,
|
|
|
|
|
query=urlparse.urlencode(ac_query)).text
|
|
|
|
|
|
|
|
|
|
if not response:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
root = ET.fromstring(response)
|
|
|
|
|
return [_.attrib['data'] for _ in
|
|
|
|
|
root.findall('.//suggestion/[@data]')]
|
|
|
|
|
except ET.ParseError:
|
|
|
|
|
# Malformed XML response
|
|
|
|
|
return []
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# Log the error but don't crash - autocomplete is non-essential
|
|
|
|
|
print(f"Autocomplete error: {str(e)}")
|
2021-12-28 18:38:18 +00:00
|
|
|
return []
|
2020-05-24 20:03:11 +00:00
|
|
|
|
2021-10-27 17:02:14 +00:00
|
|
|
def send(self, base_url='', query='', attempt=0,
|
2025-09-21 05:11:54 +00:00
|
|
|
force_mobile=False, user_agent=''):
|
2020-12-17 21:06:47 +00:00
|
|
|
"""Sends an outbound request to a URL. Optionally sends the request
|
|
|
|
|
using Tor, if enabled by the user.
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
base_url: The URL to use in the request
|
|
|
|
|
query: The optional query string for the request
|
2020-12-17 21:06:47 +00:00
|
|
|
attempt: The number of attempts made for the request
|
|
|
|
|
(used for cycling through Tor identities, if enabled)
|
2021-06-04 15:09:30 +00:00
|
|
|
force_mobile: Optional flag to enable a mobile user agent
|
|
|
|
|
(used for fetching full size images in search results)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Response: The Response object returned by the requests call
|
|
|
|
|
|
|
|
|
|
"""
|
2023-05-19 17:32:05 +00:00
|
|
|
use_client_user_agent = int(os.environ.get('WHOOGLE_USE_CLIENT_USER_AGENT', '0'))
|
|
|
|
|
if user_agent and use_client_user_agent == 1:
|
|
|
|
|
modified_user_agent = user_agent
|
2021-04-16 14:16:14 +00:00
|
|
|
else:
|
2023-05-19 17:32:05 +00:00
|
|
|
if force_mobile and not self.mobile:
|
|
|
|
|
modified_user_agent = self.modified_user_agent_mobile
|
|
|
|
|
else:
|
|
|
|
|
modified_user_agent = self.modified_user_agent
|
2021-04-16 14:16:14 +00:00
|
|
|
|
2025-11-27 04:21:23 +00:00
|
|
|
# Some Google endpoints (notably Images) now refuse legacy user agents.
|
|
|
|
|
# If an image search is detected and the generated UA isn't Chromium-
|
|
|
|
|
# like, retry with a modern Chrome string to avoid the "update your
|
|
|
|
|
# browser" interstitial.
|
|
|
|
|
if (('tbm=isch' in query) or ('udm=2' in query)) and 'Chrome' not in modified_user_agent:
|
|
|
|
|
modified_user_agent = self.image_user_agent
|
|
|
|
|
|
2020-06-02 18:54:47 +00:00
|
|
|
headers = {
|
2025-11-24 02:35:08 +00:00
|
|
|
'User-Agent': modified_user_agent,
|
|
|
|
|
'Accept': ('text/html,application/xhtml+xml,application/xml;'
|
|
|
|
|
'q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8'),
|
|
|
|
|
'Accept-Language': 'en-US,en;q=0.9',
|
|
|
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
|
'Connection': 'keep-alive',
|
|
|
|
|
'Cache-Control': 'max-age=0',
|
|
|
|
|
'Pragma': 'no-cache',
|
|
|
|
|
'Upgrade-Insecure-Requests': '1',
|
|
|
|
|
'Sec-Fetch-Site': 'none',
|
|
|
|
|
'Sec-Fetch-Mode': 'navigate',
|
|
|
|
|
'Sec-Fetch-User': '?1',
|
2025-11-27 04:21:23 +00:00
|
|
|
'Sec-Fetch-Dest': 'document'
|
2020-06-02 18:54:47 +00:00
|
|
|
}
|
2025-11-27 04:21:23 +00:00
|
|
|
# Only attach client hints when using a Chromium-like user agent to
|
|
|
|
|
# avoid sending conflicting information that can trigger unsupported
|
|
|
|
|
# browser pages.
|
|
|
|
|
if 'Chrome' in headers['User-Agent']:
|
|
|
|
|
headers.update({
|
|
|
|
|
'Sec-CH-UA': (
|
|
|
|
|
'"Not/A)Brand";v="8", '
|
|
|
|
|
'"Chromium";v="127", '
|
|
|
|
|
'"Google Chrome";v="127"'
|
|
|
|
|
),
|
|
|
|
|
'Sec-CH-UA-Mobile': '?0',
|
|
|
|
|
'Sec-CH-UA-Platform': '"Windows"'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
2025-11-24 02:35:08 +00:00
|
|
|
# Add Accept-Language header tied to the current config if requested
|
2021-10-25 21:49:09 +00:00
|
|
|
if self.lang_interface:
|
2025-11-24 02:35:08 +00:00
|
|
|
headers['Accept-Language'] = (
|
|
|
|
|
self.lang_interface.replace('lang_', '') + ';q=1.0'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Consent cookies keep Google from showing the interstitial consent wall
|
|
|
|
|
consent_cookies = {
|
|
|
|
|
'CONSENT': 'PENDING+987',
|
|
|
|
|
'SOCS': 'CAESHAgBEhIaAB'
|
|
|
|
|
}
|
2021-03-31 13:50:13 +00:00
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
# Validate Tor conn and request new identity if the last one failed
|
|
|
|
|
if self.tor and not send_tor_signal(
|
|
|
|
|
Signal.NEWNYM if attempt > 0 else Signal.HEARTBEAT):
|
|
|
|
|
raise TorError(
|
|
|
|
|
"Tor was previously enabled, but the connection has been "
|
|
|
|
|
"dropped. Please check your Tor configuration and try again.",
|
|
|
|
|
disable=True)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
|
|
|
|
# Make sure that the tor connection is valid, if enabled
|
|
|
|
|
if self.tor:
|
2021-11-13 00:19:45 +00:00
|
|
|
try:
|
2025-09-21 05:11:54 +00:00
|
|
|
tor_check = self.http_client.get('https://check.torproject.org/',
|
|
|
|
|
headers=headers,
|
|
|
|
|
retries=1)
|
2021-11-13 00:19:45 +00:00
|
|
|
self.tor_valid = 'Congratulations' in tor_check.text
|
|
|
|
|
|
|
|
|
|
if not self.tor_valid:
|
|
|
|
|
raise TorError(
|
|
|
|
|
"Tor connection succeeded, but the connection could "
|
|
|
|
|
"not be validated by torproject.org",
|
|
|
|
|
disable=True)
|
2025-09-21 05:11:54 +00:00
|
|
|
except httpx.RequestError:
|
2020-12-17 21:06:47 +00:00
|
|
|
raise TorError(
|
2021-11-13 00:19:45 +00:00
|
|
|
"Error raised during Tor connection validation",
|
2020-12-17 21:06:47 +00:00
|
|
|
disable=True)
|
|
|
|
|
|
2025-11-27 04:21:23 +00:00
|
|
|
search_base = base_url or self.search_url
|
|
|
|
|
if not base_url and ('tbm=isch' in query or 'udm=2' in query):
|
|
|
|
|
search_base = self.image_search_url
|
|
|
|
|
|
2025-09-21 05:11:54 +00:00
|
|
|
try:
|
|
|
|
|
response = self.http_client.get(
|
2025-11-27 04:21:23 +00:00
|
|
|
search_base + query,
|
2025-11-24 02:35:08 +00:00
|
|
|
headers=headers,
|
|
|
|
|
cookies=consent_cookies)
|
2025-09-21 05:11:54 +00:00
|
|
|
except httpx.HTTPError as e:
|
|
|
|
|
raise
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
|
|
|
|
# Retry query with new identity if using Tor (max 10 attempts)
|
|
|
|
|
if 'form id="captcha-form"' in response.text and self.tor:
|
|
|
|
|
attempt += 1
|
|
|
|
|
if attempt > 10:
|
|
|
|
|
raise TorError("Tor query failed -- max attempts exceeded 10")
|
2025-11-27 04:21:23 +00:00
|
|
|
return self.send(search_base, query, attempt)
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 00:47:42 +00:00
|
|
|
|
|
|
|
|
return response
|