DomainInfoLogic.php
16.9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
namespace App\Http\Logic\Aside\Domain;
use App\Http\Logic\Aside\BaseLogic;
use App\Jobs\EditAmpDomainBt;
use App\Jobs\EditCustomDomainBt;
use App\Jobs\EditDomainBt;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Domain\DomainInfo;
use App\Models\Project\CountryCustom;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
use Symfony\Component\Process\Process;
class DomainInfoLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new DomainInfo();
$this->param = $this->requestAll;
}
/**
* @remark :保存域名
* @name :createDomain
* @author :lyh
* @method :post
* @time :2023/8/1 14:52
*/
public function saveDomain()
{
$domain = parse_url($this->param['domain'], PHP_URL_HOST);
if(!empty($domain)){
$this->param['domain'] = trim($domain['host']);
}
//验证域名
$this->verifyDomain($this->param['domain'],$this->param['id'] ?? '');
if(isset($this->param['id']) && !empty($this->param['id'])){
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
}else{
//查看域名是否以WWW开头
if (strpos($this->param['domain'], 'www.') === 0) {
$this->param['other_domain'] = json_encode([str_replace('www', '*', $this->param['domain']),str_replace('www.', '', $this->param['domain'])]);
}
$rs = $this->model->add($this->param);
}
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @remark :验证域名是否存在
* @name :verifyDomain
* @author :lyh
* @method :post
* @time :2023/8/1 14:59
*/
public function verifyDomain($domain,$id = ''){
if(!empty($id)){
$info = $this->model->read(['domain'=>$domain,'id'=>['!=',$id]]);
if ($info !== false) {
$this->fail('当前域名已存在');
}
}else{
$info = $this->model->read(['domain'=>$domain]);
if ($info !== false) {
$this->fail('当前域名已存在');
}
}
return $this->success();
}
/**
* @remark :修改当前域名状态
* @name :editStatus
* @author :lyh
* @method :post
* @time :2023/8/1 15:43
*/
public function editDomainStatus(){
//查看当前域名是否有项目在使用
if($this->param['status'] != $this->model::STATUS_ONE){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前域名有项目正在使用中');
}
}
$rs = $this->model->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @remark :获取所有项目列表
* @name :getProjectList
* @author :lyh
* @method :post
* @time :2023/8/1 16:10
*/
public function getProjectList($map){
$projectModel = new Project();
$lists = $projectModel->list($map,'id',['id','title']);
return $this->success($lists);
}
/**
* 删除域名
*/
public function delDomain(){
$ids = $this->param['id'];
// 初始化数据
if (FALSE == is_array($ids))
$ids = [$ids];
foreach ($ids as $k => $v){
$domain = DomainInfo::where(['id' => $v])->first();
if (empty($domain))
continue;
if (FALSE == empty($domain['project_id']))
$this->fail($domain->domain . '域名正在使用中, 删除失败!');
$domain->delete();
}
return $this->success();
}
public function infoDomain(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者已被删除');
}
if(!empty($info['project_id'])){
$info['company'] = (new Project())->read(['id'=>$info['project_id']],['title'])['title'];
}
return $this->success($info);
}
/**
* 编辑网站证书
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2023/10/17 11:52
*/
public function setDomainSsl($initDomain,$domain,$rewrite,$other_domain,$is_https)
{
if($this->param['type'] == 2){
if(empty($this->param['key'])){
$this->fail('证书KEY值不能为空');
}
if(empty($this->param['cert'])){
$this->fail('证书cert值不能为空');
}
$api_url = 'http://'.$initDomain.'/api/setSsl';
$api_param = [
'domain' => $domain,
'private_key' => $this->param['key'],
'cert' => $this->param['cert'],
'rewrite'=>$rewrite,
'other_domain'=>$other_domain,
'is_https' => $is_https
];
}else{
$api_url = 'http://'.$initDomain.'/api/applySsl';
$api_param = [
'domain' => $domain,
'rewrite'=>$rewrite,
'other_domain'=>$other_domain,
'is_https' => $is_https
];
}
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if(isset($rs['status']) && $rs['status'] == 200){
return $this->success();
}else{
$this->fail($rs['message']??'');
}
} catch (\Exception | GuzzleException $e) {
errorLog('创建站点', $api_param, $e);
$this->fail('编辑证书失败');
}
return $this->success();
}
/**
* 编辑amp网站证书
* @param $initDomain
* @param $domain
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2024/02/22 14:58
*/
public function setAmpDomainSsl($initDomain,$domain)
{
if($this->param['amp_type'] == 2){
if(empty($this->param['amp_key'])){
$this->fail('AMP站点证书KEY值不能为空');
}
if(empty($this->param['amp_cert'])){
$this->fail('AMP站点证书cert值不能为空');
}
}
$api_url = 'http://'.$initDomain.'/api/createSiteAmp';
$api_param = [
'domain' => $domain,
'private_key' => $this->param['amp_key']??'',
'cert' => $this->param['amp_cert']??''
];
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if(isset($rs['status']) && $rs['status'] == 200){
return $this->success();
}else{
$this->fail($rs['message']??'');
}
} catch (\Exception | GuzzleException $e) {
errorLog('创建AMP站点', $api_param, $e);
$this->fail('编辑AMP站点证书失败');
}
return $this->success();
}
/**
* @remark :保存证书相关配置
* @name :sslSave
* @author :lyh
* @method :post
* @time :2023/11/6 10:50
*/
public function sslSave(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者已被删除');
}
//获取项目数据
$project_model = new Project();
$project_info = $project_model->read(['id'=>$info['project_id']],'serve_id');
if($project_info === false){
$this->fail('获取项目数据失败');
}
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id'=>$project_info['serve_id']],['servers_id','ip','domain']);
if($serversIpInfo === false){
$this->fail('获取服务器数据失败');
}
if($serversIpInfo['servers_id'] == ServerConfig::SELF_TEST_ID){
$this->fail('请切换服务器,生成站点不能使用测试服务器');
}
if($serversIpInfo['servers_id'] == ServerConfig::SELF_SITE_ID){
$this->model->edit(['amp_status' => $this->param['amp_status'] ?? 0],['id'=>$this->param['id']]);
$this->fail('自建站服务器无法生成站点');
}
//域名是否都已经解析
if(!empty($info['domain']) && !$this->check_cname($info['domain'], $serversIpInfo)){
$this->fail('域名' . $info['domain'] . '未解析至目标服务器');
}
foreach ($this->param['other_domain']??[] as $other_domain){
if($other_domain && !$this->check_cname($other_domain, $serversIpInfo)){
$this->fail('域名' . $other_domain . '未解析至目标服务器');
}
}
//如果要开通amp站点,判断m域名是否已经解析
if(isset($this->param['amp_status']) && $this->param['amp_status'] == 1){
$domain_array = parse_url($info['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(!$this->check_cname($amp_domain, $serversIpInfo)){
$this->fail('AMP站点域名' . $amp_domain . '未解析至目标服务器');
}
}
//保存301跳转数据+其他域名
$data = [
'other_domain'=>json_encode(array_filter($this->param['other_domain'] ?? [])),
'extend_config'=>json_encode(array_filter($this->param['extend_config'] ?? [])),
'type'=>$this->param['type'],
'private_key' => $this->param['key'] ?? '',
'private_cert' => $this->param['cert'] ?? '',
'is_https' => $this->param['is_https'] ?? 0,
'amp_status' => $this->param['amp_status'] ?? 0,
'amp_type' => $this->param['amp_type'] ?? 0,
'amp_private_key' => $this->param['amp_key'] ?? '',
'amp_private_cert' => $this->param['amp_cert'] ?? '',
'not_allow_country'=>json_encode(array_filter($this->param['not_allow_country'] ?? [])),
'not_allow_ip'=>json_encode(array_filter($this->param['not_allow_ip'] ?? [])),
'is_redirect' => $this->param['is_redirect'] ?? 0,
];
$this->model->edit($data,['id'=>$this->param['id']]);
//新增建站任务
$task_model = new DomainCreateTask();
$task_info = $task_model->read(['type'=>DomainCreateTask::TYPE_MAIN,'domain_id'=>$this->param['id'],'status'=>['<',DomainCreateTask::STATUS_SUC]]);
if(!$task_info){
$task_model->add([
'server_id' => $serversIpInfo['servers_id'],
'project_id' => $info['project_id'],
'domain_id' => $this->param['id'],
'type' => DomainCreateTask::TYPE_MAIN
]);
}
if($data['amp_status']){
$task_amp_info = $task_model->read(['type'=>DomainCreateTask::TYPE_AMP,'domain_id'=>$this->param['id'],'status'=>['<',DomainCreateTask::STATUS_SUC]]);
if(!$task_amp_info){
$task_model->add([
'server_id' => $serversIpInfo['servers_id'],
'project_id' => $info['project_id'],
'domain_id' => $this->param['id'],
'type' => DomainCreateTask::TYPE_AMP
]);
}
}
// //主站生成证书
// EditDomainBt::dispatch($this->param['id']);
// //amp站生成证书
// if($data['amp_status']){
// EditAmpDomainBt::dispatch($this->param['id']);
// }
return $this->success();
}
/**
* 验证是否cname或者A记录解析到目标服务器
* @param $domain
* @param $server_info
* @return mixed
* @author zbj
* @date 2023/11/13
*/
public function check_cname($domain, $server_info){
$checkA = false;
$checkCname = false;
$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;
}
/**
* 设置语种自定义跳转链接
* @param $project_id
* @return array
* @author Akun
* @date 2024/03/05 9:48
*/
public function country_custom($project_id){
$project_model = new Project();
$project_info = $project_model->read(['id'=>$project_id],'serve_id');
if($project_info === false){
$this->fail('获取项目数据失败');
}
$custom_model = new CountryCustom();
if($this->param['is_create']){
//需要创建站点
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id'=>$project_info['serve_id']],['servers_id','ip','domain']);
if($serversIpInfo === false){
$this->fail('获取服务器数据失败');
}
if($serversIpInfo['servers_id'] == ServerConfig::SELF_TEST_ID){
$this->fail('请切换服务器,生成站点不能使用测试服务器');
}
if($serversIpInfo['servers_id'] == ServerConfig::SELF_SITE_ID){
$this->fail('自建站服务器无法生成站点');
}
//域名是否都已经解析
if(strpos($this->param['custom_domain'],'//') === false){
$this->param['custom_domain'] = '//'.$this->param['custom_domain'];
}
$domain_arr = parse_url($this->param['custom_domain']);
if(!isset($domain_arr['host'])){
$this->fail('自定义域名填写错误');
}
$this->param['custom_domain'] = $domain_arr['host'];
//判断域名是否已经被使用
$has_info = $custom_model->read(['custom_domain'=>$this->param['custom_domain']]);
if($has_info && ($has_info['project_id'] != $project_id || $has_info['language_id'] != $this->param['language_id'])){
$this->fail('自定义域名已被使用');
}
if(!$this->check_cname($this->param['custom_domain'], $serversIpInfo)){
$this->fail('域名' . $this->param['custom_domain'] . '未解析至目标服务器');
}
}else{
$this->param['custom_domain'] = str_replace('https://','',$this->param['custom_domain']);
$this->param['custom_domain'] = str_replace('http://','',$this->param['custom_domain']);
$this->param['custom_domain'] = str_replace('//','',$this->param['custom_domain']);
}
$info = $custom_model->read(['project_id'=>$project_id,'language_id'=>$this->param['language_id']]);
if($info === false){
$this->param['project_id'] = $project_id;
$id = $custom_model->addReturnId($this->param);
}else{
$custom_model->edit($this->param,['id'=>$info['id']]);
$id = $info['id'];
}
if($this->param['is_create']){
$task_model = new DomainCreateTask();
$task_amp_info = $task_model->read(['type'=>DomainCreateTask::TYPE_CUSTOM,'domain_id'=>$id,'status'=>['<',DomainCreateTask::STATUS_SUC]]);
if(!$task_amp_info){
$task_model->add([
'server_id' => $serversIpInfo['servers_id'],
'project_id' => $project_id,
'domain_id' => $id,
'type' => DomainCreateTask::TYPE_CUSTOM
]);
}
// //创建站点,设置证书
// EditCustomDomainBt::dispatch($id);
}
return $this->success();
}
}