-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhotserver.rb
204 lines (156 loc) · 4.64 KB
/
hotserver.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
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
require 'hotlib'
require 'iptables'
#wraps hotdata class, provides access over wire and marshalling
class HotServer
DATAFILE = 'hotdata.dat'
CLEAN_INTERVAL = 10
def initialize()
@log = HotLogger.instance.log
@tables = IPTables.instance
@data = HotData.new
#ensure thread safety (I'm not sure if this is necessary)
#@data.extend(MonitorMixin)
load_data()
cleaner_thread()
@log.info 'HotServer ready...'
end
#------ data access methods
# create a unique token and add record
def addnew(mac)
if not mac.kind_of?(MACAddress)
raise "Incorrect MAC address supplied"
end
if get_record(mac)
raise "MAC address already registered"
end
rec = HotRecord.new(mac)
rec.token = gen_token()
@data.records << rec
reset_pending(rec) # see note below
save_data()
@log.info "Added #{rec}"
rec
end
# NOTE -- have to do this after! adding to @data
def reset_pending(rec)
@log.info "resetting time for #{rec}"
r = get_record(rec.mac)
r.time.begin(0, 0, HotConfig::PENDING_TIME, 0)
end
def get_record(mac)
@data.records.find { |r| r.mac == mac }
end
def del_record(mac)
@log.info "Deleting record with mac:#{mac}"
r = get_record(mac)
if r
@tables.remove(r.mac) if r.state.active?
@data.records.delete(r)
end
save_data()
r
end
def update_record(new)
# search for record
old = @data.records.find {|r| r.mac == new.mac}
# TODO -- something here
raise "Record not found" if not old
@log.info "Updating #{old.state} => #{new.state}"
# check if we need to update iptables
if new.state.activated?(old.state) then
@log.info "Allowing #{new.mac}"
@tables.allow(new.mac)
elsif new.state.deactivated?(old.state) then
@log.info "Removing #{new.mac}"
@tables.remove(new.mac)
end
# update the list and save
@data.records[@data.records.index(old)] = new
save_data
end
def update_record_time(mac, days, hours, mins, secs)
r = get_record(mac)
r.time.begin(days, hours, mins, secs) if r != nil
#update logs
sec_added = secs + (mins * 60) + (hours * 3600) + (days * 24 * 3600)
@data.sec_served += sec_added
@data.access_logs << HotLogRecord.new(r.token, r.mac, sec_added, Time.now)
save_data()
end
def list()
@data.records
end
def list_logs()
@data.access_logs
end
def get_sec_served()
@data.sec_served
end
def list_state(state)
@data.records.find_all { |r| r.state == state}
end
#--------------------------
def save_data()
#synchronize do
File.open(DATAFILE, "w") do |f|
Marshal.dump(@data, f)
end
#end
end
def load_data()
begin
File.open(DATAFILE) do |f|
@data = Marshal.load(f)
end
@log.info 'Loaded data file'
rescue Errno::ENOENT
#file not found, write empty and try again
@log.info 'Writing empty data file'
save_data()
retry #is this dangerous?
end
@data.records.each {|r|
@tables.allow(r.mac) if r.state.active?
}
end
private
#return a token which is free to be used
def gen_token()
used_tokens = []
@data.records.each() { |r| used_tokens << r.token }
free_tokens = @data.tokens - used_tokens
srand
free_tokens[rand(free_tokens.length)]
end
#gets rid of users in iptables who have 'timed out'
def cleaner_thread()
return if HotConfig::TEST_MODE
@log.info 'Started cleaner thread'
Thread.new {
begin
loop do
#@log.info 'Cleaner cleaning'
#find all timed out
timed_out = @data.records.find_all { |r| r.time.isover? }
#remove them
timed_out.each { |r|
if r.state.active?
@tables.remove(r.mac)
r.state = HotState::PENDING
reset_pending(r)
@log.info "Reset #{r} to PENDING"
else
@data.records.delete(r)
@log.info "Cleaned #{r}"
end
}
save_data() if timed_out.length > 0
@log.info 'Cleaner done'
sleep(CLEAN_INTERVAL)
end
catch
@log.error "Exception in cleaner thread: #{$!}"
end
}
end
end