|
|
|
@ -3,6 +3,8 @@ let ipaddr = require("ipaddr.js");
|
|
|
|
|
let db = require("../libs/Database").instance();
|
|
|
|
|
let router = express.Router();
|
|
|
|
|
|
|
|
|
|
const IP_TYPES = ["ipv4", "ipv6"];
|
|
|
|
|
|
|
|
|
|
function combineToList(a, b) {
|
|
|
|
|
return a + "\n" + b;
|
|
|
|
|
}
|
|
|
|
@ -23,7 +25,7 @@ router.get("/", (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 (!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 : ""));
|
|
|
|
@ -32,7 +34,7 @@ router.get("/:fqdn", (req, res) => {
|
|
|
|
|
|
|
|
|
|
router.get("/:fqdn/:code", (req, res) => {
|
|
|
|
|
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) => {
|
|
|
|
|
if (e) return res.status(400).send("Error");
|
|
|
|
|
res.send(addr.toString());
|
|
|
|
|