This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
244 lines (229 loc) · 10.1 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
const needle = require("needle");
const fs = require("fs-extra");
const pluginConfig = require("./config");
const COMPRESS_LUA = false;
module.exports = class remoteCommands {
constructor(mergedConfig, messageInterface, extras){
this.messageInterface = messageInterface;
this.config = mergedConfig;
this.socket = extras.socket;
this.socket.on("hello", () => {
this.socket.emit("registerPlayerManager");
});
this.socket.on("playerManagerGetPlayers", async data => {
if(this.hotpatchStatus){
try {
// let command = await this.getCommand("sharedPlugins/playerManager/lua/getPlayerData.lua");
// let playerData = await this.messageInterface("/silent-command "+command);
let playerData = await this.messageInterface(`/silent-command remote.call("playerManager", "exportPlayers")`);
/// this log statement is broken; playerData might be blank but still true (or something)
// if(playerData) messageInterface("Exported players leaving the server")
this.socket.emit("playerManagerSetPlayerdata", playerData.replace(/(\r\n\t|\n|\r\t)/gm, ""));
} catch(e){
console.log(e);
}
}
});
(async ()=>{
let hotpatchInstallStatus = await this.checkHotpatchInstallation();
this.hotpatchStatus = hotpatchInstallStatus;
this.messageInterface("Hotpach installation status: "+hotpatchInstallStatus);
if(hotpatchInstallStatus){
let mainCode = await this.getSafeLua("sharedPlugins/playerManager/lua/playerTracking.lua");
if(mainCode) var returnValue = await messageInterface(`/silent-command remote.call('hotpatch', 'update', '${pluginConfig.name}', '${pluginConfig.version}', '${mainCode}')`);
if(returnValue) console.log(returnValue);
this.messageInterface(`/silent-command remote.call("playerManager", "resetInvImportQueue")`);
this.messageInterface(`/silent-command remote.call("playerManager", "createPermissionGroups")`);
}
})().catch(e => console.log(e));
}
async importPlayer(playerName) {
console.log("isBanned?: " + playerName);
let isPlayerBanned = await needle("post", `${this.config.masterIP}:${this.config.masterPort}/api/playerManager/isPlayerBanned`, { "factorioName": playerName, "token": this.config.masterAuthToken}, {"compressed":true});
console.log("isBanned done: " + playerName);
if(isPlayerBanned.body.msg === true){
this.messageInterface(`/ban ${playerName}`);
this.messageInterface(`/kick ${playerName}`);
}
// import inventory
console.log("getPlayer: " + playerName);
let player = (await needle("post", `${this.config.masterIP}:${this.config.masterPort}/api/playerManager/getPlayer`, {"name": playerName, "token": this.config.masterAuthToken}, {"compressed":true})).body.player;
console.log("getPlayer done: " + playerName);
let playerIsAdmin = false;
if(player){
if(player.inventory){
this.messageInterface(`Downloading ${playerName}'s inventory`);
this.messageInterface(`/silent-command remote.call("playerManager", "importInventory", "${player.name}", '${player.inventory}', '${player.quickbar}', '${player.requests}', '${player.trashfilters}', '${player.forceName}', ${player.spectator}, ${player.admin}, {r=${player.r}, g=${player.g}, b=${player.b}, a=${player.a}}, {r=${player.cr}, g=${player.cg}, b=${player.cb}, a=${player.ca}}, "${player.tag || ""}")`);
}
if(player.admin === "true") {
playerIsAdmin = true;
this.messageInterface(`/silent-command remote.call("playerManager", "setPlayerPermissionGroup", "${playerName}", "Admin")`);
}
} else {
this.messageInterface(`/silent-command remote.call("playerManager", "postImportInventory", "${playerName}")`);
}
if(!playerIsAdmin) {
console.log("isPlayerWhitelisted: " + playerName);
let isPlayerWhitelisted = await needle("post", `${this.config.masterIP}:${this.config.masterPort}/api/playerManager/isPlayerWhitelisted`, { "factorioName": playerName, "token": this.config.masterAuthToken}, {"compressed":true});
console.log("isPlayerWhitelisted done: " + playerName);
if(isPlayerWhitelisted.body.msg === true){
this.messageInterface(`/silent-command remote.call("playerManager", "setPlayerPermissionGroup", "${playerName}", "Standard")`);
}
}
console.log("Player import done for player: " + playerName);
}
// Gets sent in packets, split by \n
scriptOutput(data) {
if(data == null) {
return;
}
console.log(`Got data, length: ${data.length}.`);
if(data.startsWith("IMPORT")) {
let playerName = data.substring(6);
console.log(`Importing player ${playerName}...`);
this.importPlayer(playerName);
}
else if(data.startsWith("EXPORT")) {
console.log("Exporting player. Sending to playerManagerSetPlayerdata...");
this.socket.emit("playerManagerSetPlayerdata", data.substring(6).replace(/(\r\n\t|\n|\r\t)/gm, ""));
console.log(`Player export done.`)
}
else {
console.log("ERROR: Cannot parse script output. Output:");
console.log(data);
}
}
async getCommand(file){
this.commandCache = this.commandCache || {};
if(!this.commandCache[file]){
try{
let command = (await fs.readFile(file)).toString();
this.commandCache[file] = command.replace(/(\r\n\t|\n|\r\t)/gm, " "); // remove newlines
return this.commandCache[file];
} catch(e){
console.log("Unable to get command from file!");
console.log(e)
}
} else if(typeof this.commandCache[file] == "string"){
return this.commandCache[file];
} else {
throw new Error("Command not found");
}
}
async factorioOutput(data){
try{ // these filters might not be good enough. Investigate whether it is possible for a non-admin user to write things that still goes through.
this.messageInterface(data)
if(data.includes("[BAN]")
&& data.includes("was banned by")
&& !data.includes("[CHAT]")
&& !data.includes("/c") && !data.includes("/silent-command")
// Try to weed out things that can lead to RCE vulnearabilities
&& !data.includes('"') && !data.includes("\\")){
// data = "2018-11-17 14:05:27 [BAN] James (not on map) was banned by Zr4g0n [Totally not a dragon!]. Reason: being lame."
let bannedName = data.split("[BAN] ")[1].split(" ")[0];
let banner = data.split("was banned by ")[1].split(" ")[0];
let reason = `${banner} banned ${bannedName} for ${data.split("Reason: ")[data.split("Reason: ").length-1]}`;
this.messageInterface(bannedName)
this.messageInterface(banner)
this.messageInterface(reason)
this.messageInterface("Im OK")
let banlist = (await needle("get", this.config.masterIP+":"+this.config.masterPort+"/api/playerManager/bannedPlayers"), {compressed:true}).body;
// avoid banning players who are already banned
let playerIsAlreadyBannned = false;
console.log(banlist)
banlist.forEach(ban => {
if(ban.factorioName == bannedName) playerIsAlreadyBannned = true;
});
if(playerIsAlreadyBannned) return true;
needle.post(this.config.masterIP+":"+this.config.masterPort+"/api/playerManager/banlist", {
factorioName: bannedName,
action: "add",
reason,
token: this.config.masterAuthToken,
}, {"compressed":true}, (err, response) => {
if(err){
console.error(err);
} else if(response.statusCode != 200){
console.error(`Got code ${response.statusCode} when posting to /api/playerManager/banlist`);
} else {
if(response.ok){
this.messageInterface(`/c game.print("${reason}")`);
this.messageInterface(reason);
}
this.messageInterface(response.msg);
}
});
}} catch(e){console.log(e)}
}
getInstanceName(instanceID){
return new Promise((resolve, reject) => {
let instance = this.instances[instanceID];
if(!instance){
needle.get(this.config.masterIP+":"+this.config.masterPort+ '/api/slaves', {"compressed":true}, (err, response) => {
if(err || response.statusCode != 200) {
console.log("Unable to get JSON master/api/slaves, master might be unaccessible");
} else if (response && response.body) {
if(Buffer.isBuffer(response.body)) {console.log(response.body.toString("utf-8")); throw new Error();}
try {
for (let index in response.body)
this.instances[index] = response.body[index].instanceName;
} catch (e){
console.log(e);
return null;
}
instance = this.instances[instanceID]
if (!instance) instance = instanceID; //somehow the master doesn't know the instance
resolve(instance);
}
});
} else {
resolve(instance);
}
});
}
async getSafeLua(filePath){
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf8", (err, contents) => {
if(err){
reject(err);
} else {
// split content into lines
contents = contents.split(/\r?\n/);
// join those lines after making them safe again
contents = contents.reduce((acc, val) => {
val = val.replace(/\\/g ,'\\\\');
// remove leading and trailing spaces
val = val.trim();
// escape single quotes
val = val.replace(/'/g ,'\\\'');
// remove single line comments
let singleLineCommentPosition = val.indexOf("--");
let multiLineCommentPosition = val.indexOf("--[[");
if(multiLineCommentPosition === -1 && singleLineCommentPosition !== -1) {
val = val.substr(0, singleLineCommentPosition);
}
return acc + val + '\\n';
}, ""); // need the "" or it will not process the first row, potentially leaving a single line comment in that disables the whole code
// console.log(contents);
// this takes about 46 ms to minify train_stop_tracking.lua in my tests on an i3
if(COMPRESS_LUA) contents = require("luamin").minify(contents);
resolve(contents);
}
});
});
}
async checkHotpatchInstallation(){
let yn = await this.messageInterface("/silent-command if remote.interfaces['hotpatch'] then rcon.print('true') else rcon.print('false') end");
yn = yn.replace(/(\r\n\t|\n|\r\t)/gm, "");
if(yn == "true"){
return true;
} else if(yn == "false"){
return false;
}
}
}
async function sleep(s){
return new Promise((resolve, reject) => {
setTimeout(resolve, s*1000);
});
}