[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-05-24 15:29:07 +00:00
|
|
|
# lint: pylint
|
|
|
|
# pylint: disable=global-statement
|
2021-09-07 11:34:35 +00:00
|
|
|
# pylint: disable=missing-module-docstring, missing-class-docstring
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
import atexit
|
|
|
|
import asyncio
|
|
|
|
import ipaddress
|
|
|
|
from itertools import cycle
|
2022-01-17 08:04:51 +00:00
|
|
|
from typing import Dict
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
2021-09-06 19:42:30 +00:00
|
|
|
from searx import logger, searx_debug
|
2021-09-23 09:31:29 +00:00
|
|
|
from .client import new_client, get_loop, AsyncHTTPTransportNoHttp
|
2022-05-21 16:24:47 +00:00
|
|
|
from .raise_for_httperror import raise_for_httperror
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
|
2021-09-06 19:42:30 +00:00
|
|
|
logger = logger.getChild('network')
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
DEFAULT_NAME = '__DEFAULT__'
|
2022-01-17 08:04:51 +00:00
|
|
|
NETWORKS: Dict[str, 'Network'] = {}
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
# requests compatibility when reading proxy settings from settings.yml
|
|
|
|
PROXY_PATTERN_MAPPING = {
|
|
|
|
'http': 'http://',
|
|
|
|
'https': 'https://',
|
|
|
|
'socks4': 'socks4://',
|
|
|
|
'socks5': 'socks5://',
|
|
|
|
'socks5h': 'socks5h://',
|
|
|
|
'http:': 'http://',
|
|
|
|
'https:': 'https://',
|
|
|
|
'socks4:': 'socks4://',
|
|
|
|
'socks5:': 'socks5://',
|
|
|
|
'socks5h:': 'socks5h://',
|
|
|
|
}
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
ADDRESS_MAPPING = {'ipv4': '0.0.0.0', 'ipv6': '::'}
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Network:
|
|
|
|
|
2021-05-24 15:48:47 +00:00
|
|
|
__slots__ = (
|
2021-12-27 08:26:22 +00:00
|
|
|
'enable_http',
|
|
|
|
'verify',
|
|
|
|
'enable_http2',
|
|
|
|
'max_connections',
|
|
|
|
'max_keepalive_connections',
|
|
|
|
'keepalive_expiry',
|
|
|
|
'local_addresses',
|
|
|
|
'proxies',
|
|
|
|
'using_tor_proxy',
|
|
|
|
'max_redirects',
|
|
|
|
'retries',
|
|
|
|
'retry_on_http_error',
|
|
|
|
'_local_addresses_cycle',
|
|
|
|
'_proxies_cycle',
|
|
|
|
'_clients',
|
|
|
|
'_logger',
|
2021-05-24 15:48:47 +00:00
|
|
|
)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
2021-09-23 09:31:29 +00:00
|
|
|
_TOR_CHECK_RESULT = {}
|
|
|
|
|
2021-05-24 15:29:07 +00:00
|
|
|
def __init__(
|
2021-12-27 08:26:22 +00:00
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
self,
|
|
|
|
enable_http=True,
|
|
|
|
verify=True,
|
|
|
|
enable_http2=False,
|
|
|
|
max_connections=None,
|
|
|
|
max_keepalive_connections=None,
|
|
|
|
keepalive_expiry=None,
|
|
|
|
proxies=None,
|
|
|
|
using_tor_proxy=False,
|
|
|
|
local_addresses=None,
|
|
|
|
retries=0,
|
|
|
|
retry_on_http_error=None,
|
|
|
|
max_redirects=30,
|
|
|
|
logger_name=None,
|
|
|
|
):
|
2021-05-24 15:29:07 +00:00
|
|
|
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
self.enable_http = enable_http
|
|
|
|
self.verify = verify
|
|
|
|
self.enable_http2 = enable_http2
|
|
|
|
self.max_connections = max_connections
|
|
|
|
self.max_keepalive_connections = max_keepalive_connections
|
|
|
|
self.keepalive_expiry = keepalive_expiry
|
|
|
|
self.proxies = proxies
|
2021-09-23 09:31:29 +00:00
|
|
|
self.using_tor_proxy = using_tor_proxy
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
self.local_addresses = local_addresses
|
|
|
|
self.retries = retries
|
|
|
|
self.retry_on_http_error = retry_on_http_error
|
|
|
|
self.max_redirects = max_redirects
|
|
|
|
self._local_addresses_cycle = self.get_ipaddress_cycle()
|
|
|
|
self._proxies_cycle = self.get_proxy_cycles()
|
|
|
|
self._clients = {}
|
2021-09-06 19:42:30 +00:00
|
|
|
self._logger = logger.getChild(logger_name) if logger_name else logger
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
self.check_parameters()
|
|
|
|
|
|
|
|
def check_parameters(self):
|
|
|
|
for address in self.iter_ipaddresses():
|
|
|
|
if '/' in address:
|
|
|
|
ipaddress.ip_network(address, False)
|
|
|
|
else:
|
|
|
|
ipaddress.ip_address(address)
|
|
|
|
|
|
|
|
if self.proxies is not None and not isinstance(self.proxies, (str, dict)):
|
|
|
|
raise ValueError('proxies type has to be str, dict or None')
|
|
|
|
|
|
|
|
def iter_ipaddresses(self):
|
|
|
|
local_addresses = self.local_addresses
|
|
|
|
if not local_addresses:
|
|
|
|
return
|
2021-05-24 15:29:07 +00:00
|
|
|
if isinstance(local_addresses, str):
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
local_addresses = [local_addresses]
|
2024-03-09 07:34:43 +00:00
|
|
|
yield from local_addresses
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
def get_ipaddress_cycle(self):
|
|
|
|
while True:
|
|
|
|
count = 0
|
|
|
|
for address in self.iter_ipaddresses():
|
|
|
|
if '/' in address:
|
|
|
|
for a in ipaddress.ip_network(address, False).hosts():
|
|
|
|
yield str(a)
|
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
a = ipaddress.ip_address(address)
|
|
|
|
yield str(a)
|
|
|
|
count += 1
|
|
|
|
if count == 0:
|
|
|
|
yield None
|
|
|
|
|
|
|
|
def iter_proxies(self):
|
|
|
|
if not self.proxies:
|
|
|
|
return
|
|
|
|
# https://www.python-httpx.org/compatibility/#proxy-keys
|
|
|
|
if isinstance(self.proxies, str):
|
|
|
|
yield 'all://', [self.proxies]
|
|
|
|
else:
|
|
|
|
for pattern, proxy_url in self.proxies.items():
|
|
|
|
pattern = PROXY_PATTERN_MAPPING.get(pattern, pattern)
|
|
|
|
if isinstance(proxy_url, str):
|
|
|
|
proxy_url = [proxy_url]
|
|
|
|
yield pattern, proxy_url
|
|
|
|
|
|
|
|
def get_proxy_cycles(self):
|
|
|
|
proxy_settings = {}
|
|
|
|
for pattern, proxy_urls in self.iter_proxies():
|
|
|
|
proxy_settings[pattern] = cycle(proxy_urls)
|
|
|
|
while True:
|
2021-05-24 15:29:07 +00:00
|
|
|
# pylint: disable=stop-iteration-return
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
yield tuple((pattern, next(proxy_url_cycle)) for pattern, proxy_url_cycle in proxy_settings.items())
|
|
|
|
|
2021-09-06 19:42:30 +00:00
|
|
|
async def log_response(self, response: httpx.Response):
|
|
|
|
request = response.request
|
|
|
|
status = f"{response.status_code} {response.reason_phrase}"
|
|
|
|
response_line = f"{response.http_version} {status}"
|
2021-08-07 16:48:01 +00:00
|
|
|
content_type = response.headers.get("Content-Type")
|
|
|
|
content_type = f' ({content_type})' if content_type else ''
|
2021-12-27 08:26:22 +00:00
|
|
|
self._logger.debug(f'HTTP Request: {request.method} {request.url} "{response_line}"{content_type}')
|
2021-09-06 19:42:30 +00:00
|
|
|
|
2021-09-23 09:31:29 +00:00
|
|
|
@staticmethod
|
|
|
|
async def check_tor_proxy(client: httpx.AsyncClient, proxies) -> bool:
|
|
|
|
if proxies in Network._TOR_CHECK_RESULT:
|
|
|
|
return Network._TOR_CHECK_RESULT[proxies]
|
|
|
|
|
|
|
|
result = True
|
|
|
|
# ignore client._transport because it is not used with all://
|
|
|
|
for transport in client._mounts.values(): # pylint: disable=protected-access
|
|
|
|
if isinstance(transport, AsyncHTTPTransportNoHttp):
|
|
|
|
continue
|
2022-01-22 18:51:40 +00:00
|
|
|
if getattr(transport, "_pool") and getattr(
|
|
|
|
transport._pool, "_rdns", False # pylint: disable=protected-access
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
return False
|
2022-11-06 19:35:30 +00:00
|
|
|
response = await client.get("https://check.torproject.org/api/ip", timeout=60)
|
2022-01-22 18:51:40 +00:00
|
|
|
if not response.json()["IsTor"]:
|
|
|
|
result = False
|
2021-09-23 09:31:29 +00:00
|
|
|
Network._TOR_CHECK_RESULT[proxies] = result
|
|
|
|
return result
|
|
|
|
|
|
|
|
async def get_client(self, verify=None, max_redirects=None):
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
verify = self.verify if verify is None else verify
|
|
|
|
max_redirects = self.max_redirects if max_redirects is None else max_redirects
|
|
|
|
local_address = next(self._local_addresses_cycle)
|
|
|
|
proxies = next(self._proxies_cycle) # is a tuple so it can be part of the key
|
|
|
|
key = (verify, max_redirects, local_address, proxies)
|
2021-09-06 19:42:30 +00:00
|
|
|
hook_log_response = self.log_response if searx_debug else None
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
if key not in self._clients or self._clients[key].is_closed:
|
2021-09-23 09:31:29 +00:00
|
|
|
client = new_client(
|
2021-05-24 15:48:47 +00:00
|
|
|
self.enable_http,
|
|
|
|
verify,
|
|
|
|
self.enable_http2,
|
|
|
|
self.max_connections,
|
|
|
|
self.max_keepalive_connections,
|
|
|
|
self.keepalive_expiry,
|
|
|
|
dict(proxies),
|
|
|
|
local_address,
|
|
|
|
0,
|
2021-09-06 19:42:30 +00:00
|
|
|
max_redirects,
|
2021-12-27 08:26:22 +00:00
|
|
|
hook_log_response,
|
2021-05-24 15:48:47 +00:00
|
|
|
)
|
2021-09-23 09:31:29 +00:00
|
|
|
if self.using_tor_proxy and not await self.check_tor_proxy(client, proxies):
|
|
|
|
await client.aclose()
|
|
|
|
raise httpx.ProxyError('Network configuration problem: not using Tor')
|
|
|
|
self._clients[key] = client
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
return self._clients[key]
|
|
|
|
|
|
|
|
async def aclose(self):
|
|
|
|
async def close_client(client):
|
|
|
|
try:
|
|
|
|
await client.aclose()
|
|
|
|
except httpx.HTTPError:
|
|
|
|
pass
|
2021-12-27 08:26:22 +00:00
|
|
|
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
await asyncio.gather(*[close_client(client) for client in self._clients.values()], return_exceptions=False)
|
|
|
|
|
|
|
|
@staticmethod
|
2021-12-11 12:17:04 +00:00
|
|
|
def extract_kwargs_clients(kwargs):
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
kwargs_clients = {}
|
|
|
|
if 'verify' in kwargs:
|
|
|
|
kwargs_clients['verify'] = kwargs.pop('verify')
|
|
|
|
if 'max_redirects' in kwargs:
|
|
|
|
kwargs_clients['max_redirects'] = kwargs.pop('max_redirects')
|
2021-12-11 12:17:04 +00:00
|
|
|
if 'allow_redirects' in kwargs:
|
|
|
|
# see https://github.com/encode/httpx/pull/1808
|
|
|
|
kwargs['follow_redirects'] = kwargs.pop('allow_redirects')
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
return kwargs_clients
|
|
|
|
|
2022-05-21 16:24:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def extract_do_raise_for_httperror(kwargs):
|
|
|
|
do_raise_for_httperror = True
|
|
|
|
if 'raise_for_httperror' in kwargs:
|
|
|
|
do_raise_for_httperror = kwargs['raise_for_httperror']
|
|
|
|
del kwargs['raise_for_httperror']
|
|
|
|
return do_raise_for_httperror
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def patch_response(response, do_raise_for_httperror):
|
|
|
|
if isinstance(response, httpx.Response):
|
|
|
|
# requests compatibility (response is not streamed)
|
|
|
|
# see also https://www.python-httpx.org/compatibility/#checking-for-4xx5xx-responses
|
|
|
|
response.ok = not response.is_error
|
|
|
|
|
|
|
|
# raise an exception
|
|
|
|
if do_raise_for_httperror:
|
|
|
|
raise_for_httperror(response)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2021-12-11 12:17:04 +00:00
|
|
|
def is_valid_response(self, response):
|
2021-05-24 15:29:07 +00:00
|
|
|
# pylint: disable=too-many-boolean-expressions
|
2021-12-27 08:26:22 +00:00
|
|
|
if (
|
|
|
|
(self.retry_on_http_error is True and 400 <= response.status_code <= 599)
|
2021-05-24 15:48:47 +00:00
|
|
|
or (isinstance(self.retry_on_http_error, list) and response.status_code in self.retry_on_http_error)
|
|
|
|
or (isinstance(self.retry_on_http_error, int) and response.status_code == self.retry_on_http_error)
|
|
|
|
):
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-12-11 12:17:04 +00:00
|
|
|
async def call_client(self, stream, method, url, **kwargs):
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
retries = self.retries
|
2021-12-11 12:17:04 +00:00
|
|
|
was_disconnected = False
|
2022-05-21 16:24:47 +00:00
|
|
|
do_raise_for_httperror = Network.extract_do_raise_for_httperror(kwargs)
|
2021-12-11 12:17:04 +00:00
|
|
|
kwargs_clients = Network.extract_kwargs_clients(kwargs)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
while retries >= 0: # pragma: no cover
|
2021-09-23 09:31:29 +00:00
|
|
|
client = await self.get_client(**kwargs_clients)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
try:
|
2021-12-11 12:17:04 +00:00
|
|
|
if stream:
|
|
|
|
response = client.stream(method, url, **kwargs)
|
|
|
|
else:
|
|
|
|
response = await client.request(method, url, **kwargs)
|
|
|
|
if self.is_valid_response(response) or retries <= 0:
|
2022-05-21 16:24:47 +00:00
|
|
|
return Network.patch_response(response, do_raise_for_httperror)
|
2021-12-11 12:17:04 +00:00
|
|
|
except httpx.RemoteProtocolError as e:
|
|
|
|
if not was_disconnected:
|
|
|
|
# the server has closed the connection:
|
|
|
|
# try again without decreasing the retries variable & with a new HTTP client
|
|
|
|
was_disconnected = True
|
|
|
|
await client.aclose()
|
|
|
|
self._logger.warning('httpx.RemoteProtocolError: the server has disconnected, retrying')
|
|
|
|
continue
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
if retries <= 0:
|
|
|
|
raise e
|
|
|
|
except (httpx.RequestError, httpx.HTTPStatusError) as e:
|
|
|
|
if retries <= 0:
|
|
|
|
raise e
|
|
|
|
retries -= 1
|
|
|
|
|
2021-12-11 12:17:04 +00:00
|
|
|
async def request(self, method, url, **kwargs):
|
|
|
|
return await self.call_client(False, method, url, **kwargs)
|
|
|
|
|
|
|
|
async def stream(self, method, url, **kwargs):
|
|
|
|
return await self.call_client(True, method, url, **kwargs)
|
|
|
|
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
@classmethod
|
|
|
|
async def aclose_all(cls):
|
|
|
|
await asyncio.gather(*[network.aclose() for network in NETWORKS.values()], return_exceptions=False)
|
|
|
|
|
|
|
|
|
|
|
|
def get_network(name=None):
|
2021-04-13 13:21:53 +00:00
|
|
|
return NETWORKS.get(name or DEFAULT_NAME)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
|
2021-09-23 09:31:29 +00:00
|
|
|
def check_network_configuration():
|
|
|
|
async def check():
|
|
|
|
exception_count = 0
|
|
|
|
for network in NETWORKS.values():
|
|
|
|
if network.using_tor_proxy:
|
|
|
|
try:
|
|
|
|
await network.get_client()
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
network._logger.exception('Error') # pylint: disable=protected-access
|
|
|
|
exception_count += 1
|
|
|
|
return exception_count
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2021-09-23 09:31:29 +00:00
|
|
|
future = asyncio.run_coroutine_threadsafe(check(), get_loop())
|
|
|
|
exception_count = future.result()
|
|
|
|
if exception_count > 0:
|
|
|
|
raise RuntimeError("Invalid network configuration")
|
|
|
|
|
|
|
|
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
def initialize(settings_engines=None, settings_outgoing=None):
|
2021-05-24 15:29:07 +00:00
|
|
|
# pylint: disable=import-outside-toplevel)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
from searx.engines import engines
|
|
|
|
from searx import settings
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2021-05-24 15:29:07 +00:00
|
|
|
# pylint: enable=import-outside-toplevel)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
2021-05-28 16:45:22 +00:00
|
|
|
settings_engines = settings_engines or settings['engines']
|
|
|
|
settings_outgoing = settings_outgoing or settings['outgoing']
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
# default parameters for AsyncHTTPTransport
|
2021-05-24 15:29:07 +00:00
|
|
|
# see https://github.com/encode/httpx/blob/e05a5372eb6172287458b37447c30f650047e1b8/httpx/_transports/default.py#L108-L121 # pylint: disable=line-too-long
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
default_params = {
|
|
|
|
'enable_http': False,
|
2022-07-19 21:40:11 +00:00
|
|
|
'verify': settings_outgoing['verify'],
|
2021-05-28 16:45:22 +00:00
|
|
|
'enable_http2': settings_outgoing['enable_http2'],
|
|
|
|
'max_connections': settings_outgoing['pool_connections'],
|
|
|
|
'max_keepalive_connections': settings_outgoing['pool_maxsize'],
|
|
|
|
'keepalive_expiry': settings_outgoing['keepalive_expiry'],
|
|
|
|
'local_addresses': settings_outgoing['source_ips'],
|
2021-09-23 09:31:29 +00:00
|
|
|
'using_tor_proxy': settings_outgoing['using_tor_proxy'],
|
2021-05-28 16:45:22 +00:00
|
|
|
'proxies': settings_outgoing['proxies'],
|
|
|
|
'max_redirects': settings_outgoing['max_redirects'],
|
|
|
|
'retries': settings_outgoing['retries'],
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
'retry_on_http_error': None,
|
|
|
|
}
|
|
|
|
|
2021-09-06 19:42:30 +00:00
|
|
|
def new_network(params, logger_name=None):
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
nonlocal default_params
|
|
|
|
result = {}
|
|
|
|
result.update(default_params)
|
|
|
|
result.update(params)
|
2021-09-06 19:42:30 +00:00
|
|
|
if logger_name:
|
|
|
|
result['logger_name'] = logger_name
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
return Network(**result)
|
|
|
|
|
|
|
|
def iter_networks():
|
|
|
|
nonlocal settings_engines
|
|
|
|
for engine_spec in settings_engines:
|
|
|
|
engine_name = engine_spec['name']
|
|
|
|
engine = engines.get(engine_name)
|
|
|
|
if engine is None:
|
|
|
|
continue
|
|
|
|
network = getattr(engine, 'network', None)
|
|
|
|
yield engine_name, engine, network
|
|
|
|
|
|
|
|
if NETWORKS:
|
|
|
|
done()
|
|
|
|
NETWORKS.clear()
|
2021-09-06 19:42:30 +00:00
|
|
|
NETWORKS[DEFAULT_NAME] = new_network({}, logger_name='default')
|
|
|
|
NETWORKS['ipv4'] = new_network({'local_addresses': '0.0.0.0'}, logger_name='ipv4')
|
|
|
|
NETWORKS['ipv6'] = new_network({'local_addresses': '::'}, logger_name='ipv6')
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
# define networks from outgoing.networks
|
2021-05-28 16:45:22 +00:00
|
|
|
for network_name, network in settings_outgoing['networks'].items():
|
2021-09-06 19:42:30 +00:00
|
|
|
NETWORKS[network_name] = new_network(network, logger_name=network_name)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
# define networks from engines.[i].network (except references)
|
|
|
|
for engine_name, engine, network in iter_networks():
|
|
|
|
if network is None:
|
|
|
|
network = {}
|
|
|
|
for attribute_name, attribute_value in default_params.items():
|
|
|
|
if hasattr(engine, attribute_name):
|
|
|
|
network[attribute_name] = getattr(engine, attribute_name)
|
|
|
|
else:
|
|
|
|
network[attribute_name] = attribute_value
|
2021-09-06 19:42:30 +00:00
|
|
|
NETWORKS[engine_name] = new_network(network, logger_name=engine_name)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
elif isinstance(network, dict):
|
2021-09-06 19:42:30 +00:00
|
|
|
NETWORKS[engine_name] = new_network(network, logger_name=engine_name)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
# define networks from engines.[i].network (references)
|
|
|
|
for engine_name, engine, network in iter_networks():
|
|
|
|
if isinstance(network, str):
|
|
|
|
NETWORKS[engine_name] = NETWORKS[network]
|
|
|
|
|
2021-08-14 18:12:11 +00:00
|
|
|
# the /image_proxy endpoint has a dedicated network.
|
|
|
|
# same parameters than the default network, but HTTP/2 is disabled.
|
|
|
|
# It decreases the CPU load average, and the total time is more or less the same
|
|
|
|
if 'image_proxy' not in NETWORKS:
|
|
|
|
image_proxy_params = default_params.copy()
|
|
|
|
image_proxy_params['enable_http2'] = False
|
2021-09-06 19:42:30 +00:00
|
|
|
NETWORKS['image_proxy'] = new_network(image_proxy_params, logger_name='image_proxy')
|
2021-08-14 18:12:11 +00:00
|
|
|
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
@atexit.register
|
|
|
|
def done():
|
|
|
|
"""Close all HTTP client
|
|
|
|
|
|
|
|
Avoid a warning at exit
|
2023-08-12 14:57:00 +00:00
|
|
|
See https://github.com/encode/httpx/pull/2026
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
|
|
|
|
Note: since Network.aclose has to be async, it is not possible to call this method on Network.__del__
|
|
|
|
So Network.aclose is called here using atexit.register
|
|
|
|
"""
|
|
|
|
try:
|
2021-04-27 08:03:19 +00:00
|
|
|
loop = get_loop()
|
|
|
|
if loop:
|
|
|
|
future = asyncio.run_coroutine_threadsafe(Network.aclose_all(), loop)
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
# wait 3 seconds to close the HTTP clients
|
|
|
|
future.result(3)
|
|
|
|
finally:
|
|
|
|
NETWORKS.clear()
|
|
|
|
|
|
|
|
|
|
|
|
NETWORKS[DEFAULT_NAME] = Network()
|