-
Notifications
You must be signed in to change notification settings - Fork 11
/
httpsrv.rb
224 lines (197 loc) · 3.8 KB
/
httpsrv.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
213
214
215
216
217
218
219
220
221
222
223
require 'socket'
require 'json'
require 'stringio'
$docroot = "./web"
$port = 8080
$macgeigerport = 9876
$wifis = Hash.new
$gt_socket = nil
def wifi_gather
begin
socket = TCPSocket.new 'localhost', $macgeigerport
$gt_socket = socket
rescue Errno::EADDRNOTAVAIL => e
puts e.message
puts "maybe you need to start macgeiger first?"
exit(1)
end
out = false
loop do
begin
socket.puts "LIST\n"
rescue IOError
break
end
loop do
line = socket.gets
if line == nil then
out = true
break
elsif line == "END\n" then
break
end
wifi_hash = JSON.parse(line)
#if wifis.key?(wifi_hash["bssid"])
$wifis[wifi_hash["bssid"]] = line.chomp
end
break if out
sleep 1
end
socket.close
end
def select_mac(mac)
socket = TCPSocket.new 'localhost', $macgeigerport
socket.puts "SELECT " + mac
socket.close
end
def unselect_mac()
socket = TCPSocket.new 'localhost', $macgeigerport
socket.puts "UNSELECT"
socket.close
end
def http_resp(skt, status, io, content_type)
io.rewind
skt.print "HTTP/1.1 #{status}\r\nContent-Type: #{content_type}\r\n" +
"Content-Length: #{io.size}\r\nConnection: keep-alive\r\n"+
"\r\n"
IO.copy_stream(io, skt)
end
CONTENT_TYPE_MAPPING = {
'html' => 'text/html',
'txt' => 'text/plain',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'css' => 'text/css',
'js' => 'text/javascript',
'json' => 'application/json',
}
def content_type(path)
ext = File.extname(path).split(".").last
CONTENT_TYPE_MAPPING.fetch(ext, 'application/octet-stream')
end
def serve_static(socket, url)
url = '/index.html' if url == '/'
ct = content_type(url)
path = $docroot + url
if !(File.exist?(path) && !File.directory?(path)) || url.include?("..")
io = StringIO.new
io.puts "File not found"
http_resp socket, "404 Not Found", io, "text/plain"
else
File.open(path, "rb") do |file|
http_resp socket, "200 OK", file, ct
end
end
end
def serve_full(skt)
io = StringIO.new
io.puts '{"wifis" : ['
#thanks to json's stupid decision that no trailing commas are accepted!
n = $wifis.size
i = 0
$wifis.each do |wifi, json|
io.print json
i += 1
if i != n then
io.print ","
end
io.print "\n"
end
io.print "]}\n"
http_resp(skt, "200 OK", io, "application/json")
end
def serve_empty(skt)
io = StringIO.new
http_resp(skt, "200 OK", io, "text/html")
end
def serve_update(skt)
skt.puts "full"
end
def get_path(str)
x = str.split(" ")
if x[0] == "GET" then
return x[1]
end
return nil
end
class Client
def initialize
@running = false
@socket = nil
@th = nil
end
def assign_thread(th)
@th = th
end
def running?
@running
end
def jointhr
@th.join
end
def interrupt
@socket.close
end
def run(socket)
@running = true
@socket = socket
loop do
begin
line = socket.gets
rescue IOError
break
end
if line == nil then
break
end
url = get_path(line)
if url == nil then
elsif url == "/api/unselect" then
unselect_mac()
serve_empty(@socket)
elsif url.start_with?("/api/select/") then
mac = url.split("/").last
select_mac (mac)
serve_empty(@socket)
elsif url.start_with?("/api/") then
case url
when "/api/full"
serve_full(@socket)
when "/api/update"
serve_update(@socket)
else
puts url
end
else
serve_static(@socket, url)
end
end
@socket.close
@running = false
end
end
def collect_clients
$clients.each do |client|
if not client.running? then
client.jointhr
end
end
end
gt = Thread.new { wifi_gather }
server = TCPServer.new $port
$clients = []
begin
while session = server.accept
collect_clients
client = Client.new
client.assign_thread ( Thread.new {client.run(session)} )
$clients << client
end
rescue Interrupt
end
$clients.each do |client|
client.interrupt if client.running?
end
collect_clients
$gt_socket.close
gt.join