Refactor VPN proxy client implementation
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { spawnSync } from 'node:child_process';
|
||||
|
||||
const options = { encoding: 'utf8' as const };
|
||||
|
||||
export function setGatewayInterception(enabled: boolean, chain: string, run: typeof spawnSync = spawnSync) {
|
||||
const rule = ['-w', '-t', 'mangle', 'PREROUTING', '-j', chain];
|
||||
const exists = run('iptables', [...rule.slice(0, 3), '-C', ...rule.slice(3)], options).status === 0;
|
||||
|
||||
if (!enabled) {
|
||||
if (exists) run('iptables', [...rule.slice(0, 3), '-D', ...rule.slice(3)], options);
|
||||
return;
|
||||
}
|
||||
if (exists) return;
|
||||
|
||||
const result = run(
|
||||
'iptables',
|
||||
[...rule.slice(0, 3), '-I', 'PREROUTING', '1', '-j', chain],
|
||||
options,
|
||||
);
|
||||
if (result.status !== 0) {
|
||||
throw new Error((result.stderr || 'Не удалось включить Gateway VPN').trim());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user