forked from pisto/ASkidban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASkidban.lua
executable file
·102 lines (84 loc) · 3.2 KB
/
ASkidban.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
#!/usr/bin/env lua
pcall(function() require("debugger")() end)
assert(pcall(function() loadstring"goto label ::label::"() end), "Lua version 5.2 or luajit is required")
bit32 = bit32 or bit or require"bit"
local lfs = require"lfs"
local oldcd = lfs.currentdir()
local script, sep = arg[0], package.config:sub(1, 1)
lfs.chdir(script:match(".*%" .. sep) or ".")
local fp, L, ip = require"kblibs.fp", require"kblibs.lambda", require"kblibs.ip"
local map = fp.map
geoipfn = oldcd .. sep .. "GeoIPASNum2.csv"
commit = false
force = false
local function getarg(option)
assert(#arg > 0, "Missing argument for " .. option)
return table.remove(arg, 1)
end
local options = {
h = function()
print[[
Usage: ASkidban {[-h] | [-g <GeoIPASNum2.csv file>]... {hits | decide | compile}}
Options:
-h show help
-g <file> GeoIP ASnum database
-c commit changes to git
-f force (refresh cached IP ranges)
Commands:
hits import IP hits from stdin to seed AS numbers list
decide tag AS numbers
compile fetch IP ranges associated with the AS numbers
]]
end,
g = function() geoipfn = getarg"-g" end,
c = function() commit = true end,
f = function() force = true end,
}
local commands = map.vm(L"_, loadfile(_ .. '.lua')", "hits", "decide", "compile")
local cmd
while #arg > 0 do
local a = table.remove(arg, 1)
if a:sub(1, 1) == '-' then assert(options[a:sub(2)], "Unknown option " .. a)()
else cmd = assert(commands[a:match("([^%.]+)%.?l?u?a?")], "Unknown command " .. a) end
end
function fetchwhois(AS, force)
local data = db[AS][AS]
if data.whois and not force then return data.whois end
io.write("Fetching whois... ")
local whois, err = io.popen("timeout 5 whois AS" .. AS)
if not whois then print("Cannot fetch whois for AS" .. AS .. ": " .. err) return end
local msg, err = whois:read"*a"
local ok = whois:close()
if not ok then print("Cannot fetch whois for AS" .. AS .. " (probably interrupted)") return end
if not msg then print("Cannot fetch whois for AS" .. AS .. ": " .. err) return end
data.whois = msg
db:setdata(AS, data)
print()
return msg
end
function fetchpdb(AS, force)
local data = db[AS][AS]
if data.pdb ~= nil and not force then return data.pdb end
io.write ("Fetching PeeringDB... ")
local whois, err = io.popen("timeout 5 whois -h peeringdb.com AS" .. AS)
if not whois then print("Cannot fetch PeeringDB for AS" .. AS .. ": " .. err) return end
local msg, err = whois:read"*a"
local ok = whois:close()
if not ok then print("Cannot fetch PeeringDB for AS" .. AS .. " (probably interrupted)") return end
if not msg then print("Cannot fetch PeeringDB for AS" .. AS .. ": " .. err) return end
data.pdb = not msg:match"Record not found" and msg or false
db:setdata(AS, data)
if not data.pdb then print("Not found.") else print() end
return data.pdb
end
function getexclusions(AS)
return map.si(function(_, sip) return assert(ip.ip(sip), "Invalid exclusion range in AS" .. AS) end, db[AS][AS].exclusions or {})
end
function setexclusions(AS, exclusions)
local data = db[AS][AS]
data.exclusions = next(exclusions) and map.lp(tostring, exclusions) or nil
db:setdata(AS, data)
end
db = require"kblibs.db".load"db"
print("Database check succeeded.")
return cmd and cmd()