Browse Source

调整自动登录流程,增加远程发送账号信息接口,增加自动登录v2脚本

master
李昭辉 1 month ago
parent
commit
e264e10ffc
  1. 2
      api/get_user_cookie.php
  2. 28
      api/send_user.php
  3. 28
      crontab/crosscheck_auto_shell.php
  4. 114
      crontab/crosscheck_auto_shell_v2.php

2
api/get_user_cookie.php

@ -9,4 +9,6 @@ if(!file_exists($cookie_path)){
exit(''); exit('');
} }
$str_cookie = file_get_contents($cookie_path); $str_cookie = file_get_contents($cookie_path);
//删除cookie文件
unlink($cookie_path);
exit($str_cookie); exit($str_cookie);

28
api/send_user.php

@ -0,0 +1,28 @@
<?php
$param = $_GET['v'];
$param = urldecode($param);
if(empty($param)){
exit('fail');
}
$arr_user_info = explode('zch8u9h3b', $param);
$user = $arr_user_info[0];
$passwd = $arr_user_info[1];
$tenant = $arr_user_info[2];
$need_login_file = __DIR__ . '/../cookie_file/needlogin_'.$user.'.txt'; // 需要登录的账号文件
$done_login_file = __DIR__ . '/../cookie_file/'.$user.'.txt'; // 已登录的账号文件
//$ban_file = __DIR__ . '/../cookie_file/ban_'.$user.'.txt'; // 被禁用的账号文件
// 检测是否已经登录
if(file_exists($need_login_file)){
exit('exist');
}
if(file_exists($done_login_file)){
exit('done');
}
// 写入需要登录的账号文件
file_put_contents($need_login_file, str_replace('zch8u9h3b', "\n", $param));
exit('ok');

28
crontab/crosscheck_auto_shell.php

@ -22,12 +22,12 @@ try{
$list = $obj->getCrossCheckAccount(); $list = $obj->getCrossCheckAccount();
// 测试数据 //测试数据
// $list[] = [ $list[] = [
// 'user' => 'mayi001', 'user' => 'mayi001',
// 'passwd' => 'Eg98*!f@42', 'passwd' => 'Eg98*!f@42',
// 'ext_info' => 'crossref-26027,zzz', 'ext_info' => 'crossref-26027,zzz',
// ]; ];
if (empty($list)){ if (empty($list)){
throw new Exception('获取crosscheck账号列表为空'); throw new Exception('获取crosscheck账号列表为空');
@ -47,6 +47,12 @@ try{
chgrp($cookie_path, 'nobody'); chgrp($cookie_path, 'nobody');
chmod($cookie_path, 0777); chmod($cookie_path, 0777);
} }
$capture_path = __DIR__ . '/../cookie_file/screen_shots/';
if (!is_dir($capture_path)) {
mkdir($capture_path, 0777, true);
chown($capture_path, 'nobody');
chgrp($capture_path, 'nobody');
}
// 检测账号cookie是否有效 // 检测账号cookie是否有效
$res = $obj->islogin(); $res = $obj->islogin();
if ($res) { if ($res) {
@ -61,7 +67,11 @@ try{
// 执行java登录脚本,获取脚本运行的时间 // 执行java登录脚本,获取脚本运行的时间
$start_time = time(true); $start_time = time(true);
$cmd = 'java -jar /datacenter/java/crossrefLogin.jar "' . $user.'" "'.$passwd.'" "'.$cookie_path.'"'; //$cmd = 'java -jar /datacenter/java/crossrefLogin.jar "' . $user.'" "'.$passwd.'" "'.$cookie_path.'"';
$cmd = "java -jar /datacenter/java/crossrefLogin_v2.jar -u '{$user}' -p '{$passwd}' -f '{$cookie_path}' -t '{$tenant}' -capture_path '{$capture_path}'";
echo $cmd.PHP_EOL;
//java -jar crossref-1.0-SNAPSHOT.jar -u '2105444612@qq.com' -p '038300asz_A' -f './mayi2_cookie.txt' -capture
$console_str = trim(shell_exec($cmd)); $console_str = trim(shell_exec($cmd));
// 计算脚本运行时间 // 计算脚本运行时间
@ -76,15 +86,15 @@ try{
$str_log = date('Y-m-d H:i:s')."|登录失败|".$user."|运行时间:".$run_time_str; $str_log = date('Y-m-d H:i:s')."|登录失败|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND); file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
}else{ }else{ // 登录成功
// 记录日志 // 记录日志
$str_log = date('Y-m-d H:i:s')."|登录成功|".$user."|运行时间:".$run_time_str; $str_log = date('Y-m-d H:i:s')."|登录成功|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND); file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 推送cookie到正式服务器
} }
system("pkill -9 chrome"); system("pkill -9 chrome");
} }
} }
sleep(5); sleep(5);
// 删除锁文件 // 删除锁文件
unlink($lock_file); unlink($lock_file);

114
crontab/crosscheck_auto_shell_v2.php

@ -0,0 +1,114 @@
<?php
include(__DIR__ . '/../lib/mCrossCheck.php');
ini_set('date.timezone', 'Asia/Shanghai');
// 同一时间只能有一个进程在运行
$lock_file = __DIR__ . '/crosscheck_auto_shell.lock';
if (file_exists($lock_file)) {
echo '另一个进程正在运行,请稍后重试。' . PHP_EOL;
exit(1);
}
// 创建锁文件
touch($lock_file);
$log_file = __DIR__ . '/../logs/'.date('Y-m-d').'/crosscheck_auto_shell.log';
$log_dir = dirname($log_file);
if(!is_dir($log_dir)){
mkdir($log_dir, 0777, true);
}
try{
// 获取目录下所有文件
$dir = __DIR__ . '/../cookie_file/';
$list = scandir($dir);
// 过滤出需要登录的账号文件
$list = array_filter($list, function($v) use ($dir){
return strpos($v, 'needlogin_') === 0 && strpos($v, '.txt') !== false;
});
foreach($list as $v){
$need_login_file = $dir.$v;
$fail_login_file = $dir.'fail_'.$v;
// 处理错误登录文件
if(file_exists($fail_login_file)){
// 获得文件内容
$fail_info = file_get_contents($fail_login_file);
$arr_user_info = explode("\n", $fail_info);
if($arr_user_info[0]+0 >=5){
// 记录日志
$str_log = date('Y-m-d H:i:s')."|已登录失败{$arr_user_info[0]}次|".$user;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
continue;
}
}
// 获得文件内容
$param = file_get_contents($need_login_file);
// 获得crosscheck账号密码
$arr_user_info = explode("\n", $param);
$user = ltrim($arr_user_info[0]);
$passwd = ltrim($arr_user_info[1]);
$tenant = ltrim($arr_user_info[2]);
// 账号cookie文件路径
$cookie_path = __DIR__ . '/../cookie_file/'.$user.'.txt';
$obj->cookie_path = $cookie_path ;
if (file_exists($cookie_path)) {
chown($cookie_path, 'nobody');
chgrp($cookie_path, 'nobody');
chmod($cookie_path, 0777);
}
$capture_path = __DIR__ . '/../cookie_file/screen_shots/';
if (!is_dir($capture_path)) {
mkdir($capture_path, 0777, true);
chown($capture_path, 'nobody');
chgrp($capture_path, 'nobody');
}
// 执行java登录脚本,获取脚本运行的时间
$start_time = time(true);
$cmd = "java -jar /datacenter/java/crossrefLogin_v2.jar -u '{$user}' -p '{$passwd}' -f '{$cookie_path}' -t '{$tenant}' -capture_path '{$capture_path}' -only_login";
echo $cmd.PHP_EOL;
//java -jar crossref-1.0-SNAPSHOT.jar -u '2105444612@qq.com' -p '038300asz_A' -f './mayi2_cookie.txt' -capture
$console_str = trim(shell_exec($cmd));
// 计算脚本运行时间
$end_time = time(true);
$run_time = $end_time - $start_time;
$run_time_str = date('i:s', $run_time);
$str_log = date('Y-m-d H:i:s')."|登录信息|".$console_str."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 判断登录是否成功
if (strpos($console_str, '登录成功') === false) { // 登录失败
// 记录日志
$str_log = date('Y-m-d H:i:s')."|登录失败|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 写入错误登录文件
$fail_info = $arr_user_info[0]+1;
file_put_contents($fail_login_file, $fail_info);
}else{
// 记录日志
$str_log = date('Y-m-d H:i:s')."|登录成功|".$user."|运行时间:".$run_time_str;
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 删除需要登录的账号文件
unlink($need_login_file);
}
system("pkill -9 chrome");
sleep(60*4);
}
// 删除锁文件
unlink($lock_file);
}catch(Exception $e){
$str_log = date('Y-m-d H:i:s')."|异常退出|异常信息:".$e->getMessage();
file_put_contents($log_file, $str_log . PHP_EOL, FILE_APPEND);
// 删除锁文件
unlink($lock_file);
}
Loading…
Cancel
Save