DomainInfo.php 3.1 KB
<?php
/**
 * @remark :
 * @name   :DomainInfo.php
 * @author :lyh
 * @method :post
 * @time   :2023/9/11 14:37
 */

namespace App\Console\Commands\Domain;

use Illuminate\Console\Command;
use App\Models\Domain\DomainInfo as DomainInfoModel;

class DomainInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'domain_info';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '域名相关';

    /**
     * @remark :更新证书+证书有效时间
     * @name   :handle
     * @author :lyh
     * @method :post
     * @time   :2023/9/11 15:09
     */
    public function handle(){
        $domainModel = new DomainInfoModel();
        $map = ['status'=>['!=',2]];
        $list = $domainModel->list($map);
        foreach ($list as $v){
            $ssl = $this->updateDomainSsl($v['domain']);
            $time = $this->updateDomain($v['domain']);
            $data = [
                'certificate_start_time'=>$ssl['from'],
                'certificate_end_time'=>$ssl['to'],
                'domain_start_time'=>$time['start'],
                'domain_end_time'=>$time['end']
            ];
            $domainModel->edit($data,['id'=>$v['id']]);
        }
        return 1;
    }

    /**
     * @remark :更新域名证书
     * @name   :updateDomainSsl
     * @author :lyh
     * @method :post
     * @time   :2023/9/11 15:07
     */
    public function updateDomainSsl($domain){
        try {
            $context = stream_context_create([
                'ssl' => [
                    'capture_peer_cert' => true,
                    'capture_peer_cert_chain' => false,
                ],
            ]);
            $stream = stream_socket_client('ssl://'.$domain.':443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
            if(!$stream) {
                die("Failed to connect: $errno - $errstr");
            }
            $remote_cert = stream_context_get_params($stream)['options']['ssl']['peer_certificate'];
            if(!$remote_cert) {
                die("Failed to retrieve certificate");
            }
            $valid_from = date('Y-m-d H:i:s', openssl_x509_parse($remote_cert)['validFrom_time_t']);
            $valid_to = date('Y-m-d H:i:s', openssl_x509_parse($remote_cert)['validTo_time_t']);
            fclose($stream);
        }catch (\Exception $e){
            $valid_from = date('Y-m-d H:i:s');
            $valid_to = date('Y-m-d H:i:s');
        }
        return ['from'=>$valid_from,'to'=>$valid_to];
    }

    /**
     * @remark :更新域名有限时间
     * @name   :updateDomain
     * @author :lyh
     * @method :post
     * @time   :2023/9/11 15:11
     */
    public function updateDomain($domain){
        $url = 'http://openai.waimaoq.com/v1/whois_api?domain='.$domain;
        $response = http_get($url);
        $start = '';
        $end = '';
        if($response['code'] == 200){
            $start = $response['text']['creation_date'];
            $end = $response['text']['expiration_date'];
        }
        return ['start'=>$start,'end'=>$end];
    }
}