forked from pisto/ASkidban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecide.lua
269 lines (243 loc) · 7.42 KB
/
decide.lua
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
--[[
Interactively tag AS numbers.
]]--
local fp, L, ip = require"kblibs.fp", require"kblibs.lambda", require"kblibs.ip"
local map, pick = fp.map, fp.pick
local colors = map.mp(function(name, code) return name, function(str)
return code .. tostring(str) .. "\27[0m"
end end, {
red = "\27[1;31m",
green = "\27[1;32m",
gray = "\27[9;37m",
blue = "\27[1;34m",
yellow = "\27[1;33m",
bold = "\27[1;37m",
lightgray = "\27[5;37m",
cyan = "\27[0;36m",
pink = "\27[1;35m",
orange = "\27[0;33m"
})
local whois_ignore = {
"^[#%%]",
"^[^ ]*abuse",
"^[^ ]*tech",
"^[^ ]*phone",
"^[^ ]*mnt",
"^[^ ]*hdl",
"^[^ ]*source",
"^[^ ]*country",
"^[^ ]*updated",
"^[^ ]*regdate",
"^[^ ]*person",
"^[^ ]*fax",
"^[^ ]*address",
"^[^ ]*handle",
"^[^ ]*stateprov",
"^[^ ]*postal",
"^[^ ]*city",
"^[^ ]*email",
"^[^ ]*role",
"^[^ ]*changed",
"^[^ ]*created",
"^[ \t]*$",
}
local whois_http = {
"https?://(%l[%w%-%.]*%.%l%l+)",
"@(%l[%w%-%.]*%.%l%l+)", --email
"%l[%w%-%.]*%.%l[%w%-%.]*%.%l%l+", -- so.me.thing
}
local whois_http_ignore = {
"arin%.net",
"ripe%.net",
"registro%.br",
"apnic%.net",
"cert%.br",
"nic%.ad%.jp",
"whois%.",
"twnic%.net",
"apjii%.or%.id",
"idnic%.net",
"vnnic%.net%.vn",
"lacnic%.net",
"p%.o%.box",
"afrinic%.net",
"iana%.org",
}
local maybe_kids = {
"vpn",
"hosting",
"vps",
"servers",
"anonym",
}
local maybe_sirs = {
"tele[ck]om",
"dynamic i?p? ?pool",
"broadband",
"landline",
"mobile",
"dsl",
"cable",
}
local function firstmatch(line, regexes)
for _, regex in ipairs(regexes) do if line:match(regex) then return true end end
end
local function prettywhois(whois)
print(colors.yellow("\tWhois:\n"))
local colored, websites, inellipsis = "", {}, false
for line in whois:lower():gmatch("[^\n]+") do
map.tsi(websites, function(_, regex) return line:match(regex) end, whois_http)
if line == "" or firstmatch(line, whois_ignore) or firstmatch(line, whois_http_ignore) then
if not inellipsis then inellipsis, colored = true, colored .. colors.gray("[...]\n") end
else inellipsis, colored = false, colored .. line .. '\n' end
end
for _, regex in ipairs(maybe_kids) do colored = colored:gsub(regex, colors.red) end
for _, regex in ipairs(maybe_sirs) do colored = colored:gsub(regex, colors.green) end
print(colored)
websites = pick.p(function(match) return not firstmatch(match, whois_http_ignore) and not websites["www." .. match] end, websites)
if next(websites) then print(colors.cyan(table.concat(map.lp(L"'http://'.._", websites), " "))) end
print()
end
local typecolors = {
["Educational/Research"] = colors.green,
["Non-Profit"] = colors.green,
["Cable/DSL/ISP"] = colors.green,
["Content"] = colors.red,
["Enterprise"] = colors.red,
["NSP"] = colors.red,
}
local function prettypdb(pdb)
print(colors.yellow("\tPeeringDB:\n"))
local website = pdb:match"\nWebsite *: ([^\n]+)"
if website then print(colors.cyan(website)) end
local type = pdb:match"\nNetwork Type *: ([^\n]+)"
if type then print("Network Type: " .. (typecolors[type] or colors.blue)(type)) end
local ratio = pdb:match"\nTraffic Ratios *: ([^\n]+)"
if ratio then print("Traffic Ratios: " .. ratio) end
print()
end
local h = {
{["<AS>[!-]"] = "Jump to AS number (! => add in database if non existent, - => delete)"},
{d = "Review dunno"},
{k = "Review kids"},
{s = "Review sirs"},
{r = "Decide AS numbers with whois matching regex"},
{c = "Commit"},
{q = "Quit"},
}
local flags = { ['-'] = false, ['!'] = true }
local function helpcmd(t)
for _, cmd in ipairs(t) do
local code, msg = next(cmd)
print("", colors.pink(code), msg)
end
end
local ASh = {
{["-"] = "Delete this AS"},
{d = "Dunno"},
{s = "Sir"},
{k = "Kid"},
{w = "Fetch whois again"},
{l = "Print long whois"},
{p = "Fetch PeeringDB data again"},
{["e [ex]"] = "Print exclusions, or set/get exclusion 'ex'"},
{n = "Next"},
{q = "Quit"},
}
local abbrev = { d = "dunno", k = "kids", s = "sirs" }
local tagcolor = { dunno = colors.blue, kids = colors.red, sirs = colors.green }
local function inspectAS(AS, tag)
local whois, pdb, exclusions = fetchwhois(AS), fetchpdb(AS), getexclusions(AS)
print("\n" .. colors.lightgray"========================================================================\n")
if whois then prettywhois(whois) end
if pdb then prettypdb(pdb) end
print(tagcolor[tag]("AS" .. AS))
while true do
local cmd, e
while not cmd do
io.write(colors.pink("Command (-/d/s/k/w/l/p/e/n/q): "))
local l = io.read("*l"):lower()
cmd, e = l:match("^ *([%-dskwlpnqe]) *([%d%./]*)$")
if not cmd or cmd == "e" and e ~= "" and not ip.ip(e) then helpcmd(ASh) end
end
if cmd == '-' then
db:settag(AS)
print("Deleted AS" .. AS)
break
elseif abbrev[cmd] then
db:settag(AS, abbrev[cmd])
print(tagcolor[abbrev[cmd]]("AS" .. AS))
elseif cmd == 'w' then
local whois = fetchwhois(AS, true)
if whois then prettywhois(whois) end
elseif cmd == 'l' then
local whois = fetchwhois(AS)
if whois then
local less = os.execute("which less >/dev/null 2>/dev/null") and io.popen("less", "w")
if less then less:write(whois) less:close()
else print(whois) end
end
elseif cmd == 'p' then
local pdb = fetchpdb(AS, true)
if pdb then prettypdb(pdb) end
elseif cmd == 'n' then break
elseif cmd == 'q' then return false
elseif cmd == 'e' then
if e == "" then
if not next(exclusions) then print("No exclusions.")
else for exclusion in pairs(exclusions) do print(colors.orange(exclusion)) end end
else
e = ip.ip(e)
for exclusion in pairs(exclusions) do if exclusion == e then
exclusions[exclusion] = nil
setexclusions(AS, exclusions)
print(colors.red("Removed") .. " exclusion " .. colors.orange(exclusion))
goto nextcmd
end end
exclusions[e] = true
setexclusions(AS, exclusions)
print(colors.green("Added") .. " exclusion " .. colors.orange(e))
end
end
:: nextcmd ::
end
return true
end
while true do
local cmd, flag
while not cmd do
io.write(colors.pink("Command (<AS>[!-]/d/k/s/r/c/q): "))
local l = io.read("*l"):lower()
cmd, flag = l:match("^ *([%ddksrcq]+)([%!%-]?) *$")
flag = flags[flag]
if not cmd or (force and not tonumber(cmd)) then helpcmd(h) end
end
local AS = tonumber(cmd)
if AS then
if not db[AS] then
if not flag then print("AS" .. AS .. " is not in the database.") goto nextcmd end
db:settag(AS, "dunno")
elseif flag == false then
db:settag(AS)
print("Deleted AS" .. AS)
goto nextcmd
end
inspectAS(AS, db[AS].tag)
elseif abbrev[cmd] then for AS in pairs(db.groups[abbrev[cmd]]) do
if not inspectAS(AS, abbrev[cmd]) then break end
end elseif cmd == 'c' then os.execute("git reset HEAD . && git add db/ && git commit")
elseif cmd == 'r' then
local regex
while not regex do
io.write(colors.pink("regex: "))
regex = io.read("*l")
if regex == "" then goto nextcmd end
regex = pcall(function() (""):match(regex) end) and regex or print("Invalid pattern")
end
for AS in pairs(db.groups.dunno) do
local whois = fetchwhois(AS)
if whois and whois:lower():match(regex) then if not inspectAS(AS, db[AS].tag) then break end end
end
else return end
:: nextcmd ::
end