Skip to content

Commit 48d5f77

Browse files
authored
Merge pull request #512 from AutomationSolutionz/mitm-error-handle
Handle port in use case in mitmproxy
2 parents 23a8855 + 828b7c4 commit 48d5f77

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Framework/Built_In_Automation/Sequential_Actions/common_functions.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
from imap_tools import MailBox
2727
import re
2828
from typing import List
29-
29+
import socket
30+
import signal
31+
import os
3032

3133
try:
3234
import xlwings as xw
@@ -6742,7 +6744,6 @@ def stop_ssh_tunnel(data_set):
67426744

67436745
@logger
67446746
def proxy_server(data_set):
6745-
import os
67466747
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
67476748

67486749
proxy_var = None
@@ -6756,12 +6757,20 @@ def proxy_server(data_set):
67566757
if left.lower().strip() == 'proxy server':
67576758
proxy_var = right.strip()
67586759

6759-
if action == None:
6760+
if action is None:
67606761
CommonUtil.ExecLog(sModuleInfo, "Incorrect dataset", 3)
67616762
return "zeuz_failed"
67626763

6764+
def is_port_in_use(port):
6765+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
6766+
return sock.connect_ex(('localhost', port)) == 0
67636767

67646768
if action == 'start':
6769+
# Check if the port is already in use
6770+
if is_port_in_use(port):
6771+
CommonUtil.ExecLog(sModuleInfo, f"Port {port} is already in use.", 3)
6772+
return "zeuz_failed"
6773+
67656774
CommonUtil.ExecLog(sModuleInfo, f"{action.capitalize()}ing proxy server on port {port}", 1)
67666775

67676776
proxy_log_dir = Path(sr.Get_Shared_Variables("zeuz_download_folder")).parent / 'proxy_log'
@@ -6771,7 +6780,8 @@ def proxy_server(data_set):
67716780
CommonUtil.ExecLog(sModuleInfo, f"Proxy Log file: {output_file_path}", 1)
67726781

67736782
captured_network_file_path = proxy_log_dir / 'captured_network_data.csv'
6774-
CommonUtil.ExecLog(sModuleInfo, f"Captured Network file: {output_file_path}", 1)
6783+
CommonUtil.ExecLog(sModuleInfo, f"Captured Network file: {captured_network_file_path}", 1)
6784+
67756785
# Open the output file in append mode
67766786
with open(r'{}'.format(output_file_path), 'a') as output_file:
67776787
# Start the subprocess
@@ -6793,19 +6803,18 @@ def proxy_server(data_set):
67936803
CommonUtil.mitm_proxy_pids.append(pid)
67946804
CommonUtil.ExecLog(sModuleInfo, f"Started process with PID: {pid}", 1)
67956805

6796-
sr.Set_Shared_Variables(proxy_var, {"pid":pid,"captured_network_file_path":captured_network_file_path,"log_file":output_file_path})
6806+
sr.Set_Shared_Variables(proxy_var, {"pid": pid, "captured_network_file_path": captured_network_file_path, "log_file": output_file_path})
67976807
return "passed"
67986808
else:
6799-
import signal
6800-
6809+
# Action is stop
68016810
if CommonUtil.mitm_proxy_pids:
68026811
try:
68036812
pid = CommonUtil.mitm_proxy_pids[0]
68046813
os.kill(pid, signal.SIGTERM)
6805-
CommonUtil.ExecLog(sModuleInfo,f"Process with PID {pid} has been terminated.",1)
6814+
CommonUtil.ExecLog(sModuleInfo, f"Process with PID {pid} has been terminated.", 1)
68066815
CommonUtil.mitm_proxy_pids.pop()
68076816
except OSError as e:
6808-
CommonUtil.ExecLog(sModuleInfo,f"Error: {e}", 3)
6817+
CommonUtil.ExecLog(sModuleInfo, f"Error: {e}", 3)
68096818

68106819
CommonUtil.ExecLog(sModuleInfo, f"{action.capitalize()}ing proxy server on port {port}", 1)
68116820
return "passed"

0 commit comments

Comments
 (0)