diff --git a/code/src/main/java/org/nocturne/ddos/PowFilter.java b/code/src/main/java/org/nocturne/ddos/PowFilter.java index b161970..9f0a278 100644 --- a/code/src/main/java/org/nocturne/ddos/PowFilter.java +++ b/code/src/main/java/org/nocturne/ddos/PowFilter.java @@ -1,6 +1,7 @@ package org.nocturne.ddos; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.RandomStringUtils; import org.apache.log4j.Logger; import org.nocturne.util.StringUtil; @@ -34,6 +35,8 @@ public class PowFilter implements Filter { private static final List REQUEST_FILTERS = new ArrayList<>(); + private static final ThreadLocal rayIdLocal = new ThreadLocal<>(); + @Override public void init(FilterConfig filterConfig) { // No operations. @@ -44,6 +47,17 @@ public void destroy() { // No operations. } + private void info(String message) { + if (logging) { + message = "PowFilter: powRayId=" + rayIdLocal.get() + ": " + message; + logger.info(message); + + String print = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": " + message; + System.out.println(print); + System.err.println(print); + } + } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { @@ -51,7 +65,12 @@ public void doFilter(ServletRequest request, ServletResponse response, HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response; + String rayId = RandomStringUtils.randomAlphanumeric(8); + rayIdLocal.set(rayId); + info("Starting processing request [uri=" + httpServletRequest.getRequestURI() + + ", url=" + httpServletRequest.getRequestURL() + + ", query=" + httpServletRequest.getQueryString() + ", ip=" + getIp(httpServletRequest) + "]."); for (RequestFilter requestFilter : REQUEST_FILTERS) { @@ -76,17 +95,6 @@ public void doFilter(ServletRequest request, ServletResponse response, } } - private void info(String message) { - if (logging) { - message = "PowFilter: " + message; - logger.info(message); - - String print = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": " + message; - System.out.println(print); - System.err.println(print); - } - } - private static String getIp(HttpServletRequest httpRequest) { String ip = httpRequest.getHeader(X_REAL_IP); if (StringUtil.isNotEmpty(ip)) {