|
|
|
@ -28,17 +28,14 @@ exports.WebServer = class {
|
|
|
|
|
this.setReferenceObjects(referenceObjects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: Encrypt the password on client side.
|
|
|
|
|
*/
|
|
|
|
|
configureExpress() {
|
|
|
|
|
this.app.set('view engine', 'pug');
|
|
|
|
|
this.app.set('trust proxy', 1);
|
|
|
|
|
this.app.set('views', './web/http/');
|
|
|
|
|
|
|
|
|
|
if (this.app.get('env') === 'devlopment') {
|
|
|
|
|
if (this.app.get('env') === 'devlopment')
|
|
|
|
|
this.app.use(require('cors')());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.app.use(require('cors')());
|
|
|
|
|
this.app.use(session({
|
|
|
|
|
secret: config.webservice.sessionSecret,
|
|
|
|
@ -52,11 +49,11 @@ exports.WebServer = class {
|
|
|
|
|
|
|
|
|
|
this.app.use(compression({
|
|
|
|
|
filter: (req, res) => {
|
|
|
|
|
if (req.headers['x-no-compression']) {
|
|
|
|
|
if (req.headers['x-no-compression'])
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return compression.filter(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
this.app.use('/graphql', graphqlHTTP({
|
|
|
|
@ -67,22 +64,11 @@ exports.WebServer = class {
|
|
|
|
|
this.app.use(compileSass({
|
|
|
|
|
root: './web/http/'
|
|
|
|
|
}));
|
|
|
|
|
this.app.get('/', (req, res, next) => {
|
|
|
|
|
if (req.session.user) {
|
|
|
|
|
next();
|
|
|
|
|
} else {
|
|
|
|
|
res.render('login');
|
|
|
|
|
}
|
|
|
|
|
}, (req, res) => {
|
|
|
|
|
res.render('index');
|
|
|
|
|
});
|
|
|
|
|
this.app.use('/scripts', express.static('./web/http/scripts'));
|
|
|
|
|
|
|
|
|
|
this.app.post('/', (req, res) => {
|
|
|
|
|
if (!req.body.username || !req.body.password) {
|
|
|
|
|
if (!req.body.username || !req.body.password)
|
|
|
|
|
res.render('login', {msg: 'Please enter username and password.'});
|
|
|
|
|
} else {
|
|
|
|
|
this.maindb.get('SELECT * FROM users WHERE username = ? AND password = ?', [req.body.username, sha512(req.body.password)], (err, user) => {
|
|
|
|
|
else
|
|
|
|
|
this.maindb.get('SELECT * FROM users WHERE username = ? AND password = ?', [req.body.username, req.body.password], (err, user) => {
|
|
|
|
|
if (err || !user) {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.warn(err.message);
|
|
|
|
@ -93,7 +79,15 @@ exports.WebServer = class {
|
|
|
|
|
res.render('index');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.app.use('/scripts', express.static('./web/http/scripts'));
|
|
|
|
|
this.app.use((req, res, next) => {
|
|
|
|
|
if (req.session.user)
|
|
|
|
|
next();
|
|
|
|
|
else
|
|
|
|
|
res.render('login');
|
|
|
|
|
}, (req, res) => {
|
|
|
|
|
res.render('index');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -149,7 +143,7 @@ exports.WebServer = class {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let token = generateUUID(['TOKEN', username]);
|
|
|
|
|
this.maindb.run('INSERT INTO users (username, password, token, scope) VALUES (?, ?, ?, ?)',
|
|
|
|
|
[username, password,token, scope], (err) => {
|
|
|
|
|
[username, password, token, scope], (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
logger.warn(err.message);
|
|
|
|
|
reject(err);
|
|
|
|
@ -160,33 +154,6 @@ exports.WebServer = class {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authenticateByToken(req, res, next) {
|
|
|
|
|
if (req.headers.authorization
|
|
|
|
|
&& req.headers.authorization.split(' ')[0] === 'Bearer') {
|
|
|
|
|
let bearer = req.headers.authorization.split(' ')[1];
|
|
|
|
|
this.maindb.get('SELECT * FROM users WHERE token = ?', [bearer], (err, user) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
logger.warn(err.message);
|
|
|
|
|
logger.debug('Unauthorized access');
|
|
|
|
|
res.status(401);
|
|
|
|
|
res.end('Unauthorized Access');
|
|
|
|
|
} else {
|
|
|
|
|
if (!user) {
|
|
|
|
|
res.status(401);
|
|
|
|
|
res.end('Unauthorized Access');
|
|
|
|
|
} else {
|
|
|
|
|
req.user = user;
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug('Unauthorized access');
|
|
|
|
|
res.status(401);
|
|
|
|
|
res.end('Unauthorized Access');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Setting all objects that web can query
|
|
|
|
|
* @param objects
|
|
|
|
@ -200,19 +167,19 @@ exports.WebServer = class {
|
|
|
|
|
token VARCHAR(255) UNIQUE NOT NULL,
|
|
|
|
|
scope INTEGER NOT NULL DEFAULT 0
|
|
|
|
|
)`, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
this.root = {
|
|
|
|
|
client: {
|
|
|
|
|
guilds: (args) => {
|
|
|
|
|
let dcGuilds = objects.client.guilds.values();
|
|
|
|
|
if (args.id) {
|
|
|
|
|
if (args.id)
|
|
|
|
|
return [Array.from(dcGuilds)
|
|
|
|
|
.map((x) => new Guild(x, objects.getGuildHandler(x)))
|
|
|
|
|
.find(x => (x.id === args.id))];
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
try {
|
|
|
|
|
return Array.from(dcGuilds)
|
|
|
|
|
.slice(args.offset, args.offset + args.first)
|
|
|
|
@ -221,7 +188,7 @@ exports.WebServer = class {
|
|
|
|
|
logger.error(err.stack);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
guildCount: () => {
|
|
|
|
|
return Array.from(objects.client.guilds.values()).length;
|
|
|
|
@ -242,14 +209,14 @@ exports.WebServer = class {
|
|
|
|
|
let dcGuilds = Array.from(objects.client.guilds.values());
|
|
|
|
|
return dcGuilds.filter((x) => {
|
|
|
|
|
let gh = objects.guildHandlers[x.id];
|
|
|
|
|
if (gh) {
|
|
|
|
|
if (gh)
|
|
|
|
|
if (gh.dj)
|
|
|
|
|
return gh.dj.playing;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).length;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
@ -270,18 +237,18 @@ exports.WebServer = class {
|
|
|
|
|
logEntries.push(new LogEntry(JSON.parse(line)));
|
|
|
|
|
});
|
|
|
|
|
lineReader.on('close', () => {
|
|
|
|
|
if (args.level) {
|
|
|
|
|
if (args.level)
|
|
|
|
|
logEntries = logEntries
|
|
|
|
|
.filter(x => (utils.logLevels[x.level] >= utils.logLevels[args.level]));
|
|
|
|
|
}
|
|
|
|
|
if (args.id) {
|
|
|
|
|
|
|
|
|
|
if (args.id)
|
|
|
|
|
logEntries = [logEntries.find(x => (x.id === args.id))];
|
|
|
|
|
}
|
|
|
|
|
if (args.first) {
|
|
|
|
|
|
|
|
|
|
if (args.first)
|
|
|
|
|
logEntries = logEntries.slice(args.offset, args.offset + args.first);
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
logEntries = logEntries.slice(logEntries.length - args.last);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(logEntries);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
@ -291,7 +258,7 @@ exports.WebServer = class {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generating an unique id
|
|
|
|
|
* generating an id
|
|
|
|
|
* @param valArr
|
|
|
|
|
* @returns {*}
|
|
|
|
|
*/
|
|
|
|
@ -305,6 +272,11 @@ function generateID(valArr) {
|
|
|
|
|
return md5(b64);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generating an unique id
|
|
|
|
|
* @param input
|
|
|
|
|
* @returns {*}
|
|
|
|
|
*/
|
|
|
|
|
function generateUUID(input) {
|
|
|
|
|
return generateID([input, (new Date()).getMilliseconds()]) + Date.now();
|
|
|
|
|
}
|
|
|
|
@ -327,11 +299,11 @@ class DJ {
|
|
|
|
|
thumbnail: utils.YouTube.getVideoThumbnailUrlFromUrl(x.url)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
if (args.id) {
|
|
|
|
|
if (args.id)
|
|
|
|
|
return [queue.find(x => (x.id === args.id))];
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return queue.slice(args.offset, args.offset + args.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get playing() {
|
|
|
|
@ -377,6 +349,9 @@ class DJ {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for graphql access to the discord.js Guild and lib/guilding/GuildHandler
|
|
|
|
|
*/
|
|
|
|
|
class Guild {
|
|
|
|
|
constructor(discordGuild, guildHandler) {
|
|
|
|
|
this.id = generateID(['Guild', discordGuild.id]);
|
|
|
|
@ -405,14 +380,14 @@ class Guild {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
resolve(null);
|
|
|
|
|
} else {
|
|
|
|
|
for (let row of rows) {
|
|
|
|
|
for (let row of rows)
|
|
|
|
|
saved.push({
|
|
|
|
|
id: generateID(['Media', row.url]),
|
|
|
|
|
name: row.name,
|
|
|
|
|
url: row.url,
|
|
|
|
|
thumbnail: utils.YouTube.getVideoThumbnailUrlFromUrl(row.url)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(saved);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -425,7 +400,7 @@ class Guild {
|
|
|
|
|
saved(args) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.querySaved().then((result) => {
|
|
|
|
|
if (result) {
|
|
|
|
|
if (result)
|
|
|
|
|
if (args.id) {
|
|
|
|
|
resolve([result.find(x => (x.id === args.id))]);
|
|
|
|
|
} else if (args.name) {
|
|
|
|
@ -433,31 +408,34 @@ class Guild {
|
|
|
|
|
} else {
|
|
|
|
|
resolve(result.slice(args.offset, args.offset + args.first));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
resolve(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roles(args) {
|
|
|
|
|
if (args.id) {
|
|
|
|
|
if (args.id)
|
|
|
|
|
return [this.prRoles.find(x => (x.id === args.id))];
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return this.prRoles.slice(args.offset, args.offset + args.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
members(args) {
|
|
|
|
|
if (args.id) {
|
|
|
|
|
if (args.id)
|
|
|
|
|
return [this.prMembers.find(x => (x.id === args.id))];
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return this.prMembers.slice(args.offset, args.offset + args.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for graphql access to the discord.js Role
|
|
|
|
|
*/
|
|
|
|
|
class Role {
|
|
|
|
|
constructor(discordRole) {
|
|
|
|
|
this.id = generateID(['Role', discordRole.id]);
|
|
|
|
@ -469,14 +447,17 @@ class Role {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
members(args) {
|
|
|
|
|
if (args.id) {
|
|
|
|
|
if (args.id)
|
|
|
|
|
return [this.prMembers.find(x => (x.id === args.id))];
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return this.prMembers.slice(args.offset, args.offset + args.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for graphql access to the discord.js GuildMember
|
|
|
|
|
*/
|
|
|
|
|
class GuildMember {
|
|
|
|
|
constructor(discordGuildMember) {
|
|
|
|
|
this.id = generateID(['GuildMember', discordGuildMember.id]);
|
|
|
|
@ -489,14 +470,17 @@ class GuildMember {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roles(args) {
|
|
|
|
|
if (args.id) {
|
|
|
|
|
if (args.id)
|
|
|
|
|
return [this.prRoles.find(x => (x.id === args.id))];
|
|
|
|
|
} else {
|
|
|
|
|
else
|
|
|
|
|
return this.prRoles.slice(args.offset, args.offset + args.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for graphql access to the discord.js User
|
|
|
|
|
*/
|
|
|
|
|
class User {
|
|
|
|
|
constructor(discordUser) {
|
|
|
|
|
this.id = generateID(['User', discordUser.id]);
|
|
|
|
@ -513,6 +497,9 @@ class User {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for graphql access to log entries
|
|
|
|
|
*/
|
|
|
|
|
class LogEntry {
|
|
|
|
|
constructor(entry) {
|
|
|
|
|
this.id = generateID(['LogEntry', entry.level, entry.timestamp]);
|