mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2025-09-02 00:31:27 +03:00
Settings server
This commit is contained in:
@@ -40,10 +40,6 @@ module.exports = new Logger({
|
||||
type: 'info',
|
||||
format: '&7[{timestamp}]&r &3[INFO] &d(COMMANDS)&r {text}'
|
||||
},
|
||||
http: {
|
||||
type: 'info',
|
||||
format: '&7[{timestamp}]&r &3[INFO] &d(HTTP)&r {text}'
|
||||
},
|
||||
plugins: {
|
||||
type: 'info',
|
||||
format: '&7[{timestamp}]&r &3[INFO] &d(PLUGINS)&r {text}'
|
||||
@@ -51,6 +47,14 @@ module.exports = new Logger({
|
||||
tickets: {
|
||||
type: 'info',
|
||||
format: '&7[{timestamp}]&r &3[INFO] &d(TICKETS)&r {text}'
|
||||
},
|
||||
http: {
|
||||
type: 'info',
|
||||
format: '&7[{timestamp}]&r &3[INFO] &d(HTTP)&r {text}'
|
||||
},
|
||||
ws: {
|
||||
type: 'info',
|
||||
format: '&7[{timestamp}]&r &3[INFO] &d(WS)&r {text}'
|
||||
}
|
||||
}
|
||||
});
|
@@ -34,7 +34,7 @@ module.exports = class PluginManager {
|
||||
* @param {Plugin} Main The Plugin class
|
||||
* @param {Object} pkg Contents of package.json
|
||||
*/
|
||||
registerPlugin(npm, Main, pkg) {
|
||||
register(npm, Main, pkg) {
|
||||
let {
|
||||
name: id,
|
||||
version,
|
||||
@@ -85,7 +85,7 @@ module.exports = class PluginManager {
|
||||
try {
|
||||
let main = require(plugin);
|
||||
let pkg = require(`${plugin}/package.json`);
|
||||
this.registerPlugin(true, main, pkg);
|
||||
this.register(true, main, pkg);
|
||||
} catch (e) {
|
||||
this.client.log.warn(`An error occurred whilst loading ${plugin}; have you installed it?`);
|
||||
this.client.log.error(e);
|
||||
@@ -99,7 +99,7 @@ module.exports = class PluginManager {
|
||||
if (!fs.existsSync(path(`./user/plugins/${dir}/package.json`))) return;
|
||||
let pkg = require(`../../../user/plugins/${dir}/package.json`);
|
||||
let main = require(join(`../../../user/plugins/${dir}/`, pkg.main));
|
||||
this.registerPlugin(false, main, pkg);
|
||||
this.register(false, main, pkg);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,8 @@
|
||||
const { Plugin } = require('../modules/plugins');
|
||||
const fastify = require('fastify');
|
||||
const session = require('fastify-secure-session');
|
||||
const { randomBytes } = require('crypto');
|
||||
const fs = require('fs');
|
||||
const { path } = require('../utils/fs');
|
||||
|
||||
module.exports = class SettingsServer extends Plugin {
|
||||
@@ -19,39 +21,59 @@ module.exports = class SettingsServer extends Plugin {
|
||||
}
|
||||
|
||||
async preload() {
|
||||
this.fastify.register(this.client.log.fastify, {
|
||||
this.fastify.register(this.client.log.fastify(), {
|
||||
level: 'http',
|
||||
format: '{method} {status-colour}{status} &7{route} {time-colour}({time})'
|
||||
format: '&lSETTINGS:&r {status-colour}{status}&r {method} &7{route} {time-colour}({time})'
|
||||
});
|
||||
|
||||
this.fastify.register(require('fastify-secure-session'), {
|
||||
key: randomBytes(48).toString('hex')
|
||||
this.fastify.register(session, {
|
||||
secret: randomBytes(48).toString('hex'),
|
||||
salt: randomBytes(8).toString('hex'),
|
||||
cookie: {
|
||||
path: '/',
|
||||
httpOnly: true ,
|
||||
},
|
||||
});
|
||||
|
||||
this.fastify.register(require('fastify-static'), {
|
||||
root: path('./src/server/public'),
|
||||
prefix: '/public/',
|
||||
// prefix: '/public/',
|
||||
});
|
||||
}
|
||||
|
||||
async load() {
|
||||
|
||||
let host = process.env.HTTP_HOST;
|
||||
if (!host.endsWith('/')) host = host + '/';
|
||||
this.redirect_uri = encodeURI(`${host}auth/callback`);
|
||||
}
|
||||
|
||||
let redirect_url = encodeURI(`${host}auth/callback`);
|
||||
let oauth2_url = `https://discord.com/api/oauth2/authorize?client_id=${this.client.user.id}&redirect_uri=${redirect_url}&response_type=code&scope=identify%20guilds&state=apollo`;
|
||||
|
||||
this.fastify.get('/', async (req, res) => {
|
||||
// res.type('application/json').code(200);
|
||||
// return { hello: 'world' };
|
||||
res.code(200);
|
||||
return 'Hello!';
|
||||
});
|
||||
async load() {
|
||||
const routes = fs.readdirSync(path('./src/server/routes'))
|
||||
.filter(file => file.endsWith('.js'));
|
||||
for (const r of routes) {
|
||||
const {
|
||||
method,
|
||||
route,
|
||||
execute
|
||||
} = require(`./routes/${r}`);
|
||||
this.fastify[method](route, (...args) => execute(this, ...args));
|
||||
}
|
||||
|
||||
this.fastify.listen(process.env.HTTP_PORT || 8080, (err, host) => {
|
||||
if (err) throw err;
|
||||
this.client.log.info(`Settings server listening at ${host}`);
|
||||
});
|
||||
|
||||
this.io = require('socket.io')(process.env.HTTP_PORT || 8080);
|
||||
|
||||
this.io.on('connection', socket => {
|
||||
const events = fs.readdirSync(path('./src/server/socket'))
|
||||
.filter(file => file.endsWith('.js'));
|
||||
for (const e of events) {
|
||||
const {
|
||||
event,
|
||||
execute
|
||||
} = require(`./socket/${e}`);
|
||||
socket.on(event, (...args) => execute(this, ...args));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>DiscordTickets / Settings</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.1.1/socket.io.min.js'></script>
|
||||
<script>
|
||||
const socket = io('./');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
47
src/server/routes/auth_callback.js
Normal file
47
src/server/routes/auth_callback.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const fetch = require('node-fetch');
|
||||
const FormData = require('form-data');
|
||||
|
||||
module.exports = {
|
||||
method: 'get',
|
||||
route: '/auth/callback',
|
||||
execute: async (plugin, req, res) => {
|
||||
|
||||
if (!req.query.code) {
|
||||
res.status(400);
|
||||
return 'Bad Request: no code';
|
||||
}
|
||||
|
||||
if (req.query.state !== 'apollo') {
|
||||
res.status(400);
|
||||
return 'Bad Request: state mismatch';
|
||||
}
|
||||
|
||||
const data = new FormData();
|
||||
data.append('client_id', plugin.client.user.id);
|
||||
data.append('client_secret', process.env.DISCORD_SECRET);
|
||||
data.append('grant_type', 'authorization_code');
|
||||
data.append('redirect_uri', plugin.redirect_uri);
|
||||
data.append('scope', 'identify guild');
|
||||
data.append('code', req.query.code);
|
||||
|
||||
let {
|
||||
access_token,
|
||||
expires_in,
|
||||
refresh_token
|
||||
} = await (await fetch('https://discordapp.com/api/oauth2/token', {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
})).json();
|
||||
|
||||
expires_in = expires_in * 1000;
|
||||
let expires_at = Date.now() + expires_in;
|
||||
|
||||
req.session.set('access_token', access_token);
|
||||
req.session.set('expires_in', expires_in);
|
||||
req.session.set('expires_at', expires_at);
|
||||
req.session.set('refresh_token', refresh_token);
|
||||
|
||||
res.redirect(307, '/settings');
|
||||
|
||||
}
|
||||
};
|
16
src/server/routes/auth_login.js
Normal file
16
src/server/routes/auth_login.js
Normal file
@@ -0,0 +1,16 @@
|
||||
module.exports = {
|
||||
method: 'get',
|
||||
route: '/auth/login',
|
||||
execute: async (plugin, req, res) => {
|
||||
|
||||
let url = new URL('https://discord.com/api/oauth2/authorize');
|
||||
url.searchParams.append('client_id', plugin.client.user.id);
|
||||
url.searchParams.append('redirect_uri', plugin.redirect_uri);
|
||||
url.searchParams.append('response_type', 'code');
|
||||
url.searchParams.append('scope', 'identify guilds');
|
||||
url.searchParams.append('state', 'apollo');
|
||||
|
||||
res.redirect(307, url);
|
||||
|
||||
}
|
||||
};
|
10
src/server/routes/auth_logout.js
Normal file
10
src/server/routes/auth_logout.js
Normal file
@@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
method: 'get',
|
||||
route: '/auth/logout',
|
||||
execute: async (plugin, req, res) => {
|
||||
|
||||
req.session.delete();
|
||||
res.send('Logged out successfully');
|
||||
|
||||
}
|
||||
};
|
9
src/server/routes/index.js
Normal file
9
src/server/routes/index.js
Normal file
@@ -0,0 +1,9 @@
|
||||
module.exports = {
|
||||
method: 'get',
|
||||
route: '/',
|
||||
execute: async (plugin, req, res) => {
|
||||
|
||||
res.redirect(307, '/auth/login');
|
||||
|
||||
}
|
||||
};
|
23
src/server/routes/settings.js
Normal file
23
src/server/routes/settings.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
module.exports = {
|
||||
method: 'get',
|
||||
route: '/settings',
|
||||
execute: async (plugin, req, res) => {
|
||||
|
||||
let expires_at = req.session.get('expires_at');
|
||||
if (!expires_at) res.redirect(307, '/auth/login');
|
||||
let expired = expires_at < Date.now();
|
||||
if (expired) res.redirect(307, '/auth/login');
|
||||
|
||||
let data = await (await fetch('https://discordapp.com/api/users/@me', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${req.session.get('access_token')}`
|
||||
}
|
||||
})).json();
|
||||
|
||||
console.log(data);
|
||||
|
||||
return `Hello, ${data.username}`;
|
||||
}
|
||||
};
|
6
src/server/socket/connect.js
Normal file
6
src/server/socket/connect.js
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
event: 'connect',
|
||||
execute: (plugin, data) => {
|
||||
plugin.client.log.ws('Client connected to settings socket');
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user