Skip to content

Commit 6ceba48

Browse files
committed
refactor: simplify generateBytes code
1 parent 637c374 commit 6ceba48

File tree

2 files changed

+38
-61
lines changed

2 files changed

+38
-61
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/MemShellGenerator.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.reajason.javaweb.memshell;
22

3-
import com.reajason.javaweb.memshell.config.*;
4-
import com.reajason.javaweb.memshell.generator.*;
5-
import com.reajason.javaweb.memshell.generator.command.CommandGenerator;
3+
import com.reajason.javaweb.memshell.config.GenerateResult;
4+
import com.reajason.javaweb.memshell.config.InjectorConfig;
5+
import com.reajason.javaweb.memshell.config.ShellConfig;
6+
import com.reajason.javaweb.memshell.config.ShellToolConfig;
7+
import com.reajason.javaweb.memshell.generator.InjectorGenerator;
68
import com.reajason.javaweb.memshell.server.AbstractShell;
79
import com.reajason.javaweb.memshell.utils.CommonUtil;
810
import org.apache.commons.lang3.StringUtils;
@@ -45,7 +47,7 @@ public static GenerateResult generate(ShellConfig shellConfig, InjectorConfig in
4547
shellToolConfig.setShellClass(shellClass);
4648
}
4749

48-
byte[] shellBytes = generateShellBytes(shellConfig, shellToolConfig);
50+
byte[] shellBytes = shellConfig.getShellTool().generateBytes(shellConfig, shellToolConfig);
4951

5052
injectorConfig.setInjectorClass(injectorClass);
5153
injectorConfig.setShellClassName(shellToolConfig.getShellClassName());
@@ -66,25 +68,4 @@ public static GenerateResult generate(ShellConfig shellConfig, InjectorConfig in
6668
.injectorInnerClassBytes(innerClassBytes)
6769
.build();
6870
}
69-
70-
private static byte[] generateShellBytes(ShellConfig shellConfig, ShellToolConfig shellToolConfig) {
71-
switch (shellConfig.getShellTool()) {
72-
case Godzilla:
73-
return new GodzillaGenerator(shellConfig, (GodzillaConfig) shellToolConfig).getBytes();
74-
case Command:
75-
return new CommandGenerator(shellConfig, (CommandConfig) shellToolConfig).getBytes();
76-
case Behinder:
77-
return new BehinderGenerator(shellConfig, (BehinderConfig) shellToolConfig).getBytes();
78-
case Suo5:
79-
return new Suo5Generator(shellConfig, ((Suo5Config) shellToolConfig)).getBytes();
80-
case AntSword:
81-
return new AntSwordGenerator(shellConfig, (AntSwordConfig) shellToolConfig).getBytes();
82-
case NeoreGeorg:
83-
return new NeoreGeorgGenerator(shellConfig, (NeoreGeorgConfig) shellToolConfig).getBytes();
84-
case Custom:
85-
return new CustomShellGenerator(shellConfig, (CustomConfig) shellToolConfig).getBytes();
86-
default:
87-
throw new UnsupportedOperationException("Unknown shell tool: " + shellConfig.getShellTool());
88-
}
89-
}
9071
}
Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
11
package com.reajason.javaweb.memshell;
22

3+
import com.reajason.javaweb.memshell.config.*;
4+
import com.reajason.javaweb.memshell.generator.*;
5+
import com.reajason.javaweb.memshell.generator.command.CommandGenerator;
6+
7+
import java.lang.reflect.Constructor;
8+
39
/**
410
* @author ReaJason
511
* @since 2024/11/22
612
*/
713
public enum ShellTool {
8-
/**
9-
* 哥斯拉
10-
*/
11-
Godzilla,
12-
13-
/**
14-
* 冰蝎
15-
*/
16-
Behinder,
17-
18-
/**
19-
* 蚁剑
20-
*/
21-
AntSword,
22-
23-
/**
24-
* 命令回显
25-
*/
26-
Command,
27-
28-
/**
29-
* Suo5 隧道代理 <a href="https://github.com/zema1/suo5">zema1/suo5</a>
30-
*/
31-
Suo5,
32-
33-
/**
34-
* Neo-reGeorg <a href="https://github.com/L-codes/Neo-reGeorg">L-codes/Neo-reGeorg</a>
35-
*/
36-
NeoreGeorg,
37-
38-
/**
39-
* 自定义
40-
*/
41-
Custom,
42-
43-
;
14+
Godzilla(GodzillaGenerator.class, GodzillaConfig.class),
15+
Command(CommandGenerator.class, CommandConfig.class),
16+
Behinder(BehinderGenerator.class, BehinderConfig.class),
17+
Suo5(Suo5Generator.class, Suo5Config.class),
18+
AntSword(AntSwordGenerator.class, AntSwordConfig.class),
19+
NeoreGeorg(NeoreGeorgGenerator.class, NeoreGeorgConfig.class),
20+
Custom(CustomShellGenerator.class, CustomConfig.class);
21+
22+
private final Class<? extends ShellGenerator> generatorClass;
23+
private final Class<? extends ShellToolConfig> configClass;
24+
25+
ShellTool(Class<? extends ShellGenerator> generatorClass, Class<? extends ShellToolConfig> configClass) {
26+
this.generatorClass = generatorClass;
27+
this.configClass = configClass;
28+
}
29+
30+
public byte[] generateBytes(ShellConfig shellConfig, ShellToolConfig shellToolConfig) {
31+
try {
32+
Constructor<? extends ShellGenerator> constructor =
33+
generatorClass.getConstructor(ShellConfig.class, configClass);
34+
ShellGenerator generator = constructor.newInstance(shellConfig, configClass.cast(shellToolConfig));
35+
return generator.getBytes();
36+
} catch (Exception e) {
37+
throw new RuntimeException("Failed to create generator for " + this, e);
38+
}
39+
}
4440
}

0 commit comments

Comments
 (0)