-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtc.py
executable file
·211 lines (191 loc) · 6.05 KB
/
rtc.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
import argparse
import ipaddress
import json
import logging
import pathlib
import resource
import sys
import pyasn
import asnames
from revtr import RevTrApi, RevTrMeasurement
DEFAULT_APIKEY_FILE = pathlib.Path("~/.config/revtr.apikey").expanduser()
PYASN_DATA = pathlib.Path("./resources/pyasn_20230625.dat").absolute()
ASNAMES_DATA = pathlib.Path("./resources/autnums.html").absolute()
def create_parser():
desc = """Reverse Traceroute Client"""
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
"--apikey",
dest="apikey",
action="store",
metavar="APIKEY",
type=str,
required=False,
default=None,
help="Reverse Traceroute API key [read from ~/.config/revtr.apikey]",
)
sc = parser.add_subparsers(title="command", dest="command", required=True)
_parser_sources = sc.add_parser("sources", help="List available sources")
parser_atlas = sc.add_parser("atlas", help="Manage forward traceroute atlas")
parser_launch = sc.add_parser("launch", help="Execute a reverse traceroute")
parser_batch = sc.add_parser("batch", help="Execute a batch of reverse traceroutes")
parser_fetch = sc.add_parser("fetch", help="Fetch reverse traceroutes")
parser_print = sc.add_parser("print", help="Print reverse traceroutes from file")
parser_atlas.add_argument(
"--vp",
dest="vp",
action="store",
metavar="IP",
type=ipaddress.IPv4Address,
required=True,
help="Reverse Traceroute vantage point",
)
g = parser_atlas.add_mutually_exclusive_group(required=True)
g.add_argument(
"--reset",
dest="atlas_operation",
action="store_const",
const="reset",
help="Remove all paths from current forward traceroute atlas",
)
g.add_argument(
"--rebuild",
dest="atlas_operation",
action="store_const",
const="rebuild",
help="Rebuild forward traceroute atlas",
)
parser_launch.add_argument(
"--remote",
dest="remote",
action="store",
metavar="IP",
type=ipaddress.IPv4Address,
required=True,
help="Reverse traceroute source (remote endpoint)",
)
parser_launch.add_argument(
"--vp",
dest="vp",
action="store",
metavar="IP",
type=ipaddress.IPv4Address,
required=True,
help="Reverse traceroute destination (controlled vantage point)",
)
parser_launch.add_argument(
"--label",
dest="label",
action="store",
metavar="STRING",
type=str,
required=True,
help="Label to store results under",
)
parser_batch.add_argument(
"--label",
dest="label",
action="store",
metavar="STRING",
type=str,
required=True,
help="Label to store results under",
)
parser_batch.add_argument(
"--file",
dest="file",
action="store",
metavar="PATH",
type=pathlib.Path,
required=True,
help="File containing a VP and remote per line (space-separated)",
)
parser_fetch.add_argument(
"--label",
dest="label",
action="store",
metavar="STRING",
type=str,
required=True,
help="Label to fetch",
)
parser_fetch.add_argument(
"--print",
dest="print",
action="store_true",
required=False,
default=False,
help="Print reverse traceroute on screen",
)
parser_fetch.add_argument(
"--file",
dest="file",
action="store",
metavar="PATH",
type=pathlib.Path,
required=False,
help="JSON file to store the measurements [None]",
default=None,
)
parser_print.add_argument(
"--file",
dest="file",
action="store",
metavar="JSONL",
type=pathlib.Path,
required=True,
help="File containing reverse traceroute JSON (one per line)",
)
return parser
def main():
# resource.setrlimit(resource.RLIMIT_AS, (1 << 29, 1 << 29))
# resource.setrlimit(resource.RLIMIT_FSIZE, (1 << 32, 1 << 32))
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
parser = create_parser()
opts = parser.parse_args()
if opts.apikey is None:
with open(DEFAULT_APIKEY_FILE, encoding="utf8") as fd:
opts.apikey = fd.read().strip()
api = RevTrApi(opts.apikey)
if opts.command == "sources":
r = api.sources()
elif opts.command == "atlas":
if opts.atlas_operation == "reset":
r = api.atlas_reset(opts.vp)
elif opts.atlas_operation == "rebuild":
r = api.atlas_rebuild(opts.vp)
else:
raise RuntimeError("Unreachable")
elif opts.command == "launch":
r = api.launch(opts.vp, opts.remote, opts.label)
elif opts.command == "batch":
with open(opts.file, encoding="utf8") as fd:
pairs = [tuple(line.split()) for line in fd]
r = api.batch(pairs, opts.label)
elif opts.command == "fetch":
r = api.fetch(opts.label)
if opts.file:
with open(opts.file, "w", encoding="utf8") as fd:
json.dump(r, fd)
if opts.print:
ip2asn = pyasn.pyasn(str(PYASN_DATA))
namedb = asnames.ASNamesDB(ASNAMES_DATA)
for revtr in r["revtrs"]:
measurement = RevTrMeasurement(revtr, ip2asn, namedb)
print(measurement)
r = ""
elif opts.command == "print":
with open(opts.file, encoding="utf8") as fd:
ip2asn = pyasn.pyasn(str(PYASN_DATA))
namedb = asnames.ASNamesDB(ASNAMES_DATA)
for line in fd:
revtr = json.loads(line.strip())
measurement = RevTrMeasurement(revtr, ip2asn, namedb)
print(measurement)
r = ""
else:
raise RuntimeError("Unreachable")
print(json.dumps(r))
if __name__ == "__main__":
sys.exit(main())