Skip to content

Commit 69cf4d8

Browse files
committed
fix pylint
1 parent c458b3b commit 69cf4d8

4 files changed

Lines changed: 29 additions & 35 deletions

File tree

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[MESSAGES CONTROL]
2-
disable=too-many-instance-attributes, too-many-arguments, too-many-positional-arguments
2+
disable=too-many-instance-attributes, too-many-arguments, too-many-positional-arguments, R0801

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ If you encounter any problems, or if you want to use the program in a particular
8484
- Proxy authentication
8585
- Benchmark
8686
- Admin mode, statistiques and real time request
87-
- Shortcut
8887
- Custom header
8988

9089
---

utils/proxy.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ProxyServer:
3434
# pylint: disable=too-many-locals
3535
def __init__(self, host, port, debug, access_log, block_log,
3636
html_403, no_filter, filter_mode, no_logging_access, no_logging_block, ssl_inspect,
37-
blocked_sites, blocked_url, shortcuts, inspect_ca_cert, inspect_ca_key, inspect_certs_folder):
37+
blocked_sites, blocked_url, shortcuts, inspect_ca_cert,
38+
inspect_ca_key, inspect_certs_folder):
3839
"""
3940
Initializes the ProxyServer instance with the provided configurations.
4041
"""
@@ -204,12 +205,12 @@ def handle_http_request(self, client_socket, request):
204205
print(shortcut_url)
205206
if shortcut_url:
206207
response = (
207-
"HTTP/1.1 302 Found\r\n"
208-
"Location: {shortcut_url}\r\n"
209-
"Content-Length: 0\r\n"
208+
f"HTTP/1.1 302 Found\r\n"
209+
f"Location: {shortcut_url}\r\n"
210+
f"Content-Length: 0\r\n"
210211
"\r\n"
211-
).format(shortcut_url=shortcut_url)
212-
212+
)
213+
213214
client_socket.sendall(response.encode())
214215
client_socket.close()
215216
return
@@ -268,16 +269,16 @@ def forward_request_to_server(self, client_socket, request, url):
268269
client_socket.send(response)
269270
else:
270271
break
271-
except Exception as e:
272-
self.console_logger.error(f"Error connecting to the server {server_host}: {e}")
272+
except (socket.timeout, socket.gaierror, ConnectionRefusedError, OSError) as e:
273+
self.console_logger.error("Error connecting to the server %s : %s", server_host, e)
273274
response = (
274275
f"HTTP/1.1 502 Bad Gateway\r\n"
275276
f"Content-Length: {len('Bad Gateway')} \r\n"
276-
f"\r\n"
277+
"\r\n"
277278
f"Bad Gateway"
278279
)
279280
client_socket.sendall(response.encode())
280-
client_socket.close()
281+
client_socket.close()
281282

282283
def parse_url(self, url):
283284
"""
@@ -443,16 +444,16 @@ def handle_https_connection(self, client_socket, first_line):
443444
first_line
444445
)
445446
self.transfer_data_between_sockets(client_socket, server_socket)
446-
except Exception as e:
447-
self.console_logger.error(f"Error connecting to the server {server_host}: {e}")
447+
except (socket.timeout, socket.gaierror, ConnectionRefusedError, OSError) as e:
448+
self.console_logger.error("Error connecting to the server %s: %s", server_host, e)
448449
response = (
449450
f"HTTP/1.1 502 Bad Gateway\r\n"
450451
f"Content-Length: {len('Bad Gateway')} \r\n"
451452
f"\r\n"
452453
f"Bad Gateway"
453454
)
454455
client_socket.sendall(response.encode())
455-
client_socket.close()
456+
client_socket.close()
456457

457458
def transfer_data_between_sockets(self, client_socket, server_socket):
458459
"""

utils/shortcuts.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
"""
2-
filter.py
2+
shortcuts.py
33
4-
This module contains functions and a process to filter and block domains and URLs.
5-
It loads blocked domain names and URLs from specified files, then listens for
6-
incoming requests to check if the domain or URL should be blocked.
4+
This module contains functions and a process to load and manage URL shortcuts.
5+
It loads shortcuts (alias to URL mappings) from a specified file, and provides
6+
a process that listens for requests to resolve an alias to its corresponding URL.
77
88
Functions:
9-
- load_blacklist: Loads blocked FQDNs and URLs from files into sets for fast lookup.
10-
- filter_process: The process that checks whether a domain or URL is blocked.
9+
- load_shortcuts: Loads URL alias mappings from a file into a dictionary for fast lookup.
10+
- shortcuts_process: The process that listens for alias requests and resolves them to URLs.
1111
"""
1212

1313
import multiprocessing
1414
import time
1515
import sys
1616
import threading
17-
import requests
1817

1918
def load_shortcuts(shortcuts_path: str) -> dict:
2019
"""
21-
Loads blocked FQDNs or URLs from a file or URL into a set for fast lookup.
20+
Loads URL alias mappings from a file into a dictionary for fast lookup.
2221
2322
Args:
24-
blocked_sites_path (str): The path or URL to the file containing blocked FQDNs.
25-
blocked_url_path (str): The path or URL to the file containing blocked URLs.
26-
filter_mode (str): Mode to determine if we load from local file or HTTP URL.
23+
shortcuts_path (str): The path to the file containing alias=URL mappings.
2724
2825
Returns:
29-
set: A set of blocked domains/URLs.
26+
dict: A dictionary mapping aliases to URLs.
3027
"""
3128
shortcuts = {}
3229

3330
with open(shortcuts_path, 'r', encoding='utf-8') as f:
3431
for line in f:
3532
line = line.strip()
3633
if "=" in line:
37-
alias, url = line.split("=", 1)
34+
alias, url = line.split("=", 1)
3835
shortcuts[alias.strip()] = url.strip()
3936

4037
return shortcuts
@@ -46,15 +43,12 @@ def shortcuts_process(
4643
shortcuts_path: str
4744
) -> None:
4845
"""
49-
Process that listens for requests and checks if the domain/URL should be blocked.
46+
Process that listens for alias requests and resolves them to URLs.
5047
5148
Args:
52-
queue (multiprocessing.Queue): A queue to receive URL/domain for checking.
53-
result_queue (multiprocessing.Queue): A queue to send back the result of
54-
the filtering (blocked or allowed).
55-
filter_mode (str): Filter list mode (local or http).
56-
blocked_sites_path (str): The path to the file containing blocked FQDNs.
57-
blocked_url_path (str): The path to the file containing blocked URLs.
49+
queue (multiprocessing.Queue): A queue to receive alias for URL resolution.
50+
result_queue (multiprocessing.Queue): A queue to send back the resolved URL.
51+
shortcuts_path (str): The path to the file containing alias=URL mappings.
5852
"""
5953
manager = multiprocessing.Manager()
6054
shortcuts_data = manager.dict({

0 commit comments

Comments
 (0)