Debug option

master
Bob Mottram 2019-07-03 17:14:45 +01:00
parent 0a348ac02a
commit be525dff0a
3 changed files with 90 additions and 34 deletions

105
daemon.py
View File

@ -62,11 +62,13 @@ class PubServer(BaseHTTPRequestHandler):
self.wfile.write("<html><head></head><body><h1>404 Not Found</h1></body></html>".encode('utf-8')) self.wfile.write("<html><head></head><body><h1>404 Not Found</h1></body></html>".encode('utf-8'))
def _webfinger(self) -> bool: def _webfinger(self) -> bool:
print('############### _webfinger well-known') if self.server.debug:
print('DEBUG: WEBFINGER well-known')
if not self.path.startswith('/.well-known'): if not self.path.startswith('/.well-known'):
return False return False
print('############### _webfinger host-meta') if self.server.debug:
print('DEBUG: WEBFINGER host-meta')
if self.path.startswith('/.well-known/host-meta'): if self.path.startswith('/.well-known/host-meta'):
wfResult=webfingerMeta() wfResult=webfingerMeta()
if wfResult: if wfResult:
@ -74,13 +76,15 @@ class PubServer(BaseHTTPRequestHandler):
self.wfile.write(wfResult.encode('utf-8')) self.wfile.write(wfResult.encode('utf-8'))
return 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) wfResult=webfingerLookup(self.path,self.server.baseDir)
if wfResult: if wfResult:
self._set_headers('application/jrd+json') self._set_headers('application/jrd+json')
self.wfile.write(json.dumps(wfResult).encode('utf-8')) self.wfile.write(json.dumps(wfResult).encode('utf-8'))
else: else:
print('############### _webfinger lookup 404 '+self.path) if self.server.debug:
print('DEBUG: WEBFINGER lookup 404 '+self.path)
self._404() self._404()
return True return True
@ -92,29 +96,35 @@ class PubServer(BaseHTTPRequestHandler):
return True return True
def do_GET(self): 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: if self.server.GETbusy:
currTimeGET=int(time.time()) currTimeGET=int(time.time())
if currTimeGET-self.server.lastGET<10: if currTimeGET-self.server.lastGET<10:
print('############### Busy') if self.server.debug:
print('DEBUG: GET Busy')
self.send_response(429) self.send_response(429)
self.end_headers() self.end_headers()
return return
self.server.lastGET=currTimeGET self.server.lastGET=currTimeGET
self.server.GETbusy=True self.server.GETbusy=True
print('############### _permittedDir') if self.server.debug:
print('DEBUG: GET _permittedDir')
if not self._permittedDir(self.path): if not self._permittedDir(self.path):
print('############# Not permitted') if self.server.debug:
print('DEBUG: GET Not permitted')
self._404() self._404()
self.server.GETbusy=False self.server.GETbusy=False
return return
# get webfinger endpoint for a person # get webfinger endpoint for a person
print('############### _webfinger') if self.server.debug:
print('DEBUG: GET webfinger start')
if self._webfinger(): if self._webfinger():
self.server.GETbusy=False self.server.GETbusy=False
return return
print('############### _webfinger end') if self.server.debug:
print('DEBUG: GET webfinger end')
# get outbox feed for a person # get outbox feed for a person
outboxFeed=personOutboxJson(self.server.baseDir,self.server.domain, \ outboxFeed=personOutboxJson(self.server.baseDir,self.server.domain, \
self.server.port,self.path, \ self.server.port,self.path, \
@ -157,7 +167,8 @@ class PubServer(BaseHTTPRequestHandler):
return return
# check that a json file was requested # check that a json file was requested
if not self.path.endswith('.json'): 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._404()
self.server.GETbusy=False self.server.GETbusy=False
return return
@ -170,7 +181,8 @@ class PubServer(BaseHTTPRequestHandler):
contentJson=json.loads(content) contentJson=json.loads(content)
self.wfile.write(json.dumps(contentJson).encode('utf8')) self.wfile.write(json.dumps(contentJson).encode('utf8'))
else: else:
print('############# Unknown file') if self.server.debug:
print('DEBUG: GET Unknown file')
self._404() self._404()
self.server.GETbusy=False self.server.GETbusy=False
@ -185,19 +197,34 @@ class PubServer(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
return return
self.server.lastPOST=currTimePOST self.server.lastPOST=currTimePOST
print('**************** POST ready to receive')
self.server.POSTbusy=True self.server.POSTbusy=True
if not self.headers.get('Content-type'): if not self.headers.get('Content-type'):
print('**************** No Content-type') print('Content-type header missing')
self.send_response(400) self.send_response(400)
self.end_headers() self.end_headers()
self.server.POSTbusy=False self.server.POSTbusy=False
return return
print('*****************headers: '+str(self.headers))
# refuse to receive non-json content # refuse to receive non-json content
if self.headers['Content-type'] != 'application/json': 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.send_response(400)
self.end_headers() self.end_headers()
self.server.POSTbusy=False self.server.POSTbusy=False
@ -205,18 +232,24 @@ class PubServer(BaseHTTPRequestHandler):
# read the message and convert it into a python dictionary # read the message and convert it into a python dictionary
length = int(self.headers['Content-length']) length = int(self.headers['Content-length'])
print('**************** content-length: '+str(length)) if self.server.debug:
print('DEBUG: content-length: '+str(length))
if length>maxMessageLength: if length>maxMessageLength:
self.send_response(400) self.send_response(400)
self.end_headers() self.end_headers()
self.server.POSTbusy=False self.server.POSTbusy=False
return return
print('**************** Reading message')
if self.server.debug:
print('DEBUG: Reading message')
messageBytes=self.rfile.read(length) messageBytes=self.rfile.read(length)
messageJson = json.loads(messageBytes) messageJson = json.loads(messageBytes)
# check the necessary properties are available # 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): if not inboxMessageHasParams(messageJson):
self.send_response(403) self.send_response(403)
self.end_headers() self.end_headers()
@ -224,7 +257,8 @@ class PubServer(BaseHTTPRequestHandler):
return return
if not inboxPermittedMessage(self.server.domain,messageJson,self.server.federationList): 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.send_response(403)
self.end_headers() self.end_headers()
self.server.POSTbusy=False self.server.POSTbusy=False
@ -232,27 +266,38 @@ class PubServer(BaseHTTPRequestHandler):
pprint(messageJson) pprint(messageJson)
print('**************** POST create session') if self.server.debug:
print('DEBUG: POST create session')
currSessionTime=int(time.time()) currSessionTime=int(time.time())
if currSessionTime-self.server.sessionLastUpdate>1200: if currSessionTime-self.server.sessionLastUpdate>1200:
self.server.sessionLastUpdate=currSessionTime self.server.sessionLastUpdate=currSessionTime
self.server.session = \ self.server.session = \
createSession(self.server.domain,self.server.port, \ createSession(self.server.domain,self.server.port, \
self.server.useTor) 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'] 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, \ pubKey=getPersonPubKey(self.server.session,personUrl, \
self.server.personCache) self.server.personCache)
if not pubKey: 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.send_response(401)
self.end_headers() self.end_headers()
self.server.POSTbusy=False self.server.POSTbusy=False
return return
print('**************** POST check signature')
if self.server.debug:
print('DEBUG: POST check signature')
if not verifyPostHeaders(self.server.https, pubKey, self.headers, \ if not verifyPostHeaders(self.server.https, pubKey, self.headers, \
'/inbox' ,False, json.dumps(messageJson)): '/inbox' ,False, json.dumps(messageJson)):
print('**************** POST signature verification failed') print('**************** POST signature verification failed')
@ -260,7 +305,10 @@ class PubServer(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.server.POSTbusy=False self.server.POSTbusy=False
return return
print('**************** POST valid')
if self.server.debug:
print('DEBUG: POST valid')
if receiveFollowRequest(self.server.baseDir,messageJson, \ if receiveFollowRequest(self.server.baseDir,messageJson, \
self.server.federationList): self.server.federationList):
self.send_response(200) self.send_response(200)
@ -281,7 +329,7 @@ class PubServer(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.server.POSTbusy=False 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: if len(domain)==0:
domain='localhost' domain='localhost'
if '.' not in domain: if '.' not in domain:
@ -294,6 +342,7 @@ def runDaemon(domain: str,port=80,https=True,fedList=[],useTor=False) -> None:
httpd.domain=domain httpd.domain=domain
httpd.port=port httpd.port=port
httpd.https=https httpd.https=https
httpd.debug=debug
httpd.federationList=fedList.copy() httpd.federationList=fedList.copy()
httpd.baseDir=os.getcwd() httpd.baseDir=os.getcwd()
httpd.personCache={} httpd.personCache={}

View File

@ -61,6 +61,9 @@ parser.add_argument('--postsraw', dest='postsraw', type=str,default=None,
help='Show raw json of posts for the given handle') help='Show raw json of posts for the given handle')
parser.add_argument('-f','--federate', nargs='+',dest='federationList', parser.add_argument('-f','--federate', nargs='+',dest='federationList',
help='Specify federation list separated by spaces') 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='?', parser.add_argument("--http", type=str2bool, nargs='?',
const=True, default=False, const=True, default=False,
help="Use http only") help="Use http only")
@ -75,6 +78,10 @@ parser.add_argument("--testsnetwork", type=str2bool, nargs='?',
help="Run network unit tests") help="Run network unit tests")
args = parser.parse_args() args = parser.parse_args()
debug=False
if args.debug:
debug=True
if args.tests: if args.tests:
runAllTests() runAllTests()
sys.exit() sys.exit()
@ -120,4 +127,4 @@ if not os.path.isdir(baseDir+'/accounts/'+nickname+'@'+domain):
print('Creating default admin account '+nickname+'@'+domain) print('Creating default admin account '+nickname+'@'+domain)
privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(baseDir,nickname,domain,port,https,True) privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(baseDir,nickname,domain,port,https,True)
runDaemon(domain,port,https,federationList,useTor) runDaemon(domain,port,https,federationList,useTor,debug)

View File

@ -121,7 +121,7 @@ def createServerAlice(path: str,domain: str,port: int,federationList: []):
global testServerAliceRunning global testServerAliceRunning
testServerAliceRunning = True testServerAliceRunning = True
print('Server running: Alice') 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: []): def createServerBob(path: str,domain: str,port: int,federationList: []):
print('Creating test server: Bob on port '+str(port)) print('Creating test server: Bob on port '+str(port))
@ -143,7 +143,7 @@ def createServerBob(path: str,domain: str,port: int,federationList: []):
global testServerBobRunning global testServerBobRunning
testServerBobRunning = True testServerBobRunning = True
print('Server running: Bob') print('Server running: Bob')
runDaemon(domain,port,https,federationList,useTor) runDaemon(domain,port,https,federationList,useTor,True)
def testPostMessageBetweenServers(): def testPostMessageBetweenServers():
print('Testing sending message from one server to the inbox of another') print('Testing sending message from one server to the inbox of another')