作者 赵彬吉

update

@@ -12,6 +12,7 @@ use App\Utils\HttpUtils; @@ -12,6 +12,7 @@ use App\Utils\HttpUtils;
12 use GuzzleHttp\Client; 12 use GuzzleHttp\Client;
13 use GuzzleHttp\Exception\GuzzleException; 13 use GuzzleHttp\Exception\GuzzleException;
14 use Illuminate\Support\Carbon; 14 use Illuminate\Support\Carbon;
  15 +use Symfony\Component\Process\Process;
15 16
16 class DomainInfoLogic extends BaseLogic 17 class DomainInfoLogic extends BaseLogic
17 { 18 {
@@ -226,4 +227,44 @@ class DomainInfoLogic extends BaseLogic @@ -226,4 +227,44 @@ class DomainInfoLogic extends BaseLogic
226 return $this->success(); 227 return $this->success();
227 } 228 }
228 229
  230 +
  231 + /**
  232 + * 验证是否cname或者A记录解析到目标服务器
  233 + * @param $domain
  234 + * @param $server_info
  235 + * @return mixed
  236 + * @author zbj
  237 + * @date 2023/11/13
  238 + */
  239 + public function check_cname($domain, $server_info){
  240 + $checkA = false;
  241 + $checkCname = false;
  242 +
  243 + $process = new Process(['nslookup', '-qt=a', $domain]);
  244 + $process->run();
  245 + $output = explode(PHP_EOL, $process->getOutput());
  246 + foreach ($output as $line){
  247 + if($line){
  248 + $checkA = strpos($line, $server_info['host']) !== false;
  249 + }
  250 + }
  251 + if($checkA){
  252 + return $domain;
  253 + }
  254 +
  255 + //是否cname
  256 + $process = new Process(['nslookup', '-qt=cname', $domain]);
  257 + $process->run();
  258 + $output = explode(PHP_EOL, $process->getOutput());
  259 + foreach ($output as $line){
  260 + if($line){
  261 + $checkCname = (strpos($line, $server_info['init_domain']) !== false);
  262 + }
  263 + }
  264 + if($checkCname){
  265 + return $domain;
  266 +
  267 + }
  268 + return false;
  269 + }
229 } 270 }
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 namespace App\Http\Logic\Aside\Project; 3 namespace App\Http\Logic\Aside\Project;
4 4
  5 +use App\Enums\Common\Code;
  6 +use App\Exceptions\AsideGlobalException;
5 use App\Models\Com\NoticeLog; 7 use App\Models\Com\NoticeLog;
6 use App\Models\Devops\ServerConfig; 8 use App\Models\Devops\ServerConfig;
7 use App\Models\Project\ProjectRenew; 9 use App\Models\Project\ProjectRenew;
@@ -36,6 +38,7 @@ use Illuminate\Support\Facades\DB; @@ -36,6 +38,7 @@ use Illuminate\Support\Facades\DB;
36 use Illuminate\Support\Facades\Log; 38 use Illuminate\Support\Facades\Log;
37 use Illuminate\Support\Facades\Schema; 39 use Illuminate\Support\Facades\Schema;
38 use Illuminate\Support\Str; 40 use Illuminate\Support\Str;
  41 +use App\Http\Logic\Aside\Domain\DomainInfoLogic;
39 42
40 /** 43 /**
41 * Class ProjectLogic 44 * Class ProjectLogic
@@ -130,6 +133,9 @@ class ProjectLogic extends BaseLogic @@ -130,6 +133,9 @@ class ProjectLogic extends BaseLogic
130 $this->createSite($this->param); 133 $this->createSite($this->param);
131 } 134 }
132 DB::commit(); 135 DB::commit();
  136 + }catch (AsideGlobalException $e){
  137 + DB::rollBack();
  138 + $this->fail($e->getMessage());
133 }catch (\Exception $e){ 139 }catch (\Exception $e){
134 DB::rollBack(); 140 DB::rollBack();
135 $this->fail('请填写完整后再提交'); 141 $this->fail('请填写完整后再提交');
@@ -505,18 +511,24 @@ class ProjectLogic extends BaseLogic @@ -505,18 +511,24 @@ class ProjectLogic extends BaseLogic
505 /** 511 /**
506 * 创建站点 512 * 创建站点
507 * @param $param 513 * @param $param
  514 + * @throws AsideGlobalException
508 * @author Akun 515 * @author Akun
509 * @date 2023/10/17 10:04 516 * @date 2023/10/17 10:04
510 */ 517 */
511 public function createSite($param){ 518 public function createSite($param){
512 if(isset($param['serve_id']) && $param['serve_id'] && isset($param['deploy_optimize']['domain']) && $param['deploy_optimize']['domain']){ 519 if(isset($param['serve_id']) && $param['serve_id'] && isset($param['deploy_optimize']['domain']) && $param['deploy_optimize']['domain']){
513 $server_model = new ServerConfig(); 520 $server_model = new ServerConfig();
514 - $server_info = $server_model->read(['id'=>$param['serve_id']],'init_domain'); 521 + $server_info = $server_model->read(['id'=>$param['serve_id']],['init_domain','host']);
515 522
516 $domain_model = new DomainInfo(); 523 $domain_model = new DomainInfo();
517 $domain_info = $domain_model->read(['id'=>$param['deploy_optimize']['domain']],'domain'); 524 $domain_info = $domain_model->read(['id'=>$param['deploy_optimize']['domain']],'domain');
518 525
519 if($server_info && $domain_info){ 526 if($server_info && $domain_info){
  527 +
  528 + //验证解析
  529 + if (!DomainInfoLogic::instance()->check_cname($domain_info['domain'], $server_info)) {
  530 + throw new AsideGlobalException(Code::SYSTEM_ERROR,'域名' . $domain_info['domain'] . '未解析至目标服务器');
  531 + }
520 $api_url = 'http://'.$server_info['init_domain'].'/api/createSite'; 532 $api_url = 'http://'.$server_info['init_domain'].'/api/createSite';
521 $api_param = ['domain'=>$domain_info['domain']]; 533 $api_param = ['domain'=>$domain_info['domain']];
522 try { 534 try {