Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const WebSocket = require("ws")
- const HTTPS = require("https")
- const UUID = require("uuid")
- const { exec } = require("child_process")
- const XBLAPI = require("./xbl-web-api.js")
- function getRequest (url, callback, onError) {
- HTTPS.get(url, response => {
- const chunks = []
- response.on( "data", chunk => chunks.push(chunk) )
- response.on( "end", () => {
- let dataString = Buffer.concat(chunks).toString()
- let data = JSON.parse( dataString )
- callback.call( response, data )
- })
- })//.on( "error", onError ?? (()=>{}) )
- }
- XBLAPI.get = getRequest
- function shell(command, callback, verbose = false) {
- return exec(command, (error, stdout, stderr) => {
- callback?.(command)
- if (!verbose) return;
- if (error) {
- console.log(`error: ${error.message}`)
- }
- else if (stderr) {
- console.log(`stderr: ${stderr}`)
- }
- else {
- console.log(`stdout: ${stdout}`)
- }
- })
- }
- function style(style, text) {
- let styles = style.replace(".",";")
- return "\x1b[" + styles + "m" + text + "\x1b[0m"
- }
- String.prototype.style = function(value) {
- return style(value, this.toString())
- }
- function createPayload (type, purpose, body) {
- let uuid = UUID.v4()
- return JSON.stringify({ value: {
- "header": {
- "version": 1,
- "requestId": uuid,
- "messageType": type,
- "messagePurpose": purpose
- },
- "body": body
- }, uuid })
- }
- function createSubscribePayload (event) {
- return createPayload( "commandRequest", "subscribe", {
- "eventName": event
- })
- }
- function createCommamdPayload (command) {
- return createPayload( "commandRequest", "commandRequest", {
- "version": 1,
- "origin": { "type": "player" },
- "commandLine": command,
- "overworld": "default"
- })
- }
- const EVENTS = require("./events.json")
- // Creating a new websocket server
- const PORT = 8080
- const wss = new WebSocket.Server({ port: PORT })
- WebSocket.prototype.__subscribe = function(event) {
- return this.send( createSubscribePayload( event ) )
- }
- WebSocket.prototype.__runCommand = function(command, callback) {
- let { value, uuid } = createCommamdPayload( command )
- callback?.(value)
- return this.send( value )
- }
- // Creating connection using websocket
- wss.on( "connection", ws => {
- try{
- onConnect.call(ws)
- /*
- EVENTS.forEach( event => {
- if (event.startsWith("_")) return;
- console.log(event)
- ws.__subscribe( event)
- })
- */
- ws.__subscribe("PlayerMessage")
- // sending message
- ws.on( "message", onMessage.bind(ws) )
- // handling what to do when clients disconnects from server
- ws.on( "close", onDisconnect.bind(ws) )
- // handling client connection error
- ws.on( "error", onError.bind(ws) )
- } catch(e) {console.log(e)}
- })
- const address = `ws:\/\/localhost:` + PORT
- const command = "/connect " + address
- console.log( "WebSocket Server - running at".style("1") )
- console.log( `:: ${address}`.style("35") )
- console.log( `:: /connect ${command}`.style("34") )
- /*
- shell("termux-vibrate -fd 100")
- shell("termux-clipboard-set " + command, () => {
- shell("termux-toast -b black -g top command copied to ur clipboard:\n" + command)
- shell("am start --user 0 -n com.mojang.minecraftpe/.MainActivity")
- })*/
- function onConnect(ws) {
- //this.terminate()//(0,'test')
- this.send( createCommamdPayload( `/say connexted`).value )
- console.log( ":: [Client Connected]".style("32.1") )
- }
- function onDisconnect() {
- console.log(":: [Client Diconnected]".style("31.1") )
- }
- function onError(error) {
- console.log("Some Error occurred " + error)
- }
- let pending = []
- function onMessage(raw) {
- console.log(String(raw))
- this.send("ok")
- return
- const data = JSON.parse(raw)
- const { header, body } = data
- //const { sender, message } = body
- console.log(`:: [${header.eventName}]`.style("36"))
- /*
- XBLAPI.fetch("checkByTag", nameTag, data => {
- const { available } = data
- console.log(available)
- })
- console.log(`<${sender.style("33")}> ${message}`)
- */
- let rawdata = "rawdata " + JSON.stringify(data, null, 2)
- let rawtext = JSON.stringify({ rawtext: [ { text: rawdata } ] })
- console.log(rawdata)
- let ws = this
- if (message == "tp") {
- let n = 0
- let e = setInterval(() => {
- ws.__runCommand( `tp ^ ^ ^0.05` )
- ws.__runCommand( `title @s actionbar ${n}` )
- if (n++ > 5*20) clearInterval(e);
- },50)
- }
- if (
- (header.messagePurpose == "event" && body.sender === "External") ||
- (header.messagePurpose != "event" && pending.includes(header.requestId) )
- ) return;
- this.__runCommand( `tellraw @s ${rawtext}`, e => {
- pending.push(e.uuid)
- } )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement