1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-12-26 01:09:24 +08:00

add neonetwork.json

This commit is contained in:
JerryXiao 2020-05-02 15:02:14 +08:00
parent 9504e56655
commit cf653edb6d
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA
5 changed files with 109 additions and 45 deletions

View file

@ -32,6 +32,7 @@ jobs:
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -4 -o roa_dir/roa4_bird2.conf python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -4 -o roa_dir/roa4_bird2.conf
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -6 -o roa_dir/roa6_bird2.conf python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -6 -o roa_dir/roa6_bird2.conf
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -j -o roa_dir/roa46.json python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -j -o roa_dir/roa46.json
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -e -o roa_dir/neonetwork.json
- name: Upload files - name: Upload files
env: env:

View file

@ -33,3 +33,4 @@ jobs:
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -4 -o roa_dir/roa4_bird2.conf python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -4 -o roa_dir/roa4_bird2.conf
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -6 -o roa_dir/roa6_bird2.conf python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -6 -o roa_dir/roa6_bird2.conf
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -j -o roa_dir/roa46.json python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -j -o roa_dir/roa46.json
python3 scripts/roa.py -m ${maxlen4} -M ${maxlen6} -e -o roa_dir/neonetwork.json

View file

@ -1,3 +1,3 @@
NAME="LibreHouse" NAME="LibreHouse"
OWNER="Out Vi" OWNER="LibreHouse"
DESC="" DESC=""

View file

@ -5,47 +5,90 @@ from pathlib import Path
from ipaddress import IPv4Network, IPv6Network from ipaddress import IPv4Network, IPv6Network
from itertools import combinations from itertools import combinations
def keyVal(line):
l = line.split('=') class BashParser:
assert l[0].strip() def __init__(self):
if len(l) == 1: self.__pa = None # are we parsing bash array?
l.append('') def parseline(self, line):
repl_quotes = lambda t: t.replace('"', '').replace('\'', '') repl_quotes = lambda t: t.replace('"', '').replace('\'', '')
return [l[0].strip(), '='.join([repl_quotes(i).strip() for i in l[1:]])] line = line.strip()
if '=(' in line:
cwd = Path() self.__pa = (repl_quotes(line).split('=(')[0], list())
assert not [d for d in ("asn", "route", "route6", "node") if not (cwd / d).is_dir()] return None
if self.__pa:
def str2asn(s_asn): if line:
s_asn = s_asn.lower() if line.endswith(')'):
if s_asn.startswith('as'): if line[:-1]:
s_asn = s_asn[2:] self.__pa[1].append(repl_quotes(line[:-1]))
return int(s_asn) ret = self.__pa
self.__pa = None
def get_asns(): return ret
asns = list() else:
for f in (cwd / "asn").iterdir(): self.__pa[1].append(repl_quotes(line))
try: return None
if not f.is_file(): else:
continue if not line or line.startswith('#'):
assert f.name.lower().startswith('as') return None
asns.append(int(f.name[2:])) l = line.split('=')
except Exception: assert len(l) >= 2
print("[!] Error while processing file", f) return [l[0], '='.join([repl_quotes(i) for i in l[1:]])]
raise bp = BashParser()
return asns
ASNS = get_asns()
def shell2dict(shellscript): def shell2dict(shellscript):
fc = dict() fc = dict()
for line in shellscript.split('\n'): for line in shellscript.split('\n'):
l = line.strip() r = bp.parseline(line)
if not l or l.startswith('#'): if r:
continue key, val = r
key, val = keyVal(l) fc[key.lower()] = val
fc[key.lower()] = val.lower()
return fc return fc
cwd = Path()
assert not [d for d in ("asn", "route", "route6", "node", "people") if not (cwd / d).is_dir()]
def str2asn(s_asn):
s_asn = s_asn.strip().lower()
if s_asn.startswith('as'):
s_asn = s_asn[2:]
return int(s_asn)
def neoneo_get_people():
people = dict()
for f in (cwd / "people").iterdir():
try:
if not f.is_file():
continue
fc = shell2dict(f.read_text())
present_keys = ('name', 'desc', 'contact', 'babel')
assert f.name
people[f.name] = {k: fc.get(k) for k in present_keys}
for v in people[f.name].values():
assert v is not None
except Exception:
print("[!] Error while processing file", f)
raise
return people
PEOPLE = neoneo_get_people()
def neonet_get_asns():
asns = dict()
for f in (cwd / "asn").iterdir():
try:
if not f.is_file():
continue
fc = shell2dict(f.read_text())
present_keys = ('name', 'owner', 'desc')
asns[str2asn(f.name)] = {k: fc.get(k) for k in present_keys}
assert fc.get('owner') in PEOPLE
for v in asns[str2asn(f.name)].values():
assert v is not None
except Exception:
print("[!] Error while processing file", f)
raise
return asns
ASNS = neonet_get_asns()
def node2asn(): def node2asn():
node_table = dict() node_table = dict()
for f in (cwd / "node").iterdir(): for f in (cwd / "node").iterdir():
@ -54,14 +97,14 @@ def node2asn():
continue continue
fc = shell2dict(f.read_text()) fc = shell2dict(f.read_text())
asn = str2asn(fc.get('asn')) asn = str2asn(fc.get('asn'))
node_table[f.name.lower()] = asn node_table[f.name] = asn
except Exception: except Exception:
print("[!] Error while processing file", f) print("[!] Error while processing file", f)
raise raise
return node_table return node_table
NODE_TABLE = node2asn() NODE_TABLE = node2asn()
def route2roa(dirname, is_ipv6=False): def neonet_route2roa(dirname, is_ipv6=False):
roa_entries = list() roa_entries = list()
for f in (cwd / dirname).iterdir(): for f in (cwd / dirname).iterdir():
try: try:
@ -71,13 +114,13 @@ def route2roa(dirname, is_ipv6=False):
nettype = IPv6Network if is_ipv6 else IPv4Network nettype = IPv6Network if is_ipv6 else IPv4Network
get_supernet = lambda s_net: None if not s_net else nettype(s_net, strict=True) get_supernet = lambda s_net: None if not s_net else nettype(s_net, strict=True)
roa_entries_key = ("asn", "prefix", "supernet") roa_entries_key = ("asn", "prefix", "supernet")
if fc.get('type') in ('lo', 'subnet'): if fc.get('type').lower() in ('lo', 'subnet'):
asn = str2asn(fc.get('as')) asn = str2asn(fc.get('as'))
assert asn in ASNS assert asn in ASNS
route = f.name.replace(',', '/') route = f.name.replace(',', '/')
supernet = get_supernet(fc.get('supernet')) supernet = get_supernet(fc.get('supernet'))
roa_entries.append(dict(zip(roa_entries_key, [asn, nettype(route, strict=True), supernet]))) roa_entries.append(dict(zip(roa_entries_key, [asn, nettype(route, strict=True), supernet])))
elif fc.get('type').startswith('tun'): elif fc.get('type').lower().startswith('tun'):
assert NODE_TABLE[fc.get('downstream')] # extra check for downstream assert NODE_TABLE[fc.get('downstream')] # extra check for downstream
asn = NODE_TABLE[fc.get('upstream')] asn = NODE_TABLE[fc.get('upstream')]
assert asn in ASNS assert asn in ASNS
@ -85,7 +128,7 @@ def route2roa(dirname, is_ipv6=False):
supernet = get_supernet(fc.get('supernet')) supernet = get_supernet(fc.get('supernet'))
roa_entries.append(dict(zip(roa_entries_key, [asn, nettype(route, strict=True), supernet]))) roa_entries.append(dict(zip(roa_entries_key, [asn, nettype(route, strict=True), supernet])))
else: else:
assert fc.get('type') in ('ptp',) assert fc.get('type').lower() in ('ptp',)
except Exception: except Exception:
print("[!] Error while processing file", f) print("[!] Error while processing file", f)
raise raise
@ -111,18 +154,19 @@ if __name__ == "__main__":
parser.add_argument('-o', '--output', default='', help='write output to file') parser.add_argument('-o', '--output', default='', help='write output to file')
parser.add_argument('-4', '--ipv4', action='store_true', help='print ipv4 only') parser.add_argument('-4', '--ipv4', action='store_true', help='print ipv4 only')
parser.add_argument('-6', '--ipv6', action='store_true', help='print ipv6 only') parser.add_argument('-6', '--ipv6', action='store_true', help='print ipv6 only')
parser.add_argument('-e', '--export', action='store_true', help='export registry to json')
args = parser.parse_args() args = parser.parse_args()
if args.max < 0 or args.max6 < 0 or args.max > IPv4Network(0).max_prefixlen or args.max6 > IPv6Network(0).max_prefixlen: if args.max < 0 or args.max6 < 0 or args.max > IPv4Network(0).max_prefixlen or args.max6 > IPv6Network(0).max_prefixlen:
parser.error('check your max prefix length') parser.error('check your max prefix length')
roa4 = roa6 = list() roa4 = roa6 = list()
if args.ipv4: if args.ipv4:
roa4 = route2roa('route') roa4 = neonet_route2roa('route')
elif args.ipv6: elif args.ipv6:
roa6 = route2roa('route6', True) roa6 = neonet_route2roa('route6', True)
else: else:
roa4 = route2roa('route') roa4 = neonet_route2roa('route')
roa6 = route2roa('route6', True) roa6 = neonet_route2roa('route6', True)
roa4 = [r for r in roa4 if r['prefix'].prefixlen <= args.max or r['prefix'].prefixlen == IPv4Network(0).max_prefixlen] roa4 = [r for r in roa4 if r['prefix'].prefixlen <= args.max or r['prefix'].prefixlen == IPv4Network(0).max_prefixlen]
roa6 = [r for r in roa6 if r['prefix'].prefixlen <= args.max6] roa6 = [r for r in roa6 if r['prefix'].prefixlen <= args.max6]
@ -140,7 +184,25 @@ if __name__ == "__main__":
output = "" output = ""
VALID_KEYS = ('asn', 'prefix', 'maxLength') VALID_KEYS = ('asn', 'prefix', 'maxLength')
if args.json: if args.export:
import json, time
current = int(time.time())
# people has [asns], asn has [route]
d_output = {"metadata": {"generated": current, "valid": current+14*86400}, "people": dict()}
for asn, asi in ASNS.items():
as_route4 = list()
as_route6 = list()
for r in roa4:
if r['asn'] == asn:
as_route4.append({k:v for k, v in r.items() if k in VALID_KEYS})
for r in roa6:
if r['asn'] == asn:
as_route6.append({k:v for k, v in r.items() if k in VALID_KEYS})
owner = asi['owner']
peopledict = d_output['people'].setdefault(owner, {"info": PEOPLE[owner], "asns": list()})
peopledict['asns'].append({"asn": asn, "routes": {'ipv4': as_route4, 'ipv6': as_route6}})
output = json.dumps(d_output, indent=2)
elif args.json:
import json, time import json, time
current = int(time.time()) current = int(time.time())
d_output = {"metadata": {"counts": len(roa4)+len(roa6), "generated": current, "valid": current+14*86400}, "roas": list()} d_output = {"metadata": {"counts": len(roa4)+len(roa6), "generated": current, "valid": current+14*86400}, "roas": list()}