323def initialize(settings_engines=None, settings_outgoing=None):
324
326 from searx import settings
327
328
329
330 settings_engines = settings_engines or settings['engines']
331 settings_outgoing = settings_outgoing or settings['outgoing']
332
333
334
335 default_params = {
336 'enable_http': False,
337 'verify': settings_outgoing['verify'],
338 'enable_http2': settings_outgoing['enable_http2'],
339 'max_connections': settings_outgoing['pool_connections'],
340 'max_keepalive_connections': settings_outgoing['pool_maxsize'],
341 'keepalive_expiry': settings_outgoing['keepalive_expiry'],
342 'local_addresses': settings_outgoing['source_ips'],
343 'using_tor_proxy': settings_outgoing['using_tor_proxy'],
344 'proxies': settings_outgoing['proxies'],
345 'max_redirects': settings_outgoing['max_redirects'],
346 'retries': settings_outgoing['retries'],
347 'retry_on_http_error': None,
348 }
349
350 def new_network(params, logger_name=None):
351 nonlocal default_params
352 result = {}
353 result.update(default_params)
354 result.update(params)
355 if logger_name:
356 result['logger_name'] = logger_name
357 return Network(**result)
358
359 def iter_networks():
360 nonlocal settings_engines
361 for engine_spec in settings_engines:
362 engine_name = engine_spec['name']
363 engine = engines.get(engine_name)
364 if engine is None:
365 continue
366 network = getattr(engine, 'network', None)
367 yield engine_name, engine, network
368
369 if NETWORKS:
370 done()
371 NETWORKS.clear()
372 NETWORKS[DEFAULT_NAME] = new_network({}, logger_name='default')
373 NETWORKS['ipv4'] = new_network({'local_addresses': '0.0.0.0'}, logger_name='ipv4')
374 NETWORKS['ipv6'] = new_network({'local_addresses': '::'}, logger_name='ipv6')
375
376
377 for network_name, network in settings_outgoing['networks'].items():
378 NETWORKS[network_name] = new_network(network, logger_name=network_name)
379
380
381 for engine_name, engine, network in iter_networks():
382 if network is None:
383 network = {}
384 for attribute_name, attribute_value in default_params.items():
385 if hasattr(engine, attribute_name):
386 network[attribute_name] = getattr(engine, attribute_name)
387 else:
388 network[attribute_name] = attribute_value
389 NETWORKS[engine_name] = new_network(network, logger_name=engine_name)
390 elif isinstance(network, dict):
391 NETWORKS[engine_name] = new_network(network, logger_name=engine_name)
392
393
394 for engine_name, engine, network in iter_networks():
395 if isinstance(network, str):
396 NETWORKS[engine_name] = NETWORKS[network]
397
398
399
400
401 if 'image_proxy' not in NETWORKS:
402 image_proxy_params = default_params.copy()
403 image_proxy_params['enable_http2'] = False
404 NETWORKS['image_proxy'] = new_network(image_proxy_params, logger_name='image_proxy')
405
406
407@atexit.register