forked from T0T0W/AOR-Extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
184 lines (151 loc) · 5.1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const express = require("express");
const ejs = require("ejs");
const path = require("path");
const PhotonParser = require("./scripts/classes/PhotonPacketParser");
var Cap = require("cap").Cap;
var decoders = require("cap").decoders;
const WebSocket = require("ws");
const fs = require("fs");
const { getAdapterIp } = require("./server-scripts/adapter-selector");
const app = express();
BigInt.prototype.toJSON = function () {
return this.toString();
};
app.use(express.static(__dirname + "/views"));
app.set("view engine", "ejs");
app.get("/", (req, res) => {
const viewName = "main/home";
res.render("layout", { mainContent: viewName });
});
app.get("/home", (req, res) => {
const viewName = "main/home";
res.render("./layout", { mainContent: viewName });
});
app.get("/resources", (req, res) => {
const viewName = "main/resources";
res.render("layout", { mainContent: viewName });
});
app.get("/enemies", (req, res) => {
const viewName = "main/enemies";
res.render("layout", { mainContent: viewName });
});
app.get("/chests", (req, res) => {
const viewName = "main/chests";
res.render("layout", { mainContent: viewName });
});
app.get("/map", (req, res) => {
const viewName = "main/map";
const viewRequireName = "main/require-map";
fs.access("./images/Maps", function (error) {
if (error) {
res.render("layout", { mainContent: viewRequireName });
} else {
res.render("layout", { mainContent: viewName });
}
});
});
app.get("/ignorelist", (req, res) => {
const viewName = "main/ignorelist";
res.render("layout", { mainContent: viewName });
});
app.get("/settings", (req, res) => {
const viewName = "main/settings";
res.render("layout", { mainContent: viewName });
});
app.get("/drawing", (req, res) => {
res.render("main/drawing");
});
app.use("/scripts", express.static(__dirname + "/scripts"));
app.use("/mob-info", express.static(__dirname + "/mob-info"));
app.use("/scripts/Handlers", express.static(__dirname + "/scripts/Handlers"));
app.use("/scripts/Drawings", express.static(__dirname + "/scripts/Drawings"));
app.use("/scripts/Utils", express.static(__dirname + "/scripts/Utils"));
app.use("/images/Resources", express.static(__dirname + "/images/Resources"));
app.use("/images/Maps", express.static(__dirname + "/images/Maps"));
app.use("/images/Items", express.static(__dirname + "/images/Items"));
app.use("/images/Flags", express.static(__dirname + "/images/Flags"));
app.use("/sounds", express.static(__dirname + "/sounds"));
app.use("/config", express.static(__dirname + "/config"));
const port = 5001;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
import("open")
.then((open) => {
open.default(`http://localhost:${port}`).then(() => {
setTimeout(() => {
open.default(`http://localhost:${port}/drawing`);
}, 500); // Adjust the delay as needed
});
})
.catch((err) => {
console.error("Failed to open browser:", err);
});
});
var c = new Cap();
let adapterIp;
if (fs.existsSync("ip.txt"))
adapterIp = fs.readFileSync("ip.txt", { encoding: "utf-8", flag: "r" });
if (!adapterIp) {
adapterIp = getAdapterIp();
} else {
console.log();
console.log(`Using last adapter selected - ${adapterIp}`);
console.log('If you want to change adapter, delete the "ip.txt" file.');
console.log();
}
let device = Cap.findDevice(adapterIp);
if (device == undefined) {
console.log();
console.log(`Last adapter is not working, please choose a new one.`);
console.log();
adapterIp = getAdapterIp();
device = Cap.findDevice(adapterIp);
}
const filter = "udp and (dst port 5056 or src port 5056)";
var bufSize = 4096;
var buffer = Buffer.alloc(4096);
const manager = new PhotonParser();
var linkType = c.open(device, filter, bufSize, buffer);
c.setMinBytes && c.setMinBytes(0);
// setup Cap event listener on global level
c.on("packet", function (nbytes, trunc) {
let ret = decoders.Ethernet(buffer);
ret = decoders.IPV4(buffer, ret.offset);
ret = decoders.UDP(buffer, ret.offset);
let payload = buffer.slice(ret.offset, nbytes);
// Parse the UDP payload
try {
manager.handle(payload);
} catch {}
});
const server = new WebSocket.Server({ port: 5002, host: "localhost" });
server.on("listening", () => {
manager.on("event", (dictonary) => {
const dictionaryDataJSON = JSON.stringify(dictonary);
server.clients.forEach(function (client) {
client.send(
JSON.stringify({ code: "event", dictionary: dictionaryDataJSON }),
);
});
});
manager.on("request", (dictonary) => {
const dictionaryDataJSON = JSON.stringify(dictonary);
server.clients.forEach(function (client) {
client.send(
JSON.stringify({ code: "request", dictionary: dictionaryDataJSON }),
);
});
});
manager.on("response", (dictonary) => {
const dictionaryDataJSON = JSON.stringify(dictonary);
server.clients.forEach(function (client) {
client.send(
JSON.stringify({ code: "response", dictionary: dictionaryDataJSON }),
);
});
});
});
server.on("close", () => {
console.log("closed");
manager.removeAllListeners();
});