DomainInfo.php
12.2 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* @remark :
* @name :DomainInfo.php
* @author :lyh
* @method :post
* @time :2023/9/11 14:37
*/
namespace App\Console\Commands\Domain;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Project\CountryCustom;
use App\Models\Project\Project;
use Illuminate\Console\Command;
use App\Models\Domain\DomainInfo as DomainInfoModel;
use Illuminate\Support\Facades\Log;
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()
{
//先更新所有域名证书有效期
$this->startUpdateSslTime();
//主站证书到期更新
$this->startUpdateCert();
//AMP站证书到期更新
$this->startUpdateAmpCert();
//创建的自定义小语种域名证书到期更新
$this->startUpdateCustomCert();
return true;
}
/**
* 更新域名证书有效期
* @author Akun
* @date 2024/09/06 11:16
*/
public function startUpdateSslTime()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$serverIpModel = new ServersIp();
$domainCreateTaskModel = new DomainCreateTask();
$list = $domainModel->where('status', '=', 1)->get();
foreach ($list as $v) {
$project_info = $projectModel->read(['id' => $v['project_id'], 'type' => ['!=', Project::TYPE_CLOSE]], ['serve_id', 'project_type']);
if (!$project_info) {
continue;
}
$servers_ip_info = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id', 'ip', 'domain']);
if (!$servers_ip_info) {
continue;
}
//除自建站项目外,记录已解析到别的ip的域名
if ($servers_ip_info['servers_id'] != ServerConfig::SELF_SITE_ID) {
//过滤已解析到别的ip的域名
if (!check_domain_record($v['domain'], $servers_ip_info)) {
Log::channel('analyze_other')->error('域名 [' . $v['domain'] . '] 已解析到别的IP');
continue;
}
}
//判断是否已经建站
if ($project_info['project_type'] == Project::PROJECT_TYPE_SEO) {
$type = DomainCreateTask::TYPE_BLOG;
} else {
$type = DomainCreateTask::TYPE_MAIN;
}
$task_info = $domainCreateTaskModel->read(['type' => $type, 'domain_id' => $v['id'], 'status' => DomainCreateTask::STATUS_SUC], ['id']);
if (!$task_info) {
continue;
}
//获取主站证书有效期并更新
$ssl_time = $this->getDomainSslTime($v['domain']);
if ($ssl_time['from'] && $ssl_time['to']) {
$v->certificate_start_time = $ssl_time['from'];
$v->certificate_end_time = $ssl_time['to'];
$v->save();
}
if ($v['amp_status'] == 1) {
$domain_array = parse_url($v['domain']);
$host = $domain_array['host'] ?? $domain_array['path'];
$host_array = explode('.', $host);
if (count($host_array) <= 2) {
array_unshift($host_array, 'm');
} else {
$host_array[0] = 'm';
}
$amp_domain = implode('.', $host_array);
if ($servers_ip_info['servers_id'] != ServerConfig::SELF_SITE_ID) {
//过滤已解析到别的ip的AMP域名
if (!check_domain_record($amp_domain, $servers_ip_info)) {
Log::channel('analyze_other')->error('AMP域名 [' . $amp_domain . '] 已解析到别的IP');
continue;
}
}
//获取AMP站证书有效期并更新
$amp_ssl_time = $this->getDomainSslTime($amp_domain);
if ($amp_ssl_time['from'] && $amp_ssl_time['to']) {
$v->amp_certificate_start_time = $amp_ssl_time['from'];
$v->amp_certificate_end_time = $amp_ssl_time['to'];
$v->save();
}
}
}
}
/**
* 主站证书到期更新
* @author Akun
* @date 2024/02/26 10:26
*/
public function startUpdateCert()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$serverIpModel = new ServersIp();
$domainCreateTaskModel = new DomainCreateTask();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $domainModel->where('status', '=', 1)->where('type', '=', 1)->where('certificate_end_time', '<', $end_day)->get()->toArray();
foreach ($list as $v) {
$project_info = $projectModel->read(['id' => $v['project_id'], 'type' => ['!=', Project::TYPE_CLOSE]], ['serve_id', 'project_type']);
if (!$project_info) {
continue;
}
$servers_ip_info = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id', 'ip', 'domain']);
if (!$servers_ip_info) {
continue;
}
//过滤自建站项目域名
if ($servers_ip_info['servers_id'] == ServerConfig::SELF_SITE_ID) {
continue;
}
//过滤已解析到别的ip的域名
if (!check_domain_record($v['domain'], $servers_ip_info)) {
continue;
}
if ($project_info['project_type'] == Project::PROJECT_TYPE_SEO) {
$type = DomainCreateTask::TYPE_BLOG;
} else {
$type = DomainCreateTask::TYPE_MAIN;
}
//创建更新站点证书任务
$task_info = $domainCreateTaskModel->read(['type' => $type, 'domain_id' => $v['id'], 'status' => ['<', DomainCreateTask::STATUS_SUC]], ['id']);
if (!$task_info) {
$domainCreateTaskModel->add([
'server_id' => $servers_ip_info['servers_id'],
'project_id' => $v['project_id'],
'domain_id' => $v['id'],
'type' => $type
]);
}
}
}
/**
* AMP站证书到期更新
* @author Akun
* @date 2024/02/26 10:26
*/
public function startUpdateAmpCert()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$serverIpModel = new ServersIp();
$domainCreateTaskModel = new DomainCreateTask();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $domainModel->where('status', '=', 1)->where('amp_status', 1)->where('amp_type', '=', 1)->where('amp_certificate_end_time', '<', $end_day)->get()->toArray();
foreach ($list as $v) {
$domain_array = parse_url($v['domain']);
$host = $domain_array['host'] ?? $domain_array['path'];
$host_array = explode('.', $host);
if (count($host_array) <= 2) {
array_unshift($host_array, 'm');
} else {
$host_array[0] = 'm';
}
$amp_domain = implode('.', $host_array);
$project_info = $projectModel->read(['id' => $v['project_id'], 'type' => ['!=', Project::TYPE_CLOSE]], ['serve_id']);
if (!$project_info) {
continue;
}
$servers_ip_info = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id', 'ip', 'domain']);
if (!$servers_ip_info) {
continue;
}
//过滤自建站项目域名
if ($servers_ip_info['servers_id'] == ServerConfig::SELF_SITE_ID) {
continue;
}
//过滤已解析到别的ip的域名
if (!check_domain_record($amp_domain, $servers_ip_info)) {
continue;
}
//创建更新站点证书任务
$task_info = $domainCreateTaskModel->read(['type' => DomainCreateTask::TYPE_AMP, 'domain_id' => $v['id'], 'status' => ['<', DomainCreateTask::STATUS_SUC]], ['id']);
if (!$task_info) {
$domainCreateTaskModel->add([
'server_id' => $servers_ip_info['servers_id'],
'project_id' => $v['project_id'],
'domain_id' => $v['id'],
'type' => DomainCreateTask::TYPE_AMP
]);
}
}
}
/**
* 自定义小语种站证书到期更新
* @author Akun
* @date 2024/03/23 10:08
*/
public function startUpdateCustomCert()
{
$customModel = new CountryCustom();
$projectModel = new Project();
$serverIpModel = new ServersIp();
$domainCreateTaskModel = new DomainCreateTask();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $customModel->where('status', 1)->where('is_create', 1)->where('type', '=', 1)->where('certificate_end_time', '<', $end_day)->get()->toArray();
foreach ($list as $v) {
$project_info = $projectModel->read(['id' => $v['project_id'], 'type' => ['!=', Project::TYPE_CLOSE]], ['serve_id']);
if (!$project_info) {
continue;
}
$servers_ip_info = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id', 'ip', 'domain']);
if (!$servers_ip_info) {
continue;
}
//过滤自建站项目域名
if ($servers_ip_info['servers_id'] == ServerConfig::SELF_SITE_ID) {
continue;
}
//过滤已解析到别的ip的域名
if (!check_domain_record($v['custom_domain'], $servers_ip_info)) {
Log::channel('analyze_other')->error('自定义跳转域名 [' . $v['custom_domain'] . '] 已解析到别的IP');
continue;
}
//创建更新站点证书任务
$task_info = $domainCreateTaskModel->read(['type' => DomainCreateTask::TYPE_CUSTOM, 'domain_id' => $v['id'], 'status' => ['<', DomainCreateTask::STATUS_SUC]], ['id']);
if (!$task_info) {
$domainCreateTaskModel->add([
'server_id' => $servers_ip_info['servers_id'],
'project_id' => $v['project_id'],
'domain_id' => $v['id'],
'type' => DomainCreateTask::TYPE_CUSTOM
]);
}
}
}
/**
* 获取域名证书有效时间
* @param $domain
* @return string[]
* @author Akun
* @date 2024/08/29 9:59
*/
public function getDomainSslTime($domain)
{
$valid_from = '';
$valid_to = '';
try {
$context = stream_context_create([
'ssl' => [
'capture_peer_cert' => true,
'capture_peer_cert_chain' => false,
'verify_peer' => false,
'verify_peer_name' => false
],
]);
$stream = stream_socket_client('ssl://' . $domain . ':443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($stream) {
$remote_cert = stream_context_get_params($stream)['options']['ssl']['peer_certificate'];
if ($remote_cert) {
$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 = '';
$valid_to = '';
}
return ['from' => $valid_from, 'to' => $valid_to];
}
}