-
Notifications
You must be signed in to change notification settings - Fork 5
/
hotlib.rb
212 lines (168 loc) · 4.29 KB
/
hotlib.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
205
206
207
208
209
210
211
212
require 'logger'
require 'singleton'
require 'util'
load 'hotspotd.conf'
#data wrapper which is marshalled to file
class HotData
attr_reader :records, :access_logs, :sec_served, :tokens
attr_writer :records, :access_logs, :sec_served, :tokens
def initialize()
@records = []
@access_logs = []
@sec_served = 0
#default tokens
@tokens = ['zol', 'kris', 'tom', 'ange', 'tess', 'sophie', 'chaz', 'laurence', 'tinka', 'clemmo']
load_tokens
end
def load_tokens
f = File.open("hottokens.txt")
@tokens = []
f.each { |line|
@tokens << line.chomp
}
f.close
end
end
#each individual record is stored in here
class HotRecord
attr_writer :token, :mac, :time, :state
attr_reader :token, :mac, :time, :state
def initialize(mac)
@mac = mac
@time = HotTime.new
@token = 'UNSET'
@state = HotState::PENDING
end
def to_s
"HotRecord-> token:#{@token} mac:#{@mac} state:#{@state} time:#{@time}"
end
end
class HotLogRecord
attr_reader :token, :mac, :sec_added, :at_time
def initialize(token, mac, sec_added, at_time)
@token = token
@mac = mac
@sec_added = sec_added
@at_time = at_time
end
end
class HotState < EnumeratedType
PENDING
ONLINE
PERMANENT
def activated?(old)
not old.active? and self.active?
end
def deactivated?(old)
old.active? and not self.active?
end
def active?
self == HotState::ONLINE || self == HotState::PERMANENT
end
end
#holds times by which we time out users
class HotTime
attr_reader :end_time, :start_time
def initialize()
@start_time = Time.at(0) #1969
@end_time = Time.at(0) #1969
end
def begin(days, hours, mins, secs)
@start_time = Time.now
#calculate when to terminate us
period_secs = secs + (mins * 60) + (hours * 3600) + (days * 24 * 3600)
@end_time = @start_time + period_secs
end
def add_time(days, hours, mins, secs)
period_secs = secs + (mins * 60) + (hours * 3600) + (days * 24 * 3600)
@end_time = @end_time + period_secs
end
def isover?()
Time.now > end_time
end
#format the time remaining as a string e.g. 4d 2h 31m 10s
def remaining_str()
return "expired" if isover?
diff = (@end_time - Time.now).floor
diff, seconds = diff.divmod 60
diff, minutes = diff.divmod 60
diff, hours = diff.divmod 24
days = diff
str = ''
str += "#{days}d" if days != 0
str += "#{hours}h" if hours != 0
str += "#{minutes}m" if minutes != 0
str += "#{seconds}s" if seconds != 0
"#{str} remaining"
end
def HotTime.sec_to_str(secs)
secs, seconds = secs.divmod 60
secs, minutes = secs.divmod 60
secs, hours = secs.divmod 24
days = secs
str = ''
str += "#{days}d" if days != 0
str += "#{hours}h" if hours != 0
str += "#{minutes}m" if minutes != 0
str += "#{seconds}s" if seconds != 0
str
end
def to_s
#"#{@start_time.asctime} to #{@end_time.asctime}"
remaining_str
end
end
#simple wrapper for log class
class HotLogger
include Singleton
attr_reader :log
def initialize()
set_to_STDOUT()
end
def defaults()
@log.level = Logger::DEBUG
@log.datetime_format = "%H:%H:%S"
end
def set_to_file(filename)
@log = Logger.new(filename, 1, 10*1024) #rotate when 10k
defaults()
end
def set_to_STDOUT()
@log = Logger.new(STDOUT)
defaults()
end
end
class MACAddress
attr_accessor :parts
def initialize (str)
matchstr = (["([[:xdigit:]]{2})"] * 6).join "[-:]?"
match = str.match "^#{matchstr}$"
if not match
raise "Incorrectly formatted mac address: '#{str}'"
end
@parts = match [1,6]
@parts.each {|part| part.downcase!}
end
# generate a random mac address
def self.random
mac = self.new('00:00:00:00:00:00')
srand
6.times do |i|
2.times do |j|
r = rand(16)
r = (r+87).chr if r >= 10 # to hex
mac.parts[i][j] = r.to_s
end
end
mac
end
# perhaps a bit of a hack, but not the end of the world
def ==(other)
self.to_s == other.to_s
end
def to_s ()
@parts.join ":"
end
end
class IPTablesException < RuntimeError
end