Blacklist → block(list)
This commit is contained in:
parent
3abe65a551
commit
baf9ec5387
26
neomi.py
26
neomi.py
|
@ -15,7 +15,7 @@ import urllib.parse
|
||||||
|
|
||||||
class default_config: None
|
class default_config: None
|
||||||
|
|
||||||
default_config.blacklist_file = pathlib.Path(os.environ['HOME']) / 'gopher_blacklist'
|
default_config.blocklist_file = pathlib.Path(os.environ['HOME']) / 'gopher_blocklist'
|
||||||
default_config.charset = 'utf-8'
|
default_config.charset = 'utf-8'
|
||||||
default_config.fallback_mimetype = 'application/octet-stream'
|
default_config.fallback_mimetype = 'application/octet-stream'
|
||||||
default_config.gopher_root = pathlib.Path(os.environ['HOME']) / 'gopher'
|
default_config.gopher_root = pathlib.Path(os.environ['HOME']) / 'gopher'
|
||||||
|
@ -799,18 +799,18 @@ class Threads_controller:
|
||||||
class IPParseError(OneArgumentException):
|
class IPParseError(OneArgumentException):
|
||||||
text = 'Error parsing IP: %s'
|
text = 'Error parsing IP: %s'
|
||||||
|
|
||||||
# read_blacklist(blacklist_file) → blacklist
|
# read_blocklist(blocklist_file) → blocklist
|
||||||
# Reads the contents of the blacklist file into a form usable by ip_in_ranges()
|
# Reads the contents of the blocklist file into a form usable by ip_in_ranges()
|
||||||
def read_blacklist(blacklist_file):
|
def read_blocklist(blocklist_file):
|
||||||
try:
|
try:
|
||||||
file = open(str(blacklist_file), 'r')
|
file = open(str(blocklist_file), 'r')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
lines = file.read().split('\n')
|
lines = file.read().split('\n')
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
blacklist = []
|
blocklist = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
# Comment handling
|
# Comment handling
|
||||||
if '#' in line:
|
if '#' in line:
|
||||||
|
@ -828,9 +828,9 @@ def read_blacklist(blacklist_file):
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
raise IPParseError('Invalid format: ' + line) from err
|
raise IPParseError('Invalid format: ' + line) from err
|
||||||
|
|
||||||
blacklist.append(ip_range)
|
blocklist.append(ip_range)
|
||||||
|
|
||||||
return blacklist
|
return blocklist
|
||||||
|
|
||||||
# ip_in_ranges(ip, ip_ranges) → in_rages
|
# ip_in_ranges(ip, ip_ranges) → in_rages
|
||||||
# Checks whether an ip address is in given ranges
|
# Checks whether an ip address is in given ranges
|
||||||
|
@ -870,8 +870,8 @@ def listen(config):
|
||||||
# Create a controller object for the worker threads
|
# Create a controller object for the worker threads
|
||||||
threads_controller = Threads_controller()
|
threads_controller = Threads_controller()
|
||||||
|
|
||||||
# Read blacklist of addresses
|
# Read blocklist of addresses
|
||||||
blacklist = read_blacklist(config.blacklist_file)
|
blocklist = read_blocklist(config.blocklist_file)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Wait for listening sockets to get activity
|
# Wait for listening sockets to get activity
|
||||||
|
@ -883,11 +883,11 @@ def listen(config):
|
||||||
# Accept and handle the connection
|
# Accept and handle the connection
|
||||||
conn, addr = s.accept()
|
conn, addr = s.accept()
|
||||||
|
|
||||||
# Check if connection is from a blacklisted IP address
|
# Check if connection is from a blocked IP address
|
||||||
if ip_in_ranges(addr[0], blacklist):
|
if ip_in_ranges(addr[0], blocklist):
|
||||||
# It was, skip event
|
# It was, skip event
|
||||||
conn.close()
|
conn.close()
|
||||||
log('Connection from blacklisted address %s' % addr[0])
|
log('Connection from blocked address %s' % addr[0])
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Set timeout for socket
|
# Set timeout for socket
|
||||||
|
|
Loading…
Reference in New Issue