Advertisement
KiwontaTv

kek

May 25th, 2024
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const https = require('https');
  2. const mysql = require("mysql");
  3. const fs = require("fs");
  4. const WebSocket = require('ws');
  5. const config = require('./config.json');
  6. const crc = require('crc');
  7. const http = require('http');
  8.  
  9. // Load SSL certificate and key
  10. const sslOptions = {
  11.     key: fs.readFileSync('/etc/letsencrypt/live/wss.kiwontatv.de/privkey.pem'),
  12.     cert: fs.readFileSync('/etc/letsencrypt/live/wss.kiwontatv.de/fullchain.pem'),
  13. };
  14.  
  15. // ALL THE PEOPLE WE NEED TO SUSTAIN OUR ETERNAL NEEEEEDS
  16. var people = [];
  17.  
  18. function formatTimestamp() {
  19.     const now = new Date();
  20.  
  21.     const pad = (num) => String(num).padStart(2, '0');
  22.  
  23.     const day = pad(now.getDate());
  24.     const month = pad(now.getMonth() + 1); // Months are zero-based in JavaScript
  25.     const year = now.getFullYear();
  26.  
  27.     const hours = pad(now.getHours());
  28.     const minutes = pad(now.getMinutes());
  29.     const seconds = pad(now.getSeconds());
  30.  
  31.     return `${day}.${month}.${year} ${hours}:${minutes}:${seconds}`;
  32. }
  33.  
  34. function log(d) {
  35.     let ts = formatTimestamp()
  36.     console.log("L: "+ts+ " - "+d)
  37. }
  38.  
  39. // Connect to MySQL
  40. const connection = mysql.createConnection({
  41.     host: config.mysql.host,
  42.     user: config.mysql.user,
  43.     password: config.mysql.password,
  44.     database: config.mysql.database
  45. });
  46.  
  47. connection.connect(err => {
  48.     if (err) {
  49.         log("Error connecting to MySQL database:", err);
  50.         return;
  51.     }
  52.     log("Connected to MySQL database");
  53. });
  54.  
  55. // Create an HTTPS WSS server
  56. const server = https.createServer(sslOptions);
  57. const wss = new WebSocket.Server({ server });
  58.  
  59. function getChecksum(ppl) {
  60.     const postData = JSON.stringify({
  61.         key1: 'value1',
  62.         key2: 'value2'
  63.     });
  64.  
  65.     // Options for the POST request
  66.     const options = {
  67.         hostname: 'teams.modern-gaming.net',
  68.         path: '/pages/board/cardapi.php',
  69.         method: 'POST',
  70.         headers: {
  71.             'Content-Type': 'application/json',
  72.             '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'
  73.         }
  74.     };
  75.  
  76.     // Create the request
  77.     const req = http.request(options, (res) => {
  78.         console.log(`Status Code: ${res.statusCode}`);
  79.  
  80.         let data = '';
  81.  
  82.         // A chunk of data has been received.
  83.         res.on('data', (chunk) => {
  84.             data += chunk;
  85.         });
  86.  
  87.         // The whole response has been received.
  88.         res.on('end', () => {
  89.             console.log('Response from the server:', data);
  90.         });
  91.     });
  92.  
  93.     // Handle errors
  94.     req.on('error', (error) => {
  95.         console.error('Error making the POST request:', error);
  96.     });
  97.  
  98.     // Write data to request body
  99.     req.write(postData);
  100.     req.end();
  101. }
  102.  
  103. function authentication(data, client) {
  104.     if(!data.sid || !data.secret || !data.board) {
  105.         log("Authentication to unknown user failed!");
  106.         return;
  107.     }
  108.  
  109.     const values = [data.sid, data.secret];
  110.     connection.query(`SELECT * FROM user WHERE steamID=? AND secret=?`, values, (err, result) => {
  111.         if (err) {
  112.             log("Error inserting message into database:", err);
  113.             return;
  114.         }
  115.         if (result.length != 1) {
  116.             log("DOUBELING USERROW ERROR ON "+data.sid)
  117.             return
  118.         }
  119.  
  120.         if(!people[data.sid]) {
  121.             people[data.sid] = {
  122.                 sid: data.sid,
  123.                 authenticatedAt: Date.now(),
  124.                 connections: 1,
  125.                 client: [client]
  126.             }
  127.             log(`    User ${data.sid} Connected to the WSS for the first time`)
  128.         } else {
  129.             people[data.sid].connections++
  130.             people[data.sid].client.push(client)
  131.             var conns = people[data.sid].connections
  132.            
  133.             log(`    User ${data.sid} has now ${conns} connections to the WSS`)
  134.             console.log(people)
  135.         }
  136.  
  137.         getChecksum(people[data.sid])
  138.  
  139.        
  140. /*
  141.         const dd = 'your input string';
  142.         const checksum = crc.crc32(dd);
  143.  
  144.         console.log(`CRC32 Checksum: ${checksum}`);
  145. */
  146.     });
  147. }
  148.  
  149. // Connection property
  150. wss.on('connection', ws => {
  151.     ws.on('message', data => {
  152.         var d = JSON.parse(data)
  153.         if(d) {
  154.             log(`Received communicae of type ${d.type}`)
  155.             switch (d.type) {
  156.                 case "authentication":
  157.                     authentication(d, ws)
  158.                     break;
  159.            
  160.                 default:
  161.                     break;
  162.             }
  163.         } else {
  164.             log("Received incorrect data: "+data)
  165.         }
  166.     });
  167.  
  168.     ws.on('close', () => {
  169.     });
  170. });
  171.  
  172. // Start the server
  173. server.listen(6942, () => {
  174.     log('Server is running on port 443');
  175. });
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement