add method

Future<bool> add(
  1. Protocol protocol,
  2. String ip,
  3. int port, {
  4. bool autoRm = true,
  5. int duration = 0,
  6. int? externalPort,
  7. String description = ' ',
})

Implementation

Future<bool> add(Protocol protocol, String ip, int port,
    {bool autoRm = true,
    int duration = 0,
    int? externalPort,
    String description = ' '}) async {
  externalPort ??= port;
  if (description.isEmpty) {
    description = ' ';
  }
  final r = await get('AddPortMapping',
      """<NewRemoteHost></NewRemoteHost><NewExternalPort>$externalPort</NewExternalPort><NewProtocol>${protocol.name}</NewProtocol><NewInternalPort>$port</NewInternalPort><NewInternalClient>$ip</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>$description</NewPortMappingDescription><NewLeaseDuration>$duration</NewLeaseDuration>""");

  final statusCode = r.statusCode;
  if (statusCode == 200) {
    return true;
  } else {
    final xml = XmlDocument.parse(await r.text());
    if (xml.findAllElements('errorCode').first.text == '718'
        // ConflictInMappingEntry
        ) {
      logw('upnp conflict ${protocol.name} $externalPort');
      await rm(protocol, externalPort);
      return await add(protocol, ip, port,
          autoRm: false,
          duration: duration,
          externalPort: externalPort,
          description: description);
    }
    stderr.write("❌ $statusCode : ${xml.findAllElements('s:Body').first}");
  }
  return false;
}