-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (65 loc) · 2.33 KB
/
index.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
// Be sure to add these ENV variables!
const {
KEYGEN_ACCOUNT_ID,
KEYGEN_PRODUCT_ID,
KEYGEN_POLICY_ID,
PORT = 8080
} = process.env
const https = require('https')
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const app = express()
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
app.use(bodyParser.json({ type: 'application/json' }))
app.use(morgan('combined'))
app.get('/download/:platform/:file', async (req, res) => {
const { platform, file } = req.params
const { license } = req.query
https.get(`https://dist.keygen.sh/v1/${KEYGEN_ACCOUNT_ID}/${KEYGEN_PRODUCT_ID}/releases/${platform}/${file}?policy=${KEYGEN_POLICY_ID}&key=${license}`, dist => {
res.statusCode = dist.statusCode
res.set(dist.headers)
dist.pipe(res)
})
})
app.get('/latest/:platform/:extension', async (req, res) => {
const { platform, extension } = req.params
const { license } = req.query
https.get(`https://dist.keygen.sh/v1/${KEYGEN_ACCOUNT_ID}/${KEYGEN_PRODUCT_ID}/latest/${platform}/${extension}?policy=${KEYGEN_POLICY_ID}&key=${license}`, dist => {
if (dist.statusCode === 307) {
https.get(dist.headers.location, redirect => {
res.statusCode = redirect.statusCode
res.set(redirect.headers)
redirect.pipe(res)
})
} else {
res.statusCode = dist.statusCode
res.set(dist.headers)
dist.pipe(res)
}
})
})
app.get('/update/:platform/:extension/:version', async (req, res) => {
const { platform, extension, version } = req.params
const { license } = req.query
https.get(`https://dist.keygen.sh/v1/${KEYGEN_ACCOUNT_ID}/${KEYGEN_PRODUCT_ID}/update/${platform}/${extension}/${version}?policy=${KEYGEN_POLICY_ID}&key=${license}`, dist => {
if (dist.statusCode === 307) {
https.get(dist.headers.location, redirect => {
res.statusCode = redirect.statusCode
res.set(redirect.headers)
redirect.pipe(res)
})
} else {
res.statusCode = dist.statusCode
res.set(dist.headers)
dist.pipe(res)
}
})
})
process.on('unhandledRejection', err => {
console.error(`Unhandled rejection: ${err}`, err.stack)
})
const server = app.listen(PORT, 'localhost', () => {
const { address, port } = server.address()
console.log(`Listening at http://${address}:${port}`)
})