正在显示
11 个修改的文件
包含
511 行增加
和
27 行删除
app/Console/Commands/EventExpend.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: zhl | ||
| 5 | + * Date: 2022/11/05 | ||
| 6 | + * Time: 14:11 | ||
| 7 | + */ | ||
| 8 | +namespace App\Console\Commands; | ||
| 9 | + | ||
| 10 | +use App\Models\BtEvents; | ||
| 11 | +use App\Repositories\BtRepository; | ||
| 12 | +use App\Repositories\ToolRepository; | ||
| 13 | +use Illuminate\Console\Command; | ||
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * Class EventExpend | ||
| 17 | + * @package App\Console\Commands | ||
| 18 | + */ | ||
| 19 | +class EventExpend extends Command | ||
| 20 | +{ | ||
| 21 | + /** | ||
| 22 | + * The name and signature of the console command. | ||
| 23 | + * | ||
| 24 | + * @var string | ||
| 25 | + */ | ||
| 26 | + protected $signature = 'event:expend'; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * The console command description. | ||
| 30 | + * | ||
| 31 | + * @var string | ||
| 32 | + */ | ||
| 33 | + protected $description = '消费提交事件'; | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Create a new command instance. | ||
| 37 | + * | ||
| 38 | + * @return void | ||
| 39 | + */ | ||
| 40 | + public function __construct() | ||
| 41 | + { | ||
| 42 | + parent::__construct(); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * Execute the console command. | ||
| 47 | + * | ||
| 48 | + * @return bool | ||
| 49 | + */ | ||
| 50 | + public function handle() | ||
| 51 | + { | ||
| 52 | + while (true) { | ||
| 53 | + $start_at = date('Y-m-d H:i:s'); | ||
| 54 | + $events = BtEvents::where('status', 0)->where('times', '<', BtEvents::MAX_TRIES_TIMES)->where('start_at', '<', $start_at)->orderBy('id', 'asc')->limit(10)->get(); | ||
| 55 | + // 没有需要处理的数据 | ||
| 56 | + if ($events->isEmpty()) { | ||
| 57 | + echo $start_at . ', empty event.' . PHP_EOL; | ||
| 58 | + sleep(30); | ||
| 59 | + } else { | ||
| 60 | + foreach ($events as $val) { | ||
| 61 | + | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + return true; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + public function expend($data) | ||
| 69 | + { | ||
| 70 | + foreach ($data as $val) { | ||
| 71 | + $param = json_decode($val->param, true); | ||
| 72 | + switch ($val->type) { | ||
| 73 | + case BtEvents::TYPE_CREATE_SITE: | ||
| 74 | + $result = $this->createSiteEvent($val->id, $param); | ||
| 75 | + break; | ||
| 76 | + case BtEvents::TYPE_DELETE_SITE: | ||
| 77 | + $result = $this->deleteSiteEvent($val->id, $param); | ||
| 78 | + break; | ||
| 79 | + default: | ||
| 80 | + $result = false; | ||
| 81 | + break; | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * 创建站点 | ||
| 88 | + * @param $id | ||
| 89 | + * @param $param | ||
| 90 | + * @return bool | ||
| 91 | + */ | ||
| 92 | + public function createSiteEvent($id, $param) | ||
| 93 | + { | ||
| 94 | + if (empty($param) || FALSE == is_array($param)) | ||
| 95 | + return false; | ||
| 96 | + | ||
| 97 | + $callback = $param['callback'] ?? ''; | ||
| 98 | + $domain = $param['domain'] ?? ''; | ||
| 99 | + $ssl_open = empty($param['ssl_open']) ? 1 : 0; | ||
| 100 | + $ssl_auto = empty($param['ssl_auto']) ? 1 : 0; | ||
| 101 | + $ssl_auto_day = $param['ssl_auto_day'] ?? 10; | ||
| 102 | + $ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5); | ||
| 103 | + if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) { | ||
| 104 | + if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) { | ||
| 105 | + $param['result_message'] = 'domain信息错误'; | ||
| 106 | + BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param)); | ||
| 107 | + } | ||
| 108 | + return $this->setEvent($id, BtEvents::STATUS_FINISH, 'domain信息错误'); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + try { | ||
| 112 | + $flag = app(BtRepository::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day); | ||
| 113 | + $status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR; | ||
| 114 | + $note = $flag ? '' : '创建站点失败'; | ||
| 115 | + } catch (\Exception $e) { | ||
| 116 | + $status = BtEvents::STATUS_ERROR; | ||
| 117 | + $note = $e->getMessage(); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) { | ||
| 121 | + $param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400; | ||
| 122 | + $param['result_message'] = $note; | ||
| 123 | + BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param)); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + return $this->setEvent($id, $status, $note); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + /** | ||
| 130 | + * 删除站点 | ||
| 131 | + * @param $id | ||
| 132 | + * @param $param | ||
| 133 | + * @return bool | ||
| 134 | + */ | ||
| 135 | + public function deleteSiteEvent($id, $param) | ||
| 136 | + { | ||
| 137 | + if (empty($param) || FALSE == is_array($param)) | ||
| 138 | + return false; | ||
| 139 | + $callback = $param['callback'] ?? ''; | ||
| 140 | + $domain = $param['domain'] ?? ''; | ||
| 141 | + if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) { | ||
| 142 | + if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) { | ||
| 143 | + $param['result_status'] = 400; | ||
| 144 | + $param['result_message'] = 'domain信息错误'; | ||
| 145 | + BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param)); | ||
| 146 | + } | ||
| 147 | + return $this->setEvent($id, BtEvents::STATUS_FINISH, 'domain信息错误'); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + try { | ||
| 151 | + $flag = app(BtRepository::class)->deleteBtSite($domain); | ||
| 152 | + $status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR; | ||
| 153 | + $note = $flag ? '' : '删除站点失败'; | ||
| 154 | + } catch (\Exception $e) { | ||
| 155 | + $status = BtEvents::STATUS_ERROR; | ||
| 156 | + $note = $e->getMessage(); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) { | ||
| 160 | + $param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400; | ||
| 161 | + $param['result_message'] = $note; | ||
| 162 | + BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param)); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + return $this->setEvent($id, $status, $note); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + /** | ||
| 169 | + * 回调信息 | ||
| 170 | + * @param $id | ||
| 171 | + * @param $param | ||
| 172 | + * @return bool | ||
| 173 | + */ | ||
| 174 | + public function callbackEvent($id, $param) | ||
| 175 | + { | ||
| 176 | + if (empty($param) || FALSE == is_array($param)) | ||
| 177 | + return false; | ||
| 178 | + $callback = $param['callback'] ?? ''; | ||
| 179 | + if (empty($callback) || FALSE == filter_var($callback, FILTER_VALIDATE_URL)) | ||
| 180 | + return $this->setEvent($id, BtEvents::STATUS_FINISH, 'callback信息错误'); | ||
| 181 | + $status = $param['result_status']; | ||
| 182 | + $message = $param['result_message']; | ||
| 183 | + unset($param['result_status']); | ||
| 184 | + unset($param['result_message']); | ||
| 185 | + $array = [ | ||
| 186 | + 'status' => $status, | ||
| 187 | + 'message' => $message, | ||
| 188 | + 'param' => $param | ||
| 189 | + ]; | ||
| 190 | + list($code, $result) = app(ToolRepository::class)->curlRequest($callback, $array); | ||
| 191 | + $status = $code == 200 ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR; | ||
| 192 | + return $this->setEvent($id, $status, $result); | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * 更新任务状态 | ||
| 198 | + * @param $id | ||
| 199 | + * @param $status | ||
| 200 | + * @param string $note | ||
| 201 | + * @return bool | ||
| 202 | + */ | ||
| 203 | + public function setEvent($id, $status, $note = '') | ||
| 204 | + { | ||
| 205 | + $event = BtEvents::where(['id' => $id])->first(); | ||
| 206 | + if (empty($event)) | ||
| 207 | + return false; | ||
| 208 | + | ||
| 209 | + $times = $event->times + 1; | ||
| 210 | + | ||
| 211 | + if ($status == BtEvents::STATUS_ERROR && $times < BtEvents::MAX_TRIES_TIMES) { | ||
| 212 | + $status = BtEvents::STATUS_INIT; | ||
| 213 | + // 第一次执行失败以后, 5分钟后再执行一次; 第二次执行失败以后, 10分钟后再执行一次 0->5->15 | ||
| 214 | + if ($times == 1) | ||
| 215 | + $event->start_at = date('Y-m-d H:i:s', time() + 300); | ||
| 216 | + if ($times == 2) | ||
| 217 | + $event->start_at = date('Y-m-d H:i:s', time() + 600); | ||
| 218 | + } | ||
| 219 | + $event->stauts = $status; | ||
| 220 | + $event->times = $times; | ||
| 221 | + $event->note = $note; | ||
| 222 | + $event->save(); | ||
| 223 | + return true; | ||
| 224 | + } | ||
| 225 | +} |
app/Console/Commands/SiteSslRenewal.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: zhl | ||
| 5 | + * Date: 2022/11/05 | ||
| 6 | + * Time: 09:09 | ||
| 7 | + */ | ||
| 8 | +namespace App\Console\Commands; | ||
| 9 | + | ||
| 10 | +use App\Models\BtEvents; | ||
| 11 | +use App\Models\BtSites; | ||
| 12 | +use App\Repositories\BtRepository; | ||
| 13 | +use Illuminate\Console\Command; | ||
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * Class SiteSslRenewal | ||
| 17 | + * @package App\Console\Commands | ||
| 18 | + */ | ||
| 19 | +class SiteSslRenewal extends Command | ||
| 20 | +{ | ||
| 21 | + /** | ||
| 22 | + * The name and signature of the console command. | ||
| 23 | + * | ||
| 24 | + * @var string | ||
| 25 | + */ | ||
| 26 | + protected $signature = 'site:ssl_renewal'; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * The console command description. | ||
| 30 | + * | ||
| 31 | + * @var string | ||
| 32 | + */ | ||
| 33 | + protected $description = '检测ssl续签'; | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Create a new command instance. | ||
| 37 | + * | ||
| 38 | + * @return void | ||
| 39 | + */ | ||
| 40 | + public function __construct() | ||
| 41 | + { | ||
| 42 | + parent::__construct(); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * Execute the console command. | ||
| 47 | + * | ||
| 48 | + * @return bool | ||
| 49 | + */ | ||
| 50 | + public function handle() | ||
| 51 | + { | ||
| 52 | + $sites = BtSites::where(['is_del' => 0])->get(); | ||
| 53 | + | ||
| 54 | + if ($sites->isEmpty()) | ||
| 55 | + return true; | ||
| 56 | + | ||
| 57 | + foreach ($sites as $val) { | ||
| 58 | + // 未开启ssl 抛弃 | ||
| 59 | + if ($val->ssl_open == 0) | ||
| 60 | + continue; | ||
| 61 | + | ||
| 62 | + // 未生成ssl 申请ssl | ||
| 63 | + if ($val->ssl_status == 0) { | ||
| 64 | + $result = app(BtRepository::class)->applySsl($sites->domain); | ||
| 65 | + if (empty($result)) | ||
| 66 | + BtEvents::createBtEvent(BtEvents::TYPE_CREATE_SSL, json_encode(['domain' => $sites->domain])); | ||
| 67 | + continue; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + // 开启ssl续签 检查是否到期进行续签 | ||
| 71 | + if ($val->ssl_auto) { | ||
| 72 | + $result = app(BtRepository::class)->renewalSsl($sites->domain); | ||
| 73 | + if (empty($result)) | ||
| 74 | + BtEvents::createBtEvent(BtEvents::TYPE_RENEWAL_SSL, json_encode(['domain' => $sites->domain])); | ||
| 75 | + continue; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + } | ||
| 79 | + return true; | ||
| 80 | + } | ||
| 81 | +} |
| @@ -8,7 +8,7 @@ | @@ -8,7 +8,7 @@ | ||
| 8 | namespace App\Http\Controllers\Api; | 8 | namespace App\Http\Controllers\Api; |
| 9 | 9 | ||
| 10 | use App\Http\Controllers\Controller; | 10 | use App\Http\Controllers\Controller; |
| 11 | -use App\Repositories\BtRepositories; | 11 | +use App\Repositories\BtRepository; |
| 12 | use Illuminate\Http\Request; | 12 | use Illuminate\Http\Request; |
| 13 | 13 | ||
| 14 | /** | 14 | /** |
| @@ -27,14 +27,14 @@ public function createSite(Request $request) | @@ -27,14 +27,14 @@ public function createSite(Request $request) | ||
| 27 | try { | 27 | try { |
| 28 | // $this->validate(); | 28 | // $this->validate(); |
| 29 | $domain = $request->input('domain'); | 29 | $domain = $request->input('domain'); |
| 30 | - $ssl_open = intval($request->input('ssl_open', 0)) ? 1 : 0; | ||
| 31 | - $ssl_auto = intval($request->input('ssl_auto', 0)) ? 1 : 0; | ||
| 32 | - $ssl_auto_day = intval($request->input('ssl_auto_day', 0)); | 30 | + $ssl_open = intval($request->input('ssl_open', 1)) ? 1 : 0; |
| 31 | + $ssl_auto = intval($request->input('ssl_auto', 1)) ? 1 : 0; | ||
| 32 | + $ssl_auto_day = intval($request->input('ssl_auto_day', 10)); | ||
| 33 | $ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5); | 33 | $ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5); |
| 34 | - if (empty($domain) && filter_var($domain, FILTER_VALIDATE_URL)) | 34 | + if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) |
| 35 | return $this->error('请提交有效domain信息'); | 35 | return $this->error('请提交有效domain信息'); |
| 36 | 36 | ||
| 37 | - $result = app(BtRepositories::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day); | 37 | + $result = app(BtRepository::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day); |
| 38 | return $result ? $this->success($result) : $this->error('创建站点失败,提交异步创建任务!'); | 38 | return $result ? $this->success($result) : $this->error('创建站点失败,提交异步创建任务!'); |
| 39 | } catch (\Exception $e) { | 39 | } catch (\Exception $e) { |
| 40 | return $this->error($e->getMessage()); | 40 | return $this->error($e->getMessage()); |
| @@ -54,7 +54,7 @@ public function deleteSite(Request $request) | @@ -54,7 +54,7 @@ public function deleteSite(Request $request) | ||
| 54 | if (empty($domain) && filter_var($domain, FILTER_VALIDATE_URL)) | 54 | if (empty($domain) && filter_var($domain, FILTER_VALIDATE_URL)) |
| 55 | return $this->error('请提交有效domain信息'); | 55 | return $this->error('请提交有效domain信息'); |
| 56 | 56 | ||
| 57 | - $result = app(BtRepositories::class)->deleteBtSite($domain); | 57 | + $result = app(BtRepository::class)->deleteBtSite($domain); |
| 58 | return $result ? $this->success() : $this->error('删除站点失败,提交异步删除任务!'); | 58 | return $result ? $this->success() : $this->error('删除站点失败,提交异步删除任务!'); |
| 59 | } catch (\Exception $d) { | 59 | } catch (\Exception $d) { |
| 60 | return $this->error(); | 60 | return $this->error(); |
| @@ -14,8 +14,8 @@ class Kernel extends HttpKernel | @@ -14,8 +14,8 @@ class Kernel extends HttpKernel | ||
| 14 | * @var array<int, class-string|string> | 14 | * @var array<int, class-string|string> |
| 15 | */ | 15 | */ |
| 16 | protected $middleware = [ | 16 | protected $middleware = [ |
| 17 | - // \App\Http\Middleware\TrustHosts::class, | ||
| 18 | - \App\Http\Middleware\TrustProxies::class, | 17 | + \App\Http\Middleware\TrustHosts::class, |
| 18 | +// \App\Http\Middleware\TrustProxies::class, | ||
| 19 | \Fruitcake\Cors\HandleCors::class, | 19 | \Fruitcake\Cors\HandleCors::class, |
| 20 | \App\Http\Middleware\PreventRequestsDuringMaintenance::class, | 20 | \App\Http\Middleware\PreventRequestsDuringMaintenance::class, |
| 21 | \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, | 21 | \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, |
| @@ -20,20 +20,48 @@ class BtEvents extends Model | @@ -20,20 +20,48 @@ class BtEvents extends Model | ||
| 20 | */ | 20 | */ |
| 21 | protected $table = 'bt_events'; | 21 | protected $table = 'bt_events'; |
| 22 | 22 | ||
| 23 | + // 最大执行次数 | ||
| 24 | + const MAX_TRIES_TIMES = 3; | ||
| 25 | + | ||
| 23 | const STATUS_INIT = 0; | 26 | const STATUS_INIT = 0; |
| 24 | const STATUS_FINISH = 1; | 27 | const STATUS_FINISH = 1; |
| 25 | const STATUS_ERROR = 9; | 28 | const STATUS_ERROR = 9; |
| 26 | 29 | ||
| 30 | + const TYPE_CREATE_SITE = 1; | ||
| 31 | + const TYPE_DELETE_SITE = 2; | ||
| 32 | + const TYPE_CREATE_SSL = 3; | ||
| 33 | + const TYPE_RENEWAL_SSL = 4; | ||
| 34 | + const TYPE_HTTP_TO_HTTPS_OPEN = 5; | ||
| 35 | + const TYPE_HTTP_TO_HTTPS_CLOSE = 6; | ||
| 36 | + const TYPE_EVENT_CALLBACK = 99; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * @return array | ||
| 40 | + */ | ||
| 41 | + public static function typeMap() | ||
| 42 | + { | ||
| 43 | + return [ | ||
| 44 | + self::TYPE_CREATE_SITE => '创建站点', | ||
| 45 | + self::TYPE_DELETE_SITE => '删除站点', | ||
| 46 | + self::TYPE_CREATE_SSL => '创建ssl证书', | ||
| 47 | + self::TYPE_RENEWAL_SSL => '续签ssl证书', | ||
| 48 | + self::TYPE_HTTP_TO_HTTPS_OPEN => '开启强制https', | ||
| 49 | + self::TYPE_HTTP_TO_HTTPS_CLOSE => '关闭强制https', | ||
| 50 | + ]; | ||
| 51 | + } | ||
| 52 | + | ||
| 27 | /** | 53 | /** |
| 28 | * @param $type | 54 | * @param $type |
| 29 | * @param $param | 55 | * @param $param |
| 30 | * @return mixed | 56 | * @return mixed |
| 31 | */ | 57 | */ |
| 32 | - public static function createBtEvent($type, $param) | 58 | + public static function createBtEvent($type, $param, $start_at = '') |
| 33 | { | 59 | { |
| 60 | + $start_at = $start_at ?: date('Y-m-d H:i:s'); | ||
| 34 | $event = new self(); | 61 | $event = new self(); |
| 35 | $event->type = $type; | 62 | $event->type = $type; |
| 36 | $event->param = $param; | 63 | $event->param = $param; |
| 64 | + $event->start_at = $start_at; | ||
| 37 | $event->save(); | 65 | $event->save(); |
| 38 | return $event->id; | 66 | return $event->id; |
| 39 | } | 67 | } |
| @@ -37,7 +37,7 @@ class BtSites extends Model | @@ -37,7 +37,7 @@ class BtSites extends Model | ||
| 37 | */ | 37 | */ |
| 38 | public static function createBtSite($domain, $site_id, $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day) | 38 | public static function createBtSite($domain, $site_id, $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day) |
| 39 | { | 39 | { |
| 40 | - $site = self::where(['domain' => $domain])->fisrt(); | 40 | + $site = self::where(['domain' => $domain])->first(); |
| 41 | if (empty($site)) { | 41 | if (empty($site)) { |
| 42 | $site = new self(); | 42 | $site = new self(); |
| 43 | } | 43 | } |
| @@ -59,6 +59,6 @@ public static function createBtSite($domain, $site_id, $ssl_open, $ssl_status, $ | @@ -59,6 +59,6 @@ public static function createBtSite($domain, $site_id, $ssl_open, $ssl_status, $ | ||
| 59 | */ | 59 | */ |
| 60 | public static function getSiteByDomain($domain) | 60 | public static function getSiteByDomain($domain) |
| 61 | { | 61 | { |
| 62 | - return self::where(['domain' => $domain, 'is_del' => self::IS_DEL_FALSE])->fisrt(); | 62 | + return self::where(['domain' => $domain, 'is_del' => self::IS_DEL_FALSE])->first(); |
| 63 | } | 63 | } |
| 64 | } | 64 | } |
| @@ -12,10 +12,10 @@ | @@ -12,10 +12,10 @@ | ||
| 12 | use App\Repositories\Bt\Bt; | 12 | use App\Repositories\Bt\Bt; |
| 13 | 13 | ||
| 14 | /** | 14 | /** |
| 15 | - * Class BtRepositories | 15 | + * Class BtRepository |
| 16 | * @package App\Repositories | 16 | * @package App\Repositories |
| 17 | */ | 17 | */ |
| 18 | -class BtRepositories | 18 | +class BtRepository |
| 19 | { | 19 | { |
| 20 | public $instance = []; | 20 | public $instance = []; |
| 21 | 21 | ||
| @@ -24,13 +24,21 @@ class BtRepositories | @@ -24,13 +24,21 @@ class BtRepositories | ||
| 24 | * @param $ssl_open | 24 | * @param $ssl_open |
| 25 | * @param $ssl_auto | 25 | * @param $ssl_auto |
| 26 | * @param $ssl_auto_day | 26 | * @param $ssl_auto_day |
| 27 | + * @param int $ssl_status | ||
| 27 | * @return array|bool | 28 | * @return array|bool |
| 28 | */ | 29 | */ |
| 29 | - public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day) | 30 | + public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day, $ssl_status = 0) |
| 30 | { | 31 | { |
| 31 | $domain_array = parse_url($domain); | 32 | $domain_array = parse_url($domain); |
| 32 | $host = $domain_array['host']; | 33 | $host = $domain_array['host']; |
| 33 | 34 | ||
| 35 | + // 如果站点已经存在, 更新数据 | ||
| 36 | + $site = BtSites::getSiteByDomain($host); | ||
| 37 | + if ($site) { | ||
| 38 | + $result = BtSites::createBtSite($host, $site->site_id, $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day); | ||
| 39 | + return $result->toArray(); | ||
| 40 | + } | ||
| 41 | + | ||
| 34 | $host_array = explode('.', $host); | 42 | $host_array = explode('.', $host); |
| 35 | if (empty($host_array[0]) && $host_array[0] == 'www') | 43 | if (empty($host_array[0]) && $host_array[0] == 'www') |
| 36 | unset($host_array[0]); | 44 | unset($host_array[0]); |
| @@ -56,12 +64,14 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day) | @@ -56,12 +64,14 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day) | ||
| 56 | 64 | ||
| 57 | $bt = $this->getBtObject(); | 65 | $bt = $this->getBtObject(); |
| 58 | $result = $bt->AddSite($array); | 66 | $result = $bt->AddSite($array); |
| 59 | - | ||
| 60 | - if (empty($result) || empty($result['id'])) | 67 | + // result 返回信息 |
| 68 | + // $result = ['siteStatus' => true, 'siteId' => 39, 'ftpStatus' => false, 'databaseStatus' => false,]; | ||
| 69 | + if (empty($result) || empty($result['siteId'])) | ||
| 61 | return false; | 70 | return false; |
| 71 | + // 伪静态设置 | ||
| 72 | + // $bt->SaveFileBody($host, ''); | ||
| 62 | 73 | ||
| 63 | - $ssl_status = 0; | ||
| 64 | - $result = BtSites::createBtSite($host, $result['id'], $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day); | 74 | + $result = BtSites::createBtSite($host, $result['siteId'], $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day); |
| 65 | return $result->toArray(); | 75 | return $result->toArray(); |
| 66 | } | 76 | } |
| 67 | 77 | ||
| @@ -72,21 +82,20 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day) | @@ -72,21 +82,20 @@ public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day) | ||
| 72 | */ | 82 | */ |
| 73 | public function deleteBtSite($domain) | 83 | public function deleteBtSite($domain) |
| 74 | { | 84 | { |
| 75 | - $domain = parse_url($domain); | ||
| 76 | $domain_array = parse_url($domain); | 85 | $domain_array = parse_url($domain); |
| 77 | $host = $domain_array['host']; | 86 | $host = $domain_array['host']; |
| 78 | 87 | ||
| 79 | $bt = $this->getBtObject(); | 88 | $bt = $this->getBtObject(); |
| 80 | 89 | ||
| 81 | $site = BtSites::getSiteByDomain($host); | 90 | $site = BtSites::getSiteByDomain($host); |
| 82 | -// $id = $site->site_id; | 91 | + // $id = $site->site_id; |
| 83 | // 获取bt数据 可能要可靠一些 | 92 | // 获取bt数据 可能要可靠一些 |
| 84 | $result = $bt->Websites($host); | 93 | $result = $bt->Websites($host); |
| 85 | if (empty($result['data'])) | 94 | if (empty($result['data'])) |
| 86 | return false; | 95 | return false; |
| 87 | $id = 0; | 96 | $id = 0; |
| 88 | foreach ($result['data'] as $v) { | 97 | foreach ($result['data'] as $v) { |
| 89 | - if ($v['name'] == $domain) { | 98 | + if ($v['name'] == $host) { |
| 90 | $id = $v['id']; | 99 | $id = $v['id']; |
| 91 | break; | 100 | break; |
| 92 | } | 101 | } |
| @@ -94,14 +103,103 @@ public function deleteBtSite($domain) | @@ -94,14 +103,103 @@ public function deleteBtSite($domain) | ||
| 94 | 103 | ||
| 95 | if (empty($id)) | 104 | if (empty($id)) |
| 96 | return false; | 105 | return false; |
| 97 | - $result = $bt->WebDeleteSite($id,$domain,false,false,false); | 106 | + $result = $bt->WebDeleteSite($id,$host,false,false,false); |
| 107 | + // $result 返回信息 | ||
| 108 | + // $result = ['status' => true, 'msg' => '站点删除成功!',] | ||
| 109 | + if (empty($result['status'])) | ||
| 110 | + return false; | ||
| 111 | + | ||
| 112 | + if ($site) { | ||
| 113 | + $site->is_del = BtSites::IS_DEL_TRUE; | ||
| 114 | + $site->save(); | ||
| 115 | + } | ||
| 116 | + return true; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + /** | ||
| 120 | + * 申请ssl 并设置ssl访问 | ||
| 121 | + * @param $host | ||
| 122 | + * @return bool | ||
| 123 | + */ | ||
| 124 | + public function applySsl($host) | ||
| 125 | + { | ||
| 126 | + $site = BtSites::getSiteByDomain($host); | ||
| 127 | + if (empty($site) || empty($site->ssl_open)) | ||
| 128 | + return false; | ||
| 129 | + | ||
| 130 | + $bt = $this->getBtObject(); | ||
| 131 | + // $domains = [["id" => 71, "pid" => 35, "name" => "www.shopk.com", "port" => 80, "addtime" => "2022-11-03 14:22:08",]]; | ||
| 132 | + $domain_list = $bt->WebDoaminList($site->site_id); | ||
| 133 | + // 被删除的站点 返回空数组 不做数据删除同步 可能bt接口问题 | ||
| 134 | + if (empty($domain_list)) | ||
| 135 | + return false; | ||
| 136 | + | ||
| 137 | + // $result = ["status" => false, "msg" => "指定网站配置文件不存在!"]; | ||
| 138 | + // $result = ["status" => true, "oid" => -1, "domain" => [["name" => "*.shopk.com"],["name" => "www.shopk.com"]], "key" => "key", "csr" => "csr", "type" => 0, "httpTohttps" => true, | ||
| 139 | + // "cert_data" => ["issuer" => "TrustAsia TLS RSA CA", "notAfter" => "2023-02-22", "notBefore" => "2022-02-21", "dns" => [0 => "*.shopk.com", 1 => "shopk.com"], | ||
| 140 | + // "subject" => "*.shopk.com", "endtime" => 110], "email" => "test@message.com", "index" => null, "auth_type" => "http", "push" => ["status" => false]]; | ||
| 141 | + $ssl = $bt->GetSSL($host); | ||
| 142 | + // 已经有ssl证书了 | ||
| 143 | + if (FALSE == empty($ssl['status'])) { | ||
| 144 | + $bt->SetSSL('1', $host, $ssl['key'], $ssl['csr']); | ||
| 145 | + // 开启强制https | ||
| 146 | + // $bt->HttpToHttps($host); | ||
| 147 | + $site->ssl_status = 1; | ||
| 148 | + $site->save(); | ||
| 149 | + return true; | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + $domain = array_column($domain_list, 'name'); | ||
| 153 | + $result = $bt->ApplyCert(json_encode($domain), $site->site_id); | ||
| 154 | + if (empty($result) || empty($result['cert'])) | ||
| 155 | + return false; | ||
| 98 | 156 | ||
| 99 | - $site->is_del = BtSites::IS_DEL_TRUE; | 157 | + $bt->SetSSL('1', $host, $result['private_key'], $result['cert']); |
| 158 | + // 开启强制https | ||
| 159 | + // $bt->HttpToHttps($host); | ||
| 160 | + $site->ssl_status = 1; | ||
| 100 | $site->save(); | 161 | $site->save(); |
| 101 | return true; | 162 | return true; |
| 102 | } | 163 | } |
| 103 | 164 | ||
| 104 | /** | 165 | /** |
| 166 | + * 续签ssl | ||
| 167 | + * @param $host | ||
| 168 | + * @return bool | ||
| 169 | + */ | ||
| 170 | + public function renewalSsl($host) | ||
| 171 | + { | ||
| 172 | + $site = BtSites::getSiteByDomain($host); | ||
| 173 | + if (empty($site) || empty($site->ssl_open) || empty($site->ssl_auto)) | ||
| 174 | + return false; | ||
| 175 | + | ||
| 176 | + $bt = $this->getBtObject(); | ||
| 177 | + // $result = ["status" => false, "oid" => -1, "domain" => [["name" => "test.hagro.cn"], ["name" => "test.hagro.cn"]], "key" => false, "csr" => false, | ||
| 178 | + // "type" => -1, "httpTohttps" => false, "cert_data" => [], "email" => "test@message.com", "index" => "", "auth_type" => "http", "push" => ["status" => false]]; | ||
| 179 | + $ssl = $bt->GetSSL($host); | ||
| 180 | + if (empty($ssl['status'])) | ||
| 181 | + return $this->applySsl($host); | ||
| 182 | + | ||
| 183 | + $end = strtotime($ssl["cert_data"]["notAfter"]); | ||
| 184 | + | ||
| 185 | + if ($end - time() >= $site->ssl_auto_day * 86400) | ||
| 186 | + return true; | ||
| 187 | + | ||
| 188 | + $domain_list = $bt->WebDoaminList($site->site_id); | ||
| 189 | + // 被删除的站点 返回空数组 不做数据删除同步 可能bt接口问题 | ||
| 190 | + if (empty($domain_list)) | ||
| 191 | + return false; | ||
| 192 | + | ||
| 193 | + // 不是宝塔生成的ssl 不能续签, 直接申请ssl | ||
| 194 | + $result = $bt->RenewCert($ssl["index"]); | ||
| 195 | + if (empty($result['status'])) | ||
| 196 | + return $this->applySsl($host); | ||
| 197 | + | ||
| 198 | + $bt->SetSSL(1, $host, $result['private_key'], $result['cert']); | ||
| 199 | + return true; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + /** | ||
| 105 | * 获取bt对象 | 203 | * 获取bt对象 |
| 106 | * @param string $key | 204 | * @param string $key |
| 107 | * @param string $panel | 205 | * @param string $panel |
app/Repositories/ToolRepository.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: zhl | ||
| 5 | + * Date: 2022/11/05 | ||
| 6 | + * Time: 16:40 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace App\Repositories; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Class ToolRepository | ||
| 14 | + * @package App\Repositories | ||
| 15 | + */ | ||
| 16 | +class ToolRepository | ||
| 17 | +{ | ||
| 18 | + /** | ||
| 19 | + * @param $url | ||
| 20 | + * @param $data | ||
| 21 | + * @param string $method | ||
| 22 | + * @param array $header | ||
| 23 | + * @param int $time_out | ||
| 24 | + * @return array | ||
| 25 | + */ | ||
| 26 | + public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 30) | ||
| 27 | + { | ||
| 28 | + $ch = curl_init(); | ||
| 29 | + curl_setopt($ch, CURLOPT_TIMEOUT, $time_out); | ||
| 30 | + curl_setopt($ch, CURLOPT_URL, $url); | ||
| 31 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
| 32 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | ||
| 33 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); | ||
| 34 | + if ($data) | ||
| 35 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | ||
| 36 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([ | ||
| 37 | + 'Expect:', | ||
| 38 | + 'Content-type: application/json', | ||
| 39 | + 'Accept: application/json', | ||
| 40 | + ], $header) | ||
| 41 | + ); | ||
| 42 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | ||
| 43 | + $response = curl_exec($ch); | ||
| 44 | + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 45 | + curl_close($ch); | ||
| 46 | + return [$code, $response]; | ||
| 47 | + } | ||
| 48 | +} |
| @@ -6,9 +6,11 @@ | @@ -6,9 +6,11 @@ | ||
| 6 | "license": "MIT", | 6 | "license": "MIT", |
| 7 | "require": { | 7 | "require": { |
| 8 | "php": "^7.3|^8.0", | 8 | "php": "^7.3|^8.0", |
| 9 | + "ext-json": "*", | ||
| 10 | + "ext-curl": "*", | ||
| 9 | "fruitcake/laravel-cors": "^2.0", | 11 | "fruitcake/laravel-cors": "^2.0", |
| 10 | "guzzlehttp/guzzle": "^7.0.1", | 12 | "guzzlehttp/guzzle": "^7.0.1", |
| 11 | - "laravel/framework": "^8.75", | 13 | + "laravel/framework": "^8.0", |
| 12 | "laravel/sanctum": "^2.11", | 14 | "laravel/sanctum": "^2.11", |
| 13 | "laravel/tinker": "^2.5" | 15 | "laravel/tinker": "^2.5" |
| 14 | }, | 16 | }, |
| @@ -18,7 +20,8 @@ | @@ -18,7 +20,8 @@ | ||
| 18 | "laravel/sail": "^1.0.1", | 20 | "laravel/sail": "^1.0.1", |
| 19 | "mockery/mockery": "^1.4.4", | 21 | "mockery/mockery": "^1.4.4", |
| 20 | "nunomaduro/collision": "^5.10", | 22 | "nunomaduro/collision": "^5.10", |
| 21 | - "phpunit/phpunit": "^9.5.10" | 23 | + "phpunit/phpunit": "^9.5.10", |
| 24 | + "fzaninotto/faker": "^1.4" | ||
| 22 | }, | 25 | }, |
| 23 | "autoload": { | 26 | "autoload": { |
| 24 | "psr-4": { | 27 | "psr-4": { |
composer.lock
已删除
100644 → 0
此 diff 太大无法显示。
-
请 注册 或 登录 后发表评论