forked from kd7lxl/mtr-web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
56 lines (49 loc) · 1.13 KB
/
app.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
require 'sinatra'
require 'sinatra-websocket'
require 'pty'
require 'shellwords'
set :bind, '0.0.0.0'
set :port, '8000'
set :server, 'thin'
get '/' do
if !request.websocket?
erb :index
else
request.websocket do |ws|
ws.onmessage do |msg|
request = JSON.parse(msg)
args = ['mtr', '-p', '-c', '10']
if request['no_dns']
args << '--no-dns'
elsif request['protocol'] == 'TCP'
args << '-T'
else request['protocol'] == 'UDP'
args << '-u'
end
if request['version'] == '4'
args << '-4'
elsif request['version'] == '6'
args << '-6'
end
args << Shellwords.escape(request['hostname'])
EM.defer { run(ws, args) }
end
end
end
end
def run(ws, args)
begin
PTY.spawn(args.join(' ')) do |r, w, pid|
begin
r.each do |line|
parts = line.split(' ')
parts[2] = sprintf('%.2f%%', parts[2].to_f / 1000.0)
ws.send(parts.to_json)
end
rescue Errno::EIO
end
end
rescue PTY::ChildExited => e
puts "The child process exited!"
end
end