add simple syntax and parser of syntax

This commit is contained in:
FullGreaM 2025-01-10 04:37:05 +03:00
parent 6fb0660894
commit ebbc862326
7 changed files with 99 additions and 1 deletions

3
components/_component.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = class Component {
constructor () {}
};

View File

@ -0,0 +1,5 @@
const Component = require("./_component");
module.exports = class HTTPReverse extends Component {
constructor () { super(); }
};

3
components/index.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
"revHTTP": require("./http-reverse"),
};

1
firewall/GLOBAL.txt Normal file
View File

@ -0,0 +1 @@
deny 172.202.246.146;

71
routemap-parser.js Normal file
View File

@ -0,0 +1,71 @@
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);
}

11
routemap.txt Normal file
View File

@ -0,0 +1,11 @@
127.0.0.1:9889 {
type revHTTP;
target https://example.org; # RRR
domain localhost {
firewall whitelist GLOBAL;
type revHTTP;
target https://github.org;
};
}
127.0.0.1:7889 {}

View File

@ -8,6 +8,8 @@ const path = require('path');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
global.config = require("./config.json"); global.config = require("./config.json");
const rtmap = require("./routemap-parser");
const app = express(); const app = express();
const address = global.config.net.address ?? "127.0.0.1"; const address = global.config.net.address ?? "127.0.0.1";
@ -15,7 +17,7 @@ const reverseTo = global.config.target ?? "https://example.org";
function logger (...p) { function logger (...p) {
return; return;
console.log("[LOGGER]:", ...p); console.log("[DEBUG.main]:", ...p);
} }
app.use('/*', async (req, res, next) => { // Myself body parser :) app.use('/*', async (req, res, next) => { // Myself body parser :)
@ -58,6 +60,8 @@ app.use("*", async (req, res) => {
}); });
}); });
rtmap.parse();
app.listen(global.config.net.port, address, (err) => { app.listen(global.config.net.port, address, (err) => {
if (err) { if (err) {
throw err; throw err;