作者 周海龙

合并分支 'zhl' 到 'master'

Zhl



查看合并请求 !1
... ... @@ -13,4 +13,5 @@ npm-debug.log
yarn-error.log
/.idea
/.vscode
/app/Console/Commands/Demo.php
\ No newline at end of file
/app/Console/Commands/Demo.php
composer.lock
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2022/11/05
* Time: 14:11
*/
namespace App\Console\Commands;
use App\Models\BtEvents;
use App\Repositories\BtRepository;
use App\Repositories\ToolRepository;
use Illuminate\Console\Command;
/**
* Class EventExpend
* @package App\Console\Commands
*/
class EventExpend extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'event:expend';
/**
* The console command description.
*
* @var string
*/
protected $description = '消费提交事件';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return bool
*/
public function handle()
{
while (true) {
$start_at = date('Y-m-d H:i:s');
$events = BtEvents::where('status', 0)->where('times', '<', BtEvents::MAX_TRIES_TIMES)->where('start_at', '<', $start_at)->orderBy('id', 'asc')->limit(10)->get();
// 没有需要处理的数据
if ($events->isEmpty()) {
echo $start_at . ', empty event.' . PHP_EOL;
sleep(30);
} else {
$this->expend($events);
}
}
return true;
}
/**
* @param $data
* @return bool
*/
public function expend($data)
{
foreach ($data as $val) {
$param = json_decode($val->param, true);
switch ($val->type) {
case BtEvents::TYPE_CREATE_SITE:
$result = $this->createSiteEvent($val->id, $param);
break;
case BtEvents::TYPE_DELETE_SITE:
$result = $this->deleteSiteEvent($val->id, $param);
break;
case BtEvents::TYPE_CREATE_SSL:
$result = $this->createSiteSsl($val->id, $param);
break;
case BtEvents::TYPE_RENEWAL_SSL:
$result = $this->renewalSiteSsl($val->id, $param);
break;
case BtEvents::TYPE_EVENT_CALLBACK:
$result = $this->callbackEvent($val->id, $param);
break;
default:
$result = false;
break;
}
}
return true;
}
/**
* 创建站点
* @param $id
* @param $param
* @return bool
*/
public function createSiteEvent($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
$domain = $param['domain'] ?? '';
$ssl_open = empty($param['ssl_open']) ? 1 : 0;
$ssl_auto = empty($param['ssl_auto']) ? 1 : 0;
$ssl_auto_day = $param['ssl_auto_day'] ?? 10;
$ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5);
if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) {
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_message'] = 'domain信息错误';
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'domain信息错误');
}
try {
$flag = app(BtRepository::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day);
$status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
$note = $flag ? '' : '创建站点失败';
} catch (\Exception $e) {
$status = BtEvents::STATUS_ERROR;
$note = $e->getMessage();
}
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400;
$param['result_message'] = $note;
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, $status, $note);
}
/**
* 删除站点
* @param $id
* @param $param
* @return bool
*/
public function deleteSiteEvent($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
$domain = $param['domain'] ?? '';
if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) {
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = 400;
$param['result_message'] = 'domain信息错误';
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'domain信息错误');
}
try {
$flag = app(BtRepository::class)->deleteBtSite($domain);
$status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
$note = $flag ? '' : '删除站点失败';
} catch (\Exception $e) {
$status = BtEvents::STATUS_ERROR;
$note = $e->getMessage();
}
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400;
$param['result_message'] = $note;
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, $status, $note);
}
/**
* 创建ssl证书
* @param $id
* @param $param
* @return bool
*/
public function createSiteSsl($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
$host = $param['host'] ?? '';
if (empty($host)) {
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = 400;
$param['result_message'] = 'host信息错误';
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'host信息错误');
}
try {
$flag = app(BtRepository::class)->applySsl($host);
$status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
$note = $flag ? '' : '删除站点失败';
} catch (\Exception $e) {
$status = BtEvents::STATUS_ERROR;
$note = $e->getMessage();
}
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400;
$param['result_message'] = $note;
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, $status, $note);
}
/**
* 续签证书
* @param $id
* @param $param
* @return bool
*/
public function renewalSiteSsl($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
$host = $param['host'] ?? '';
if (empty($host)) {
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = 400;
$param['result_message'] = 'host信息错误';
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'host信息错误');
}
try {
$flag = app(BtRepository::class)->renewalSsl($host);
$status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
$note = $flag ? '' : '删除站点失败';
} catch (\Exception $e) {
$status = BtEvents::STATUS_ERROR;
$note = $e->getMessage();
}
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400;
$param['result_message'] = $note;
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, $status, $note);
}
/**
* 回调信息
* @param $id
* @param $param
* @return bool
*/
public function callbackEvent($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
if (empty($callback) || FALSE == filter_var($callback, FILTER_VALIDATE_URL))
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'callback信息错误');
$status = $param['result_status'];
$message = $param['result_message'];
unset($param['result_status']);
unset($param['result_message']);
$array = [
'status' => $status,
'message' => $message,
'data' => [],
'param' => $param
];
list($code, $result) = app(ToolRepository::class)->curlRequest($callback, $array);
$status = $code == 200 ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
return $this->setEvent($id, $status, $result);
}
/**
* 更新任务状态
* @param $id
* @param $status
* @param string $note
* @return bool
*/
public function setEvent($id, $status, $note = '')
{
$event = BtEvents::where(['id' => $id])->first();
if (empty($event))
return false;
$times = $event->times + 1;
if ($status == BtEvents::STATUS_ERROR && $times < BtEvents::MAX_TRIES_TIMES) {
$status = BtEvents::STATUS_INIT;
// 第一次执行失败以后, 5分钟后再执行一次; 第二次执行失败以后, 10分钟后再执行一次 0->5->15
if ($times == 1)
$event->start_at = date('Y-m-d H:i:s', time() + 300);
if ($times == 2)
$event->start_at = date('Y-m-d H:i:s', time() + 600);
}
$event->stauts = $status;
$event->times = $times;
$event->note = $note;
$event->save();
return true;
}
}
... ...
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2022/11/05
* Time: 09:09
*/
namespace App\Console\Commands;
use App\Models\BtEvents;
use App\Models\BtSites;
use App\Repositories\BtRepository;
use Illuminate\Console\Command;
/**
* Class SiteSslRenewal
* @package App\Console\Commands
*/
class SiteSslRenewal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'site:ssl_renewal';
/**
* The console command description.
*
* @var string
*/
protected $description = '检测ssl续签';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return bool
*/
public function handle()
{
$sites = BtSites::where(['is_del' => 0])->get();
if ($sites->isEmpty())
return true;
foreach ($sites as $val) {
// 未开启ssl 抛弃
if ($val->ssl_open == 0)
continue;
// 未生成ssl 申请ssl
if ($val->ssl_status == 0) {
$result = app(BtRepository::class)->applySsl($sites->domain);
if (empty($result))
BtEvents::createBtEvent(BtEvents::TYPE_CREATE_SSL, json_encode(['host' => $sites->domain]));
continue;
}
// 开启ssl续签 检查是否到期进行续签
if ($val->ssl_auto) {
$result = app(BtRepository::class)->renewalSsl($sites->domain);
if (empty($result))
BtEvents::createBtEvent(BtEvents::TYPE_RENEWAL_SSL, json_encode(['host' => $sites->domain]));
continue;
}
sleep(2);
}
return true;
}
}
... ...
... ... @@ -16,6 +16,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('site:ssl_renewal')->dailyAt('01:00'); // 站点创建ssl证书, 续签
}
/**
... ...
... ... @@ -8,7 +8,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Repositories\BtRepositories;
use App\Repositories\BtRepository;
use Illuminate\Http\Request;
/**
... ... @@ -27,14 +27,14 @@ public function createSite(Request $request)
try {
// $this->validate();
$domain = $request->input('domain');
$ssl_open = intval($request->input('ssl_open', 0)) ? 1 : 0;
$ssl_auto = intval($request->input('ssl_auto', 0)) ? 1 : 0;
$ssl_auto_day = intval($request->input('ssl_auto_day', 0));
$ssl_open = intval($request->input('ssl_open', 1)) ? 1 : 0;
$ssl_auto = intval($request->input('ssl_auto', 1)) ? 1 : 0;
$ssl_auto_day = intval($request->input('ssl_auto_day', 10));
$ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5);
if (empty($domain) && filter_var($domain, FILTER_VALIDATE_URL))
if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL))
return $this->error('请提交有效domain信息');
$result = app(BtRepositories::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day);
$result = app(BtRepository::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day);
return $result ? $this->success($result) : $this->error('创建站点失败,提交异步创建任务!');
} catch (\Exception $e) {
return $this->error($e->getMessage());
... ... @@ -54,10 +54,10 @@ public function deleteSite(Request $request)
if (empty($domain) && filter_var($domain, FILTER_VALIDATE_URL))
return $this->error('请提交有效domain信息');
$result = app(BtRepositories::class)->deleteBtSite($domain);
$result = app(BtRepository::class)->deleteBtSite($domain);
return $result ? $this->success() : $this->error('删除站点失败,提交异步删除任务!');
} catch (\Exception $d) {
return $this->error();
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
... ... @@ -68,7 +68,17 @@ public function deleteSite(Request $request)
*/
public function createSsl(Request $request)
{
return $this->error();
try {
// $this->validate();
$host = $request->input('host');
if (empty($host))
return $this->error('请提交有效host信息');
$result = app(BtRepository::class)->applySsl($host);
return $result ? $this->success() : $this->error('创建ssl失败,提交异步任务!');
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
... ... @@ -76,8 +86,18 @@ public function createSsl(Request $request)
* @param Request $request
* @return string
*/
public function updateSsl(Request $request)
public function renewalSsl(Request $request)
{
return $this->error();
try {
// $this->validate();
$host = $request->input('host');
if (empty($host))
return $this->error('请提交有效host信息');
$result = app(BtRepository::class)->renewalSsl($host);
return $result ? $this->success() : $this->error('续签ssl失败,提交异步任务!');
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
}
\ No newline at end of file
... ...
... ... @@ -31,7 +31,7 @@ protected function success($data = [], $message = 'success', $status = 200)
*/
protected function error($message = 'error', $status = 400, $data = [])
{
$array = compact('status', 'message', $data);
$array = compact('status', 'message', 'data');
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
}
... ...
... ... @@ -14,8 +14,8 @@ class Kernel extends HttpKernel
* @var array<int, class-string|string>
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\TrustHosts::class,
// \App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
... ...
... ... @@ -20,20 +20,48 @@ class BtEvents extends Model
*/
protected $table = 'bt_events';
// 最大执行次数
const MAX_TRIES_TIMES = 3;
const STATUS_INIT = 0;
const STATUS_FINISH = 1;
const STATUS_ERROR = 9;
const TYPE_CREATE_SITE = 1;
const TYPE_DELETE_SITE = 2;
const TYPE_CREATE_SSL = 3;
const TYPE_RENEWAL_SSL = 4;
const TYPE_HTTP_TO_HTTPS_OPEN = 5;
const TYPE_HTTP_TO_HTTPS_CLOSE = 6;
const TYPE_EVENT_CALLBACK = 99;
/**
* @return array
*/
public static function typeMap()
{
return [
self::TYPE_CREATE_SITE => '创建站点',
self::TYPE_DELETE_SITE => '删除站点',
self::TYPE_CREATE_SSL => '创建ssl证书',
self::TYPE_RENEWAL_SSL => '续签ssl证书',
self::TYPE_HTTP_TO_HTTPS_OPEN => '开启强制https',
self::TYPE_HTTP_TO_HTTPS_CLOSE => '关闭强制https',
];
}
/**
* @param $type
* @param $param
* @return mixed
*/
public static function createBtEvent($type, $param)
public static function createBtEvent($type, $param, $start_at = '')
{
$start_at = $start_at ?: date('Y-m-d H:i:s');
$event = new self();
$event->type = $type;
$event->param = $param;
$event->start_at = $start_at;
$event->save();
return $event->id;
}
... ...
... ... @@ -37,7 +37,7 @@ class BtSites extends Model
*/
public static function createBtSite($domain, $site_id, $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day)
{
$site = self::where(['domain' => $domain])->fisrt();
$site = self::where(['domain' => $domain])->first();
if (empty($site)) {
$site = new self();
}
... ... @@ -59,6 +59,6 @@ public static function createBtSite($domain, $site_id, $ssl_open, $ssl_status, $
*/
public static function getSiteByDomain($domain)
{
return self::where(['domain' => $domain, 'is_del' => self::IS_DEL_FALSE])->fisrt();
return self::where(['domain' => $domain, 'is_del' => self::IS_DEL_FALSE])->first();
}
}
\ No newline at end of file
... ...
... ... @@ -12,10 +12,10 @@
use App\Repositories\Bt\Bt;
/**
* Class BtRepositories
* Class BtRepository
* @package App\Repositories
*/
class BtRepositories
class BtRepository
{
public $instance = [];
... ... @@ -24,15 +24,23 @@ class BtRepositories
* @param $ssl_open
* @param $ssl_auto
* @param $ssl_auto_day
* @param int $ssl_status
* @return array|bool
*/
public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day)
public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day, $ssl_status = 0)
{
$domain_array = parse_url($domain);
$host = $domain_array['host'];
// 如果站点已经存在, 更新数据
$site = BtSites::getSiteByDomain($host);
if ($site) {
$result = BtSites::createBtSite($host, $site->site_id, $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day);
return $result->toArray();
}
$host_array = explode('.', $host);
if (empty($host_array[0]) && $host_array[0] == 'www')
if (FALSE == empty($host_array[0]) && $host_array[0] == 'www')
unset($host_array[0]);
$domain_top = implode('.', $host_array);
... ... @@ -42,7 +50,7 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day)
'path' => env('PROJECT_PATH') ? env('PROJECT_PATH') : dirname(dirname(app_path())) . DIRECTORY_SEPARATOR . $domain,
'type_id' => 0,
'type' => 'PHP',
'version' => '73',
'version' => '74',
'port' => '80',
'ps' => $host,
'ftp' => false,
... ... @@ -56,13 +64,43 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day)
$bt = $this->getBtObject();
$result = $bt->AddSite($array);
if (empty($result) || empty($result['id']))
// result 返回信息
// $result = ['siteStatus' => true, 'siteId' => 39, 'ftpStatus' => false, 'databaseStatus' => false,];
if (empty($result) || empty($result['siteId']))
return false;
// 伪静态设置
$htaccess = '# SEO URL Settings
# Nginx configuration of OC htaccess
location = /sitemap.xml {
rewrite ^(.*)$ /index.php?route=feed/google_sitemap break;
}
location = /googlebase.xml {
rewrite ^(.*)$ /index.php?route=feed/google_base break;
}
location / {
# This try_files directive is used to enable SEO-friendly URLs for OpenCart
try_files $uri $uri/ @opencart;
# 隐藏index.php
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1 last;
}
}
location @opencart {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
# End SEO settings';
$bt->SaveFileBody($host, $htaccess);
$ssl_status = 0;
$result = BtSites::createBtSite($host, $result['id'], $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day);
return $result->toArray();
$result = BtSites::createBtSite($host, $result['siteId'], $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day);
$site = $result->toArray();
unset($site['id']);
unset($site['created_at']);
unset($site['updated_at']);
unset($site['is_del']);
return $site;
}
/**
... ... @@ -72,21 +110,20 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day)
*/
public function deleteBtSite($domain)
{
$domain = parse_url($domain);
$domain_array = parse_url($domain);
$host = $domain_array['host'];
$bt = $this->getBtObject();
$site = BtSites::getSiteByDomain($host);
// $id = $site->site_id;
// $id = $site->site_id;
// 获取bt数据 可能要可靠一些
$result = $bt->Websites($host);
if (empty($result['data']))
return false;
$id = 0;
foreach ($result['data'] as $v) {
if ($v['name'] == $domain) {
if ($v['name'] == $host) {
$id = $v['id'];
break;
}
... ... @@ -94,14 +131,103 @@ public function deleteBtSite($domain)
if (empty($id))
return false;
$result = $bt->WebDeleteSite($id,$domain,false,false,false);
$result = $bt->WebDeleteSite($id,$host,false,false,false);
// $result 返回信息
// $result = ['status' => true, 'msg' => '站点删除成功!',]
if (empty($result['status']))
return false;
if ($site) {
$site->is_del = BtSites::IS_DEL_TRUE;
$site->save();
}
return true;
}
/**
* 申请ssl 并设置ssl访问
* @param $host
* @return bool
*/
public function applySsl($host)
{
$site = BtSites::getSiteByDomain($host);
if (empty($site) || empty($site->ssl_open))
return false;
$bt = $this->getBtObject();
// $domains = [["id" => 71, "pid" => 35, "name" => "www.shopk.com", "port" => 80, "addtime" => "2022-11-03 14:22:08",]];
$domain_list = $bt->WebDoaminList($site->site_id);
// 被删除的站点 返回空数组 不做数据删除同步 可能bt接口问题
if (empty($domain_list))
return false;
// $result = ["status" => false, "msg" => "指定网站配置文件不存在!"];
// $result = ["status" => true, "oid" => -1, "domain" => [["name" => "*.shopk.com"],["name" => "www.shopk.com"]], "key" => "key", "csr" => "csr", "type" => 0, "httpTohttps" => true,
// "cert_data" => ["issuer" => "TrustAsia TLS RSA CA", "notAfter" => "2023-02-22", "notBefore" => "2022-02-21", "dns" => [0 => "*.shopk.com", 1 => "shopk.com"],
// "subject" => "*.shopk.com", "endtime" => 110], "email" => "test@message.com", "index" => null, "auth_type" => "http", "push" => ["status" => false]];
$ssl = $bt->GetSSL($host);
// 已经有ssl证书了
if (FALSE == empty($ssl['status'])) {
$bt->SetSSL('1', $host, $ssl['key'], $ssl['csr']);
// 开启强制https
// $bt->HttpToHttps($host);
$site->ssl_status = 1;
$site->save();
return true;
}
$domain = array_column($domain_list, 'name');
$result = $bt->ApplyCert(json_encode($domain), $site->site_id);
if (empty($result) || empty($result['cert']))
return false;
$site->is_del = BtSites::IS_DEL_TRUE;
$bt->SetSSL('1', $host, $result['private_key'], $result['cert']);
// 开启强制https
// $bt->HttpToHttps($host);
$site->ssl_status = 1;
$site->save();
return true;
}
/**
* 续签ssl
* @param $host
* @return bool
*/
public function renewalSsl($host)
{
$site = BtSites::getSiteByDomain($host);
if (empty($site) || empty($site->ssl_open) || empty($site->ssl_auto))
return false;
$bt = $this->getBtObject();
// $result = ["status" => false, "oid" => -1, "domain" => [["name" => "test.hagro.cn"], ["name" => "test.hagro.cn"]], "key" => false, "csr" => false,
// "type" => -1, "httpTohttps" => false, "cert_data" => [], "email" => "test@message.com", "index" => "", "auth_type" => "http", "push" => ["status" => false]];
$ssl = $bt->GetSSL($host);
if (empty($ssl['status']))
return $this->applySsl($host);
$end = strtotime($ssl["cert_data"]["notAfter"]);
if ($end - time() >= $site->ssl_auto_day * 86400)
return true;
$domain_list = $bt->WebDoaminList($site->site_id);
// 被删除的站点 返回空数组 不做数据删除同步 可能bt接口问题
if (empty($domain_list))
return false;
// 不是宝塔生成的ssl 不能续签, 直接申请ssl
$result = $bt->RenewCert($ssl["index"]);
if (empty($result['status']))
return $this->applySsl($host);
$bt->SetSSL(1, $host, $result['private_key'], $result['cert']);
return true;
}
/**
* 获取bt对象
* @param string $key
* @param string $panel
... ...
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2022/11/05
* Time: 16:40
*/
namespace App\Repositories;
/**
* Class ToolRepository
* @package App\Repositories
*/
class ToolRepository
{
/**
* @param $url
* @param $data
* @param string $method
* @param array $header
* @param int $time_out
* @return array
*/
public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if ($data)
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
'Expect:',
'Content-type: application/json',
'Accept: application/json',
], $header)
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [$code, $response];
}
}
\ No newline at end of file
... ...
... ... @@ -6,9 +6,11 @@
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"ext-json": "*",
"ext-curl": "*",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.75",
"laravel/framework": "^8.0",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5"
},
... ... @@ -18,7 +20,8 @@
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^5.10",
"phpunit/phpunit": "^9.5.10"
"phpunit/phpunit": "^9.5.10",
"fzaninotto/faker": "^1.4"
},
"autoload": {
"psr-4": {
... ...
此 diff 太大无法显示。
... ... @@ -67,7 +67,7 @@
|
*/
'timezone' => 'UTC',
'timezone' => 'PRC',
/*
|--------------------------------------------------------------------------
... ...
... ... @@ -22,7 +22,7 @@
Route::any('/create_site', 'WebSiteController@createSite')->name('create_site');
Route::any('/delete_site', 'WebSiteController@deleteSite')->name('delete_site');
Route::any('/create_site_ssl', 'WebSiteController@createSsl')->name('create_site_ssl');
Route::any('/update_site_ssl', 'WebSiteController@updateSsl')->name('update_site_ssl');
Route::any('/renewal_site_ssl', 'WebSiteController@renewalSsl')->name('renewal_site_ssl');
Route::get('/create_event', 'ReceiveController@createEvent')->name('create_event');
});
... ...