Skip to content

Commit b68c661

Browse files
committed
Cleaning code and adding docstrings
1 parent 6d36101 commit b68c661

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/main.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323

2424
class ConnectionInfo:
25+
""" Class to store connection information """
26+
2527
def __init__(self, src_ip, dst_domain, method):
2628
self.src_ip = src_ip
2729
self.dst_domain = dst_domain
@@ -32,8 +34,10 @@ def __init__(self, src_ip, dst_domain, method):
3234

3335

3436
class ProxyServer:
37+
""" Class to handle the proxy server """
3538

36-
def __init__(self, host, port, blacklist, log_access, log_err, no_blacklist, auto_blacklist, quiet):
39+
def __init__(self, host, port, blacklist,
40+
log_access, log_err, no_blacklist, auto_blacklist, quiet):
3741

3842
self.host = host
3943
self.port = port
@@ -78,6 +82,7 @@ def print(self, *args, **kwargs):
7882
Parameters:
7983
**kwargs: Any arguments accepted by the built-in print() function.
8084
"""
85+
8186
if not self.quiet:
8287
print(*args, **kwargs)
8388

@@ -127,6 +132,7 @@ def load_blacklist(self):
127132
"""
128133
Load the blacklist from the specified file.
129134
"""
135+
130136
if self.no_blacklist or self.auto_blacklist:
131137
return
132138
if not os.path.exists(self.blacklist):
@@ -147,6 +153,7 @@ async def run(self):
147153
protocol handler. The server is then started with the `serve_forever`
148154
method.
149155
"""
156+
150157
self.print_banner()
151158
if not self.quiet:
152159
asyncio.create_task(self.display_stats())
@@ -167,6 +174,7 @@ def print_banner(self):
167174
"""
168175
Print a banner with the NoDPI logo and information about the proxy.
169176
"""
177+
170178
self.print(
171179
'''
172180
\033[92m ██████ █████ ██████████ ███████████ █████
@@ -205,6 +213,7 @@ async def display_stats(self):
205213
"""
206214
Display the current statistics of the proxy server.
207215
"""
216+
208217
while True:
209218
await asyncio.sleep(1)
210219
current_time = time.time()
@@ -350,7 +359,7 @@ async def handle_connection(self, reader, writer):
350359
),
351360
]
352361
)
353-
except Exception as e:
362+
except Exception:
354363
try:
355364
writer.write(b"HTTP/1.1 500 Internal Server Error\r\n\r\n")
356365
await writer.drain()
@@ -360,8 +369,10 @@ async def handle_connection(self, reader, writer):
360369
host_err = host
361370
except Exception:
362371
host_err = "Unknown"
363-
self.logger.error(str(host_err.decode()) +
364-
": " + traceback.format_exc())
372+
373+
self.logger.error("%s: %s", host_err.decode(),
374+
traceback.format_exc())
375+
365376
writer.close()
366377

367378
async def pipe(self, reader, writer, direction, conn_key):
@@ -378,6 +389,7 @@ async def pipe(self, reader, writer, direction, conn_key):
378389
direction (str): The direction of the transfer (in or out)
379390
conn_key (tuple): The connection key
380391
"""
392+
381393
try:
382394
while not reader.at_eof() and not writer.is_closing():
383395
data = await reader.read(1500)
@@ -392,10 +404,10 @@ async def pipe(self, reader, writer, direction, conn_key):
392404
conn_info.traffic_in += len(data)
393405
writer.write(data)
394406
await writer.drain()
395-
except Exception as e:
407+
except Exception:
396408
host_err = conn_info.dst_domain
397-
self.logger.error(str(host_err.decode()) +
398-
": " + traceback.format_exc())
409+
self.logger.error("%s: %s", host_err.decode(),
410+
traceback.format_exc())
399411
finally:
400412
writer.close()
401413
async with self.connections_lock:
@@ -420,10 +432,11 @@ async def fragment_data(self, reader, writer):
420432
reader (asyncio.StreamReader): The reader to read from
421433
writer (asyncio.StreamWriter): The writer to write to
422434
"""
435+
423436
try:
424437
head = await reader.read(5)
425438
data = await reader.read(2048)
426-
except Exception as e:
439+
except Exception:
427440
self.logger.error(traceback.format_exc())
428441
return
429442

@@ -464,6 +477,7 @@ async def shutdown(self):
464477
This function closes the server and cancels all tasks running on the
465478
event loop. If a server is not running, the function does nothing.
466479
"""
480+
467481
if self.server:
468482
self.server.close()
469483
await self.server.wait_closed()
@@ -516,7 +530,11 @@ def parse_args():
516530

517531
@staticmethod
518532
def manage_autostart(action="install"):
519-
"""Manage proxy autostart on Windows"""
533+
""" Manage proxy autostart on Windows
534+
535+
Parameters:
536+
action (str): "install" or "uninstall"
537+
"""
520538

521539
if sys.platform != "win32":
522540
print(
@@ -557,6 +575,9 @@ def manage_autostart(action="install"):
557575

558576
@classmethod
559577
async def run(cls):
578+
"""
579+
Run the proxy server
580+
"""
560581

561582
logging.getLogger("asyncio").setLevel(logging.CRITICAL)
562583

0 commit comments

Comments
 (0)