Onion webfinger requests

merge-requests/30/head
Bob Mottram 2020-03-02 14:35:44 +00:00
parent b44d364449
commit 3c5b549db7
3 changed files with 40 additions and 3 deletions

View File

@ -651,7 +651,9 @@ class PubServer(BaseHTTPRequestHandler):
if self.server.debug: if self.server.debug:
print('DEBUG: WEBFINGER lookup '+self.path+' '+str(self.server.baseDir)) print('DEBUG: WEBFINGER lookup '+self.path+' '+str(self.server.baseDir))
wfResult=webfingerLookup(self.path,self.server.baseDir,self.server.port,self.server.debug) wfResult=webfingerLookup(self.path,self.server.baseDir, \
self.server.domain,self.server.onionDomain, \
self.server.port,self.server.debug)
if wfResult: if wfResult:
msg=json.dumps(wfResult).encode('utf-8') msg=json.dumps(wfResult).encode('utf-8')
self._set_headers('application/jrd+json',len(msg),None) self._set_headers('application/jrd+json',len(msg),None)

View File

@ -56,6 +56,27 @@ def loadJson(filename: str,delaySec=2) -> {}:
tries+=1 tries+=1
return jsonObject return jsonObject
def loadJsonOnionify(filename: str,domain: str,onionDomain: str,delaySec=2) -> {}:
"""Makes a few attempts to load a json formatted file
This also converts the domain name to the onion domain
"""
jsonObject=None
tries=0
while tries<5:
try:
with open(filename, 'r') as fp:
data=fp.read()
if data:
data=data.replace(domain,onionDomain)
jsonObject=json.loads(data)
break
except:
print('WARN: loadJson exception')
if delaySec>0:
time.sleep(delaySec)
tries+=1
return jsonObject
def getStatusNumber() -> (str,str): def getStatusNumber() -> (str,str):
"""Returns the status number and published date """Returns the status number and published date
""" """

View File

@ -17,6 +17,7 @@ from session import getJson
from cache import storeWebfingerInCache from cache import storeWebfingerInCache
from cache import getWebfingerFromCache from cache import getWebfingerFromCache
from utils import loadJson from utils import loadJson
from utils import loadJsonOnionify
from utils import saveJson from utils import saveJson
def parseHandle(handle: str) -> (str,str): def parseHandle(handle: str) -> (str,str):
@ -181,7 +182,9 @@ def webfingerMeta(httpPrefix: str,domainFull: str) -> str:
metaStr+="</XRD>" metaStr+="</XRD>"
return metaStr return metaStr
def webfingerLookup(path: str,baseDir: str,port: int,debug: bool) -> {}: def webfingerLookup(path: str,baseDir: str, \
domain: str,onionDomain: str, \
port: int,debug: bool) -> {}:
"""Lookup the webfinger endpoint for an account """Lookup the webfinger endpoint for an account
""" """
if not path.startswith('/.well-known/webfinger?'): if not path.startswith('/.well-known/webfinger?'):
@ -217,6 +220,13 @@ def webfingerLookup(path: str,baseDir: str,port: int,debug: bool) -> {}:
handleDomain=handle.split('@')[1] handleDomain=handle.split('@')[1]
if handle.startswith(handleDomain+'@'): if handle.startswith(handleDomain+'@'):
handle='inbox@'+handleDomain handle='inbox@'+handleDomain
# if this is a lookup for a handle using its onion domain
# then swap the onion domain for the clearnet version
onionify=False
if onionDomain:
if onionDomain in handle:
handle=handle.replace(onionDomain,domain)
onionify=True
filename=baseDir+'/wfendpoints/'+handle.lower()+'.json' filename=baseDir+'/wfendpoints/'+handle.lower()+'.json'
if debug: if debug:
print('DEBUG: WEBFINGER filename '+filename) print('DEBUG: WEBFINGER filename '+filename)
@ -224,7 +234,11 @@ def webfingerLookup(path: str,baseDir: str,port: int,debug: bool) -> {}:
if debug: if debug:
print('DEBUG: WEBFINGER filename not found '+filename) print('DEBUG: WEBFINGER filename not found '+filename)
return None return None
if not onionify:
wfJson=loadJson(filename) wfJson=loadJson(filename)
else:
print('Webfinger request for onionified '+handle)
wfJson=loadJsonOnionify(filename,domain,onionDomain)
if not wfJson: if not wfJson:
wfJson={"nickname": "unknown"} wfJson={"nickname": "unknown"}
return wfJson return wfJson