2020-01-03 15:00:44 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Utilize crypto domains DNS and either redirect, or display information."""
|
2020-01-06 11:20:07 -05:00
|
|
|
from bottle import Bottle, run, response, request, static_file
|
2020-01-03 15:00:44 -05:00
|
|
|
import json
|
|
|
|
import requests
|
2020-01-03 17:02:10 -05:00
|
|
|
import json2html
|
2020-01-03 15:00:44 -05:00
|
|
|
|
2020-01-06 11:20:07 -05:00
|
|
|
VERSION = '0.1.0'
|
2020-01-03 15:00:44 -05:00
|
|
|
|
2020-01-03 16:17:43 -05:00
|
|
|
app = Bottle()
|
2020-01-03 15:00:44 -05:00
|
|
|
|
|
|
|
|
2020-01-06 10:26:24 -05:00
|
|
|
def domainLookup(domain):
|
2020-01-06 11:20:07 -05:00
|
|
|
"""Return a dictionary from JSON results of API call
|
|
|
|
or return False if there is an error
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
apibase = 'https://unstoppabledomains.com/api/v1/'
|
|
|
|
dnslookup = requests.get(apibase + domain)
|
|
|
|
if dnslookup.status_code == 200:
|
|
|
|
domainInfo = json.loads(dnslookup.content)
|
|
|
|
return domainInfo
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
return False
|
2020-01-06 10:26:24 -05:00
|
|
|
|
|
|
|
|
2020-01-03 15:00:44 -05:00
|
|
|
@app.route("/")
|
|
|
|
def root():
|
2020-01-06 11:20:07 -05:00
|
|
|
"""Index page, just displays information about URL formatting"""
|
2020-01-03 16:17:43 -05:00
|
|
|
host = request.get_header('host')
|
2020-01-03 17:02:10 -05:00
|
|
|
helptext = f"""
|
|
|
|
<p>General format is {host}/<domain>/<action>
|
2020-01-05 22:31:02 -05:00
|
|
|
<p>If <action> 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;">
|
2020-01-03 17:02:10 -05:00
|
|
|
<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>
|
2020-01-03 17:02:10 -05:00
|
|
|
<td>html</td>
|
|
|
|
<td>Hit the IPFS hash via cloudflare-ipfs.com</td>
|
|
|
|
</tr>
|
2020-01-04 23:29:30 -05:00
|
|
|
<tr>
|
2020-01-03 17:02:10 -05:00
|
|
|
<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>
|
2020-01-03 17:02:10 -05:00
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
"""
|
|
|
|
return helptext
|
2020-01-03 15:00:44 -05:00
|
|
|
|
|
|
|
|
2020-01-03 16:17:43 -05:00
|
|
|
@app.route("/<domain>")
|
2020-01-06 10:26:24 -05:00
|
|
|
@app.route("/<domain>/")
|
2020-01-03 17:02:10 -05:00
|
|
|
@app.route("/<domain>/<action>")
|
|
|
|
def redirectDomain(domain, action=None):
|
2020-01-06 11:20:07 -05:00
|
|
|
"""Handle the actual redirect for the domain queried"""
|
2020-01-06 10:26:24 -05:00
|
|
|
lookupResult = domainLookup(domain)
|
2020-01-06 11:20:07 -05:00
|
|
|
if lookupResult:
|
|
|
|
redirect_url = lookupResult['ipfs']['redirect_domain']
|
|
|
|
ipfs_hash = lookupResult['ipfs']['html']
|
|
|
|
else:
|
|
|
|
return f"Unable to get info for {domain}"
|
|
|
|
if action is None or action == 'html':
|
|
|
|
response.status = 302
|
|
|
|
if not ipfs_hash.startswith('/ip'):
|
|
|
|
ipfshash = "ipfs/" + ipfs_hash
|
|
|
|
else:
|
|
|
|
ipfshash = ipfs_hash
|
|
|
|
response.set_header('Location',
|
|
|
|
f"https://cloudflare-ipfs.com/{ipfshash}")
|
|
|
|
elif action == 'redir':
|
2020-01-03 17:02:10 -05:00
|
|
|
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)
|
2020-01-03 17:02:10 -05:00
|
|
|
except KeyError:
|
|
|
|
return f'Did not find a redirect for {domain}'
|
|
|
|
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
|
|
|
|
2020-01-06 11:20:07 -05:00
|
|
|
@app.route("/favicon.ico")
|
|
|
|
def favicon():
|
|
|
|
"""Display a favicon to make the errors go away :)"""
|
|
|
|
return static_file('favicon.ico', root='images')
|
|
|
|
|
|
|
|
|
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)
|