tomSecretsApprovalLinkProblem function

String? tomSecretsApprovalLinkProblem(
  1. String text, {
  2. bool anyPort = false,
})

Why text cannot be the device setting approvalLink (the address the approval link listens on, protocol.md 9.7), or null: host:port, an IPv6 address in brackets ([fd00::1]:47731), a port 1–65535 (with anyPort also 0, the system picks one), and never every interface (0.0.0.0, ::): the link listens on a VPN or LAN address only.

Implementation

String? tomSecretsApprovalLinkProblem(String text, {bool anyPort = false}) {
  final at = text.lastIndexOf(':');
  final port = at < 0 ? null : int.tryParse(text.substring(at + 1));
  if (at <= 0 ||
      port == null ||
      port < (anyPort ? 0 : 1) ||
      port > 65535 ||
      text.substring(at + 1).length > 5) {
    return 'the approval link is <VPN or LAN address>:<port>, not $text';
  }
  var host = text.substring(0, at);
  if (host.startsWith('[') && host.endsWith(']')) {
    host = host.substring(1, host.length - 1);
    if (!RegExp(r'^[0-9a-fA-F:]+$').hasMatch(host)) {
      return 'the approval link is <VPN or LAN address>:<port>, not $text';
    }
  } else if (!RegExp(r'^[A-Za-z0-9.-]+$').hasMatch(host)) {
    return 'the approval link is <VPN or LAN address>:<port>, not $text';
  }
  final parsed = InternetAddress.tryParse(host);
  if (parsed != null &&
      (parsed == InternetAddress.anyIPv4 ||
          parsed == InternetAddress.anyIPv6)) {
    return 'the approval link listens on a VPN or LAN address, not on every '
        'interface ($text)';
  }
  return null;
}