-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (51 loc) · 1.55 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
require('dotenv').config()
const http = require('http')
const PORT = process.env.PORT || 3000
const HOSTNAME = process.env.HOST || '0.0.0.0'
const server = http.createServer()
server.on('listening', function () {
console.log(`Server running at http://${HOSTNAME}:${PORT}/`)
})
server.on('request', function (req, res) {
if (req.url === '/health' && req.method === 'HEAD') {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('OK\n')
return
}
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405, { 'Content-Type': 'text/plain' })
res.end('Method not allowed\n')
return
}
const path = req.url
const uri = `${process.env.OSS_ENDPOINT}${path}`
http.get(uri, (result) => {
if (req.method === 'HEAD') {
result.destroy()
res.writeHead(result.statusCode, result.headers)
res.end()
return
}
if (!!result.headers[`x-oss-meta-${process.env.IS_SENSITIVE_META_NAME}`]) {
result.destroy()
const newUri = `${process.env.REDIRECT_DOMAIN}${path}`
if (req.headers.referer?.startsWith(process.env.ALLOW_REDIRECT_REFERER)) {
res.writeHead(301, { Location: newUri })
res.end()
return
}
const text = 'Forbidden\n'
res.writeHead(403, {
...result.headers,
'x-kemono-redirect': newUrl,
'content-type': 'text/plain',
'content-length': text.length,
})
res.end(text)
return
}
res.writeHead(result.statusCode, result.headers)
result.pipe(res)
})
})
server.listen(PORT, HOSTNAME)