blockTCPPort method
Blocks a specific TCP port.
port: The TCP port to be blocked.sudo: A flag indicating if sudo privileges should be used. Defaults tofalse.allowedPorts: A set of allowed ports. Ignored ifallowAllPortsis true.allowAllPorts: A flag indicating if all ports should be allowed.
Returns a Future that completes with true if the port was successfully blocked, or false if it failed.
Implementation
@override
Future<bool> blockTCPPort(int port,
{bool sudo = false,
required Set<int>? allowedPorts,
required bool allowAllPorts}) async {
_checkValidPort(port);
if (!allowAllPorts &&
(allowedPorts == null || !allowedPorts.contains(port))) {
return false;
}
// Block on every available family so the port is actually closed for both
// IPv4 and IPv6 traffic.
final bins = await _resolveFirewallBinaries();
var allOk = true;
for (final bin in bins) {
var output = await runCommand(
bin,
<String>['-A', 'INPUT', '-p', 'tcp', '--dport', '$port', '-j', 'DROP'],
sudo: sudo,
expectedExitCode: 0,
);
if (output == null) allOk = false;
}
if (!allOk) {
return false;
}
var blocked = await isBlockedTCPPort(port,
sudo: sudo, allowedPorts: allowAllPorts ? null : (allowedPorts ?? {}));
return blocked;
}