atomicshop 3.2.13__py3-none-any.whl → 3.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of atomicshop might be problematic. Click here for more details.
- atomicshop/__init__.py +1 -1
- atomicshop/basics/multiprocesses.py +67 -0
- atomicshop/config_init.py +7 -2
- atomicshop/mitm/config_static.py +6 -3
- atomicshop/mitm/connection_thread_worker.py +11 -4
- atomicshop/mitm/engines/__reference_general/responder___reference_general.py +21 -0
- atomicshop/mitm/import_config.py +12 -6
- atomicshop/mitm/initialize_engines.py +15 -7
- atomicshop/mitm/mitm_main.py +267 -150
- atomicshop/wrappers/loggingw/loggingw.py +464 -33
- atomicshop/wrappers/socketw/dns_server.py +13 -3
- atomicshop/wrappers/socketw/socket_client.py +7 -0
- atomicshop/wrappers/socketw/socket_wrapper.py +164 -172
- atomicshop/wrappers/socketw/statistics_csv.py +32 -13
- atomicshop/wrappers/ubuntu_terminal.py +11 -2
- {atomicshop-3.2.13.dist-info → atomicshop-3.3.0.dist-info}/METADATA +1 -1
- {atomicshop-3.2.13.dist-info → atomicshop-3.3.0.dist-info}/RECORD +21 -21
- {atomicshop-3.2.13.dist-info → atomicshop-3.3.0.dist-info}/LICENSE.txt +0 -0
- {atomicshop-3.2.13.dist-info → atomicshop-3.3.0.dist-info}/WHEEL +0 -0
- {atomicshop-3.2.13.dist-info → atomicshop-3.3.0.dist-info}/entry_points.txt +0 -0
- {atomicshop-3.2.13.dist-info → atomicshop-3.3.0.dist-info}/top_level.txt +0 -0
atomicshop/mitm/mitm_main.py
CHANGED
|
@@ -3,6 +3,7 @@ import multiprocessing
|
|
|
3
3
|
import time
|
|
4
4
|
import datetime
|
|
5
5
|
import os
|
|
6
|
+
import sys
|
|
6
7
|
import logging
|
|
7
8
|
|
|
8
9
|
import atomicshop # Importing atomicshop package to get the version of the package.
|
|
@@ -10,9 +11,10 @@ import atomicshop # Importing atomicshop package to get the version of the pac
|
|
|
10
11
|
from .. import filesystem, on_exit, print_api, networks, dns
|
|
11
12
|
from ..permissions import permissions
|
|
12
13
|
from ..python_functions import get_current_python_version_string, check_python_version_compliance
|
|
13
|
-
from ..wrappers.socketw import socket_wrapper, dns_server, base
|
|
14
|
+
from ..wrappers.socketw import socket_wrapper, dns_server, base, statistics_csv
|
|
14
15
|
from ..wrappers.loggingw import loggingw
|
|
15
16
|
from ..wrappers.ctyping import win_console
|
|
17
|
+
from ..basics import multiprocesses
|
|
16
18
|
|
|
17
19
|
from .connection_thread_worker import thread_worker_main
|
|
18
20
|
from . import config_static, recs_files
|
|
@@ -64,13 +66,16 @@ IS_SET_DNS_GATEWAY: bool = False
|
|
|
64
66
|
RECS_PROCESS_INSTANCE: multiprocessing.Process = None
|
|
65
67
|
|
|
66
68
|
|
|
69
|
+
STATISTICS_LOGGER_NAME: str = 'statistics'
|
|
67
70
|
EXCEPTIONS_CSV_LOGGER_NAME: str = 'exceptions'
|
|
68
71
|
EXCEPTIONS_CSV_LOGGER_HEADER: str = 'time,exception'
|
|
69
72
|
# noinspection PyTypeChecker
|
|
70
73
|
MITM_ERROR_LOGGER: loggingw.ExceptionCsvLogger = None
|
|
71
74
|
|
|
72
|
-
# Create logger
|
|
75
|
+
# Create logger queues.
|
|
73
76
|
NETWORK_LOGGER_QUEUE: multiprocessing.Queue = multiprocessing.Queue()
|
|
77
|
+
STATISTICS_CSV_LOGGER_QUEUE: multiprocessing.Queue = multiprocessing.Queue()
|
|
78
|
+
EXCEPTIONS_CSV_LOGGER_QUEUE: multiprocessing.Queue = multiprocessing.Queue()
|
|
74
79
|
|
|
75
80
|
# Create finalization queue for the rec archiving process.
|
|
76
81
|
FINALIZE_RECS_ARCHIVE_QUEUE: multiprocessing.Queue = multiprocessing.Queue()
|
|
@@ -304,6 +309,7 @@ def get_ipv4s_for_tcp_server():
|
|
|
304
309
|
if port in ports_to_create_ips_for:
|
|
305
310
|
engine.port_target_dict[port]['ip'] = engine_ips.pop(0)
|
|
306
311
|
|
|
312
|
+
|
|
307
313
|
def mitm_server(config_file_path: str, script_version: str):
|
|
308
314
|
on_exit.register_exit_handler(exit_cleanup, at_exit=False, kill_signal=False)
|
|
309
315
|
|
|
@@ -323,7 +329,11 @@ def mitm_server(config_file_path: str, script_version: str):
|
|
|
323
329
|
|
|
324
330
|
global MITM_ERROR_LOGGER
|
|
325
331
|
MITM_ERROR_LOGGER = loggingw.ExceptionCsvLogger(
|
|
326
|
-
logger_name=EXCEPTIONS_CSV_LOGGER_NAME,
|
|
332
|
+
logger_name=EXCEPTIONS_CSV_LOGGER_NAME,
|
|
333
|
+
directory_path=config_static.LogRec.logs_path,
|
|
334
|
+
log_queue=EXCEPTIONS_CSV_LOGGER_QUEUE,
|
|
335
|
+
add_queue_handler_start_listener_multiprocessing=True,
|
|
336
|
+
)
|
|
327
337
|
|
|
328
338
|
# Create folders.
|
|
329
339
|
filesystem.create_directory(config_static.LogRec.logs_path)
|
|
@@ -334,10 +344,11 @@ def mitm_server(config_file_path: str, script_version: str):
|
|
|
334
344
|
|
|
335
345
|
network_logger_name = config_static.MainConfig.LOGGER_NAME
|
|
336
346
|
|
|
337
|
-
#
|
|
338
|
-
|
|
339
|
-
|
|
347
|
+
# Start the network logger and its queue listener.
|
|
348
|
+
_ = loggingw.create_logger(
|
|
349
|
+
logger_name=network_logger_name,
|
|
340
350
|
log_queue=NETWORK_LOGGER_QUEUE,
|
|
351
|
+
start_queue_listener_multiprocess_add_queue_handler=True,
|
|
341
352
|
file_path=f'{config_static.LogRec.logs_path}{os.sep}{network_logger_name}.txt',
|
|
342
353
|
add_stream=True,
|
|
343
354
|
add_timedfile=True,
|
|
@@ -345,13 +356,16 @@ def mitm_server(config_file_path: str, script_version: str):
|
|
|
345
356
|
formatter_filehandler='DEFAULT',
|
|
346
357
|
backupCount=config_static.LogRec.store_logs_for_x_days)
|
|
347
358
|
|
|
348
|
-
|
|
349
|
-
|
|
359
|
+
"""
|
|
360
|
+
# Create this in other multiprocesses (You need to pass only the logger_name and log_queue to the other process):
|
|
361
|
+
logger = loggingw.create_logger(
|
|
362
|
+
logger_name=logger_name,
|
|
350
363
|
add_queue_handler=True,
|
|
351
|
-
log_queue=NETWORK_LOGGER_QUEUE
|
|
364
|
+
log_queue=NETWORK_LOGGER_QUEUE
|
|
365
|
+
)
|
|
366
|
+
"""
|
|
352
367
|
|
|
353
368
|
# Initiate Listener logger, which is a child of network logger, so he uses the same settings and handlers
|
|
354
|
-
listener_logger: logging.Logger = loggingw.get_logger_with_level(f'{network_logger_name}.listener')
|
|
355
369
|
system_logger: logging.Logger = loggingw.get_logger_with_level(f'{network_logger_name}.system')
|
|
356
370
|
|
|
357
371
|
if config_static.LogRec.enable_request_response_recordings_in_logs:
|
|
@@ -372,7 +386,7 @@ def mitm_server(config_file_path: str, script_version: str):
|
|
|
372
386
|
error_type=True, color="red", logger=system_logger, logger_method='critical')
|
|
373
387
|
# Wait for the message to be printed and saved to file.
|
|
374
388
|
time.sleep(1)
|
|
375
|
-
network_logger_queue_listener.stop()
|
|
389
|
+
# network_logger_queue_listener.stop()
|
|
376
390
|
return 1
|
|
377
391
|
|
|
378
392
|
# Logging Startup information.
|
|
@@ -380,66 +394,90 @@ def mitm_server(config_file_path: str, script_version: str):
|
|
|
380
394
|
|
|
381
395
|
print_api.print_api("Press [Ctrl]+[C] to stop.", color='blue')
|
|
382
396
|
|
|
397
|
+
multiprocess_list: list[multiprocessing.Process] = list()
|
|
398
|
+
# noinspection PyTypeHints
|
|
399
|
+
is_ready_multiprocessing_event_list: list[multiprocessing.Event] = list()
|
|
400
|
+
|
|
383
401
|
# === Initialize DNS module ====================================================================================
|
|
384
402
|
if config_static.DNSServer.enable:
|
|
403
|
+
is_dns_process_ready: multiprocessing.Event = multiprocessing.Event()
|
|
404
|
+
is_ready_multiprocessing_event_list.append(is_dns_process_ready)
|
|
405
|
+
|
|
406
|
+
dns_server_kwargs: dict = dict(
|
|
407
|
+
listening_address=config_static.DNSServer.listening_address,
|
|
408
|
+
log_directory_path=config_static.LogRec.logs_path,
|
|
409
|
+
backupCount_log_files_x_days=config_static.LogRec.store_logs_for_x_days,
|
|
410
|
+
forwarding_dns_service_ipv4=config_static.DNSServer.forwarding_dns_service_ipv4,
|
|
411
|
+
forwarding_dns_service_port=config_static.DNSServer.forwarding_dns_service_port,
|
|
412
|
+
resolve_by_engine=(
|
|
413
|
+
config_static.DNSServer.resolve_by_engine, config_static.ENGINES_LIST),
|
|
414
|
+
resolve_regular_pass_thru=config_static.DNSServer.resolve_regular_pass_thru,
|
|
415
|
+
resolve_all_domains_to_ipv4=(
|
|
416
|
+
config_static.DNSServer.resolve_all_domains_to_ipv4_enable, config_static.DNSServer.target_ipv4),
|
|
417
|
+
offline_mode=config_static.MainConfig.offline,
|
|
418
|
+
cache_timeout_minutes=config_static.DNSServer.cache_timeout_minutes,
|
|
419
|
+
logging_queue=NETWORK_LOGGER_QUEUE,
|
|
420
|
+
logger_name=network_logger_name,
|
|
421
|
+
is_ready_multiprocessing=is_dns_process_ready
|
|
422
|
+
)
|
|
423
|
+
|
|
385
424
|
dns_process = multiprocessing.Process(
|
|
386
425
|
target=dns_server.start_dns_server_multiprocessing_worker,
|
|
387
|
-
kwargs=
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
'forwarding_dns_service_ipv4': config_static.DNSServer.forwarding_dns_service_ipv4,
|
|
392
|
-
'forwarding_dns_service_port': config_static.DNSServer.forwarding_dns_service_port,
|
|
393
|
-
'resolve_by_engine': (
|
|
394
|
-
config_static.DNSServer.resolve_by_engine, config_static.ENGINES_LIST),
|
|
395
|
-
'resolve_regular_pass_thru': config_static.DNSServer.resolve_regular_pass_thru,
|
|
396
|
-
'resolve_all_domains_to_ipv4': (
|
|
397
|
-
config_static.DNSServer.resolve_all_domains_to_ipv4_enable, config_static.DNSServer.target_ipv4),
|
|
398
|
-
'offline_mode': config_static.MainConfig.offline,
|
|
399
|
-
'cache_timeout_minutes': config_static.DNSServer.cache_timeout_minutes,
|
|
400
|
-
'logging_queue': NETWORK_LOGGER_QUEUE,
|
|
401
|
-
'logger_name': network_logger_name
|
|
402
|
-
},
|
|
403
|
-
name="dns_server")
|
|
404
|
-
dns_process.daemon = True
|
|
426
|
+
kwargs=dns_server_kwargs,
|
|
427
|
+
name="dns_server",
|
|
428
|
+
daemon=True
|
|
429
|
+
)
|
|
405
430
|
dns_process.start()
|
|
406
431
|
|
|
407
|
-
|
|
408
|
-
is_alive: bool = False
|
|
409
|
-
max_wait_time: int = 5
|
|
410
|
-
while not is_alive:
|
|
411
|
-
is_alive = dns_process.is_alive()
|
|
412
|
-
time.sleep(1)
|
|
413
|
-
max_wait_time -= 1
|
|
414
|
-
if max_wait_time == 0:
|
|
415
|
-
message = "DNS Server process didn't start."
|
|
416
|
-
print_api.print_api(message, error_type=True, color="red", logger=system_logger)
|
|
417
|
-
# Wait for the message to be printed and saved to file.
|
|
418
|
-
time.sleep(1)
|
|
419
|
-
network_logger_queue_listener.stop()
|
|
420
|
-
return 1
|
|
421
|
-
|
|
422
|
-
# Now we can check if the process wasn't terminated after the check.
|
|
423
|
-
max_wait_time: int = 5
|
|
424
|
-
while max_wait_time > 0:
|
|
425
|
-
is_alive = dns_process.is_alive()
|
|
432
|
+
multiprocess_list.append(dns_process)
|
|
426
433
|
|
|
427
|
-
if not is_alive:
|
|
428
|
-
message = "DNS Server process terminated."
|
|
429
|
-
print_api.print_api(message, error_type=True, color="red", logger=system_logger)
|
|
430
|
-
# Wait for the message to be printed and saved to file.
|
|
431
|
-
time.sleep(1)
|
|
432
|
-
network_logger_queue_listener.stop()
|
|
433
|
-
return 1
|
|
434
|
-
|
|
435
|
-
time.sleep(1)
|
|
436
|
-
max_wait_time -= 1
|
|
437
434
|
|
|
438
435
|
# === EOF Initialize DNS module ================================================================================
|
|
439
436
|
# === Initialize TCP Server ====================================================================================
|
|
440
437
|
if config_static.TCPServer.enable:
|
|
441
|
-
|
|
442
|
-
|
|
438
|
+
# Start statistics CSV Queue listener and the logger.
|
|
439
|
+
_ = statistics_csv.StatisticsCSVWriter(
|
|
440
|
+
directory_path=config_static.LogRec.logs_path,
|
|
441
|
+
log_queue=STATISTICS_CSV_LOGGER_QUEUE,
|
|
442
|
+
add_queue_handler_start_listener_multiprocessing=True)
|
|
443
|
+
|
|
444
|
+
no_engine_usage_enable: bool = config_static.TCPServer.no_engines_usage_to_listen_addresses_enable
|
|
445
|
+
no_engines_listening_address_list: list[str] = config_static.TCPServer.no_engines_listening_address_list
|
|
446
|
+
|
|
447
|
+
# If engines were passed, we will use the listening addresses from the engines.
|
|
448
|
+
listening_interfaces: list[dict] = list()
|
|
449
|
+
if not no_engine_usage_enable:
|
|
450
|
+
for engine in config_static.ENGINES_LIST:
|
|
451
|
+
# Combine the domain and port dicts.
|
|
452
|
+
connection_dict: dict = {**engine.domain_target_dict, **engine.port_target_dict}
|
|
453
|
+
|
|
454
|
+
# Start all the regular listening interfaces.
|
|
455
|
+
for domain_or_port, ip_port_dict in connection_dict.items():
|
|
456
|
+
current_interface_dict: dict = {
|
|
457
|
+
'engine': engine,
|
|
458
|
+
'process_name': f'tcp_server-{engine.engine_name}-{domain_or_port}',
|
|
459
|
+
'ip': ip_port_dict['ip'],
|
|
460
|
+
'port': int(ip_port_dict['port'])
|
|
461
|
+
}
|
|
462
|
+
listening_interfaces.append(current_interface_dict)
|
|
463
|
+
else:
|
|
464
|
+
# If no engines were passed, we will use the listening addresses from the configuration.
|
|
465
|
+
for address in no_engines_listening_address_list:
|
|
466
|
+
listening_ip_address, port_str = address.split(':')
|
|
467
|
+
current_interface_dict: dict = {
|
|
468
|
+
'engine': None, # No engine for this address.
|
|
469
|
+
'process_name': f'tcp_server-{listening_ip_address}_{port_str}',
|
|
470
|
+
'ip': listening_ip_address,
|
|
471
|
+
'port': int(port_str)
|
|
472
|
+
}
|
|
473
|
+
listening_interfaces.append(current_interface_dict)
|
|
474
|
+
|
|
475
|
+
# Starting the TCP server multiprocessing processes.
|
|
476
|
+
for interface_dict in listening_interfaces:
|
|
477
|
+
socket_wrapper_kwargs: dict = dict(
|
|
478
|
+
ip_address=interface_dict['ip'],
|
|
479
|
+
port=interface_dict['port'],
|
|
480
|
+
engine=interface_dict['engine'],
|
|
443
481
|
ca_certificate_name=config_static.MainConfig.ca_certificate_name,
|
|
444
482
|
ca_certificate_filepath=config_static.MainConfig.ca_certificate_filepath,
|
|
445
483
|
ca_certificate_crt_filepath=config_static.MainConfig.ca_certificate_crt_filepath,
|
|
@@ -469,109 +507,188 @@ def mitm_server(config_file_path: str, script_version: str):
|
|
|
469
507
|
ssh_user=config_static.ProcessName.ssh_user,
|
|
470
508
|
ssh_pass=config_static.ProcessName.ssh_pass,
|
|
471
509
|
ssh_script_to_execute=config_static.ProcessName.ssh_script_to_execute,
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
510
|
+
logs_directory=config_static.LogRec.logs_path,
|
|
511
|
+
logger_name=network_logger_name,
|
|
512
|
+
logger_queue=NETWORK_LOGGER_QUEUE,
|
|
513
|
+
statistics_logger_name=STATISTICS_LOGGER_NAME,
|
|
514
|
+
statistics_logger_queue=STATISTICS_CSV_LOGGER_QUEUE,
|
|
515
|
+
exceptions_logger_name=EXCEPTIONS_CSV_LOGGER_NAME,
|
|
516
|
+
exceptions_logger_queue=EXCEPTIONS_CSV_LOGGER_QUEUE,
|
|
475
517
|
forwarding_dns_service_ipv4_list___only_for_localhost=[config_static.DNSServer.forwarding_dns_service_ipv4],
|
|
476
|
-
skip_extension_id_list=config_static.SkipExtensions.SKIP_EXTENSION_ID_LIST
|
|
477
|
-
no_engine_usage_enable=config_static.TCPServer.no_engines_usage_to_listen_addresses_enable,
|
|
478
|
-
no_engines_listening_address_list=config_static.TCPServer.no_engines_listening_address_list,
|
|
479
|
-
engines_list=config_static.ENGINES_LIST
|
|
518
|
+
skip_extension_id_list=config_static.SkipExtensions.SKIP_EXTENSION_ID_LIST
|
|
480
519
|
)
|
|
481
|
-
except socket_wrapper.SocketWrapperPortInUseError as e:
|
|
482
|
-
print_api.print_api(e, error_type=True, color="red", logger=system_logger)
|
|
483
|
-
# Wait for the message to be printed and saved to file.
|
|
484
|
-
time.sleep(1)
|
|
485
|
-
network_logger_queue_listener.stop()
|
|
486
|
-
return 1
|
|
487
|
-
except socket_wrapper.SocketWrapperConfigurationValuesError as e:
|
|
488
|
-
print_api.print_api(e, error_type=True, color="red", logger=system_logger, logger_method='critical')
|
|
489
|
-
# Wait for the message to be printed and saved to file.
|
|
490
|
-
time.sleep(1)
|
|
491
|
-
network_logger_queue_listener.stop()
|
|
492
|
-
return 1
|
|
493
520
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
if (NETWORK_INTERFACE_IS_DYNAMIC or (not NETWORK_INTERFACE_IS_DYNAMIC and
|
|
522
|
-
NETWORK_INTERFACE_IPV4_ADDRESS_LIST != dns_gateway_server_list)):
|
|
523
|
-
try:
|
|
524
|
-
dns.set_connection_dns_gateway_static(
|
|
525
|
-
dns_servers=dns_gateway_server_list,
|
|
526
|
-
use_default_connection=True
|
|
527
|
-
)
|
|
528
|
-
except PermissionError as e:
|
|
529
|
-
print_api.print_api(e, error_type=True, color="red", logger=system_logger)
|
|
530
|
-
# Wait for the message to be printed and saved to file.
|
|
531
|
-
time.sleep(1)
|
|
532
|
-
network_logger_queue_listener.stop()
|
|
533
|
-
return 1
|
|
534
|
-
else:
|
|
535
|
-
# Change the adapter settings and add the virtual IPs.
|
|
536
|
-
try:
|
|
537
|
-
networks.add_virtual_ips_to_default_adapter_by_current_setting(
|
|
538
|
-
virtual_ipv4s_to_add=IPS_TO_ASSIGN, virtual_ipv4_masks_to_add=MASKS_TO_ASSIGN, dns_gateways=dns_gateway_server_list)
|
|
539
|
-
except PermissionError as e:
|
|
540
|
-
print_api.print_api(e, error_type=True, color="red", logger=system_logger)
|
|
521
|
+
is_tcp_process_ready: multiprocessing.Event = multiprocessing.Event()
|
|
522
|
+
is_ready_multiprocessing_event_list.append(is_tcp_process_ready)
|
|
523
|
+
|
|
524
|
+
tcp_process: multiprocessing.Process = multiprocessing.Process(
|
|
525
|
+
target=_create_tcp_server_process,
|
|
526
|
+
name=interface_dict['process_name'],
|
|
527
|
+
args=(
|
|
528
|
+
socket_wrapper_kwargs,
|
|
529
|
+
config_file_path,
|
|
530
|
+
network_logger_name,
|
|
531
|
+
NETWORK_LOGGER_QUEUE,
|
|
532
|
+
is_tcp_process_ready
|
|
533
|
+
),
|
|
534
|
+
daemon=True
|
|
535
|
+
)
|
|
536
|
+
tcp_process.start()
|
|
537
|
+
multiprocess_list.append(tcp_process)
|
|
538
|
+
|
|
539
|
+
# Compress recordings each day in a separate process.
|
|
540
|
+
recs_archiver_thread = threading.Thread(target=_loop_at_midnight_recs_archive, args=(network_logger_name,), daemon=True)
|
|
541
|
+
recs_archiver_thread.start()
|
|
542
|
+
|
|
543
|
+
# Check that all the multiprocesses are ready.
|
|
544
|
+
for event in is_ready_multiprocessing_event_list:
|
|
545
|
+
if not event.wait(timeout=30):
|
|
546
|
+
print_api.print_api("One of the processes didn't start in time.", error_type=True, color="red",
|
|
547
|
+
logger=system_logger)
|
|
541
548
|
# Wait for the message to be printed and saved to file.
|
|
542
549
|
time.sleep(1)
|
|
543
|
-
network_logger_queue_listener.stop()
|
|
544
550
|
return 1
|
|
545
551
|
|
|
546
|
-
|
|
552
|
+
# Get the default network adapter configuration and set the one from config.
|
|
553
|
+
dns_setting_result: int = _set_default_dns_gateway(system_logger)
|
|
554
|
+
if dns_setting_result != 0:
|
|
555
|
+
print_api.print_api("Failed to set the default DNS gateway.", error_type=True, color="red",
|
|
556
|
+
logger=system_logger)
|
|
557
|
+
# Wait for the message to be printed and saved to file.
|
|
558
|
+
time.sleep(1)
|
|
559
|
+
return dns_setting_result
|
|
547
560
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
config_static.REFERENCE_MODULE)
|
|
553
|
-
)
|
|
561
|
+
if config_static.DNSServer.enable or config_static.TCPServer.enable:
|
|
562
|
+
print_api.print_api("The Server is Ready for Operation!", color="green", logger=system_logger)
|
|
563
|
+
# Get al the queue listener processes (basically this is not necessary, since they're 'daemons', but this is a good practice).
|
|
564
|
+
multiprocess_list.extend(loggingw.get_listener_processes())
|
|
554
565
|
|
|
555
|
-
#
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
# socket_thread.start()
|
|
566
|
+
# This is needed for Keyboard Exception.
|
|
567
|
+
while True:
|
|
568
|
+
# If it is the first cycle and some process had an exception, we will exist before printing that the
|
|
569
|
+
# server is ready for operation.
|
|
570
|
+
result, process_name = multiprocesses.is_process_crashed(multiprocess_list)
|
|
571
|
+
# If result is None, all processes are still alive.
|
|
572
|
+
if result is not None:
|
|
573
|
+
# If result is 0 or 1, we can exit the loop.
|
|
574
|
+
print(f"Process [{process_name}] finished with exit code {result}.")
|
|
575
|
+
break
|
|
566
576
|
|
|
567
|
-
|
|
568
|
-
recs_archiver_thread = threading.Thread(target=_loop_at_midnight_recs_archive, args=(network_logger_name,), daemon=True)
|
|
569
|
-
recs_archiver_thread.start()
|
|
577
|
+
time.sleep(1)
|
|
570
578
|
|
|
571
|
-
|
|
572
|
-
|
|
579
|
+
|
|
580
|
+
def _create_tcp_server_process(
|
|
581
|
+
socket_wrapper_kwargs: dict,
|
|
582
|
+
config_file_path: str,
|
|
583
|
+
network_logger_name: str,
|
|
584
|
+
network_logger_queue: multiprocessing.Queue,
|
|
585
|
+
is_tcp_process_ready: multiprocessing.Event
|
|
586
|
+
):
|
|
587
|
+
# Load config_static per process, since it is not shared between processes.
|
|
588
|
+
config_static.load_config(config_file_path, print_kwargs=dict(stdout=False))
|
|
589
|
+
|
|
590
|
+
# First create a network logger with a queue handler.
|
|
591
|
+
_ = loggingw.create_logger(
|
|
592
|
+
logger_name=network_logger_name,
|
|
593
|
+
add_queue_handler=True,
|
|
594
|
+
log_queue=network_logger_queue,
|
|
595
|
+
)
|
|
596
|
+
|
|
597
|
+
# Now get the system logger and listener loggers.
|
|
598
|
+
system_logger: logging.Logger = loggingw.get_logger_with_level(f'{network_logger_name}.system')
|
|
599
|
+
# If the listener logger is available in current process, the SocketWrapper will use it.
|
|
600
|
+
_ = loggingw.get_logger_with_level(f'{network_logger_name}.listener')
|
|
601
|
+
|
|
602
|
+
try:
|
|
603
|
+
# noinspection PyTypeChecker
|
|
604
|
+
socket_wrapper_instance = socket_wrapper.SocketWrapper(**socket_wrapper_kwargs)
|
|
605
|
+
except socket_wrapper.SocketWrapperPortInUseError as e:
|
|
606
|
+
print_api.print_api(e, error_type=True, color="red", logger=system_logger)
|
|
607
|
+
# Wait for the message to be printed and saved to file.
|
|
608
|
+
time.sleep(1)
|
|
609
|
+
# network_logger_queue_listener.stop()
|
|
610
|
+
sys.exit(1)
|
|
611
|
+
except socket_wrapper.SocketWrapperConfigurationValuesError as e:
|
|
612
|
+
print_api.print_api(e, error_type=True, color="red", logger=system_logger, logger_method='critical')
|
|
613
|
+
# Wait for the message to be printed and saved to file.
|
|
614
|
+
time.sleep(1)
|
|
615
|
+
# network_logger_queue_listener.stop()
|
|
616
|
+
sys.exit(1)
|
|
617
|
+
|
|
618
|
+
socket_wrapper_instance.start_listening_socket(
|
|
619
|
+
callable_function=thread_worker_main, callable_args=(config_static,))
|
|
620
|
+
|
|
621
|
+
# Notify that the TCP server is ready.
|
|
622
|
+
is_tcp_process_ready.set()
|
|
623
|
+
|
|
624
|
+
try:
|
|
573
625
|
while True:
|
|
626
|
+
time.sleep(1) # Keep the process alive, since the listening socket is in an infinite loop.
|
|
627
|
+
except KeyboardInterrupt:
|
|
628
|
+
sys.exit(0)
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
def _set_default_dns_gateway(system_logger: logging.Logger) -> int:
|
|
632
|
+
"""
|
|
633
|
+
The function reads the current DNS gateway setting and sets the new one.
|
|
634
|
+
|
|
635
|
+
:param system_logger: The logger to use for logging messages.
|
|
636
|
+
:return: 0 if successful, 1 if there was an error.
|
|
637
|
+
"""
|
|
638
|
+
|
|
639
|
+
# This setting is needed only for the dns gateways configurations from the main config on localhost.
|
|
640
|
+
set_local_dns_gateway: bool = False
|
|
641
|
+
# Set the default gateway if specified.
|
|
642
|
+
if config_static.DNSServer.set_default_dns_gateway:
|
|
643
|
+
dns_gateway_server_list = config_static.DNSServer.set_default_dns_gateway
|
|
644
|
+
set_local_dns_gateway = True
|
|
645
|
+
elif config_static.DNSServer.set_default_dns_gateway_to_localhost:
|
|
646
|
+
dns_gateway_server_list = [base.LOCALHOST_IPV4]
|
|
647
|
+
set_local_dns_gateway = True
|
|
648
|
+
elif config_static.DNSServer.set_default_dns_gateway_to_default_interface_ipv4:
|
|
649
|
+
dns_gateway_server_list = [base.DEFAULT_IPV4]
|
|
650
|
+
set_local_dns_gateway = True
|
|
651
|
+
else:
|
|
652
|
+
dns_gateway_server_list = NETWORK_INTERFACE_SETTINGS.dns_gateways
|
|
653
|
+
|
|
654
|
+
if config_static.ENGINES_LIST[0].is_localhost:
|
|
655
|
+
if set_local_dns_gateway:
|
|
656
|
+
global IS_SET_DNS_GATEWAY
|
|
657
|
+
IS_SET_DNS_GATEWAY = True
|
|
658
|
+
|
|
659
|
+
# Get current network interface state.
|
|
660
|
+
global NETWORK_INTERFACE_IS_DYNAMIC, NETWORK_INTERFACE_IPV4_ADDRESS_LIST
|
|
661
|
+
NETWORK_INTERFACE_IS_DYNAMIC, NETWORK_INTERFACE_IPV4_ADDRESS_LIST = dns.get_default_dns_gateway()
|
|
662
|
+
|
|
663
|
+
# Set the DNS gateway to the specified one only if the DNS gateway is dynamic, or it is static but different
|
|
664
|
+
# from the one specified in the configuration file.
|
|
665
|
+
if (NETWORK_INTERFACE_IS_DYNAMIC or (not NETWORK_INTERFACE_IS_DYNAMIC and
|
|
666
|
+
NETWORK_INTERFACE_IPV4_ADDRESS_LIST != dns_gateway_server_list)):
|
|
667
|
+
try:
|
|
668
|
+
dns.set_connection_dns_gateway_static(
|
|
669
|
+
dns_servers=dns_gateway_server_list,
|
|
670
|
+
use_default_connection=True
|
|
671
|
+
)
|
|
672
|
+
except PermissionError as e:
|
|
673
|
+
print_api.print_api(e, error_type=True, color="red", logger=system_logger)
|
|
674
|
+
# Wait for the message to be printed and saved to file.
|
|
675
|
+
time.sleep(1)
|
|
676
|
+
# network_logger_queue_listener.stop()
|
|
677
|
+
return 1
|
|
678
|
+
else:
|
|
679
|
+
# Change the adapter settings and add the virtual IPs.
|
|
680
|
+
try:
|
|
681
|
+
networks.add_virtual_ips_to_default_adapter_by_current_setting(
|
|
682
|
+
virtual_ipv4s_to_add=IPS_TO_ASSIGN, virtual_ipv4_masks_to_add=MASKS_TO_ASSIGN,
|
|
683
|
+
dns_gateways=dns_gateway_server_list)
|
|
684
|
+
except PermissionError as e:
|
|
685
|
+
print_api.print_api(e, error_type=True, color="red", logger=system_logger)
|
|
686
|
+
# Wait for the message to be printed and saved to file.
|
|
574
687
|
time.sleep(1)
|
|
688
|
+
# network_logger_queue_listener.stop()
|
|
689
|
+
return 1
|
|
690
|
+
|
|
691
|
+
return 0
|
|
575
692
|
|
|
576
693
|
|
|
577
694
|
def _loop_at_midnight_recs_archive(network_logger_name):
|