|
4 | 4 | to those URLs. The proxy can handle both HTTP and HTTPS requests, and logs access and block events. |
5 | 5 | """ |
6 | 6 |
|
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 |
95 | 9 |
|
| 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() |
96 | 17 | config = load_config(args.config_file) |
97 | 18 |
|
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") |
181 | 38 |
|
182 | 39 | proxy = ProxyServer( |
183 | 40 | host=host, |
|
202 | 59 | ) |
203 | 60 |
|
204 | 61 | proxy.start() |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments