UNPKG

node-red-contrib-vban

Version:
55 lines (54 loc) 2.49 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const Node_1 = require("../lib/Node"); const registerNode_1 = require("../lib/registerNode"); const Security_1 = require("../lib/Security"); const NODE_NAME = 'vban-ip-filter'; class IPFilter extends Node_1.Node { init() { const _super = Object.create(null, { init: { get: () => super.init } }); return __awaiter(this, void 0, void 0, function* () { yield _super.init.call(this); this.security = new Security_1.Security(this.definition.allowedIps); this.node.on('input', (msg, send) => { var _a, _b; const sender = (_a = msg.payload) === null || _a === void 0 ? void 0 : _a.sender; const sendError = (error) => { const errorMessage = {}; errorMessage.error = error; this.send([null, errorMessage], send); }; if (!sender) { sendError('fail to get sender from payload'); return; } try { if (!((_b = this.security) === null || _b === void 0 ? void 0 : _b.check(sender.address))) { sendError(`ip ${sender.address} seems not allowed`); return; } } catch (e) { const topic = `fail to parse "${sender.address}", seems to doesn't be an IPv4 or an IPV6`; sendError(topic); this.node.error(e); } this.send([msg]); }); }); } } module.exports = (RED) => { (0, registerNode_1.registerNode)(RED, NODE_NAME, IPFilter); };