Skip to content

Commit

Permalink
Merge pull request #458 from stratosphereips/alya/fix-using-k-parameter
Browse files Browse the repository at this point in the history
Fix using -k to kill servers that were manually killed by the user
  • Loading branch information
AlyaGomaa authored Feb 15, 2024
2 parents d60b9f5 + 1e2a1f7 commit 78366c8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
39 changes: 25 additions & 14 deletions managers/redis_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, main):
def get_start_port(self):
return self.start_port

def log_redis_server_PID(self, redis_port: int, redis_pid: int):
def log_redis_server_pid(self, redis_port: int, redis_pid: int):
now = utils.convert_format(datetime.now(), utils.alerts_format)
try:
# used in case we need to remove the line using 6379 from running logfile
Expand All @@ -50,7 +50,7 @@ def log_redis_server_PID(self, redis_port: int, redis_pid: int):
# last run was by root, change the file ownership to non-root
os.remove(self.running_logfile)
open(self.running_logfile, 'w').close()
self.log_redis_server_PID(redis_port, redis_pid)
self.log_redis_server_pid(redis_port, redis_pid)

if redis_port == 6379:
# remove the old logline using this port
Expand All @@ -63,7 +63,7 @@ def load_redis_db(self, redis_port):
self.main.input_information = os.path.basename(self.main.args.db)
redis_pid: int = self.get_pid_of_redis_server(redis_port)
self.zeek_folder = '""'
self.log_redis_server_PID(redis_port, redis_pid)
self.log_redis_server_pid(redis_port, redis_pid)
self.remove_old_logline(redis_port)

print(
Expand Down Expand Up @@ -117,20 +117,23 @@ def check_redis_database(
except Exception as ex:
# only try to open redis-server twice.
if tries == 2:
print(f'[Main] Problem starting redis cache database. \n{ex}\nStopping')
print(f'[Main] Problem starting redis cache database.'
f' \n{ex}\nStopping')
self.main.terminate_slips()
return False

print('[Main] Starting redis cache database..')
os.system('redis-server config/redis.conf --daemonize yes > /dev/null 2>&1')
os.system('redis-server config/redis.conf --daemonize yes '
' > /dev/null 2>&1')
# give the server time to start
time.sleep(1)
tries += 1


def get_random_redis_port(self) -> int:
"""
Keeps trying to connect to random generated ports until we found an available port.
Keeps trying to connect to random generated ports until
we found an available port.
returns the port number
"""
for port in range(self.start_port, self.end_port+1):
Expand Down Expand Up @@ -171,7 +174,8 @@ def clear_redis_cache_database(

def close_all_ports(self):
"""
Closes all the redis ports in running_slips_info.txt and in slips supported range of ports
Closes all the redis ports in running_slips_info.txt and
in slips supported range of ports
"""
if not hasattr(self, 'open_servers_PIDs'):
self.get_open_redis_servers()
Expand All @@ -192,7 +196,8 @@ def close_all_ports(self):
self.kill_redis_server(pid)


# print(f"Successfully closed all redis servers on ports {self.start_port} to {self.end_port}")
# print(f"Successfully closed all redis servers on ports
# {self.start_port} to {self.end_port}")
print("Successfully closed all open redis servers")

with contextlib.suppress(FileNotFoundError):
Expand Down Expand Up @@ -340,8 +345,10 @@ def get_port_of_redis_server(self, pid: int) -> Union[int, bool]:

def flush_redis_server(self, pid: int=None, port: int=None):
"""
Flush the redis server on this pid, only 1 param should be given, pid or port
:param pid: can be False if port is given
Flush the redis server on this pid, only 1 param should be
given, pid or port
:kwarg pid: can be False if port is given
:kwarg port: redis server port to flush
Gets the pid of the port if not given
"""
if not port and not pid:
Expand All @@ -368,10 +375,14 @@ def flush_redis_server(self, pid: int=None, port: int=None):
start_sqlite=False,
start_redis_server=False
)
db.rdb.r.flushall()
db.rdb.r.flushdb()
db.rdb.r.script_flush()
return True
# if the redis server opened by slips is closed manually by the
# user, not by slips, slips won't be able to connect to it
# that's why we check for db.rdb
if db.rdb:
db.rdb.r.flushall()
db.rdb.r.flushdb()
db.rdb.r.script_flush()
return True
except redis.exceptions.ConnectionError:
# server already killed!
return False
Expand Down
4 changes: 1 addition & 3 deletions slips/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,10 @@ def handle_flows_from_stdin(self, input_information):
):
print(f"[Main] Invalid file path {input_information}. Stopping.")
sys.exit(-1)
return False

if self.mode == "daemonized":
print("Can't read input from stdin in daemonized mode. " "Stopping")
sys.exit(-1)
return False
line_type = input_information
input_type = "stdin"
return input_type, line_type.lower()
Expand Down Expand Up @@ -602,7 +600,7 @@ def start(self):
# log the PID of the started redis-server
# should be here after we're sure that the server was started
redis_pid = self.redis_man.get_pid_of_redis_server(self.redis_port)
self.redis_man.log_redis_server_PID(self.redis_port, redis_pid)
self.redis_man.log_redis_server_pid(self.redis_port, redis_pid)

self.db.set_slips_mode(self.mode)

Expand Down
2 changes: 1 addition & 1 deletion slips_files/core/helpers/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def clear_redis_cache(self):
self.main.redis_man.clear_redis_cache_database()
self.main.input_information = ""
self.main.zeek_dir = ""
self.main.redis_man.log_redis_server_PID(
self.main.redis_man.log_redis_server_pid(
6379, self.main.redis_man.get_pid_of_redis_server(6379)
)
self.main.terminate_slips()
Expand Down

0 comments on commit 78366c8

Please sign in to comment.