const io = require('socket.io-client'); const crypto = require('crypto'); const os = require('os'); const cpusarray = os.cpus(); let actiontime = 0; let contime = 0; let auftime = 0; let datasintime = 0; let socket; let lastdatesync = new Date(0).toISOString(); let usernumber = '+7 (999) 777-77-77'; let pw = 'somePassword'; let baseref = process.cwd(); let basename = 'push0k client'; let baseid = crypto .createHash('md5') .update(appdirectory) .digest('hex'); baseid = baseid.substring(0, 8) + '-' + baseid.substring(8, 12) + '-' + baseid.substring(12, 16) + '-' + baseid.substring(16, 20) + '-' + baseid.substring(20, 32); let basever = '19.02'; let clientid = crypto .createHash('md5') .update(os.hostname()) .digest('hex'); clientid = clientid.substring(0, 8) + '-' + clientid.substring(8, 12) + '-' + clientid.substring(12, 16) + '-' + clientid.substring(16, 20) + '-' + clientid.substring(20, 32); let syncdata = ''; let syncdatasize = 0; function sha256(p) { const hash = crypto.createHash('sha256'); hash.update(p); return '' + hash.digest('hex'); } function connect() { socket = io('http://yourServer.com:6789', { transports: ['websocket'], timeout: 5000 }); socket.connect(); socket.on('connect', onconnect); socket.on('message', onmessage); actiontime = new Date().getTime(); } function onconnect() { contime = new Date().getTime() - actiontime; usernumber = usernumber.replace(/\D/g, ''); socket .binary(false) .emit( 'message', '{"event":"auf","user":"' + usernumber + '","password":"' + sha256(pw + socket.id) + '","roomsjoin":true,"basename":"' + basename + '","basever":"' + basever + '","baseid":"' + baseid + '","baseref":"' + encodeURIComponent(baseref) + '","osversion":"' + encodeURIComponent(os.release()) + '","appversion":"18.08","clientid":"' + clientid + '","infappview":"","ram":"' + os.totalmem() / 1024 / 1024 + '","proc":"' + encodeURIComponent(cpusarray[0].model) + '","ostype":"' + encodeURIComponent(os.type() + ' ' + os.arch()) + '","compname":"' + encodeURIComponent(os.hostname()) + '"}' ); // Настройки передачи файлов с сервера let filetranfer = msgdata.filetranfer; let filemaxsize = msgdata.filemaxsize; let filepartsize = msgdata.filepartsize; actiontime = new Date().getTime(); } function onmessage(msg) { let msgdata; let mestime = new Date().getTime(); if (typeof msg === 'string') { try { msgdata = JSON.parse(msg); } catch (err) { this.message = err.toString(); return; } } else if (typeof msg === 'object') { msgdata = msg; } if (msgdata.event === 'connected') { auftime = mestime - actiontime; socket .binary(false) .emit( 'message', '{"event":"getData","userid":"' + msgdata.userid + '","id":"' + msgdata.id + '","baseid":"' + baseid + '","clientid":"' + clientid + '","lastdatesinc":"' + lastdatesync + '"}' ); if (msgdata.setpass === 'true') { // временный пароль должен быть изменен пользователем // в модальном диалоге без возможности отказа // openDialogSetNewPassword(); } actiontime = new Date().getTime(); } else if (msgdata.event === 'datasync') { syncdata += msgdata.data; syncdatasize += Buffer.byteLength(msg, 'utf8'); if (msgdata.dataPart < msgdata.partsCount) { return; } datasintime = mestime - actiontime; socket .binary(false) .emit( 'message', '{"event":"dataConfirm","userid":"' + msgdata.userid + '","dataid":"' + msgdata.dataid + '","baseid":"' + baseid + '","contime":' + contime + ',"auftime":' + auftime + ',"datasintime":' + datasintime + ',"datesync":"' + msgdata.datesync + '","datasize":' + syncdatasize + '}' ); contime = 0; datasintime = 0; auftime = 0; syncdatasize = 0; let datasync = JSON.parse(Buffer.from(syncdata, 'base64').toString('utf8')); syncdata = ''; // обработка полученных данных с сервера // datasync.Users массив пользователей // datasync.Rooms массив комнат // datasync.Cons массив подключений пользователей // datasync.joinedRooms массив подключенных комнат // datasync.Mess массив сообщений } }
Source: https://habr.com/ru/post/439414/