Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F29662397
raw.txt
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
faidon
Jun 28 2019, 2:54 PM
2019-06-28 14:54:24 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
raw.txt
View Options
#!/usr/bin/env python3
import re
import pytricia
import urllib.request
import random
import timeit
anafile = "ana-invalids.txt"
anaurl = "https://as286.net/data/ana-invalids.txt"
def parse(iterable):
pattern = "^(?P<prefix>[^;]+);srcAS=(?P<asn>[^;]+);altpfx=(?P<altpfx>[^;]+);.*"
prefixes = pytricia.PyTricia(128)
for line in iterable:
match = re.search(pattern, line)
if not match:
continue
prefix = match.group("prefix")
if match.group("altpfx") == "NONE":
altpfx = False
else:
altpfx = True
prefixes[prefix] = altpfx
return prefixes
def read_and_parse_file(filename):
with open(filename, "r") as f:
return parse(f)
def read_and_parse_url(url):
response = urllib.request.urlopen(url)
content = response.read().decode("utf-8")
return parse(content.splitlines())
prefixes = read_and_parse_file(anafile)
# prefixes = read_and_parse_url(anaurl)
def lookup(ip):
try:
altpfx = prefixes[ip]
if altpfx:
result = "invalid-but-reachable"
else:
result = "unreachable"
except KeyError:
result = "valid-or-unverified"
return result
# test some lookups that we
def test_lookup():
for ip in (
"2.59.118.1", # unreachable
"2.176.52.1", # invalid-but-reachable
"1.1.1.1", # valid-or-unverified
"2a0d:5643::1", # unreachable
"2a0d:5084::1", # invalid-but-reachable
"2606:4700:4700::1111", # valid-or-unverified
):
try:
print(ip, lookup(ip))
except KeyError:
# invalid prefix?
pass
def random_ip(n):
for i in range(n):
random.randint(0, 255)
random_ip4 = ".".join([str(random.randint(0, 255)) for j in range(0, 4)])
yield random_ip4
def test_random():
for ip in random_ip(1000):
lookup(ip)
if __name__ == "__main__":
test_lookup()
n = 10
v = timeit.timeit(
"test_random()", setup="from __main__ import test_random", number=n
)
print("Execution time per run: ", v / n)
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
7686006
Default Alt Text
raw.txt (2 KB)
Attached To
Mode
P8683 RPKI check for invalids
Attached
Detach File
Event Timeline
Log In to Comment