unblockTCPPort method

  1. @override
Future<bool> unblockTCPPort(
  1. int port, {
  2. bool sudo = false,
  3. required Set<int>? allowedPorts,
  4. required bool allowAllPorts,
})
override

Unblocks a specific TCP port.

  • port: The TCP port to be unblocked.
  • sudo: A flag indicating if sudo privileges should be used. Defaults to false.
  • allowedPorts: A set of allowed ports. Ignored if allowAllPorts is true.
  • allowAllPorts: A flag indicating if all ports should be allowed.

Returns a Future that completes with true if the port was successfully unblocked, or false if it failed.

Implementation

@override
Future<bool> unblockTCPPort(int port,
    {bool sudo = false,
    required Set<int>? allowedPorts,
    required bool allowAllPorts}) async {
  _checkValidPort(port);

  if (!allowAllPorts &&
      (allowedPorts == null || !allowedPorts.contains(port))) {
    return false;
  }

  final iptablesBin = await resolveBinaryPathCached('iptables');
  final iptablesArgs = <String>[
    '-D',
    'INPUT',
    '-p',
    'tcp',
    '--dport',
    '$port',
    '-j',
    'DROP',
  ];

  var output = await runCommand(
    iptablesBin,
    iptablesArgs,
    sudo: sudo,
    expectedExitCode: 0,
  );

  if (output == null) {
    return false;
  }

  var blocked = await isBlockedTCPPort(port,
      sudo: sudo, allowedPorts: allowAllPorts ? null : (allowedPorts ?? {}));
  return !blocked;
}