chainlink/chainlink.py

77 lines
2.2 KiB
Python
Raw Normal View History

2020-01-03 15:00:44 -05:00
#!/usr/bin/env python
"""Utilize crypto domains DNS and either redirect, or display information."""
from bottle import Bottle, run, response, request
2020-01-03 15:00:44 -05:00
import json
import requests
import json2html
2020-01-03 15:00:44 -05:00
VERSION = '0.0.1'
app = Bottle()
2020-01-03 15:00:44 -05:00
2020-01-06 10:26:24 -05:00
def domainLookup(domain):
apibase = 'https://unstoppabledomains.com/api/v1/'
dnslookup = requests.get(apibase + domain)
domainJSON = json.loads(dnslookup.content)
print(domainJSON)
return domainJSON
2020-01-03 15:00:44 -05:00
@app.route("/")
def root():
host = request.get_header('host')
helptext = f"""
<p>General format is {host}/&lt;domain&gt;/&lt;action&gt;
<p>If &lt;action&gt; is blank, it will attempt to use the IPFS hash
2020-01-06 08:48:23 -05:00
<p>You may also/optionally specify an action which can be any of the
following
<table style="margin-left: 50px;border=1px;">
<tr>
<td>raw</td>
<td>Show the raw json (formatted as an html table)</td>
</tr>
2020-01-04 23:29:30 -05:00
<tr>
<td>html</td>
<td>Hit the IPFS hash via cloudflare-ipfs.com</td>
</tr>
2020-01-04 23:29:30 -05:00
<tr>
<td>redir</td>
2020-01-06 08:48:23 -05:00
<td>Use the redirect parameter and just return a 302 redirect to whatever
is set</td>
</tr>
</table>
"""
return helptext
2020-01-03 15:00:44 -05:00
@app.route("/<domain>")
2020-01-06 10:26:24 -05:00
@app.route("/<domain>/")
@app.route("/<domain>/<action>")
def redirectDomain(domain, action=None):
2020-01-06 10:26:24 -05:00
lookupResult = domainLookup(domain)
redirect_url = lookupResult['ipfs']['redirect_domain']
html = lookupResult['ipfs']['html']
if action == 'redir':
try:
2020-01-06 10:26:24 -05:00
if not redirect_url.startswith('http'):
redirect_url = "http://" + redirect_url
response.status = 302
response.set_header('Location', redirect_url)
except KeyError:
return f'Did not find a redirect for {domain}'
elif action is None or action == 'html':
2020-01-06 10:26:24 -05:00
response.status = 302
if not html.startswith('/ip'):
ipfshash = "ipfs/" + html
else:
ipfshash = html
response.set_header('Location',
f"https://cloudflare-ipfs.com/{ipfshash}")
elif action == 'raw':
2020-01-06 10:26:24 -05:00
return json2html.json2html.convert(json=lookupResult)
2020-01-06 08:48:23 -05:00
2020-01-03 15:00:44 -05:00
if __name__ == "__main__":
2020-01-03 20:44:30 -05:00
run(app, host='0.0.0.0', port='5000', reloader=True)