Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #8

Merged
merged 10 commits into from
Dec 31, 2021
32 changes: 32 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Java CI with Maven

on:
push:
pull_request:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
java: [ 17 ]

steps:
- uses: actions/checkout@v2

- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v2
with:
java-version: ${{ matrix.java }}
distribution: 'adopt'
cache: maven

- name: Build with Maven
run: mvn -B package --file pom.xml

- uses: actions/upload-artifact@v2
with:
name: mymap-with-dependencies.jar
path: target/mymap-v*-jar-with-dependencies.jar
10 changes: 7 additions & 3 deletions src/main/java/com/zfkun/plugins/mymap/MapTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.janetfilter.core.models.FilterRule;
import com.janetfilter.core.plugin.MyTransformer;

import java.util.List;

import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.tree.ClassNode;
Expand All @@ -26,23 +28,25 @@ public String getHookClassName() {

public byte[] transform(String className, byte[] classBytes, int order) throws Exception {
PutFilter.setRules(this.rules);

ClassReader reader = new ClassReader(classBytes);
ClassNode node = new ClassNode(ASM5);
reader.accept(node, 0);

for (MethodNode mn : node.methods) {
if ("put".equals(mn.name) && "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(mn.desc)) {
for (MethodNode methodNode : node.methods) {
if (methodNode.name.equals("put") && methodNode.desc.equals("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")) {
InsnList list = new InsnList();
list.add(new VarInsnNode(ALOAD, 1));
list.add(new VarInsnNode(ALOAD, 2));
list.add(new MethodInsnNode(INVOKESTATIC, "com/zfkun/plugins/mymap/PutFilter", "testPut", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false));
list.add(new VarInsnNode(ASTORE, 2));
mn.instructions.insert(list);
methodNode.instructions.insert(list);
}
}

ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
node.accept(writer);

return writer.toByteArray();
}
}
9 changes: 4 additions & 5 deletions src/main/java/com/zfkun/plugins/mymap/PutFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
import com.janetfilter.core.commons.DebugInfo;
import com.janetfilter.core.enums.RuleType;
import com.janetfilter.core.models.FilterRule;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PutFilter {
private static Map<Object, Object> map;
private static final Map<Object, Object> map = new HashMap<>();

public static void setRules(List<FilterRule> rules) {
map = new HashMap();

for (FilterRule rule : rules) {
if (rule.getType() == RuleType.EQUAL) {
String[] sections = rule.getRule().split("->", 2);
if (2 != sections.length) {
if (sections.length != 2) {
DebugInfo.output("Invalid record: " + rule + ", skipped.");
} else {
map.put(sections[0], sections[1]);
Expand All @@ -26,7 +25,7 @@ public static void setRules(List<FilterRule> rules) {
}

public static Object testPut(Object k, Object v) {
if (null == k) return v;
if (k == null) return v;
return map.getOrDefault(k, v);
}
}