72 lines
2.8 KiB
JavaScript
72 lines
2.8 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const components = require("./components");
|
|
|
|
// General regexps
|
|
// const FIND_ADDRESS_INSTR_REGEX = /([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]{1,5}\s{0,}\{[\s\n\r0-9a-z;:\/\."',]{0,}\}/gmi;
|
|
const DOMAINER_REGEX = /domain\s{1,}[a-z0-9\.]{1,}\s{1,}\{[\s\n\r0-9a-z;:\/\."',]{0,}\}/gmi;
|
|
const ADDRESS_WITH_PORT_REGEX = /([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]{1,5}/gmi
|
|
const FIND_ADDRESS_INSTR_REGEX = /([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]{1,5}\s{0,}\{([\s\n\r0-9a-z;:\/\."',]|domain\s{1,}[a-z0-9\.]{1,}\s{1,}\{[\s\n\r0-9a-z;:\/\."',]{0,}\}){0,}\}/gmi;
|
|
const COMMENT_REGEX = /#.{0,}(\n|\r|$)/gmi;
|
|
|
|
// Instruction regexps
|
|
function readInstructions (instructions, fullCode) {
|
|
const syntaxTree = { domains: {}, firewall: false };
|
|
|
|
for (let instruction of instructions) {
|
|
if (!instruction)
|
|
break;
|
|
const instructionParts = instruction.split(/\s{1,}/);
|
|
const mainOperator = instructionParts[0];
|
|
switch (mainOperator) {
|
|
case "type":
|
|
if (instructionParts.length !== 2)
|
|
throw new SyntaxError("Invalid syntax of operator `type`: type <type_of_connection>");
|
|
const SelectedComponent = components[instructionParts[1]];
|
|
if (SelectedComponent === undefined)
|
|
throw new SyntaxError(`Unknown component: ${instructionParts[1]}. Valid components: ${Object.keys(components).join(", ")}`);
|
|
syntaxTree.Type = SelectedComponent;
|
|
break;
|
|
case "target":
|
|
if (instructionParts.length !== 2)
|
|
throw new SyntaxError("Invalid syntax of operator `target`: target <connection_target>");
|
|
syntaxTree.target = instructionParts[1];
|
|
break;
|
|
case "#instuction:domain":
|
|
break;
|
|
default:
|
|
throw new SyntaxError(`Unknown routemap's operator: ${mainOperator}`);
|
|
}
|
|
}
|
|
|
|
return syntaxTree;
|
|
}
|
|
|
|
function logger (...p) {
|
|
//return;
|
|
console.debug("[DEBUG.routemap]:", ...p);
|
|
}
|
|
|
|
module.exports.parse = function () {
|
|
const syntaxTree = new Object();
|
|
const routemapContent = fs.readFileSync(path.join(__dirname, "./routemap.txt"), { encoding: "utf-8" })
|
|
.replace(COMMENT_REGEX, "\n");
|
|
const findedAdrInstructions = routemapContent.match(FIND_ADDRESS_INSTR_REGEX);
|
|
logger("findedAdrInstructions:", findedAdrInstructions);
|
|
if (findedAdrInstructions === null)
|
|
throw new SyntaxError("routemap.txt is empty");
|
|
for (let itemAdrInst of findedAdrInstructions) {
|
|
itemAdrInst = itemAdrInst.replace(/[\n\r]{1,}/g, "");
|
|
const content = itemAdrInst
|
|
.replace(ADDRESS_WITH_PORT_REGEX, "")
|
|
.replace(DOMAINER_REGEX, "#instuction:domain")
|
|
.trim()
|
|
.slice(1, -1)
|
|
.split(/\s{0,};\s{0,}/g).map(x => x.trim());
|
|
logger("itemAdrInst:", itemAdrInst, "with content:", content);
|
|
const localSyntaxTree = readInstructions(content, itemAdrInst);
|
|
syntaxTree[itemAdrInst.match(ADDRESS_WITH_PORT_REGEX)[0]] = localSyntaxTree;
|
|
}
|
|
logger("Full syntax tree:", syntaxTree);
|
|
}
|