From 00b7526ca30fe4ee6b26f44ad2c3bbbe9a89d6eb Mon Sep 17 00:00:00 2001 From: Phil Huang Date: Fri, 4 Oct 2024 01:15:42 +0800 Subject: [PATCH 1/2] Parse and validate config file before curses.wrapper --- deadman | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/deadman b/deadman index ce40135..81cf36e 100755 --- a/deadman +++ b/deadman @@ -801,6 +801,27 @@ def pingcmd(osname, ipv): return None +def parse_and_validate_config(configfile): + + try: + with open(configfile, 'r') as file: + for line in file: + stripped_line = line.strip() + # Only check without '#' + if not stripped_line.startswith('#'): + # Check if hping3 is installed and if the user has root privileges + if 'tcp=dstport:' in stripped_line: + if not which('hping3'): + print("hping3 is not installed. Please install it to use tcp based ping.") + sys.exit(-1) + if os.geteuid() != 0: + print("Root privileges are required to use tcp based ping. Please run as root.") + sys.exit(-1) + except FileNotFoundError: + print(f"Configuration file {configfile} not found.") + sys.exit(-1) + + return None def main(stdscr, configfile, async_mode): @@ -810,9 +831,6 @@ def main(stdscr, configfile, async_mode): curses.init_pair(UP_COLOR, curses.COLOR_GREEN, -1) curses.init_pair(DOWN_COLOR, curses.COLOR_RED, -1) - """ - XXX: parse and validating config file shoud be done before curses.wrapper. - """ deadman = Deadman(stdscr, configfile) if async_mode: @@ -847,6 +865,9 @@ if __name__ == '__main__': BLINK_ARROW = args.blink_arrow LOGDIR = args.logdir + # Parse and validate config file + parse_and_validate_config(args.configfile.name) + try: curses.wrapper(main, args.configfile, args.async_mode) From cc77f6163f68b1f40c1fdf2916deae9f932628bf Mon Sep 17 00:00:00 2001 From: Phil Huang Date: Fri, 4 Oct 2024 01:21:58 +0800 Subject: [PATCH 2/2] Fixed SyntaxWarning: invalid escape sequence '\s' --- deadman | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/deadman b/deadman index 81cf36e..d262e12 100755 --- a/deadman +++ b/deadman @@ -732,10 +732,10 @@ class Deadman: for line in configfile: - line = re.sub('\t', ' ', line) - line = re.sub('\s+', ' ', line) - line = re.sub('^#.*', '', line) - line = re.sub(';\s*#', '', line) + line = re.sub(r'\t', ' ', line) + line = re.sub(r'\s+', ' ', line) + line = re.sub(r'^#.*', '', line) + line = re.sub(r';\s*#', '', line) line = line.strip() if line == "":