mirror of https://gitlab.com/bashrc2/epicyon
Cache blocked domains
parent
1c25de21c6
commit
084da8d2a4
43
blocking.py
43
blocking.py
|
@ -9,6 +9,7 @@ __module_group__ = "ActivityPub"
|
|||
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime
|
||||
from utils import isAccountDir
|
||||
from utils import getCachedPostFilename
|
||||
|
@ -167,7 +168,29 @@ def getDomainBlocklist(baseDir: str) -> str:
|
|||
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?
|
||||
"""
|
||||
if '.' not in domain:
|
||||
|
@ -186,16 +209,24 @@ def isBlockedDomain(baseDir: str, domain: str) -> bool:
|
|||
|
||||
allowFilename = baseDir + '/accounts/allowedinstances.txt'
|
||||
if not os.path.isfile(allowFilename):
|
||||
# instance block list
|
||||
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
||||
if os.path.isfile(globalBlockingFilename):
|
||||
with open(globalBlockingFilename, 'r') as fpBlocked:
|
||||
blockedStr = fpBlocked.read()
|
||||
if blockedCache:
|
||||
for blockedStr in blockedCache:
|
||||
if '*@' + domain in blockedStr:
|
||||
return True
|
||||
if shortDomain:
|
||||
if '*@' + shortDomain in blockedStr:
|
||||
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:
|
||||
# instance allow list
|
||||
if not shortDomain:
|
||||
|
|
24
daemon.py
24
daemon.py
|
@ -114,6 +114,7 @@ from media import attachMedia
|
|||
from media import pathIsImage
|
||||
from media import pathIsVideo
|
||||
from media import pathIsAudio
|
||||
from blocking import updateBlockedCache
|
||||
from blocking import mutePost
|
||||
from blocking import unmutePost
|
||||
from blocking import setBrochMode
|
||||
|
@ -487,6 +488,12 @@ class PubServer(BaseHTTPRequestHandler):
|
|||
# is the User-Agent domain blocked
|
||||
blockedUA = False
|
||||
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)
|
||||
# if self.server.debug:
|
||||
if blockedUA:
|
||||
|
@ -1212,6 +1219,13 @@ class PubServer(BaseHTTPRequestHandler):
|
|||
|
||||
messageDomain, messagePort = \
|
||||
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):
|
||||
print('POST from blocked domain ' + messageDomain)
|
||||
self._400()
|
||||
|
@ -15180,6 +15194,16 @@ def runDaemon(userAgentsBlocked: [],
|
|||
# contains threads used to send posts to followers
|
||||
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
|
||||
httpd.cssCache = {}
|
||||
|
||||
|
|
Loading…
Reference in New Issue