7from ssl
import SSLContext
9from typing
import Any, Dict
12from httpx_socks
import AsyncProxyTransport
13from python_socks
import parse_proxy_url, ProxyConnectionError, ProxyTimeoutError, ProxyError
15from searx
import logger
26logger = logger.getChild(
'searx.network.client')
28SSLCONTEXTS: Dict[Any, SSLContext] = {}
32 """Shuffle httpx's default ciphers of a SSL context randomly.
34 From `What Is TLS Fingerprint and How to Bypass It`_
36 > When implementing TLS fingerprinting, servers can't operate based on a
37 > locked-in whitelist database of fingerprints. New fingerprints appear
38 > when web clients or TLS libraries release new versions. So, they have to
39 > live off a blocklist database instead.
41 > It's safe to leave the first three as is but shuffle the remaining ciphers
42 > and you can bypass the TLS fingerprint check.
44 .. _What Is TLS Fingerprint and How to Bypass It:
45 https://www.zenrows.com/blog/what-is-tls-fingerprint#how-to-bypass-tls-fingerprinting
48 c_list = httpx._config.DEFAULT_CIPHERS.split(
':')
49 sc_list, c_list = c_list[:3], c_list[3:]
50 random.shuffle(c_list)
51 ssl_context.set_ciphers(
":".join(sc_list + c_list))
54def get_sslcontexts(proxy_url=None, cert=None, verify=True, trust_env=True, http2=False):
55 key = (proxy_url, cert, verify, trust_env, http2)
56 if key
not in SSLCONTEXTS:
57 SSLCONTEXTS[key] = httpx.create_ssl_context(cert, verify, trust_env, http2)
59 return SSLCONTEXTS[key]
65 The constructor is blank because httpx.AsyncHTTPTransport.__init__ creates an SSLContext unconditionally:
66 https://github.com/encode/httpx/blob/0f61aa58d66680c239ce43c8cdd453e7dc532bfc/httpx/_transports/default.py#L271
68 Each SSLContext consumes more than 500kb of memory, since there is about one network per engine.
70 In consequence, this class overrides all public methods
72 For reference: https://github.com/encode/httpx/issues/2298
81 raise httpx.UnsupportedProtocol(
'HTTP protocol is disabled')
98class AsyncProxyTransportFixed(AsyncProxyTransport):
99 """Fix httpx_socks.AsyncProxyTransport
101 Map python_socks exceptions to httpx.ProxyError exceptions
107 except ProxyConnectionError
as e:
108 raise httpx.ProxyError(
"ProxyConnectionError: " + e.strerror, request=request)
from e
109 except ProxyTimeoutError
as e:
110 raise httpx.ProxyError(
"ProxyTimeoutError: " + e.args[0], request=request)
from e
111 except ProxyError
as e:
112 raise httpx.ProxyError(
"ProxyError: " + e.args[0], request=request)
from e
121 socks5h =
'socks5h://'
122 if proxy_url.startswith(socks5h):
123 proxy_url =
'socks5://' + proxy_url[len(socks5h) :]
126 proxy_type, proxy_host, proxy_port, proxy_username, proxy_password = parse_proxy_url(proxy_url)
127 verify =
get_sslcontexts(proxy_url,
None, verify,
True, http2)
if verify
is True else verify
129 proxy_type=proxy_type,
130 proxy_host=proxy_host,
131 proxy_port=proxy_port,
132 username=proxy_username,
133 password=proxy_password,
138 local_address=local_address,
145 verify =
get_sslcontexts(
None,
None, verify,
True, http2)
if verify
is True else verify
146 return httpx.AsyncHTTPTransport(
151 proxy=httpx._config.Proxy(proxy_url)
if proxy_url
else None,
152 local_address=local_address,
163 max_keepalive_connections,
171 limit = httpx.Limits(
172 max_connections=max_connections,
173 max_keepalive_connections=max_keepalive_connections,
174 keepalive_expiry=keepalive_expiry,
178 for pattern, proxy_url
in proxies.items():
179 if not enable_http
and pattern.startswith(
'http://'):
181 if proxy_url.startswith(
'socks4://')
or proxy_url.startswith(
'socks5://')
or proxy_url.startswith(
'socks5h://'):
183 verify, enable_http2, local_address, proxy_url, limit, retries
186 mounts[pattern] =
get_transport(verify, enable_http2, local_address, proxy_url, limit, retries)
191 transport =
get_transport(verify, enable_http2, local_address,
None, limit, retries)
194 if hook_log_response:
195 event_hooks = {
'response': [hook_log_response]}
197 return httpx.AsyncClient(
200 max_redirects=max_redirects,
201 event_hooks=event_hooks,
214 'httpcore.connection',
220 logging.getLogger(logger_name).setLevel(logging.WARNING)
225 LOOP = asyncio.new_event_loop()
228 thread = threading.Thread(
__init__(self, *args, **kwargs)
None __aexit__(self, exc_type=None, exc_value=None, traceback=None)
handle_async_request(self, request)
handle_async_request(self, request)
new_client(enable_http, verify, enable_http2, max_connections, max_keepalive_connections, keepalive_expiry, proxies, local_address, retries, max_redirects, hook_log_response)
get_transport_for_socks_proxy(verify, http2, local_address, proxy_url, limit, retries)
get_sslcontexts(proxy_url=None, cert=None, verify=True, trust_env=True, http2=False)
shuffle_ciphers(ssl_context)
get_transport(verify, http2, local_address, proxy_url, limit, retries)