2013-04-25 16:00:06 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var Auth = require("./auth");
|
|
|
|
var Logger = require("./logger");
|
|
|
|
var ActionLog = require("./actionlog");
|
2013-05-03 20:53:10 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
|
2013-06-22 01:28:21 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
module.exports = function (Server) {
|
2013-07-28 15:49:29 +00:00
|
|
|
function getIP(req) {
|
|
|
|
var raw = req.connection.remoteAddress;
|
|
|
|
var forward = req.header("x-forwarded-for");
|
|
|
|
if(Server.cfg["trust-x-forward"] && forward) {
|
|
|
|
var ip = forward.split(",")[0];
|
|
|
|
Logger.syslog.log("REVPROXY " + raw + " => " + ip);
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
function getChannelData(channel) {
|
|
|
|
var data = {
|
|
|
|
name: channel.name,
|
|
|
|
loaded: true
|
|
|
|
};
|
|
|
|
|
|
|
|
data.pagetitle = channel.opts.pagetitle;
|
|
|
|
data.media = channel.playlist.current ?
|
|
|
|
channel.playlist.current.media.pack() :
|
|
|
|
{};
|
|
|
|
data.usercount = channel.users.length;
|
|
|
|
data.afkcount = channel.afkers.length;
|
|
|
|
data.users = [];
|
|
|
|
for(var i in channel.users)
|
|
|
|
if(channel.users[i].name !== "")
|
|
|
|
data.users.push(channel.users[i].name);
|
|
|
|
|
|
|
|
data.chat = [];
|
|
|
|
for(var i in channel.chatbuffer)
|
|
|
|
data.chat.push(channel.chatbuffer[i]);
|
|
|
|
|
|
|
|
return data;
|
2013-07-16 15:46:09 +00:00
|
|
|
}
|
2013-04-25 16:00:06 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
var app = Server.app;
|
2013-04-25 16:00:06 +00:00
|
|
|
|
2013-08-11 22:23:20 +00:00
|
|
|
/* <https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol> */
|
|
|
|
app.get("/api/coffee", function (req, res) {
|
|
|
|
res.send(418); // 418 I'm a teapot
|
|
|
|
});
|
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
/* REGION channels */
|
|
|
|
|
|
|
|
/* data about a specific channel */
|
|
|
|
app.get("/api/channels/:channel", function (req, res) {
|
|
|
|
var name = req.params.channel;
|
|
|
|
if(!name.match(/^[\w-_]+$/)) {
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
name: name,
|
|
|
|
loaded: false
|
|
|
|
};
|
|
|
|
|
|
|
|
if(Server.channelLoaded(name))
|
2013-08-12 03:36:42 +00:00
|
|
|
data = getChannelData(Server.getChannel(name));
|
2013-08-08 03:44:41 +00:00
|
|
|
|
|
|
|
res.type("application/json");
|
|
|
|
res.jsonp(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* data about all channels (filter= public or all) */
|
|
|
|
app.get("/api/allchannels/:filter", function (req, res) {
|
|
|
|
var filter = req.params.filter;
|
|
|
|
if(filter !== "public" && filter !== "all") {
|
|
|
|
res.send(400);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-04 16:40:46 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
var query = req.query;
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
// Listing non-public channels requires authenticating as an admin
|
|
|
|
if(filter !== "public") {
|
|
|
|
var name = query.name || "";
|
|
|
|
var session = query.session || "";
|
|
|
|
var row = Auth.login(name, "", session);
|
2013-07-15 22:57:33 +00:00
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
2013-08-08 03:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var channels = [];
|
|
|
|
for(var key in Server.channels) {
|
|
|
|
var channel = Server.channels[key];
|
|
|
|
if(channel.opts.show_public) {
|
|
|
|
channels.push(getChannelData(channel));
|
|
|
|
} else if(filter !== "public") {
|
|
|
|
channels.push(getChannelData(channel));
|
2013-05-01 22:49:34 +00:00
|
|
|
}
|
2013-08-08 03:44:41 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
res.type("application/jsonp");
|
|
|
|
res.jsonp(channels);
|
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
/* ENDREGION channels */
|
2013-05-29 15:49:09 +00:00
|
|
|
|
2013-08-12 02:20:09 +00:00
|
|
|
/* REGION authentication, account management */
|
2013-08-08 03:44:41 +00:00
|
|
|
|
|
|
|
/* login */
|
|
|
|
app.post("/api/login", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.body.name;
|
|
|
|
var pw = req.body.pw;
|
|
|
|
var session = req.body.session;
|
|
|
|
|
|
|
|
// for some reason CyTube previously allowed guest logins
|
|
|
|
// over the API...wat
|
|
|
|
if(!pw && !session) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "You must provide a password"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row) {
|
|
|
|
if(session && !pw) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Session expired"
|
2013-07-15 22:57:33 +00:00
|
|
|
});
|
2013-08-08 03:44:41 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ActionLog.record(getIP(req), name, "login-failure",
|
|
|
|
"invalid_password");
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Provided username/password pair is invalid"
|
2013-07-15 22:57:33 +00:00
|
|
|
});
|
2013-08-08 03:44:41 +00:00
|
|
|
return;
|
2013-07-15 22:57:33 +00:00
|
|
|
}
|
2013-08-08 03:44:41 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
// record the login if the user is an administrator
|
|
|
|
if(row.global_rank >= 255)
|
|
|
|
ActionLog.record(getIP(req), name, "login-success");
|
|
|
|
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
name: name,
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-12 02:20:09 +00:00
|
|
|
/* register an account */
|
|
|
|
app.post("/api/register", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.body.name;
|
|
|
|
var pw = req.body.pw;
|
|
|
|
var ip = getIP(req);
|
|
|
|
|
|
|
|
// Limit registrations per IP within a certain time period
|
|
|
|
if(ActionLog.tooManyRegistrations(ip)) {
|
|
|
|
ActionLog.record(ip, name, "register-failure",
|
|
|
|
"Too many recent registrations");
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Your IP address has registered too many accounts "+
|
|
|
|
"in the past 48 hours. Please wait a while before"+
|
|
|
|
" registering another."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pw) {
|
|
|
|
// costanza.jpg
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "You must provide a password"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Auth.validateName(name)) {
|
|
|
|
ActionLog.record(ip, name, "register-failure", "Invalid name");
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Invalid username. Valid usernames must be 1-20 "+
|
|
|
|
"characters long and consist only of alphanumeric "+
|
|
|
|
"characters and underscores (_)"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Auth.isRegistered(name)) {
|
|
|
|
ActionLog.record(ip, name, "register-failure", "Name taken");
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "That username is already taken"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var session = Auth.register(name, pw);
|
|
|
|
if(!session) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Registration error. Contact an administrator "+
|
|
|
|
"for assistance."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionLog.record(ip, name, "register-success");
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
session: session
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-11 22:23:20 +00:00
|
|
|
/* password change */
|
2013-08-11 22:55:53 +00:00
|
|
|
app.post("/api/account/passwordchange", function (req, res) {
|
2013-08-11 22:23:20 +00:00
|
|
|
res.type("application/jsonp");
|
2013-05-29 18:19:43 +00:00
|
|
|
|
2013-08-11 22:23:20 +00:00
|
|
|
var name = req.body.name;
|
|
|
|
var oldpw = req.body.oldpw;
|
|
|
|
var newpw = req.body.newpw;
|
2013-05-29 18:19:43 +00:00
|
|
|
|
2013-08-11 22:23:20 +00:00
|
|
|
if(!oldpw || !newpw) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Password cannot be empty"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-05-30 18:07:29 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
var row = Auth.login(name, oldpw, "");
|
2013-08-11 22:23:20 +00:00
|
|
|
if(!row) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Invalid username/password combination"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionLog.record(getIP(req), name, "password-change");
|
|
|
|
var success = Auth.setUserPassword(name, newpw);
|
|
|
|
|
|
|
|
if(!success) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Server error. Please try again or ask an "+
|
|
|
|
"administrator for assistance."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* password reset */
|
2013-08-11 22:55:53 +00:00
|
|
|
app.post("/api/account/passwordreset", function (req, res) {
|
2013-08-11 22:23:20 +00:00
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.body.name;
|
|
|
|
var email = req.body.email;
|
|
|
|
var ip = getIP(req);
|
|
|
|
var hash = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
hash = Server.db.generatePasswordReset(ip, name, email);
|
|
|
|
ActionLog.record(ip, name, "password-reset-generate", email);
|
|
|
|
} catch(e) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Server.cfg["enable-mail"]) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "This server does not have email recovery enabled."+
|
|
|
|
" Contact an administrator for assistance."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!email) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "You don't have a recovery email address set. "+
|
|
|
|
"Contact an administrator for assistance."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = "A password reset request was issued for your account '"+
|
|
|
|
name + "' on " + Server.cfg["domain"] + ". This request"+
|
|
|
|
" is valid for 24 hours. If you did not initiate this, "+
|
|
|
|
"there is no need to take action. To reset your "+
|
|
|
|
"password, copy and paste the following link into your "+
|
|
|
|
"browser: " + Server.cfg["domain"] + "/reset.html?"+hash;
|
|
|
|
|
|
|
|
var mail = {
|
|
|
|
from: "CyTube Services <" + Server.cfg["mail-from"] + ">",
|
2013-08-12 04:09:43 +00:00
|
|
|
to: email,
|
2013-08-11 22:23:20 +00:00
|
|
|
subject: "Password reset request",
|
|
|
|
text: msg
|
|
|
|
};
|
|
|
|
|
2013-08-12 04:09:43 +00:00
|
|
|
Server.cfg["nodemailer"].sendMail(mail, function (err, response) {
|
2013-08-11 22:23:20 +00:00
|
|
|
if(err) {
|
|
|
|
Logger.errlog.log("mail fail: " + err);
|
|
|
|
res.jsonp({
|
2013-07-15 22:57:33 +00:00
|
|
|
success: false,
|
2013-08-11 22:23:20 +00:00
|
|
|
error: "Email send failed. Contact an administrator "+
|
|
|
|
"if this persists"
|
2013-07-15 22:57:33 +00:00
|
|
|
});
|
2013-08-11 22:23:20 +00:00
|
|
|
} else {
|
|
|
|
res.jsonp({
|
|
|
|
success: true
|
2013-07-15 22:57:33 +00:00
|
|
|
});
|
|
|
|
}
|
2013-08-11 22:23:20 +00:00
|
|
|
});
|
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
/* password recovery */
|
|
|
|
app.get("/api/account/passwordrecover", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var hash = req.query.hash;
|
|
|
|
var ip = getIP(req);
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
try {
|
|
|
|
var info = Server.db.recoverPassword(hash);
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
name: info[0],
|
|
|
|
pw: info[1]
|
|
|
|
});
|
|
|
|
ActionLog.record(ip, info[0], "password-recover-success");
|
|
|
|
} catch(e) {
|
|
|
|
ActionLog.record(ip, "", "password-recover-failure", hash);
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
/* profile retrieval */
|
|
|
|
app.get("/api/users/:user/profile", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.params.user;
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
try {
|
|
|
|
var prof = Server.db.getProfile(name);
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
profile_image: prof.profile_image,
|
|
|
|
profile_text: prof.profile_text
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
/* profile change */
|
|
|
|
app.post("/api/account/profile", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.body.name;
|
|
|
|
var pw = req.body.pw;
|
|
|
|
var session = req.body.session;
|
|
|
|
var img = req.body.profile_image;
|
|
|
|
var text = req.body.profile_text;
|
2013-05-30 01:04:22 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Invalid login"
|
2013-07-15 22:57:33 +00:00
|
|
|
});
|
2013-08-11 22:55:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-05-30 01:04:22 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
var result = Server.db.setProfile(name, {
|
|
|
|
image: img,
|
|
|
|
text: text
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!result) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Server error. Contact an administrator for assistance"
|
2013-07-15 22:57:33 +00:00
|
|
|
});
|
2013-08-11 22:55:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.jsonp({
|
|
|
|
success: true
|
|
|
|
});
|
2013-05-30 01:04:22 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
// Update profile on all channels the user is connected to
|
|
|
|
name = name.toLowerCase();
|
|
|
|
for(var i in Server.channels) {
|
|
|
|
var chan = Server.channels[i];
|
|
|
|
for(var j in chan.users) {
|
|
|
|
var user = chan.users[j];
|
|
|
|
if(user.name.toLowerCase() == name) {
|
|
|
|
user.profile = {
|
|
|
|
image: img,
|
|
|
|
text: text
|
|
|
|
};
|
|
|
|
chan.broadcastUserUpdate(user);
|
2013-07-15 22:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-11 22:55:53 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
});
|
2013-05-29 15:49:09 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
/* set email */
|
|
|
|
app.post("/api/account/email", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.body.name;
|
|
|
|
var pw = req.body.pw;
|
|
|
|
var email = req.body.email;
|
2013-05-29 15:49:09 +00:00
|
|
|
|
2013-08-11 22:55:53 +00:00
|
|
|
if(!email.match(/^[\w_\.]+@[\w_\.]+[a-z]+$/i)) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Invalid email address"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Nice try, but no"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
var row = Auth.login(name, pw, "");
|
2013-08-11 22:55:53 +00:00
|
|
|
if(!row) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Invalid login credentials"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var success = Server.db.setUserEmail(name, email);
|
|
|
|
if(!success) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Email update failed. Contact an administrator "+
|
|
|
|
"for assistance."
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionLog.record(getIP(req), name, "email-update", email);
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-12 02:20:09 +00:00
|
|
|
/* my channels */
|
|
|
|
app.get("/api/account/mychannels", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.query.name;
|
|
|
|
var session = req.query.session;
|
2013-08-06 15:20:15 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
var row = Auth.login(name, "", session);
|
2013-08-12 02:20:09 +00:00
|
|
|
if(!row) {
|
|
|
|
res.jsonp({
|
|
|
|
success: false,
|
|
|
|
error: "Invalid login"
|
2013-08-06 15:20:15 +00:00
|
|
|
});
|
2013-08-12 02:20:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-08-06 15:20:15 +00:00
|
|
|
|
2013-08-12 02:20:09 +00:00
|
|
|
var channels = Server.db.listUserChannels(name);
|
|
|
|
res.jsonp({
|
|
|
|
success: true,
|
|
|
|
channels: channels
|
|
|
|
});
|
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
/* END REGION */
|
2013-08-12 02:20:09 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
/* REGION log reading */
|
2013-07-13 16:05:58 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
/* action log */
|
|
|
|
app.get("/api/logging/actionlog", function (req, res) {
|
|
|
|
res.type("application/jsonp");
|
|
|
|
var name = req.query.name;
|
|
|
|
var session = req.query.session;
|
|
|
|
var types = req.query.actions;
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
var row = Auth.login(name, "", session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-03 20:53:10 +00:00
|
|
|
|
2013-08-12 02:32:02 +00:00
|
|
|
types = types.split(",");
|
2013-08-12 03:36:42 +00:00
|
|
|
var actions = ActionLog.readLog(types);
|
2013-08-12 02:32:02 +00:00
|
|
|
res.jsonp(actions);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* helper function to pipe the last N bytes of a file */
|
|
|
|
function pipeLast(res, file, len) {
|
|
|
|
fs.stat(file, function (err, data) {
|
|
|
|
if(err) {
|
|
|
|
res.send(500);
|
|
|
|
return;
|
2013-05-03 20:53:10 +00:00
|
|
|
}
|
2013-08-12 02:32:02 +00:00
|
|
|
var start = data.size - len;
|
|
|
|
if(start < 0) {
|
|
|
|
start = 0;
|
2013-05-03 20:53:10 +00:00
|
|
|
}
|
2013-08-12 02:32:02 +00:00
|
|
|
var end = data.size - 1;
|
|
|
|
fs.createReadStream(file, { start: start, end: end })
|
|
|
|
.pipe(res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
app.get("/api/logging/syslog", function (req, res) {
|
|
|
|
res.type("text/plain");
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
var name = req.query.name;
|
|
|
|
var session = req.query.session;
|
|
|
|
|
|
|
|
var row = Auth.login(name, "", session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeLast(res, "sys.log", 1048576);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/api/logging/errorlog", function (req, res) {
|
|
|
|
res.type("text/plain");
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
var name = req.query.name;
|
|
|
|
var session = req.query.session;
|
|
|
|
|
|
|
|
var row = Auth.login(name, "", session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeLast(res, "error.log", 1048576);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/api/logging/channels/:channel", function (req, res) {
|
|
|
|
res.type("text/plain");
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
var name = req.query.name;
|
|
|
|
var session = req.query.session;
|
|
|
|
|
|
|
|
var row = Auth.login(name, "", session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var chan = req.params.channel || "";
|
|
|
|
if(!chan.match(/^[\w-_]+$/)) {
|
|
|
|
res.send(400);
|
|
|
|
return;
|
2013-07-15 22:57:33 +00:00
|
|
|
}
|
2013-08-12 02:32:02 +00:00
|
|
|
|
|
|
|
fs.exists("chanlogs/" + chan + ".log", function(exists) {
|
|
|
|
if(exists) {
|
|
|
|
pipeLast(res, "chanlogs/" + chan + ".log", 1048576);
|
|
|
|
} else {
|
|
|
|
res.send(404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-07-16 15:46:09 +00:00
|
|
|
|
2013-08-08 03:44:41 +00:00
|
|
|
return null;
|
2013-05-03 20:53:10 +00:00
|
|
|
}
|