作者 刘锟

update

@@ -84,7 +84,7 @@ class DomainInfo extends Command @@ -84,7 +84,7 @@ class DomainInfo extends Command
84 //除自建站项目外,记录已解析到别的ip的域名 84 //除自建站项目外,记录已解析到别的ip的域名
85 if ($servers_ip_info['servers_id'] != ServerConfig::SELF_SITE_ID) { 85 if ($servers_ip_info['servers_id'] != ServerConfig::SELF_SITE_ID) {
86 //过滤已解析到别的ip的域名 86 //过滤已解析到别的ip的域名
87 - if (!$this->check_cname($v['domain'], $servers_ip_info)) { 87 + if (!check_domain_record($v['domain'], $servers_ip_info)) {
88 Log::channel('analyze_other')->error('域名 [' . $v['domain'] . '] 已解析到别的IP'); 88 Log::channel('analyze_other')->error('域名 [' . $v['domain'] . '] 已解析到别的IP');
89 continue; 89 continue;
90 } 90 }
@@ -111,7 +111,7 @@ class DomainInfo extends Command @@ -111,7 +111,7 @@ class DomainInfo extends Command
111 111
112 if ($servers_ip_info['servers_id'] != ServerConfig::SELF_SITE_ID) { 112 if ($servers_ip_info['servers_id'] != ServerConfig::SELF_SITE_ID) {
113 //过滤已解析到别的ip的AMP域名 113 //过滤已解析到别的ip的AMP域名
114 - if (!$this->check_cname($amp_domain, $servers_ip_info)) { 114 + if (!check_domain_record($amp_domain, $servers_ip_info)) {
115 Log::channel('analyze_other')->error('AMP域名 [' . $amp_domain . '] 已解析到别的IP'); 115 Log::channel('analyze_other')->error('AMP域名 [' . $amp_domain . '] 已解析到别的IP');
116 continue; 116 continue;
117 } 117 }
@@ -158,7 +158,7 @@ class DomainInfo extends Command @@ -158,7 +158,7 @@ class DomainInfo extends Command
158 } 158 }
159 159
160 //过滤已解析到别的ip的域名 160 //过滤已解析到别的ip的域名
161 - if (!$this->check_cname($v['domain'], $servers_ip_info)) { 161 + if (!check_domain_record($v['domain'], $servers_ip_info)) {
162 continue; 162 continue;
163 } 163 }
164 164
@@ -215,7 +215,7 @@ class DomainInfo extends Command @@ -215,7 +215,7 @@ class DomainInfo extends Command
215 } 215 }
216 216
217 //过滤已解析到别的ip的域名 217 //过滤已解析到别的ip的域名
218 - if (!$this->check_cname($amp_domain, $servers_ip_info)) { 218 + if (!check_domain_record($amp_domain, $servers_ip_info)) {
219 continue; 219 continue;
220 } 220 }
221 221
@@ -262,7 +262,7 @@ class DomainInfo extends Command @@ -262,7 +262,7 @@ class DomainInfo extends Command
262 } 262 }
263 263
264 //过滤已解析到别的ip的域名 264 //过滤已解析到别的ip的域名
265 - if (!$this->check_cname($v['custom_domain'], $servers_ip_info)) { 265 + if (!check_domain_record($v['custom_domain'], $servers_ip_info)) {
266 Log::channel('analyze_other')->error('自定义跳转域名 [' . $v['custom_domain'] . '] 已解析到别的IP'); 266 Log::channel('analyze_other')->error('自定义跳转域名 [' . $v['custom_domain'] . '] 已解析到别的IP');
267 continue; 267 continue;
268 } 268 }
@@ -315,41 +315,4 @@ class DomainInfo extends Command @@ -315,41 +315,4 @@ class DomainInfo extends Command
315 } 315 }
316 return ['from' => $valid_from, 'to' => $valid_to]; 316 return ['from' => $valid_from, 'to' => $valid_to];
317 } 317 }
318 -  
319 - /**  
320 - * 验证是否cname或者A记录解析到目标服务器  
321 - * @param $domain  
322 - * @param $server_info  
323 - * @return mixed  
324 - * @author zbj  
325 - * @date 2023/11/13  
326 - */  
327 - public function check_cname($domain, $server_info)  
328 - {  
329 - $process = new Process(['nslookup', '-qt=a', $domain]);  
330 - $process->run();  
331 - $output = explode(PHP_EOL, $process->getOutput());  
332 - foreach ($output as $line) {  
333 - if ($line) {  
334 - $checkA = strpos($line, $server_info['ip']) !== false;  
335 - if ($checkA) {  
336 - return $domain;  
337 - }  
338 - }  
339 - }  
340 -  
341 - //是否cname  
342 - $process = new Process(['nslookup', '-qt=cname', $domain]);  
343 - $process->run();  
344 - $output = explode(PHP_EOL, $process->getOutput());  
345 - foreach ($output as $line) {  
346 - if ($line) {  
347 - $checkCname = (strpos($line, $server_info['domain']) !== false);  
348 - if ($checkCname) {  
349 - return $domain;  
350 - }  
351 - }  
352 - }  
353 - return false;  
354 - }  
355 } 318 }
@@ -40,7 +40,7 @@ class EmergencyRelieve extends Command @@ -40,7 +40,7 @@ class EmergencyRelieve extends Command
40 40
41 //判断域名是否已经解析到目标服务器 41 //判断域名是否已经解析到目标服务器
42 $target_server = $server_ip_model->select(['id', 'ip', 'domain'])->where('id', $value->server_ip_id)->first()->toArray(); 42 $target_server = $server_ip_model->select(['id', 'ip', 'domain'])->where('id', $value->server_ip_id)->first()->toArray();
43 - if (!$this->check_cname($domain_info['domain'], $target_server)) { 43 + if (!check_domain_record($domain_info['domain'], $target_server)) {
44 $this->output($domain_info['domain'] . ' | 未解析到目标服务器'); 44 $this->output($domain_info['domain'] . ' | 未解析到目标服务器');
45 continue; 45 continue;
46 } 46 }
@@ -86,43 +86,6 @@ class EmergencyRelieve extends Command @@ -86,43 +86,6 @@ class EmergencyRelieve extends Command
86 } 86 }
87 87
88 /** 88 /**
89 - * 验证是否cname或者A记录解析到目标服务器  
90 - * @param $domain  
91 - * @param $server_info  
92 - * @return mixed  
93 - * @author zbj  
94 - * @date 2023/11/13  
95 - */  
96 - public function check_cname($domain, $server_info)  
97 - {  
98 - $process = new Process(['nslookup', '-qt=a', $domain]);  
99 - $process->run();  
100 - $output = explode(PHP_EOL, $process->getOutput());  
101 - foreach ($output as $line) {  
102 - if ($line) {  
103 - $checkA = strpos($line, $server_info['ip']) !== false;  
104 - if ($checkA) {  
105 - return $domain;  
106 - }  
107 - }  
108 - }  
109 -  
110 - //是否cname  
111 - $process = new Process(['nslookup', '-qt=cname', $domain]);  
112 - $process->run();  
113 - $output = explode(PHP_EOL, $process->getOutput());  
114 - foreach ($output as $line) {  
115 - if ($line) {  
116 - $checkCname = (strpos($line, $server_info['domain']) !== false);  
117 - if ($checkCname) {  
118 - return $domain;  
119 - }  
120 - }  
121 - }  
122 - return false;  
123 - }  
124 -  
125 - /**  
126 * 输出处理日志 89 * 输出处理日志
127 * @param $message 90 * @param $message
128 */ 91 */
@@ -42,7 +42,7 @@ class EmergencyRenewSite extends Command @@ -42,7 +42,7 @@ class EmergencyRenewSite extends Command
42 } 42 }
43 43
44 //判断域名是否已经解析到目标服务器 44 //判断域名是否已经解析到目标服务器
45 - if (!$this->check_cname($domain_info['domain'], $target_server)) { 45 + if (!check_domain_record($domain_info['domain'], $target_server)) {
46 $this->output($domain_info['domain'] . ' | 未解析到目标服务器'); 46 $this->output($domain_info['domain'] . ' | 未解析到目标服务器');
47 continue; 47 continue;
48 } 48 }
@@ -93,43 +93,6 @@ class EmergencyRenewSite extends Command @@ -93,43 +93,6 @@ class EmergencyRenewSite extends Command
93 } 93 }
94 94
95 /** 95 /**
96 - * 验证是否cname或者A记录解析到目标服务器  
97 - * @param $domain  
98 - * @param $server_info  
99 - * @return mixed  
100 - * @author zbj  
101 - * @date 2023/11/13  
102 - */  
103 - public function check_cname($domain, $server_info)  
104 - {  
105 - $process = new Process(['nslookup', '-qt=a', $domain]);  
106 - $process->run();  
107 - $output = explode(PHP_EOL, $process->getOutput());  
108 - foreach ($output as $line) {  
109 - if ($line) {  
110 - $checkA = strpos($line, $server_info['ip']) !== false;  
111 - if ($checkA) {  
112 - return $domain;  
113 - }  
114 - }  
115 - }  
116 -  
117 - //是否cname  
118 - $process = new Process(['nslookup', '-qt=cname', $domain]);  
119 - $process->run();  
120 - $output = explode(PHP_EOL, $process->getOutput());  
121 - foreach ($output as $line) {  
122 - if ($line) {  
123 - $checkCname = (strpos($line, $server_info['domain']) !== false);  
124 - if ($checkCname) {  
125 - return $domain;  
126 - }  
127 - }  
128 - }  
129 - return false;  
130 - }  
131 -  
132 - /**  
133 * 输出处理日志 96 * 输出处理日志
134 * @param $message 97 * @param $message
135 */ 98 */
@@ -30,7 +30,7 @@ class ProjectDomainCheck extends Command @@ -30,7 +30,7 @@ class ProjectDomainCheck extends Command
30 $project_info = $project_model->read(['id' => $value->project_id], ['id', 'serve_id', 'company']); 30 $project_info = $project_model->read(['id' => $value->project_id], ['id', 'serve_id', 'company']);
31 $server_ip_info = $server_ip_model->read(['id' => $project_info['serve_id']], ['ip', 'domain']); 31 $server_ip_info = $server_ip_model->read(['id' => $project_info['serve_id']], ['ip', 'domain']);
32 32
33 - if ($this->check_cname($value->domain, $server_ip_info)) { 33 + if (check_domain_record($value->domain, $server_ip_info)) {
34 continue; 34 continue;
35 } else { 35 } else {
36 $data[] = [ 36 $data[] = [
@@ -58,43 +58,6 @@ class ProjectDomainCheck extends Command @@ -58,43 +58,6 @@ class ProjectDomainCheck extends Command
58 } 58 }
59 59
60 /** 60 /**
61 - * 验证是否cname或者A记录解析到目标服务器  
62 - * @param $domain  
63 - * @param $server_info  
64 - * @return mixed  
65 - * @author zbj  
66 - * @date 2023/11/13  
67 - */  
68 - public function check_cname($domain, $server_info)  
69 - {  
70 - $process = new Process(['nslookup', '-qt=a', $domain]);  
71 - $process->run();  
72 - $output = explode(PHP_EOL, $process->getOutput());  
73 - foreach ($output as $line) {  
74 - if ($line) {  
75 - $checkA = strpos($line, $server_info['ip']) !== false;  
76 - if ($checkA) {  
77 - return $domain;  
78 - }  
79 - }  
80 - }  
81 -  
82 - //是否cname  
83 - $process = new Process(['nslookup', '-qt=cname', $domain]);  
84 - $process->run();  
85 - $output = explode(PHP_EOL, $process->getOutput());  
86 - foreach ($output as $line) {  
87 - if ($line) {  
88 - $checkCname = (strpos($line, $server_info['domain']) !== false);  
89 - if ($checkCname) {  
90 - return $domain;  
91 - }  
92 - }  
93 - }  
94 - return false;  
95 - }  
96 -  
97 - /**  
98 * 输出处理日志 61 * 输出处理日志
99 * @param $message 62 * @param $message
100 */ 63 */
@@ -81,7 +81,7 @@ class Temp extends Command @@ -81,7 +81,7 @@ class Temp extends Command
81 */ 81 */
82 public function handle1() 82 public function handle1()
83 { 83 {
84 - $server_id = 14; 84 + $server_id = 15;
85 85
86 $server_ip_model = new ServersIp(); 86 $server_ip_model = new ServersIp();
87 87
@@ -99,23 +99,17 @@ class Temp extends Command @@ -99,23 +99,17 @@ class Temp extends Command
99 } 99 }
100 $domain = $domain_info['domain']; 100 $domain = $domain_info['domain'];
101 101
102 - $check = $this->check_cname($domain);  
103 - foreach ($check as $item) {  
104 - if (strpos($item, 'Address:') !== false) {  
105 - $ip = trim(str_replace('Address:', '', $item));  
106 - if (strpos($ip, '#') === false) {  
107 - $ip_info = $server_ip_model->read(['ip' => $ip]);  
108 - if ($ip_info === false || $ip_info['servers_id'] != $server_id) {  
109 - $data[] = [  
110 - $value->id,  
111 - $value->title,  
112 - $domain,  
113 - $ip,  
114 - $ip_info ? $ip_info['servers_id'] : '',  
115 - ];  
116 - }  
117 - }  
118 - } 102 + $check = dns_get_record($domain, DNS_A);
  103 + $ip = $check[0]['ip'] ?? '';
  104 + $ip_info = $server_ip_model->read(['ip' => $ip]);
  105 + if ($ip_info === false || $ip_info['servers_id'] != $server_id) {
  106 + $data[] = [
  107 + $value->id,
  108 + $value->title,
  109 + $domain,
  110 + $ip,
  111 + $ip_info ? $ip_info['servers_id'] : '',
  112 + ];
119 } 113 }
120 } 114 }
121 $map = ['项目id', '名称', '域名', 'IP', '服务器ID',]; 115 $map = ['项目id', '名称', '域名', 'IP', '服务器ID',];
@@ -132,13 +126,6 @@ class Temp extends Command @@ -132,13 +126,6 @@ class Temp extends Command
132 } 126 }
133 } 127 }
134 128
135 - public function check_cname($domain)  
136 - {  
137 - $process = new Process(['nslookup', '-qt=a', $domain]);  
138 - $process->run();  
139 - return explode(PHP_EOL, $process->getOutput());  
140 - }  
141 -  
142 /** 129 /**
143 * 创建所有amp站页面生成任务 130 * 创建所有amp站页面生成任务
144 * @author Akun 131 * @author Akun
@@ -959,6 +959,31 @@ function generateRandomString($length) { @@ -959,6 +959,31 @@ function generateRandomString($length) {
959 return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length); 959 return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
960 } 960 }
961 961
  962 +if (!function_exists('check_domain_record')) {
  963 + /**
  964 + * 验证是否cname或者A记录解析到目标服务器
  965 + * @param $domain
  966 + * @param $server_info
  967 + * @return bool
  968 + * @author Akun
  969 + * @date 2024/10/14 11:02
  970 + */
  971 + function check_domain_record($domain, $server_info)
  972 + {
  973 + $records = dns_get_record($domain,DNS_A);
  974 + if(count($records) != 1){
  975 + return false;
  976 + }
  977 +
  978 + $record = $records[0];
  979 + if($record['host'] == $server_info['domain'] || $record['ip'] == $server_info['ip']){
  980 + return $domain;
  981 + }else{
  982 + return false;
  983 + }
  984 + }
  985 +}
  986 +
962 987
963 988
964 989
@@ -134,13 +134,13 @@ class NoticeController extends BaseController @@ -134,13 +134,13 @@ class NoticeController extends BaseController
134 return $this->error('target_domain参数不能为空'); 134 return $this->error('target_domain参数不能为空');
135 } 135 }
136 136
137 - if(!$this->check_a($origin_domain,DomainInfo::SERVER_IP_301)){ 137 + if(!check_domain_record($origin_domain,['domain'=>'','ip'=>DomainInfo::SERVER_IP_301])){
138 return $this->error($origin_domain . ' 未解析至 ' . DomainInfo::SERVER_IP_301); 138 return $this->error($origin_domain . ' 未解析至 ' . DomainInfo::SERVER_IP_301);
139 } 139 }
140 140
141 if($other_domain){ 141 if($other_domain){
142 foreach ($other_domain as $ov) { 142 foreach ($other_domain as $ov) {
143 - if (!$this->check_a($ov, DomainInfo::SERVER_IP_301)) { 143 + if (!check_domain_record($ov, ['domain'=>'','ip'=>DomainInfo::SERVER_IP_301])) {
144 return $this->error($ov . ' 未解析至 ' . DomainInfo::SERVER_IP_301); 144 return $this->error($ov . ' 未解析至 ' . DomainInfo::SERVER_IP_301);
145 } 145 }
146 } 146 }
@@ -159,28 +159,4 @@ class NoticeController extends BaseController @@ -159,28 +159,4 @@ class NoticeController extends BaseController
159 159
160 return $this->success(); 160 return $this->success();
161 } 161 }
162 -  
163 - /**  
164 - * 验证是否A记录解析到目标服务器  
165 - * @param $domain  
166 - * @param $ip  
167 - * @return bool  
168 - * @author Akun  
169 - * @date 2024/09/19 11:14  
170 - */  
171 - public function check_a($domain, $ip)  
172 - {  
173 - $process = new Process(['nslookup', '-qt=a', $domain]);  
174 - $process->run();  
175 - $output = explode(PHP_EOL, $process->getOutput());  
176 - foreach ($output as $line) {  
177 - if ($line) {  
178 - $checkA = strpos($line, $ip) !== false;  
179 - if ($checkA) {  
180 - return $domain;  
181 - }  
182 - }  
183 - }  
184 - return false;  
185 - }  
186 } 162 }
@@ -175,16 +175,16 @@ class DomainInfoLogic extends BaseLogic @@ -175,16 +175,16 @@ class DomainInfoLogic extends BaseLogic
175 $this->fail('自建站服务器无法生成站点'); 175 $this->fail('自建站服务器无法生成站点');
176 } 176 }
177 //域名是否都已经解析 177 //域名是否都已经解析
178 - if(!$this->check_cname($info['domain'], $serversIpInfo)){ 178 + if(!check_domain_record($info['domain'], $serversIpInfo)){
179 $this->fail('域名' . $info['domain'] . '未解析至目标服务器'); 179 $this->fail('域名' . $info['domain'] . '未解析至目标服务器');
180 } 180 }
181 $domain_301 = ''; 181 $domain_301 = '';
182 foreach ($this->param['other_domain']??[] as $other_domain){ 182 foreach ($this->param['other_domain']??[] as $other_domain){
183 if($other_domain && substr($other_domain,0,2) != '*.'){ 183 if($other_domain && substr($other_domain,0,2) != '*.'){
184 - if($this->check_a($other_domain,DomainInfo::SERVER_IP_301)){ 184 + if(check_domain_record($other_domain,['domain'=>'','ip'=>DomainInfo::SERVER_IP_301])){
185 $domain_301 = $other_domain; 185 $domain_301 = $other_domain;
186 }else{ 186 }else{
187 - if(!$this->check_cname($other_domain, $serversIpInfo)){ 187 + if(!check_domain_record($other_domain, $serversIpInfo)){
188 $this->fail('域名' . $other_domain . '未解析至目标服务器'); 188 $this->fail('域名' . $other_domain . '未解析至目标服务器');
189 } 189 }
190 } 190 }
@@ -202,7 +202,7 @@ class DomainInfoLogic extends BaseLogic @@ -202,7 +202,7 @@ class DomainInfoLogic extends BaseLogic
202 $host_array[0] = 'm'; 202 $host_array[0] = 'm';
203 } 203 }
204 $amp_domain = implode('.',$host_array); 204 $amp_domain = implode('.',$host_array);
205 - if(!$this->check_cname($amp_domain, $serversIpInfo)){ 205 + if(!check_domain_record($amp_domain, $serversIpInfo)){
206 $this->fail('AMP站点域名' . $amp_domain . '未解析至目标服务器'); 206 $this->fail('AMP站点域名' . $amp_domain . '未解析至目标服务器');
207 } 207 }
208 } 208 }
@@ -286,64 +286,6 @@ class DomainInfoLogic extends BaseLogic @@ -286,64 +286,6 @@ class DomainInfoLogic extends BaseLogic
286 return $this->success(); 286 return $this->success();
287 } 287 }
288 288
289 -  
290 - /**  
291 - * 验证是否cname或者A记录解析到目标服务器  
292 - * @param $domain  
293 - * @param $server_info  
294 - * @return mixed  
295 - * @author zbj  
296 - * @date 2023/11/13  
297 - */  
298 - public function check_cname($domain, $server_info){  
299 - $process = new Process(['nslookup', '-qt=a', $domain]);  
300 - $process->run();  
301 - $output = explode(PHP_EOL, $process->getOutput());  
302 - foreach ($output as $line){  
303 - if($line){  
304 - $checkA = strpos($line, $server_info['ip']) !== false;  
305 - if($checkA){  
306 - return $domain;  
307 - }  
308 - }  
309 - }  
310 -  
311 - //是否cname  
312 - $process = new Process(['nslookup', '-qt=cname', $domain]);  
313 - $process->run();  
314 - $output = explode(PHP_EOL, $process->getOutput());  
315 - foreach ($output as $line){  
316 - if($line){  
317 - $checkCname = (strpos($line, $server_info['domain']) !== false);  
318 - if($checkCname){  
319 - return $domain;  
320 - }  
321 - }  
322 - }  
323 - return false;  
324 - }  
325 -  
326 - /**  
327 - * 验证是否A记录解析到目标服务器  
328 - * @param $domain  
329 - * @param $ip  
330 - * @return mixed  
331 - */  
332 - public function check_a($domain, $ip){  
333 - $process = new Process(['nslookup', '-qt=a', $domain]);  
334 - $process->run();  
335 - $output = explode(PHP_EOL, $process->getOutput());  
336 - foreach ($output as $line){  
337 - if($line){  
338 - $checkA = strpos($line, $ip) !== false;  
339 - if($checkA){  
340 - return $domain;  
341 - }  
342 - }  
343 - }  
344 - return false;  
345 - }  
346 -  
347 /** 289 /**
348 * 设置语种自定义跳转链接 290 * 设置语种自定义跳转链接
349 * @param $project_id 291 * @param $project_id
@@ -386,7 +328,7 @@ class DomainInfoLogic extends BaseLogic @@ -386,7 +328,7 @@ class DomainInfoLogic extends BaseLogic
386 if($has_info && ($has_info['project_id'] != $project_id || $has_info['language_id'] != $this->param['language_id'])){ 328 if($has_info && ($has_info['project_id'] != $project_id || $has_info['language_id'] != $this->param['language_id'])){
387 $this->fail('自定义域名已被使用'); 329 $this->fail('自定义域名已被使用');
388 } 330 }
389 - if(!$this->check_cname($this->param['custom_domain'], $serversIpInfo)){ 331 + if(!check_domain_record($this->param['custom_domain'], $serversIpInfo)){
390 $this->fail('域名' . $this->param['custom_domain'] . '未解析至目标服务器'); 332 $this->fail('域名' . $this->param['custom_domain'] . '未解析至目标服务器');
391 } 333 }
392 }else{ 334 }else{