Skip to content

Commit df4fa3b

Browse files
committed
Added option to autodetect blocked domains (closed #35)
1 parent 008bad9 commit df4fa3b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,8 @@ Please report any problems and malfunctions to us on [the Issues page](https://g
124124

125125
## Supported arguments / Поддерживаемые аргументы командной строки
126126
```
127-
usage: nodpi [-h] [--host HOST] [--port PORT]
128-
[--blacklist BLACKLIST | --no_blacklist]
129-
[--log_access LOG_ACCESS] [--log_error LOG_ERROR]
130-
[-q] [-v] [--install | --uninstall]
127+
usage: nodpi [-h] [--host HOST] [--port PORT] [--blacklist BLACKLIST | --no_blacklist | --autoblacklist]
128+
[--log_access LOG_ACCESS] [--log_error LOG_ERROR] [-q] [-v] [--install | --uninstall]
131129
132130
options:
133131
-h, --help show this help message and exit
@@ -136,6 +134,7 @@ options:
136134
--blacklist BLACKLIST
137135
Path to blacklist file
138136
--no_blacklist Use fragmentation for all domains
137+
--autoblacklist Automatic detection of blocked domains
139138
--log_access LOG_ACCESS
140139
Path to the access control log
141140
--log_error LOG_ERROR

src/main.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import logging
77
import os
88
import sys
9+
import requests
910
from datetime import datetime
1011
import time
1112
import traceback
1213

13-
__version__ = "1.8.2"
14+
__version__ = "1.9"
1415

1516
os.system("")
1617

@@ -30,14 +31,15 @@ def __init__(self, src_ip, dst_domain, method):
3031

3132
class ProxyServer:
3233

33-
def __init__(self, host, port, blacklist, log_access, log_err, no_blacklist, quiet, verbose):
34+
def __init__(self, host, port, blacklist, log_access, log_err, no_blacklist, auto_blacklist, quiet, verbose):
3435

3536
self.host = host
3637
self.port = port
3738
self.blacklist = blacklist
3839
self.log_access_file = log_access
3940
self.log_err_file = log_err
4041
self.no_blacklist = no_blacklist
42+
self.auto_blacklist = auto_blacklist
4143
self.quiet = quiet
4244
self.verbose = verbose
4345

@@ -61,6 +63,7 @@ def __init__(self, host, port, blacklist, log_access, log_err, no_blacklist, qui
6163
self.tasks_lock = asyncio.Lock()
6264

6365
self.blocked = []
66+
self.whitelist = []
6467
self.tasks = []
6568
self.server = None
6669

@@ -289,6 +292,16 @@ async def handle_connection(self, reader, writer):
289292
conn_info = ConnectionInfo(
290293
client_ip, host.decode(), method.decode())
291294

295+
if method == b"CONNECT" and self.auto_blacklist:
296+
try:
297+
if host not in self.blocked and host not in self.whitelist:
298+
requests.get('https://' + host.decode(), timeout=3)
299+
self.whitelist.append(host)
300+
except Exception: # pylint: disable=broad-except
301+
self.blocked.append(host)
302+
with open(self.blacklist, "a", encoding="utf-8") as f:
303+
f.write(host.decode() + "\n")
304+
292305
async with self.connections_lock:
293306
self.active_connections[conn_key] = conn_info
294307

@@ -465,6 +478,9 @@ def parse_args():
465478
blacklist_group.add_argument(
466479
"--no_blacklist", action="store_true", help="Use fragmentation for all domains"
467480
)
481+
blacklist_group.add_argument(
482+
"--autoblacklist", action="store_true", help="Automatic detection of blocked domains"
483+
)
468484

469485
parser.add_argument(
470486
"--log_access", required=False, help="Path to the access control log"
@@ -563,6 +579,7 @@ async def run(cls):
563579
args.log_access,
564580
args.log_error,
565581
args.no_blacklist,
582+
args.autoblacklist,
566583
args.quiet,
567584
args.verbose,
568585
)

0 commit comments

Comments
 (0)