Skip to content

Commit 3211d45

Browse files
committed
add cancel inspection for bank or medical websites
1 parent 69cf4d8 commit 3211d45

6 files changed

Lines changed: 132 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
- Inspection SSL
3535
- Custom page for 403 Forbidden
3636
- Support distant (http) blacklist
37+
- Shortcuts
38+
- Cancel inspection on bank site
3739

3840
## 📦 **Installation**
3941

@@ -77,7 +79,6 @@ If you encounter any problems, or if you want to use the program in a particular
7779

7880
## 🔧 **To do**
7981

80-
- Cancel inspection on bank site
8182
- Support content analysis
8283
- Caching of latest and most searched pages
8384
- Adding ACL

config.ini.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ shortcuts = config/shortcuts.txt
2525
ssl_inspect = false
2626
inspect_ca_cert = certs/ca/cert.pem
2727
inspect_ca_key = certs/ca/key.pem
28-
inspect_certs_folder = certs/
28+
inspect_certs_folder = certs/
29+
cancel_inspect = config/cancel_inspect.txt

config/cancel_inspect.example.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mybank.com

pyproxy.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
type=str,
4444
help="Path to the block log file"
4545
)
46-
parser.add_argument(
47-
"--shortcuts",
48-
type=str,
49-
help="Path to the shortcuts file"
50-
)
5146
parser.add_argument(
5247
"--html-403",
5348
type=str,
@@ -70,6 +65,11 @@
7065
type=str,
7166
help="Path to the text file containing the list of URLs to block"
7267
)
68+
parser.add_argument(
69+
"--shortcuts",
70+
type=str,
71+
help="Path to the text file containing the list of shortcuts"
72+
)
7373
parser.add_argument("--no-logging-access", action="store_true", help="Disable access logging")
7474
parser.add_argument("--no-logging-block", action="store_true", help="Disable block logging")
7575
parser.add_argument("--ssl-inspect", action="store_true", help="Enable SSL inspection")
@@ -80,6 +80,11 @@
8080
type=str,
8181
help="Path to the generated certificates folder"
8282
)
83+
parser.add_argument(
84+
"--cancel-inspect",
85+
type=str,
86+
help="Path to the text file containing the list of URLs without ssl inspection"
87+
)
8388

8489
args = parser.parse_args()
8590

@@ -158,6 +163,11 @@
158163
if args.inspect_ca_key
159164
else config.get('Security', 'inspect_ca_key', fallback="certs/ca/key.pem")
160165
)
166+
cancel_inspect = (
167+
args.inspect_ca_key
168+
if args.inspect_ca_key
169+
else config.get('Security', 'cancel_inspect', fallback="config/cancel_inspect.txt")
170+
)
161171

162172
proxy = ProxyServer(
163173
host=host,
@@ -176,7 +186,8 @@
176186
shortcuts=shortcuts,
177187
inspect_ca_cert=inspect_ca_cert,
178188
inspect_ca_key=inspect_ca_key,
179-
inspect_certs_folder=inspect_certs_folder
189+
inspect_certs_folder=inspect_certs_folder,
190+
cancel_inspect=cancel_inspect
180191
)
181192

182193
proxy.start()

utils/cancel_inspect.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
cancel_inspect.py
3+
4+
This module contains functions and a process to load and monitor cancel inspection entries.
5+
It reads a file containing cancel inspection data and checks whether specific entries exist
6+
in that file. The file is monitored in a background thread for live updates.
7+
8+
Functions:
9+
- load_cancel_inspect: Loads the cancel inspection list from a file into a list.
10+
- cancel_inspect_process: Process that listens for URL-like entries and checks
11+
if they exist in the cancel inspection list.
12+
"""
13+
14+
import multiprocessing
15+
import time
16+
import sys
17+
import threading
18+
19+
def load_cancel_inspect(cancel_inspect_path: str) -> dict:
20+
"""
21+
Loads cancel inspection entries from a file into a list.
22+
23+
Args:
24+
cancel_inspect_path (str): The path to the file containing the entries.
25+
26+
Returns:
27+
list: A list containing each line (entry) from the file.
28+
"""
29+
cancel_inspect = []
30+
31+
with open(cancel_inspect_path, 'r', encoding='utf-8') as f:
32+
for line in f:
33+
cancel_inspect.append(line)
34+
35+
return cancel_inspect
36+
37+
# pylint: disable=too-many-locals
38+
def cancel_inspect_process(
39+
queue: multiprocessing.Queue,
40+
result_queue: multiprocessing.Queue,
41+
cancel_inspect_path: str
42+
) -> None:
43+
"""
44+
Process that monitors the cancel inspection file and checks if received entries exist in it.
45+
46+
Args:
47+
queue (multiprocessing.Queue): A queue to receive entries to check.
48+
result_queue (multiprocessing.Queue): A queue to send back True/False depending on match.
49+
cancel_inspect_path (str): Path to the file containing cancel inspection entries.
50+
"""
51+
manager = multiprocessing.Manager()
52+
cancel_inspect_data = manager.list(
53+
load_cancel_inspect(cancel_inspect_path)
54+
)
55+
56+
error_event = threading.Event()
57+
58+
def file_monitor() -> None:
59+
try:
60+
while True:
61+
new_cancel_inspect = load_cancel_inspect(cancel_inspect_path)
62+
63+
cancel_inspect_data = new_cancel_inspect
64+
65+
time.sleep(5)
66+
except (IOError, ValueError) as e:
67+
print(f"File monitor error: {e}")
68+
error_event.set()
69+
70+
monitor_thread = threading.Thread(target=file_monitor, daemon=True)
71+
monitor_thread.start()
72+
73+
while True:
74+
if error_event.is_set():
75+
print("Error detected in file monitor thread, terminating process.")
76+
sys.exit(1)
77+
78+
try:
79+
url = queue.get()
80+
print("url", url)
81+
print("cancel_inspect_data", cancel_inspect_data)
82+
if url in cancel_inspect_data:
83+
result_queue.put(True)
84+
else:
85+
result_queue.put(False)
86+
87+
except KeyboardInterrupt:
88+
break

utils/proxy.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from utils.filter import filter_process
2424
from utils.shortcuts import shortcuts_process
25+
from utils.cancel_inspect import cancel_inspect_process
2526
from utils.logger import configure_file_logger, configure_console_logger
2627

2728
class ProxyServer:
@@ -35,7 +36,7 @@ class ProxyServer:
3536
def __init__(self, host, port, debug, access_log, block_log,
3637
html_403, no_filter, filter_mode, no_logging_access, no_logging_block, ssl_inspect,
3738
blocked_sites, blocked_url, shortcuts, inspect_ca_cert,
38-
inspect_ca_key, inspect_certs_folder):
39+
inspect_ca_key, inspect_certs_folder, cancel_inspect):
3940
"""
4041
Initializes the ProxyServer instance with the provided configurations.
4142
"""
@@ -53,10 +54,14 @@ def __init__(self, host, port, debug, access_log, block_log,
5354
self.shortcuts_proc = None
5455
self.shortcuts_queue = multiprocessing.Queue()
5556
self.shortcuts_result_queue = multiprocessing.Queue()
57+
self.cancel_inspect_proc = None
58+
self.cancel_inspect_queue = multiprocessing.Queue()
59+
self.cancel_inspect_result_queue = multiprocessing.Queue()
5660
self.console_logger = configure_console_logger()
5761
self.config_blocked_sites = blocked_sites
5862
self.config_blocked_url = blocked_url
5963
self.config_shortcuts = shortcuts
64+
self.config_cancel_inspect = cancel_inspect
6065
self.config_inspect_cert = inspect_ca_cert
6166
self.config_inspect_key = inspect_ca_key
6267
self.config_inspect_certs_folder = inspect_certs_folder
@@ -145,6 +150,18 @@ def start(self):
145150
self.shortcuts_proc.start()
146151
self.console_logger.debug("[*] Starting the shortcuts process...")
147152

153+
if self.config_cancel_inspect and os.path.isfile(self.config_cancel_inspect):
154+
self.cancel_inspect_proc = multiprocessing.Process(
155+
target=cancel_inspect_process,
156+
args=(
157+
self.cancel_inspect_queue,
158+
self.cancel_inspect_result_queue,
159+
self.config_cancel_inspect
160+
)
161+
)
162+
self.cancel_inspect_proc.start()
163+
self.console_logger.debug("[*] Starting the cancel inspection process...")
164+
148165
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
149166
server.bind(self.host_port)
150167
server.listen(10)
@@ -198,11 +215,8 @@ def handle_http_request(self, client_socket, request):
198215

199216
if self.config_shortcuts:
200217
domain, _ = self.parse_url(url)
201-
print(url)
202-
print(domain)
203218
self.shortcuts_queue.put(domain)
204219
shortcut_url = self.shortcuts_result_queue.get()
205-
print(shortcut_url)
206220
if shortcut_url:
207221
response = (
208222
f"HTTP/1.1 302 Found\r\n"
@@ -344,6 +358,10 @@ def handle_https_connection(self, client_socket, first_line):
344358
return
345359

346360
if self.ssl_inspect:
361+
self.cancel_inspect_queue.put(server_host)
362+
not_inspect = self.cancel_inspect_result_queue.get()
363+
364+
if self.ssl_inspect and not not_inspect:
347365
cert_path, key_path = self.generate_certificate(server_host)
348366
client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
349367
client_context.load_cert_chain(certfile=cert_path, keyfile=key_path)

0 commit comments

Comments
 (0)