Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const https = require('https');
- const mysql = require("mysql");
- const fs = require("fs");
- const WebSocket = require('ws');
- const config = require('./config.json');
- const crc = require('crc');
- const http = require('http');
- // Load SSL certificate and key
- const sslOptions = {
- key: fs.readFileSync('/etc/letsencrypt/live/wss.kiwontatv.de/privkey.pem'),
- cert: fs.readFileSync('/etc/letsencrypt/live/wss.kiwontatv.de/fullchain.pem'),
- };
- // ALL THE PEOPLE WE NEED TO SUSTAIN OUR ETERNAL NEEEEEDS
- var people = [];
- function formatTimestamp() {
- const now = new Date();
- const pad = (num) => String(num).padStart(2, '0');
- const day = pad(now.getDate());
- const month = pad(now.getMonth() + 1); // Months are zero-based in JavaScript
- const year = now.getFullYear();
- const hours = pad(now.getHours());
- const minutes = pad(now.getMinutes());
- const seconds = pad(now.getSeconds());
- return `${day}.${month}.${year} ${hours}:${minutes}:${seconds}`;
- }
- function log(d) {
- let ts = formatTimestamp()
- console.log("L: "+ts+ " - "+d)
- }
- // Connect to MySQL
- const connection = mysql.createConnection({
- host: config.mysql.host,
- user: config.mysql.user,
- password: config.mysql.password,
- database: config.mysql.database
- });
- connection.connect(err => {
- if (err) {
- log("Error connecting to MySQL database:", err);
- return;
- }
- log("Connected to MySQL database");
- });
- // Create an HTTPS WSS server
- const server = https.createServer(sslOptions);
- const wss = new WebSocket.Server({ server });
- function getChecksum(ppl) {
- const postData = JSON.stringify({
- key1: 'value1',
- key2: 'value2'
- });
- // Options for the POST request
- const options = {
- hostname: 'teams.modern-gaming.net',
- path: '/pages/board/cardapi.php',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
- }
- };
- // Create the request
- const req = http.request(options, (res) => {
- console.log(`Status Code: ${res.statusCode}`);
- let data = '';
- // A chunk of data has been received.
- res.on('data', (chunk) => {
- data += chunk;
- });
- // The whole response has been received.
- res.on('end', () => {
- console.log('Response from the server:', data);
- });
- });
- // Handle errors
- req.on('error', (error) => {
- console.error('Error making the POST request:', error);
- });
- // Write data to request body
- req.write(postData);
- req.end();
- }
- function authentication(data, client) {
- if(!data.sid || !data.secret || !data.board) {
- log("Authentication to unknown user failed!");
- return;
- }
- const values = [data.sid, data.secret];
- connection.query(`SELECT * FROM user WHERE steamID=? AND secret=?`, values, (err, result) => {
- if (err) {
- log("Error inserting message into database:", err);
- return;
- }
- if (result.length != 1) {
- log("DOUBELING USERROW ERROR ON "+data.sid)
- return
- }
- if(!people[data.sid]) {
- people[data.sid] = {
- sid: data.sid,
- authenticatedAt: Date.now(),
- connections: 1,
- client: [client]
- }
- log(` User ${data.sid} Connected to the WSS for the first time`)
- } else {
- people[data.sid].connections++
- people[data.sid].client.push(client)
- var conns = people[data.sid].connections
- log(` User ${data.sid} has now ${conns} connections to the WSS`)
- console.log(people)
- }
- getChecksum(people[data.sid])
- /*
- const dd = 'your input string';
- const checksum = crc.crc32(dd);
- console.log(`CRC32 Checksum: ${checksum}`);
- */
- });
- }
- // Connection property
- wss.on('connection', ws => {
- ws.on('message', data => {
- var d = JSON.parse(data)
- if(d) {
- log(`Received communicae of type ${d.type}`)
- switch (d.type) {
- case "authentication":
- authentication(d, ws)
- break;
- default:
- break;
- }
- } else {
- log("Received incorrect data: "+data)
- }
- });
- ws.on('close', () => {
- });
- });
- // Start the server
- server.listen(6942, () => {
- log('Server is running on port 443');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement