Added ping router

master
Felix Stupp 6 years ago
parent 2732f76847
commit 201a980344
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -79,6 +79,7 @@ function route(path, route) {
}
route("/ssh", "ssh");
route("/ping", "ping");
// catch 404 and forward to error handler
app.use(function (req, res, next) {

@ -0,0 +1,28 @@
let express = require("express");
let ipaddr = require("ipaddr.js");
let db = require("../libs/Database").instance();
let router = express.Router();
router.use((req, res, next) => {
res.type('plain/txt');
next();
});
router.get("/:fqdn", (req, res) => {
db.q.select(["ipv4", "ipv6"]).from("addresses").where("fqdn", req.params.fqdn).first((e, d) => {
if (e) return res.status(400).send("Error");
if (!d) return res.status(404).send("Unknown FQDN");
res.send(d.ipv4 ? (d.ipv6 ? d.ipv4 + "\n" + d.ipv6 : d.ipv4) : (d.ipv6 ? d.ipv6 : ""));
});
});
router.get("/:fqdn/:code", (req, res) => {
let addr = ipaddr.parse(req.ip);
if (!["ipv4", "ipv6"].includes(addr.kind())) return res.status(400).send("Invalid protocoll");
db.query("UDPATE addresses SET " + addr.kind() + " = ? WHERE fqdn = ? AND code = ?", [addr.toString(), req.params.fqdn, req.params.code], (e, d) => {
if (e) return res.status(400).send("Error");
res.send(d);
});
});
module.exports = router;
Loading…
Cancel
Save