-
Notifications
You must be signed in to change notification settings - Fork 21
/
netblocks
executable file
·164 lines (117 loc) · 4.54 KB
/
netblocks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
from argparse import ArgumentParser
from collections import defaultdict
from ipaddress import ip_address, ip_network, summarize_address_range
import json
import time
from filereader import get_communities_data
class Prefix:
def __init__(self, net, community='', visible=True):
self.net = net
self.children = set()
self.community = community
self.visible = visible
PREFIX = ip_network('10.0.0.0/8')
def add_net(nets, net, community):
try:
net = ip_network(net)
except ValueError:
return
if PREFIX.overlaps(net):
nets[net.prefixlen].append(Prefix(net, community))
def get_nets(srcdir):
nets = defaultdict(list)
for community, data in get_communities_data(srcdir, []):
if 'networks' in data:
if 'ipv4' in data['networks']:
for net in data['networks']['ipv4']:
add_net(nets, net, community)
if 'delegate' in data:
for d in data['delegate']:
for net in data['delegate'][d]:
add_net(nets, net, community)
return nets
def insert(net, tree):
for candidate in tree.children:
if candidate.net == net.net:
return
if candidate.net.overlaps(net.net):
insert(net, candidate)
return
tree.children.add(net)
def build_prefixtree(nets):
tree = Prefix(PREFIX, 'root')
# iterate over prefixes - biggest first
for k, v in nets.items():
for net in v:
insert(net, tree)
return tree
def insert_empty_nets(tree):
children = set()
old_children = sorted(tree.children, key=lambda x: int(x.net.network_address))
prev = None
for child in old_children:
insert_empty_nets(child)
children.add(child)
if prev is not None:
start = int(prev.net.broadcast_address) + 1
end = int(child.net.network_address) - 1
if end > start:
internets = summarize_address_range(ip_address(start), ip_address(end))
for internet in internets:
children.add(Prefix(internet, 'free', False))
prev = child
tree.children = children
return tree
def insert_json(tree):
tmp = {'prefix': tree.net.compressed,
'size': tree.net.num_addresses,
'children': list()}
for child in tree.children:
tmp['children'].append(insert_json(child))
tmp['children'] = sorted(tmp['children'], key=lambda x: int(ip_network(x['prefix']).network_address))
if not tree.visible:
tmp['display'] = 'none'
return tmp
def get_inetnum(net, status='assigned'):
inetnum = {'admin-c': list(),
'status': list(),
'inetnum': list(),
'netname': list()}
inetnum['admin-c'].append(net.community)
inetnum['status'].append(status)
inetnum['inetnum'].append("{} - {}".format(net.net.network_address, net.net.broadcast_address))
inetnum['netname'].append(net.community)
return inetnum
def build_inetnums(nets):
inetnums = dict()
for k, v in nets.items():
for net in v:
inetnums[net.net.compressed] = get_inetnum(net)
inetnums[PREFIX.compressed] = get_inetnum(Prefix(PREFIX, 'Freifunk'), 'ask')
return inetnums
def get_prefix_count(nets):
return sum([len(prefixes) for prefixes in nets.values()])
def generate(srcdir, destdir):
nets = get_nets(srcdir)
prefix_tree = build_prefixtree(nets)
json_tree = insert_json(insert_empty_nets(prefix_tree))
json_tree['prefixes'] = get_prefix_count(nets)
json_tree['origin'] = 'icvpn-meta'
json_tree['date'] = time.time()
with open(destdir + '/registry-prefixes.json', 'w') as outfile:
json.dump(json_tree, outfile)
with open(destdir + '/registry-inetnums.json', 'w') as outfile:
json.dump(build_inetnums(nets), outfile)
if __name__ == '__main__':
parser = ArgumentParser(description='Populates the JSON-Feeds for the Netblocks Visulization')
parser.add_argument('-s', '--sourcedir', dest='src',
help='Use files in DIR as input files. Default: ../icvpn-meta/',
metavar='DIR',
default='../icvpn-meta/')
parser.add_argument('-d', '--destdir', dest='dest',
help='Use DIR as destination for the generated files. Default: ./netblocks-data',
metavar='DIR',
default='./netblocks-data')
args = parser.parse_args()
generate(args.src, args.dest)