Browse Source

implement isAddedOrUpdated function

develop
ray-1337 2 years ago
parent
commit
c2fb0a9238
No known key found for this signature in database GPG Key ID: DED41DCC150FCD32
  1. 4
      src/Util.ts
  2. 6
      src/methods/Allow.ts
  3. 6
      src/methods/Deny.ts

4
src/Util.ts

@ -2,6 +2,10 @@ import { execSync, exec } from "node:child_process";
import { getuid, versions, platform } from "node:process"; import { getuid, versions, platform } from "node:process";
import { promisify } from "node:util"; import { promisify } from "node:util";
export function isAddedOrUpdated(response: string) {
return /((adde|update)d)/gi.test(response.toLowerCase());
};
export const shouldDryRunDuringTesting = process.env.npm_lifecycle_event === "test" ? "--dry-run" : ""; export const shouldDryRunDuringTesting = process.env.npm_lifecycle_event === "test" ? "--dry-run" : "";
export async function runCommand(command: string) { export async function runCommand(command: string) {

6
src/methods/Allow.ts

@ -1,4 +1,4 @@
import { checkAppropriatePort, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; import { checkAppropriatePort, isAddedOrUpdated, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util";
import type { PortProtocol } from "../Typings"; import type { PortProtocol } from "../Typings";
/** /**
@ -11,7 +11,7 @@ async function port(port: number, protocol?: PortProtocol) {
if (!checkPort) return false; if (!checkPort) return false;
let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} allow ${port}${protocol ? `/${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} allow ${port}${protocol ? `/${protocol}` : ""}`);
return command ? command.toLowerCase().match(/(added)/gi) !== null : false; return command ? isAddedOrUpdated(command) : false;
} catch (err) { } catch (err) {
throw err; throw err;
}; };
@ -33,7 +33,7 @@ async function address(address: string, port?: number, protocol?: PortProtocol)
}; };
let command = await runCommand(`echo "y" | sudo ufw allow from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw allow from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`);
return command ? command.toLowerCase().match(/(added)/gi) !== null : false; return command ? isAddedOrUpdated(command) : false;
} catch (err) { } catch (err) {
throw err; throw err;
}; };

6
src/methods/Deny.ts

@ -1,4 +1,4 @@
import { checkAppropriatePort, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; import { checkAppropriatePort, isAddedOrUpdated, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util";
import type { PortProtocol } from "../Typings"; import type { PortProtocol } from "../Typings";
/** /**
@ -11,7 +11,7 @@ async function port(port: number, protocol?: PortProtocol) {
if (!checkPort) return false; if (!checkPort) return false;
let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} deny ${port}${protocol ? `/${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} deny ${port}${protocol ? `/${protocol}` : ""}`);
return command ? command.toLowerCase().match(/(added)/gi) !== null : false; return command ? isAddedOrUpdated(command) : false;
} catch (err) { } catch (err) {
throw err; throw err;
}; };
@ -33,7 +33,7 @@ async function address(address: string, port?: number, protocol?: PortProtocol)
}; };
let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`);
return command ? command.toLowerCase().match(/(added)/gi) !== null : false; return command ? isAddedOrUpdated(command) : false;
} catch (err) { } catch (err) {
throw err; throw err;
}; };

Loading…
Cancel
Save