Added ping router
parent
2732f76847
commit
201a980344
@ -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…
Reference in New Issue