node-red-contrib-vban
Version:
Nodes to interact with vban protocol
47 lines (46 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Security = void 0;
const ip_address_1 = require("ip-address");
const CustomError_1 = require("./CustomError");
class Security {
constructor(allowedIps) {
this.ipsAllowed = [];
this.ipsAllowed = (allowedIps !== null && allowedIps !== void 0 ? allowedIps : '')
.replace(/\s+/g, '')
.replace(/;/g, ',')
.split(',')
.map((i) => i.trim())
.filter((i) => !!i)
.map((ip) => {
try {
return this.parseIp(ip);
}
catch (error) {
const topic = `ip "${ip}" seems to doesn't be an IPv4 or an IPv6`;
throw new CustomError_1.CustomError(topic, {
ip,
error
}, error);
}
});
}
parseIp(ip) {
try {
return new ip_address_1.Address4(ip);
}
catch (e) {
if (e.name === 'AddressError') {
return new ip_address_1.Address6(ip);
}
else {
throw e;
}
}
}
check(address) {
let ip = this.parseIp(address);
return !!this.ipsAllowed.find((ipAllowed) => ip.isInSubnet(ipAllowed));
}
}
exports.Security = Security;