Skip to content

Commit

Permalink
Fix DNS modify
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Feb 16, 2024
1 parent dfec027 commit 61121e3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 56 deletions.
66 changes: 15 additions & 51 deletions src/executor/iptables.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { encodeIptablesDoc, genRules, parseIptablesDoc } from '../parsers/iptables.js';
import {
cat,
appendIfNotExist,
Expand All @@ -15,68 +16,39 @@ class IptablesExecutor {
* @param {any} parsed
*/
getByUser(parsed, userName, userID = "") {
const setRules = [
`-A OUTPUT -m owner --uid-owner ${userID} -j REJECT -m comment --comment "${userName}"`,
`-A OUTPUT -m owner --uid-owner ${userName} -j REJECT`,
]

const setRules = genRules(userName, userID);
return parsed.filter.some((x) => setRules.includes(x));
}
/**
*
* @param {string} doc
* @returns {Record<string, string[]>}
*/
parseIptablesDoc(doc = '') {
return doc.split('*').slice(1)
.map(block => '*' + block.trim())
.map(block => block.split("\n").filter(x => !x.startsWith('#')))
.reduce((obj, block) => {
obj[block[0].substring(1)] = block;
return obj;
}, {});
}
encodeIptablesDoc(doc) {
return Object.values(doc).map(x => x.join('\n')).join('\n\n') + '\n';
}
async getParsed() {
await executeLock('iptables', async () => {
await spawnSudoUtil('IPTABLES_GET');
});
return this.parseIptablesDoc(cat(tmpFile));
return parseIptablesDoc(cat(tmpFile));
}
async setAddUser(userName, userID = "") {
const v4 = await executeLock('iptables', async () => {
await spawnSudoUtil('IPTABLES_GET');
var p = this.parseIptablesDoc(cat(tmpFile));
var p = parseIptablesDoc(cat(tmpFile));
const rules = p.filter;

const setRules = [
`-A OUTPUT -m owner --uid-owner ${userID} -j REJECT -m comment --comment "${userName}"`,
`-A OUTPUT -m owner --uid-owner ${userName} -j REJECT`,
]
const setRules = genRules(userName, userID);

if (!appendIfNotExist(rules, setRules)) {
return "Done unchanged for iptables";
}
writeTo(tmpFile, this.encodeIptablesDoc(p));
writeTo(tmpFile, encodeIptablesDoc(p));
await spawnSudoUtil('IPTABLES_SET');
return "Updated for iptables";
});
const v6 = await executeLock('iptables', async () => {
await spawnSudoUtil('IP6TABLES_GET');
var p = this.parseIptablesDoc(cat(tmpFile6));
var p = parseIptablesDoc(cat(tmpFile6));
const rules = p.filter;

const setRules = [
`-A OUTPUT -m owner --uid-owner ${userID} -j REJECT -m comment --comment "${userName}"`,
`-A OUTPUT -m owner --uid-owner ${userName} -j REJECT`,
]
const setRules = genRules(userName, userID);

if (!appendIfNotExist(rules, setRules)) {
return "Done unchanged for iptables";
}
writeTo(tmpFile6, this.encodeIptablesDoc(p));
writeTo(tmpFile6, encodeIptablesDoc(p));
await spawnSudoUtil('IP6TABLES_SET');
return "Updated for ip6tables";
});
Expand All @@ -85,35 +57,27 @@ class IptablesExecutor {
async setDelUser(userName, userID = "") {
const v4 = await executeLock('iptables', async () => {
await spawnSudoUtil('IPTABLES_GET');
var p = this.parseIptablesDoc(cat(tmpFile));
var p = parseIptablesDoc(cat(tmpFile));
const rules = p.filter;

const setRules = [
`-A OUTPUT -m owner --uid-owner ${userID} -j REJECT -m comment --comment "${userName}"`,
`-A OUTPUT -m owner --uid-owner ${userName} -j REJECT`,
]
const setRules = genRules(userName, userID);

if (!deleteIfExist(rules, setRules)) {
return "Done unchanged for iptables";
}
writeTo(tmpFile, this.encodeIptablesDoc(p));
writeTo(tmpFile, encodeIptablesDoc(p));
await spawnSudoUtil('IPTABLES_SET');
return "Updated for iptables";
});
const v6 = await executeLock('iptables', async () => {
await spawnSudoUtil('IP6TABLES_GET');
var p = this.parseIptablesDoc(cat(tmpFile6));
var p = parseIptablesDoc(cat(tmpFile6));
const rules = p.filter;

const setRules = [
`-A OUTPUT -m owner --uid-owner ${userID} -j REJECT -m comment --comment "${userName}"`,
`-A OUTPUT -m owner --uid-owner ${userName} -j REJECT`,
]
const setRules = genRules(userName, userID);

if (!deleteIfExist(rules, setRules)) {
return "Done unchanged for iptables";
}
writeTo(tmpFile6, this.encodeIptablesDoc(p));
writeTo(tmpFile6, encodeIptablesDoc(p));
await spawnSudoUtil('IP6TABLES_SET');
return "Updated for ip6tables";
});
Expand Down
46 changes: 46 additions & 0 deletions src/parsers/iptables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

export const deleteIfRecordExist = ( /** @type {string[]} */ arr, /** @type {string[]} */ record) => {
const idx = arr.findIndex((x) => record.includes(x));
if (idx === -1) {
return false;
} else {
arr.splice(idx, 1);
return true;
}
}

export const appendIfRecordNotExist = ( /** @type {string[]} */ arr, /** @type {string[]} */ record) => {
const idx = arr.findIndex((x) => record.includes(x));
if (idx === -1) {
arr.splice(arr.length - 1, 0, record[0])
return true;
} else {
return false;
}
}

/**
*
* @param {string} doc
* @returns {Record<string, string[]>}
*/
export function parseIptablesDoc(doc = '') {
return doc.split('*').slice(1)
.map(block => '*' + block.trim())
.map(block => block.split("\n").filter(x => !x.startsWith('#')))
.reduce((obj, block) => {
obj[block[0].substring(1)] = block;
return obj;
}, {});
}

export function encodeIptablesDoc(doc) {
return Object.values(doc).map(x => x.join('\n')).join('\n\n') + '\n';
}

export function genRules(userName = "", userID = "") {
return [
`-A OUTPUT -m owner --uid-owner ${userID} -j REJECT -m comment --comment "${userName}"`,
`-A OUTPUT -m owner --uid-owner ${userName} -j REJECT`,
]
}
27 changes: 22 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,36 @@ export const executeLock = function (
});
}

export const deleteIfExist = ( /** @type {string[]} */ arr, /** @type {string[]} */ record) => {
const idx = arr.findIndex((x) => record.includes(x));
// Returns whether an object has a given set of `key:value` pairs.
/**
* @param {any} object
* @param {Record<string, any>} attrs
*/
export function isMatch(object, attrs) {
var _keys = Object.keys(attrs),
length = _keys.length;
if (object == null) return !length;
var obj = Object(object);
for (var i = 0; i < length; i++) {
var key = _keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
}

export const deleteIfExist = ( /** @type {any[]} */ arr, /** @type {any} */ record) => {
const idx = arr.findIndex((x) => isMatch(x, record));
if (idx === -1) {
return false;
} else {
arr.splice(idx, 1);
return true;
}
}
export const appendIfNotExist = ( /** @type {string[]} */ arr, /** @type {string[]} */ record) => {
const idx = arr.findIndex((x) => record.includes(x));
export const appendIfNotExist = ( /** @type {any[]} */ arr, /** @type {{}} */ record) => {
const idx = arr.findIndex((x) => isMatch(x, record));
if (idx === -1) {
arr.splice(arr.length - 1, 0, record[0])
arr.push(record);
return true;
} else {
return false;
Expand Down

0 comments on commit 61121e3

Please sign in to comment.