forked from indymedia/epicyon
flake8 style
parent
2c6169fa64
commit
8dd64c187f
136
blocking.py
136
blocking.py
|
@ -8,9 +8,17 @@ __status__="Production"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from utils import isEvil
|
from utils import isEvil
|
||||||
|
from utils import locatePost
|
||||||
from utils import evilIncarnate
|
from utils import evilIncarnate
|
||||||
|
from utils import getDomainFromActor
|
||||||
|
from utils import getNicknameFromActor
|
||||||
|
from session import postJson
|
||||||
|
from auth import createBasicAuthHeader
|
||||||
|
from posts import getPersonBox
|
||||||
|
from webfinger import webfingerHandle
|
||||||
|
|
||||||
def addGlobalBlock(baseDir: str, \
|
|
||||||
|
def addGlobalBlock(baseDir: str,
|
||||||
blockNickname: str, blockDomain: str) -> bool:
|
blockNickname: str, blockDomain: str) -> bool:
|
||||||
"""Global block which applies to all accounts
|
"""Global block which applies to all accounts
|
||||||
"""
|
"""
|
||||||
|
@ -33,13 +41,15 @@ def addGlobalBlock(baseDir: str, \
|
||||||
blockFile.close()
|
blockFile.close()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def addBlock(baseDir: str,nickname: str,domain: str, \
|
|
||||||
|
def addBlock(baseDir: str, nickname: str, domain: str,
|
||||||
blockNickname: str, blockDomain: str) -> bool:
|
blockNickname: str, blockDomain: str) -> bool:
|
||||||
"""Block the given account
|
"""Block the given account
|
||||||
"""
|
"""
|
||||||
if ':' in domain:
|
if ':' in domain:
|
||||||
domain = domain.split(':')[0]
|
domain = domain.split(':')[0]
|
||||||
blockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
|
blockingFilename = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/blocking.txt'
|
||||||
blockHandle = blockNickname + '@' + blockDomain
|
blockHandle = blockNickname + '@' + blockDomain
|
||||||
if os.path.isfile(blockingFilename):
|
if os.path.isfile(blockingFilename):
|
||||||
if blockHandle in open(blockingFilename).read():
|
if blockHandle in open(blockingFilename).read():
|
||||||
|
@ -49,8 +59,9 @@ def addBlock(baseDir: str,nickname: str,domain: str, \
|
||||||
blockFile.close()
|
blockFile.close()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def removeGlobalBlock(baseDir: str, \
|
|
||||||
unblockNickname: str, \
|
def removeGlobalBlock(baseDir: str,
|
||||||
|
unblockNickname: str,
|
||||||
unblockDomain: str) -> bool:
|
unblockDomain: str) -> bool:
|
||||||
"""Unblock the given global block
|
"""Unblock the given global block
|
||||||
"""
|
"""
|
||||||
|
@ -83,13 +94,15 @@ def removeGlobalBlock(baseDir: str, \
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def removeBlock(baseDir: str,nickname: str,domain: str, \
|
|
||||||
|
def removeBlock(baseDir: str, nickname: str, domain: str,
|
||||||
unblockNickname: str, unblockDomain: str) -> bool:
|
unblockNickname: str, unblockDomain: str) -> bool:
|
||||||
"""Unblock the given account
|
"""Unblock the given account
|
||||||
"""
|
"""
|
||||||
if ':' in domain:
|
if ':' in domain:
|
||||||
domain = domain.split(':')[0]
|
domain = domain.split(':')[0]
|
||||||
unblockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
|
unblockingFilename = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/blocking.txt'
|
||||||
unblockHandle = unblockNickname + '@' + unblockDomain
|
unblockHandle = unblockNickname + '@' + unblockDomain
|
||||||
if os.path.isfile(unblockingFilename):
|
if os.path.isfile(unblockingFilename):
|
||||||
if unblockHandle in open(unblockingFilename).read():
|
if unblockHandle in open(unblockingFilename).read():
|
||||||
|
@ -104,6 +117,7 @@ def removeBlock(baseDir: str,nickname: str,domain: str, \
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def isBlockedHashtag(baseDir: str, hashtag: str) -> bool:
|
def isBlockedHashtag(baseDir: str, hashtag: str) -> bool:
|
||||||
"""Is the given hashtag blocked?
|
"""Is the given hashtag blocked?
|
||||||
"""
|
"""
|
||||||
|
@ -114,6 +128,7 @@ def isBlockedHashtag(baseDir: str,hashtag: str) -> bool:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def getDomainBlocklist(baseDir: str) -> str:
|
def getDomainBlocklist(baseDir: str) -> str:
|
||||||
"""Returns all globally blocked domains as a string
|
"""Returns all globally blocked domains as a string
|
||||||
This can be used for fast matching to mitigate flooding
|
This can be used for fast matching to mitigate flooding
|
||||||
|
@ -131,6 +146,7 @@ def getDomainBlocklist(baseDir: str) -> str:
|
||||||
blockedStr += file.read()
|
blockedStr += file.read()
|
||||||
return blockedStr
|
return blockedStr
|
||||||
|
|
||||||
|
|
||||||
def isBlockedDomain(baseDir: str, domain: str) -> bool:
|
def isBlockedDomain(baseDir: str, domain: str) -> bool:
|
||||||
"""Is the given domain blocked?
|
"""Is the given domain blocked?
|
||||||
"""
|
"""
|
||||||
|
@ -142,7 +158,8 @@ def isBlockedDomain(baseDir: str,domain: str) -> bool:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
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) -> bool:
|
||||||
"""Is the given nickname blocked?
|
"""Is the given nickname blocked?
|
||||||
"""
|
"""
|
||||||
|
@ -156,13 +173,13 @@ def isBlocked(baseDir: str,nickname: str,domain: str, \
|
||||||
blockHandle = blockNickname + '@' + blockDomain
|
blockHandle = blockNickname + '@' + blockDomain
|
||||||
if blockHandle in open(globalBlockingFilename).read():
|
if blockHandle in open(globalBlockingFilename).read():
|
||||||
return True
|
return True
|
||||||
allowFilename= \
|
allowFilename = baseDir + '/accounts/' + \
|
||||||
baseDir+'/accounts/'+nickname+'@'+domain+'/allowedinstances.txt'
|
nickname + '@' + domain + '/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= \
|
blockingFilename = baseDir + '/accounts/' + \
|
||||||
baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
|
nickname + '@' + domain + '/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
|
||||||
|
@ -172,11 +189,12 @@ def isBlocked(baseDir: str,nickname: str,domain: str, \
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def sendBlockViaServer(baseDir: str,session, \
|
|
||||||
fromNickname: str,password: str, \
|
def sendBlockViaServer(baseDir: str, session,
|
||||||
fromDomain: str,fromPort: int, \
|
fromNickname: str, password: str,
|
||||||
httpPrefix: str,blockedUrl: str, \
|
fromDomain: str, fromPort: int,
|
||||||
cachedWebfingers: {},personCache: {}, \
|
httpPrefix: str, blockedUrl: str,
|
||||||
|
cachedWebfingers: {}, personCache: {},
|
||||||
debug: bool, projectVersion: str) -> {}:
|
debug: bool, projectVersion: str) -> {}:
|
||||||
"""Creates a block via c2s
|
"""Creates a block via c2s
|
||||||
"""
|
"""
|
||||||
|
@ -191,8 +209,8 @@ def sendBlockViaServer(baseDir: str,session, \
|
||||||
fromDomainFull = fromDomain + ':' + str(fromPort)
|
fromDomainFull = fromDomain + ':' + str(fromPort)
|
||||||
|
|
||||||
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
||||||
ccUrl= \
|
ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + \
|
||||||
httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
fromNickname + '/followers'
|
||||||
|
|
||||||
blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
|
blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
|
||||||
newBlockJson = {
|
newBlockJson = {
|
||||||
|
@ -207,8 +225,8 @@ def sendBlockViaServer(baseDir: str,session, \
|
||||||
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
||||||
|
|
||||||
# lookup the inbox for the To handle
|
# lookup the inbox for the To handle
|
||||||
wfRequest= \
|
wfRequest = webfingerHandle(session, handle, httpPrefix,
|
||||||
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
cachedWebfingers,
|
||||||
fromDomain, projectVersion)
|
fromDomain, projectVersion)
|
||||||
if not wfRequest:
|
if not wfRequest:
|
||||||
if debug:
|
if debug:
|
||||||
|
@ -218,9 +236,12 @@ def sendBlockViaServer(baseDir: str,session, \
|
||||||
postToBox = 'outbox'
|
postToBox = 'outbox'
|
||||||
|
|
||||||
# get the actor inbox for the To handle
|
# get the actor inbox for the To handle
|
||||||
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
|
(inboxUrl, pubKeyId, pubKey,
|
||||||
getPersonBox(baseDir,session,wfRequest,personCache, \
|
fromPersonId, sharedInbox,
|
||||||
projectVersion,httpPrefix,fromNickname, \
|
capabilityAcquisition, avatarUrl,
|
||||||
|
displayName) = getPersonBox(baseDir, session, wfRequest,
|
||||||
|
personCache,
|
||||||
|
projectVersion, httpPrefix, fromNickname,
|
||||||
fromDomain, postToBox)
|
fromDomain, postToBox)
|
||||||
|
|
||||||
if not inboxUrl:
|
if not inboxUrl:
|
||||||
|
@ -235,23 +256,26 @@ def sendBlockViaServer(baseDir: str,session, \
|
||||||
authHeader = createBasicAuthHeader(fromNickname, password)
|
authHeader = createBasicAuthHeader(fromNickname, password)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'host': fromDomain, \
|
'host': fromDomain,
|
||||||
'Content-type': 'application/json', \
|
'Content-type': 'application/json',
|
||||||
'Authorization': authHeader
|
'Authorization': authHeader
|
||||||
}
|
}
|
||||||
postResult= \
|
postResult = postJson(session, newBlockJson, [], inboxUrl,
|
||||||
postJson(session,newBlockJson,[],inboxUrl,headers,"inbox:write")
|
headers, "inbox:write")
|
||||||
|
if not postResult:
|
||||||
|
print('WARN: Unable to post block')
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s POST block success')
|
print('DEBUG: c2s POST block success')
|
||||||
|
|
||||||
return newBlockJson
|
return newBlockJson
|
||||||
|
|
||||||
def sendUndoBlockViaServer(baseDir: str,session, \
|
|
||||||
fromNickname: str,password: str, \
|
def sendUndoBlockViaServer(baseDir: str, session,
|
||||||
fromDomain: str,fromPort: int, \
|
fromNickname: str, password: str,
|
||||||
httpPrefix: str,blockedUrl: str, \
|
fromDomain: str, fromPort: int,
|
||||||
cachedWebfingers: {},personCache: {}, \
|
httpPrefix: str, blockedUrl: str,
|
||||||
|
cachedWebfingers: {}, personCache: {},
|
||||||
debug: bool, projectVersion: str) -> {}:
|
debug: bool, projectVersion: str) -> {}:
|
||||||
"""Creates a block via c2s
|
"""Creates a block via c2s
|
||||||
"""
|
"""
|
||||||
|
@ -266,8 +290,8 @@ def sendUndoBlockViaServer(baseDir: str,session, \
|
||||||
fromDomainFull = fromDomain + ':' + str(fromPort)
|
fromDomainFull = fromDomain + ':' + str(fromPort)
|
||||||
|
|
||||||
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
||||||
ccUrl= \
|
ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + \
|
||||||
httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
fromNickname + '/followers'
|
||||||
|
|
||||||
blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
|
blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
|
||||||
newBlockJson = {
|
newBlockJson = {
|
||||||
|
@ -286,8 +310,8 @@ def sendUndoBlockViaServer(baseDir: str,session, \
|
||||||
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
||||||
|
|
||||||
# lookup the inbox for the To handle
|
# lookup the inbox for the To handle
|
||||||
wfRequest= \
|
wfRequest = webfingerHandle(session, handle, httpPrefix,
|
||||||
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
cachedWebfingers,
|
||||||
fromDomain, projectVersion)
|
fromDomain, projectVersion)
|
||||||
if not wfRequest:
|
if not wfRequest:
|
||||||
if debug:
|
if debug:
|
||||||
|
@ -297,9 +321,11 @@ def sendUndoBlockViaServer(baseDir: str,session, \
|
||||||
postToBox = 'outbox'
|
postToBox = 'outbox'
|
||||||
|
|
||||||
# get the actor inbox for the To handle
|
# get the actor inbox for the To handle
|
||||||
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
|
(inboxUrl, pubKeyId, pubKey,
|
||||||
getPersonBox(baseDir,session,wfRequest,personCache, \
|
fromPersonId, sharedInbox,
|
||||||
projectVersion,httpPrefix,fromNickname, \
|
capabilityAcquisition, avatarUrl,
|
||||||
|
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
|
||||||
|
projectVersion, httpPrefix, fromNickname,
|
||||||
fromDomain, postToBox)
|
fromDomain, postToBox)
|
||||||
|
|
||||||
if not inboxUrl:
|
if not inboxUrl:
|
||||||
|
@ -314,20 +340,23 @@ def sendUndoBlockViaServer(baseDir: str,session, \
|
||||||
authHeader = createBasicAuthHeader(fromNickname, password)
|
authHeader = createBasicAuthHeader(fromNickname, password)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'host': fromDomain, \
|
'host': fromDomain,
|
||||||
'Content-type': 'application/json', \
|
'Content-type': 'application/json',
|
||||||
'Authorization': authHeader
|
'Authorization': authHeader
|
||||||
}
|
}
|
||||||
postResult= \
|
postResult = postJson(session, newBlockJson, [], inboxUrl,
|
||||||
postJson(session,newBlockJson,[],inboxUrl,headers,"inbox:write")
|
headers, "inbox:write")
|
||||||
|
if not postResult:
|
||||||
|
print('WARN: Unable to post block')
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s POST block success')
|
print('DEBUG: c2s POST block success')
|
||||||
|
|
||||||
return newBlockJson
|
return newBlockJson
|
||||||
|
|
||||||
def outboxBlock(baseDir: str,httpPrefix: str, \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def outboxBlock(baseDir: str, httpPrefix: str,
|
||||||
|
nickname: str, domain: str, port: int,
|
||||||
messageJson: {}, debug: bool) -> None:
|
messageJson: {}, debug: bool) -> None:
|
||||||
""" When a block request is received by the outbox from c2s
|
""" When a block request is received by the outbox from c2s
|
||||||
"""
|
"""
|
||||||
|
@ -380,14 +409,15 @@ def outboxBlock(baseDir: str,httpPrefix: str, \
|
||||||
if ':' not in domainBlocked:
|
if ':' not in domainBlocked:
|
||||||
domainBlockedFull = domainBlocked + ':' + str(portBlocked)
|
domainBlockedFull = domainBlocked + ':' + str(portBlocked)
|
||||||
|
|
||||||
addBlock(baseDir,nickname,domain, \
|
addBlock(baseDir, nickname, domain,
|
||||||
nicknameBlocked, domainBlockedFull)
|
nicknameBlocked, domainBlockedFull)
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: post blocked via c2s - ' + postFilename)
|
print('DEBUG: post blocked via c2s - ' + postFilename)
|
||||||
|
|
||||||
def outboxUndoBlock(baseDir: str,httpPrefix: str, \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def outboxUndoBlock(baseDir: str, httpPrefix: str,
|
||||||
|
nickname: str, domain: str, port: int,
|
||||||
messageJson: {}, debug: bool) -> None:
|
messageJson: {}, debug: bool) -> None:
|
||||||
""" When an undo block request is received by the outbox from c2s
|
""" When an undo block request is received by the outbox from c2s
|
||||||
"""
|
"""
|
||||||
|
@ -448,18 +478,18 @@ def outboxUndoBlock(baseDir: str,httpPrefix: str, \
|
||||||
return
|
return
|
||||||
nicknameBlocked = getNicknameFromActor(messageJson['object']['object'])
|
nicknameBlocked = getNicknameFromActor(messageJson['object']['object'])
|
||||||
if not nicknameBlocked:
|
if not nicknameBlocked:
|
||||||
print('WARN: unable to find nickname in '+ \
|
print('WARN: unable to find nickname in ' +
|
||||||
messageJson['object']['object'])
|
messageJson['object']['object'])
|
||||||
return
|
return
|
||||||
domainBlocked,portBlocked= \
|
domainObject = messageJson['object']['object']
|
||||||
getDomainFromActor(messageJson['object']['object'])
|
domainBlocked, portBlocked = getDomainFromActor(domainObject)
|
||||||
domainBlockedFull = domainBlocked
|
domainBlockedFull = domainBlocked
|
||||||
if portBlocked:
|
if portBlocked:
|
||||||
if portBlocked != 80 and portBlocked != 443:
|
if portBlocked != 80 and portBlocked != 443:
|
||||||
if ':' not in domainBlocked:
|
if ':' not in domainBlocked:
|
||||||
domainBlockedFull = domainBlocked + ':' + str(portBlocked)
|
domainBlockedFull = domainBlocked + ':' + str(portBlocked)
|
||||||
|
|
||||||
removeBlock(baseDir,nickname,domain, \
|
removeBlock(baseDir, nickname, domain,
|
||||||
nicknameBlocked, domainBlockedFull)
|
nicknameBlocked, domainBlockedFull)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: post undo blocked via c2s - ' + postFilename)
|
print('DEBUG: post undo blocked via c2s - ' + postFilename)
|
||||||
|
|
Loading…
Reference in New Issue