feat: support plugin update zip scan and .vmoptions read interception

This commit is contained in:
zpj80231
2026-06-08 16:54:22 +08:00
parent cae96dd654
commit 88cbdac735
13 changed files with 646 additions and 236 deletions

View File

@@ -1,19 +1,56 @@
package com.novitechie.rules;
import com.janetfilter.core.commons.DebugInfo;
import com.janetfilter.core.models.FilterRule;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
public class StackTraceRule {
private static final Pattern PACKAGE_NAME_PATTERN = Pattern.compile("\\A\\p{ASCII}*\\z");
private static List<FilterRule> traceCheckPackageRules = Collections.emptyList();
public static void initRules(List<FilterRule> traceCheckPackageRules) {
StackTraceRule.traceCheckPackageRules = traceCheckPackageRules == null
? Collections.emptyList()
: traceCheckPackageRules;
}
public static boolean check() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if (!PACKAGE_NAME_PATTERN.matcher(stackTraceElement.getMethodName()).matches()) {
if (isSuspiciousFrame(stackTraceElement)) {
return true;
}
}
return false;
}
static boolean isSuspiciousFrame(StackTraceElement stackTraceElement) {
String methodName = stackTraceElement.getMethodName();
if (!PACKAGE_NAME_PATTERN.matcher(methodName).matches()) {
return true;
}
if (methodName.length() <= 1) {
String className = stackTraceElement.getClassName();
if (checkTracePackage(className)) {
// DebugInfo.info("short method frame, className: " + className + ", methodName: " + methodName);
return true;
}
}
return false;
}
private static boolean checkTracePackage(String className) {
if (className == null) {
return false;
}
for (FilterRule rule : traceCheckPackageRules) {
if (rule != null && rule.test(className)) {
return true;
}
}

View File

@@ -4,6 +4,7 @@ import com.janetfilter.core.commons.DebugInfo;
import com.janetfilter.core.models.FilterRule;
import com.novitechie.LogUtil;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
@@ -12,6 +13,8 @@ import java.util.List;
*/
public class SystemRule {
private static final String JB_VM_OPTIONS_FILE = "jb.vmOptionsFile";
private static List<FilterRule> hideEnvRules = Collections.emptyList();
private static List<FilterRule> hidePropertyRules = Collections.emptyList();
@@ -31,7 +34,7 @@ public class SystemRule {
}
public static boolean checkProperty(String name) {
if (checkHideProperty(name) && StackTraceRule.check()) {
if ((checkVMOptionsProperty(name) || checkHideProperty(name)) && StackTraceRule.check()) {
DebugInfo.output("======================Hide Property: " + name);
LogUtil.printStackTrace();
return true;
@@ -39,6 +42,14 @@ public class SystemRule {
return false;
}
public static String hookProperty(String name) {
if (checkVMOptionsProperty(name)) {
Path path = VMOptionsRule.hook();
return path == null ? null : path.toString();
}
return null;
}
private static boolean checkHideEnv(String name) {
return hideEnvRules.stream().anyMatch(rule -> rule.test(name));
}
@@ -46,4 +57,8 @@ public class SystemRule {
private static boolean checkHideProperty(String name) {
return hidePropertyRules.stream().anyMatch(rule -> rule.test(name));
}
private static boolean checkVMOptionsProperty(String name) {
return JB_VM_OPTIONS_FILE.equals(name);
}
}

View File

@@ -0,0 +1,79 @@
package com.novitechie.rules;
import com.janetfilter.core.commons.DebugInfo;
import java.io.*;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class VMOptionsReadRule {
private static final String VMOPTIONS_SUFFIX = ".vmoptions";
public static boolean shouldHide(Path path) {
return isVMOptions(path) && StackTraceRule.check();
}
public static File redirectFile(File file) {
if (isVMOptions(file) && StackTraceRule.check()) {
return hookFile(file);
}
return file;
}
public static String redirectPath(String path) {
if (isVMOptions(path) && StackTraceRule.check()) {
return hookFile(new File(path)).getPath();
}
return path;
}
public static byte[] emptyBytes(Path path) {
log(path);
return new byte[0];
}
public static String emptyString(Path path) {
log(path);
return "";
}
public static List<String> emptyLines(Path path) {
log(path);
return Collections.emptyList();
}
public static InputStream emptyInputStream(Path path) {
log(path);
return new ByteArrayInputStream(new byte[0]);
}
public static BufferedReader emptyBufferedReader(Path path) {
log(path);
return new BufferedReader(new StringReader(""));
}
private static boolean isVMOptions(Path path) {
return path != null && isVMOptions(path.toString());
}
private static boolean isVMOptions(File file) {
return file != null && isVMOptions(file.getPath());
}
private static boolean isVMOptions(String path) {
return path != null && path.toLowerCase(Locale.ROOT).endsWith(VMOPTIONS_SUFFIX);
}
private static File hookFile(File fallback) {
Path path = VMOptionsRule.hook();
return path == null ? fallback : path.toFile();
}
private static void log(Path path) {
DebugInfo.output("======================Hide VMOptions read: " + path);
}
}