listBlockedTCPPorts method

  1. @override
Future<Set<int>> listBlockedTCPPorts({
  1. bool sudo = false,
  2. Set<int>? allowedPorts,
})
override

Lists all the currently blocked TCP ports.

  • sudo: A flag indicating if sudo privileges should be used. Defaults to false.
  • allowedPorts: A set of allowed ports, or null to allow all ports.

Returns a Future that completes with a Set of blocked TCP ports.

Implementation

@override
Future<Set<int>> listBlockedTCPPorts(
    {bool sudo = false, Set<int>? allowedPorts}) async {
  final iptablesBin = await resolveBinaryPathCached('iptables');
  final iptablesArgs = <String>['-L', 'INPUT', '-n', '-v'];

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

  if (output == null || output.isEmpty) return {};

  final regExpPort = RegExp(r'dpt:(\d\d+)');

  final blockedPorts = <int>{};

  for (final line in output.split('\n')) {
    if (line.contains('DROP') || line.contains('REJECT')) {
      final match = regExpPort.firstMatch(line);
      if (match != null) {
        var g1 = match.group(1)!;
        var p = int.tryParse(g1.trim());
        if (p != null && p >= 10) {
          blockedPorts.add(p);
        }
      }
    }
  }

  if (allowedPorts != null) {
    blockedPorts.retainAll(allowedPorts);
  }

  return blockedPorts;
}