Skip to content

Commit 2fe57ce

Browse files
committed
add utils/modules folders
1 parent d214e47 commit 2fe57ce

10 files changed

Lines changed: 337 additions & 352 deletions

File tree

pyproxy.py

Lines changed: 31 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -4,180 +4,37 @@
44
to those URLs. The proxy can handle both HTTP and HTTPS requests, and logs access and block events.
55
"""
66

7-
import argparse
8-
from rich_argparse import MetavarTypeRichHelpFormatter
9-
10-
from utils.proxy import ProxyServer
11-
from utils.config import load_config
12-
from utils.version import __version__
13-
14-
if __name__ == "__main__":
15-
parser = argparse.ArgumentParser(
16-
description="Lightweight and fast python web proxy",
17-
formatter_class=MetavarTypeRichHelpFormatter
18-
)
19-
parser.add_argument(
20-
"-v",
21-
"--version",
22-
action='version',
23-
version=__version__,
24-
help="Show version"
25-
)
26-
parser.add_argument("--debug", action="store_true", help="Enable debug logging")
27-
parser.add_argument("-H", "--host", type=str, help="IP address to listen on")
28-
parser.add_argument("-P", "--port", type=int, help="Port to listen on")
29-
parser.add_argument(
30-
"-f",
31-
"--config-file",
32-
type=str,
33-
default="./config.ini",
34-
help="Path to config.ini file"
35-
)
36-
parser.add_argument(
37-
"--access-log",
38-
type=str,
39-
help="Path to the access log file"
40-
)
41-
parser.add_argument(
42-
"--block-log",
43-
type=str,
44-
help="Path to the block log file"
45-
)
46-
parser.add_argument(
47-
"--html-403",
48-
type=str,
49-
help="Path to the custom 403 Forbidden HTML page"
50-
)
51-
parser.add_argument("--no-filter", action="store_true", help="Disable URL and domain filtering")
52-
parser.add_argument(
53-
"--filter-mode",
54-
type=str,
55-
choices=["local", "http"],
56-
help="Filter list mode"
57-
)
58-
parser.add_argument(
59-
"--blocked-sites",
60-
type=str,
61-
help="Path to the text file containing the list of sites to block"
62-
)
63-
parser.add_argument(
64-
"--blocked-url",
65-
type=str,
66-
help="Path to the text file containing the list of URLs to block"
67-
)
68-
parser.add_argument(
69-
"--shortcuts",
70-
type=str,
71-
help="Path to the text file containing the list of shortcuts"
72-
)
73-
parser.add_argument(
74-
"--custom-header",
75-
type=str,
76-
help="Path to the json file containing the list of custom headers"
77-
)
78-
parser.add_argument("--no-logging-access", action="store_true", help="Disable access logging")
79-
parser.add_argument("--no-logging-block", action="store_true", help="Disable block logging")
80-
parser.add_argument("--ssl-inspect", action="store_true", help="Enable SSL inspection")
81-
parser.add_argument("--inspect-ca-cert", type=str, help="Path to the CA certificate")
82-
parser.add_argument("--inspect-ca-key", type=str, help="Path to the CA key")
83-
parser.add_argument(
84-
"--inspect-certs-folder",
85-
type=str,
86-
help="Path to the generated certificates folder"
87-
)
88-
parser.add_argument(
89-
"--cancel-inspect",
90-
type=str,
91-
help="Path to the text file containing the list of URLs without ssl inspection"
92-
)
93-
94-
args = parser.parse_args()
7+
from utils.server import ProxyServer
8+
from utils.config import parse_args, load_config, get_config_value
959

10+
# pylint: disable=R0914,C0301
11+
def main():
12+
"""
13+
Main entry point of the proxy server. It parses command-line arguments, loads the configuration file,
14+
retrieves configuration values, and starts the proxy server.
15+
"""
16+
args = parse_args()
9617
config = load_config(args.config_file)
9718

98-
host = args.host if args.host else config.get('Server', 'host', fallback="0.0.0.0")
99-
port = args.port if args.port else config.getint('Server', 'port', fallback=8080)
100-
debug = args.debug if args.debug else config.getboolean('Logging', 'debug', fallback=False)
101-
access_log = (
102-
args.access_log
103-
if args.access_log
104-
else config.get('Logging', 'access_log', fallback="logs/access.log")
105-
)
106-
block_log = (
107-
args.block_log
108-
if args.block_log
109-
else config.get('Logging', 'block_log', fallback="logs/block.log")
110-
)
111-
html_403 = (
112-
args.html_403
113-
if args.html_403
114-
else config.get('Files', 'html_403', fallback="assets/403.html")
115-
)
116-
no_filter = (
117-
args.no_filter
118-
if args.no_filter
119-
else config.getboolean('Filtering', 'no_filter', fallback=False)
120-
)
121-
filter_mode = (
122-
args.filter_mode
123-
if args.filter_mode
124-
else config.get('Filtering', 'filter_mode', fallback="local")
125-
)
126-
blocked_sites = (
127-
args.blocked_sites
128-
if args.blocked_sites
129-
else config.get('Filtering', 'blocked_sites', fallback="config/blocked_sites.txt")
130-
)
131-
blocked_url = (
132-
args.blocked_url
133-
if args.blocked_url
134-
else config.get('Filtering', 'blocked_url', fallback="config/blocked_url.txt")
135-
)
136-
shortcuts = (
137-
args.blocked_url
138-
if args.blocked_url
139-
else config.get('Options', 'shortcuts', fallback="config/shortcuts.txt")
140-
)
141-
custom_header = (
142-
args.blocked_url
143-
if args.blocked_url
144-
else config.get('Options', 'custom_header', fallback="config/custom_header.json")
145-
)
146-
no_logging_access = (
147-
args.no_logging_access
148-
if args.no_logging_access
149-
else config.getboolean('Logging', 'no_logging_access', fallback=False)
150-
)
151-
no_logging_block = (
152-
args.no_logging_block
153-
if args.no_logging_block
154-
else config.getboolean('Logging', 'no_logging_block', fallback=False)
155-
)
156-
ssl_inspect = (
157-
args.ssl_inspect
158-
if args.ssl_inspect
159-
else config.getboolean('Security', 'ssl_inspect', fallback=False)
160-
)
161-
inspect_certs_folder = (
162-
args.inspect_certs_folder
163-
if args.inspect_certs_folder
164-
else config.get('Security', 'inspect_certs_folder', fallback="certs/")
165-
)
166-
inspect_ca_cert = (
167-
args.inspect_ca_cert
168-
if args.inspect_ca_cert
169-
else config.get('Security', 'inspect_ca_cert', fallback="certs/ca/cert.pem")
170-
)
171-
inspect_ca_key = (
172-
args.inspect_ca_key
173-
if args.inspect_ca_key
174-
else config.get('Security', 'inspect_ca_key', fallback="certs/ca/key.pem")
175-
)
176-
cancel_inspect = (
177-
args.inspect_ca_key
178-
if args.inspect_ca_key
179-
else config.get('Security', 'cancel_inspect', fallback="config/cancel_inspect.txt")
180-
)
19+
host = get_config_value(args, config, 'host', 'Server', "0.0.0.0")
20+
port = get_config_value(args, config, 'port', 'Server', 8080)
21+
debug = get_config_value(args, config, 'debug', 'Logging', False)
22+
access_log = get_config_value(args, config, 'access_log', 'Logging', "logs/access.log")
23+
block_log = get_config_value(args, config, 'block_log', 'Logging', "logs/block.log")
24+
html_403 = get_config_value(args, config, 'html_403', 'Files', "assets/403.html")
25+
no_filter = get_config_value(args, config, 'no_filter', 'Filtering', False)
26+
filter_mode = get_config_value(args, config, 'filter_mode', 'Filtering', "local")
27+
blocked_sites = get_config_value(args, config, 'blocked_sites', 'Filtering', "config/blocked_sites.txt")
28+
blocked_url = get_config_value(args, config, 'blocked_url', 'Filtering', "config/blocked_url.txt")
29+
shortcuts = get_config_value(args, config, 'shortcuts', 'Options', "config/shortcuts.txt")
30+
custom_header = get_config_value(args, config, 'custom_header', 'Options', "config/custom_header.json")
31+
no_logging_access = get_config_value(args, config, 'no_logging_access', 'Logging', False)
32+
no_logging_block = get_config_value(args, config, 'no_logging_block', 'Logging', False)
33+
ssl_inspect = get_config_value(args, config, 'ssl_inspect', 'Security', False)
34+
inspect_certs_folder = get_config_value(args, config, 'inspect_certs_folder', 'Security', "certs/")
35+
inspect_ca_cert = get_config_value(args, config, 'inspect_ca_cert', 'Security', "certs/ca/cert.pem")
36+
inspect_ca_key = get_config_value(args, config, 'inspect_ca_key', 'Security', "certs/ca/key.pem")
37+
cancel_inspect = get_config_value(args, config, 'cancel_inspect', 'Security', "config/cancel_inspect.txt")
18138

18239
proxy = ProxyServer(
18340
host=host,
@@ -202,3 +59,6 @@
20259
)
20360

20461
proxy.start()
62+
63+
if __name__ == "__main__":
64+
main()

tests/test_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import multiprocessing
1818
import time
1919
from unittest.mock import patch, mock_open
20-
from utils.filter import load_blacklist, filter_process
20+
from utils.proxy.filter import load_blacklist, filter_process
2121

2222
class TestFilter(unittest.TestCase):
2323
"""

utils/config.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,49 @@
55
"""
66

77
import configparser
8+
import argparse
9+
from rich_argparse import MetavarTypeRichHelpFormatter
10+
from utils.version import __version__
11+
12+
# pylint: disable=C0301
13+
14+
def parse_args() -> argparse.Namespace:
15+
"""
16+
Parses command-line arguments and returns the parsed arguments as an object.
17+
18+
Args:
19+
None
20+
21+
Returns:
22+
argparse.Namespace: The object containing parsed command-line arguments.
23+
"""
24+
parser = argparse.ArgumentParser(
25+
description="Lightweight and fast python web proxy",
26+
formatter_class=MetavarTypeRichHelpFormatter
27+
)
28+
parser.add_argument("-v", "--version", action='version', version=__version__, help="Show version")
29+
parser.add_argument("--debug", action="store_true", help="Enable debug logging")
30+
parser.add_argument("-H", "--host", type=str, help="IP address to listen on")
31+
parser.add_argument("-P", "--port", type=int, help="Port to listen on")
32+
parser.add_argument("-f", "--config-file", type=str, default="./config.ini", help="Path to config.ini file")
33+
parser.add_argument("--access-log", type=str, help="Path to the access log file")
34+
parser.add_argument("--block-log", type=str, help="Path to the block log file")
35+
parser.add_argument("--html-403", type=str, help="Path to the custom 403 Forbidden HTML page")
36+
parser.add_argument("--no-filter", action="store_true", help="Disable URL and domain filtering")
37+
parser.add_argument("--filter-mode", type=str, choices=["local", "http"], help="Filter list mode")
38+
parser.add_argument("--blocked-sites", type=str, help="Path to the text file containing the list of sites to block")
39+
parser.add_argument("--blocked-url", type=str, help="Path to the text file containing the list of URLs to block")
40+
parser.add_argument("--shortcuts", type=str, help="Path to the text file containing the list of shortcuts")
41+
parser.add_argument("--custom-header", type=str, help="Path to the json file containing the list of custom headers")
42+
parser.add_argument("--no-logging-access", action="store_true", help="Disable access logging")
43+
parser.add_argument("--no-logging-block", action="store_true", help="Disable block logging")
44+
parser.add_argument("--ssl-inspect", action="store_true", help="Enable SSL inspection")
45+
parser.add_argument("--inspect-ca-cert", type=str, help="Path to the CA certificate")
46+
parser.add_argument("--inspect-ca-key", type=str, help="Path to the CA key")
47+
parser.add_argument("--inspect-certs-folder", type=str, help="Path to the generated certificates folder")
48+
parser.add_argument("--cancel-inspect", type=str, help="Path to the text file containing the list of URLs without ssl inspection")
49+
50+
return parser.parse_args()
851

952
def load_config(config_path: str) -> configparser.ConfigParser:
1053
"""
@@ -19,3 +62,22 @@ def load_config(config_path: str) -> configparser.ConfigParser:
1962
config = configparser.ConfigParser()
2063
config.read(config_path)
2164
return config
65+
66+
def get_config_value(args: argparse.Namespace, config: configparser.ConfigParser, arg_name: str, section: str, fallback_value: str) -> str:
67+
"""
68+
Retrieves the configuration value, either from the command-line arguments or from the config file.
69+
70+
Args:
71+
args (argparse.Namespace): The parsed command-line arguments object.
72+
config (configparser.ConfigParser): The parsed configuration object.
73+
arg_name (str): The name of the command-line argument.
74+
section (str): The section in the config file where the value is located.
75+
fallback_value (str): The fallback value to return if neither argument nor config has a value.
76+
77+
Returns:
78+
str: The final value, either from command-line arguments, config file, or fallback.
79+
"""
80+
arg_value = getattr(args, arg_name, None)
81+
if arg_value:
82+
return arg_value
83+
return config.get(section, arg_name, fallback=fallback_value)

0 commit comments

Comments
 (0)