-
Notifications
You must be signed in to change notification settings - Fork 532
/
localFileServer.plugin.js
271 lines (239 loc) · 12.8 KB
/
localFileServer.plugin.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
271
//META { "name": "localFileServer" } *//
global.localFileServer = function () {
var app, assertMainProcJsPatch, bw, dialog, favicon, findPatchRelaunch, fs, fs2, func, getSettings, https, i, isImage, len, onRequest, path, pfx, ref, remote, server, settings, shell, startServer, stopServer, url;
class localFileServer {
getName() {
return "Local File Server";
}
getDescription() {
return "Hosts a selected folder so you can use local files in your theme. Has to restart discord first time enabling.";
}
getAuthor() {
return "square";
}
getVersion() {
return "1.1.3";
}
load() {}
start() {
assertMainProcJsPatch();
getSettings();
startServer();
}
stop() {
stopServer();
}
getSettingsPanel() {
getSettings();
return `<div id="settings_localFileServer">\n <style>\n #settings_localFileServer {\n color: #87909C;\n }\n #settings_localFileServer button {\n background: rgba(128,128,128,0.4);\n width: calc(100% - 20px);\n padding: 5px 10px;\n box-sizing: content-box;\n height: 1em;\n font-size: 1em;\n line-height: 0.1em;\n }\n #settings_localFileServer input {\n text-align: center;\n color: black;\n width: 63px;\n border-width: 0;\n outline-width: 0;\n }\n #settings_localFileServer .invalid {\n background: rgba(255,0,0,.5);\n font-weight: 500;\n }\n #settings_localFileServer * {\n margin-bottom: 2px;\n }\n </style>\n <button name="folder" ${fs.existsSync(settings.folder) ? "" : "class=\"invalid\" "}type="button" onclick="localFileServer.chooseDirectory()">${settings.folder}</button>\n <button type="button" onclick="localFileServer.openInBrowser()">Open in browser.</button>\n Port: <input name="port" type="number" value="${settings.port}" placeholder="...port..." oninput="localFileServer.updateSettings()" autocomplete="off" />\n only accepts 443 and [10001-65535]\n</div>`;
}
static openInBrowser() {
shell.openExternal(`https://localhost:${settings.port}/`);
}
static chooseDirectory() {
dialog.showOpenDialog(bw, {
defaultPath: settings.folder,
buttonLabel: "Choose",
properties: ["openDirectory", "showHiddenFiles", "createDirectory", "noResolveAliases", "treatPackageAsDirectory"]
}, selection => {
var ref;
document.querySelector("#settings_localFileServer button").innerHTML = (ref = selection != null ? selection[0] : void 0) != null ? ref : "";
return this.updateSettings();
});
}
static updateSettings() {
var i, input, len, name, oldPort, ref, type, value;
oldPort = settings.port;
ref = document.querySelectorAll("#settings_localFileServer :-webkit-any(input, button, checkbox)");
for (i = 0, len = ref.length; i < len; i++) {
input = ref[i];
({ name, type, value } = input);
if (!name) {
continue;
}
if (type === "button") {
value = input.innerHTML;
} else if (type === "checkbox") {
value = input.checked;
}
if (function () {
var ref1;
switch (name) {
case "folder":
return value && path.isAbsolute(value) && fs.existsSync(value = path.normalize(value));
case "port":
return (/^[0-9]+$/.test(value) && (1e4 < (ref1 = value = 0 | value) && ref1 <= 0xFFFF || 443 === value)
);
default:
return true;
}
}()) {
settings[name] = value;
input.className = "";
} else {
input.className = "invalid";
if (name === "folder") {
input.innerHTML = "invalid path";
}
}
}
BdApi.setData("localFileServer", "settings", settings);
if (oldPort !== settings.port) {
stopServer();
startServer();
}
}
};
fs = require("fs");
path = require("path");
https = require("https");
url = require("url");
({ remote, shell } = require("electron"));
({ dialog, app } = remote);
bw = remote.getCurrentWindow();
settings = server = null;
getSettings = function () {
var k, ref, ref1, v;
settings = (ref = BdApi.getData("localFileServer", "settings")) != null ? ref : {};
ref1 = {
folder: path.join(process.env[process.platform === "win32" ? "USERPROFILE" : "HOME"], "pictures"),
port: 35724
};
for (k in ref1) {
v = ref1[k];
if (settings[k] == null) {
settings[k] = v;
}
}
};
startServer = function () {
var ref;
if ((ref = remote.getGlobal("localFileServerMainProcObj")) != null) {
ref.port = 443 === settings.port ? "" : `:${settings.port}`;
}
server = https.createServer({ pfx }, onRequest);
server.on("error", function (e) {
console.error(e);
});
server.timeout = 10e3;
server.keepAliveTimeout = 0;
server.listen(settings.port, "127.0.0.1");
};
stopServer = function () {
var ref;
if ((ref = remote.getGlobal("localFileServerMainProcObj")) != null) {
ref.port = null;
}
server.close();
};
onRequest = function (req, res) {
var _path;
if (req.url.endsWith("/favicon.ico")) {
res.writeHead(200);
res.end(favicon);
return;
}
_path = path.normalize(path.join(settings.folder, decodeURIComponent(url.parse(req.url).path)));
if (_path[_path.length - 1] === path.sep) {
_path = _path.slice(0, -1);
}
fs.lstat(_path, function (e, stats) {
if (e != null) {
res.writeHead(500, e.message);
res.end();
return console.error(e);
}
if (!stats.isDirectory()) {
fs.readFile(_path, function (e, buffer) {
if (e != null) {
res.writeHead(500, e.message);
res.end();
return console.error(e);
}
res.writeHead(200);
res.end(buffer);
});
} else {
fs.readdir(_path, function (e, files) {
var encoded, file, i, image, images, j, len, len1;
if (e != null) {
res.writeHead(500, e.message);
res.end();
return console.error(e);
}
res.writeHead(200, {
"Content-Type": "text/html"
});
res.write(`<html><head><title>Local File Server</title><base href="${req.url[req.url.length - 1] === "/" ? req.url : req.url + "/"}" /><style>\n a { float: left; margin: 5px; display: inline-block; }\n br { clear: left; }\n .image { width: 100%; max-width: 300px; height: 200px; background: #20242a 50%/contain no-repeat; border: solid 1px black; }\n</style></head><body>`);
if (_path !== settings.folder) {
files.unshift("..");
}
images = [];
for (i = 0, len = files.length; i < len; i++) {
file = files[i];
if (isImage(file)) {
images.push(file);
continue;
}
res.write(`<a href="${encodeURIComponent(file)}">${file}</a>`);
}
res.write("<br/>");
for (j = 0, len1 = images.length; j < len1; j++) {
image = images[j];
res.write(`<a href="${encoded = encodeURIComponent(image)}" class="image" style="background-image: url('${encoded}');" />`);
}
res.end("</body></html>");
});
}
});
};
assertMainProcJsPatch = async function () {
var _path, e;
try {
_path = remote.app.getAppPath().split(".asar")[0] + "/index.js";
return await findPatchRelaunch(_path, null, "\n\n// localFileServer plugin start #ref1#\nglobal.localFileServerMainProcObj={port:null};\nelectron.app.commandLine.appendSwitch(\"allow-insecure-localhost\");\nelectron.app.on(\"certificate-error\",(ev,x,url,y,z,cb)=>(new RegExp(`https://(localhost|127\\.0\\.0\\.1)${localFileServerMainProcObj.port}/`)).test(url)?(ev.preventDefault(),cb(!0)):cb(!1));\n// localFileServer plugin end\n".replace(/\r?\n/g, require("os").EOL));
} catch (error) {
e = error;
console.error(e);
}
};
findPatchRelaunch = async function (_path, split, mainjs) {
var data, newData;
data = await fs2.readFile(_path, "utf8");
if (-1 !== data.indexOf(mainjs)) {
return;
}
newData = split ? data.split(split).join(`${split}${mainjs}`) : data + mainjs;
if (data.length + mainjs.length !== newData.length) {
throw new Error("localFileServer needs fixing!");
}
await fs2.writeFile(_path, newData);
app.relaunch();
app.quit();
};
fs2 = {};
ref = ["readFile", "writeFile"];
for (i = 0, len = ref.length; i < len; i++) {
func = ref[i];
(function (func) {
return fs2[func] = function (...args) {
return new Promise(function (c, r) {
return fs[func](...args, function (e, res) {
if (e != null) {
return r(e);
} else {
return c(res);
}
});
});
};
})(func);
}
isImage = function (filename) {
var ref1;
return (ref1 = filename.slice(filename.lastIndexOf(".")).toLowerCase()) === ".png" || ref1 === ".jpeg" || ref1 === ".jpg" || ref1 === ".bmp" || ref1 === ".gif" || ref1 === ".webp" || ref1 === ".svg" || ref1 === ".tiff" || ref1 === ".apng";
};
favicon = Buffer.from("AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAMMOAADDDgAAEAAAABAAAAAAAAAAL2sUAEyxIgAXNgoA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASIiIiIiEAACEDIAIwEgAAIDIxEyMCAAAjIAAAAjIAACIQEREBIgAAIDAQAQMCAAAgMBABAwIAACIQEREBIgAAIyAAAAIyAAAgMjETIwIAACEDIAIwEgAAEiIiIiIhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "base64");
pfx = Buffer.from("MIIJOgIBAzCCCPYGCSqGSIb3DQEHAaCCCOcEggjjMIII3zCCBggGCSqGSIb3DQEHAaCCBfkEggX1MIIF8TCCBe0GCyqGSIb3DQEMCgECoIIE/jCCBPowHAYKKoZIhvcNAQwBAzAOBAgdXkoB55/j7QICB9AEggTY8LQ/W6ztvehDIIsZZ15J8jsSvsTD5tCwm6tazSYArSk1zavcCgW3a6y/GLoS4ooiRnsMDM3DIqJtzInewJmlbCFx9jpPuubci/p63Lq3G5ltc9dLHIwBSYKk9GxiwDqzUK4Pp4Gc1xtMFcQB77zckCRVIAjyxG9uVVywjDCG5qikIsMGgDMw4SQz6mADQf+D/LFB5N6piHaTTq4borZVmouClGW7WkkVnenVX3wp+/3DCZlcLKkXaNJRQK0cJyVvMRsdvJpNII2Tz/usOsBBEZZRYpkr0GP/jWVLfL2jxrstTAl6in2kEFlHAaJi8yz7DmjoiVmXvFJLdAh5IzRcRZk4VdLjGbhsSPFdvmPNlCaCaX3jPr2PvESda8eDGqpF5Y+tnOa75fkplhiDUpwsg+EJR0HoTB78G+G2imqzINaI6fst+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