DomainInfoLogic.php
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
namespace App\Http\Logic\Aside\Domain;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Aside\BaseLogic;
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
use App\Models\Aside\Domain\DomainInfo;
use App\Models\Aside\Domain\DomainInfoLog;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class DomainInfoLogic extends BaseLogic
{
/**
* @var array
*/
private $param;
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
}
/**
* 添加数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function create()
{
$request = $this->param ?? [];
if ($this->checkDomain($request['domain'])) {
return $this->fail('域名已存在!');
}
$domain = new DomainInfo();
$this->extracted($request, $domain, $domain->FieldsArray());
DB::beginTransaction();
if ($domain->save()) {
$original = $domain->getOriginal();
$original['belong_to'] = $request['belong_to'];
$original['status'] = $request['status'];
// 添加日志
$this->domain_action_log(DomainInfoLog::ACTION_ADD, $original, $original, '添加域名信息成功 - ID : ' . $domain->id);
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail('域名信息添加失败');
}
/**
* 修改数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function update()
{
$domain = $this->getDomain();
$original = $domain->getOriginal();
$original['belong_to'] = $domain->BelongToStr($original['belong_to']);
$original['status'] = $domain->StatusToStr($original['status']);
$request = $this->param;
$this->extracted($request, $domain, $original);
// 检查ip是否存在
if (array_key_exists('domain', $request)) {
if ($domain->domain != $request['domain']) {
if ($this->checkDomain($request['domain'])) {
$this->fail('域名信息修改失败,域名已存在', Code::USER_ERROR);
}
}
}
DB::beginTransaction();
if ($domain->save()) {
$fields_array = $domain->FieldsArray();
$revised = $domain->getAttributes();
$diff = array_diff_assoc($original, $revised);
unset($diff['created_at']);
unset($diff['updated_at']);
unset($diff['deleted']);
$remarks = '';
if ($diff) {
$remarks .= '修改ID为 ' . $domain->id . ' 的服务器信息,修改内容为:';
foreach ($diff as $key => $value) {
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
}
} else {
$remarks .= '修改ID为 ' . $domain->id . ' 的域名信息,无修改';
}
// 添加日志
$this->domain_action_log(DomainInfoLog::ACTION_UPDATE, $original, $revised, $remarks);
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail('域名信息修改失败');
}
/**
* 根据ID获取数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function getDomain(int $deleted = DomainInfo::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('ID不能为空');
}
$data = DomainInfo::query()->where('deleted', $deleted)->find($id);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
}
/**
* 检查域名是否存在
* @param $domain
* @return bool
*/
public function checkDomain($domain)
{
$usIp = DomainInfo::query()->where('domain', $domain)->first();
if ($usIp) {
return true;
} else {
return false;
}
}
/**
* 详情
* @param int $deleted
* @return array|Builder|Collection|Model
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function domainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('ID不能为空');
}
$data = DomainInfo::query()->where('deleted', $deleted)->find($id, ['domain', 'belong_to', 'status', 'domain_start_time', 'domain_end_time', 'certificate_start_time', 'certificate_end_time', 'created_at', 'updated_at']);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
}
/**
* 服务器操作日志
* @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
* @param array $original 原始数据
* @param array $revised 修改后数据
*/
public function domain_action_log(int $action = DomainInfoLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
{
$log = new DomainInfoLog();
$this->log($log, $action, $original, $revised, $remarks);
}
/**
* @param array $request
* @param $domain
* @param array $original
* @return void
*/
public function extracted(array $request, $domain, array $original)
{
$request = array_intersect_key($request, $original);
foreach ($request as $key => $value) {
$domain->$key = trim($value) ?? $original[$key];
}
}
/**
* 批量获取数据删除
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL)
{
$ids = (new ServerInformationLogic())->getIds();
$data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
$restore_ids = $data->pluck('id')->toArray();
$actionArr = DomainInfoLog::actionArr();
$actionStr = $actionArr[$action];
if (empty($restore_ids)) {
$this->fail($actionStr . '域名信息不存在!', Code::USER_ERROR);
}
$original = $data->toArray();
DB::beginTransaction();
try {
$update = $deleted == DomainInfo::DELETED_NORMAL ? DomainInfo::DELETED_DELETE : DomainInfo::DELETED_NORMAL;
DomainInfo::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
$this->domain_action_log($action, $original, $original, $actionStr . '域名信息 - ID : ' . implode(', ', $restore_ids));
DB::commit();
return $this->success();
} catch (\Exception $e) {
DB::rollBack();
return $this->fail('域名信息' . $actionStr . '失败', Code::USER_ERROR);
}
}
/**
* 证书到期时间
* @return array
*/
public function getDomainCertificateTime($domain)
{
$domain = trim($domain);
$data = [];
$context = stream_context_create(['ssl' => ['capture_peer_cert' => true]]); // Notice: only 7.0.7+ supports this
$stream = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($stream) {
$params = stream_context_get_params($stream);
$peerCertificate = openssl_x509_parse($params['options']['ssl']['peer_certificate']);
if ($peerCertificate) {
$validFrom = date_create_from_format('U', $peerCertificate['validFrom_time_t']); // 有效期开始时间
$validTo = date_create_from_format('U', $peerCertificate['validTo_time_t']); // 有效期结束时间
$data['validFrom'] = $validFrom->format('Y-m-d H:i:s');
$data['validTo'] = $validTo->format('Y-m-d H:i:s');
}
}
return $data;
}
/**
* 获取所有正常域名信息
* @return Builder[]|Collection
*/
public function getAllDomain()
{
return DomainInfo::query()->where('status', 1)
->where('deleted', DomainInfo::DELETED_NORMAL)
->orderBy('updated_at', 'desc')
->get();
}
/**
* 域名到期时间
* @param $domain
* @return array
* @throws GuzzleException
*/
public function getDomainTime($domain)
{
$url = "http://openai.waimaoq.com/v1/whois_api?domain={$domain}";
$client = new Client(['verify' => false]);
$http = $client->get($url);
$data = [];
if ($http->getStatusCode() != 200) {
return $data;
}
$content = $http->getBody()->getContents();
$json = json_decode($content, true);
if ($json['code'] == 200) {
$con = $json['text'];
$data['domain'] = $domain;
$data['validFrom'] = $con['creation_date'];
$data['validTo'] = $con['expiration_date'];
}
return $data;
}
/**
* 根据域名更新证书到期时间
* @param $id
* @param $updata
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function updateDomain($id, $updata)
{
$isRes = DomainInfo::query()->where('id', $id)->where('deleted', DomainInfo::DELETED_NORMAL)->update($updata);
if ($isRes) {
return $this->success();
} else {
return $this->fail('更新域名到期时间失败');
}
}
}