86 lines
2.4 KiB
Python
86 lines
2.4 KiB
Python
import argparse
|
|
from ctypes.wintypes import INT
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-f', help='Writes visualization PNG to this path. Defaults to \"nets.png\".', default='nets.png', metavar='PATH')
|
|
parser.add_argument('-s', help='Smallest visible net, defaults to /32 for IPv4 and /64 for IPv6', default=-1, metavar='CIDR', type=int)
|
|
parser.add_argument(
|
|
'-n', '--networks',
|
|
required=True,
|
|
help='File containing network definitions.')
|
|
|
|
args = parser.parse_args()
|
|
|
|
import re
|
|
import ip
|
|
import imggen
|
|
|
|
def parseLine(line: str):
|
|
def splitC(c):
|
|
r = (c & 0xFF0000) >> 16
|
|
g = (c & 0x00FF00) >> 8
|
|
b = (c & 0x0000FF) >> 0
|
|
return (r, g, b)
|
|
|
|
if re.fullmatch("^color\ *=.*$", line) is not None:
|
|
cstr = line.split("=")[1].strip().lstrip("#x")
|
|
cv = int(cstr, 16)
|
|
return ("col", splitC(cv))
|
|
|
|
if re.fullmatch("^fill\ *=.*$", line) is not None:
|
|
cstr = line.split("=")[1].strip().lstrip("#x")
|
|
cv = int(cstr, 16)
|
|
return ("fil", splitC(cv))
|
|
|
|
if len(line) == 0 or line[0] == "#":
|
|
return ("com", line)
|
|
|
|
if re.fullmatch("^[\.:0-9a-f]*\/[0-9]*$", line) is not None:
|
|
n = ip.net()
|
|
n.parseNet(line)
|
|
return("net", n)
|
|
|
|
return ("nal", 0)
|
|
|
|
with open(args.networks) as f:
|
|
snet = ip.net()
|
|
snet.parseNet(f.readline())
|
|
|
|
smallest = args.s
|
|
if smallest == -1:
|
|
if snet.type == 0:
|
|
smallest = 32
|
|
else:
|
|
smallest = 64
|
|
|
|
if snet.type == 0:
|
|
smallest = 32 - smallest
|
|
else:
|
|
smallest = 128 - smallest
|
|
|
|
if (snet.getSize() - smallest) % 2 != 0:
|
|
raise argparse.ArgumentError(message="The difference between the base net and the smallest net-size must be even!")
|
|
|
|
gen = imggen.imggen(snet.getSize() - smallest)
|
|
|
|
c = (255, 255, 255)
|
|
|
|
for l in f.readlines():
|
|
pl = parseLine(l.strip())
|
|
if pl[0] == "col":
|
|
c = pl[1]
|
|
|
|
if pl[0] == "fil":
|
|
gen.clear(pl[1])
|
|
|
|
if pl[0] == "net":
|
|
s = pl[1].start.toInt(smallest) - snet.start.toInt(smallest)
|
|
e = pl[1].end.toInt(smallest) - snet.start.toInt(smallest)
|
|
gen.colorRange(s, e, c)
|
|
|
|
s = int(1024 / gen.x)
|
|
if (s < 1):
|
|
s = 1
|
|
|
|
gen.saveImage(args.f, s)
|
|
gen.showImage(s) |