作者 刘锟

update

@@ -8,12 +8,15 @@ @@ -8,12 +8,15 @@
8 namespace App\Http\Controllers\Api; 8 namespace App\Http\Controllers\Api;
9 9
10 use App\Models\Com\KeywordVideoTaskLog; 10 use App\Models\Com\KeywordVideoTaskLog;
  11 +use App\Models\Domain\DomainInfo;
  12 +use App\Models\Domain\DomainRedirectTask;
11 use App\Models\Product\Keyword; 13 use App\Models\Product\Keyword;
12 use App\Models\Visit\SyncSubmitTask; 14 use App\Models\Visit\SyncSubmitTask;
13 use App\Models\Visit\Visit; 15 use App\Models\Visit\Visit;
14 use App\Services\ProjectServer; 16 use App\Services\ProjectServer;
15 use Illuminate\Http\Request; 17 use Illuminate\Http\Request;
16 use Illuminate\Support\Facades\DB; 18 use Illuminate\Support\Facades\DB;
  19 +use Symfony\Component\Process\Process;
17 20
18 /** 21 /**
19 * Class NoticeController 22 * Class NoticeController
@@ -105,4 +108,79 @@ class NoticeController extends BaseController @@ -105,4 +108,79 @@ class NoticeController extends BaseController
105 DB::disconnect('custom_mysql'); 108 DB::disconnect('custom_mysql');
106 return 200; 109 return 200;
107 } 110 }
  111 +
  112 + /**
  113 + * 创建301跳转任务
  114 + * @param Request $request
  115 + * @return false|string
  116 + * @author Akun
  117 + * @date 2024/10/08 15:24
  118 + */
  119 + public function addRedirect(Request $request)
  120 + {
  121 + $origin_domain = $request->input('origin_domain', '');
  122 + $other_domain = $request->input('other_domain', []);
  123 + $target_domain = $request->input('target_domain', '');
  124 +
  125 + if ($other_domain && !is_array($other_domain)) {
  126 + return $this->error('other_domain参数必须为数组');
  127 + }
  128 +
  129 + if (empty($origin_domain)) {
  130 + return $this->error('origin_domain参数不能为空');
  131 + }
  132 +
  133 + if (empty($target_domain)) {
  134 + return $this->error('target_domain参数不能为空');
  135 + }
  136 +
  137 + if(!$this->check_a($origin_domain,DomainInfo::SERVER_IP_301)){
  138 + return $this->error($origin_domain . ' 未解析至 ' . DomainInfo::SERVER_IP_301);
  139 + }
  140 +
  141 + if($other_domain){
  142 + foreach ($other_domain as $ov) {
  143 + if (!$this->check_a($ov, DomainInfo::SERVER_IP_301)) {
  144 + return $this->error($ov . ' 未解析至 ' . DomainInfo::SERVER_IP_301);
  145 + }
  146 + }
  147 + }
  148 +
  149 + //新增重定向任务
  150 + $redirect_model = new DomainRedirectTask();
  151 + $task_redirect_info = $redirect_model->read(['origin_domain'=>$origin_domain]);
  152 + if(!$task_redirect_info){
  153 + $redirect_model->add([
  154 + 'origin_domain'=> $origin_domain,
  155 + 'other_domain' => json_encode($other_domain),
  156 + 'target_domain' => $target_domain
  157 + ]);
  158 + }
  159 +
  160 + return $this->success();
  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 + }
108 } 186 }
@@ -53,4 +53,7 @@ Route::post('selfSiteApi', [\App\Http\Controllers\Api\SelfSiteController::class, @@ -53,4 +53,7 @@ Route::post('selfSiteApi', [\App\Http\Controllers\Api\SelfSiteController::class,
53 Route::post('selfSiteNotify', [\App\Http\Controllers\Api\SelfSiteController::class, 'selfSiteNotify']); 53 Route::post('selfSiteNotify', [\App\Http\Controllers\Api\SelfSiteController::class, 'selfSiteNotify']);
54 Route::post('selfSiteVerify', [\App\Http\Controllers\Api\SelfSiteController::class, 'selfSiteVerify']); 54 Route::post('selfSiteVerify', [\App\Http\Controllers\Api\SelfSiteController::class, 'selfSiteVerify']);
55 55
  56 +//创建301跳转任务
  57 +Route::any('/addRedirect',[\App\Http\Controllers\Api\NoticeController::class,'addRedirect']);
  58 +
56 59