<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ page import="java.util.*,java.io.*,weaver.filter.XssUtil" %>
<%@ page import="weaver.security.classLoader.ReflectMethodCall,weaver.hrm.*,java.util.regex.*" %>
<jsp:useBean id="xssUtil" class="weaver.filter.XssUtil" scope="page"></jsp:useBean>
<%String secondPrefixPath = xssUtil.null2String(xssUtil.getRule().get("second-prefix-path"));%>
<%

User userr = HrmUserVarify.getUser(request, response);
if(userr == null){
	return;
}
 XssUtil sc = new XssUtil();
int uid = sc.getIntValue(sc.null2String(sc.getRule().get("userID")),1);
if(userr.getUID()!=uid){
	//response.sendRedirect("/notice/noright.jsp");
	out.println("please login with admin.");
	return;
}
	
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>WebService拦截信息</title>

    <link href="<%=secondPrefixPath%>/security/monitor/logparser/static/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="<%=secondPrefixPath%>/security/monitor/logparser/static/bootstrap-3.3.5/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/>
   
</head>
<body>
<div class="container">
    <h2 class="text-center">WebService拦截信息</h2>
</div>
<br>

<div class="bs-example" data-example-id="hoverable-table">
    <table class="table table-hover table-responsive  table-condensed table-striped">
      <thead>
          <tr>
              <th>日期</th>
              <th>路径</th>
              <th>服务器IP</th>
              <th>服务器端口</th>
              <th>操作者</th>
              <th>拦截的IP</th>
			  <th>来源城市</th>
          </tr>
      </thead>
      <tbody>
        <%!
	           //返回正则的方法
				public String returnNeedStr(String reg,String str){
					try{
						Pattern pattern = Pattern.compile(reg);
						Matcher matcher = pattern.matcher(str);
						while(matcher.find()){
							return htmlEncode(matcher.group(1));
						}
					}catch(Exception e){
					   // e.printStackTrace();
					}
					return  null;
				}

				//返回正则的方法
				public String returnNeedStr2(String reg,String str){
					try{
						Pattern pattern = Pattern.compile(reg);
						Matcher matcher = pattern.matcher(str);
						while(matcher.find()){
							return htmlEncode(matcher.group(0));
						}
					}catch(Exception e){
						// e.printStackTrace();
					}
					return  null;
				}

				public  String htmlEncode(String source) {
						if(source == null) {
						   return "";
						}
					   String html = "";
					   StringBuffer buffer = new StringBuffer();
						for(int i = 0; i < source.length(); i++) {
						   char c = source.charAt(i);
						   switch (c) {
						   case '<':
							   buffer.append("&lt;");
							   break;
						   case '>':
							   buffer.append("&gt;");
							   break;
						   case '&':
							   buffer.append("&amp;");
							   break;
						   case '"':
							   buffer.append("&quot;");
							   break;
						   case 10:
						   case 13:
							   break;
						   default:
							   buffer.append(c);
						   }
						}
					   html = buffer.toString();
					   return html;
		 }
	     %>
		<%   
		     InputStreamReader inputStreamReader = null;
			BufferedReader reader = null;
			try{
			
				String securityLogPath = sc.getXssLogFilePath()+ File.separatorChar+"systemSecurity"+sc.getCurrentDateString()+".log";
				File logFile = new File(securityLogPath);
				if(!logFile.exists()){
					 out.println("<tr><td>暂无当天日志<td></tr>");
					 return;
				}
				inputStreamReader = new InputStreamReader( new FileInputStream(logFile),"GBK");
				reader = new BufferedReader(inputStreamReader);
            String line = null;
			int i = 0;
            while((line = reader.readLine()) != null){
           if(line.indexOf("Xss(WEBSERVICE CHECK)") > 0){
			    out.println("<tr>");
				String dateReg =  "[0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}[ ][0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}";
                String date = returnNeedStr2(dateReg,line);
                out.println(" <td data-toggle=\"modal\">"+date+"</td>");

                String pathReg = "path=([\\S]+)";
                String path = returnNeedStr(pathReg,line);
                out.println(" <td data-toggle=\"modal\">"+path+"</td>");

                String serverNameReg = "serverName=([\\S]+)";
                String serverName = returnNeedStr(serverNameReg,line);
                out.println(" <td data-toggle=\"modal\" >"+serverName+"</td>");

				String serverPortReg = "serverPort=([\\S]+)";
                String serverPort = returnNeedStr(serverPortReg,line);
				out.println(" <td data-toggle=\"modal\" >"+serverPort+"</td>");
				
				/*
				String xForwardedHostReg = "X-Forwarded-Host=([\\S]+)";
                String xForwardedHost = returnNeedStr(xForwardedHostReg,line);
                out.println(" <td data-toggle=\"modal\" >"+xForwardedHost+"</td>");
                */
                String userReg = "user:([\\S]+)";
                String user = returnNeedStr(userReg,line);
                out.println(" <td data-toggle=\"modal\" >"+user+"</td>");


                String SourceIPReg = "Source IP:([\\S]+)";
                String sourceIP = returnNeedStr(SourceIPReg,line);
                out.println(" <td data-toggle=\"modal\" >"+sourceIP+"</td>");
				
				ReflectMethodCall rmc = new ReflectMethodCall();	
				String city = (String)rmc.call("weaver.security.util.IPUtil",null,"getCityInfo",
		            		new Class[]{String.class}, sourceIP);
				out.println(" <td data-toggle=\"modal\">"+city+"</td>");

			    out.println("</tr>");
            }
        }
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(inputStreamReader != null){
				inputStreamReader.close();
			}
			if(reader != null){
				reader.close();
			}
		}
     %>

      </tbody>
    </table>
</div>
</body>
</html>