|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import json |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +from generator import download, get_abspath_list_file, get_version, write_to_file |
| 8 | + |
| 9 | +# Source: https://ipfs.github.io/public-gateway-checker/ is a JavaScript |
| 10 | +# single-page app that renders its gateway list client-side from these two |
| 11 | +# JSON data files in the same GitHub repository. |
| 12 | +GATEWAYS_URL = "https://raw.githubusercontent.com/ipfs/public-gateway-checker/main/gateways.json" |
| 13 | +ONION_GATEWAYS_URL = "https://raw.githubusercontent.com/ipfs/public-gateway-checker/main/onion-gateways.json" |
| 14 | + |
| 15 | + |
| 16 | +def to_hostname(entry): |
| 17 | + # Entries are URL templates, historically like "https://ipfs.io/ipfs/:hash" |
| 18 | + # and currently bare origins like "https://ipfs.filebase.io". Parsing with |
| 19 | + # urlparse and taking .hostname handles scheme, port, path/placeholder, |
| 20 | + # and mixed case uniformly, whichever shape upstream currently uses. |
| 21 | + entry = entry.strip() |
| 22 | + if not entry: |
| 23 | + return None |
| 24 | + if "://" not in entry: |
| 25 | + entry = "https://" + entry |
| 26 | + hostname = urlparse(entry).hostname |
| 27 | + if not hostname: |
| 28 | + return None |
| 29 | + return hostname.rstrip('.').lower() |
| 30 | + |
| 31 | + |
| 32 | +if __name__ == '__main__': |
| 33 | + gateways = json.loads(download(GATEWAYS_URL).text) |
| 34 | + onion_gateways = json.loads(download(ONION_GATEWAYS_URL).text) |
| 35 | + |
| 36 | + hostnames = set() |
| 37 | + for entry in gateways + onion_gateways: |
| 38 | + hostname = to_hostname(entry) |
| 39 | + if hostname: |
| 40 | + hostnames.add(hostname) |
| 41 | + |
| 42 | + # Union with the entries already committed, never replace them. Upstream |
| 43 | + # tracks gateways that are *currently reachable*, so it drops hosts that |
| 44 | + # are merely down or retired -- including long-lived ones such as ipfs.io |
| 45 | + # and dweb.link. For a warninglist that exists to suppress false positives, |
| 46 | + # a gateway that operated in the past is still worth recognising, so the |
| 47 | + # generator only ever adds. |
| 48 | + try: |
| 49 | + with open(get_abspath_list_file("public-ipfs-gateways")) as existing_file: |
| 50 | + hostnames.update(json.load(existing_file)["list"]) |
| 51 | + except (IOError, OSError, ValueError, KeyError): |
| 52 | + pass |
| 53 | + |
| 54 | + warninglist = { |
| 55 | + 'name': "List of known public IPFS gateways", |
| 56 | + 'version': get_version(), |
| 57 | + 'description': "Event contains one or more entries of known public IPFS gateways", |
| 58 | + 'type': "string", |
| 59 | + 'list': sorted(hostnames), |
| 60 | + 'matching_attributes': ["domain", "hostname", "domain|ip", "url", "uri"] |
| 61 | + } |
| 62 | + |
| 63 | + write_to_file(warninglist, "public-ipfs-gateways") |
0 commit comments