Upload 3 files
Browse files- package-lock.json +36 -0
- package.json +11 -0
- serveur.js +91 -0
package-lock.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "mon-chat-websocket",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"lockfileVersion": 3,
|
| 5 |
+
"requires": true,
|
| 6 |
+
"packages": {
|
| 7 |
+
"": {
|
| 8 |
+
"name": "mon-chat-websocket",
|
| 9 |
+
"version": "1.0.0",
|
| 10 |
+
"dependencies": {
|
| 11 |
+
"ws": "^8.21.0"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"node_modules/ws": {
|
| 15 |
+
"version": "8.21.0",
|
| 16 |
+
"resolved": "https://registry.npmjs.org/ws/-/ws-8.21.0.tgz",
|
| 17 |
+
"integrity": "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==",
|
| 18 |
+
"license": "MIT",
|
| 19 |
+
"engines": {
|
| 20 |
+
"node": ">=10.0.0"
|
| 21 |
+
},
|
| 22 |
+
"peerDependencies": {
|
| 23 |
+
"bufferutil": "^4.0.1",
|
| 24 |
+
"utf-8-validate": ">=5.0.2"
|
| 25 |
+
},
|
| 26 |
+
"peerDependenciesMeta": {
|
| 27 |
+
"bufferutil": {
|
| 28 |
+
"optional": true
|
| 29 |
+
},
|
| 30 |
+
"utf-8-validate": {
|
| 31 |
+
"optional": true
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
}
|
package.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "mon-chat-websocket",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"main": "serveur.js",
|
| 5 |
+
"scripts": {
|
| 6 |
+
"start": "node serveur.js"
|
| 7 |
+
},
|
| 8 |
+
"dependencies": {
|
| 9 |
+
"ws": "^8.21.0"
|
| 10 |
+
}
|
| 11 |
+
}
|
serveur.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { WebSocketServer } = require('ws');
|
| 2 |
+
const fs = require('fs');
|
| 3 |
+
const path = require('path');
|
| 4 |
+
|
| 5 |
+
// Les hébergeurs en ligne imposent leur propre PORT via la variable d'environnement process.env.PORT.
|
| 6 |
+
// Si aucune n'est fournie, il écoutera sur le port 7860.
|
| 7 |
+
const PORT = process.env.PORT || 7860;
|
| 8 |
+
|
| 9 |
+
// En ligne, on écoute sur '0.0.0.0' pour accepter toutes les connexions entrantes (Internet)
|
| 10 |
+
const wss = new WebSocketServer({ port: PORT, host: '0.0.0.0' });
|
| 11 |
+
|
| 12 |
+
// Dossier où stocker les utilisateurs en persistance locale
|
| 13 |
+
const DOSSIER_USER = path.join(__dirname, 'user');
|
| 14 |
+
|
| 15 |
+
// Crée le dossier 'user' automatiquement s'il n'existe pas encore
|
| 16 |
+
if (!fs.existsSync(DOSSIER_USER)) {
|
| 17 |
+
fs.mkdirSync(DOSSIER_USER);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
console.log("=== SERVEUR DE CHAT PRODUCTION EN LIGNE ===");
|
| 21 |
+
console.log(`Serveur WebSocket actif sur le port ${PORT}...\n`);
|
| 22 |
+
|
| 23 |
+
wss.on('connection', (ws) => {
|
| 24 |
+
console.log("[CONNEXION] Un client distant s'est connecté.");
|
| 25 |
+
|
| 26 |
+
ws.on('message', (message) => {
|
| 27 |
+
const texteRecu = message.toString();
|
| 28 |
+
|
| 29 |
+
try {
|
| 30 |
+
const donnees = JSON.parse(texteRecu);
|
| 31 |
+
|
| 32 |
+
// --- CAS 1 : ENREGISTREMENT EN LIGNE ---
|
| 33 |
+
if (donnees.type === "enregistrement" && donnees.email && donnees.pseudo) {
|
| 34 |
+
const contenuFichier = `pseudo: ${donnees.pseudo}\nemail: ${donnees.email}`;
|
| 35 |
+
|
| 36 |
+
// Sécurisation du nom de fichier
|
| 37 |
+
const nomFichier = `${donnees.email.replace(/[^a-zA-Z0-9@.]/g, "_")}.txt`;
|
| 38 |
+
const cheminFichier = path.join(DOSSIER_USER, nomFichier);
|
| 39 |
+
|
| 40 |
+
// On écrit le fichier sur le disque du serveur en ligne
|
| 41 |
+
fs.writeFileSync(cheminFichier, contenuFichier, 'utf8');
|
| 42 |
+
console.log(`[PROD-USER] Nouvel enregistrement : user/${nomFichier}`);
|
| 43 |
+
|
| 44 |
+
ws.send(JSON.stringify({ type: "reponse_enregistrement", statut: "succes" }));
|
| 45 |
+
return;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// --- CAS 2 : COMPARAISON / VÉRIFICATION EN LIGNE ---
|
| 49 |
+
if (donnees.type === "comparer" && donnees.email && donnees.pseudo) {
|
| 50 |
+
const nomFichier = `${donnees.email.replace(/[^a-zA-Z0-9@.]/g, "_")}.txt`;
|
| 51 |
+
const cheminFichier = path.join(DOSSIER_USER, nomFichier);
|
| 52 |
+
|
| 53 |
+
if (fs.existsSync(cheminFichier)) {
|
| 54 |
+
const contenu = fs.readFileSync(cheminFichier, 'utf8');
|
| 55 |
+
const contenuAttendu = `pseudo: ${donnees.pseudo}\nemail: ${donnees.email}`;
|
| 56 |
+
|
| 57 |
+
if (contentsMatch(contenu, contenuAttendu)) {
|
| 58 |
+
console.log(`[PROD-AUTH] Succès pour : ${donnees.email}`);
|
| 59 |
+
ws.send(JSON.stringify({ type: "reponse_comparer", valide: true, message: "Infos correctes" }));
|
| 60 |
+
} else {
|
| 61 |
+
console.log(`[PROD-AUTH] Échec (Pseudo invalide) pour : ${donnees.email}`);
|
| 62 |
+
ws.send(JSON.stringify({ type: "reponse_comparer", valide: false, message: "Pseudo incorrect" }));
|
| 63 |
+
}
|
| 64 |
+
} else {
|
| 65 |
+
console.log(`[PROD-AUTH] Échec (Inconnu) pour : ${donnees.email}`);
|
| 66 |
+
ws.send(JSON.stringify({ type: "reponse_comparer", valide: false, message: "Utilisateur inconnu" }));
|
| 67 |
+
}
|
| 68 |
+
return;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
} catch (e) {
|
| 72 |
+
// Pas du JSON -> Chat classique
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// --- CODE DE CHAT CLASSIQUE ---
|
| 76 |
+
wss.clients.forEach((client) => {
|
| 77 |
+
if (client.readyState === 1) {
|
| 78 |
+
client.send(texteRecu);
|
| 79 |
+
}
|
| 80 |
+
});
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
ws.on('close', () => {
|
| 84 |
+
console.log("[DÉCONNEXION] Un client s'est déconnecté.");
|
| 85 |
+
});
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
// Fonction utilitaire pour nettoyer et comparer les textes sans être bloqué par des retours à la ligne cachés (\r)
|
| 89 |
+
function contentsMatch(str1, str2) {
|
| 90 |
+
return str1.replace(/\r/g, '').trim() === str2.replace(/\r/g, '').trim();
|
| 91 |
+
}
|