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