From be525dff0ae2a1166ee17ca38a391d6ec6e7011e Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 3 Jul 2019 17:14:45 +0100 Subject: [PATCH] Debug option --- daemon.py | 109 ++++++++++++++++++++++++++++++++++++++--------------- epicyon.py | 11 +++++- tests.py | 4 +- 3 files changed, 90 insertions(+), 34 deletions(-) diff --git a/daemon.py b/daemon.py index e1d46415..3571666f 100644 --- a/daemon.py +++ b/daemon.py @@ -62,11 +62,13 @@ class PubServer(BaseHTTPRequestHandler): self.wfile.write("

404 Not Found

".encode('utf-8')) def _webfinger(self) -> bool: - print('############### _webfinger well-known') + if self.server.debug: + print('DEBUG: WEBFINGER well-known') if not self.path.startswith('/.well-known'): return False - print('############### _webfinger host-meta') + if self.server.debug: + print('DEBUG: WEBFINGER host-meta') if self.path.startswith('/.well-known/host-meta'): wfResult=webfingerMeta() if wfResult: @@ -74,13 +76,15 @@ class PubServer(BaseHTTPRequestHandler): self.wfile.write(wfResult.encode('utf-8')) return - print('############### _webfinger lookup '+self.path+' '+str(self.server.baseDir)) + if self.server.debug: + print('DEBUG: WEBFINGER lookup '+self.path+' '+str(self.server.baseDir)) wfResult=webfingerLookup(self.path,self.server.baseDir) if wfResult: self._set_headers('application/jrd+json') self.wfile.write(json.dumps(wfResult).encode('utf-8')) else: - print('############### _webfinger lookup 404 '+self.path) + if self.server.debug: + print('DEBUG: WEBFINGER lookup 404 '+self.path) self._404() return True @@ -92,29 +96,35 @@ class PubServer(BaseHTTPRequestHandler): return True def do_GET(self): - print('############### GET from '+self.server.baseDir+' path: '+self.path) + if self.server.debug: + print('DEBUG: GET from '+self.server.baseDir+' path: '+self.path) if self.server.GETbusy: currTimeGET=int(time.time()) if currTimeGET-self.server.lastGET<10: - print('############### Busy') + if self.server.debug: + print('DEBUG: GET Busy') self.send_response(429) self.end_headers() return self.server.lastGET=currTimeGET self.server.GETbusy=True - print('############### _permittedDir') + if self.server.debug: + print('DEBUG: GET _permittedDir') if not self._permittedDir(self.path): - print('############# Not permitted') + if self.server.debug: + print('DEBUG: GET Not permitted') self._404() self.server.GETbusy=False return # get webfinger endpoint for a person - print('############### _webfinger') + if self.server.debug: + print('DEBUG: GET webfinger start') if self._webfinger(): self.server.GETbusy=False return - print('############### _webfinger end') + if self.server.debug: + print('DEBUG: GET webfinger end') # get outbox feed for a person outboxFeed=personOutboxJson(self.server.baseDir,self.server.domain, \ self.server.port,self.path, \ @@ -157,7 +167,8 @@ class PubServer(BaseHTTPRequestHandler): return # check that a json file was requested if not self.path.endswith('.json'): - print('############# Not json: '+self.path+' '+self.server.baseDir) + if self.server.debug: + print('DEBUG: GET Not json: '+self.path+' '+self.server.baseDir) self._404() self.server.GETbusy=False return @@ -170,7 +181,8 @@ class PubServer(BaseHTTPRequestHandler): contentJson=json.loads(content) self.wfile.write(json.dumps(contentJson).encode('utf8')) else: - print('############# Unknown file') + if self.server.debug: + print('DEBUG: GET Unknown file') self._404() self.server.GETbusy=False @@ -185,38 +197,59 @@ class PubServer(BaseHTTPRequestHandler): self.end_headers() return self.server.lastPOST=currTimePOST - print('**************** POST ready to receive') + self.server.POSTbusy=True if not self.headers.get('Content-type'): - print('**************** No Content-type') + print('Content-type header missing') self.send_response(400) self.end_headers() self.server.POSTbusy=False return - print('*****************headers: '+str(self.headers)) # refuse to receive non-json content if self.headers['Content-type'] != 'application/json': - print("**************** That's no Json!") + print("POST is not json: "+self.headers['Content-type']) self.send_response(400) self.end_headers() self.server.POSTbusy=False return - + + # TODO + if self.path=='/outbox': + print('c2s posts not supported yet') + self.send_response(400) + self.end_headers() + self.server.POSTbusy=False + return + + # check that the post is to an expected path + if not (self.path=='/outbox' or self.path.endswith('/inbox')): + print('Attempt to POST to invalid path '+self.path) + self.send_response(400) + self.end_headers() + self.server.POSTbusy=False + return + # read the message and convert it into a python dictionary length = int(self.headers['Content-length']) - print('**************** content-length: '+str(length)) + if self.server.debug: + print('DEBUG: content-length: '+str(length)) if length>maxMessageLength: self.send_response(400) self.end_headers() self.server.POSTbusy=False return - print('**************** Reading message') + + if self.server.debug: + print('DEBUG: Reading message') + messageBytes=self.rfile.read(length) messageJson = json.loads(messageBytes) # check the necessary properties are available - print('**************** Check message has params') + if self.server.debug: + print('DEBUG: Check message has params') + if not inboxMessageHasParams(messageJson): self.send_response(403) self.end_headers() @@ -224,7 +257,8 @@ class PubServer(BaseHTTPRequestHandler): return if not inboxPermittedMessage(self.server.domain,messageJson,self.server.federationList): - print('**************** Ah Ah Ah') + if self.server.debug: + print('DEBUG: Ah Ah Ah') self.send_response(403) self.end_headers() self.server.POSTbusy=False @@ -232,35 +266,49 @@ class PubServer(BaseHTTPRequestHandler): pprint(messageJson) - print('**************** POST create session') + if self.server.debug: + print('DEBUG: POST create session') + currSessionTime=int(time.time()) if currSessionTime-self.server.sessionLastUpdate>1200: self.server.sessionLastUpdate=currSessionTime self.server.session = \ createSession(self.server.domain,self.server.port, \ self.server.useTor) - print('**************** POST started new session') + if self.server.debug: + print('DEBUG: POST started new session') - print('**************** POST get actor url from '+self.server.baseDir) + if self.server.debug: + print('DEBUG: POST get actor url from '+self.server.baseDir) personUrl=messageJson['actor'] - print('**************** POST get public key of '+personUrl+' from '+self.server.baseDir) + + if self.server.debug: + print('DEBUG: POST get public key of '+personUrl+' from '+self.server.baseDir) + pubKey=getPersonPubKey(self.server.session,personUrl, \ self.server.personCache) if not pubKey: - print('**************** POST no sender public key') + if self.server.debug: + print('DEBUG: POST no sender public key') self.send_response(401) self.end_headers() self.server.POSTbusy=False return - print('**************** POST check signature') + + if self.server.debug: + print('DEBUG: POST check signature') + if not verifyPostHeaders(self.server.https, pubKey, self.headers, \ '/inbox' ,False, json.dumps(messageJson)): print('**************** POST signature verification failed') self.send_response(401) self.end_headers() self.server.POSTbusy=False - return - print('**************** POST valid') + return + + if self.server.debug: + print('DEBUG: POST valid') + if receiveFollowRequest(self.server.baseDir,messageJson, \ self.server.federationList): self.send_response(200) @@ -281,7 +329,7 @@ class PubServer(BaseHTTPRequestHandler): self.end_headers() self.server.POSTbusy=False -def runDaemon(domain: str,port=80,https=True,fedList=[],useTor=False) -> None: +def runDaemon(domain: str,port=80,https=True,fedList=[],useTor=False,debug=False) -> None: if len(domain)==0: domain='localhost' if '.' not in domain: @@ -294,6 +342,7 @@ def runDaemon(domain: str,port=80,https=True,fedList=[],useTor=False) -> None: httpd.domain=domain httpd.port=port httpd.https=https + httpd.debug=debug httpd.federationList=fedList.copy() httpd.baseDir=os.getcwd() httpd.personCache={} diff --git a/epicyon.py b/epicyon.py index 85b36857..4241e3a9 100644 --- a/epicyon.py +++ b/epicyon.py @@ -61,6 +61,9 @@ parser.add_argument('--postsraw', dest='postsraw', type=str,default=None, help='Show raw json of posts for the given handle') parser.add_argument('-f','--federate', nargs='+',dest='federationList', help='Specify federation list separated by spaces') +parser.add_argument("--debug", type=str2bool, nargs='?', + const=True, default=False, + help="Show debug messages") parser.add_argument("--http", type=str2bool, nargs='?', const=True, default=False, help="Use http only") @@ -75,6 +78,10 @@ parser.add_argument("--testsnetwork", type=str2bool, nargs='?', help="Run network unit tests") args = parser.parse_args() +debug=False +if args.debug: + debug=True + if args.tests: runAllTests() sys.exit() @@ -99,7 +106,7 @@ if args.postsraw: if not args.domain: print('Specify a domain with --domain [name]') sys.exit() - + nickname='admin' domain=args.domain port=args.port @@ -120,4 +127,4 @@ if not os.path.isdir(baseDir+'/accounts/'+nickname+'@'+domain): print('Creating default admin account '+nickname+'@'+domain) privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(baseDir,nickname,domain,port,https,True) -runDaemon(domain,port,https,federationList,useTor) +runDaemon(domain,port,https,federationList,useTor,debug) diff --git a/tests.py b/tests.py index 4c3b4474..abf49e45 100644 --- a/tests.py +++ b/tests.py @@ -121,7 +121,7 @@ def createServerAlice(path: str,domain: str,port: int,federationList: []): global testServerAliceRunning testServerAliceRunning = True print('Server running: Alice') - runDaemon(domain,port,https,federationList,useTor) + runDaemon(domain,port,https,federationList,useTor,True) def createServerBob(path: str,domain: str,port: int,federationList: []): print('Creating test server: Bob on port '+str(port)) @@ -143,7 +143,7 @@ def createServerBob(path: str,domain: str,port: int,federationList: []): global testServerBobRunning testServerBobRunning = True print('Server running: Bob') - runDaemon(domain,port,https,federationList,useTor) + runDaemon(domain,port,https,federationList,useTor,True) def testPostMessageBetweenServers(): print('Testing sending message from one server to the inbox of another')