Cache blocked domains

merge-requests/30/head
Bob Mottram 2021-06-21 10:22:24 +01:00
parent 1c25de21c6
commit 084da8d2a4
2 changed files with 61 additions and 6 deletions

View File

@ -9,6 +9,7 @@ __module_group__ = "ActivityPub"
import os import os
import json import json
import time
from datetime import datetime from datetime import datetime
from utils import isAccountDir from utils import isAccountDir
from utils import getCachedPostFilename from utils import getCachedPostFilename
@ -167,7 +168,29 @@ def getDomainBlocklist(baseDir: str) -> str:
return blockedStr return blockedStr
def isBlockedDomain(baseDir: str, domain: str) -> bool: def updateBlockedCache(baseDir: str,
blockedCache: [],
blockedCacheLastUpdated: int,
blockedCacheUpdateSecs: int) -> int:
"""Updates the cache of globally blocked domains held in memory
"""
currTime = int(time.time())
secondsSinceLastUpdate = currTime - blockedCacheLastUpdated
if secondsSinceLastUpdate < blockedCacheUpdateSecs:
return blockedCacheLastUpdated
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if not os.path.isfile(globalBlockingFilename):
return blockedCacheLastUpdated
with open(globalBlockingFilename, 'r') as fpBlocked:
blockedLines = fpBlocked.readlines()
blockedCache.clear()
for line in blockedLines:
blockedCache.append(line)
return currTime
def isBlockedDomain(baseDir: str, domain: str,
blockedCache: [] = None) -> bool:
"""Is the given domain blocked? """Is the given domain blocked?
""" """
if '.' not in domain: if '.' not in domain:
@ -186,16 +209,24 @@ def isBlockedDomain(baseDir: str, domain: str) -> bool:
allowFilename = baseDir + '/accounts/allowedinstances.txt' allowFilename = baseDir + '/accounts/allowedinstances.txt'
if not os.path.isfile(allowFilename): if not os.path.isfile(allowFilename):
# instance block list if blockedCache:
globalBlockingFilename = baseDir + '/accounts/blocking.txt' for blockedStr in blockedCache:
if os.path.isfile(globalBlockingFilename):
with open(globalBlockingFilename, 'r') as fpBlocked:
blockedStr = fpBlocked.read()
if '*@' + domain in blockedStr: if '*@' + domain in blockedStr:
return True return True
if shortDomain: if shortDomain:
if '*@' + shortDomain in blockedStr: if '*@' + shortDomain in blockedStr:
return True return True
else:
# instance block list
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename):
with open(globalBlockingFilename, 'r') as fpBlocked:
blockedStr = fpBlocked.read()
if '*@' + domain in blockedStr:
return True
if shortDomain:
if '*@' + shortDomain in blockedStr:
return True
else: else:
# instance allow list # instance allow list
if not shortDomain: if not shortDomain:

View File

@ -114,6 +114,7 @@ from media import attachMedia
from media import pathIsImage from media import pathIsImage
from media import pathIsVideo from media import pathIsVideo
from media import pathIsAudio from media import pathIsAudio
from blocking import updateBlockedCache
from blocking import mutePost from blocking import mutePost
from blocking import unmutePost from blocking import unmutePost
from blocking import setBrochMode from blocking import setBrochMode
@ -487,6 +488,12 @@ class PubServer(BaseHTTPRequestHandler):
# is the User-Agent domain blocked # is the User-Agent domain blocked
blockedUA = False blockedUA = False
if not agentDomain.startswith(callingDomain): if not agentDomain.startswith(callingDomain):
self.server.blockedCacheLastUpdated = \
updateBlockedCache(self.server.baseDir,
self.server.blockedCache,
self.server.blockedCacheLastUpdated,
self.server.blockedCacheUpdateSecs)
blockedUA = isBlockedDomain(self.server.baseDir, agentDomain) blockedUA = isBlockedDomain(self.server.baseDir, agentDomain)
# if self.server.debug: # if self.server.debug:
if blockedUA: if blockedUA:
@ -1212,6 +1219,13 @@ class PubServer(BaseHTTPRequestHandler):
messageDomain, messagePort = \ messageDomain, messagePort = \
getDomainFromActor(messageJson['actor']) getDomainFromActor(messageJson['actor'])
self.server.blockedCacheLastUpdated = \
updateBlockedCache(self.server.baseDir,
self.server.blockedCache,
self.server.blockedCacheLastUpdated,
self.server.blockedCacheUpdateSecs)
if isBlockedDomain(self.server.baseDir, messageDomain): if isBlockedDomain(self.server.baseDir, messageDomain):
print('POST from blocked domain ' + messageDomain) print('POST from blocked domain ' + messageDomain)
self._400() self._400()
@ -15180,6 +15194,16 @@ def runDaemon(userAgentsBlocked: [],
# contains threads used to send posts to followers # contains threads used to send posts to followers
httpd.followersThreads = [] httpd.followersThreads = []
# create a cache of blocked domains in memory.
# This limits the amount of slow disk reads which need to be done
httpd.blockedCache = []
httpd.blockedCacheLastUpdated = 0
httpd.blockedCacheUpdateSecs = 60
httpd.blockedCacheLastUpdated = \
updateBlockedCache(baseDir, httpd.blockedCache,
httpd.blockedCacheLastUpdated,
httpd.blockedCacheUpdateSecs)
# cache to store css files # cache to store css files
httpd.cssCache = {} httpd.cssCache = {}