Mark's blog 这是 Mark 的个人博客,记录些东西。

SSL 证书监控 php

最近网站有点多了,为了方便看所有站点的 SSL 证书,想搞个监控,目前就写到检测是否有效(很垃圾,就这样吧)

我的环境 php70,其他版本未测,应该都可以用

我的 SSL 监控站:https://ssl.009898.xyz/

创建一个 php,复制下面内容,修改 siteArray 里面的网站

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="https://cdn.staticfile.org/layui/2.6.8/css/layui.min.css">
        <style>
        i.layui-icon.layui-icon-close {
            border-radius: 2px;
            background-color: #FF5722;
            color: #fff;
            padding: 3px;
            font-size: 14px;
        }
        i.layui-icon.layui-icon-ok {
            border-radius: 2px;
            background-color: #2ec770;
            color: #fff;
            padding: 3px;
            font-size: 14px;
        }
        .layui-table tr th {
            font-weight: 600;
        }
        .layui-table {
            color: #333;
        }
        .layui-table th, .layui-table td{
            font-size:15px;
        }
        </style>
    </head>
<body style="margin: 0 50px;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>SSL 证书监控</legend>
    </fieldset>
<div class="layui-form">
  <table class="layui-table">
    <colgroup>
      <col width="250">
      <col width="120">
      <col width="230">
    </colgroup>
    <thead>
      <tr>
        <th>网站</th>
        <th>是否有效</th>
        <th>到期时间</th>
        <th>剩余时间</th>
      </tr> 
    </thead>
    <tbody>
<?php
$g = stream_context_create ([
    "ssl" => ["capture_peer_cert" => true],
    'http' => [
        'method' => 'GET',
        'user_agent' => 'Chrome 42.0.2311.135',
        'timeout'=>500
    ]
]);
date_default_timezone_set('PRC');
$siteArray=array("https://009898.xyz","https://status.009898.xyz","https://cloud.009898.xyz","https://pic.009898.xyz","https://github.009898.xyz","https://jp.009898.xyz","https://sj.009898.xyz","https://nl.009898.xyz","https://hostcli.009898.xyz","https://imgcdn.009898.xyz");
foreach ($siteArray as $value){
    //$r = fopen($value, "rb", false, $g);
    $orignal_parse = parse_url($value, PHP_URL_HOST);
    $r = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $g);
    $cont = stream_context_get_params($r);
    $cert = openssl_x509_parse($cont["options"]["ssl"]["peer_certificate"]);
    $expireDate = date("Y-m-d H:i:s", $cert['validTo_time_t']);
    $date1 = date_create(date('Y-m-d H:i:s')); 
    $date2 = date_create($expireDate); 
    $diff = date_diff($date1, $date2)->format('%R%a 天');
    if(empty($cert['validTo_time_t'])) {
        echo "<tr><td>", str_replace("https://","",$value,$i), "</td><td><i class=\"layui-icon layui-icon-close\"></i></td><td>", "已过期", "</td><td>", $diff ,"</td></tr>";
    } else {
        echo "<tr><td>", str_replace("https://","",$value,$i), "</td><td><i class=\"layui-icon layui-icon-ok\"></i></td><td>", $expireDate , "</td><td>", $diff ,"</td></tr>";
    }
}
?>
    </tbody>
  </table>
</div>

<div class="site-footer" style="text-align:center;padding:40px 0;color:#868e96;display:flex;justify-content:center;align-items:center;position:fixed;bottom:0;width:100%;">
  Powered by<a href="https://009898.xyz/post/ssl-zheng-shu-jian-kong-php/" target="_blank"> Mark</a>  
</div>
</body>
</html>
参考文章:https://cloud.tencent.com/developer/article/1116498
By Mark On