DomainInfo.php 4.7 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){
            //域名结束时间<2天时,重新生成
            if($v['certificate_end_time'] > date('Y-m-d H:i:s',time()+ 24*3600)){
                $this->updatePrivate($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   :updatePrivate
     * @author :lyh
     * @method :post
     * @time   :2023/12/8 16:16
     */
    public function updatePrivate($param)
    {
            $key = $param['private_key'];
            $cert = $param['private_cert'];
            if (empty($key) || empty($cert)){
                $url = 'https://' . $v->domain. '/api/applySsl/';
            }else{
                $url = 'https://' . $v->domain. '/api/setSsl/';
            }

            $extend_config = json_decode($v->extend_config, true);
            $top_domain = Str::getTopDomain($v->domain);
            if ((empty($extend_config) || empty($extend_config[0]['origin'])) && $v->id != 3) {
                $extend_config = [
                    ['origin' => $top_domain, 'target' => $v->domain]
                ];
            }
            $param = [
                'project_id' => $k,
                'type' => 1,
                'route' => 1,
                "domain" =>$v->domain,
                "rewrite"=> $extend_config,
                'other_domain' => [$top_domain, '*.' . $top_domain],
                'private_key' => $key,
                'cert' => $cert
            ];
            $result = app(ToolRepository::class)->curlRequest($url, $param);
            Log::info('domain id: ' . $v->id . ', domain: ' . $v->domain . ', result: ' . var_export($result, true));
            var_dump($result);

    }

    /**
     * @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];
    }
}