@@ -0,0 +1,385 @@
package com.nis.web.controller.log.pxy ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.Calendar ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.Properties ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.apache.commons.lang.StringEscapeUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.stereotype.Controller ;
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.ModelAttribute ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.servlet.mvc.support.RedirectAttributes ;
import com.google.gson.Gson ;
import com.google.gson.GsonBuilder ;
import com.google.gson.reflect.TypeToken ;
import com.nis.domain.FunctionServiceDict ;
import com.nis.domain.Page ;
import com.nis.domain.PageLog ;
import com.nis.domain.SysDataDictionaryItem ;
import com.nis.domain.SysUser ;
import com.nis.domain.configuration.AppIpCfg ;
import com.nis.domain.log.BaseLogEntity ;
import com.nis.domain.log.PxyHttpLog ;
import com.nis.domain.log.PxyHttpManipulationLog ;
import com.nis.domain.maat.LogRecvData ;
import com.nis.domain.specific.SpecificServiceCfg ;
import com.nis.util.CodeDicUtils ;
import com.nis.util.Constants ;
import com.nis.util.DateUtils ;
import com.nis.util.DictUtils ;
import com.nis.util.LogUtils ;
import com.nis.util.StringUtil ;
import com.nis.util.httpclient.HttpClientUtil ;
import com.nis.web.controller.BaseController ;
import com.nis.web.dao.dashboard.codedic.CodeResult ;
import com.nis.web.security.UserUtils ;
@Controller
@RequestMapping ( " ${adminPath}/log/pxy " )
public class PxyHttpBlockLogController extends BaseController {
@RequestMapping ( value = " /httpBlockList " )
public String list ( @ModelAttribute ( " log " ) PxyHttpManipulationLog log , Model model , HttpServletRequest request ,
HttpServletResponse response , Integer searchAction ) {
try {
List < CodeResult > protocolCodeList = CodeDicUtils . getCodeList ( " protocolCode " ) ;
List < CodeResult > appCodeList = CodeDicUtils . getCodeList ( " appCode " ) ;
PageLog < PxyHttpManipulationLog > page = new PageLog < PxyHttpManipulationLog > ( request , response ) ;
Map < String , Object > params = new HashMap < String , Object > ( ) ;
params . put ( " pageSize " , page . getPageSize ( ) ) ;
params . put ( " pageNo " , page . getPageNo ( ) ) ;
if ( searchAction ! = null ) {
log . setAction ( searchAction ) ;
}
if ( StringUtils . isNotBlank ( log . getDomain ( ) ) ) {
String domain = StringEscapeUtils . unescapeHtml ( log . getDomain ( ) ) ;
params . put ( " searchDomain " , domain ) ;
}
if ( StringUtils . isNotBlank ( log . getUrl ( ) ) ) {
String httpurl = StringEscapeUtils . unescapeHtml ( log . getUrl ( ) ) ;
params . put ( " searchUrl " , httpurl ) ;
}
if ( StringUtils . isNotBlank ( log . getsIp ( ) ) ) {
log . setClientIp ( log . getsIp ( ) ) ;
}
if ( StringUtils . isNotBlank ( log . getdIp ( ) ) ) {
log . setServerIp ( log . getdIp ( ) ) ;
}
if ( log . getCfgId ( ) ! = null & & log . getPolicyId ( ) = = null ) {
log . setPolicyId ( log . getCfgId ( ) ) ;
}
if ( StringUtils . isNotBlank ( log . getAppId ( ) ) ) {
params . put ( " searchAppId " , log . getAppId ( ) ) ;
}
if ( StringUtils . isNotBlank ( log . getProtocolId ( ) ) ) {
params . put ( " searchProtocolId " , log . getProtocolId ( ) ) ;
}
initLogSearchValues ( log , params ) ;
List < FunctionServiceDict > serviceList = DictUtils . getFunctionServiceDictList ( log . getFunctionId ( ) ) ;
model . addAttribute ( " serviceList " , serviceList ) ;
model . addAttribute ( " appList " , appCodeList ) ;
model . addAttribute ( " protocolList " , protocolCodeList ) ;
String url = Constants . LOG_BASE_URL + Constants . POLICY_EVENT ;
String recv = HttpClientUtil . getMsg ( url , params , request ) ;
Gson gson = new GsonBuilder ( ) . create ( ) ;
LogRecvData < PxyHttpManipulationLog > fromJson = gson . fromJson ( recv , new TypeToken < LogRecvData < PxyHttpManipulationLog > > ( ) {
} . getType ( ) ) ;
if ( fromJson . getStatus ( ) . intValue ( ) = = 200 ) {
Page < PxyHttpManipulationLog > data = fromJson . getData ( ) ;
page . setCount ( data . getCount ( ) ) ;
page . setLast ( data . getLast ( ) ) ;
page . setList ( data . getList ( ) ) ;
List < PxyHttpManipulationLog > list = page . getList ( ) ;
for ( PxyHttpManipulationLog l : list ) {
l . setFunctionId ( log . getFunctionId ( ) ) ;
// setLogAction(l, serviceList);
l . setStartTime ( timeStamp2Date ( l . getStartTime ( ) , null ) ) ;
l . setEndTime ( timeStamp2Date ( l . getEndTime ( ) , null ) ) ;
l . setRecvTime ( timeStamp2Date ( l . getRecvTime ( ) , null ) ) ;
for ( CodeResult code : appCodeList ) {
Integer value3 = Integer . valueOf ( code . getCode ( ) ) ;
if ( StringUtils . isNotBlank ( l . getAppId ( ) ) & & l . getAppId ( ) . equals ( code . getCode ( ) ) ) {
l . setAppName ( code . getItem ( ) ) ;
break ;
}
}
for ( CodeResult code : protocolCodeList ) {
Integer value3 = Integer . valueOf ( code . getCode ( ) ) ;
if ( StringUtils . isNotBlank ( l . getProtocolId ( ) ) & & l . getProtocolId ( ) . equals ( code . getCode ( ) ) ) {
l . setProtocolName ( code . getItem ( ) ) ;
break ;
}
}
}
}
model . addAttribute ( " page " , page ) ;
} catch ( Exception e ) {
logger . error ( " 查询失败 " , e ) ;
addMessageLog ( model , e . getMessage ( ) ) ;
LogUtils . saveLog ( request , null , e , null ) ;
}
return " /log/pxy/pxyHttpBlockList " ;
}
// 导出
@RequestMapping ( value = " exportPxyHttpBlock " )
public void exportPxy ( @ModelAttribute ( " log " ) PxyHttpManipulationLog log , Model model , String hColumns , String type ,
HttpServletRequest request , HttpServletResponse response , RedirectAttributes redirectAttributes ) {
try {
List < String > titleList = new ArrayList < String > ( ) ;
Map < String , Class < ? > > classMap = new HashMap < String , Class < ? > > ( ) ;
Map < String , List > dataMap = new HashMap < String , List > ( ) ;
Map < String , String > noExportMap = new HashMap < String , String > ( ) ;
// ---------------------------
List < CodeResult > protocolCodeList = CodeDicUtils . getCodeList ( " protocolCode " ) ;
List < CodeResult > appCodeList = CodeDicUtils . getCodeList ( " appCode " ) ;
PageLog < PxyHttpManipulationLog > page = new PageLog < PxyHttpManipulationLog > ( request , response ) ;
page . setPageNo ( 1 ) ;
page . setPageSize ( Constants . MAX_LOG_EXPORT_SIZE ) ;
Map < String , Object > params = new HashMap < String , Object > ( ) ;
params . put ( " pageSize " , page . getPageSize ( ) ) ;
params . put ( " pageNo " , page . getPageNo ( ) ) ;
if ( StringUtils . isNotBlank ( log . getsIp ( ) ) ) {
log . setClientIp ( log . getsIp ( ) ) ;
}
if ( StringUtils . isNotBlank ( log . getdIp ( ) ) ) {
log . setServerIp ( log . getdIp ( ) ) ;
}
initLogSearchValues ( log , params ) ;
if ( StringUtils . isNotBlank ( log . getUrl ( ) ) ) {
String httpurl = StringEscapeUtils . unescapeHtml ( log . getUrl ( ) ) ;
params . put ( " searchUrl " , httpurl ) ;
}
if ( StringUtils . isNotBlank ( log . getDomain ( ) ) ) {
String domain = StringEscapeUtils . unescapeHtml ( log . getDomain ( ) ) ;
params . put ( " searchDomain " , domain ) ;
}
if ( StringUtils . isNotBlank ( log . getAppId ( ) ) ) {
params . put ( " searchAppId " , log . getAppId ( ) ) ;
}
if ( StringUtils . isNotBlank ( log . getProtocolId ( ) ) ) {
params . put ( " searchProtocolId " , log . getProtocolId ( ) ) ;
}
List < FunctionServiceDict > serviceList = DictUtils . getFunctionServiceDictList ( log . getFunctionId ( ) ) ;
model . addAttribute ( " serviceList " , serviceList ) ;
String url = Constants . LOG_BASE_URL + Constants . POLICY_EVENT ;
String recv = HttpClientUtil . getMsg ( url , params , request ) ;
List < PxyHttpManipulationLog > list = new ArrayList < PxyHttpManipulationLog > ( ) ;
if ( StringUtils . isNotBlank ( recv ) ) {
Gson gson = new GsonBuilder ( ) . create ( ) ;
LogRecvData < PxyHttpManipulationLog > fromJson = gson . fromJson ( recv , new TypeToken < LogRecvData < PxyHttpManipulationLog > > ( ) {
} . getType ( ) ) ;
if ( fromJson . getStatus ( ) . intValue ( ) = = 200 ) {
Page < PxyHttpManipulationLog > data = fromJson . getData ( ) ;
list = data . getList ( ) ;
}
}
for ( PxyHttpManipulationLog l : list ) {
l . setFunctionId ( log . getFunctionId ( ) ) ;
l . setStartTime ( timeStamp2Date ( l . getStartTime ( ) , null ) ) ;
l . setEndTime ( timeStamp2Date ( l . getEndTime ( ) , null ) ) ;
l . setRecvTime ( timeStamp2Date ( l . getRecvTime ( ) , null ) ) ;
for ( CodeResult code : appCodeList ) {
Integer value3 = Integer . valueOf ( code . getCode ( ) ) ;
if ( StringUtils . isNotBlank ( l . getAppId ( ) ) & & l . getAppId ( ) . equals ( code . getCode ( ) ) ) {
l . setAppName ( code . getItem ( ) ) ;
break ;
}
}
for ( CodeResult code : protocolCodeList ) {
Integer value3 = Integer . valueOf ( code . getCode ( ) ) ;
if ( StringUtils . isNotBlank ( l . getProtocolId ( ) ) & & l . getProtocolId ( ) . equals ( code . getCode ( ) ) ) {
l . setProtocolName ( code . getItem ( ) ) ;
break ;
}
}
}
titleList . add ( " http_manipulation " ) ;
classMap . put ( " http_manipulation " , PxyHttpManipulationLog . class ) ;
SysUser user = UserUtils . getUser ( ) ;
if ( ! user . isAdmin ( ) ) {
hColumns + = " ,scene_file, " ;
} else {
hColumns + = " , " ;
}
String cfgIndexInfoNoExport = " ,req_header,resp_header,inner_smac,inner_dmac,nest_addr_list,scene_file, "
+ " ,found_time, "
+ hColumns ;
noExportMap . put ( " http_manipulation " , cfgIndexInfoNoExport ) ;
dataMap . put ( " http_manipulation " , list ) ;
String timeRange = initLogMaps ( log , " http_manipulation " ) ;
noExportMap . put ( " timeRange " , timeRange ) ;
if ( " csv " . equals ( type ) ) {
this . _exportCsv ( model , request , response , redirectAttributes , " http_manipulation " , titleList , classMap ,
dataMap , noExportMap ) ;
} else {
this . _export ( model , request , response , redirectAttributes , " http_manipulation " , titleList , classMap , dataMap ,
noExportMap ) ;
}
} catch ( Exception e ) {
logger . error ( " Http Manipulation export failed " , e ) ;
addMessage ( redirectAttributes , " error " , " export_failed " ) ;
LogUtils . saveLog ( request , null , e , null ) ;
}
}
public void initLogSearchValues ( PxyHttpManipulationLog entry , Map < String , Object > params ) {
if ( StringUtils . isNotBlank ( entry . getSearchFoundStartTime ( ) )
& & StringUtils . isNotBlank ( entry . getSearchFoundEndTime ( ) ) ) {
params . put ( " searchStartRecvTime " , date2TimeStamp ( entry . getSearchFoundStartTime ( ) , " yyyy-MM-dd HH:mm:ss " ) ) ;
params . put ( " searchEndRecvTime " , date2TimeStamp ( entry . getSearchFoundEndTime ( ) , " yyyy-MM-dd HH:mm:ss " ) ) ;
} else {
// 判断是否是从配置界面过来的日志查询
if ( StringUtils . isNotBlank ( entry . getIsLogTotalSearch ( ) ) ) {
Calendar time = Calendar . getInstance ( ) ;
String searchEndTime = DateUtils . formatDateTime ( time . getTime ( ) ) ;
time . add ( Calendar . MINUTE , - ( Constants . LOG_TIME_RANGE / 1000 / 60 ) ) ;
String searchStartTime = DateUtils . formatDateTime ( time . getTime ( ) ) ;
params . put ( " searchStartRecvTime " , date2TimeStamp ( searchStartTime , " yyyy-MM-dd HH:mm:ss " ) ) ;
params . put ( " searchEndRecvTime " , date2TimeStamp ( searchEndTime , " yyyy-MM-dd HH:mm:ss " ) ) ;
entry . setSearchFoundStartTime ( searchStartTime ) ;
entry . setSearchFoundEndTime ( searchEndTime ) ;
} else {
// 设置默认查询当前时间及前五分钟
String endTime = DateUtils . getDateTime ( ) ;
Date dateStart = new Date ( new Date ( ) . getTime ( ) - Constants . LOG_TIME_INTERVAL ) ;
String startTime = DateUtils . formatDateTime ( dateStart ) ;
params . put ( " searchStartRecvTime " , String . valueOf ( dateStart . getTime ( ) / 1000 ) ) ;
params . put ( " searchEndRecvTime " , date2TimeStamp ( endTime , " yyyy-MM-dd HH:mm:ss " ) ) ;
entry . setSearchFoundStartTime ( startTime ) ;
entry . setSearchFoundEndTime ( endTime ) ;
}
logger . info ( " searchStartRecvTime: " + params . get ( " searchStartRecvTime " ) ) ;
logger . info ( " searchEndRecvTime: " + params . get ( " searchEndRecvTime " ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getTransProto ( ) ) ) {
params . put ( " searchTransProto " , entry . getTransProto ( ) ) ;
}
/* if (entry.getService() != null) {
params.put("searchService", entry.getService());
} else if (entry.getAction() != null) {
List<FunctionServiceDict> serviceList = DictUtils.getFunctionServiceDictList(entry.getFunctionId());
for (FunctionServiceDict dict : serviceList) {
if (dict.getAction().intValue() == entry.getAction().intValue()
&& dict.getFunctionId().intValue() == entry.getFunctionId().intValue()) {
entry.setService(dict.getServiceId());
params.put("searchService", dict.getServiceId());
}
}
}*/
if ( StringUtils . isNotBlank ( entry . getServerIp ( ) ) ) {
params . put ( " searchServerIp " , entry . getServerIp ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getClientIp ( ) ) ) {
params . put ( " searchClientIp " , entry . getClientIp ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getCapIp ( ) ) ) {
params . put ( " searchCapIp " , entry . getCapIp ( ) ) ;
}
if ( entry . getDirection ( ) ! = null ) {
params . put ( " searchDirection " , entry . getDirection ( ) ) ;
}
if ( entry . getEntranceId ( ) ! = null ) {
params . put ( " searchEntranceId " , entry . getEntranceId ( ) ) ;
}
if ( entry . getPolicyId ( ) ! = null ) {
params . put ( " searchPolicyId " , entry . getPolicyId ( ) ) ;
}
if ( entry . getAction ( ) ! = null ) {
params . put ( " searchAction " , entry . getAction ( ) ) ;
}
if ( entry . getAddrType ( ) ! = null ) {
params . put ( " searchAddrType " , entry . getAddrType ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getIsp ( ) ) ) {
params . put ( " searchIsp " , entry . getIsp ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getServerLocation ( ) ) ) {
params . put ( " searchServerLocation " , entry . getServerLocation ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getClientLocation ( ) ) ) {
params . put ( " searchClientLocation " , entry . getClientLocation ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getHost ( ) ) ) {
params . put ( " searchHost " , entry . getHost ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getSni ( ) ) ) {
params . put ( " searchSni " , entry . getSni ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getSan ( ) ) ) {
params . put ( " searchSan " , entry . getSan ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getCn ( ) ) ) {
params . put ( " searchCn " , entry . getCn ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getOrderBy ( ) ) ) {
params . put ( " orderBy " , entry . getOrderBy ( ) ) ;
}
if ( StringUtils . isNotBlank ( entry . getFields ( ) ) ) {
params . put ( " fields " , entry . getFields ( ) ) ;
}
}
public String initLogMaps ( PxyHttpManipulationLog log , String title ) {
Properties msgProp = getMsgProp ( ) ;
String logTime = msgProp . getProperty ( title , title ) ; ;
if ( log . getSearchFoundStartTime ( ) ! = null ) {
logTime + = " " + msgProp . getProperty ( " begin_date " ) + " : " + log . getSearchFoundStartTime ( ) ;
}
if ( log . getSearchFoundEndTime ( ) ! = null ) {
logTime + = " " + msgProp . getProperty ( " end_date " ) + " : " + log . getSearchFoundEndTime ( ) ;
}
return logTime ;
}
/**
* 日期格式字符串转换成时间戳
* @param date 字符串日期
* @param format 如: yyyy-MM-dd HH:mm:ss
* @return
*/
public static String date2TimeStamp ( String date_str , String format ) {
try {
SimpleDateFormat sdf = new SimpleDateFormat ( format ) ;
return String . valueOf ( sdf . parse ( date_str ) . getTime ( ) / 1000 ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return " " ;
}
/**
* 时间戳转换成日期格式字符串
* @param seconds 精确到秒的字符串
* @param formatStr
* @return
*/
public static String timeStamp2Date ( String seconds , String format ) {
if ( seconds = = null | | seconds . isEmpty ( ) | | seconds . equals ( " null " ) ) {
return " " ;
}
if ( format = = null | | format . isEmpty ( ) ) format = " yyyy-MM-dd HH:mm:ss " ;
SimpleDateFormat sdf = new SimpleDateFormat ( format ) ;
return sdf . format ( new Date ( Long . valueOf ( seconds + " 000 " ) ) ) ;
}
}