Fix ports on post filenames

master
Bob Mottram 2019-07-18 12:35:48 +01:00
parent c70d37cdf2
commit 982ab616d9
6 changed files with 58 additions and 12 deletions

View File

@ -180,16 +180,23 @@ class PubServer(BaseHTTPRequestHandler):
return False return False
if messageJson.get('id'): if messageJson.get('id'):
postId=messageJson['id'].replace('/activity','') postId=messageJson['id'].replace('/activity','')
if self.server.debug:
print('DEBUG: id attribute exists within POST to outbox')
else: else:
if self.server.debug:
print('DEBUG: No id attribute within POST to outbox')
postId=None postId=None
if self.server.debug: if self.server.debug:
pprint(messageJson) pprint(messageJson)
print('DEBUG: savePostToBox') print('DEBUG: savePostToBox')
domainFull=self.server.domain
if self.server.port!=80 and self.server.port!=443:
domainFull=self.server.domain+':'+str(self.server.port)
savePostToBox(self.server.baseDir, \ savePostToBox(self.server.baseDir, \
self.server.httpPrefix, \ self.server.httpPrefix, \
postId, \ postId, \
self.postToNickname, \ self.postToNickname, \
self.server.domain,messageJson,'outbox') domainFull,messageJson,'outbox')
if not self.server.session: if not self.server.session:
if self.server.debug: if self.server.debug:
print('DEBUG: creating new session for c2s') print('DEBUG: creating new session for c2s')
@ -259,12 +266,16 @@ class PubServer(BaseHTTPRequestHandler):
if len(self.server.inboxQueue)>=self.server.maxQueueLength: if len(self.server.inboxQueue)>=self.server.maxQueueLength:
return 1 return 1
# save the json for later queue processing domainFull=self.server.domain
if self.server.port!=80 and self.server.port!=443:
domainFull=self.server.domain+':'+str(self.server.port)
# save the json for later queue processing
queueFilename = \ queueFilename = \
savePostToInboxQueue(self.server.baseDir, \ savePostToInboxQueue(self.server.baseDir, \
self.server.httpPrefix, \ self.server.httpPrefix, \
nickname, \ nickname, \
self.server.domain, \ domainFull, \
messageJson, messageJson,
self.headers['host'], self.headers['host'],
self.headers['signature'], self.headers['signature'],
@ -736,7 +747,7 @@ class PubServer(BaseHTTPRequestHandler):
messageBytes=self.rfile.read(length) messageBytes=self.rfile.read(length)
messageJson=json.loads(messageBytes) messageJson=json.loads(messageBytes)
# https://www.w3.org/TR/activitypub/#object-without-create # https://www.w3.org/TR/activitypub/#object-without-create
if self.outboxAuthenticated: if self.outboxAuthenticated:
if self._postToOutbox(messageJson): if self._postToOutbox(messageJson):

View File

@ -232,8 +232,8 @@ if args.tests:
if args.testsnetwork: if args.testsnetwork:
print('Network Tests') print('Network Tests')
testPostMessageBetweenServers() #testPostMessageBetweenServers()
testFollowBetweenServers() #testFollowBetweenServers()
testClientToServer() testClientToServer()
sys.exit() sys.exit()

View File

@ -257,7 +257,7 @@ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \
if tempPort: if tempPort:
fromPort=tempPort fromPort=tempPort
if tempPort!=80 and tempPort!=443: if tempPort!=80 and tempPort!=443:
domainFull=domain+':'+str(tempPort) domainFull=domain+':'+str(tempPort)
if not domainPermitted(domain,federationList): if not domainPermitted(domain,federationList):
if debug: if debug:
print('DEBUG: follower from domain not permitted - '+domain) print('DEBUG: follower from domain not permitted - '+domain)
@ -293,7 +293,6 @@ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \
print('DEBUG: followed account not found - '+ \ print('DEBUG: followed account not found - '+ \
baseDir+'/accounts/'+handleToFollow) baseDir+'/accounts/'+handleToFollow)
return False return False
if not followerOfPerson(baseDir,nicknameToFollow,domainToFollowFull, \ if not followerOfPerson(baseDir,nicknameToFollow,domainToFollowFull, \
nickname,domainFull,federationList,debug): nickname,domainFull,federationList,debug):
@ -384,6 +383,7 @@ def sendFollowRequestViaServer(session,fromNickname: str,password: str,
fromDomainFull=fromDomain fromDomainFull=fromDomain
if fromPort!=80 and fromPort!=443: if fromPort!=80 and fromPort!=443:
fromDomainFull=fromDomain+':'+str(fromPort) fromDomainFull=fromDomain+':'+str(fromPort)
followDomainFull=followDomain followDomainFull=followDomain
if followPort!=80 and followPort!=443: if followPort!=80 and followPort!=443:
followDomainFull=followDomain+':'+str(followPort) followDomainFull=followDomain+':'+str(followPort)

View File

@ -41,6 +41,8 @@ from blocking import isBlocked
from filters import isFiltered from filters import isFiltered
def validInbox(baseDir: str,nickname: str,domain: str) -> bool: def validInbox(baseDir: str,nickname: str,domain: str) -> bool:
"""Checks whether files were correctly saved to the inbox
"""
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain=domain.split(':')[0]
inboxDir=baseDir+'/accounts/'+nickname+'@'+domain+'/inbox' inboxDir=baseDir+'/accounts/'+nickname+'@'+domain+'/inbox'
@ -53,7 +55,29 @@ def validInbox(baseDir: str,nickname: str,domain: str) -> bool:
print('filename: '+filename) print('filename: '+filename)
return False return False
if 'postNickname' in open(filename).read(): if 'postNickname' in open(filename).read():
print(filename) print('queue file incorrectly saved to '+filename)
return False
return True
def validInboxFilenames(baseDir: str,nickname: str,domain: str, \
expectedDomain: str,expectedPort: int) -> bool:
"""Used by unit tests to check that the port number gets appended to
domain names within saved post filenames
"""
if ':' in domain:
domain=domain.split(':')[0]
inboxDir=baseDir+'/accounts/'+nickname+'@'+domain+'/inbox'
if not os.path.isdir(inboxDir):
return True
expectedStr=expectedDomain+':'+str(expectedPort)
for subdir, dirs, files in os.walk(inboxDir):
for f in files:
filename = os.path.join(subdir, f)
if not os.path.isfile(filename):
print('filename: '+filename)
return False
if not expectedStr in filename:
print('Invalid filename: '+filename)
return False return False
return True return True
@ -137,6 +161,7 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str,nickname: str, domain: 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
""" """
originalDomain=domain
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain=domain.split(':')[0]
@ -164,7 +189,7 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str,nickname: str, domain: str
postId=postJsonObject['id'].replace('/activity','') postId=postJsonObject['id'].replace('/activity','')
else: else:
statusNumber,published = getStatusNumber() statusNumber,published = getStatusNumber()
postId=httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber postId=httpPrefix+'://'+originalDomain+'/users/'+nickname+'/statuses/'+statusNumber
currTime=datetime.datetime.utcnow() currTime=datetime.datetime.utcnow()
published=currTime.strftime("%Y-%m-%dT%H:%M:%SZ") published=currTime.strftime("%Y-%m-%dT%H:%M:%SZ")

View File

@ -296,12 +296,13 @@ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \
""" """
if boxname!='inbox' and boxname!='outbox': if boxname!='inbox' and boxname!='outbox':
return return
originalDomain=domain
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain=domain.split(':')[0]
if not postId: if not postId:
statusNumber,published = getStatusNumber() statusNumber,published = getStatusNumber()
postId=httpPrefix+'://'+domain+'/users/'+nickname+ \ postId=httpPrefix+'://'+originalDomain+'/users/'+nickname+ \
'/statuses/'+statusNumber '/statuses/'+statusNumber
postJsonObject['id']=postId+'/activity' postJsonObject['id']=postId+'/activity'
if postJsonObject.get('object'): if postJsonObject.get('object'):
@ -327,7 +328,8 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
nickname,domain,content) nickname,domain,content)
if port!=80 and port!=443: if port!=80 and port!=443:
domain=domain+':'+str(port) if ':' not in domain:
domain=domain+':'+str(port)
statusNumber,published = getStatusNumber() statusNumber,published = getStatusNumber()
conversationDate=published.split('T')[0] conversationDate=published.split('T')[0]
@ -884,6 +886,7 @@ def sendSignedJson(postJsonObject: {},session,baseDir: str, \
sendThreads.pop(0) sendThreads.pop(0)
if debug: if debug:
print('DEBUG: starting thread to send post') print('DEBUG: starting thread to send post')
pprint(postJsonObject)
thr = threadWithTrace(target=threadSendPost, \ thr = threadWithTrace(target=threadSendPost, \
args=(session, \ args=(session, \
postJsonObject.copy(), \ postJsonObject.copy(), \

View File

@ -55,6 +55,7 @@ from announce import sendAnnounceViaServer
from media import getMediaPath from media import getMediaPath
from delete import sendDeleteViaServer from delete import sendDeleteViaServer
from inbox import validInbox from inbox import validInbox
from inbox import validInboxFilenames
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -366,6 +367,7 @@ def testPostMessageBetweenServers():
# queue item removed # queue item removed
assert len([name for name in os.listdir(queuePath) if os.path.isfile(os.path.join(queuePath, name))])==0 assert len([name for name in os.listdir(queuePath) if os.path.isfile(os.path.join(queuePath, name))])==0
assert validInbox(bobDir,'bob',bobDomain) assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
print('\n\n*******************************************************') print('\n\n*******************************************************')
print("Bob likes Alice's post") print("Bob likes Alice's post")
@ -562,6 +564,7 @@ def testFollowBetweenServers():
pprint(bobCapsJson) pprint(bobCapsJson)
assert False assert False
assert validInbox(bobDir,'bob',bobDomain) assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
print('\n\n*********************************************************') print('\n\n*********************************************************')
print('Eve tries to send to Bob') print('Eve tries to send to Bob')
@ -1079,6 +1082,7 @@ def testClientToServer():
assert outboxPostId assert outboxPostId
print('message id obtained: '+outboxPostId) print('message id obtained: '+outboxPostId)
assert validInbox(bobDir,'bob',bobDomain) assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
print('\n\nAlice follows Bob') print('\n\nAlice follows Bob')
sendFollowRequestViaServer(sessionAlice,'alice',password, \ sendFollowRequestViaServer(sessionAlice,'alice',password, \
@ -1100,6 +1104,7 @@ def testClientToServer():
assert 'alice@'+aliceDomain+':'+str(alicePort) in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read() assert 'alice@'+aliceDomain+':'+str(alicePort) in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read()
assert 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read()
assert validInbox(bobDir,'bob',bobDomain) assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
print('\n\nBob follows Alice') print('\n\nBob follows Alice')
sendFollowRequestViaServer(sessionAlice,'bob','bobpass', \ sendFollowRequestViaServer(sessionAlice,'bob','bobpass', \
@ -1187,6 +1192,7 @@ def testClientToServer():
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==postsBefore-1 assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==postsBefore-1
print(">>> post deleted from Alice's outbox and Bob's inbox") print(">>> post deleted from Alice's outbox and Bob's inbox")
assert validInbox(bobDir,'bob',bobDomain) assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
print('\n\nAlice unfollows Bob') print('\n\nAlice unfollows Bob')
@ -1208,6 +1214,7 @@ def testClientToServer():
assert 'alice@'+aliceDomain+':'+str(alicePort) not in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read() assert 'alice@'+aliceDomain+':'+str(alicePort) not in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read()
assert 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read()
assert validInbox(bobDir,'bob',bobDomain) assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
# stop the servers # stop the servers
thrAlice.kill() thrAlice.kill()