From 63924d654d1cc1502b8d1e6707b53f51688024d7 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Fri, 20 Jan 2023 03:00:54 -0500 Subject: [PATCH] Keep building classes --- README.md | 2 - package-lock.json | 27 +++++++++++- package.json | 3 +- src/index.js | 4 +- src/lib/classes/Job.js | 39 ++++++++++++++++- src/lib/classes/RCCService.js | 11 ++--- src/lib/randport.js | 0 src/routes/index.js | 80 ++++++++++++----------------------- 8 files changed, 100 insertions(+), 66 deletions(-) create mode 100644 src/lib/randport.js diff --git a/README.md b/README.md index 74c3dd2..04e76ca 100644 --- a/README.md +++ b/README.md @@ -1,3 +1 @@ # bingle-arbiter - -Run `npm run build`, compiled binaries are in `build/` diff --git a/package-lock.json b/package-lock.json index 6944b7c..9605e32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "chalk": "^4.1.2", "express": "^4.18.2", - "soap": "^1.0.0" + "soap": "^1.0.0", + "wait-port": "^1.0.4" } }, "node_modules/@xmldom/xmldom": { @@ -190,6 +191,14 @@ "node": ">= 0.8" } }, + "node_modules/commander": { + "version": "9.5.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.5.0.tgz", + "integrity": "sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==", + "engines": { + "node": "^12.20.0 || >=14" + } + }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -939,6 +948,22 @@ "node": ">= 0.8" } }, + "node_modules/wait-port": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/wait-port/-/wait-port-1.0.4.tgz", + "integrity": "sha512-w8Ftna3h6XSFWWc2JC5gZEgp64nz8bnaTp5cvzbJSZ53j+omktWTDdwXxEF0jM8YveviLgFWvNGrSvRHnkyHyw==", + "dependencies": { + "chalk": "^4.1.2", + "commander": "^9.3.0", + "debug": "^4.3.4" + }, + "bin": { + "wait-port": "bin/wait-port.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/whatwg-mimetype": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", diff --git a/package.json b/package.json index fba5f60..0ac2d50 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "chalk": "^4.1.2", "express": "^4.18.2", - "soap": "^1.0.0" + "soap": "^1.0.0", + "wait-port": "^1.0.4" } } diff --git a/src/index.js b/src/index.js index 58bf831..8d3b3b3 100644 --- a/src/index.js +++ b/src/index.js @@ -14,8 +14,8 @@ app.use("/render/game", require("./routes/render/game.js")) app.use("*", require("./routes/index.js")) -app.listen(process.env.PORT || 5173, () => { - logger.info(`Listening on http://127.0.0.1:${process.env.PORT || 5173}/`) +app.listen(process.env.PORT || 64989, () => { + logger.info(`Listening on http://127.0.0.1:${process.env.PORT || 64989}/`) }) process.on("uncaughtException", (err) => { diff --git a/src/lib/classes/Job.js b/src/lib/classes/Job.js index c031b11..02cf8bb 100644 --- a/src/lib/classes/Job.js +++ b/src/lib/classes/Job.js @@ -1,8 +1,43 @@ const RCCService = require("./RCCService.js") +const soap = require("soap") class Job extends RCCService { - constructor(id, expirationInSeconds = 10, category = 0, cores = 1) { - super() + constructor(id, port, expirationInSeconds = 10, category = 0, cores = 1) { + super(port) + this.id = id + this.expirationInSeconds = expirationInSeconds + this.category = category + this.cores = cores + } + + async CreateClient() { + this.client = await soap.createClientAsync(__dirname + "/../RCCService.wsdl", {}, `http://127.0.0.1:${this.port}/`) + return this.client + } + + async Open(script) { + if (!this.client) throw new Error("There is no client") + return await this.client.OpenJobExAsync({ + job: { + id: this.id, + expirationInSeconds: this.expirationInSeconds, + category: this.category, + cores: this.cores, + }, + script, + }) + } + + async Close() { + if (!this.client) return true + return await this.client.CloseAllJobsAsync({}) + } + + async RenewLease(expirationInSeconds) { + return await this.client.RenewLeaseAsync({ + jobID: this.id, + expirationInSeconds, + }) } } diff --git a/src/lib/classes/RCCService.js b/src/lib/classes/RCCService.js index cd6101c..d0f652a 100644 --- a/src/lib/classes/RCCService.js +++ b/src/lib/classes/RCCService.js @@ -2,16 +2,17 @@ const EventEmitter = require("events") const child_process = require("child_process") class RCCService extends EventEmitter { - constructor(path = process.env.RCCSERVICE_PATH) { + constructor(port, path = process.env.RCCSERVICE_PATH) { super() this.path = path + this.port = port } - start(port, options = { cwd: this.path }) { + Start(options = { cwd: this.path }) { return new Promise((resolve, reject) => { try { - this.proc = child_process.spawn("wine", ["RCCService.exe", "-console", "-placeid:-1", `-port`, port], options) - this.proc.once("spawn", resolve(this.proc)) + this.proc = child_process.spawn("wine", ["RCCService.exe", "-Console", "-PlaceId:-1", `-Port`, this.port], options) + this.proc.once("spawn", () => resolve(this)) this.proc.once("exit", () => { this.proc = null }) @@ -21,7 +22,7 @@ class RCCService extends EventEmitter { }) } - stop(signal = "SIGTERM") { + Stop(signal = "SIGTERM") { if (!this.proc) throw new Error("Process is not running") return this.proc.kill(signal) } diff --git a/src/lib/randport.js b/src/lib/randport.js new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/index.js b/src/routes/index.js index 97227af..3224405 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,65 +1,39 @@ -const soap = require("soap") -const { readFileSync } = require("fs") +const waitPort = require("wait-port") +const { readFileSync, read } = require("fs") const express = require("express") -const RCCService = require("../lib/classes/RCCService") -const wait = require("../lib/wait") +const Job = require("../lib/classes/Job.js") const app = express.Router() +let tempPort = 64990 + app.all("*", async (request, response) => { - const rcc = new RCCService() - const proc = await rcc.start(64989) - if (proc.exitCode !== null) return response.json(false) + const job = new Job("KILL JACKD", tempPort) + await job.Start() - await wait(1000) + const { open } = await waitPort({ host: "127.0.0.1", port: tempPort, timeout: 5000, output: "silent" }) + if (!open) { + job.Close() + return response.json(false) + } - const jobId = "RenderTest" - const client = await soap.createClientAsync(__dirname + "/../lib/RCCService.wsdl", {}, "http://127.0.0.1:64989/") - const result = await client.OpenJobExAsync({ - job: { - id: jobId, - expirationInSeconds: 10, - category: 0, - cores: 1, - }, - script: { - name: jobId, - script: readFileSync(__dirname + "/../lua/user_headshot.lua", { encoding: "utf-8" }), - arguments: { - LuaValue: [ - { - type: "LUA_TSTRING", - value: jobId, - }, - { - type: "LUA_TSTRING", - value: "Avatar", - }, - { - type: "LUA_TSTRING", - value: "PNG", - }, - { - type: "LUA_TNUMBER", - value: "1920", - }, - { - type: "LUA_TNUMBER", - value: "1920", - }, - { - type: "LUA_TSTRING", - value: "https://economy.ittblox.gay", - }, - { - type: "LUA_TNUMBER", - value: "1", - }, - ], - }, + await job.CreateClient() + const result = await job.Open({ + name: job.id, + script: readFileSync(__dirname + "/../lua/user_headshot.lua", { encoding: "utf-8" }), + arguments: { + LuaValue: [ + { type: "LUA_TSTRING", value: job.id }, + { type: "LUA_TSTRING", value: "RenderUserHeadshot" }, + { type: "LUA_TSTRING", value: "PNG" }, + { type: "LUA_TNUMBER", value: "1920" }, + { type: "LUA_TNUMBER", value: "1920" }, + { type: "LUA_TSTRING", value: "https://sitetest.unexp.xyz" }, + { type: "LUA_TNUMBER", value: "1" }, + ], }, }) + await job.Stop() - rcc.stop() return response.end(Buffer.from(result[0].OpenJobExResult.LuaValue[0].value, "base64")) })