Merge branch 'main' of ssh://code.freedombone.net:2222/bashrc/epicyon

main
Bob Mottram 2021-06-21 23:56:27 +01:00
commit 259f7eac7d
4 changed files with 79 additions and 28 deletions

View File

@ -186,12 +186,27 @@ def updateBlockedCache(baseDir: str,
return blockedCacheLastUpdated return blockedCacheLastUpdated
with open(globalBlockingFilename, 'r') as fpBlocked: with open(globalBlockingFilename, 'r') as fpBlocked:
blockedLines = fpBlocked.readlines() blockedLines = fpBlocked.readlines()
# remove newlines
for index in range(len(blockedLines)):
blockedLines[index] = blockedLines[index].replace('\n', '')
# update the cache
blockedCache.clear() blockedCache.clear()
for line in blockedLines: blockedCache += blockedLines
blockedCache.append(line.replace('\n', ''))
return currTime return currTime
def _getShortDomain(domain: str) -> str:
""" by checking a shorter version we can thwart adversaries
who constantly change their subdomain
e.g. subdomain123.mydomain.com becomes mydomain.com
"""
sections = domain.split('.')
noOfSections = len(sections)
if noOfSections > 2:
return sections[noOfSections-2] + '.' + sections[-1]
return None
def isBlockedDomain(baseDir: str, domain: str, def isBlockedDomain(baseDir: str, domain: str,
blockedCache: [] = None) -> bool: blockedCache: [] = None) -> bool:
"""Is the given domain blocked? """Is the given domain blocked?
@ -202,13 +217,7 @@ def isBlockedDomain(baseDir: str, domain: str,
if isEvil(domain): if isEvil(domain):
return True return True
# by checking a shorter version we can thwart adversaries shortDomain = _getShortDomain(domain)
# who constantly change their subdomain
sections = domain.split('.')
noOfSections = len(sections)
shortDomain = None
if noOfSections > 2:
shortDomain = domain[noOfSections-2] + '.' + domain[noOfSections-1]
if not brochModeIsActive(baseDir): if not brochModeIsActive(baseDir):
if blockedCache: if blockedCache:
@ -243,31 +252,58 @@ def isBlockedDomain(baseDir: str, domain: str,
def isBlocked(baseDir: str, nickname: str, domain: str, def isBlocked(baseDir: str, nickname: str, domain: str,
blockNickname: str, blockDomain: str) -> bool: blockNickname: str, blockDomain: str,
blockedCache: [] = None) -> bool:
"""Is the given nickname blocked? """Is the given nickname blocked?
""" """
if isEvil(blockDomain): if isEvil(blockDomain):
return True return True
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename): blockHandle = None
if '*@' + blockDomain in open(globalBlockingFilename).read(): if blockNickname and blockDomain:
return True blockHandle = blockNickname + '@' + blockDomain
if blockNickname:
blockHandle = blockNickname + '@' + blockDomain if not brochModeIsActive(baseDir):
if blockHandle in open(globalBlockingFilename).read(): # instance level block list
if blockedCache:
for blockedStr in blockedCache:
if '*@' + domain in blockedStr:
return True
if blockHandle:
if blockHandle in blockedStr:
return True
else:
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename):
if '*@' + blockDomain in open(globalBlockingFilename).read():
return True
if blockHandle:
if blockHandle in open(globalBlockingFilename).read():
return True
else:
# instance allow list
allowFilename = baseDir + '/accounts/allowedinstances.txt'
shortDomain = _getShortDomain(blockDomain)
if not shortDomain:
if blockDomain not in open(allowFilename).read():
return True return True
allowFilename = baseDir + '/accounts/' + \ else:
nickname + '@' + domain + '/allowedinstances.txt' if shortDomain not in open(allowFilename).read():
return True
# account level allow list
accountDir = baseDir + '/accounts/' + nickname + '@' + domain
allowFilename = accountDir + '/allowedinstances.txt'
if os.path.isfile(allowFilename): if os.path.isfile(allowFilename):
if blockDomain not in open(allowFilename).read(): if blockDomain not in open(allowFilename).read():
return True return True
blockingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/blocking.txt' # account level block list
blockingFilename = accountDir + '/blocking.txt'
if os.path.isfile(blockingFilename): if os.path.isfile(blockingFilename):
if '*@' + blockDomain in open(blockingFilename).read(): if '*@' + blockDomain in open(blockingFilename).read():
return True return True
if blockNickname: if blockHandle:
blockHandle = blockNickname + '@' + blockDomain
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return True return True
return False return False

View File

@ -130,6 +130,7 @@ def _getCityPulse(currTimeOfDay, decoySeed: int) -> (float, float):
def parseNogoString(nogoLine: str) -> []: def parseNogoString(nogoLine: str) -> []:
"""Parses a line from locations_nogo.txt and returns the polygon """Parses a line from locations_nogo.txt and returns the polygon
""" """
nogoLine = nogoLine.replace('\n', '').replace('\r', '')
polygonStr = nogoLine.split(':', 1)[1] polygonStr = nogoLine.split(':', 1)[1]
if ';' in polygonStr: if ';' in polygonStr:
pts = polygonStr.split(';') pts = polygonStr.split(';')

View File

@ -1277,6 +1277,13 @@ class PubServer(BaseHTTPRequestHandler):
beginSaveTime = time.time() beginSaveTime = time.time()
# save the json for later queue processing # save the json for later queue processing
messageBytesDecoded = messageBytes.decode('utf-8') messageBytesDecoded = messageBytes.decode('utf-8')
self.server.blockedCacheLastUpdated = \
updateBlockedCache(self.server.baseDir,
self.server.blockedCache,
self.server.blockedCacheLastUpdated,
self.server.blockedCacheUpdateSecs)
queueFilename = \ queueFilename = \
savePostToInboxQueue(self.server.baseDir, savePostToInboxQueue(self.server.baseDir,
self.server.httpPrefix, self.server.httpPrefix,
@ -1286,7 +1293,8 @@ class PubServer(BaseHTTPRequestHandler):
messageBytesDecoded, messageBytesDecoded,
headersDict, headersDict,
self.path, self.path,
self.server.debug) self.server.debug,
self.server.blockedCache)
if queueFilename: if queueFilename:
# add json to the queue # add json to the queue
if queueFilename not in self.server.inboxQueue: if queueFilename not in self.server.inboxQueue:
@ -14833,7 +14841,10 @@ class EpicyonServer(ThreadingHTTPServer):
if e.errno != errno.ECONNRESET: if e.errno != errno.ECONNRESET:
print('ERROR: (EpicyonServer) ' + str(cls) + ", " + str(e)) print('ERROR: (EpicyonServer) ' + str(cls) + ", " + str(e))
pass pass
elif cls is BrokenPipeError:
pass
else: else:
print('ERROR: (EpicyonServer) ' + str(cls) + ", " + str(e))
return HTTPServer.handle_error(self, request, client_address) return HTTPServer.handle_error(self, request, client_address)

View File

@ -353,7 +353,8 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str,
originalPostJsonObject: {}, originalPostJsonObject: {},
messageBytes: str, messageBytes: str,
httpHeaders: {}, httpHeaders: {},
postPath: str, debug: bool) -> str: postPath: str, debug: bool,
blockedCache: []) -> str:
"""Saves the give json to the inbox queue for the person """Saves the give json to the inbox queue for the person
keyId specifies the actor sending the post keyId specifies the actor sending the post
""" """
@ -384,7 +385,8 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str,
pprint(postJsonObject) pprint(postJsonObject)
print('No post Domain in actor') print('No post Domain in actor')
return None return None
if isBlocked(baseDir, nickname, domain, postNickname, postDomain): if isBlocked(baseDir, nickname, domain,
postNickname, postDomain, blockedCache):
if debug: if debug:
print('DEBUG: post from ' + postNickname + ' blocked') print('DEBUG: post from ' + postNickname + ' blocked')
return None return None
@ -398,7 +400,7 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str,
postJsonObject['object']['inReplyTo'] postJsonObject['object']['inReplyTo']
replyDomain, replyPort = \ replyDomain, replyPort = \
getDomainFromActor(inReplyTo) getDomainFromActor(inReplyTo)
if isBlockedDomain(baseDir, replyDomain): if isBlockedDomain(baseDir, replyDomain, blockedCache):
if debug: if debug:
print('WARN: post contains reply from ' + print('WARN: post contains reply from ' +
str(actor) + str(actor) +
@ -409,7 +411,8 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str,
getNicknameFromActor(inReplyTo) getNicknameFromActor(inReplyTo)
if replyNickname and replyDomain: if replyNickname and replyDomain:
if isBlocked(baseDir, nickname, domain, if isBlocked(baseDir, nickname, domain,
replyNickname, replyDomain): replyNickname, replyDomain,
blockedCache):
if debug: if debug:
print('WARN: post contains reply from ' + print('WARN: post contains reply from ' +
str(actor) + str(actor) +