processCommand method

Future<bool> processCommand(
  1. String? command
)

Processes a command entered by the user.

  • command: The command line to execute.

Returns a Future that completes with true if the command was processed, or false if the command was unrecognized or failed.

Implementation

Future<bool> processCommand(String? command) async {
  if (command == null) return false;
  command = command.trim();
  if (command.isEmpty) return false;

  var parts = command.split(RegExp(r'\s+'));

  var cmd = parts[0].trim().toLowerCase();

  switch (cmd) {
    case 'l':
    case 'ls':
    case 'list':
      {
        var type = parts.length > 1 ? parts[1].trim().toLowerCase() : 'all';

        switch (type) {
          case 'port':
          case 'ports':
          case 'block':
          case 'blocks':
          case 'blocked':
            {
              var ports = await listBlockedTCPPorts();
              print('-- Blocked ports: $ports');
              return true;
            }

          case 'address':
          case 'addresses':
          case 'accept':
          case 'accepts':
          case 'accepted':
            {
              var accepts = await listAcceptedAddressesOnTCPPorts();
              print('-- Accepted addresses: $accepts');
              return true;
            }

          case 'all':
            {
              var ports = await listBlockedTCPPorts();
              print('-- Blocked ports: $ports');

              var accepts = await listAcceptedAddressesOnTCPPorts();
              print('-- Accepted addresses: $accepts');

              return true;
            }

          default:
            {
              print('** Unknown list type: $type');
              return false;
            }
        }
      }

    case 'block':
      {
        var port = int.tryParse(parts[1].trim());
        if (port == null || port < 10) {
          print('** Invalid port: $port');
          return false;
        }

        var blocked = await blockTCPPort(port);
        print('-- Blocked $port: $blocked');
        return true;
      }

    case 'unblock':
      {
        var port = int.tryParse(parts[1].trim());
        if (port == null || port < 10) {
          print('** Invalid port: $port');
          return false;
        }

        var blocked = await unblockTCPPort(port);
        print('-- Unblocked $port: $blocked');
        return true;
      }

    case 'accept':
      {
        var address = parts[1].trim();
        if (address.isEmpty) {
          print('** Empty address');
          return false;
        }

        var port = int.tryParse(parts[2].trim());
        if (port == null || port < 10) {
          print('** Invalid port: $port');
          return false;
        }

        var accepted = await acceptAddressOnTCPPort(address, port);
        print('-- Accepted address `$address` on port $port: $accepted');
        return true;
      }

    case 'unaccept':
      {
        var address = parts[1].trim();
        if (address.isEmpty) {
          print('** Empty address');
          return false;
        }

        var port = parts.length > 2 ? int.tryParse(parts[2].trim()) : null;

        var unaccepted = await unacceptAddressOnTCPPort(address, port);
        print(
            '-- Unaccepted address `$address` on port ${port ?? '*'}: $unaccepted');
        return true;
      }

    case 'myip':
      {
        var ip = await myIP();
        print('-- My IP: $ip');

        return true;
      }

    case 'my':
      {
        var type = parts.length > 1 ? parts[1].trim().toLowerCase() : '';

        if (type == 'ip') {
          var ip = await myIP();
          print('-- My IP: $ip');

          return true;
        }

        return false;
      }

    case 'exit':
      {
        var ok = await disconnect();
        print('-- Disconnect: $ok');
        print('[EXIT] By!');
        exit(0);
      }

    default:
      {
        print('** Unknown command: `$cmd`');
        return false;
      }
  }
}