Path: blob/master/node_modules/@bochilteam/scraper/lib/cjs/others/minecraft.js
1126 views
"use strict";1var __importDefault = (this && this.__importDefault) || function (mod) {2return (mod && mod.__esModule) ? mod : { "default": mod };3};4Object.defineProperty(exports, "__esModule", { value: true });5exports.statusJava = exports.statusBedrock = void 0;6// import dgram, { Socket } from 'dgram'7const events_1 = __importDefault(require("events"));8const net_1 = __importDefault(require("net"));9const utils_js_1 = require("../utils.js");10// TODO11async function statusBedrock(ip, port, opts = { timeout: 5 * 1000 }) { }12exports.statusBedrock = statusBedrock;13// TODO: fix connection timeout14function statusJava(ip, port = 25565, opts = { timeout: 5 * 1000 }) {15// eslint-disable-next-line no-async-promise-executor16return new Promise(async (resolve, reject) => {17var _a;18setTimeout(() => reject(new utils_js_1.ScraperError('timeout')), opts.timeout);19// reference https://github.com/PassTheMayo/minecraft-server-util/blob/master/src/status.ts20const socket = new TCPsocket(ip, port, opts, reject);21// https://wiki.vg/Server_List_Ping#Handshake22socket.writeVarInt(0x00);23socket.writeVarInt(47);24socket.writeStringVarInt(ip);25socket.writeUnsignedShort(port);26socket.writeVarInt(1);27await socket.send();28// https://wiki.vg/Server_List_Ping#Request29socket.writeVarInt(0x00);30socket.send();31await socket.waitForResponse();32socket.readVarInt();33const responseId = socket.readVarInt();34// eslint-disable-next-line eqeqeq35if (responseId == -1)36reject(new Error('Premature end of stream'));37// eslint-disable-next-line eqeqeq38if (responseId != 0x00)39reject(new Error('Invalid responseId'));40// https://wiki.vg/Protocol#Response41const response = JSON.parse(socket.readStringVarInt());42// https://wiki.vg/Server_List_Ping#Ping43socket.writeVarInt(0x01);44const ping = Date.now();45socket.writeVarLong(ping);46socket.send();47await socket.waitForResponse();48const pong = Date.now() - ping;49socket.close();50resolve({51ip,52port,53description: response.description.extra.map(({ text }) => text.trim()).join(' ').trim(),54descriptionText: response.description.text.trim(),55players: {56max: response.players.max,57online: response.players.online,58sample: response.players.sample.map(({ name }) => name.trim())59},60version: {61name: response.version.name,62protocol: response.version.protocol63},64favicon: (_a = response.favicon) !== null && _a !== void 0 ? _a : null,65ping: pong,66originalResponse: response67});68});69}70exports.statusJava = statusJava;71// class UDPsocket extends EventEmitter {72// public socket: Socket;73// constructor (public ip: string, public port: number, public opts: object) {74// super()75// this.socket = dgram.createSocket('udp4')76// }77// connect () {78// if (!this.socket) return79// this.socket.bind(this.port, this.ip)80// }81// }82class TCPsocket extends events_1.default {83constructor(ip, port, opts, reject) {84super();85this.ip = ip;86this.port = port;87this.opts = opts;88this.reject = reject;89this.data = Buffer.alloc(0);90this.response = Buffer.alloc(0);91}92connect(opts) {93this.socket = net_1.default.createConnection({94host: this.ip,95port: this.port,96timeout: this.opts.timeout,97...opts98});99this.socket.on('data', (data) => {100this.response = Buffer.concat([this.response, data]);101this.emit('data', data, this.response);102});103this.socket.on('connect', () => {104this.emit('connect');105});106this.socket.on('close', () => {107// this.reject(new ScraperError('Connection closed'));108this.emit('close');109});110this.socket.on('error', () => {111this.reject(new utils_js_1.ScraperError('Connection error'));112this.emit('error');113});114this.socket.on('timeout', () => {115this.reject(new utils_js_1.ScraperError('Connection timeout'));116this.emit('timeout');117});118}119write(data) {120return (this.data = Buffer.concat([this.data, data]));121}122writeVarInt(value, save = true) {123const buffer = Buffer.alloc(5);124let i = 0;125do {126buffer[i++] = value & 0x7f | 0x80;127value >>= 7;128} while (value > 0);129buffer[i - 1] &= 0x7f;130const result = buffer.slice(0, i);131if (save)132this.write(result);133return result;134}135writeVarLong(value) {136const buffer = Buffer.alloc(9);137let i = 0;138do {139buffer[i++] = value & 0x7f | 0x80;140value >>= 7;141} while (value > 0);142buffer[i - 1] &= 0x7f;143const result = buffer.slice(0, i);144this.write(result);145return result;146}147writeStringVarInt(value) {148this.writeVarInt(value.length);149this.write(Buffer.from(value, 'utf8'));150}151writeUnsignedShort(value) {152this.write(Buffer.from([value >> 8, value & 0xff]));153}154readVarInt() {155let result = 0;156let i = 0;157let b;158do {159b = this.response[i++];160result |= (b & 0x7f) << (7 * i);161} while (b & 0x80);162this.response = this.response.slice(i);163return result;164}165readVarLong() {166let result = 0;167let i = 0;168let b;169do {170b = this.response[i++];171result |= (b & 0x7f) << (7 * i);172} while (b & 0x80);173this.response = this.response.slice(i);174return result;175}176readStringVarInt() {177let length = 0;178let i = 0;179let b;180do {181b = this.response[i++];182length |= (b & 0x7f) << (7 * i);183} while (b & 0x80);184// @ts-ignore185const result = this.response.slice(i, i + length).toString('utf8');186this.response = this.response.slice(i + length);187return result;188}189readInt64BE() {190// @ts-ignore191const result = this.response.slice(0, 8).readBigInt64BE(0);192this.response = this.response.slice(8);193return result;194}195send() {196if (!this.socket)197this.connect();198return new Promise((resolve, reject) => {199var _a, _b;200this.response = Buffer.alloc(0);201// https://gist.github.com/zh32/7190955#file-serverlistping17-java-L92202// https://github.com/PassTheMayo/minecraft-server-util/blob/68a7a16beb48226cdd5b63c45604fd3bea6c12ca/src/structure/TCPClient.ts#L464203const data = Buffer.concat([this.writeVarInt(this.data.byteLength, false), this.data]);204(_b = (_a = this.socket) === null || _a === void 0 ? void 0 : _a.write) === null || _b === void 0 ? void 0 : _b.call(_a, data, (err) => {205if (err)206return reject(err);207resolve();208});209this.data = Buffer.alloc(0);210});211}212close() {213var _a, _b, _c, _d, _e, _f;214(_b = (_a = this.socket) === null || _a === void 0 ? void 0 : _a.end) === null || _b === void 0 ? void 0 : _b.call(_a);215(_d = (_c = this.socket) === null || _c === void 0 ? void 0 : _c.destroy) === null || _d === void 0 ? void 0 : _d.call(_c);216(_f = (_e = this.socket) === null || _e === void 0 ? void 0 : _e.removeAllListeners) === null || _f === void 0 ? void 0 : _f.call(_e);217}218waitForResponse() {219return new Promise((resolve) => {220const timeout = setTimeout(resolve, 250);221this.on('data', () => {222// @ts-ignore223timeout.refresh();224});225});226}227}228//# sourceMappingURL=minecraft.js.map229230