acceptAddressOnTCPPort method
Future<bool>
acceptAddressOnTCPPort(
- String address,
- int port, {
- bool sudo = false,
- required Set<
int> ? allowedPorts, - required bool allowAllPorts,
override
Add rule to accept connections on a specified TCP port from the given address.
address: The address (IP or hostname) to accept connections from.port: The TCP port number to accept connections on.sudo: Whether elevated permissions are required to configure the port (default:false).allowedPorts: A set of allowed ports for validation. Ifnull, validation is skipped.allowAllPorts: Whether to allow connections on all ports, overridingallowedPorts.
Returns a Future that completes with true if the operation succeeded,
or false if it failed.
Implementation
@override
Future<bool> acceptAddressOnTCPPort(String address, int port,
{bool sudo = false,
required Set<int>? allowedPorts,
required bool allowAllPorts}) async {
_checkValidPort(port);
address = _checkAddress(address);
if (!allowAllPorts &&
(allowedPorts == null || !allowedPorts.contains(port))) {
return false;
}
final iptablesBin = await resolveBinaryPathCached('iptables');
final iptablesArgs = <String>[
'-I',
'INPUT',
'-p',
'tcp',
'--dport',
'$port',
'-s',
address,
'-j',
'ACCEPT',
];
var output = await runCommand(
iptablesBin,
iptablesArgs,
sudo: sudo,
expectedExitCode: 0,
);
if (output == null) {
return false;
}
var accepted = await isAcceptedAddressOnPort(address, port,
sudo: sudo, allowedPorts: allowAllPorts ? null : (allowedPorts ?? {}));
return accepted;
}