Version 0.2.0 - Ajout du serveur Web et de l'Autheification Discord
This commit is contained in:
		@@ -39,9 +39,6 @@ module.exports = {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        embed.addFields({name: "Niveau", value: levelString},{name: "Description", value: desc})
 | 
			
		||||
      
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        const channel = await client.channels.fetch('1102177962817749033')
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,10 @@
 | 
			
		||||
const fs = require("node:fs")
 | 
			
		||||
const path = require("path")
 | 
			
		||||
const { LogType } = require("./modules/sub-log")
 | 
			
		||||
const { DiscordBot } = require("./modules/discord-bot")
 | 
			
		||||
const { __glob } = require("./modules/global-variables")
 | 
			
		||||
const { DiscordBot } = require("./modules/discord-bot")
 | 
			
		||||
const { WebServer } = require("./modules/sub-web")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
setup()
 | 
			
		||||
 | 
			
		||||
@@ -11,12 +13,11 @@ function setup() {
 | 
			
		||||
    //Log - INIT PHASE
 | 
			
		||||
 | 
			
		||||
    const dlog = new LogType("Discord")
 | 
			
		||||
    const wlog = new LogType("Web")
 | 
			
		||||
    const alog = new LogType("Authentification")
 | 
			
		||||
 | 
			
		||||
    // Discord Bot - INIT PHASE
 | 
			
		||||
        
 | 
			
		||||
    const bot = new DiscordBot(getConfig(dlog), dlog)
 | 
			
		||||
    const web = new WebServer()
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,6 @@ const { __glob } = require("./global-variables")
 | 
			
		||||
const { LogType } = require("../modules/sub-log")
 | 
			
		||||
const { List } = require("./sub-list")
 | 
			
		||||
const nodeFinder = require("./nodes-finder")
 | 
			
		||||
const { platform } = require("node:os")
 | 
			
		||||
 | 
			
		||||
const client = new Client({
 | 
			
		||||
    intents:[GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMembers],
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,9 @@ const root = path.resolve(__dirname, '../../')
 | 
			
		||||
 | 
			
		||||
const __glob = {
 | 
			
		||||
    CONFIG:  root + path.sep + "data" + path.sep + "config.json",
 | 
			
		||||
    USERS:  root + path.sep + "data" + path.sep + "users.json",
 | 
			
		||||
    ROOT: root,
 | 
			
		||||
    WEB: root + path.sep + "src" + path.sep + "web",
 | 
			
		||||
    WEB_DIR: root + path.sep + "src" + path.sep + "web",
 | 
			
		||||
    COMMANDS: root + path.sep + "src" + path.sep + "commands",
 | 
			
		||||
    SUBLOG: root + path.sep + "src" + path.sep + "modules" + path.sep + "sub-log.js",
 | 
			
		||||
    SUBPLAYER: root + path.sep + "src" + path.sep + "modules" + path.sep + "sub-player.js",
 | 
			
		||||
@@ -15,5 +16,13 @@ const __glob = {
 | 
			
		||||
    NODES:  root + path.sep + "data" + path.sep + "nodes.json",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = { __glob };
 | 
			
		||||
const webroot = __glob.WEB_DIR + path.sep 
 | 
			
		||||
const __web = {
 | 
			
		||||
 | 
			
		||||
    ROUTER: webroot + "routes" + path.sep,
 | 
			
		||||
    PUBLIC: webroot + "public",
 | 
			
		||||
    TEMPLATES: webroot + "templates"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = { __glob, __web };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										181
									
								
								src/modules/sub-auth.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								src/modules/sub-auth.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,181 @@
 | 
			
		||||
const { resolve } = require("path");
 | 
			
		||||
const { __glob } = require("../modules/global-variables");
 | 
			
		||||
const { LogType } = require('./sub-log');
 | 
			
		||||
const fs = require("fs")
 | 
			
		||||
 | 
			
		||||
const alog = new LogType("Authentification")
 | 
			
		||||
 | 
			
		||||
var users = new Map()
 | 
			
		||||
var sessions = new Array()
 | 
			
		||||
 | 
			
		||||
updateUsers()
 | 
			
		||||
 | 
			
		||||
module.exports.checkUser = function (token) {
 | 
			
		||||
    
 | 
			
		||||
    if(users.has(token)) {
 | 
			
		||||
 | 
			
		||||
        return true
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        return false
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.getSession = function (session) {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if(sessions.includes(session)) {
 | 
			
		||||
    
 | 
			
		||||
        return true
 | 
			
		||||
       
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        return false
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.getDiscordUser = function (code, session) {
 | 
			
		||||
 | 
			
		||||
    return new Promise((resolve, reject) => {
 | 
			
		||||
 | 
			
		||||
        alog.log("Récupération de l'autorisation de récupération des informations de l'utilisateur associé à la session : " + session + "[ETAPE 2]")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            var link = "https://subsonics.raphix.fr" 
 | 
			
		||||
 | 
			
		||||
            if(process.env.DEV == "true") {
 | 
			
		||||
                link = "http://localhost:3001"
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            const params = new URLSearchParams();
 | 
			
		||||
            params.append('client_id', "1094727789682380922");
 | 
			
		||||
            params.append('client_secret', "uwtyPOPKCgw6ciBs20qiJ7LJrW9Ziclo");
 | 
			
		||||
            params.append('grant_type', 'authorization_code');
 | 
			
		||||
            params.append('code', code);
 | 
			
		||||
            params.append('redirect_uri',  link + "/internal/redirect");
 | 
			
		||||
            params.append('scope', 'identify guilds');
 | 
			
		||||
 | 
			
		||||
            fetch('https://discord.com/api/oauth2/token', {
 | 
			
		||||
                method: "POST",
 | 
			
		||||
                headers: {
 | 
			
		||||
                    'Content-Type': 'application/x-www-form-urlencoded'
 | 
			
		||||
                }, body : params
 | 
			
		||||
            }).then(resp1 => resp1.json()).then(authorizationKey => {
 | 
			
		||||
 | 
			
		||||
                alog.log("Récupération des informations de l'utilisateur associé à l'autorisation : '" + authorizationKey.access_token + "' et associé à la session : " + session + "[ETAPE 3]")
 | 
			
		||||
 | 
			
		||||
                fetch('https://discord.com/api/users/@me/guilds/137291455336022018/member', {
 | 
			
		||||
                    headers: {
 | 
			
		||||
                        authorization: `${authorizationKey.token_type} ${authorizationKey.access_token}`,
 | 
			
		||||
                    },
 | 
			
		||||
                }).then(resp2 => resp2.json()).then(userInfo => {
 | 
			
		||||
 | 
			
		||||
                    var user = {}
 | 
			
		||||
 | 
			
		||||
                    user.auth = authorizationKey
 | 
			
		||||
                    Object.assign(user, userInfo)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                    resolve(user)
 | 
			
		||||
                    
 | 
			
		||||
 | 
			
		||||
                }).catch(error => reject(error)) 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            }).catch(error => reject(error))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.saveSession = function (session) {
 | 
			
		||||
 | 
			
		||||
    sessions.push(session)
 | 
			
		||||
    alog.log("Nouvelle session enregistré : " + session)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.removeSession = function (session) {
 | 
			
		||||
 | 
			
		||||
    const index = sessions.indexOf(session)
 | 
			
		||||
    sessions.splice(index, 1)
 | 
			
		||||
    alog.log("Supression de la session : " + session)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports.getUser = function (token) {
 | 
			
		||||
    return users.get(token)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports.addUser = function (user) {
 | 
			
		||||
 | 
			
		||||
    if(!fs.existsSync(__glob.USERS)){
 | 
			
		||||
 | 
			
		||||
        fs.writeFileSync(__glob.USERS, '[]')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    const userDB = JSON.parse(fs.readFileSync(__glob.USERS))
 | 
			
		||||
    userDB.push(user)
 | 
			
		||||
    fs.writeFileSync(__glob.USERS, JSON.stringify(userDB, null, 2))
 | 
			
		||||
 | 
			
		||||
    updateUsers()
 | 
			
		||||
    alog.log("Ajout de " + user.user.username + " en tant qu'utilisateur avec le token : " + user.token)
 | 
			
		||||
 | 
			
		||||
}  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports.removeUser = function (token) {
 | 
			
		||||
 | 
			
		||||
    if(!fs.existsSync(__glob.USERS)){
 | 
			
		||||
 | 
			
		||||
        fs.writeFileSync(__glob.USERS, '[]')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    const userDB = JSON.parse(fs.readFileSync(__glob.USERS))
 | 
			
		||||
    var selectedUser = null
 | 
			
		||||
    for (const user of userDB) {
 | 
			
		||||
        if(user.token == token) {
 | 
			
		||||
 | 
			
		||||
            selectedUser = user
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    const index = userDB.indexOf(selectedUser)
 | 
			
		||||
    alog.log("Supression de " + selectedUser.user.username + " en tant qu'utilisateur avec le token : " + selectedUser.token)
 | 
			
		||||
 | 
			
		||||
    userDB.splice(index, 1)
 | 
			
		||||
    fs.writeFileSync(__glob.USERS, JSON.stringify(userDB, null, 2))
 | 
			
		||||
 | 
			
		||||
    updateUsers()
 | 
			
		||||
    
 | 
			
		||||
}  
 | 
			
		||||
 | 
			
		||||
function updateUsers() {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if(!fs.existsSync(__glob.USERS)){
 | 
			
		||||
 | 
			
		||||
        fs.writeFileSync(__glob.USERS, '[]')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const userDB = JSON.parse(fs.readFileSync(__glob.USERS))
 | 
			
		||||
 | 
			
		||||
    for (const user of userDB) {
 | 
			
		||||
        
 | 
			
		||||
        users.set(user.token, user)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    alog.log("Actualisation de " + userDB.length + " utilisateurs depuis : " + __glob.USERS)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										196
									
								
								src/modules/sub-web.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										196
									
								
								src/modules/sub-web.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,196 @@
 | 
			
		||||
const internal = require("stream");
 | 
			
		||||
const { __glob, __web } = require("../modules/global-variables");
 | 
			
		||||
const { LogType } = require("./sub-log");
 | 
			
		||||
const log = require("./sub-log");
 | 
			
		||||
const auth = require("./sub-auth");
 | 
			
		||||
const cookieParser = require("cookie-parser");
 | 
			
		||||
const wlog = new LogType("Web")
 | 
			
		||||
 | 
			
		||||
module.exports.WebServer = class {
 | 
			
		||||
 | 
			
		||||
    constructor() {
 | 
			
		||||
 | 
			
		||||
        wlog.step.init("start_server", "Démarrage du serveur Express (Web)")
 | 
			
		||||
        init()
 | 
			
		||||
      
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function init() {
 | 
			
		||||
    const createError = require('http-errors');
 | 
			
		||||
    const express = require('express');
 | 
			
		||||
    const path = require('path');
 | 
			
		||||
    const cookieParser = require('cookie-parser');
 | 
			
		||||
    const http = require("http");
 | 
			
		||||
 | 
			
		||||
    const app = express();
 | 
			
		||||
 | 
			
		||||
    const port = normalizePort(process.env.PORT || '3001');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    const server = require('http').createServer(app);
 | 
			
		||||
    const io = require('socket.io')(server)
 | 
			
		||||
 | 
			
		||||
    const indexRouter = require(__web.ROUTER + "index.js");
 | 
			
		||||
    const loginRouter = require(__web.ROUTER + "login.js");
 | 
			
		||||
    const internalRouter = require(__web.ROUTER + "internal.js")
 | 
			
		||||
 | 
			
		||||
    IOConnection(io)
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    app.set('views', __web.TEMPLATES); // general config
 | 
			
		||||
    app.set('view engine', 'ejs');
 | 
			
		||||
    app.use(express.json());
 | 
			
		||||
    app.use(express.urlencoded({ extended: false }));
 | 
			
		||||
    app.use(cookieParser());
 | 
			
		||||
    app.use(express.static(__web.PUBLIC));
 | 
			
		||||
    app.set('port', port);
 | 
			
		||||
    app.use('/', indexRouter);
 | 
			
		||||
    app.use('/login', loginRouter);
 | 
			
		||||
    app.use("/internal", internalRouter)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    app.use(function (req, res, next) {
 | 
			
		||||
        next(createError(404));
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    app.use(function (err, req, res, next) {
 | 
			
		||||
 
 | 
			
		||||
        // Set locals, only providing error
 | 
			
		||||
        // in development
 | 
			
		||||
        res.locals.message = err.message;
 | 
			
		||||
        res.locals.error = req.app.get('env')
 | 
			
		||||
            === 'development' ? err : {};
 | 
			
		||||
     
 | 
			
		||||
        // render the error page
 | 
			
		||||
        res.status(err.status || 500);
 | 
			
		||||
        res.render('error');
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    server.listen(port);
 | 
			
		||||
    server.on('error', onError);
 | 
			
		||||
    server.on('listening', () => {
 | 
			
		||||
 | 
			
		||||
        let addr = server.address();
 | 
			
		||||
        let bind = typeof addr === 'string'
 | 
			
		||||
            ? 'pipe ' + addr
 | 
			
		||||
            : 'port ' + addr.port;
 | 
			
		||||
        wlog.log("Serveur démarré sur le port : " + bind )
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    wlog.step.end("start_server")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EXPRESS FUNCTIONS
 | 
			
		||||
 | 
			
		||||
function normalizePort(val) {
 | 
			
		||||
    let port = parseInt(val, 10);
 | 
			
		||||
 
 | 
			
		||||
    if (isNaN(port)) {
 | 
			
		||||
 
 | 
			
		||||
        // Named pipe
 | 
			
		||||
        return val;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
    if (port >= 0) {
 | 
			
		||||
 
 | 
			
		||||
        // Port number
 | 
			
		||||
        return port;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
// Event listener for HTTP server "error" event
 | 
			
		||||
function onError(error) {
 | 
			
		||||
    if (error.syscall !== 'listen') {
 | 
			
		||||
        throw error;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
    let bind = typeof port === 'string'
 | 
			
		||||
        ? 'Pipe ' + port
 | 
			
		||||
        : 'Port ' + port;
 | 
			
		||||
 
 | 
			
		||||
    // Handle specific listen errors with
 | 
			
		||||
    // friendly messages
 | 
			
		||||
    switch (error.code) {
 | 
			
		||||
        case 'EACCES':
 | 
			
		||||
            console.error(bind
 | 
			
		||||
                + ' requires elevated privileges');
 | 
			
		||||
            process.exit(1);
 | 
			
		||||
            break;
 | 
			
		||||
        case 'EADDRINUSE':
 | 
			
		||||
            console.error(bind + ' is already in use');
 | 
			
		||||
            process.exit(1);
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            throw error;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function IOConnection(io) {
 | 
			
		||||
 | 
			
		||||
    const alog = log.getInstance("Authentification")
 | 
			
		||||
 | 
			
		||||
    io.on("connection", (socket) => {
 | 
			
		||||
 | 
			
		||||
        wlog.log("[SOCKET] - Nouvelle session : " + socket.id)
 | 
			
		||||
 | 
			
		||||
        socket.on("disconnect", () => {
 | 
			
		||||
 | 
			
		||||
            wlog.log("[SOCKET] - Fin de session : " + socket.id)
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        socket.on("GET/DISCORD_LOGIN_LINK", () => {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            var discordlink = null
 | 
			
		||||
 | 
			
		||||
            if(process.env.DEV == "true") {
 | 
			
		||||
 | 
			
		||||
                alog.log("Mode Developpeur Actif : Redirige vers LOCALHOST")
 | 
			
		||||
                discordlink = "https://discord.com/api/oauth2/authorize?client_id=1094727789682380922&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Finternal%2Fredirect&response_type=code&scope=identify%20guilds%20guilds.members.read" //DEV
 | 
			
		||||
            } else {
 | 
			
		||||
                discordlink = "https://discord.com/api/oauth2/authorize?client_id=1094727789682380922&redirect_uri=https%3A%2F%2Fsubsonics.raphix.fr%2Finternal%2Fredirect&response_type=code&scope=identify%20guilds%20guilds.members.read" //OFFICIEL
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            alog.log("Redirection de '" + socket.id + "' vers le service d'Authentification de Discord [ETAPE 1]")
 | 
			
		||||
            io.emit("ANSWER/GET/DISCORD_LOGIN_LINK", discordlink )
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        socket.on("GET/USER_INFO", () => {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            var token = socket.handshake.headers.cookie
 | 
			
		||||
 | 
			
		||||
            if(token) {
 | 
			
		||||
 | 
			
		||||
                token = socket.handshake.headers.cookie.replace("token=", "")
 | 
			
		||||
                 
 | 
			
		||||
                if(auth.checkUser(token)) {
 | 
			
		||||
 | 
			
		||||
                    const user = auth.getUser(token)
 | 
			
		||||
                    alog.log("Envoi des informations Discord de '" + user.user.username + "' à '" + socket.id + "'" )
 | 
			
		||||
                    socket.emit("ANSWER/GET/USER_INFO",user)
 | 
			
		||||
                
 | 
			
		||||
                } else {
 | 
			
		||||
 | 
			
		||||
                    io.emit("ANSWER/GET/USER_INFO", {"error":"USER_DONT_EXIST"})
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                io.emit("ANSWER/GET/USER_INFO", {"error":"TOKEN_NOT_FINDED"})
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
         
 | 
			
		||||
           
 | 
			
		||||
 | 
			
		||||
        })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
   
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								src/web/public/javascript/IO.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/web/public/javascript/IO.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
var socketLink = null
 | 
			
		||||
 | 
			
		||||
fetch('/internal/socketlink', {
 | 
			
		||||
    method: "GET"
 | 
			
		||||
}).then(link => {socketLink = link})
 | 
			
		||||
 | 
			
		||||
const socket = io(socketLink);
 | 
			
		||||
 | 
			
		||||
socket.on("connect", () => {
 | 
			
		||||
    console.log("Connecté au serveur par le Socket avec l'ID : " + socket.id)
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
function get(request) {
 | 
			
		||||
 | 
			
		||||
    return new Promise((resolve, reject) => {
 | 
			
		||||
 | 
			
		||||
        socket.emit("GET/" + request)
 | 
			
		||||
        console.log("Envoi de la requête : " + request)
 | 
			
		||||
        
 | 
			
		||||
        socket.once("ANSWER/GET/" + request, (answer) => {
 | 
			
		||||
            
 | 
			
		||||
            console.log("Réponse pour la requête : " + request)
 | 
			
		||||
            
 | 
			
		||||
            resolve(answer)
 | 
			
		||||
                    
 | 
			
		||||
        })
 | 
			
		||||
        
 | 
			
		||||
    })
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								src/web/public/javascript/__index_script.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/web/public/javascript/__index_script.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
const userInfoDiv = document.getElementById("userInfo")
 | 
			
		||||
 | 
			
		||||
const userInfo = get("USER_INFO")
 | 
			
		||||
 | 
			
		||||
userInfo.then(user => {
 | 
			
		||||
 | 
			
		||||
    userInfoDiv.innerHTML = "<img src='https://cdn.discordapp.com/avatars/" + user.user.id + "/" + user.user.avatar + "'><p>" + user.user.global_name + "</p><p>" + user.user.username + "</p>"
 | 
			
		||||
})
 | 
			
		||||
							
								
								
									
										11
									
								
								src/web/public/javascript/__login_script.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/web/public/javascript/__login_script.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
const loginBtn = document.getElementById("loginBtn")
 | 
			
		||||
 | 
			
		||||
loginBtn.addEventListener("click", () => {
 | 
			
		||||
 | 
			
		||||
    const discordLink = get("DISCORD_LOGIN_LINK")
 | 
			
		||||
    discordLink.then(link => {
 | 
			
		||||
 | 
			
		||||
        window.location.href = link
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
})
 | 
			
		||||
							
								
								
									
										0
									
								
								src/web/public/stylesheets/style.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/web/public/stylesheets/style.css
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										21
									
								
								src/web/routes/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/web/routes/index.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
var express = require('express');
 | 
			
		||||
var router = express.Router();
 | 
			
		||||
var path = require("path")
 | 
			
		||||
var auth = require("../../modules/sub-auth")
 | 
			
		||||
var log = require("../../modules/sub-log")
 | 
			
		||||
var uuid = require("uuid")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
router.get('/', function(req, res, next) {
 | 
			
		||||
    if(!auth.checkUser(req.cookies.token)) {
 | 
			
		||||
 | 
			
		||||
        res.redirect("/login")
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        res.render("index")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
module.exports = router;
 | 
			
		||||
							
								
								
									
										88
									
								
								src/web/routes/internal.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								src/web/routes/internal.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
			
		||||
var express = require('express');
 | 
			
		||||
var router = express.Router();
 | 
			
		||||
var path = require("path")
 | 
			
		||||
var auth = require("../../modules/sub-auth")
 | 
			
		||||
var log = require("../../modules/sub-log")
 | 
			
		||||
var uuid = require("uuid")
 | 
			
		||||
 | 
			
		||||
const wlog = log.getInstance("Web")
 | 
			
		||||
const alog = log.getInstance("Authentification")
 | 
			
		||||
 | 
			
		||||
router.get('/redirect', function(req, res, next) {
 | 
			
		||||
 | 
			
		||||
    if(auth.checkUser(req.cookies.token)) {
 | 
			
		||||
 | 
			
		||||
        res.redirect("/")
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        if(auth.getSession(req.cookies.session)) {
 | 
			
		||||
 | 
			
		||||
            if(req.query.code) {
 | 
			
		||||
 | 
			
		||||
                alog.step.init("get_discord_info_" + req.cookies.session , "Récupération des informations de l'utilisateur associé à l'autorisation : '" + req.cookies.sessionn + "'")
 | 
			
		||||
                
 | 
			
		||||
                const user = auth.getDiscordUser(req.query.code, req.cookies.session)
 | 
			
		||||
 | 
			
		||||
                user.then(data => {
 | 
			
		||||
                    
 | 
			
		||||
                    alog.step.end("get_discord_info_" + req.cookies.session)
 | 
			
		||||
                    
 | 
			
		||||
                    var user = data
 | 
			
		||||
                    const token = uuid.v4().toString()
 | 
			
		||||
 | 
			
		||||
                    user.token = token
 | 
			
		||||
                    auth.addUser(user)
 | 
			
		||||
 | 
			
		||||
                    res.cookie("token", token, { maxAge: 90000000000000, httpOnly: true })
 | 
			
		||||
 | 
			
		||||
                    auth.removeSession(req.cookies.session)
 | 
			
		||||
                    res.clearCookie("session")
 | 
			
		||||
                    res.redirect("/")
 | 
			
		||||
 | 
			
		||||
                }).catch(error => {
 | 
			
		||||
 | 
			
		||||
                    alog.step.error("get_discord_info_" + req.cookies.session)
 | 
			
		||||
                    console.log(error)
 | 
			
		||||
                    res.redirect('/login?error=CANCEL_LOGIN')
 | 
			
		||||
 | 
			
		||||
                })
 | 
			
		||||
             
 | 
			
		||||
             
 | 
			
		||||
            } else {
 | 
			
		||||
 | 
			
		||||
                res.redirect('/login?error=CANCEL_LOGIN')
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
 | 
			
		||||
            res.redirect("/login")
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get("/socketlink", (req,res,next) => {
 | 
			
		||||
        
 | 
			
		||||
    if(process.env.DEV == true) {
 | 
			
		||||
 | 
			
		||||
        res.send("ws://localhost:3001")
 | 
			
		||||
    } else {
 | 
			
		||||
        res.send("ws://subsonics.raphix.fr")
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
router.get("/logout", (req,res,next) => {
 | 
			
		||||
 | 
			
		||||
    if(auth.checkUser(req.cookies.token)) {
 | 
			
		||||
 | 
			
		||||
        auth.removeUser(req.cookies.token)
 | 
			
		||||
        res.clearCookie("token")
 | 
			
		||||
        res.redirect(302, "/login")
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        res.redirect(302, "/login")
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports = router;
 | 
			
		||||
							
								
								
									
										29
									
								
								src/web/routes/login.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/web/routes/login.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
var express = require('express');
 | 
			
		||||
var router = express.Router();
 | 
			
		||||
var path = require("path")
 | 
			
		||||
var auth = require("../../modules/sub-auth")
 | 
			
		||||
var log = require("../../modules/sub-log")
 | 
			
		||||
var uuid = require("uuid")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
router.get('/', function(req, res, next) {
 | 
			
		||||
 | 
			
		||||
    if(auth.checkUser(req.cookies.token)) {
 | 
			
		||||
 | 
			
		||||
        res.redirect("/")
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        if(!auth.getSession(req.cookies.session)) {
 | 
			
		||||
 | 
			
		||||
            const session = uuid.v4().toString()
 | 
			
		||||
            auth.saveSession(session)
 | 
			
		||||
            res.cookie("session", session)
 | 
			
		||||
           
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
        res.render("login")
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
module.exports = router;
 | 
			
		||||
							
								
								
									
										11
									
								
								src/web/templates/error.ejs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/web/templates/error.ejs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
    <title>Subsonics - Error</title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <h1>ERROR !</h1>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										17
									
								
								src/web/templates/index.ejs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/web/templates/index.ejs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
    <link rel='stylesheet' href='/stylesheets/style.css'/>
 | 
			
		||||
    <title>Subsonics - Web</title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <h1>Subsonics</h1>
 | 
			
		||||
    <div id="userInfo"></div>
 | 
			
		||||
    <a href="/internal/logout"><button>Disconnect</button></a>
 | 
			
		||||
    <script src="/socket.io/socket.io.js"></script>
 | 
			
		||||
    <script src="/javascript/IO.js"></script>
 | 
			
		||||
    <script src="/javascript/__index_script.js"></script>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										17
									
								
								src/web/templates/login.ejs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/web/templates/login.ejs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
    <title>Subsonics - Login</title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <h1>Subsonics Login</h1>
 | 
			
		||||
    <button id="loginBtn">Connexion</button>
 | 
			
		||||
    <p>Fait avec le 💖 par Raphix</p>
 | 
			
		||||
    <script src="/socket.io/socket.io.js"></script>
 | 
			
		||||
    <script src="/javascript/IO.js"></script>
 | 
			
		||||
    <script src="/javascript/__login_script.js"></script>
 | 
			
		||||
  
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
		Reference in New Issue
	
	Block a user