Temp.php
4.3 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
<?php
namespace App\Console\Commands\Test;
use App\Helper\Arr;
use App\Models\Collect\CollectTask;
use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateVisit;
use App\Models\Devops\ServerConfig;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
class Temp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test_temp';
/**
* The console command description.
*
* @var string
*/
protected $description = '临时脚本';
public function handle()
{
$domain_model = new DomainInfo();
$server_model = new ServerConfig();
$project_model = new Project();
$domain_list = $domain_model->list(['domain' => ['like', 'www.%']], 'id', ['id', 'domain', 'project_id'], 'asc');
foreach ($domain_list as $info) {
$this->output('domain:' . $info['domain'] . ',开始');
$project_info = $project_model->read(['id' => $info['project_id']], 'serve_id');
if ($project_info === false) {
$this->output('获取项目数据失败');
continue;
}
$server_info = $server_model->read(['id' => $project_info['serve_id']], ['init_domain', 'host']);
if ($server_info === false) {
$this->output('获取服务器数据失败');
continue;
}
$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, $server_info)) {
$this->output('AMP站点域名' . $amp_domain . '未解析至目标服务器');
continue;
}
$api_url = 'http://' . $server_info['init_domain'] . '/api/createSiteAmp';
$api_param = [
'domain' => $info['domain'],
'private_key' => '',
'cert' => ''
];
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if (isset($rs['status']) && $rs['status'] == 200) {
$this->output('创建AMP站点成功');
} else {
$this->output($rs['message'] ?? '');
continue;
}
} catch (\Exception | GuzzleException $e) {
errorLog('创建AMP站点', $api_param, $e);
$this->output('创建AMP站点失败');
continue;
}
$data = [
'amp_status' => 1,
'amp_type' => 1,
];
$domain_model->edit($data, ['id' => $info['id']]);
}
echo '成功' . PHP_EOL;
}
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['host']) !== 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['init_domain']) !== false);
if ($checkCname) {
return $domain;
}
}
}
return false;
}
public function output($msg)
{
echo $msg . PHP_EOL;
}
}