/
app.js
executable file
·338 lines (288 loc) · 9.35 KB
/
app.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/local/bin/node
const express = require('express')
const serveIndex = require('serve-index')
const pty = require('node-pty')
const crypto = require('crypto')
const { spawn } = require("child_process")
const http = require('http')
const websocket = require('ws')
const hljs = require('highlight.js')
const urlencode = require('urlencode')
const fs = require('fs')
const { SIGTERM, SIGQUIT, SIGINT, SIGKILL } = require('constants')
const app = express()
app.disable('x-powered-by')
const base_dir = '/home/ryugod'
const port = 5000
const md = require('markdown-it')({
highlight: (str, lang) => {
if (lang && hljs.getLanguage(lang)) {
try {
return '<pre class="hljs">' +
hljs.highlight(str, {language: lang}, true).value +
'</pre>';
} catch (e) {log.err(e.message)}
}
return '<pre class="hljs">' + md.utils.escapeHtml(str) + '</pre>';
}
})
const mdi = require('markdown-it-mdi')
let dockerCount = 0
md.use(mdi)
app.use(express.static(base_dir + '/public'))
app.use(express.json({limit : "1gb"}));
app.use(express.urlencoded({limit : "1mb", extended: false}));
app.use('/list', serveIndex(base_dir + '/contents'))
const sendStatic = (req, res) => {
url = urlencode.decode(req.url)
const path = base_dir + url
if (fs.existsSync(path)) {
fs.readFile(path, function(err, data) {
if (err)
console.err(err.message)
else {
res.send(data)
}
})
}
else {
res.redirect(`/contents/404.md`)
}
}
hash = () => {
return crypto.createHmac('sha256', 'mysharedsource01').update(Date.now().toString()).digest('base64').replace('/', '_').substr(0, 10)
}
app.post('/', (req, res) => {
const path = base_dir + '/public/index.html'
res.sendFile(path)
})
app.post('/shareMySource', (req, res) => {
const base_path = `${base_dir}/contents/shared/${req.body.params.language}`
if (!fs.existsSync(base_path))
fs.mkdirSync(base_path)
let h = hash()
let path = `${base_path}/${h}.template`
while (fs.existsSync(path)) {
console.log(`hash collision ${h}, recreating...`)
h = hash()
path = `${base_path}/${h}.template`
}
fs.writeFile(path, req.body.params.source, function(err) {
if(err) {
return res.send('')
}
res.send(h)
console.log(`created shared source ${path}...`)
});
})
app.get('/pages/?**', (req, res) => {
const path = base_dir + '/public/index.html'
res.sendFile(path)
})
app.get('/list/*\.(md|template|png)$', (req, res) => {
const url = urlencode.decode(req.url).replace('/list/', '/contents/')
const path = base_dir + url
if (fs.existsSync(path, {encoding:'utf-8'})) {
res.sendFile(path)
}
else {
res.redirect(`/contents/404.md`)
}
})
app.get('/contents/*\.(template)$', (req, res) => {
const url = urlencode.decode(req.url)
const path = base_dir + url
if (fs.existsSync(path, {encoding:'utf-8'})) {
res.sendFile(path)
}
else {
res.redirect(`/contents/404.md`)
}
})
app.get('*\.(png|js)$', (req, res) => {
sendStatic(req, res)
})
app.get('*\.(md|md.html|template|template.html)$', (req, res) => {
const url = urlencode.decode(req.url).replace(/\.html$/, '')
const path = base_dir + url
if (fs.existsSync(path, {encoding:'utf-8'})) {
stats = fs.statSync(path)
fs.readFile(path, 'utf8', function(err, data) {
if (err)
console.err(err.message)
else {
if (url.match(/\.template(\.html)?$/)) {
data = `# ${url}\n` + '```\n' + data + '\n```'
}
const html = '<html>' +
'<head>' +
'<body>' +
md.render(data) +
`<hr><sub><sup>Modified at ${stats.mtime}</sup></sub>` +
'</body></html>'
res.header('Access-Control-Allow-Origin', '*')
res.send(html)
}
})
}
else {
res.redirect(`/contents/404.md`)
}
})
app.get('*\.(html|jsls)$', (req, res) => {
sendStatic(req, res)
})
app.get('/sitemap.xml', (req, res) => {
const path = base_dir + '/contents'
const file_list = []
readdir = (path, file_list) => {
if (fs.existsSync(path, {encoding:'utf-8'})) {
stats = fs.lstatSync(path)
const files = fs.readdirSync(path, {encoding:'utf-8'})
files.sort()
for (file of files) {
const fpath = path + '/' + file
if (fs.existsSync(fpath, {encoding:'utf-8'})) {
stats = fs.lstatSync(fpath)
if (stats.isDirectory()) {
readdir(path + '/' + file, file_list)
continue
}
if (file.slice(-3) === '.png')
continue
file_list.push(`<url><loc>https://www.ryugod.com${fpath.replace(base_dir, '').replace(/\/contents\//, '/pages/')}</loc>` +
`<lastmod>${stats.mtime.toISOString()}</lastmod></url>`)
}
}
}
}
readdir(path, file_list)
const html = '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' +
file_list.join('\n') +
'</urlset>\n'
res.send(html)
})
app.get('/contents/?**', (req, res) => {
const url = urlencode.decode(req.url)
const path = base_dir + url
var ret = {}
if (fs.existsSync(path, {encoding:'utf-8'})) {
stats = fs.lstatSync(path)
if (stats.isDirectory()) {
fs.readdir(path, {encoding:'utf-8'}, (err, files) => {
if (err) {
console.log(err)
res.send('Error')
}
if (path.match(/\/shared\//)) {
res.send('')
return
}
file_list = []
files.sort()
files.map(file => {
stats = fs.lstatSync(path + '/' + file)
isDir = stats.isDirectory()
if (isDir || file.endsWith('\.md') || file.endsWith('\.template')) {
subfiles = isDir ? fs.readdirSync(path + '/' + file, {encoding:'utf-8'}):[]
subfiles.sort()
subfiles = subfiles.filter(f => f.endsWith('\.md') || f.endsWith('\.template'))
file_list.push({name:file,type:isDir ? "d":"f",list:subfiles})
}
})
ret["path"] = url
ret["list"] = file_list
res.header('Access-Control-Allow-Origin', '*')
res.send(ret)
})
return
}
}
res.redirect(`/contents/404.md`)
})
const server = http.createServer(app).listen(port, () => {
console.log(`app listening at http://localhost:${port}`)
})
const websocketServer = new websocket.Server(
{server, path: '/terminal'}
)
setInterval(function ping() {
if (websocketServer.clients.size > 0) {
websocketServer.clients.forEach(function each(socket) {
if (socket.isAlive === false) return socket.terminate()
socket.isAlive = false;
socket.ping(websocketServer.clients.size)
socket.send('2' + websocketServer.clients.size)
})
}
}, 5000)
let docker_seq = 0
websocketServer.on('connection', (ws, req) => {
docker_seq = docker_seq > 99999999 ? 0 : docker_seq+1
let logMessage = ''
const docker_name = 'RS' + `0000000${docker_seq}`.slice(-8)
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
var lang = req.headers["accept-language"]
var locale = (lang && lang.split(',')[0].indexOf("ko") >= 0)?'ko_KR':'C'
console.log(new Date().toString(), 'connected...', ip, docker_name, lang)
const child = pty.spawn('/usr/bin/docker', [
'run',
'--env',
`LANG=${locale}.UTF-8`,
'--env',
'TMOUT=1200',
'--env',
`DOCKER_NAME=${docker_name}`,
'-it',
'--name',
docker_name,
'--rm',
'--workdir',
'/home/ryugod',
'--user',
'ryugod',
'--hostname',
'ryugod-server',
'ubuntu:ryugod',
'/bin/bash'
], {
name: 'xterm-color',
})
console.log('forking docker: ', docker_name, child.pid)
dockerCount++
child.onData((data) => {
ws.send('1' + data.toString())
})
child.onExit((code) => {
ws.close()
dockerCount--
console.log('child closed', docker_name, child.pid, code)
})
ws.on('message', (message) => {
const cmd = message[0]
switch (cmd) {
case '1':
if (message) {
const msg = message.slice(1)
child.write(msg)
}
break
case '2': /* resize */
const size = message.split(' ')
child.resize(parseInt(size[1]), parseInt(size[2]))
break
}
})
ws.on('close', (e) => {
spawn('docker', ['kill', docker_name]).on('close', code => {
console.log('socket closed...', new Date().toString(), docker_name, child.pid, e)
})
})
ws.on('error', (err) => {
console.log('error occured', err)
})
ws.on('pong', () => {
ws.isAlive = true
})
})