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