Commit dca02c34 authored by 赵然's avatar 赵然

zr

parent e7834fe4
package main.com.atguigu.springmvc.handlers;
import com.alibaba.fastjson.JSON;
import io.restassured.response.Response;
import main.com.atguigu.springmvc.common.MatcherString;
import main.com.atguigu.springmvc.service.AccountService;
import main.com.atguigu.springmvc.service.Authorization;
import main.com.atguigu.springmvc.service.SqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Controller
public class LogModule {
@Autowired
SqlService sqlService;
@Autowired
AccountService accountService;
@Autowired
Authorization authorization;
final Base64.Decoder decoder = Base64.getDecoder();
private String filepathRisk = "E:\\test_appendfile.txt" ;
private String filepathBury = "E:\\test_appendfile2.txt" ;
/**
* 功能:查询风控日志
* @return
*/
@RequestMapping(value = "/risklog", method= RequestMethod.POST)
@ResponseBody
public List risklog(HttpServletRequest request) throws Exception {
String projectId = request.getParameter("projectId");
//根据项目id找到project_id_num
Response response1 = sqlService.findSimpleResult("SELECT * FROM `projectx`.`tb_project` where project_id = '"+projectId+"' ");
String activityId = response1.jsonPath().getString("project_id_num");
//构建关键词搜索的正则表达式
String word1= "(?=.*"+activityId+")";
//获取容器id
Response response = accountService.dokerList();
//System.out.println("xxx为:"+response.asString());
String name = String.valueOf(response.jsonPath().getString("list[0].name"));
System.out.println("容器id为:"+name);
//获取日志响应
response = accountService.download_file(name,"/root/logs/projectx/inner/inner.log");
String log = response.asString();
//写入文件
try {
//String content = "a dog will be write in file";
File file = new File(filepathRisk);
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(log);
bw.close();
System.out.println("写入文件完成!");
} catch (IOException e) {
e.printStackTrace();
}
//查找文件
// BufferedReader bfr=new BufferedReader(new FileReader(filepath));//通过文件的地址“/home/fxl/java”读取文件内容
// String str=null;
// int lineNUmber=0;
// while ((str=bfr.readLine())!=null){ //读取每一行的内容将每一行赋值给str
// lineNUmber++;
// System.out.println(lineNUmber+" "+str);//输出每一行的行数和内容
// }
// bfr.close();
List list = new ArrayList();
Pattern r= Pattern.compile(word1);
File file=new File(filepathRisk);
InputStreamReader read=new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader=new BufferedReader(read);
String line=null;
int lineNumber=0;
while((line=bufferedReader.readLine())!=null){
lineNumber++;
Matcher m=r.matcher(line);//在读取时需检验所读取的行是否已经为空,若不为空则通过Matcher m=r.matcher(line)检测每行字符串中是否含有“hello”
if(m.find()){
list.add(JSON.parse(line));
break;
}
}
return list;
}
/**
* 功能:查询埋点日志
* @return
*/
@RequestMapping(value = "/burylog", method= RequestMethod.POST)
@ResponseBody
public List burylog(HttpServletRequest request) throws Exception {
String userId = request.getParameter("uid");
String projectId = request.getParameter("projectId");
String type = request.getParameter("type");
//userid转consumerid
Map<String,String> login_map = authorization.dafuwengLogin(Integer.parseInt(userId));
System.out.println("用户login_map为:" + login_map.toString());
String consumer_base64 = String.valueOf(login_map.get("_ac"));
System.out.println("用户consumer_base64为:" + consumer_base64);
String decodedText = new String(decoder.decode(consumer_base64));
System.out.println("助力者consumer_base64转码后为:" + decodedText);
String consumerId = MatcherString.getString(decodedText, "cid\":(.*?)}", 1);
System.out.println("用户consumer_id为:" + consumerId);
//构建关键词搜索的正则表达式
String word1= "(?=.*"+projectId+")";
String word2= "(?=.*"+type+")";
String word3= "(?=.*"+consumerId+")";
//获取容器id
Response response = accountService.dokerList();
//System.out.println("xxx为:"+response.asString());
String name = String.valueOf(response.jsonPath().getString("list[0].name"));
System.out.println("容器id为:"+name);
//获取日志响应
response = accountService.download_file(name,"/root/logs/projectx/access/access.log");
String log = response.asString();
//写入文件
try {
//String content = "a dog will be write in file";
File file = new File(filepathBury);
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(log);
bw.close();
System.out.println("写入文件完成!");
} catch (IOException e) {
e.printStackTrace();
}
//查找文件
// BufferedReader bfr=new BufferedReader(new FileReader(filepath));//通过文件的地址“/home/fxl/java”读取文件内容
// String str=null;
// int lineNUmber=0;
// while ((str=bfr.readLine())!=null){ //读取每一行的内容将每一行赋值给str
// lineNUmber++;
// System.out.println(lineNUmber+" "+str);//输出每一行的行数和内容
// }
// bfr.close();
List list = new ArrayList();
Pattern r= Pattern.compile(word1+word2+word3);
File file=new File(filepathBury);
InputStreamReader read=new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader=new BufferedReader(read);
String line=null;
int lineNumber=0;
while((line=bufferedReader.readLine())!=null){
lineNumber++;
Matcher m=r.matcher(line);//在读取时需检验所读取的行是否已经为空,若不为空则通过Matcher m=r.matcher(line)检测每行字符串中是否含有“hello”
if(m.find()){
list.add(JSON.parse(line));
// System.out.println("第"+lineNumber+"行"+line+"中含有"+m.group());//输出含有目标的行和内容
break;
}
}
return list;
}
public static void findStringInFile(String path,String word3)throws IOException{//实现grep功能,在main函数之外,定义了一个findStringInFile(String path)函数,其中设置了path为形式参数,表示需要读取文件的存储位置
Pattern r= Pattern.compile(word3);
File file=new File(path);
InputStreamReader read=new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader=new BufferedReader(read);
String line=null;
int lineNumber=0;
while((line=bufferedReader.readLine())!=null){
lineNumber++;
Matcher m=r.matcher(line);//在读取时需检验所读取的行是否已经为空,若不为空则通过Matcher m=r.matcher(line)检测每行字符串中是否含有“hello”
if(m.find()){
System.out.println("第"+lineNumber+"行"+line+"中含有"+m.group());//输出含有目标的行和内容
}
}
}
}
......@@ -3,7 +3,6 @@ package main.com.atguigu.springmvc.service;
import io.restassured.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.Assert;
import java.util.HashMap;
import java.util.Map;
......@@ -35,12 +34,26 @@ public class AccountService {
}
public Response dokerList() throws Exception{
// public static void main(String[] args) {
// NewtoolsService service = new NewtoolsService();
// Response response = service.index("17236");
// NewActivityService services = new NewActivityService();
// services.doJoin(response.getDetailedCookies(),"");
// }
Map<String, String> map = new HashMap<>();
map.put("deployId","252");
Response response = given().relaxedHTTPSValidation().cookies(authorization.miriaLogin()).params(map).get("https://console.duibatest.com.cn/miria/forward/deploy/dokerList");
return response;
}
public Response download_file(String name,String path) throws Exception{
Map<String, String> map = new HashMap<>();
map.put("k8sId","5");
map.put("path",path);
Response response = given().relaxedHTTPSValidation().cookies(authorization.miriaLogin()).params(map).get("http://console.duibatest.com.cn/miria/forward/file/download_file/"+name);
return response;
}
}
......@@ -49,7 +49,7 @@ public class Authorization {
private String hdPassword = "3Rq1TXSZuVQw3RxydD9K2IR/skk=";
private String ssoLoginUrl = "sso.duibatest.com.cn/doLogin";
private String ssoName = "zhaoran@duiba.com.cn";
private String ssoPassword = "123456";
private String ssoPassword = "zr112800";
final static String dafuwengLoginPath="/user/loginDuiba";
final static String hdLoginPath="/doLogin";
......@@ -204,9 +204,6 @@ public class Authorization {
hdCookies=response.getCookies();
i--;
}
// logger.info(hdCookies);
return hdCookies;
}
......@@ -259,6 +256,24 @@ public class Authorization {
return ssoCookies;
}
//miriasso登录
public Map miriaLogin(){
if(ssoCookies ==null) {
Map<String, String> paras = new HashMap<>();
paras.put("email", ssoName);
paras.put("password", ssoPassword);
paras.put("systemId", "27");
paras.put("redirect", "https://console.duibatest.com.cn/miria");
paras.put("loginTimeout", "1");
String url = "https://" + ssoLoginUrl;
Response response = given().relaxedHTTPSValidation().contentType(ContentType.JSON).body(paras).post(url);
ssoCookies =response.getCookies();
}
return ssoCookies;
}
public static void main(String[] args) {
Map<Integer,Map> map = new HashMap<>();
Map<String,String> map1 = new HashMap<>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment