sync/src/user.js

500 lines
14 KiB
JavaScript
Raw Normal View History

2013-10-11 21:31:40 +00:00
var Server = require("./server");
var util = require("./utilities");
2014-01-06 15:55:12 +00:00
var db = require("./database");
2014-01-22 23:11:26 +00:00
var Config = require("./config");
2014-01-30 03:50:45 +00:00
var ACP = require("./acp");
2014-05-21 02:30:14 +00:00
var Account = require("./account");
var Flags = require("./flags");
import { EventEmitter } from 'events';
2017-04-05 06:02:31 +00:00
import Logger from './logger';
2017-08-02 02:29:11 +00:00
import net from 'net';
2017-04-05 06:02:31 +00:00
2017-07-09 03:11:54 +00:00
const LOGGER = require('@calzoneman/jsli')('user');
2017-07-25 05:08:26 +00:00
function User(socket, ip, loginInfo) {
this.flags = 0;
this.socket = socket;
2017-08-02 02:29:11 +00:00
// Expanding IPv6 addresses shouldn't really be necessary
// At some point, the IPv6 related stuff should be revisited
this.realip = net.isIPv6(ip) ? util.expandIPv6(ip) : ip;
this.displayip = util.cloakIP(this.realip);
2017-07-25 05:08:26 +00:00
this.channel = null;
this.queueLimiter = util.newRateLimiter();
this.chatLimiter = util.newRateLimiter();
this.reqPlaylistLimiter = util.newRateLimiter();
this.awaytimer = false;
if (loginInfo) {
2017-08-02 02:29:11 +00:00
this.account = new Account.Account(this.realip, loginInfo, socket.context.aliases);
2017-07-25 05:08:26 +00:00
this.registrationTime = new Date(this.account.user.time);
this.setFlag(Flags.U_REGISTERED | Flags.U_LOGGED_IN | Flags.U_READY);
socket.emit("login", {
success: true,
name: this.getName(),
guest: false
});
socket.emit("rank", this.account.effectiveRank);
2017-08-07 03:42:33 +00:00
if (this.account.globalRank >= 255) {
this.initAdminCallbacks();
}
this.emit("login", this.account);
2017-07-25 05:08:26 +00:00
LOGGER.info(ip + " logged in as " + this.getName());
} else {
2017-08-02 02:29:11 +00:00
this.account = new Account.Account(this.realip, null, socket.context.aliases);
2017-07-25 05:08:26 +00:00
socket.emit("rank", -1);
this.setFlag(Flags.U_READY);
2017-08-07 03:42:33 +00:00
this.once("login", account => {
if (account.globalRank >= 255) {
this.initAdminCallbacks();
}
});
2014-05-21 02:30:14 +00:00
}
2017-07-25 05:08:26 +00:00
socket.once("joinChannel", data => this.handleJoinChannel(data));
socket.once("initACP", () => this.handleInitACP());
socket.on("login", data => this.handleLogin(data));
}
2017-07-25 05:08:26 +00:00
User.prototype = Object.create(EventEmitter.prototype);
2014-05-21 02:30:14 +00:00
2017-07-25 05:08:26 +00:00
User.prototype.handleJoinChannel = function handleJoinChannel(data) {
if (typeof data !== "object" || typeof data.name !== "string") {
return;
}
2014-05-21 02:30:14 +00:00
2017-07-25 05:08:26 +00:00
if (this.inChannel()) {
return;
}
2017-07-25 05:08:26 +00:00
if (!util.isValidChannelName(data.name)) {
this.socket.emit("errorMsg", {
msg: "Invalid channel name. Channel names may consist of 1-30 " +
"characters in the set a-z, A-Z, 0-9, -, and _"
});
this.kick("Invalid channel name");
return;
}
2017-07-25 05:08:26 +00:00
data.name = data.name.toLowerCase();
if (data.name in Config.get("channel-blacklist")) {
this.kick("This channel is blacklisted.");
return;
}
this.waitFlag(Flags.U_READY, () => {
var chan;
try {
chan = Server.getServer().getChannel(data.name);
} catch (error) {
if (error.code === 'EWRONGPART') {
this.socket.emit("errorMsg", {
msg: "Channel '" + data.name + "' is hosted on another server. " +
"Try refreshing the page to update the connection URL."
});
2014-01-30 03:50:45 +00:00
} else {
2017-07-25 05:08:26 +00:00
LOGGER.error("Unexpected error from getChannel(): %s", error.stack);
this.socket.emit("errorMsg", {
msg: "Unable to join channel due to an internal error"
});
2014-01-30 03:50:45 +00:00
}
2014-01-06 15:55:12 +00:00
return;
}
2017-07-25 05:08:26 +00:00
if (!chan.is(Flags.C_READY)) {
chan.once("loadFail", reason => {
this.socket.emit("errorMsg", {
msg: reason,
alert: true
});
this.kick(`Channel could not be loaded: ${reason}`);
});
2014-05-21 02:30:14 +00:00
}
2017-07-25 05:08:26 +00:00
chan.joinUser(this, data);
});
};
2014-05-21 02:30:14 +00:00
2017-07-25 05:08:26 +00:00
User.prototype.handleInitACP = function handleInitACP() {
this.waitFlag(Flags.U_LOGGED_IN, () => {
if (this.account.globalRank >= 255) {
ACP.init(this);
2014-05-21 02:30:14 +00:00
} else {
2017-07-25 05:08:26 +00:00
this.kick("Attempted initACP from non privileged user. This incident " +
"will be reported.");
Logger.eventlog.log("[acp] Attempted initACP from socket client " +
this.getName() + "@" + this.realip);
2014-01-06 15:55:12 +00:00
}
});
2017-07-25 05:08:26 +00:00
};
2014-01-06 15:55:12 +00:00
2017-07-25 05:08:26 +00:00
User.prototype.handleLogin = function handleLogin(data) {
if (typeof data !== "object") {
this.socket.emit("errorMsg", {
msg: "Invalid login frame"
});
return;
}
2013-02-16 05:02:42 +00:00
2017-07-25 05:08:26 +00:00
var name = data.name;
if (typeof name !== "string") {
return;
}
var pw = data.pw || "";
if (typeof pw !== "string") {
pw = "";
}
if (this.is(Flags.U_LOGGING_IN) || this.is(Flags.U_LOGGED_IN)) {
return;
}
if (!pw) {
this.guestLogin(name);
} else {
this.login(name, pw);
}
};
2014-05-21 02:30:14 +00:00
User.prototype.die = function () {
2018-04-07 22:30:30 +00:00
for (const key in this.socket._events) {
2014-05-21 02:30:14 +00:00
delete this.socket._events[key];
}
delete this.socket.typecheckedOn;
delete this.socket.typecheckedOnce;
2018-04-07 22:30:30 +00:00
for (const key in this.__evHandlers) {
2014-05-21 02:30:14 +00:00
delete this.__evHandlers[key];
}
if (this.awaytimer) {
clearTimeout(this.awaytimer);
}
this.dead = true;
};
User.prototype.is = function (flag) {
return Boolean(this.flags & flag);
};
User.prototype.setFlag = function (flag) {
this.flags |= flag;
this.emit("setFlag", flag);
};
User.prototype.clearFlag = function (flag) {
this.flags &= ~flag;
this.emit("clearFlag", flag);
};
User.prototype.waitFlag = function (flag, cb) {
var self = this;
if (self.is(flag)) {
cb();
} else {
var wait = function (f) {
if (f === flag) {
self.removeListener("setFlag", wait);
2014-05-21 02:30:14 +00:00
cb();
}
};
self.on("setFlag", wait);
}
};
User.prototype.getName = function () {
return this.account.name;
};
User.prototype.getLowerName = function () {
return this.account.lowername;
};
2013-09-18 23:27:42 +00:00
User.prototype.inChannel = function () {
return this.channel != null && !this.channel.dead;
2013-11-25 22:20:15 +00:00
};
User.prototype.inRegisteredChannel = function () {
return this.inChannel() && this.channel.is(Flags.C_REGISTERED);
};
2014-05-21 02:30:14 +00:00
/* Called when a user's AFK status changes */
User.prototype.setAFK = function (afk) {
if (!this.inChannel()) {
return;
}
2014-05-21 02:30:14 +00:00
/* No change in AFK status, don't need to change anything */
if (this.is(Flags.U_AFK) === afk) {
2014-05-24 02:07:54 +00:00
this.autoAFK();
2013-08-01 19:12:57 +00:00
return;
}
2013-09-26 18:29:36 +00:00
if (afk) {
2014-05-21 02:30:14 +00:00
this.setFlag(Flags.U_AFK);
if (this.channel.modules.voteskip) {
this.channel.modules.voteskip.unvote(this.realip);
2017-04-04 03:31:21 +00:00
this.socket.emit("clearVoteskipVote");
}
2013-09-12 03:16:56 +00:00
} else {
2014-05-21 02:30:14 +00:00
this.clearFlag(Flags.U_AFK);
2013-08-01 13:39:10 +00:00
this.autoAFK();
}
2017-12-12 06:46:41 +00:00
if (!this.inChannel()) {
/*
* In unusual circumstances, the above emit("clearVoteskipVote")
* can cause the "disconnect" event to be fired synchronously,
* which results in this user no longer being in the channel.
*/
2017-12-12 06:46:41 +00:00
return;
}
2014-05-21 02:30:14 +00:00
/* Number of AFK users changed, voteskip state changes */
if (this.channel.modules.voteskip) {
this.channel.modules.voteskip.update();
}
this.emit('afk', afk);
2013-09-26 18:29:36 +00:00
};
2014-05-21 02:30:14 +00:00
/* Automatically tag a user as AFK after a period of inactivity */
User.prototype.autoAFK = function () {
2013-09-26 18:29:36 +00:00
var self = this;
if (self.awaytimer) {
2013-09-26 18:29:36 +00:00
clearTimeout(self.awaytimer);
}
2014-05-21 02:30:14 +00:00
if (!self.inChannel() || !self.channel.modules.options) {
return;
2013-11-30 03:09:19 +00:00
}
2014-05-21 02:30:14 +00:00
/* Don't set a timer if the duration is invalid */
var timeout = parseFloat(self.channel.modules.options.get("afk_timeout"));
if (isNaN(timeout) || timeout <= 0) {
2013-11-30 03:09:19 +00:00
return;
}
2013-09-26 18:29:36 +00:00
self.awaytimer = setTimeout(function () {
self.setAFK(true);
2013-11-30 03:09:19 +00:00
}, timeout * 1000);
2013-09-26 18:29:36 +00:00
};
2013-10-12 23:59:50 +00:00
User.prototype.kick = function (reason) {
2019-08-02 02:49:22 +00:00
LOGGER.info(
'%s (%s) was kicked: "%s"',
this.realip,
this.getName(),
reason
);
2013-10-12 23:59:50 +00:00
this.socket.emit("kick", { reason: reason });
2014-10-26 03:49:22 +00:00
this.socket.disconnect();
2013-10-12 23:59:50 +00:00
};
User.prototype.isAnonymous = function(){
var self = this;
return !self.is(Flags.U_LOGGED_IN);
};
User.prototype.initAdminCallbacks = function () {
var self = this;
self.socket.on("borrow-rank", function (rank) {
if (self.inChannel()) {
if (typeof rank !== "number") {
return;
}
2014-05-21 02:30:14 +00:00
if (rank > self.account.globalRank) {
return;
}
2014-05-21 02:30:14 +00:00
if (rank === 255 && self.account.globalRank > 255) {
rank = self.account.globalRank;
}
2014-05-21 02:30:14 +00:00
self.account.channelRank = rank;
2014-10-10 01:46:16 +00:00
self.account.effectiveRank = rank;
self.socket.emit("rank", rank);
2014-05-21 02:30:14 +00:00
self.channel.broadcastAll("setUserRank", {
name: self.getName(),
rank: rank
});
}
});
};
2014-02-16 19:27:01 +00:00
User.prototype.login = function (name, pw) {
var self = this;
2014-05-21 02:30:14 +00:00
self.setFlag(Flags.U_LOGGING_IN);
2014-02-16 19:27:01 +00:00
db.users.verifyLogin(name, pw, function (err, user) {
if (err) {
if (err === "Invalid username/password combination") {
Logger.eventlog.log("[loginfail] Login failed (bad password): " + name
+ "@" + self.realip);
2014-02-16 19:27:01 +00:00
}
self.socket.emit("login", {
success: false,
error: err
});
2014-07-02 04:09:20 +00:00
self.clearFlag(Flags.U_LOGGING_IN);
2014-02-16 19:27:01 +00:00
return;
}
const oldRank = self.account.effectiveRank;
self.account.user = user;
self.account.update();
self.socket.emit("rank", self.account.effectiveRank);
self.emit("effectiveRankChange", self.account.effectiveRank, oldRank);
2016-08-11 05:20:53 +00:00
self.registrationTime = new Date(user.time);
2014-05-21 02:30:14 +00:00
self.setFlag(Flags.U_REGISTERED);
self.socket.emit("login", {
success: true,
name: user.name
2014-05-21 02:30:14 +00:00
});
db.recordVisit(self.realip, self.getName());
2017-04-05 06:02:31 +00:00
LOGGER.info(self.realip + " logged in as " + user.name);
self.setFlag(Flags.U_LOGGED_IN);
self.clearFlag(Flags.U_LOGGING_IN);
self.emit("login", self.account);
2014-02-16 19:27:01 +00:00
});
};
2013-05-03 03:13:46 +00:00
var lastguestlogin = {};
2013-09-26 18:18:37 +00:00
User.prototype.guestLogin = function (name) {
2013-08-17 20:54:23 +00:00
var self = this;
2013-09-26 18:18:37 +00:00
if (self.realip in lastguestlogin) {
var diff = (Date.now() - lastguestlogin[self.realip]) / 1000;
2014-01-22 23:11:26 +00:00
if (diff < Config.get("guest-login-delay")) {
2013-08-17 20:54:23 +00:00
self.socket.emit("login", {
success: false,
error: "Guest logins are restricted to one per IP address per " +
2014-01-22 23:11:26 +00:00
Config.get("guest-login-delay") + " seconds."
2013-08-17 20:54:23 +00:00
});
return;
2013-08-17 20:54:23 +00:00
}
2013-09-26 18:18:37 +00:00
}
2013-08-17 20:54:23 +00:00
if (!util.isValidUserName(name)) {
2013-09-26 18:18:37 +00:00
self.socket.emit("login", {
success: false,
error: "Invalid username. Usernames must be 1-20 characters long and " +
2014-05-01 00:36:01 +00:00
"consist only of characters a-z, A-Z, 0-9, -, or _."
2013-09-26 18:18:37 +00:00
});
return;
}
2013-08-17 20:54:23 +00:00
if (name.match(Config.get("reserved-names.usernames"))) {
LOGGER.warn(
'Rejecting attempt by %s to use reserved username "%s"',
self.realip,
name
);
self.socket.emit("login", {
success: false,
error: "That username is reserved."
});
return;
}
// Prevent duplicate logins
2014-05-21 02:30:14 +00:00
self.setFlag(Flags.U_LOGGING_IN);
db.users.isUsernameTaken(name, function (err, taken) {
2014-05-21 02:30:14 +00:00
self.clearFlag(Flags.U_LOGGING_IN);
2013-09-26 18:29:36 +00:00
if (err) {
2013-09-26 18:18:37 +00:00
self.socket.emit("login", {
success: false,
error: err
2013-09-26 18:18:37 +00:00
});
return;
}
2013-08-17 20:54:23 +00:00
2013-09-26 18:29:36 +00:00
if (taken) {
2013-08-17 20:54:23 +00:00
self.socket.emit("login", {
2013-09-26 18:18:37 +00:00
success: false,
error: "That username is registered."
2013-02-16 05:02:42 +00:00
});
2013-09-26 18:18:37 +00:00
return;
}
2013-09-26 18:29:36 +00:00
if (self.inChannel()) {
var nameLower = name.toLowerCase();
for (var i = 0; i < self.channel.users.length; i++) {
2014-05-21 02:30:14 +00:00
if (self.channel.users[i].getLowerName() === nameLower) {
2013-09-26 18:18:37 +00:00
self.socket.emit("login", {
success: false,
error: "That name is already in use on this channel."
2013-09-26 18:18:37 +00:00
});
return;
}
2013-06-19 21:54:27 +00:00
}
2013-09-26 18:18:37 +00:00
}
// Login succeeded
lastguestlogin[self.realip] = Date.now();
const oldRank = self.account.effectiveRank;
self.account.guestName = name;
self.account.update();
self.socket.emit("rank", self.account.effectiveRank);
self.emit("effectiveRankChange", self.account.effectiveRank, oldRank);
self.socket.emit("login", {
success: true,
name: name,
guest: true
2014-05-21 02:30:14 +00:00
});
db.recordVisit(self.realip, self.getName());
2017-04-05 06:02:31 +00:00
LOGGER.info(self.realip + " signed in as " + name);
self.setFlag(Flags.U_LOGGED_IN);
self.emit("login", self.account);
});
2013-09-26 18:29:36 +00:00
};
2013-02-16 05:02:42 +00:00
/* Clean out old login throttlers to save memory */
setInterval(function () {
var delay = Config.get("guest-login-delay");
for (var ip in lastguestlogin) {
var diff = (Date.now() - lastguestlogin[ip]) / 1000;
if (diff > delay) {
delete lastguestlogin[ip];
}
}
if (Config.get("aggressive-gc") && global && global.gc) {
global.gc();
}
}, 5 * 60 * 1000);
User.prototype.getFirstSeenTime = function getFirstSeenTime() {
2017-08-02 02:29:11 +00:00
if (this.registrationTime && this.socket.context.ipSessionFirstSeen) {
return Math.min(
this.registrationTime.getTime(),
this.socket.context.ipSessionFirstSeen.getTime()
);
} else if (this.registrationTime) {
return this.registrationTime.getTime();
2017-08-02 02:29:11 +00:00
} else if (this.socket.context.ipSessionFirstSeen) {
return this.socket.context.ipSessionFirstSeen.getTime();
} else {
2017-04-05 06:02:31 +00:00
LOGGER.error(`User "${this.getName()}" (IP: ${this.realip}) has neither ` +
"an IP session first seen time nor a registered account.");
return Date.now();
}
};
User.prototype.setChannelRank = function setRank(rank) {
const oldRank = this.account.effectiveRank;
const changed = oldRank !== rank;
this.account.channelRank = rank;
this.account.update();
this.socket.emit("rank", this.account.effectiveRank);
if (changed) {
this.emit("effectiveRankChange", this.account.effectiveRank, oldRank);
}
};
2013-07-16 03:01:12 +00:00
module.exports = User;