-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathserverless.js
96 lines (84 loc) · 3.25 KB
/
serverless.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
import Router from 'router'
import addonInterface from "./addon.js"
import landingTemplate from "./lib/util/landingTemplate.js"
import StreamProvider from './lib/stream-provider.js'
import { decode } from 'urlencode'
import qs from 'querystring'
import requestIp from 'request-ip'
import { getManifest } from './lib/util/manifest.js'
import { parseConfiguration } from './lib/util/configuration.js'
import { BadTokenError, BadRequestError, AccessDeniedError } from './lib/util/error-codes.js'
const router = new Router();
router.get('/', (_, res) => {
res.redirect('/configure')
})
router.get('/:configuration?/configure', (req, res) => {
const config = parseConfiguration(req.params.configuration)
const landingHTML = landingTemplate(addonInterface.manifest, config)
res.setHeader('content-type', 'text/html')
res.end(landingHTML)
})
router.get('/:configuration?/manifest.json', (req, res) => {
const config = parseConfiguration(req.params.configuration)
res.setHeader('content-type', 'application/json; charset=utf-8')
res.end(JSON.stringify(getManifest(config)))
})
router.get(`/:configuration?/:resource/:type/:id/:extra?.json`, (req, res, next) => {
const { resource, type, id } = req.params
const config = parseConfiguration(req.params.configuration)
const extra = req.params.extra ? qs.parse(req.url.split('/').pop().slice(0, -5)) : {}
addonInterface.get(resource, type, id, extra, config)
.then(resp => {
let cacheHeaders = {
cacheMaxAge: 'max-age',
staleRevalidate: 'stale-while-revalidate',
staleError: 'stale-if-error'
}
const cacheControl = Object.keys(cacheHeaders)
.map(prop => Number.isInteger(resp[prop]) && cacheHeaders[prop] + '=' + resp[prop])
.filter(val => !!val).join(', ')
res.setHeader('Cache-Control', `${cacheControl}, private`)
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(JSON.stringify(resp))
})
.catch(err => {
console.error(err)
handleError(err, res)
})
})
router.get('/resolve/:debridProvider/:debridApiKey/:id/:hostUrl', (req, res) => {
const clientIp = requestIp.getClientIp(req)
StreamProvider.resolveUrl(req.params.debridProvider, req.params.debridApiKey, req.params.id, decode(req.params.hostUrl), clientIp)
.then(url => {
res.redirect(url)
})
.catch(err => {
console.log(err)
handleError(err, res)
})
})
router.get('/ping', (_, res) => {
res.statusCode = 200
res.end()
})
function handleError(err, res) {
if (err == BadTokenError) {
res.writeHead(401)
res.end(JSON.stringify({ err: 'Bad token' }))
} else if (err == AccessDeniedError) {
res.writeHead(403)
res.end(JSON.stringify({ err: 'Access denied' }))
} else if (err == BadRequestError) {
res.writeHead(400)
res.end(JSON.stringify({ err: 'Bad request' }))
} else {
res.writeHead(500)
res.end(JSON.stringify({ err: 'Server error' }))
}
}
export default function (req, res) {
router(req, res, function () {
res.statusCode = 404;
res.end();
});
}