From f4a6ff6d435ec480d1d3c104be86224999c82742 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Mon, 29 Nov 2021 14:24:13 +0800 Subject: [PATCH] hide debug info by default Signed-off-by: pengzhile --- .../zhile/research/ja/netfilter/Launcher.java | 1 - .../ja/netfilter/TransformDispatcher.java | 3 +- .../ja/netfilter/commons/DebugInfo.java | 20 ++++++++++ .../ja/netfilter/filters/DNSFilter.java | 5 ++- .../ja/netfilter/filters/URLFilter.java | 3 +- .../ja/netfilter/utils/DateUtils.java | 40 +++++++++++++++++++ 6 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java create mode 100644 src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java diff --git a/src/main/java/io/zhile/research/ja/netfilter/Launcher.java b/src/main/java/io/zhile/research/ja/netfilter/Launcher.java index 6a02704..89cfdf3 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Launcher.java +++ b/src/main/java/io/zhile/research/ja/netfilter/Launcher.java @@ -20,7 +20,6 @@ public class Launcher { } try { - jarURL = new URL("file:/Users/neo/Downloads/ja-netfilter/target/ja-netfilter-jar-with-dependencies.jar"); inst.appendToBootstrapClassLoaderSearch(new JarFile(jarURL.getPath())); } catch (Throwable e) { throw new RuntimeException("Can not access ja-netfilter jar file.", e); diff --git a/src/main/java/io/zhile/research/ja/netfilter/TransformDispatcher.java b/src/main/java/io/zhile/research/ja/netfilter/TransformDispatcher.java index d6511e3..b5fb5f8 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/TransformDispatcher.java +++ b/src/main/java/io/zhile/research/ja/netfilter/TransformDispatcher.java @@ -1,5 +1,6 @@ package io.zhile.research.ja.netfilter; +import io.zhile.research.ja.netfilter.commons.DebugInfo; import io.zhile.research.ja.netfilter.transformers.HttpClientTransformer; import io.zhile.research.ja.netfilter.transformers.InetAddressTransformer; import io.zhile.research.ja.netfilter.transformers.MyTransformer; @@ -33,7 +34,7 @@ public class TransformDispatcher implements ClassFileTransformer { try { return transformer.transform(className, classFileBuffer); } catch (Exception e) { - System.out.println("=== Transform class failed: " + e.getMessage()); + DebugInfo.output("=== Transform class failed: " + e.getMessage()); } } while (false); diff --git a/src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java b/src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java new file mode 100644 index 0000000..027c99b --- /dev/null +++ b/src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java @@ -0,0 +1,20 @@ +package io.zhile.research.ja.netfilter.commons; + +import io.zhile.research.ja.netfilter.utils.DateUtils; + +public class DebugInfo { + private static final boolean DEBUG = "1".equals(System.getenv("JANF_DEBUG")); + + public static void output(String content) { // No logger lib required + if (!DEBUG) { + return; + } + + String template = "[%s] %s DEBUG : %s%n"; + + StackTraceElement[] traces = new Throwable().getStackTrace(); + String caller = traces.length < 2 ? "UNKNOWN" : traces[1].toString(); + + System.out.printf(template, DateUtils.formatNow(), caller, content); + } +} diff --git a/src/main/java/io/zhile/research/ja/netfilter/filters/DNSFilter.java b/src/main/java/io/zhile/research/ja/netfilter/filters/DNSFilter.java index 24c7c52..cda875a 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/filters/DNSFilter.java +++ b/src/main/java/io/zhile/research/ja/netfilter/filters/DNSFilter.java @@ -1,5 +1,6 @@ package io.zhile.research.ja.netfilter.filters; +import io.zhile.research.ja.netfilter.commons.DebugInfo; import io.zhile.research.ja.netfilter.enums.RuleType; import io.zhile.research.ja.netfilter.models.FilterRule; @@ -26,7 +27,7 @@ public class DNSFilter { continue; } - System.out.println("=== reject dns query: " + host + ", rule: " + rule); + DebugInfo.output("=== reject dns query: " + host + ", rule: " + rule); throw new java.net.UnknownHostException(); } @@ -43,7 +44,7 @@ public class DNSFilter { continue; } - System.out.println("=== reject dns reachable test: " + n.getHostName() + ", rule: " + rule); + DebugInfo.output("=== reject dns reachable test: " + n.getHostName() + ", rule: " + rule); return false; } diff --git a/src/main/java/io/zhile/research/ja/netfilter/filters/URLFilter.java b/src/main/java/io/zhile/research/ja/netfilter/filters/URLFilter.java index 50dd8db..7038b81 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/filters/URLFilter.java +++ b/src/main/java/io/zhile/research/ja/netfilter/filters/URLFilter.java @@ -1,5 +1,6 @@ package io.zhile.research.ja.netfilter.filters; +import io.zhile.research.ja.netfilter.commons.DebugInfo; import io.zhile.research.ja.netfilter.enums.RuleType; import io.zhile.research.ja.netfilter.models.FilterRule; @@ -27,7 +28,7 @@ public class URLFilter { continue; } - System.out.println("=== reject url: " + url + ", rule: " + rule); + DebugInfo.output("=== reject url: " + url + ", rule: " + rule); throw new SocketTimeoutException("connect timed out"); } diff --git a/src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java b/src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java new file mode 100644 index 0000000..38b71e2 --- /dev/null +++ b/src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java @@ -0,0 +1,40 @@ +package io.zhile.research.ja.netfilter.utils; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateUtils { + public static final DateFormat FULL_DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + public static final DateFormat DATE_DF = new SimpleDateFormat("yyyy-MM-dd"); + public static final DateFormat TIME_DF = new SimpleDateFormat("HH:mm:ss"); + + public static String formatDateTime(Date date) { + return FULL_DF.format(date); + } + + public static String formatDate(Date date) { + return DATE_DF.format(date); + } + + public static String formatTime(Date date) { + return TIME_DF.format(date); + } + + public static Date parseTime(String timeStr) throws ParseException { + return TIME_DF.parse(timeStr); + } + + public static Date parseDate(String dateStr) throws ParseException { + return DATE_DF.parse(dateStr); + } + + public static Date parseDateTime(String dateTimeStr) throws ParseException { + return FULL_DF.parse(dateTimeStr); + } + + public static String formatNow() { + return formatDateTime(new Date()); + } +}