This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.rb
103 lines (89 loc) · 2.17 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
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
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra-websocket'
class App < Sinatra::Base
register Sinatra::Reloader
set :server, 'thin'
set :sockets, []
enable :logging
not_found do
erb :"404.html"
end
get '/about' do
erb :"about.html"
end
get '/' do
unless (request.websocket? rescue nil)
@ruby_v = "#{RUBY_VERSION}"
@ruby_v << "p#{RUBY_PATCHLEVEL}" if RUBY_PATCHLEVEL
erb :"ws.html"
else
request.websocket do |ws|
ws.onmessage do |msg|
ws.send( evaluate(msg) )
settings.sockets << ws
end
ws.onclose do
warn("websocket closed")
settings.sockets.delete(ws)
end
end
end
end
get '/unsupported' do
@ruby_v = "#{RUBY_VERSION}"
@ruby_v << "p#{RUBY_PATCHLEVEL}" if RUBY_PATCHLEVEL
erb :"index.html"
end
def evaluate(message)
stdout_id = $stdout.to_i
cmd = <<-EOF
class IRB < Sinatra::Base
DEFAULT_TIMEOUT = 5 #sec
$SAFE = 3
$stdout = StringIO.new
value = nil
thread = Thread.start do
begin
value = #{message}
end
end
thread.join(DEFAULT_TIMEOUT)
if thread.alive?
if thread.respond_to? :kill!
thread.kill!
else
thread.kill
end
raise TimeoutError, "timed out"
end
value
end
EOF
begin
respond = eval(cmd, TOPLEVEL_BINDING)
result = " => #{respond.nil? ? 'nil' : respond }"
rescue SecurityError
result = "SecurityError: Can't process this line!"
rescue TimeoutError
result = "TimeoutError: Code took longer than 5 seconds to terminate"
rescue Exception => e
result = e.message.gsub(/<|>/,"")
ensure
begin
output = get_stdout
rescue
output = ""
result = "SyntaxError: Can't process this line!"
end
$stdout = IO.new(stdout_id)
end
output.empty? ? result : "#{output}<br />#{result}"
end
def get_stdout
$stdout.rewind
$stdout.read
end
# start the server if ruby file executed directly
run! if app_file == $0
end