-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
47 lines (36 loc) · 1.37 KB
/
proxy.js
File metadata and controls
47 lines (36 loc) · 1.37 KB
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
const port = 3001;
const http = require('http');
const httpProxy = require('http-proxy');
const auth = require('basic-auth');
require('dotenv').config();
let { SECURE_ENABLED, SECURE_USER, SECURE_PASS } = process.env;
SECURE_ENABLED = SECURE_ENABLED === true || SECURE_ENABLED === 'true';
if (SECURE_ENABLED && (!SECURE_USER || !SECURE_PASS))
throw 'Missing SECURE_USER or SECURE_PASS. Please specify both in the environment variables.';
if (!SECURE_ENABLED)
console.warn('Secure not enabled, the database is open to anyone that can access the url');
const proxy = httpProxy.createProxyServer({});
http.createServer((req, res) => {
if (SECURE_ENABLED) {
const user = auth(req);
if (!user || user.name !== SECURE_USER || user.pass !== SECURE_PASS) {
res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Secure Prisma Studio"' })
res.end('Access denied');
return
}
}
proxy.web(req, res, { target: 'http://localhost:3000' });
}).listen(port, () => {
// to ensure this log is at bottom
setTimeout(log, 1000 * 2);
setTimeout(log, 1000 * 30);
});
function log() {
console.log('');
console.log(`Proxied Prisma Studio available at port ${port}`);
if (SECURE_ENABLED) {
console.log(`Username: ${SECURE_USER}`);
console.log(`Password: ${SECURE_PASS}`);
}
console.log('');
}