-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
34 lines (30 loc) · 1.19 KB
/
proxy.js
File metadata and controls
34 lines (30 loc) · 1.19 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
// Load the local config file
const config = require('./config.json');
var express = require("express"),
requestProxy = require("express-request-proxy")
var app = express();
app.all("/api/:apiversion/servers/:server/zones/:zone", function(req, res, next) {
var proxy = requestProxy({
url: config.backend + req.path,
headers: {
"X-API-Key": config.XApiKey
}
});
// Check api-key with zone
if (req.get("X-API-Key") in config.keys) {
// API Key is valid. Check if it matches the zone.
if (config.keys[req.get("X-API-Key")] == req.params.zone) {
// API Key is valid for this zone. Proxy the api call to the pdns backend.
proxy(req, res, next);
} else {
// API Key used in the api call does not match the zone
res.status(403).send('Not authorized for zone "' + req.params.zone + '"!');
console.log("WARN: API-Key '" + req.get("X-API-Key") + "' tried accessing zone " + req.params.zone);
}
} else {
// API Key not present in config.keys
res.status(401).send('Unknown API Key!');
}
});
// Start the proxy
app.listen(config.proxyPort, () => console.log('pdns-auth-proxy started and listening on port ' + config.proxyPort +'!'));