DomainInfo.php
8.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
<?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 Symfony\Component\Process\Process;
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->startUpdateCert();
//AMP站证书到期更新
$this->startUpdateAmpCert();
//创建的自定义小语种域名证书到期更新
$this->startUpdateCustomCert();
return true;
}
/**
* 主站证书到期更新
* @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', '!=', 2)->where('certificate_end_time', '<', $end_day)->get()->toArray();
foreach ($list as $v) {
$project_info = $projectModel->read(['id' => $v['project_id']], ['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 (!$this->check_cname($v['domain'], $servers_ip_info)) {
continue;
}
//创建更新站点证书任务
$task_info = $domainCreateTaskModel->read(['type' => DomainCreateTask::TYPE_MAIN, 'domain_id' => $v['id'], 'status' => ['<', DomainCreateTask::STATUS_SUC]]);
if (!$task_info) {
$domainCreateTaskModel->add([
'server_id' => $servers_ip_info['servers_id'],
'project_id' => $v['project_id'],
'domain_id' => $v['id'],
'type' => DomainCreateTask::TYPE_MAIN
]);
}
}
}
/**
* 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', '!=', 2)->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']], ['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 (!$this->check_cname($amp_domain, $servers_ip_info)) {
continue;
}
//创建更新站点证书任务
$task_info = $domainCreateTaskModel->read(['type' => DomainCreateTask::TYPE_AMP, 'domain_id' => $v['id'], 'status' => ['<', DomainCreateTask::STATUS_SUC]]);
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', '!=', 2)->where('certificate_end_time', '<', $end_day)->get()->toArray();
foreach ($list as $v) {
$project_info = $projectModel->read(['id' => $v['project_id']], ['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 (!$this->check_cname($v['custom_domain'], $servers_ip_info)) {
continue;
}
//创建更新站点证书任务
$task_info = $domainCreateTaskModel->read(['type' => DomainCreateTask::TYPE_CUSTOM, 'domain_id' => $v['id'], 'status' => ['<', DomainCreateTask::STATUS_SUC]]);
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
]);
}
}
}
/**
* 验证是否cname或者A记录解析到目标服务器
* @param $domain
* @param $server_info
* @return mixed
* @author zbj
* @date 2023/11/13
*/
public function check_cname($domain, $server_info)
{
$process = new Process(['nslookup', '-qt=a', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line) {
if ($line) {
$checkA = strpos($line, $server_info['ip']) !== false;
if ($checkA) {
return $domain;
}
}
}
//是否cname
$process = new Process(['nslookup', '-qt=cname', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line) {
if ($line) {
$checkCname = (strpos($line, $server_info['domain']) !== false);
if ($checkCname) {
return $domain;
}
}
}
return false;
}
}