Skip to content

Commit 7a09ae8

Browse files
authored
Merge pull request #381 from elhoim/warninglists/add-ipfs-generator
add: [tools] generator for public-ipfs-gateways
2 parents 9ae5422 + b5558f5 commit 7a09ae8

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

generate_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ python3 generate_phone_numbers.py
2828
#python3 generate-stackpath.py # source https://k3t9x2h3.map2.ssl.hwcdn.net/ipblocks.txt is dead (NXDOMAIN); StackPath wound down its CDN and hwcdn.net is now a parked domain-for-sale page
2929
python3 generate-tlds.py
3030
python3 generate-github.py
31+
python3 generate-public-ipfs-gateways.py
3132
python3 generate_tranco.py
3233
python3 generate-university-domain-list.py
3334
# python3 generate-windows-binary-hashes.py # ON HOLD DUE TO https://github.com/m417z/winbindex/commit/24dd6995fd1c8eacbf59b5ce658d34ccf00bae00
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)