ping: Extracted constant IP_TYPES

master
Felix Stupp 6 years ago
parent 2b52a7f1df
commit b684fa74dd
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -3,6 +3,8 @@ let ipaddr = require("ipaddr.js");
let db = require("../libs/Database").instance(); let db = require("../libs/Database").instance();
let router = express.Router(); let router = express.Router();
const IP_TYPES = ["ipv4", "ipv6"];
function combineToList(a, b) { function combineToList(a, b) {
return a + "\n" + b; return a + "\n" + b;
} }
@ -23,7 +25,7 @@ router.get("/", (req, res) => {
}); });
router.get("/:fqdn", (req, res) => { router.get("/:fqdn", (req, res) => {
db.q.select(["ipv4", "ipv6"]).from("addresses").filter("fqdn", req.params.fqdn).first((e, d) => { db.q.select(IP_TYPES).from("addresses").filter("fqdn", req.params.fqdn).first((e, d) => {
if (e) return res.status(400).send("Error"); if (e) return res.status(400).send("Error");
if (!d) return res.status(404).send("Unknown FQDN"); 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 : "")); res.send(d.ipv4 ? (d.ipv6 ? d.ipv4 + "\n" + d.ipv6 : d.ipv4) : (d.ipv6 ? d.ipv6 : ""));
@ -32,7 +34,7 @@ router.get("/:fqdn", (req, res) => {
router.get("/:fqdn/:code", (req, res) => { router.get("/:fqdn/:code", (req, res) => {
let addr = ipaddr.parse(req.ip); let addr = ipaddr.parse(req.ip);
if (!["ipv4", "ipv6"].includes(addr.kind())) return res.status(400).send("Invalid ip address"); if (!IP_TYPES.includes(addr.kind())) return res.status(400).send("Invalid ip address");
db.query("UPDATE addresses SET " + addr.kind() + " = ? WHERE fqdn = ? AND code = ?", [addr.toString(), req.params.fqdn, req.params.code], (e, d) => { db.query("UPDATE 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"); if (e) return res.status(400).send("Error");
res.send(addr.toString()); res.send(addr.toString());

Loading…
Cancel
Save