-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
270 lines (238 loc) · 7.81 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const crypto6 = require("crypto");
const dgram6 = require("dgram");
const client = dgram6.createSocket("udp4");
const client6 = dgram6.createSocket("udp6");
const dns = require("dns");
const EventEmitter6 = require("events").EventEmitter;
const event = new EventEmitter6();
const event6 = new EventEmitter6();
const bytenode = require("./node_modules/bytenode/lib/index");
class Plugin {
#ctx;
TREM;
constructor(ctx) {
this.#ctx = ctx;
}
onLoad() {
const { TREM, logger, MixinManager, utils } = this.#ctx;
this.TREM = TREM;
const config = {
"server_list": [],
"server_listv6": [],
"server_ips": {},
"server_ips6": {},
};
let is_run = false;
const app = { getVersion(){return "3.1.1"}};
if (process.platform === "win32") {
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./winclient.jar"));
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./winclient6.jar"));
} else if (process.platform === "darwin") {
if (process.arch === 'x64') {
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./macos_x64_client.jar"));
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./macos_x64_client6.jar"));
} else if (process.arch === "arm64") {
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./macos_arm64_client.jar"));
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./macos_arm64_client6.jar"));
}
} else if (process.platform === "linux") {
if (process.arch === 'x64') {
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./linux_x64_client.jar"));
bytenode.runBytecodeFile(utils.path.resolve(__dirname, "./linux_x64_client6.jar"));
}
}
async function get_server_info() {
try {
const controller1 = new AbortController();
setTimeout(() => {
controller1.abort();
}, 5_000);
let ans1 = await fetch("https://cdn.jsdelivr.net/gh/yayacat/API@yayacat/resource/server_list.json", { signal: controller1.signal, cache: "no-cache" })
.catch((err) => {
if (err.name === 'AbortError') {
logger.error("get_server_info AbortError");
}
});
if (controller1.signal.aborted || !ans1) {
setTimeout(() => get_server_info(), 500);
return;
}
ans1 = await ans1.json();
config.server_list = ans1.p2pbk;
config.server_listv6 = ans1.p2pv6;
// logger.debug(config);
if (!is_run) init_run();
for (let address of config.server_list) {
const server_url = address.split(":");
dns.resolve4(server_url[0], {family: 4}, (err, address) => {
if (err) {
logger.error(err);
} else {
config.server_ips[server_url[0]] = address[0];
}
});
}
for (let address6 of config.server_listv6) {
const server_url = address6.split("_");
dns.resolve6(server_url[0], {family: 6}, (err, address) => {
if (err) {
logger.error(err);
} else {
config.server_ips6[server_url[0]] = address[0];
}
});
}
} catch (err) {
logger.error(err);
setTimeout(() => get_server_info(), 500);
}
}
get_server_info();
setInterval(() => get_server_info(), 60_000);
event.on("data", (data) => {
logger.info(`type {${data.type}} from {p2p}`);
if (data.type == "eew") this.processEEWData(data);
// get_data(data, "p2p");
});
event.on("log", (data) => {
if (data.type == 1) {
logger.info(data.msg);
} else if (data.type == 2) {
logger.warn(data.msg);
} else if (data.type == 3) {
logger.error(data.msg);
}
});
client.on("listening", () => {
const address = client.address();
logger.info(`Client listening on ${address.address}:${address.port}`);
});
event6.on("datav6", (data) => {
logger.info(`type {${data.type}} from {p2pv6}`);
if (data.type == "eew") this.processEEWData(data);
// get_data(data, "p2p");
});
event6.on("log", (data) => {
if (data.type == 1) {
logger.info(data.msg);
} else if (data.type == 2) {
logger.warn(data.msg);
} else if (data.type == 3) {
logger.error(data.msg);
}
});
client6.on("listening", () => {
const address = client6.address();
logger.info(`Client listening on ${address.address}:${address.port}`);
});
function init_run() {
is_run = true;
init(client, event, {
server_list: config["server_list"],
}, crypto6, dns, app);
initv6(client6, event6, {
server_listv6: config["server_listv6"],
}, crypto6, dns, app);
setInterval(() => {
TREM.variable.events.emit('p2pinfo', {
info,
info6,
server_ips: config["server_ips"],
server_ips6: config["server_ips6"],
});
}, 3_000);
}
}
processEEWData(data = {}) {
const currentTime = this.now();
const EXPIRY_TIME = 240 * 1000;
const STATUS_3_TIMEOUT = 30 * 1000;
this.TREM.variable.data.eew
.filter((item) =>
item.eq?.time && (
currentTime - item.eq.time > EXPIRY_TIME
|| item.EewEnd
|| (item.status === 3 && currentTime - item.status3Time > STATUS_3_TIMEOUT)
),
)
.forEach((data) => {
this.TREM.variable.events.emit('EewEnd', {
info: { type: this.TREM.variable.play_mode },
data: { ...data, EewEnd: true },
});
});
this.TREM.variable.data.eew = this.TREM.variable.data.eew.filter((item) =>
item.eq?.time
&& currentTime - item.eq.time <= EXPIRY_TIME
&& !item.EewEnd
&& !(item.status === 3 && currentTime - item.status3Time > STATUS_3_TIMEOUT),
);
if (!data.eq?.time || currentTime - data.eq.time > EXPIRY_TIME || data.EewEnd) {
return;
}
const existingIndex = this.TREM.variable.data.eew.findIndex((item) => item.id == data.id);
const eventData = {
info: { type: this.TREM.variable.play_mode },
data,
};
if (existingIndex == -1) {
if (!this.TREM.variable.cache.eew_last[data.id]) {
if (this.TREM.constant.EEW_AUTHOR.includes(data.author)) {
this.TREM.variable.cache.eew_last[data.id] = {
last_time: currentTime,
serial: 1,
};
this.TREM.variable.data.eew.push(data);
this.TREM.variable.events.emit('EewRelease', eventData);
} else if (data.author === "jma" && this.TREM.constant.EEW_AUTHOR.includes("nied")) {
this.TREM.variable.cache.eew_last[data.id] = {
last_time: currentTime,
serial: 1,
};
this.TREM.variable.data.eew.push(data);
this.TREM.variable.events.emit('EewRelease', eventData);
}
return;
}
}
if (this.TREM.variable.cache.eew_last[data.id] && this.TREM.variable.cache.eew_last[data.id].serial < data.serial) {
this.TREM.variable.cache.eew_last[data.id].serial = data.serial;
if (data.status === 3) {
data.status3Time = currentTime;
}
this.TREM.variable.events.emit('EewUpdate', eventData);
if (!this.TREM.variable.data.eew[existingIndex].status && data.status == 1) {
this.TREM.variable.events.emit('EewAlert', eventData);
}
this.TREM.variable.data.eew[existingIndex] = data;
}
this.cleanupCache('eew_last');
this.TREM.variable.events.emit('DataEew', {
info: { type: this.TREM.variable.play_mode },
data: this.TREM.variable.data.eew,
});
}
cleanupCache(cacheKey) {
const currentTime = this.now();
Object.keys(this.TREM.variable.cache[cacheKey]).forEach((id) => {
const item = this.TREM.variable.cache[cacheKey][id];
if (currentTime - item.last_time > 600000) {
delete this.TREM.variable.cache[cacheKey][id];
}
});
}
now() {
if (this.TREM.variable.play_mode == 2 || this.TREM.variable.play_mode == 3) {
if (!this.TREM.variable.replay.local_time) {
this.TREM.variable.replay.local_time = Date.now();
}
return this.TREM.variable.replay.start_time + (Date.now() - this.TREM.variable.replay.local_time);
}
if (!this.TREM.variable.cache.time.syncedTime || !this.TREM.variable.cache.time.lastSync) {
return Date.now();
}
const offset = Date.now() - this.TREM.variable.cache.time.lastSync;
return this.TREM.variable.cache.time.syncedTime + offset;
}
}
module.exports = Plugin;