Version 0.2.0 - Ajout du serveur Web et de l'Autheification Discord

This commit is contained in:
CICD - Pipeline 2023-08-22 17:44:21 +02:00
parent 99ce148521
commit baa74459a1
20 changed files with 902 additions and 12 deletions

View File

@ -4,5 +4,17 @@
"password": "DevamOP",
"port": 443,
"secure": true
},
{
"host": "suki.nathan.to",
"password": "adowbongmanacc",
"port": 443,
"secure": true
},
{
"host": "ssl.horizxon.studio",
"password": "horizxon.studio",
"port": 443,
"secure": true
}
]

39
data/users.json Normal file
View File

@ -0,0 +1,39 @@
[
{
"auth": {
"token_type": "Bearer",
"access_token": "jY17lCWaOpTdPQCnz50kfNm7ABpsGz",
"expires_in": 604800,
"refresh_token": "3TronknZ7j5kVSDBjI3OechmCYF8ck",
"scope": "identify guilds.members.read guilds"
},
"avatar": null,
"communication_disabled_until": null,
"flags": 0,
"joined_at": "2023-03-06T18:20:23.413000+00:00",
"nick": "Raphix",
"pending": false,
"premium_since": null,
"roles": [
"397725552968204288"
],
"user": {
"id": "486943594893017119",
"username": "raphixscrap",
"avatar": "7129e80c23aa3ba3d2d45814dd352a29",
"discriminator": "0",
"public_flags": 4194368,
"flags": 4194368,
"banner": null,
"accent_color": 10712214,
"global_name": "Raphix",
"avatar_decoration_data": null,
"banner_color": "#a37496"
},
"mute": false,
"deaf": false,
"bio": "",
"banner": null,
"token": "0e33a0fe-0248-47a4-949d-1c4d37b2c443"
}
]

227
package-lock.json generated
View File

@ -1,15 +1,16 @@
{
"name": "subsonics-web",
"version": "0.1.0",
"version": "0.2.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "subsonics-web",
"version": "0.1.0",
"version": "0.2.0",
"dependencies": {
"cookie-parser": "^1.4.6",
"discord.js": "^14.9.0",
"ejs": "^3.1.9",
"erela.js": "^2.4.0",
"express": "^4.18.2",
"nodemon": "^2.0.22",
@ -192,6 +193,20 @@
"node": ">= 0.6"
}
},
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/anymatch": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz",
@ -209,6 +224,11 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
},
"node_modules/async": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz",
"integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ=="
},
"node_modules/axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
@ -312,6 +332,40 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/chalk/node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"engines": {
"node": ">=8"
}
},
"node_modules/chalk/node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/chokidar": {
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",
@ -338,6 +392,22 @@
"fsevents": "~2.3.2"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
},
"node_modules/concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
@ -458,6 +528,20 @@
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
},
"node_modules/ejs": {
"version": "3.1.9",
"resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.9.tgz",
"integrity": "sha512-rC+QVNMJWv+MtPgkt0y+0rVEIdbtxVADApW9JXrUVlzHetgcyczP/E7DJmWJ4fJCZF2cPcBk0laWO9ZHMG3DmQ==",
"dependencies": {
"jake": "^10.8.5"
},
"bin": {
"ejs": "bin/cli.js"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/encodeurl": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
@ -632,6 +716,33 @@
"url": "https://github.com/sindresorhus/file-type?sponsor=1"
}
},
"node_modules/filelist": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz",
"integrity": "sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==",
"dependencies": {
"minimatch": "^5.0.1"
}
},
"node_modules/filelist/node_modules/brace-expansion": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
"dependencies": {
"balanced-match": "^1.0.0"
}
},
"node_modules/filelist/node_modules/minimatch": {
"version": "5.1.6",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz",
"integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==",
"dependencies": {
"brace-expansion": "^2.0.1"
},
"engines": {
"node": ">=10"
}
},
"node_modules/fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -880,6 +991,23 @@
"node": ">=0.12.0"
}
},
"node_modules/jake": {
"version": "10.8.7",
"resolved": "https://registry.npmjs.org/jake/-/jake-10.8.7.tgz",
"integrity": "sha512-ZDi3aP+fG/LchyBzUM804VjddnwfSfsdeYkwt8NcbKRvo4rFkjhs456iLFn3k2ZUWvNe4i48WACDbza8fhq2+w==",
"dependencies": {
"async": "^3.2.3",
"chalk": "^4.0.2",
"filelist": "^1.0.4",
"minimatch": "^3.1.2"
},
"bin": {
"jake": "bin/cli.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
@ -1731,6 +1859,14 @@
"negotiator": "0.6.3"
}
},
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"requires": {
"color-convert": "^2.0.1"
}
},
"anymatch": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz",
@ -1745,6 +1881,11 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
},
"async": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz",
"integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ=="
},
"axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
@ -1826,6 +1967,30 @@
"get-intrinsic": "^1.0.2"
}
},
"chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"dependencies": {
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"requires": {
"has-flag": "^4.0.0"
}
}
}
},
"chokidar": {
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",
@ -1841,6 +2006,19 @@
"readdirp": "~3.6.0"
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"requires": {
"color-name": "~1.1.4"
}
},
"color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
},
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
@ -1936,6 +2114,14 @@
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
},
"ejs": {
"version": "3.1.9",
"resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.9.tgz",
"integrity": "sha512-rC+QVNMJWv+MtPgkt0y+0rVEIdbtxVADApW9JXrUVlzHetgcyczP/E7DJmWJ4fJCZF2cPcBk0laWO9ZHMG3DmQ==",
"requires": {
"jake": "^10.8.5"
}
},
"encodeurl": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
@ -2065,6 +2251,32 @@
"token-types": "^5.0.1"
}
},
"filelist": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz",
"integrity": "sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==",
"requires": {
"minimatch": "^5.0.1"
},
"dependencies": {
"brace-expansion": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
"requires": {
"balanced-match": "^1.0.0"
}
},
"minimatch": {
"version": "5.1.6",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz",
"integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==",
"requires": {
"brace-expansion": "^2.0.1"
}
}
}
},
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -2221,6 +2433,17 @@
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng=="
},
"jake": {
"version": "10.8.7",
"resolved": "https://registry.npmjs.org/jake/-/jake-10.8.7.tgz",
"integrity": "sha512-ZDi3aP+fG/LchyBzUM804VjddnwfSfsdeYkwt8NcbKRvo4rFkjhs456iLFn3k2ZUWvNe4i48WACDbza8fhq2+w==",
"requires": {
"async": "^3.2.3",
"chalk": "^4.0.2",
"filelist": "^1.0.4",
"minimatch": "^3.1.2"
}
},
"lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",

View File

@ -1,7 +1,7 @@
{
"name": "subsonics-web",
"author": "Raphix",
"version": "0.1.1",
"version": "0.2.0",
"nodemonConfig": {
"ext": "js, html",
"ignore": [
@ -14,6 +14,7 @@
"dependencies": {
"cookie-parser": "^1.4.6",
"discord.js": "^14.9.0",
"ejs": "^3.1.9",
"erela.js": "^2.4.0",
"express": "^4.18.2",
"nodemon": "^2.0.22",

View File

@ -40,9 +40,6 @@ module.exports = {
embed.addFields({name: "Niveau", value: levelString},{name: "Description", value: desc})
const channel = await client.channels.fetch('1102177962817749033')
channel.send({embeds: [embed]})

View File

@ -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()
}

View File

@ -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],

View File

@ -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
View 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
View 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"})
}
})
})
}

View 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)
})
})
}

View 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>"
})

View 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
})
})

View File

21
src/web/routes/index.js Normal file
View 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;

View 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
View 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;

View 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>

View 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>

View 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>