1
0
Fork 0
mirror of https://github.com/dbarzin/pandora-box.git synced 2025-07-22 23:09:41 +02:00

add hostname in log and add lock on startup

This commit is contained in:
dbarzin 2023-03-06 18:31:38 +01:00
parent f4ab95b840
commit 70f8a60e06

View file

@ -28,13 +28,13 @@ import pyudev
import psutil import psutil
import queue import queue
import shutil import shutil
import socket
import sys import sys
import time import time
import threading import threading
import pypandora import pypandora
# ----------------------------------------------------------- # -----------------------------------------------------------
# Threading variables # Threading variables
# ----------------------------------------------------------- # -----------------------------------------------------------
@ -53,6 +53,7 @@ has_quarantine = None
quarantine_folder = None quarantine_folder = None
has_curses = None has_curses = None
maxThreads = None maxThreads = None
hostname = socket.gethostname()
# ----------------------------------------------------------- # -----------------------------------------------------------
# Curses # Curses
@ -143,6 +144,7 @@ class scanThread (threading.Thread):
'-> ' '-> '
f'{status} ({(file_scan_end_time - file_scan_start_time):.1f}s)') f'{status} ({(file_scan_end_time - file_scan_start_time):.1f}s)')
logging.info( logging.info(
f'hostname="{hostname}", '
f'file="{file_name}", ' f'file="{file_name}", '
f'size="{file_size}", ' f'size="{file_size}", '
f'status="{status}"", ' f'status="{status}"", '
@ -171,7 +173,9 @@ class scanThread (threading.Thread):
except Exception as ex: except Exception as ex:
log(f"Unexpected error: {str(ex)}", flush=True) log(f"Unexpected error: {str(ex)}", flush=True)
logging.info(f'error="{str(ex)}"', exc_info=True) logging.info(
f'hostname="{hostname}", '
f'error="{str(ex)}"', exc_info=True)
# ---------------------------------------------------------- # ----------------------------------------------------------
@ -377,7 +381,9 @@ def print_screen():
init_bar() init_bar()
update_bar(0, flush=True) update_bar(0, flush=True)
log('Ready.', flush=True) log('Ready.', flush=True)
logging.info("pandora-box-start") logging.info(
f'hostname="{hostname}", '
"pandora-box-start")
def end_curses(): def end_curses():
@ -495,32 +501,20 @@ def umount_device():
def log_device_info(dev): def log_device_info(dev):
"""Log device information""" """Log device information"""
logging.info( logging.info(
'device_name="%s", ' f'hostname="{hostname}", '
'path_id="%s", ' f'device_name="{dev.get("DEVNAME")}, '
'bus system="%s", ' f'path_id="{dev.get("ID_PATH")}", '
'USB_driver="%s", ' f'bus system="{dev.get("ID_BUS")}", '
'device_type="%s", ' f'USB_driver="{dev.get("ID_USB_DRIVER")}", '
'device_usage="%s", ' f'device_type="{dev.get("DEVTYPE")}", '
'partition type="%s", ' f'device_usage="{dev.get("ID_FS_USAGE")}", '
'fs_type="%s", ' f'partition type="{dev.get("ID_PART_TABLE_TYPE")}", '
'partition_label="%s", ' f'fs_type="{dev.get("ID_FS_TYPE")}", '
'device_model="%s", ' f'partition_label="{dev.get("ID_FS_LABEL")}", '
'model_id="%s", ' f'device_model="{dev.get("ID_MODEL")}", '
'serial_short="%s", ' f'model_id="{dev.get("ID_MODEL_ID")}", '
'serial="%s"', f'serial_short="dev.get("ID_SERIAL_SHORT")", '
dev.get("DEVNAME"), f'serial="dev.get("ID_SERIAL")"'
dev.get("ID_PATH"),
dev.get("ID_BUS"),
dev.get("ID_USB_DRIVER"),
dev.get("DEVTYPE"),
dev.get("ID_FS_USAGE"),
dev.get("ID_PART_TABLE_TYPE"),
dev.get("ID_FS_TYPE"),
dev.get("ID_FS_LABEL"),
dev.get("ID_MODEL"),
dev.get("ID_MODEL_ID"),
dev.get("ID_SERIAL_SHORT"),
dev.get("ID_SERIAL")
) )
@ -539,7 +533,10 @@ def scan():
statvfs = os.statvfs(mount_point) statvfs = os.statvfs(mount_point)
except Exception as ex: except Exception as ex:
log(f"error={ex}", flush=True) log(f"error={ex}", flush=True)
logging.info("An exception was thrown!", exc_info=True) logging.info(
f'hostname="{hostname}", '
f'error="{str(ex)}"',
exc_info=True)
if not has_curses: if not has_curses:
display_image("ERROR") display_image("ERROR")
return "ERROR" return "ERROR"
@ -597,6 +594,7 @@ def scan():
((time.time() - scan_start_time), file_count, len(infected_files)), ((time.time() - scan_start_time), file_count, len(infected_files)),
flush=True) flush=True)
logging.info( logging.info(
f'hostname="{hostname}", '
f'duration="{int(time.time() - scan_start_time)}", ' f'duration="{int(time.time() - scan_start_time)}", '
f'files_scanned="{file_count}", ' f'files_scanned="{file_count}", '
f'files_infected="{len(infected_files)}"') f'files_infected="{len(infected_files)}"')
@ -620,7 +618,9 @@ def wait():
return device_removed() return device_removed()
except Exception as ex: except Exception as ex:
log(f"Unexpected error: {str(ex)}", flush=True) log(f"Unexpected error: {str(ex)}", flush=True)
logging.info(f'error="{str(ex)}"', exc_info=True) logging.info(
f'hostname="{hostname}", '
f'error="{str(ex)}"', exc_info=True)
return "STOP" return "STOP"
@ -628,7 +628,9 @@ def device_inserted(dev):
global has_curses global has_curses
global device global device
log("Device inserted", flush=True) log("Device inserted", flush=True)
logging.info("device-inserted") logging.info(
f'hostname="{hostname}", '
"device-inserted")
device = dev device = dev
log_device_info(device) log_device_info(device)
if not has_curses: if not has_curses:
@ -646,7 +648,9 @@ def device_removed():
global has_curses global has_curses
global device global device
log("Device removed", flush=True) log("Device removed", flush=True)
logging.info("device-removed") logging.info(
f'hostname="{hostname}", '
"device-removed")
device = None device = None
if not has_curses: if not has_curses:
display_image("WAIT") display_image("WAIT")
@ -678,7 +682,9 @@ def mount():
os.statvfs(mount_point) os.statvfs(mount_point)
except Exception as ex: except Exception as ex:
log(f"Unexpected error: {str(ex)}", flush=True) log(f"Unexpected error: {str(ex)}", flush=True)
logging.info(f'error="{str(ex)}"', exc_info=True) logging.info(
f'hostname="{hostname}", '
f'error="{str(ex)}"', exc_info=True)
if not has_curses: if not has_curses:
display_image("WAIT") display_image("WAIT")
return "WAIT" return "WAIT"
@ -702,7 +708,9 @@ def clean():
if len(infected_files) > 0: if len(infected_files) > 0:
# display message # display message
log(f"{len(infected_files)} infected files detecetd:") log(f"{len(infected_files)} infected files detecetd:")
logging.info(f"infeted_files={len(infected_files)}") logging.info(
f'hostname="{hostname}", '
f"infeted_files={len(infected_files)}")
if not has_curses: if not has_curses:
display_image("BAD") display_image("BAD")
@ -729,11 +737,16 @@ def clean():
try: try:
os.remove(file) os.remove(file)
log(f"{file} removed") log(f"{file} removed")
logging.info(f'removed="{file}"') logging.info(
f'hostname="{hostname}", '
f'removed="{file}"')
files_removed += 1 files_removed += 1
except Exception as ex: except Exception as ex:
log(f"could not remove: {str(ex)}", flush=True) log(f"could not remove: {str(ex)}", flush=True)
logging.info(f'not_removed="{file}, error="{str(ex)}"', exc_info=True) logging.info(
f'hostname="{hostname}", '
f'not_removed="{file}, '
f'error="{str(ex)}"', exc_info=True)
has_error = True has_error = True
if not has_curses: if not has_curses:
display_image("OK") display_image("OK")
@ -742,7 +755,9 @@ def clean():
log('Device cleaned !', flush=True) log('Device cleaned !', flush=True)
else: else:
log('Device not cleaned !', flush=True) log('Device not cleaned !', flush=True)
logging.info(f'cleaned="{files_removed}/{len(infected_files)}"') logging.info(
f'hostname="{hostname}", '
f'cleaned="{files_removed}/{len(infected_files)}"')
else: else:
if not has_curses: if not has_curses:
display_image("OK") display_image("OK")
@ -803,6 +818,27 @@ def loop(state):
return "STOP" return "STOP"
# --------------------------------------
def get_lock(process_name):
""" Get a lock to check that Pandora-box is not already running """
# Without holding a reference to our socket somewhere it gets garbage
# collected when the function exits
get_lock._lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
# The null byte (\0) means the socket is created
# in the abstract namespace instead of being created
# on the file system itself.
# Works only in Linux
get_lock._lock_socket.bind('\0' + process_name)
except socket.error:
print('Pandora-box is already running !', file=sys.stderr)
sys.exit()
# -------------------------------------- # --------------------------------------
def main(args): def main(args):
@ -814,12 +850,13 @@ def main(args):
except Exception as ex: except Exception as ex:
print({str(ex)}) print({str(ex)})
log(f"Unexpected error: {str(ex)}", flush=True) log(f"Unexpected error: {str(ex)}", flush=True)
logging.info(f'error="{str(ex)}"', exc_info=True) logging.info(
f'hostname="{hostname}", '
f'error="{str(ex)}"', exc_info=True)
finally: finally:
end_curses() end_curses()
if __name__ == "__main__": if __name__ == "__main__":
print("start") get_lock('pandora-box')
curses.wrapper(main) curses.wrapper(main)
print("done")