removed unused stuff, changed natQ to use opnsense api
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { error, info, success } from "../util/Console.js";
|
||||
import { runRouterCmds } from "./sshExec.js";
|
||||
|
||||
const queue = [];
|
||||
let running = false;
|
||||
@@ -13,38 +12,95 @@ async function processQueue() {
|
||||
if (queue.length === 0) { running = false; return; }
|
||||
running = true;
|
||||
const t = queue.shift();
|
||||
try { await t(); } catch (err) { error('Queue error: ' + String(err)); }
|
||||
try { await t(); } catch (err) { error('Q error ' + err); }
|
||||
setImmediate(processQueue);
|
||||
}
|
||||
|
||||
export function enqueueCmd(fn) {
|
||||
enqueue(() => fn());
|
||||
async function opnsenseRequest(method, path, body = null) {
|
||||
const auth = Buffer.from(`${process.env.OPNSENSE_KEY}:${process.env.OPNSENSE_SECRET}`).toString('base64');
|
||||
const opts = {
|
||||
method,
|
||||
headers: {
|
||||
'Authorization': `Basic ${auth}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
};
|
||||
if (body) opts.body = JSON.stringify(body);
|
||||
const res = await fetch(`${process.env.OPNSENSE_HOST}${path}`, opts);
|
||||
const text = await res.text();
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
error(`got non-json response: ${text}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function enqueueNatAdd(ip, port, proto='tcp') {
|
||||
async function applyRules() {
|
||||
await opnsenseRequest('POST', '/api/firewall/d_nat/apply');
|
||||
}
|
||||
|
||||
export function enqueueNatAdd(ip, port, proto = 'tcp') {
|
||||
enqueue(async () => {
|
||||
info(`NAT ADD ${proto} ${ip}:${port}`);
|
||||
await runRouterCmds([
|
||||
'conf t',
|
||||
`ip nat inside source static tcp ${ip} ${port} interface ${process.env.ROUTER_WAN_IF || 'gi0'} ${port}`,
|
||||
`ip nat inside source static udp ${ip} ${port} interface ${process.env.ROUTER_WAN_IF || 'gi0'} ${port}`,
|
||||
'end',
|
||||
'wr mem'
|
||||
]);
|
||||
success(`NAT ADDED ${proto} ${ip}:${port}`);
|
||||
info(`adding rule udp+tcp at ${ip}:${port}`);
|
||||
|
||||
await opnsenseRequest('POST', '/api/firewall/d_nat/addRule', {
|
||||
rule: {
|
||||
interface: 'wan',
|
||||
protocol: 'tcp',
|
||||
destination: {
|
||||
network: 'wanip',
|
||||
port: `${port}`,
|
||||
},
|
||||
target: ip,
|
||||
'local-port': `${port}`,
|
||||
descr: `pelican-${ip}-${port}-tcp`,
|
||||
disabled: '0',
|
||||
}
|
||||
});
|
||||
|
||||
await opnsenseRequest('POST', '/api/firewall/d_nat/addRule', {
|
||||
rule: {
|
||||
interface: 'wan',
|
||||
protocol: 'udp',
|
||||
destination: {
|
||||
network: 'wanip',
|
||||
port: `${port}`,
|
||||
},
|
||||
target: ip,
|
||||
'local-port': `${port}`,
|
||||
descr: `pelican-${ip}-${port}-udp`,
|
||||
disabled: '0',
|
||||
}
|
||||
});
|
||||
|
||||
await applyRules();
|
||||
success(`added rule udp+tcp at ${ip}:${port}`);
|
||||
});
|
||||
}
|
||||
|
||||
export function enqueueNatRemove(ip, port, proto='tcp') {
|
||||
export function enqueueNatRemove(ip, port, proto = 'tcp') {
|
||||
enqueue(async () => {
|
||||
info(`NAT DEL ${proto} ${ip}:${port}`);
|
||||
await runRouterCmds([
|
||||
'conf t',
|
||||
`no ip nat inside source static tcp ${ip} ${port} interface ${process.env.ROUTER_WAN_IF || 'gi0'} ${port}`,
|
||||
`no ip nat inside source static udp ${ip} ${port} interface ${process.env.ROUTER_WAN_IF || 'gi0'} ${port}`,
|
||||
'end',
|
||||
'wr mem'
|
||||
]);
|
||||
success(`NAT DELETED ${proto} ${ip}:${port}`);
|
||||
info(`deleting rule udp+tcp at ${ip}:${port}`);
|
||||
|
||||
const result = await opnsenseRequest('POST', '/api/firewall/d_nat/searchRule', {
|
||||
current: 1,
|
||||
rowCount: 1000,
|
||||
searchPhrase: `pelican-${ip}-${port}`,
|
||||
})
|
||||
|
||||
const rows = result?.rows || [];
|
||||
|
||||
for (const rule of rows) {
|
||||
if (
|
||||
rule.descr === `pelican-${ip}-${port}-udp` ||
|
||||
rule.descr === `pelican-${ip}-${port}-tcp`
|
||||
) {
|
||||
await opnsenseRequest('POST', `/api/firewall/d_nat/delRule/${rule.uuid}`);
|
||||
}
|
||||
}
|
||||
|
||||
await applyRules();
|
||||
success(`deleted rule udp+tcp at ${ip}:${port}`);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user