作者 赵彬吉

引流

@@ -174,9 +174,11 @@ class WebTraffic extends Command @@ -174,9 +174,11 @@ class WebTraffic extends Command
174 } 174 }
175 $need_project = []; 175 $need_project = [];
176 foreach ($project_list as $project) { 176 foreach ($project_list as $project) {
  177 + Log::channel('traffic')->info('开始引流 project_id:' . $project['project_id']);
177 //随机引流间隔 178 //随机引流间隔
178 $res_sjjg = $this->get_rand($this->sjjg); 179 $res_sjjg = $this->get_rand($this->sjjg);
179 if ($res_sjjg == 1) { 180 if ($res_sjjg == 1) {
  181 + Log::channel('traffic')->info('随机不引流 project_id:' . $project['project_id']);
180 continue; 182 continue;
181 } 183 }
182 184
@@ -224,6 +226,7 @@ class WebTraffic extends Command @@ -224,6 +226,7 @@ class WebTraffic extends Command
224 } 226 }
225 //不访问 227 //不访问
226 if($res == 1){ 228 if($res == 1){
  229 + Log::channel('traffic')->info('特殊日期随机不引流 project_id:' . $project['project_id']);
227 unset($need_project[$project_key]); 230 unset($need_project[$project_key]);
228 unset($ips[$project_key]); 231 unset($ips[$project_key]);
229 } 232 }
  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Models\Product\Category;
  7 +use App\Models\Product\Product;
  8 +use App\Models\Project\OnlineCheck;
  9 +use App\Models\Project\Project;
  10 +use App\Models\Template\BCustomTemplate;
  11 +use App\Models\WebSetting\WebLanguage;
  12 +use App\Services\ProjectServer;
  13 +use Carbon\Carbon;
  14 +use GuzzleHttp\Client;
  15 +use GuzzleHttp\Promise\Utils;
  16 +use Illuminate\Console\Command;
  17 +use Illuminate\Support\Facades\Cache;
  18 +use Illuminate\Support\Facades\DB;
  19 +use Illuminate\Support\Facades\Log;
  20 +use Illuminate\Support\Str;
  21 +
  22 +/**
  23 + * 网站特殊引流
  24 + * 需要单独加流量的
  25 + * Class Traffic
  26 + * @package App\Console\Commands
  27 + * @author zbj
  28 + * @date 2023/5/18
  29 + */
  30 +class WebTrafficSpecial extends Command
  31 +{
  32 + /**
  33 + * The name and signature of the console command.
  34 + *
  35 + * @var string
  36 + */
  37 + protected $signature = 'web_traffic_special';
  38 +
  39 + /**
  40 + * The console command description.
  41 + *
  42 + * @var string
  43 + */
  44 + protected $description = '网站引流';
  45 +
  46 + /**
  47 + * Create a new command instance.
  48 + *
  49 + * @return void
  50 + */
  51 + public function __construct()
  52 + {
  53 + parent::__construct();
  54 + }
  55 +
  56 +
  57 + /**
  58 + * 要增加引流的项目及腰增加的数量
  59 + * key 要引流的ID
  60 + * value 每天要增加的流量
  61 + * @var array
  62 + */
  63 + protected $projects = [
  64 + 509 => 30
  65 + ];
  66 +
  67 +
  68 +
  69 + /**
  70 + * google域名后缀
  71 + * @var string[]
  72 + */
  73 + protected $suffix = [
  74 + 'co.jp' => '日本',
  75 + 'com.tr' => '土耳其',
  76 + 'nl' => '荷兰',
  77 + 'ru' => '俄罗斯',
  78 + 'fr' => '法国',
  79 + 'co.kr' => '韩国',
  80 + 'fi' => '芬兰',
  81 + 'be' => '比利时',
  82 + 'lt' => '立陶宛',
  83 + 'es' => '西班牙',
  84 + 'it' => '意大利',
  85 + 'com.au' => '澳大利亚',
  86 + 'no' => '挪威',
  87 + 'al' => '阿尔巴尼亚',
  88 + 'pt' => '葡萄牙',
  89 + 'lv' => '拉脱维亚',
  90 + 'hu' => '匈牙利',
  91 + 'cz' => '捷克',
  92 + 'de' => '德国',
  93 + 'ca' => '加拿大',
  94 + 'co.in' => '印度',
  95 + 'co.uk' => '英国',
  96 + 'com.vn' => '越南',
  97 + 'com.br' => '巴西',
  98 + 'co.il' => '以色列',
  99 + 'pl' => '波兰',
  100 + 'com.eg' => '埃及',
  101 + 'co.th' => '泰国',
  102 + 'sk' => '斯洛伐克',
  103 + 'ro' => '罗马尼亚',
  104 + 'com.mx' => '墨西哥',
  105 + 'com.my' => '马来西亚',
  106 + 'com.pk' => '巴基斯坦',
  107 + 'co.nz' => '新西兰',
  108 + 'co.za' => '南非',
  109 + 'com.ar' => '阿根廷',
  110 + 'com.kw' => '科威特',
  111 + 'com.sg' => '新加坡',
  112 + 'com.co' => '哥伦比亚',
  113 + 'co.id' => '印度尼西亚',
  114 + 'gr' => '希腊',
  115 + 'bg' => '保加利亚',
  116 + 'mn' => '蒙古',
  117 + 'dk' => '丹麦',
  118 + 'com.sa' => '沙特阿拉伯',
  119 + 'com.pe' => '秘鲁',
  120 + 'com.ph' => '菲律宾',
  121 + 'com.ua' => '乌克兰',
  122 + 'ge' => '格鲁吉亚',
  123 + 'ae' => '阿拉伯联合酋长国',
  124 + 'tn' => '突尼斯',
  125 + ];
  126 +
  127 + /**
  128 + * 概率值
  129 + * @var int[]
  130 + */
  131 + protected $sjjg = [720, 280];//访问间隔占比 访问|不访问
  132 + //访问页面类型占比 产品详情页、单页|产品分类页
  133 + protected $ymzb = [
  134 + 'urls_cats' => 700,
  135 + 'urls_details' => 300
  136 + ];
  137 + protected $sdzb = [600, 200, 150, 50]; //访问页面深度占比 1页|2页|3-6页|7-11页
  138 + protected $yddzb = [1 => 700, 2 => 300]; //移动端占比 pc|mobile
  139 + //模拟访问来源占比 (美国)
  140 + protected $lyzb = [
  141 + 'https://www.google.com/' => 630,
  142 + 'http://www.google.com/' => 30,
  143 + 'http://www.bing.com/' => 20,
  144 + 'https://www.bing.com/' => 5,
  145 + 'https://www.youtube.com/' => 5,
  146 + 'https://search.yahoo.com/' => 5,
  147 + 'https://www.facebook.com/' => 5,
  148 + ];
  149 +
  150 + //俄语
  151 + protected $eylyzb = [
  152 + 'https://www.yandex.com/' => 630,
  153 + 'https://www.google.com/' => 30,
  154 + 'http://www.google.com/' => 30,
  155 + 'http://www.bing.com/' => 20,
  156 + 'https://www.bing.com/' => 5,
  157 + 'https://www.youtube.com/' => 5,
  158 + 'https://search.yahoo.com/' => 5,
  159 + 'https://www.facebook.com/' => 5,
  160 + ];
  161 +
  162 + protected $otherzb = [700, 300]; //模拟访问来源占比 (非美国) google.com|google.其他后缀
  163 +
  164 + protected $pc_ua = [
  165 + 0 => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
  166 + 1 => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
  167 + 2 => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
  168 + ];
  169 +
  170 + protected $mobile_ua = [
  171 + 0 => 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko; googleweblight) Chrome/38.0.1025.166 Mobile Safari/535.19',
  172 + ];
  173 +
  174 + /**
  175 + * @return bool
  176 + */
  177 + public function handle()
  178 + {
  179 + try {
  180 + $type = $this->argument('type');
  181 +
  182 + $this->sleep(1);
  183 +
  184 + $page = 1;
  185 + while (true){
  186 + $project_list = $this->getProjectList($type, $page);
  187 + if(!$project_list){
  188 + break;
  189 + }
  190 + $need_project = [];
  191 + foreach ($project_list as $project) {
  192 + Log::channel('traffic')->info('开始特殊引流 project_id:' . $project['project_id']);
  193 + //随机引流间隔
  194 + $res_sjjg = $this->get_rand($this->sjjg);
  195 + if ($res_sjjg == 1) {
  196 + Log::channel('traffic')->info('随机不特殊引流 project_id:' . $project['project_id']);
  197 + continue;
  198 + }
  199 +
  200 + $project_urls = $this->getProductUrls($project['project_id']);
  201 + $project_urls['home'] = $project['domain'];
  202 + //随机访问页面
  203 + $project['visit_urls'] = $this->getVisitUrls($project_urls);
  204 + //随机客户端
  205 + $project['device_port'] = $this->get_rand($this->yddzb);
  206 + $project['user_agent'] = $project['device_port'] == 1 ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua);
  207 +
  208 + $need_project[] = $project;
  209 + }
  210 + //随机访问ip
  211 + $ips = $this->getIpAreas(count($need_project));
  212 +
  213 + //特殊日期 降访问率
  214 + foreach ($need_project as $project_key => $project){
  215 + $diff = $ips[$project_key]['diff'];
  216 + //当地时间
  217 + $w = date('w', strtotime($diff . 'hour'));
  218 + $date = date('m-d', strtotime($diff . 'hour'));
  219 + //元旦节和圣诞节按照周六的比例处理
  220 + if(in_array($date, ['01-01', '12-25'])){
  221 + $w = 6;
  222 + }
  223 + switch ($w){
  224 + case 0:
  225 + //周日降70-80%
  226 + $rate = rand(70, 80) * 10;
  227 + $res = $this->get_rand([1000-$rate, $rate]);
  228 + break;
  229 + case 5:
  230 + //周5降30-40%
  231 + $rate = rand(30, 40) * 10;
  232 + $res = $this->get_rand([1000-$rate, $rate]);
  233 + break;
  234 + case 6:
  235 + //周6降60-70%
  236 + $rate = rand(60, 70) * 10;
  237 + $res = $this->get_rand([1000-$rate, $rate]);
  238 + break;
  239 + default:
  240 + $res = 0;
  241 + }
  242 + //不访问
  243 + if($res == 1){
  244 + Log::channel('traffic')->info('特殊日期随机不特殊引流 project_id:' . $project['project_id']);
  245 + unset($need_project[$project_key]);
  246 + unset($ips[$project_key]);
  247 + }
  248 + }
  249 +
  250 + //最多10层深度
  251 + $client = new Client(['verify' => false]);
  252 + for ($j = 0; $j < 10; $j++) {
  253 + for ($j = 0; $j < 10; $j++) {
  254 + //并发请求
  255 + $promises = [];
  256 + foreach ($need_project as $project_key => $project) {
  257 + if (empty($project['visit_urls'][$j])) {
  258 + continue;
  259 + }
  260 +
  261 + $data = [
  262 + 'ip' => $ips[$project_key]['ip'],
  263 + 'url' => $project['visit_urls'][$j],
  264 + 'device_port' => $project['device_port'],
  265 + 'referrer_url' => $this->getReferer($ips[$project_key]['ip_area'], $project['lang']),
  266 + 'user_agent' => $project['user_agent'],
  267 + ];
  268 + Log::channel('traffic')->info('traffic_special project_id:' . $project['project_id'], $data);
  269 + $promises[] = $client->postAsync($project['domain'] . 'api/traffic_visit', ['form_params' => $data]);
  270 + }
  271 +
  272 + if($promises){
  273 + Utils::settle($promises)->wait();
  274 + //每个深度随机等待
  275 + sleep(rand(2, 10));
  276 + }
  277 + }
  278 + }
  279 +
  280 + $page++;
  281 + }
  282 + }catch (\Exception $e){
  283 + Log::channel('traffic')->error($e->getMessage());
  284 + }
  285 + }
  286 +
  287 + /**
  288 + * 不同项目 休眠
  289 + */
  290 + protected function sleep($type){
  291 + if($type == 1){ //1-3个月的项目
  292 + sleep(rand(5,480));
  293 + }elseif($type == 2){ //4-8个月的项目
  294 + sleep(rand(5,240));
  295 + }elseif($type == 3){ // 大于9个月的项目
  296 + sleep(rand(5,120));
  297 + }
  298 + }
  299 +
  300 + /**
  301 + * 非俄语站的引流的项目
  302 + */
  303 + protected function getProjectList($type, $page){
  304 + $ru_lang_id = WebLanguage::getIdByLang('ru');
  305 +
  306 + //推广项目
  307 + $list = Project::with('domainInfo')
  308 + ->leftJoin('gl_project_deploy_optimize as pdo', 'pdo.project_id', '=', 'gl_project.id')
  309 + ->leftJoin('gl_project_online_check as poc', 'poc.project_id', '=', 'gl_project.id')
  310 + ->where('pdo.domain', '>', 0)
  311 + ->where('poc.qa_status', OnlineCheck::STATUS_ONLINE_TRUE)
  312 + ->whereIn('gl_project.type', [Project::TYPE_TWO, Project::TYPE_FOUR])
  313 + ->where('gl_project.is_upgrade', 0) //非升级项目
  314 + ->where('gl_project.main_lang_id', '<>', $ru_lang_id) //非俄语站
  315 + ->whereIn('gl_project.id', array_keys($this->projects))
  316 + ->select(['pdo.project_id','gl_project.main_lang_id','gl_project.id'])->forPage($page, 500)->get();
  317 + //其他地方在引流的域名
  318 +// $other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray();
  319 + $other = [];
  320 + $data = [];
  321 + foreach ($list as $project) {
  322 +
  323 + //根据增加的次数 设置间隔
  324 + if(Cache::get('traffic_special_' . $project['project_id'])){
  325 + continue;
  326 + }else{
  327 + $ttl = 24 * 60 * 60 / $this->projects[$project['project_id']];
  328 + Cache::put('traffic_special_' . $project['project_id'], 1, $ttl);
  329 + }
  330 +
  331 + $lang = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
  332 + if(empty($project->domainInfo['domain'])){
  333 + continue;
  334 + }
  335 + //其他地方在引流就不再引流了
  336 + if(in_array($project->domainInfo['domain'], $other)){
  337 + continue;
  338 + }
  339 + $data[] = [
  340 + 'project_id' => $project['project_id'],
  341 + 'domain' => 'https://' . $project->domainInfo['domain'] . '/',
  342 + 'lang' => $lang
  343 + ];
  344 + }
  345 + return $data;
  346 + }
  347 +
  348 + /**
  349 + * 获取产品分类、单页和详情链接
  350 + */
  351 + protected function getProductUrls($project_id){
  352 + ProjectServer::useProject($project_id);
  353 + //已发布产品分类页面
  354 + $data['urls_cats'] = DB::connection('custom_mysql')->table('gl_product_category')
  355 + ->where('project_id', $project_id)->where('status', Category::STATUS_ACTIVE)
  356 + ->whereNull('deleted_at')
  357 + ->pluck('route','id')->toArray();
  358 +
  359 + //已发布单页面
  360 + $data['urls_page'] = [];
  361 +// $data['urls_page'] = DB::connection('custom_mysql')->table('gl_web_custom_template')
  362 +// ->where('project_id', $project_id)->where('url', '<>', '404')->where('status', BCustomTemplate::STATUS_ACTIVE)->pluck('url', 'id')->toArray();
  363 +
  364 + //已发布产品详情页
  365 + $data['urls_details'] = DB::connection('custom_mysql')->table('gl_product')
  366 + ->where('project_id', $project_id)->where('status', Product::STATUS_ON)
  367 + ->whereNull('deleted_at')
  368 + ->pluck('route', 'id')->toArray();
  369 +
  370 + $data['urls_cats'] = array_merge($data['urls_cats'], $data['urls_page']);
  371 + if(empty($data['urls_cats'])){
  372 + $data['urls_cats'] = $data['urls_details'];
  373 + }
  374 + DB::disconnect('custom_mysql');
  375 + return $data;
  376 + }
  377 +
  378 + /**
  379 + * 获取地区IP
  380 + */
  381 + protected function getIpAreas($num)
  382 + {
  383 + //本地时间为7-23点的地区
  384 + $h = date('H');
  385 + $areas = [];
  386 + $list = DB::table('gl_area_timezone')->get();
  387 + $time_zones = [];
  388 + foreach ($list as $v) {
  389 + $v = (array)$v;
  390 + $country_hour = $h + $v['diff'];
  391 + if ($country_hour < 0) {
  392 + $country_hour = 24 + $country_hour;
  393 + }
  394 + if ($country_hour >= 7 && $country_hour < 23) {
  395 + $areas[] = $v['name'];
  396 + $time_zones[$v['name']] = $v['diff'];
  397 + }
  398 + }
  399 + //根据地区随机取该地区的IP
  400 + $data = DB::table('gl_xunpan_ipdata')->whereIn('ip_area', $areas)->inRandomOrder()->limit($num)->get();
  401 + $data = Arr::s2a(Arr::a2s($data));
  402 + foreach ($data as &$item){
  403 + $item['diff'] = $time_zones[$item['ip_area']];
  404 + }
  405 + return $data;
  406 + }
  407 +
  408 + /**
  409 + * 概率算法
  410 + */
  411 + protected function get_rand($proArr) {
  412 + $result = '';
  413 + $proSum = array_sum($proArr);
  414 + foreach ($proArr as $key => $proCur) {
  415 + $randNum = mt_rand(1, $proSum);
  416 + if ($randNum <= $proCur) {
  417 + $result = $key;
  418 + break;
  419 + } else {
  420 + $proSum -= $proCur;
  421 + }
  422 + }
  423 + unset ($proArr);
  424 + return $result;
  425 + }
  426 +
  427 + /**
  428 + * 根据随机访问深度 随机获取访问页面
  429 + */
  430 + protected function getVisitUrls($project_urls){
  431 + //没有分类页 就只访问首页
  432 + if(!$project_urls['urls_cats']){
  433 + $url[] = $project_urls['home'];
  434 + return $url;
  435 + }
  436 + //随机访问深度
  437 + $res_sdzb = $this->get_rand($this->sdzb);
  438 + //随机访问页面类型
  439 + $res_ymzb = $this->get_rand($this->ymzb);
  440 +
  441 + $all_url = array_merge($project_urls['urls_cats'],$project_urls['urls_details']);
  442 + if(!$all_url){
  443 + $url[] = $project_urls['home'];
  444 + return $url;
  445 + }
  446 +
  447 + $url = [];
  448 + if($res_sdzb == 0){//深度一页
  449 + $url[] = $project_urls[$res_ymzb] ? Arr::random($project_urls[$res_ymzb]) : '';
  450 + }elseif($res_sdzb == 1){//深度两页
  451 + $url[] = $project_urls['home'];
  452 + $url[] = $project_urls[$res_ymzb] ? Arr::random($project_urls[$res_ymzb]) : '';
  453 + }elseif($res_sdzb == 2){//深度3-6页
  454 + $yms = rand(2,5); //随机页面数
  455 + $url = Arr::random($all_url, $yms);
  456 + $url = Arr::prepend($url, $project_urls['home']);//首页加到最前面去
  457 + }elseif($res_sdzb == 3){//深度7-11页
  458 + $yms = rand(6,10); //随机页面数
  459 + $url = Arr::random($all_url, $yms);
  460 + $url = Arr::prepend($url, $project_urls['home']);//首页加到最前面去
  461 + }
  462 + foreach ($url as &$v){
  463 + if(!Str::contains($v, $project_urls['home'])){
  464 + if (FALSE === strpos($v, '.htm')) {
  465 + $v .= '/';
  466 + }
  467 + $v = $project_urls['home'] . $v;
  468 + }
  469 + }
  470 + return array_unique(array_filter($url));
  471 + }
  472 +
  473 + /**
  474 + * 获取访问来路
  475 + */
  476 + protected function getReferer($ip_area, $lang){
  477 +
  478 + if($lang == 'ru'){
  479 + return $this->get_rand($this->eylyzb);
  480 + }
  481 +
  482 + if($ip_area == '美国'){
  483 + $referer = $this->get_rand($this->lyzb);
  484 + }else{
  485 + $referer = 'https://www.google.com/';
  486 +
  487 + $suffix = array_search($ip_area, $this->suffix);
  488 + if($suffix){
  489 + $res_qtzb = $this->get_rand($this->otherzb);
  490 + if($res_qtzb == 1){
  491 + $referer = 'https://www.google.'.$suffix.'/';
  492 + }
  493 + }
  494 + }
  495 + return $referer;
  496 + }
  497 +}
@@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel @@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel
28 // $schedule->command('share_user')->dailyAt('01:00')->withoutOverlapping(1); // 清除用户ayr_share数据,每天凌晨1点执行一次 28 // $schedule->command('share_user')->dailyAt('01:00')->withoutOverlapping(1); // 清除用户ayr_share数据,每天凌晨1点执行一次
29 // $schedule->command('count')->dailyAt('00:30')->withoutOverlapping(1); //每天凌晨1点执行一次 29 // $schedule->command('count')->dailyAt('00:30')->withoutOverlapping(1); //每天凌晨1点执行一次
30 $schedule->command('service_count')->dailyAt('01:00')->withoutOverlapping(1); //服务器使用情况,每天凌晨1点执行一次 30 $schedule->command('service_count')->dailyAt('01:00')->withoutOverlapping(1); //服务器使用情况,每天凌晨1点执行一次
  31 + $schedule->command('web_traffic_special')->everyMinute()->withoutOverlapping(1); // 特殊引流
31 $schedule->command('web_traffic 1')->everyThirtyMinutes(); // 引流 1-3个月的项目,半小时一次 32 $schedule->command('web_traffic 1')->everyThirtyMinutes(); // 引流 1-3个月的项目,半小时一次
32 $schedule->command('web_traffic 2')->cron('*/18 * * * *'); // 引流 4-8个月的项目,18分钟一次 33 $schedule->command('web_traffic 2')->cron('*/18 * * * *'); // 引流 4-8个月的项目,18分钟一次
33 $schedule->command('web_traffic 3')->cron('*/12 * * * *'); // 引流 大于9个月的项目,12分钟一次 34 $schedule->command('web_traffic 3')->cron('*/12 * * * *'); // 引流 大于9个月的项目,12分钟一次