-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth-proxy.js
More file actions
37 lines (29 loc) · 852 Bytes
/
oauth-proxy.js
File metadata and controls
37 lines (29 loc) · 852 Bytes
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
const http = require('http');
const url = require('url');
const { isAllowed } = require('./utils/url');
const { createConfig } = require('./utils/config');
function createServer({
port,
proxyParam,
allow,
} = createConfig()) {
const server = http.createServer((req, res) => {
const parsedURL = url.parse(req.url);
const search = new URLSearchParams(parsedURL.search);
const proxyTo = search.get(proxyParam);
search.delete(proxyParam);
if (isAllowed(proxyTo, allow)) {
res.writeHead(302, { 'Location': `${proxyTo}?${search}` });
res.end();
return;
}
res.writeHead(401);
res.end();
});
server.listen(port, () => {
console.log(`Server was started on port ${port}`);
});
}
module.exports = {
createServer,
}