Advertisement
WavePlayz

Be wss issue

May 9th, 2022
3,549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const WebSocket = require("ws")
  3. const HTTPS = require("https")
  4. const UUID = require("uuid")
  5.  
  6. const { exec } = require("child_process")
  7.  
  8. const XBLAPI = require("./xbl-web-api.js")
  9.  
  10. function getRequest (url, callback, onError) {
  11.     HTTPS.get(url, response => {
  12.         const chunks = []
  13.        
  14.         response.on( "data", chunk => chunks.push(chunk) )
  15.  
  16.         response.on( "end", () => {
  17.             let dataString = Buffer.concat(chunks).toString()
  18.             let data = JSON.parse( dataString )
  19.            
  20.             callback.call( response, data )
  21.         })
  22.        
  23.     })//.on( "error", onError ?? (()=>{}) )
  24. }
  25.  
  26. XBLAPI.get = getRequest
  27.  
  28.  
  29. function shell(command, callback, verbose = false) {
  30.     return exec(command, (error, stdout, stderr) => {
  31.         callback?.(command)
  32.        
  33.         if (!verbose) return;
  34.        
  35.         if (error) {
  36.             console.log(`error: ${error.message}`)
  37.         }
  38.         else if (stderr) {
  39.             console.log(`stderr: ${stderr}`)
  40.         }
  41.         else {
  42.             console.log(`stdout: ${stdout}`)
  43.         }
  44.     })
  45. }
  46.  
  47.  
  48.  
  49. function style(style, text) {
  50.     let styles = style.replace(".",";")
  51.     return "\x1b[" + styles + "m" + text + "\x1b[0m"
  52. }
  53.  
  54. String.prototype.style = function(value) {
  55.     return style(value, this.toString())
  56. }
  57.  
  58. function createPayload (type, purpose, body) {
  59.     let uuid = UUID.v4()
  60.    
  61.     return JSON.stringify({ value: {
  62.         "header": {
  63.             "version": 1,
  64.             "requestId": uuid,
  65.             "messageType": type,
  66.             "messagePurpose": purpose
  67.         },
  68.         "body": body
  69.     }, uuid })
  70. }
  71.  
  72. function createSubscribePayload (event) {
  73.     return createPayload( "commandRequest", "subscribe", {
  74.         "eventName": event
  75.     })
  76. }
  77.  
  78. function createCommamdPayload (command) {
  79.     return createPayload( "commandRequest", "commandRequest", {
  80.         "version": 1,
  81.         "origin": { "type": "player" },
  82.         "commandLine": command,
  83.         "overworld": "default"
  84.     })
  85. }
  86.  
  87.  
  88. const EVENTS = require("./events.json")
  89.  
  90. // Creating a new websocket server
  91. const PORT = 8080
  92. const wss = new WebSocket.Server({ port: PORT })
  93.  
  94.  
  95. WebSocket.prototype.__subscribe = function(event) {
  96.     return this.send( createSubscribePayload( event ) )
  97. }
  98.  
  99. WebSocket.prototype.__runCommand = function(command, callback) {
  100.     let { value, uuid } = createCommamdPayload( command )
  101.     callback?.(value)
  102.    
  103.     return this.send( value )
  104. }
  105.  
  106. // Creating connection using websocket
  107. wss.on( "connection", ws => {
  108.     try{
  109.     onConnect.call(ws)
  110.    
  111.     /*
  112.     EVENTS.forEach( event => {
  113.         if (event.startsWith("_")) return;
  114.        
  115.         console.log(event)
  116.        
  117.         ws.__subscribe( event)
  118.     })
  119.     */
  120.    
  121.     ws.__subscribe("PlayerMessage")
  122.    
  123.     // sending message
  124.     ws.on( "message", onMessage.bind(ws) )
  125.    
  126.     // handling what to do when clients disconnects from server
  127.     ws.on( "close", onDisconnect.bind(ws) )
  128.    
  129.     // handling client connection error
  130.     ws.on( "error", onError.bind(ws) )
  131.     } catch(e) {console.log(e)}
  132. })
  133.  
  134.  
  135. const address = `ws:\/\/localhost:` + PORT
  136. const command = "/connect " + address
  137.  
  138. console.log( "WebSocket Server - running at".style("1") )
  139. console.log( `:: ${address}`.style("35") )
  140. console.log( `:: /connect ${command}`.style("34") )
  141.  
  142. /*
  143. shell("termux-vibrate -fd 100")
  144. shell("termux-clipboard-set " + command, () => {
  145.     shell("termux-toast -b black -g top command copied to ur clipboard:\n" + command)
  146.     shell("am start --user 0 -n com.mojang.minecraftpe/.MainActivity")
  147. })*/
  148.  
  149.  
  150. function onConnect(ws) {
  151.     //this.terminate()//(0,'test')
  152.     this.send( createCommamdPayload( `/say connexted`).value )
  153.     console.log( ":: [Client Connected]".style("32.1") )
  154.    
  155. }
  156.  
  157. function onDisconnect() {
  158.     console.log(":: [Client Diconnected]".style("31.1") )
  159. }
  160.  
  161. function onError(error) {
  162.     console.log("Some Error occurred " + error)
  163. }
  164.  
  165. let pending = []
  166.  
  167. function onMessage(raw) {
  168.     console.log(String(raw))
  169.     this.send("ok")
  170.     return
  171.     const data = JSON.parse(raw)
  172.    
  173.     const { header, body } = data
  174.    
  175.    
  176.    
  177.     //const { sender, message } = body
  178.    
  179.     console.log(`:: [${header.eventName}]`.style("36"))
  180.     /*
  181.     XBLAPI.fetch("checkByTag", nameTag, data => {
  182.         const { available } = data
  183.         console.log(available)
  184.     })
  185.    
  186.     console.log(`<${sender.style("33")}> ${message}`)
  187.     */
  188.    
  189.     let rawdata = "rawdata " + JSON.stringify(data, null, 2)
  190.    
  191.     let rawtext = JSON.stringify({ rawtext: [ { text: rawdata } ] })
  192.    
  193.     console.log(rawdata)
  194.    
  195.    
  196.     let ws = this
  197.    
  198.     if (message == "tp") {
  199.         let n = 0
  200.         let e = setInterval(() => {
  201.             ws.__runCommand( `tp ^ ^ ^0.05` )
  202.             ws.__runCommand( `title @s actionbar ${n}` )
  203.            
  204.             if (n++ > 5*20) clearInterval(e);
  205.         },50)
  206.     }
  207.    
  208.    
  209.    
  210.     if (
  211.         (header.messagePurpose == "event" && body.sender === "External") ||
  212.         (header.messagePurpose != "event" && pending.includes(header.requestId) )
  213.     ) return;
  214.     this.__runCommand( `tellraw @s ${rawtext}`, e => {
  215.         pending.push(e.uuid)
  216.     } )
  217.    
  218. }
  219.  
  220.  
  221.  
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement