14 socket = TCPSocket.new 'localhost', $macgeigerport
16 rescue Errno::EADDRNOTAVAIL => e
18 puts "maybe you need to start macgeiger first?"
33 elsif line == "END\n" then
36 wifi_hash = JSON.parse(line)
37 #if wifis.key?(wifi_hash["bssid"])
38 $wifis[wifi_hash["bssid"]] = line.chomp
47 socket = TCPSocket.new 'localhost', $macgeigerport
48 socket.puts "SELECT " + mac
53 socket = TCPSocket.new 'localhost', $macgeigerport
54 socket.puts "UNSELECT"
58 def http_resp(skt, status, io, content_type)
60 skt.print "HTTP/1.1 #{status}\r\nContent-Type: #{content_type}\r\n" +
61 "Content-Length: #{io.size}\r\nConnection: keep-alive\r\n"+
63 IO.copy_stream(io, skt)
66 CONTENT_TYPE_MAPPING = {
67 'html' => 'text/html',
68 'txt' => 'text/plain',
70 'jpg' => 'image/jpeg',
72 'js' => 'text/javascript',
73 'json' => 'application/json',
76 def content_type(path)
77 ext = File.extname(path).split(".").last
78 CONTENT_TYPE_MAPPING.fetch(ext, 'application/octet-stream')
82 def serve_static(socket, url)
83 url = '/index.html' if url == '/'
84 ct = content_type(url)
86 if !(File.exist?(path) && !File.directory?(path)) || url.include?("..")
88 io.puts "File not found"
89 http_resp socket, "404 Not Found", io, "text/plain"
91 File.open(path, "rb") do |file|
92 http_resp socket, "200 OK", file, ct
99 io.puts '{"wifis" : ['
100 #thanks to json's stupid decision that no trailing commas are accepted!
103 $wifis.each do |wifi, json|
112 http_resp(skt, "200 OK", io, "application/json")
117 http_resp(skt, "200 OK", io, "text/html")
120 def serve_update(skt)
126 if x[0] == "GET" then
139 def assign_thread(th)
169 elsif url == "/api/unselect" then
172 elsif url.start_with?("/api/select/") then
173 mac = url.split("/").last
176 elsif url.start_with?("/api/") then
181 serve_update(@socket)
186 serve_static(@socket, url)
195 $clients.each do |client|
196 if not client.running? then
202 gt = Thread.new { wifi_gather }
204 server = TCPServer.new $port
209 while session = server.accept
212 client.assign_thread ( Thread.new {client.run(session)} )
218 $clients.each do |client|
219 client.interrupt if client.running?