Onion webfinger requests

main
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:
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:
msg=json.dumps(wfResult).encode('utf-8')
self._set_headers('application/jrd+json',len(msg),None)

View File

@ -56,6 +56,27 @@ def loadJson(filename: str,delaySec=2) -> {}:
tries+=1
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):
"""Returns the status number and published date
"""

View File

@ -17,6 +17,7 @@ from session import getJson
from cache import storeWebfingerInCache
from cache import getWebfingerFromCache
from utils import loadJson
from utils import loadJsonOnionify
from utils import saveJson
def parseHandle(handle: str) -> (str,str):
@ -181,7 +182,9 @@ def webfingerMeta(httpPrefix: str,domainFull: str) -> str:
metaStr+="</XRD>"
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
"""
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]
if handle.startswith(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'
if debug:
print('DEBUG: WEBFINGER filename '+filename)
@ -224,7 +234,11 @@ def webfingerLookup(path: str,baseDir: str,port: int,debug: bool) -> {}:
if debug:
print('DEBUG: WEBFINGER filename not found '+filename)
return None
wfJson=loadJson(filename)
if not onionify:
wfJson=loadJson(filename)
else:
print('Webfinger request for onionified '+handle)
wfJson=loadJsonOnionify(filename,domain,onionDomain)
if not wfJson:
wfJson={"nickname": "unknown"}
return wfJson