Blacklist → block(list)

This commit is contained in:
Juhani Krekelä 2020-07-21 23:18:58 +03:00
parent 3abe65a551
commit baf9ec5387
1 changed files with 13 additions and 13 deletions

View File

@ -15,7 +15,7 @@ import urllib.parse
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.fallback_mimetype = 'application/octet-stream'
default_config.gopher_root = pathlib.Path(os.environ['HOME']) / 'gopher'
@ -799,18 +799,18 @@ class Threads_controller:
class IPParseError(OneArgumentException):
text = 'Error parsing IP: %s'
# read_blacklist(blacklist_file) → blacklist
# Reads the contents of the blacklist file into a form usable by ip_in_ranges()
def read_blacklist(blacklist_file):
# read_blocklist(blocklist_file) → blocklist
# Reads the contents of the blocklist file into a form usable by ip_in_ranges()
def read_blocklist(blocklist_file):
try:
file = open(str(blacklist_file), 'r')
file = open(str(blocklist_file), 'r')
except FileNotFoundError:
return []
lines = file.read().split('\n')
file.close()
blacklist = []
blocklist = []
for line in lines:
# Comment handling
if '#' in line:
@ -828,9 +828,9 @@ def read_blacklist(blacklist_file):
except ValueError as 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
# Checks whether an ip address is in given ranges
@ -870,8 +870,8 @@ def listen(config):
# Create a controller object for the worker threads
threads_controller = Threads_controller()
# Read blacklist of addresses
blacklist = read_blacklist(config.blacklist_file)
# Read blocklist of addresses
blocklist = read_blocklist(config.blocklist_file)
while True:
# Wait for listening sockets to get activity
@ -883,11 +883,11 @@ def listen(config):
# Accept and handle the connection
conn, addr = s.accept()
# Check if connection is from a blacklisted IP address
if ip_in_ranges(addr[0], blacklist):
# Check if connection is from a blocked IP address
if ip_in_ranges(addr[0], blocklist):
# It was, skip event
conn.close()
log('Connection from blacklisted address %s' % addr[0])
log('Connection from blocked address %s' % addr[0])
continue
# Set timeout for socket