2016-08-05 02:00:20 +00:00
|
|
|
if (/^v0/.test(process.version)) {
|
|
|
|
console.error('node.js ' + process.version + ' is not supported. ' +
|
|
|
|
'For more information, visit ' +
|
|
|
|
'https://github.com/calzoneman/sync/wiki/CyTube-3.0-Installation-Guide#nodejs');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-07 04:25:27 +00:00
|
|
|
try {
|
2017-04-05 06:02:31 +00:00
|
|
|
require("./lib/logger");
|
2015-10-07 04:25:27 +00:00
|
|
|
var Server = require("./lib/server");
|
|
|
|
} catch (err) {
|
|
|
|
console.error('FATAL: Failed to require() lib/server.js');
|
2015-12-22 01:34:22 +00:00
|
|
|
if (/module version mismatch/i.test(err.message)) {
|
|
|
|
console.error('Module version mismatch, try running `npm rebuild` or removing ' +
|
|
|
|
'the node_modules folder and re-running `npm install`');
|
|
|
|
} else {
|
|
|
|
console.error('Possible causes:\n' +
|
|
|
|
' * You haven\'t run `npm run build-server` to regenerate ' +
|
|
|
|
'the runtime\n' +
|
|
|
|
' * You\'ve upgraded node/npm and haven\'t rebuilt dependencies ' +
|
|
|
|
'(try `npm rebuild` or `rm -rf node_modules && npm install`)\n' +
|
|
|
|
' * A dependency failed to install correctly (check the output ' +
|
|
|
|
'of `npm install` next time)');
|
|
|
|
}
|
2015-10-27 05:56:53 +00:00
|
|
|
console.error(err.stack);
|
2015-10-07 04:25:27 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2013-10-11 20:48:01 +00:00
|
|
|
var Config = require("./lib/config");
|
2013-10-11 21:31:40 +00:00
|
|
|
var Logger = require("./lib/logger");
|
2016-04-24 02:53:18 +00:00
|
|
|
const Switches = require("./lib/switches");
|
2015-09-21 05:06:53 +00:00
|
|
|
require("source-map-support").install();
|
2013-10-11 20:48:01 +00:00
|
|
|
|
2014-01-22 23:11:26 +00:00
|
|
|
Config.load("config.yaml");
|
|
|
|
var sv = Server.init();
|
|
|
|
if (!Config.get("debug")) {
|
|
|
|
process.on("uncaughtException", function (err) {
|
|
|
|
Logger.errlog.log("[SEVERE] Uncaught Exception: " + err);
|
|
|
|
Logger.errlog.log(err.stack);
|
|
|
|
});
|
2013-10-11 20:48:01 +00:00
|
|
|
|
2014-01-22 23:11:26 +00:00
|
|
|
process.on("SIGINT", function () {
|
|
|
|
sv.shutdown();
|
|
|
|
});
|
|
|
|
}
|
2014-02-26 20:28:24 +00:00
|
|
|
|
|
|
|
var stdinbuf = "";
|
|
|
|
process.stdin.on("data", function (data) {
|
|
|
|
stdinbuf += data;
|
|
|
|
if (stdinbuf.indexOf("\n") !== -1) {
|
|
|
|
var line = stdinbuf.substring(0, stdinbuf.indexOf("\n"));
|
|
|
|
stdinbuf = stdinbuf.substring(stdinbuf.indexOf("\n") + 1);
|
|
|
|
handleLine(line);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-22 01:17:38 +00:00
|
|
|
var validIP = require('net').isIP;
|
2014-02-26 20:28:24 +00:00
|
|
|
function handleLine(line) {
|
|
|
|
if (line === "/reload") {
|
|
|
|
Logger.syslog.log("Reloading config");
|
|
|
|
Config.load("config.yaml");
|
2014-04-11 02:54:46 +00:00
|
|
|
} else if (line === "/gc") {
|
|
|
|
if (global && global.gc) {
|
|
|
|
Logger.syslog.log("Running GC");
|
|
|
|
global.gc();
|
|
|
|
} else {
|
|
|
|
Logger.syslog.log("Failed to invoke GC: node started without --expose-gc");
|
|
|
|
}
|
2014-06-24 05:15:57 +00:00
|
|
|
} else if (line === "/delete_old_tables") {
|
|
|
|
require("./lib/database/update").deleteOldChannelTables(function (err) {
|
|
|
|
if (!err) {
|
|
|
|
Logger.syslog.log("Deleted old channel tables");
|
|
|
|
}
|
|
|
|
});
|
2016-04-24 02:53:18 +00:00
|
|
|
} else if (line.indexOf("/switch") === 0) {
|
|
|
|
var args = line.split(" ");
|
|
|
|
args.shift();
|
|
|
|
if (args.length === 1) {
|
|
|
|
Logger.syslog.log("Switch " + args[0] + " is " +
|
|
|
|
(Switches.isActive(args[0]) ? "ON" : "OFF"));
|
|
|
|
} else if (args.length === 2) {
|
|
|
|
Switches.setActive(args[0], args[1].toLowerCase() === "on" ? true : false);
|
|
|
|
Logger.syslog.log("Switch " + args[0] + " is now " +
|
|
|
|
(Switches.isActive(args[0]) ? "ON" : "OFF"));
|
|
|
|
}
|
2016-06-09 05:54:16 +00:00
|
|
|
} else if (line.indexOf("/reload-partitions") === 0) {
|
|
|
|
sv.reloadPartitionMap();
|
2016-07-16 05:06:27 +00:00
|
|
|
} else if (line.indexOf("/globalban") === 0) {
|
|
|
|
var args = line.split(/\s+/); args.shift();
|
2016-07-22 01:17:38 +00:00
|
|
|
if (args.length >= 2 && validIP(args[0]) !== 0) {
|
2016-07-16 05:06:27 +00:00
|
|
|
var ip = args.shift();
|
|
|
|
var comment = args.join(' ');
|
|
|
|
require("./lib/database").globalBanIP(ip, comment, function (err, res) {
|
|
|
|
if (!err) {
|
|
|
|
Logger.eventlog.log("[acp] " + "SYSTEM" + " global banned " + ip);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else if (line.indexOf("/unglobalban") === 0) {
|
|
|
|
var args = line.split(/\s+/); args.shift();
|
2016-07-22 01:17:38 +00:00
|
|
|
if (args.length >= 1 && validIP(args[0]) !== 0) {
|
2016-07-16 05:06:27 +00:00
|
|
|
var ip = args.shift();
|
|
|
|
require("./lib/database").globalUnbanIP(ip, function (err, res) {
|
|
|
|
if (!err) {
|
|
|
|
Logger.eventlog.log("[acp] " + "SYSTEM" + " un-global banned " + ip);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-07-20 10:01:34 +00:00
|
|
|
} else if (line.indexOf("/unloadchan") === 0) {
|
|
|
|
var args = line.split(/\s+/); args.shift();
|
|
|
|
if(args.length){
|
|
|
|
var name = args.shift();
|
|
|
|
var chan = sv.getChannel(name);
|
|
|
|
var users = Array.prototype.slice.call(chan.users);
|
|
|
|
chan.emit("empty");
|
|
|
|
users.forEach(function (u) {
|
|
|
|
u.kick("Channel shutting down");
|
|
|
|
});
|
|
|
|
Logger.eventlog.log("[acp] " + "SYSTEM" + " forced unload of " + name);
|
|
|
|
}
|
2017-05-01 00:20:19 +00:00
|
|
|
} else if (line.indexOf("/reloadcert") === 0) {
|
|
|
|
sv.reloadCertificateData();
|
2014-02-26 20:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-16 05:06:27 +00:00
|
|
|
|
|
|
|
// Go Go Gadget Service Socket
|
|
|
|
if (Config.get("service-socket.enabled")) {
|
|
|
|
Logger.syslog.log("Opening service socket");
|
|
|
|
var ServiceSocket = require('./lib/servsock');
|
|
|
|
var server = new ServiceSocket;
|
|
|
|
server.init(handleLine, Config.get("service-socket.socket"));
|
|
|
|
}
|
2016-12-20 08:09:24 +00:00
|
|
|
|
2017-05-14 11:20:58 +00:00
|
|
|
// Hi I'm Mr POSIX! Look at me!
|
|
|
|
process.on('SIGUSR2', () => {
|
|
|
|
sv.reloadCertificateData();
|
|
|
|
});
|
|
|
|
|
2016-12-20 08:09:24 +00:00
|
|
|
require("bluebird");
|
|
|
|
process.on("unhandledRejection", function (reason, promise) {
|
|
|
|
Logger.errlog.log("[SEVERE] Unhandled rejection: " + reason.stack);
|
|
|
|
});
|