-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathiptables.rb
83 lines (67 loc) · 1.83 KB
/
iptables.rb
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
require 'singleton'
require 'hotlib'
#load 'hotspotd.conf'
class IPTables
include Singleton
#IPTABLES_SCRIPT = './iptables/iptables'
IPTABLES_SCRIPT = '/usr/bin/iptables-wrapper'
def initialize ()
@allowed = []
@log = HotLogger.instance.log
reset
end
def reset ()
return if HotConfig::TEST_MODE
runCommand "reset", 0
@log.info "iptables: resetting"
end
def allow (mac)
return if HotConfig::TEST_MODE
index = @allowed.length
runCommand 'add-filt', index, mac.to_s
begin # if this one fails, we better delete the last one
runCommand 'add-nat', index, mac.to_s
rescue IPTablesException
runCommand 'del-filt', index
raise
end
# if we get here, we successfully added
@log.info "iptables: added mac #{mac}"
@allowed [index] = mac
end
def remove (mac)
return if HotConfig::TEST_MODE
index = @allowed.index mac
if not index
raise "Mac address not registered #{mac}"
end
removeIndex index
@log.info "iptables: deleted mac #{mac}"
end
private
def removeIndex (index)
if not @allowed [index]
raise "Incorrect index #{index}"
end
begin
runCommand 'del-filt', index
runCommand 'del-nat', index
end
# delete the positions of the array.
# NOTE That all the other indices change. Thus this method is private
@allowed [index, 1] = []
end
# NOTE - iptables expects minimum index 1
# so we will fake it by adding 1 to the index here. AND HERE ONLY
def runCommand (cmd, index, mac = nil)
index += 1
error = system(IPTABLES_SCRIPT, cmd, index.to_s, mac.to_s)
if not error then
raise IPTablesException.new("iptables error: #{$?.exitstatus}")
end
end
end
#tables = IPTables.instance
#mac = MACAddress.new('00000000bac0')
#tables.allow mac
#tables.remove mac