From a1d67cedb89deeef1a7e18686e4459a15879f0ba Mon Sep 17 00:00:00 2001 From: Alex Kelly Date: Mon, 6 Jan 2020 11:20:07 -0500 Subject: [PATCH] Added favicon, shifted if logic to handle default first --- chainlink.py | 55 ++++++++++++++++++++++++++++++--------------- images/favicon.ico | Bin 0 -> 4286 bytes 2 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 images/favicon.ico diff --git a/chainlink.py b/chainlink.py index ebd5f33..ffe82e5 100755 --- a/chainlink.py +++ b/chainlink.py @@ -1,25 +1,34 @@ #!/usr/bin/env python """Utilize crypto domains DNS and either redirect, or display information.""" -from bottle import Bottle, run, response, request +from bottle import Bottle, run, response, request, static_file import json import requests import json2html -VERSION = '0.0.1' +VERSION = '0.1.0' app = Bottle() def domainLookup(domain): - apibase = 'https://unstoppabledomains.com/api/v1/' - dnslookup = requests.get(apibase + domain) - domainJSON = json.loads(dnslookup.content) - print(domainJSON) - return domainJSON + """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 @app.route("/") def root(): + """Index page, just displays information about URL formatting""" host = request.get_header('host') helptext = f"""

General format is {host}/<domain>/<action> @@ -49,10 +58,22 @@ def root(): @app.route("//") @app.route("//") def redirectDomain(domain, action=None): + """Handle the actual redirect for the domain queried""" lookupResult = domainLookup(domain) - redirect_url = lookupResult['ipfs']['redirect_domain'] - html = lookupResult['ipfs']['html'] - if action == 'redir': + 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': try: if not redirect_url.startswith('http'): redirect_url = "http://" + redirect_url @@ -60,17 +81,15 @@ def redirectDomain(domain, action=None): response.set_header('Location', redirect_url) except KeyError: return f'Did not find a redirect for {domain}' - elif action is None or action == 'html': - 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': return json2html.json2html.convert(json=lookupResult) +@app.route("/favicon.ico") +def favicon(): + """Display a favicon to make the errors go away :)""" + return static_file('favicon.ico', root='images') + + if __name__ == "__main__": run(app, host='0.0.0.0', port='5000', reloader=True) diff --git a/images/favicon.ico b/images/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..f62f4e6a2f940b0692f606cde4d0e294c6a104f5 GIT binary patch literal 4286 zcmeHL-%Aux7`;A-4%9ZFiboH=LPXT3BZ9-n;jl`M&RbXYLY` zNAgH%DItWElCdH}P7^{d=p#Zd5rSDvl3`yyE5NsRapzCDjh^ZV!MPXk}s~ki_1b3d!x^4w)DY$bEh<$>-Hz+rASV)?z+k-1E+8;KyvRrinA< zlwkDSLg;Gb{i9CDIK)+@5cvlF-gH>b25VZ(F`m<|G&#kAtMnpj4fu%>zvLO}Z0@cz z95_?TQRl#a^H7zXDP`vSH^ycT8w1U|$JGT_l>%rC_+LasE``1K4LqxBDhKFdC0gHS zweD}eTZuLdU@vg^FcFjsUA6vfzhCI8?fx9^*4KmkaeI_tv>5sZzRuzC?LRnhRXMR3 z^4^)(JgtH$XQ5B5Mefiiv+>Pk4!YhPY}?PoVJYm@&ta zDu<2!X1Uti7|(U@T-TX7AnrS(0r`5kL$3E-*ZY(nhiN?*Pk4alV{bcs6C6;JhAxbF zV2t}t#^I0y;$XLLLKn~`G=@4ytpQ`Lw^5|!VmhRAvf^roZ=y#Drj(;sbEfFE#u?4G z;^3xluJ$$xi(E_!MoZlCO~gF4Cea_DRn()KzPTK!6R62U577**rTPVG*sf<73*S6H zr+Hcz!(Pp(kxU%^`)1TN-!yW7t6<6*w|z4WU%iZHO}!4?^37EIna=8KAD?#V_%8Zp ziBPLf&U9A$wud|P=RD{T=d4t2c6`p}o9Ii1hvBS)vz{H8<~=g`=5K8O0nU2n-`P3N a=$kR0z*)~utZaK(`KEb&+Mfac;PndxNNgMc literal 0 HcmV?d00001