From 982ab616d93397123860f07f2b3fb34f7ebaebfd Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 18 Jul 2019 12:35:48 +0100 Subject: [PATCH] Fix ports on post filenames --- daemon.py | 19 +++++++++++++++---- epicyon.py | 4 ++-- follow.py | 4 ++-- inbox.py | 29 +++++++++++++++++++++++++++-- posts.py | 7 +++++-- tests.py | 7 +++++++ 6 files changed, 58 insertions(+), 12 deletions(-) diff --git a/daemon.py b/daemon.py index bdd04768..5daa46f8 100644 --- a/daemon.py +++ b/daemon.py @@ -180,16 +180,23 @@ class PubServer(BaseHTTPRequestHandler): return False if messageJson.get('id'): postId=messageJson['id'].replace('/activity','') + if self.server.debug: + print('DEBUG: id attribute exists within POST to outbox') else: + if self.server.debug: + print('DEBUG: No id attribute within POST to outbox') postId=None if self.server.debug: pprint(messageJson) 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, \ self.server.httpPrefix, \ postId, \ self.postToNickname, \ - self.server.domain,messageJson,'outbox') + domainFull,messageJson,'outbox') if not self.server.session: if self.server.debug: print('DEBUG: creating new session for c2s') @@ -259,12 +266,16 @@ class PubServer(BaseHTTPRequestHandler): if len(self.server.inboxQueue)>=self.server.maxQueueLength: 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 = \ savePostToInboxQueue(self.server.baseDir, \ self.server.httpPrefix, \ nickname, \ - self.server.domain, \ + domainFull, \ messageJson, self.headers['host'], self.headers['signature'], @@ -736,7 +747,7 @@ class PubServer(BaseHTTPRequestHandler): messageBytes=self.rfile.read(length) messageJson=json.loads(messageBytes) - + # https://www.w3.org/TR/activitypub/#object-without-create if self.outboxAuthenticated: if self._postToOutbox(messageJson): diff --git a/epicyon.py b/epicyon.py index 9c6f41c9..ce2580cf 100644 --- a/epicyon.py +++ b/epicyon.py @@ -232,8 +232,8 @@ if args.tests: if args.testsnetwork: print('Network Tests') - testPostMessageBetweenServers() - testFollowBetweenServers() + #testPostMessageBetweenServers() + #testFollowBetweenServers() testClientToServer() sys.exit() diff --git a/follow.py b/follow.py index 17454798..125fa5b8 100644 --- a/follow.py +++ b/follow.py @@ -257,7 +257,7 @@ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \ if tempPort: fromPort=tempPort if tempPort!=80 and tempPort!=443: - domainFull=domain+':'+str(tempPort) + domainFull=domain+':'+str(tempPort) if not domainPermitted(domain,federationList): if debug: 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 - '+ \ baseDir+'/accounts/'+handleToFollow) return False - if not followerOfPerson(baseDir,nicknameToFollow,domainToFollowFull, \ nickname,domainFull,federationList,debug): @@ -384,6 +383,7 @@ def sendFollowRequestViaServer(session,fromNickname: str,password: str, fromDomainFull=fromDomain if fromPort!=80 and fromPort!=443: fromDomainFull=fromDomain+':'+str(fromPort) + followDomainFull=followDomain if followPort!=80 and followPort!=443: followDomainFull=followDomain+':'+str(followPort) diff --git a/inbox.py b/inbox.py index 8e30c8b3..058cc896 100644 --- a/inbox.py +++ b/inbox.py @@ -41,6 +41,8 @@ from blocking import isBlocked from filters import isFiltered def validInbox(baseDir: str,nickname: str,domain: str) -> bool: + """Checks whether files were correctly saved to the inbox + """ if ':' in domain: domain=domain.split(':')[0] inboxDir=baseDir+'/accounts/'+nickname+'@'+domain+'/inbox' @@ -53,7 +55,29 @@ def validInbox(baseDir: str,nickname: str,domain: str) -> bool: print('filename: '+filename) return False 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 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 keyId specifies the actor sending the post """ + originalDomain=domain if ':' in domain: domain=domain.split(':')[0] @@ -164,7 +189,7 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str,nickname: str, domain: str postId=postJsonObject['id'].replace('/activity','') else: statusNumber,published = getStatusNumber() - postId=httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber + postId=httpPrefix+'://'+originalDomain+'/users/'+nickname+'/statuses/'+statusNumber currTime=datetime.datetime.utcnow() published=currTime.strftime("%Y-%m-%dT%H:%M:%SZ") diff --git a/posts.py b/posts.py index 426db2e8..e8c1ae10 100644 --- a/posts.py +++ b/posts.py @@ -296,12 +296,13 @@ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \ """ if boxname!='inbox' and boxname!='outbox': return + originalDomain=domain if ':' in domain: domain=domain.split(':')[0] if not postId: statusNumber,published = getStatusNumber() - postId=httpPrefix+'://'+domain+'/users/'+nickname+ \ + postId=httpPrefix+'://'+originalDomain+'/users/'+nickname+ \ '/statuses/'+statusNumber postJsonObject['id']=postId+'/activity' if postJsonObject.get('object'): @@ -327,7 +328,8 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ nickname,domain,content) if port!=80 and port!=443: - domain=domain+':'+str(port) + if ':' not in domain: + domain=domain+':'+str(port) statusNumber,published = getStatusNumber() conversationDate=published.split('T')[0] @@ -884,6 +886,7 @@ def sendSignedJson(postJsonObject: {},session,baseDir: str, \ sendThreads.pop(0) if debug: print('DEBUG: starting thread to send post') + pprint(postJsonObject) thr = threadWithTrace(target=threadSendPost, \ args=(session, \ postJsonObject.copy(), \ diff --git a/tests.py b/tests.py index 232bd212..1ab9d5bb 100644 --- a/tests.py +++ b/tests.py @@ -55,6 +55,7 @@ from announce import sendAnnounceViaServer from media import getMediaPath from delete import sendDeleteViaServer from inbox import validInbox +from inbox import validInboxFilenames testServerAliceRunning = False testServerBobRunning = False @@ -366,6 +367,7 @@ def testPostMessageBetweenServers(): # queue item removed 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 validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort) print('\n\n*******************************************************') print("Bob likes Alice's post") @@ -562,6 +564,7 @@ def testFollowBetweenServers(): pprint(bobCapsJson) assert False assert validInbox(bobDir,'bob',bobDomain) + assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort) print('\n\n*********************************************************') print('Eve tries to send to Bob') @@ -1079,6 +1082,7 @@ def testClientToServer(): assert outboxPostId print('message id obtained: '+outboxPostId) assert validInbox(bobDir,'bob',bobDomain) + assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort) print('\n\nAlice follows Bob') 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 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert validInbox(bobDir,'bob',bobDomain) + assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort) print('\n\nBob follows Alice') 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 print(">>> post deleted from Alice's outbox and Bob's inbox") assert validInbox(bobDir,'bob',bobDomain) + assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort) 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 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert validInbox(bobDir,'bob',bobDomain) + assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort) # stop the servers thrAlice.kill()