forked from nestorInc/jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjukebox.rb
More file actions
executable file
·274 lines (230 loc) · 7.17 KB
/
jukebox.rb
File metadata and controls
executable file
·274 lines (230 loc) · 7.17 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env ruby
$:.unshift File.dirname($0)
require 'rev'
require 'socket'
require 'cgi'
require 'yaml.rb'
require 'json'
require 'yaml'
require 'bcrypt'
require 'stream.rb'
require 'http.rb'
require 'channel.rb'
require 'encode.rb'
require 'db.rb'
require 'json_api.rb'
require 'upload.rb'
require 'basic_api.rb'
require 'web_debug.rb'
require 'token_api.rb'
raise("Not support ruby version < 1.9") if(RUBY_VERSION < "1.9.0");
$error_file = File.open("error.log", "a+");
# Config
config = {}
begin
data = File.open("jukebox.cfg", &:read);
config = YAML.load(data);
rescue => e
error("Config file error: #{([ e.to_s ] + e.backtrace).join("\n")}", true, $error_file);
end
begin
pid_filename = config[:pid.to_s] || "jukebox.pid";
old_pid = File.read(pid_filename);
rescue => e
end
if old_pid
begin
Process.getpgid( old_pid.to_i )
error("Jukebox already started with pid #{old_pid}");
exit
rescue Errno::ESRCH
File.delete(pid_filename);
end
end
begin
File.open(pid_filename, 'w') { |file| file.write(Process.pid) }
rescue => e
error("Could not save pid #{pid_filename}");
end
library = Library.new();
channelList = {};
# Encode
Thread.new() {
e = Encode.new(library, config[:encode.to_s]);
e.attach(Rev::Loop.default);
begin
Rev::Loop.default.run();
rescue => e
error(([ e.to_s ] + e.backtrace).join("\n"), true, $error_file);
retry;
end
}
# Create HTTP server
json = JsonManager.new(channelList, library, config[:upload.to_s], config[:encode.to_s]);
basic = BasicApi.new(channelList);
upload = UploadManager.new(config[:upload.to_s]);
debug = DebugPage.new();
token = TokenManager.new(library);
main = HttpNodeMapping.new("html");
main_src = HttpNodeMapping.new("html_src");
stream = Stream.new(channelList, library);
sessions = HttpSessionStateCollection.new;
main.addAuth() { |s, req, user, pass|
# next nil if(s.ssl != true);
# Remove invalid HttpSessionState objects from memory
sessions.removeExpired();
# For now we haven't attach the current request to any HttpSessionState
currentSession = nil;
# Remove invalid sessions in database
library.invalidate_sessions();
req.options = {} if req.options == nil
isStreamPage = req.uri.path.end_with?("/stream");
isMainPage = ['/','/index.html'].include?(req.uri.path);
ip_address = s.remote_address.ip_address;
user_agent = "";
user_agent = req.options["User-Agent"] if( req.options["User-Agent"] )
if req.uri.query
form = Hash[URI.decode_www_form(req.uri.query)];
if(form["token"])
token = form["token"];
luser = library.check_login_token(nil, token);
if luser
sid = library.get_login_token_session(token);
if not sid
#TODO check if user has right to create session
sid = library.create_user_session(luser, ip_address, user_agent);
library.update_login_token_session(token, sid);
end
s.user.replace(luser);
s.sid.replace(sid);
user.replace(luser);
stream.channel_init(luser);
if not isStreamPage
req.options["Set-Cookie"] = []
req.options["Set-Cookie"] << Cookie.new({"session" => sid}, nil, "/", Time.now()+(2*7*24*60*60), nil, nil).to_s();
req.options["Set-Cookie"] << Cookie.new({"user" => luser}, nil, "/", Time.now()+(2*7*24*60*60), nil, nil).to_s();
end
next "token"
end
end
end
if not isStreamPage and req.options["Cookie"]
cookies = Hash[req.options["Cookie"].split(';').map{ |i| i.strip().split('=')}];
if cookies["session"]
session = cookies["session"];
# Check if the session is known in RAM
if sessions.exists(session)
currentSession = sessions.get(session);
luser = currentSession.Items["user"];
else # Check in db
luser = library.check_session(session, ip_address, user_agent);
if luser
currentSession = sessions.add(session, luser, ip_address, user_agent);
end
end
if luser
s.user.replace(luser);
s.sid.replace(session);
user.replace(luser);
stream.channel_init(luser);
# Avoid update session last_access for each resources
if isMainPage
currentSession.updateLastRequest() if currentSession;
library.update_session_last_connexion(session);
end
next "cookie"
end
end
end
if pass
if(user != "void" and library.login(user, pass) )
sid = library.create_user_session(user, ip_address, user_agent);
if sessions.exists(sid)
currentSession = sessions.get(sid);
else
currentSession = sessions.add(sid, user, ip_address, user_agent);
end
stream.channel_init(s.user)
s.sid.replace(sid)
if not isStreamPage
req.options["Set-Cookie"] = []
req.options["Set-Cookie"] << Cookie.new({"session" => sid}, nil, "/", Time.now()+(2*7*24*60*60), nil, nil).to_s();
req.options["Set-Cookie"] << Cookie.new({"user" => user}, nil, "/", Time.now()+(2*7*24*60*60), nil, nil).to_s();
end
next "httpAuth"
end
end
nil;
}
root = HttpRootNode.new({ "/api/json" => json,
"/api" => basic,
"/upload" => upload,
"/" => main,
"/src" => main_src,
"/api/token" => token,
"/stream" => stream});
# "/debug" => debug,
if(config[:server.to_s] == nil)
error("Config file error: no server section", true, $error_file);
exit(1);
end
config[:server.to_s].each { |server_config|
h = HttpServer.new(root, server_config);
h.attach(Rev::Loop.default)
}
class BugInfo
attr_accessor :issue
attr_accessor :frequency
def initialize()
@frequency = 0;
end
end
# Main loop
begin
Rev::Loop.default.run();
rescue => e
File.delete(pid_filename);
stat = YAML::load(File.open("exception_stat", File::RDONLY | File::CREAT, 0600));
stat = {} if(stat == false);
detail = ([ e.to_s ] + e.backtrace).join("\n")
puts detail;
stat[detail] = BugInfo.new() if(stat[detail] == nil);
info = stat[detail];
info.frequency += 1;
#create redmine ticket
if(config["redmine"])
require 'redmine_client'
cfg = config["redmine"];
RedmineClient::Base.configure do
self.site = cfg["site"];
self.user = cfg["user"];
self.password = cfg["password"];
end
# New bug
if(info.issue == nil)
issue = RedmineClient::Issue.new(:subject => e.to_s,
:project_id => cfg["project_id"],
:description => detail);
if(issue.save)
info.issue = issue.id
else
puts issue.errors.full_messages
end
end
# Add comment
if(info.issue)
issue = RedmineClient::Issue.find(info.issue);
issue.notes = dump_events() + "\n";
issue.save
end
end
File.open("exception_stat", "w") { |fd| fd.write(YAML::dump(stat)); }
report = detail;
report << "\n"
report << "----- Last events -----\n"
report << dump_events();
report << "\n"
File.open("crash#{Time.now.to_i}", "w") { |fd|
fd.write(report)
}
end