sslChecker function

Future<SslCheckResult> sslChecker(
  1. String hostname, {
  2. int port = HttpClient.defaultHttpsPort,
  3. String method = 'HEAD',
  4. bool ignoreException = true,
})

Check certificate validity of host.

Implementation

Future<SslCheckResult> sslChecker(
  String hostname, {
  int port = HttpClient.defaultHttpsPort,
  String method = 'HEAD',
  bool ignoreException = true,
}) async {
  var badCertificate = false;
  final client = HttpClient()
    ..badCertificateCallback = (cert, host, port) {
      badCertificate = true;
      return true;
    }
    ..connectionTimeout = const Duration(seconds: 55);
  try {
    final uri = Uri(
      scheme: 'https',
      host: hostname,
      path: '/',
      port: port,
    );
    final req = await client.openUrl(method, uri);
    final res = await req.close();
    final cert = res.certificate;

    return SslCheckResult._(
      cert,
      !badCertificate,
      cert != null ? _daysRemaining(cert.endValidity) : 0,
      cert != null ? _commonName(cert.subject) : null,
    );
  } catch (e) {
    if (ignoreException) return SslCheckResult._(null, false, -1, null);
    rethrow;
  } finally {
    client.close();
  }
}