Skip to content

Commit d2094bf

Browse files
committed
Use net-browserify Proxy in game
1 parent 630daf8 commit d2094bf

File tree

10 files changed

+265
-328
lines changed

10 files changed

+265
-328
lines changed

index.js

Lines changed: 3 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
1-
const version = "1.16.5";
21
const opn = require("open");
32
const express = require("express");
43
const app = express();
5-
const server = require("http").createServer(app);
6-
const mineflayer = require("mineflayer");
7-
const Chunk = require("prismarine-chunk")(version);
8-
const vec3 = require("vec3");
9-
const Convert = require("ansi-to-html");
10-
const convert = new Convert();
114
const helmet = require("helmet");
125
const compression = require("compression");
13-
const WebSocket = require("ws");
14-
const { encode, decode } = require("@msgpack/msgpack");
156
const port = process.env.PORT || 8080;
16-
7+
var netApi = require("net-browserify");
178
app.use(
189
helmet({
1910
contentSecurityPolicy: false,
2011
})
2112
);
13+
app.use(netApi({ allowOrigin: "*" }));
2214
app.use(compression());
2315

24-
const wss = new WebSocket.Server({
25-
server,
26-
clientTracking: false,
27-
});
28-
2916
const mode = process.argv[2];
3017
if (mode === "production") {
3118
app.use(express.static(`${__dirname}/src/dist`));
@@ -38,206 +25,7 @@ if (mode === "production") {
3825
} else {
3926
console.log("Incorrect mode!");
4027
}
41-
server.listen(port, function () {
28+
app.listen(port, function () {
4229
opn(`http://localhost:${port}`);
4330
return console.log(`Server is running on \x1b[34m*:${port}\x1b[0m`);
4431
});
45-
46-
const botByNick = new Map();
47-
48-
wss.on("connection", (socket, req) => {
49-
const query = new URLSearchParams(req.url.substr(2, req.url.length));
50-
const emit = (type, ...data) => {
51-
socket.send(encode([type, ...data]));
52-
};
53-
54-
if (botByNick.get(query.get("nick")) !== undefined) {
55-
emit("alreadyPlaying");
56-
return;
57-
}
58-
console.log(`[\x1b[32m+\x1b[0m] ${query.get("nick")}`);
59-
let heldItem = null;
60-
const bot = mineflayer.createBot({
61-
host: query.get("server"),
62-
port: query.get("port") !== "null" ? query.get("port") : null,
63-
username: query.get("nick"),
64-
version: version,
65-
password:
66-
query.get("premium") === "true" ? query.get("password") : undefined,
67-
});
68-
botByNick.set(query.get("nick"), bot);
69-
bot._client.on("map_chunk", function (packet) {
70-
const cell = new Chunk();
71-
cell.load(packet.chunkData, packet.bitMap, true, true);
72-
emit("mapChunk", cell.sections, packet.x, packet.z);
73-
});
74-
bot._client.on("respawn", function (packet) {
75-
emit("dimension", packet.dimension.value.effects.value);
76-
});
77-
bot.on("heldItemChanged", function (item) {
78-
heldItem = item;
79-
});
80-
bot.on("login", function () {
81-
emit("dimension", bot.game.dimension);
82-
});
83-
bot.on("move", function () {
84-
emit("move", bot.entity.position);
85-
});
86-
bot.on("health", function () {
87-
emit("hp", bot.health);
88-
emit("food", bot.food);
89-
});
90-
bot.on("spawn", function () {
91-
emit("spawn", bot.entity.yaw, bot.entity.pitch);
92-
});
93-
bot.on("kicked", function (reason) {
94-
emit("kicked", reason);
95-
});
96-
bot.on("message", function (msg) {
97-
let message = msg.toAnsi();
98-
99-
const replacements = [
100-
[/&/g, "&"],
101-
[/</g, "&lt;"],
102-
[/>/g, "&gt;"],
103-
[/"/g, "&quot;"],
104-
];
105-
for (const replacement of replacements)
106-
message = message.replace(replacement[0], replacement[1]);
107-
108-
emit("msg", convert.toHtml(message));
109-
});
110-
bot.on("experience", function () {
111-
emit("xp", bot.experience);
112-
});
113-
bot.on("blockUpdate", function (oldb, newb) {
114-
emit("blockUpdate", [
115-
newb.position.x,
116-
newb.position.y,
117-
newb.position.z,
118-
newb.stateId,
119-
]);
120-
});
121-
bot.on("diggingCompleted", function (block) {
122-
emit("diggingCompleted", block);
123-
});
124-
bot.on("diggingAborted", function (block) {
125-
emit("diggingAborted", block);
126-
});
127-
bot.on("game", function () {
128-
emit("game", bot.game);
129-
});
130-
let inv = "";
131-
const interval = setInterval(function () {
132-
const inv_new = JSON.stringify(bot.inventory.slots);
133-
if (inv !== inv_new) {
134-
inv = inv_new;
135-
emit("inventory", bot.inventory.slots);
136-
}
137-
let entities = {
138-
mobs: [],
139-
players: [],
140-
};
141-
for (let k in bot.entities) {
142-
const v = bot.entities[k];
143-
if (v.type === "mob") {
144-
entities.mobs.push([v.position.x, v.position.y, v.position.z]);
145-
}
146-
if (v.type === "player") {
147-
entities.players.push([
148-
v.username,
149-
v.position.x,
150-
v.position.y,
151-
v.position.z,
152-
]);
153-
}
154-
}
155-
emit("entities", entities);
156-
}, 100);
157-
158-
const handlers = new Map();
159-
160-
bot.once("spawn", function () {
161-
handlers.set("fly", function (toggle) {
162-
if (toggle) {
163-
bot.creative.startFlying();
164-
} else {
165-
bot.creative.stopFlying();
166-
}
167-
});
168-
handlers.set("blockPlace", function (pos, vec) {
169-
const block = bot.blockAt(new vec3(...pos));
170-
if (heldItem !== void 0 && heldItem !== null) {
171-
console.log(heldItem);
172-
bot.placeBlock(block, new vec3(...vec), function (r) {
173-
console.log(r);
174-
});
175-
}
176-
});
177-
handlers.set("invc", function (num) {
178-
const item = bot.inventory.slots[num + 36];
179-
if (item !== null && item !== void 0) {
180-
bot.equip(item, "hand");
181-
} else if (heldItem !== void 0) {
182-
bot.unequip("hand");
183-
}
184-
});
185-
handlers.set("move", function (state, toggle) {
186-
if (state === "right") {
187-
state = "left";
188-
} else if (state === "left") {
189-
state = "right";
190-
}
191-
bot.setControlState(state, toggle);
192-
});
193-
handlers.set("command", function (com) {
194-
bot.chat(com);
195-
});
196-
handlers.set("rotate", function (data) {
197-
bot.look(...data);
198-
});
199-
handlers.set("dig", function (pos) {
200-
const block = bot.blockAt(vec3(pos[0], pos[1] - 16, pos[2]));
201-
if (block !== null) {
202-
const digTime = bot.digTime(block);
203-
if (bot.targetDigBlock !== null) {
204-
console.log("Already digging...");
205-
bot.stopDigging();
206-
}
207-
emit("digTime", digTime, block);
208-
console.log("Start");
209-
bot.dig(block, false, function (xd) {
210-
if (xd === void 0) {
211-
return console.log("SUCCESS");
212-
} else {
213-
return console.log("FAIL");
214-
}
215-
});
216-
}
217-
});
218-
handlers.set("stopDigging", function () {
219-
bot.stopDigging();
220-
});
221-
222-
socket.on("close", () => {
223-
try {
224-
clearInterval(interval);
225-
console.log(`[\x1b[31m-\x1b[0m] ${query.get("nick")}`);
226-
botByNick.delete(query.get("nick"));
227-
bot.end();
228-
} catch (error) {}
229-
});
230-
231-
socket.on("message", (message) => {
232-
try {
233-
const [type, ...data] = decode(message);
234-
235-
const handler = handlers.get(type);
236-
237-
handler(...data);
238-
} catch (err) {
239-
console.log(err);
240-
}
241-
});
242-
});
243-
});

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"jquery": "^3.5.1",
4848
"lodash-webpack-plugin": "^0.11.6",
4949
"mineflayer": "^3.0.0",
50+
"net-browserify": "michaljaz/net-browserify",
5051
"node": "^14.15.2",
5152
"open": "^7.4.2",
5253
"popper.js": "^1.16.1",

src/scripts/Setup.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import { PlayerInInventory } from "./gui/PlayerInInventory.js";
1313
import { BlockBreak } from "./rendering/BlockBreak.js";
1414
import { BlockPlace } from "./rendering/BlockPlace.js";
1515
import { EventHandler } from "./EventHandler.js";
16-
import { Socket } from "./Socket.js";
17-
import proxy from "./proxy/proxy.worker.js";
18-
16+
import { Socket } from "./proxy/Socket.js";
1917
async function Setup(game) {
2018
return new Promise((resolve) => {
2119
game.canvas = document.querySelector("#c");
@@ -37,21 +35,11 @@ async function Setup(game) {
3735
game.stats.showPanel(0);
3836
document.body.appendChild(game.stats.dom);
3937
game.distanceBasedFog = new DistanceBasedFog(game);
40-
game.proxy = new proxy();
4138
UrlParams(game).then((password) => {
39+
game.password = password;
4240
$(".loadingText").text(`Connecting to ${game.server}...`);
4341
console.warn(gpuInfo());
44-
let prefix;
45-
if (document.location.protocol === "https:") {
46-
prefix = "wss";
47-
} else {
48-
prefix = "ws";
49-
}
50-
game.socket = new Socket(
51-
game,
52-
`${prefix}://${document.location.host}?nick=${game.nick}&server=${game.server}&port=${game.serverPort}&password=${password}&premium=${game.premium}`
53-
);
54-
42+
game.socket = new Socket(game);
5543
game.pii = new PlayerInInventory(game);
5644
game.bb = new BlockBreak(game);
5745
game.bp = new BlockPlace(game);
@@ -91,11 +79,8 @@ async function Setup(game) {
9179
.easing(TWEEN.Easing.Quadratic.Out)
9280
.start();
9381
};
94-
95-
game.socket.ws.onopen = () => {
96-
game.eh = new EventHandler(game);
97-
resolve();
98-
};
82+
game.eh = new EventHandler(game);
83+
resolve();
9984
});
10085
});
10186
}

src/scripts/Socket.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)