DomainInfo.php
10.7 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
<?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\Project\CountryCustom;
use App\Models\Project\Project;
use Illuminate\Console\Command;
use App\Models\Domain\DomainInfo as DomainInfoModel;
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->startUpdateDomain();
//主站证书到期更新
$this->startUpdateCert();
//AMP站证书到期更新
$this->startUpdateAmpCert();
//创建的自定义小语种域名证书到期更新
$this->startUpdateCustomCert();
return true;
}
/**
* 更新域名到期时间
* @author Akun
* @date 2024/02/26 10:26
*/
public function startUpdateDomain()
{
$domainModel = new DomainInfoModel();
$list = $domainModel->where('status', '=', 1)->where(function ($query) {
$query->whereNull('domain_end_time')->orWhere('domain_end_time', '<', date('Y-m-d H:i:s'));
})->get()->toArray();
foreach ($list as $v) {
$time = $this->updateDomain($v['domain']);
$data = [
'domain_start_time' => $time['start'],
'domain_end_time' => $time['end']
];
$domainModel->edit($data, ['id' => $v['id']]);
}
}
/**
* 主站证书到期更新
* @author Akun
* @date 2024/02/26 10:26
*/
public function startUpdateCert()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $domainModel->where('status', '=', 1)->where(function ($query) use ($end_day) {
$query->whereNull('certificate_end_time')->orWhere('certificate_end_time', '<', $end_day);
})->get()->toArray();
foreach ($list as $v) {
//更新证书到期时间
$data = [];
$ssl = $this->updateDomainSsl($v['domain']);
$ssl['from'] && $data['certificate_start_time'] = $ssl['from'];
$ssl['to'] && $data['certificate_end_time'] = $ssl['to'];
$project_info = $projectModel->read(['id' => $v['project_id']], ['serve_id']);
if ($v['type'] == 1 && $ssl['to'] < $end_day && $project_info && $project_info['serve_id'] != ServerConfig::SELF_SITE_ID) {
//非自建站项目,申请免费证书
$this->updatePrivate($v);
$ssl_new = $this->updateDomainSsl($v['domain']);
$ssl_new['from'] && $data['certificate_start_time'] = $ssl_new['from'];
$ssl_new['to'] && $data['certificate_end_time'] = $ssl_new['to'];
}
$domainModel->edit($data, ['id' => $v['id']]);
}
}
/**
* AMP站证书到期更新
* @author Akun
* @date 2024/02/26 10:26
*/
public function startUpdateAmpCert()
{
$domainModel = new DomainInfoModel();
$projectModel = new Project();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $domainModel->where('status', '=', 1)->where('amp_status', 1)->where(function ($query) use ($end_day) {
$query->whereNull('amp_certificate_end_time')->orWhere('amp_certificate_end_time', '<', $end_day);
})->get()->toArray();
foreach ($list as $v) {
//更新amp站点证书到期时间
$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);
$data = [];
$ssl = $this->updateDomainSsl($amp_domain);
$ssl['from'] && $data['amp_certificate_start_time'] = $ssl['from'];
$ssl['to'] && $data['amp_certificate_end_time'] = $ssl['to'];
$project_info = $projectModel->read(['id' => $v['project_id']], ['serve_id']);
if ($v['amp_type'] == 1 && $ssl['to'] < $end_day && $project_info && $project_info['serve_id'] != ServerConfig::SELF_SITE_ID) {
//非自建站项目,申请免费证书
$this->updateAmpPrivate($v['domain']);
$ssl_new = $this->updateDomainSsl($v['domain']);
$ssl_new['from'] && $data['certificate_start_time'] = $ssl_new['from'];
$ssl_new['to'] && $data['certificate_end_time'] = $ssl_new['to'];
}
$domainModel->edit($data, ['id' => $v['id']]);
}
}
/**
* 自定义小语种站证书到期更新
* @author Akun
* @date 2024/03/23 10:08
*/
public function startUpdateCustomCert()
{
$customModel = new CountryCustom();
$end_day = date('Y-m-d H:i:s', time() + 3 * 24 * 3600);//3天后到期
$list = $customModel->where('status', 1)->where('is_create', 1)->where(function ($query) use ($end_day) {
$query->whereNull('certificate_end_time')->orWhere('certificate_end_time', '<', $end_day);
})->get()->toArray();
foreach ($list as $v) {
//更新证书到期时间
$data = [];
$ssl = $this->updateDomainSsl($v['custom_domain']);
$ssl['from'] && $data['certificate_start_time'] = $ssl['from'];
$ssl['to'] && $data['certificate_end_time'] = $ssl['to'];
if ($v['type'] == 1 && $ssl['to'] < $end_day) {
//申请免费证书
$this->updateCustomPrivate($v['custom_domain']);
$ssl_new = $this->updateDomainSsl($v['domain']);
$ssl_new['from'] && $data['certificate_start_time'] = $ssl_new['from'];
$ssl_new['to'] && $data['certificate_end_time'] = $ssl_new['to'];
}
$customModel->edit($data, ['id' => $v['id']]);
}
}
/**
* @remark :更新主站证书
* @name :updatePrivate
* @author :lyh
* @method :post
* @time :2023/12/8 16:16
*/
public function updatePrivate($param)
{
$url = 'https://' . $param['domain'] . '/api/applySsl/';
$param = [
"domain" => $param['domain'],
"rewrite" => $param['extend_config'],
'other_domain' => $param['other_domain'],
'is_https' => $param['is_https'],
];
return $this->curlRequest($url, $param);
}
/**
* 更新amp站证书
* @param $domain
* @return array
* @author Akun
* @date 2024/02/26 10:25
*/
public function updateAmpPrivate($domain)
{
$url = 'https://' . $domain . '/api/createSiteAmp/';
$param = [
"domain" => $domain,
'private_key' => '',
'cert' => ''
];
return $this->curlRequest($url, $param);
}
/**
* 更新小语种自定义站证书
* @param $domain
* @return array
* @author Akun
* @date 2024/03/23 10:07
*/
public function updateCustomPrivate($domain)
{
$url = 'http://' . $domain . '/api/applySsl';
$param = [
'domain' => $domain,
'rewrite' => [],
'other_domain' => [],
'is_https' => 1
];
return $this->curlRequest($url, $param);
}
public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 60)
{
$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];
}
/**
* @remark :获取域名证书有效时间
* @name :updateDomainSsl
* @author :lyh
* @method :post
* @time :2023/9/11 15:07
*/
public function updateDomainSsl($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];
}
/**
* @remark :更新域名有效时间
* @name :updateDomain
* @author :lyh
* @method :post
* @time :2023/9/11 15:11
*/
public function updateDomain($domain)
{
$url = 'http://openai.waimaoq.com/v1/whois_api?domain=' . $domain;
$response = http_get($url);
$start = date('Y-m-d H:i:s');
$end = date('Y-m-d H:i:s');
if ($response['code'] == 200) {
$start = $response['text']['creation_date'];
$end = $response['text']['expiration_date'];
}
return ['start' => $start, 'end' => $end];
}
}