Skip to content

Commit

Permalink
PowFilter: powRayId logging
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMirzayanov committed Dec 24, 2023
1 parent 8d05d14 commit ec5b30e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions code/src/main/java/org/nocturne/ddos/PowFilter.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -34,6 +35,8 @@ public class PowFilter implements Filter {

private static final List<RequestFilter> REQUEST_FILTERS = new ArrayList<>();

private static final ThreadLocal<String> rayIdLocal = new ThreadLocal<>();

@Override
public void init(FilterConfig filterConfig) {
// No operations.
Expand All @@ -44,14 +47,30 @@ 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 {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit ec5b30e

Please sign in to comment.