NoticeController.php
5.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
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2024/01/04
* Time: 16:08
*/
namespace App\Http\Controllers\Api;
use App\Models\Com\KeywordVideoTaskLog;
use App\Models\Domain\DomainInfo;
use App\Models\Domain\DomainRedirectTask;
use App\Models\Product\Keyword;
use App\Models\Visit\SyncSubmitTask;
use App\Models\Visit\Visit;
use App\Services\ProjectServer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
/**
* Class NoticeController
* @package App\Http\Controllers\Api
*/
class NoticeController extends BaseController
{
/**
* 网站引流
* FIXME 当前接口给内部引流用, 使用简易接口, 后期需要添加完整校验机制以及多数据接口
* @param Request $request
* @return false|string
*/
public function trafficVisit(Request $request)
{
$ip = $request->input('ip');
$url = $request->input('url');
$device_port = intval($request->input('device_port'));
$referrer_url = $request->input('referrer_url');
$user_agent = $request->input('user_agent');
// 数据占时不入库, 些日志分析数据
file_put_contents(storage_path('logs/trafficVisit.log'), var_export($request->all(), true) . PHP_EOL, FILE_APPEND);
// return $this->success([]);
if (empty($ip))
return $this->error('IP不能为空');
if (empty($url))
return $this->error('访问链接不能为空');
if (empty($user_agent))
return $this->error('请求头信息user_agent不能为空');
$url_array = parse_url($url);
$array = [
'ip' => $ip,
'domain' => $url_array['host'] ?? '',
'referer' => $referrer_url,
'user_agent' => $user_agent,
'data' => [
'url' => $url,
'domain' => empty($url_array['host']) ? '' : $url_array['scheme'] . '://' . $url_array['host'],
'device_port' => in_array($device_port, array_keys(Visit::deviceMap())) ? $device_port : Visit::DEVICE_PC,
'referrer_url' => $referrer_url
]
];
SyncSubmitTask::createTask($array, SyncSubmitTask::TYPE_VISIT);
return $this->success([]);
}
/**
* 生成视频任务回调
* @param Request $request
* @return int
*/
public function videoTaskCallback(Request $request)
{
// 获取参数
$task_id = $request->input('task_id');
$status = intval($request->input('status', 0));
$thumb = $request->input('video_thumb');
$video = $request->input('callback_resource');
$embed_code = $request->input('embed_code');
$all = $request->all();
// 获取子任务
$log = KeywordVideoTaskLog::where(['task_id' => $task_id])->first();
if (empty($log)){
return 200;
}
// 更新子任务状态 更新任务信息
$log->status = KeywordVideoTaskLog::STATUS_FINISH;
$log->result_status = $status;
$log->result_info = json_encode($all);
$log->save();
if ($status != 200) {
return 200;
}
// 更新关键词信息
ProjectServer::useProject($log->project_id);
$keyword = Keyword::where(['id' => $log->keyword_id])->first();
// 关键词可能已被删除
if (empty($keyword)){
return 200;
}
$keyword->video = $video;
$keyword->embed_code = $embed_code;
$keyword->video_thumb = $thumb;
$keyword->is_video_keyword = 1;
$keyword->save();
DB::disconnect('custom_mysql');
return 200;
}
/**
* 创建301跳转任务
* @param Request $request
* @return false|string
* @author Akun
* @date 2024/10/08 15:24
*/
public function addRedirect(Request $request)
{
$origin_domain = $request->input('origin_domain', '');
$other_domain = $request->input('other_domain', []);
$target_domain = $request->input('target_domain', '');
if ($other_domain && !is_array($other_domain)) {
return $this->error('other_domain参数必须为数组');
}
if (empty($origin_domain)) {
return $this->error('origin_domain参数不能为空');
}
if (empty($target_domain)) {
return $this->error('target_domain参数不能为空');
}
if(!$this->check_a($origin_domain,DomainInfo::SERVER_IP_301)){
return $this->error($origin_domain . ' 未解析至 ' . DomainInfo::SERVER_IP_301);
}
if($other_domain){
foreach ($other_domain as $ov) {
if (!$this->check_a($ov, DomainInfo::SERVER_IP_301)) {
return $this->error($ov . ' 未解析至 ' . DomainInfo::SERVER_IP_301);
}
}
}
//新增重定向任务
$redirect_model = new DomainRedirectTask();
$task_redirect_info = $redirect_model->read(['origin_domain'=>$origin_domain]);
if(!$task_redirect_info){
$redirect_model->add([
'origin_domain'=> $origin_domain,
'other_domain' => json_encode($other_domain),
'target_domain' => $target_domain
]);
}
return $this->success();
}
/**
* 验证是否A记录解析到目标服务器
* @param $domain
* @param $ip
* @return bool
* @author Akun
* @date 2024/09/19 11:14
*/
public function check_a($domain, $ip)
{
$process = new Process(['nslookup', '-qt=a', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line) {
if ($line) {
$checkA = strpos($line, $ip) !== false;
if ($checkA) {
return $domain;
}
}
}
return false;
}
}