-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
147 lines (131 loc) · 4.38 KB
/
server.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
const WebSocket = require("ws")
const child_proc = require("child_process");
const uuid = require("uuid");
const process = require("process");
const fs = require("fs");
const path = require("path");
//
// Configuration files
//
// Please do not modify the following variables,
// but you may modify the values in "Configuration.js"
//
const Configuration = require("./Configuration");
let FloodAmount = Configuration.Flood_Amount;
let FloodInterval = Configuration.Flood_Interval;
let ServerPort = Configuration.Server_Port;
let RFInterval = Configuration.RunFile_Interval;
let Userfiles = path.join(__dirname, "userfiles");
const wss = new WebSocket.Server({ port: ServerPort })
console.log(`Running on port ${ServerPort} | /connect localhost:${ServerPort}`)
wss.on("close", () => {
console.log("Client disconnected");
process.exit();
})
// use /connect localhost:3000
wss.on("connection", socket => {
console.log("Connected to client")
function sendCommand(cmd) {
const msg = {
"header": {
"version": 1,
"requestId": uuid.v4(),
"messagePurpose": "commandRequest",
"messageType": "commandRequest"
},
"body": {
"version": 1,
"commandLine": cmd,
"origin": {
"type": "player"
}
}
}
socket.send(JSON.stringify(msg))
};
function shellComment(comment) {
console.log(`# ${comment}`);
};
socket.send(JSON.stringify({
"header": {
"version": 1,
"requestId": uuid.v4(),
"messageType": "commandRequest",
"messagePurpose": "subscribe"
},
"body": {
"eventName": "PlayerMessage"
},
}));
socket.on("message", packet => {
const msg = JSON.parse(packet);
//console.log(msg);
if (msg.header.messagePurpose == "commandResponse") {
console.log(`- ${msg.body.message}`);
};
})
// Check command
function CheckCommand(command, prefix, commandCallback) {
if (command.startsWith(prefix)) {
command = command.replace(prefix, "");
commandCallback(command);
}
}
function RunCommand(message) {
// -- COMMANDS -- Run command
CheckCommand(message, "/", (command) => {
sendCommand(command);
})
// -- COMMANDS -- Flood
CheckCommand(message, "fl/", (command) => {
shellComment(`Running the command "${command}" ${FloodAmount} times with an interval of ${FloodInterval} milliseconds.`);
for (let repeatI = 0; repeatI < FloodAmount; repeatI++) {
setTimeout(() => {
sendCommand(command);
}, FloodInterval * repeatI);
};
})
// -- COMMANDS -- Run file
CheckCommand(message, "rf/", (command) => {
try {
const data = fs.readFileSync(path.join(Userfiles, command), "UTF-8")
const lines = data.split(/\r?\n/)
lines.forEach((line, commandIndex) => {
setTimeout(() => {
sendCommand(line);
}, commandIndex * RFInterval);
})
} catch (rfError) {
shellComment(`rfError: ${rfError}`);
}
})
// -- COMMANDS -- Temp Modify
//
// FloodAmount
CheckCommand(message, "FloodAmount=", (command) => {
FloodAmount = Number(command);
shellComment(`Temporary set FloodAmount to ${FloodAmount}`);
})
// FloodInterval
CheckCommand(message, "FloodInterval=", (command) => {
FloodInterval = Number(command);
shellComment(`Temporary set FloodInterval to ${FloodInterval}`);
})
//
// -- COMMANDS -- Quit
if (message == "quit" || message == "close" || message == "exit") {
shell.disconnect();
process.exit();
}
}
const shell = child_proc.fork("./shell.js");
shell.on("message", (shellMessage) => {
let message = shellMessage.input;
RunCommand(message);
});
shell.on("exit", () => {
wss.close();
process.exit();
});
sendCommand('tellraw @s {"rawtext":[{"text":"§aWelcome to Rmine!§§"}]}');
})