Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
outlaws-bai committed Jul 14, 2024
1 parent ce140b5 commit 1bbb64f
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/m2sec/core/common/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void dumpOption() {
}

public void dumpSetting() {
FileTools.writeFile(Constants.OPTION_FILE_PATH, YamlUtil.toYamlStr(option));
}


Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/m2sec/core/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ public class Constants {

public static final String JS_FILE_SUFFIX = ".js";

// swing

public static final String COMBO_BOX_DEFAULT_ITEM = "...";
// def

public static final String INPUT_JYTHON_JAR_DEF = "If you need cross platform capabilities related to Python, " +
"please enter the jython jar path. On the contrary, you will not be able to use it, but the impact on you " +
"will not be significant.";


}
32 changes: 32 additions & 0 deletions src/main/java/org/m2sec/core/common/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,37 @@ public static String generateRandomString(int length) {
return stringBuilder.toString();
}

public static String camelToSnake(String camelCase) {
if (camelCase == null || camelCase.isEmpty()) {
return camelCase;
}

StringBuilder snakeCase = new StringBuilder();
char[] charArray = camelCase.toCharArray();
boolean firstChar = true;

for (char c : charArray) {
if (Character.isUpperCase(c)) {
if (!firstChar) {
snakeCase.append('_');
}
snakeCase.append(Character.toLowerCase(c));
} else {
snakeCase.append(c);
}
firstChar = false;
}

return snakeCase.toString();
}

public static String capitalizeFirstLetter(String str) {
if (str == null || str.isEmpty()) {
return str;
}
// 将首字母大写,其他字母保持不变
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
}


}
63 changes: 38 additions & 25 deletions src/main/java/org/m2sec/core/common/ReflectTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
Expand Down Expand Up @@ -123,29 +122,43 @@ public static Object callFunc(Class<?> clazz, Object instance, String funcName,
}
}

public static String camelToSnake(String camelCase) {
if (camelCase == null || camelCase.isEmpty()) {
return camelCase;
}

StringBuilder snakeCase = new StringBuilder();
char[] charArray = camelCase.toCharArray();
boolean firstChar = true;

for (char c : charArray) {
if (Character.isUpperCase(c)) {
if (!firstChar) {
snakeCase.append('_');
}
snakeCase.append(Character.toLowerCase(c));
} else {
snakeCase.append(c);
}
firstChar = false;
}

return snakeCase.toString();
}

// public static void addJarToClassPath(String jarPath) {
// try {
// // 创建一个URL对象,指向要添加的JAR包的路径
// File jarFile = new File(jarPath);
// URL jarUrl = jarFile.toURI().toURL();
//
// // 获取系统类加载器
// ClassLoader classLoader = ClassLoader.getSystemClassLoader();
//
// // 获取ClassLoader的addURL方法
// Method addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
// addURLMethod.setAccessible(true);
//
// // 调用addURL方法,将JAR包添加到classpath
// addURLMethod.invoke(classLoader, jarUrl);
// } catch (MalformedURLException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
// throw new RuntimeException(e);
// }
// }
//
// public static void tryLoadJython(Config config) {
// String jythonPath = config.getSetting().getJythonPath();
// if (jythonPath == null || jythonPath.isBlank()) {
// String jarPath = SwingTools.showInputDialog(Constants.INPUT_JYTHON_JAR_DEF);
// if (jarPath != null && !jarPath.isBlank()) {
// addJarToClassPath(jarPath);
// config.getSetting().setJythonPath(jythonPath);
// config.dumpSetting();
// }
// } else {
// try {
// addJarToClassPath(jythonPath);
// } catch (Exception e) {
// config.getSetting().setJythonPath("");
// config.dumpSetting();
// }
// }
// }

}
10 changes: 1 addition & 9 deletions src/main/java/org/m2sec/core/common/SwingTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ public static void changeComponentStatus(Component component, boolean target) {
}


public static String capitalizeFirstLetter(String str) {
if (str == null || str.isEmpty()) {
return str;
}
// 将首字母大写,其他字母保持不变
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
}

public static void showErrorDetailDialog(Exception e) {
JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.ERROR_MESSAGE);
}
Expand All @@ -57,7 +49,7 @@ public static boolean showConfirmDialog(String message) {
return result == JOptionPane.YES_OPTION;
}

public static String showInputDialog(String message){
public static String showInputDialog(String message) {
return JOptionPane.showInputDialog(null, message);
}

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/m2sec/core/httphook/JsHookerFactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.m2sec.core.common.Constants;
import org.m2sec.core.common.FileTools;
import org.m2sec.core.common.Option;
import org.m2sec.core.common.ReflectTools;
import org.m2sec.core.common.*;
import org.m2sec.core.models.Request;
import org.m2sec.core.models.Response;
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
Expand Down Expand Up @@ -74,7 +71,7 @@ public void destroy() {

public Object safeRun(String funcName, Object arg) {
try {
return invocable.invokeFunction(ReflectTools.camelToSnake(funcName), arg);
return invocable.invokeFunction(Helper.camelToSnake(funcName), arg);
} catch (NoSuchMethodException e) {
log.warn("You have not implemented the {} method, which may lead to unknown issues", funcName);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void init() {
}

public PyFunction safeGetPyFunction(String funcName) {
PyObject object = interpreter.get(ReflectTools.camelToSnake(funcName));
PyObject object = interpreter.get(Helper.camelToSnake(funcName));
if (object == null) {
log.warn("You have not implemented the {} method, which may lead to unknown issues", funcName);
return null;
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/m2sec/panels/httphook/CodeFileHookerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,13 @@ public class CodeFileHookerPanel extends IHookerPanel<IHttpHooker> {

private String CODE_FILE_SUFFIX;

private HttpHookService service;

private JComboBox<String> codeCombo;

RSyntaxTextArea codeTextArea = new RSyntaxTextArea();

public CodeFileHookerPanel(Option option, MontoyaApi api) {
super(option, api);
}

public CodeFileHookerPanel setService(HttpHookService service) {
this.service = service;
CODE_LANGUAGE = SwingTools.capitalizeFirstLetter(service.name());
public CodeFileHookerPanel(Option option, MontoyaApi api, HttpHookService service) {
super(option, api, service);
CODE_LANGUAGE = Helper.capitalizeFirstLetter(service.name());
if (service.equals(HttpHookService.JAVA)) {
CODE_FILE_SUFFIX = Constants.JAVA_FILE_SUFFIX;
} else if (service.equals(HttpHookService.PYTHON)) {
Expand All @@ -60,10 +54,8 @@ public CodeFileHookerPanel setService(HttpHookService service) {
throw new InputMismatchException(service.name());
}
initPanel();
return this;
}


private void initPanel() {
codeCombo = new JComboBox<>();
JTextComponent.removeKeymap("RTextAreaKeymap");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/m2sec/panels/httphook/GrpcHookerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class GrpcHookerPanel extends IHookerPanel<GRpcHooker> {

private final JTextField grpcConnTextField = new JTextField(20);

public GrpcHookerPanel(Option option, MontoyaApi api) {
super(option, api);
public GrpcHookerPanel(Option option, MontoyaApi api, HttpHookService service) {
super(option, api, service);
initPanel();
}

Expand Down
29 changes: 15 additions & 14 deletions src/main/java/org/m2sec/panels/httphook/HttpHookPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import burp.api.montoya.MontoyaApi;
import lombok.extern.slf4j.Slf4j;
import org.m2sec.Galaxy;
import org.m2sec.core.common.Helper;
import org.m2sec.core.common.Option;
import org.m2sec.core.enums.HttpHookService;
import org.m2sec.core.enums.RunStatus;
Expand Down Expand Up @@ -38,18 +39,18 @@ private void initPanel() {
// 存放几种hook方式
Map<String, IHookerPanel<?>> serviceMap = new LinkedHashMap<>();

GrpcHookerPanel rpcImpl = new GrpcHookerPanel(option, api);
CodeFileHookerPanel javaFileHookerPanel = new CodeFileHookerPanel(option, api);
javaFileHookerPanel.setService(HttpHookService.JAVA).resetCodeTheme();
CodeFileHookerPanel pythonFileHookerPanel = new CodeFileHookerPanel(option, api);
pythonFileHookerPanel.setService(HttpHookService.PYTHON).resetCodeTheme();
CodeFileHookerPanel jsFileHookerPanel = new CodeFileHookerPanel(option, api);
jsFileHookerPanel.setService(HttpHookService.JS).resetCodeTheme();
GrpcHookerPanel rpcImpl = new GrpcHookerPanel(option, api, HttpHookService.GRPC);
CodeFileHookerPanel javaFileHookerPanel = new CodeFileHookerPanel(option, api,HttpHookService.JAVA);
javaFileHookerPanel.resetCodeTheme();
CodeFileHookerPanel pythonFileHookerPanel = new CodeFileHookerPanel(option, api,HttpHookService.JS);
pythonFileHookerPanel.resetCodeTheme();
CodeFileHookerPanel jsFileHookerPanel = new CodeFileHookerPanel(option, api,HttpHookService.PYTHON);
jsFileHookerPanel.resetCodeTheme();

serviceMap.put(HttpHookService.GRPC.name(), rpcImpl);
serviceMap.put(HttpHookService.JAVA.name(), javaFileHookerPanel);
serviceMap.put(HttpHookService.JS.name(), jsFileHookerPanel);
serviceMap.put(HttpHookService.PYTHON.name(), pythonFileHookerPanel);
serviceMap.put(Helper.capitalizeFirstLetter(HttpHookService.JS.name()), jsFileHookerPanel);
serviceMap.put(Helper.capitalizeFirstLetter(HttpHookService.JAVA.name()), javaFileHookerPanel);
serviceMap.put(Helper.capitalizeFirstLetter(HttpHookService.GRPC.name()), rpcImpl);
serviceMap.put(Helper.capitalizeFirstLetter(HttpHookService.PYTHON.name()), pythonFileHookerPanel);

// 创建一个容器(卡片)用于放置不同方式的JPanel
JPanel wayPanelContainer = new JPanel(new CardLayout());
Expand All @@ -71,7 +72,7 @@ private void initPanel() {
JPanel nextControlPanel = new JPanel(new BorderLayout());
nextControlPanel.setVisible(false);
JPanel switchAndCheckBoxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton switchButton = new JButton(SwingTools.capitalizeFirstLetter(RunStatus.START.name()));
JButton switchButton = new JButton(Helper.capitalizeFirstLetter(RunStatus.START.name()));
switchButton.setToolTipText("Start hook...");
JCheckBox hookRequestCheckBox = new JCheckBox("Hook request");
hookRequestCheckBox.setToolTipText("HTTP requests need to be hook?");
Expand Down Expand Up @@ -128,7 +129,7 @@ private void initPanel() {

try {
if (!isStop) {
HttpHookService service = HttpHookService.valueOf(selectItem);
HttpHookService service = hookerPanel.getService();
// 设置本次所选择的配置
option.setHookStart(true)
.setHookService(service)
Expand All @@ -147,7 +148,7 @@ private void initPanel() {
return;
}

switchButton.setText(SwingTools.capitalizeFirstLetter(text));
switchButton.setText(Helper.capitalizeFirstLetter(text));
SwingTools.changePanelStatus(hookerPanel, isStop);
SwingTools.changePanelStatus(requestCheckPanel, isStop);
SwingTools.changeComponentStatus(comboBox, isStop);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/m2sec/panels/httphook/IHookerPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.m2sec.panels.httphook;

import burp.api.montoya.MontoyaApi;
import lombok.Getter;
import org.m2sec.abilities.MasterHttpHandler;
import org.m2sec.abilities.MaterProxyHandler;
import org.m2sec.core.common.Option;
Expand All @@ -15,16 +16,18 @@
* @date: 2024/7/11 20:32
* @description:
*/

public abstract class IHookerPanel<T extends IHttpHooker> extends JPanel {

protected final Option option;

protected final MontoyaApi api;
@Getter
protected final HttpHookService service;

public IHookerPanel(Option option, MontoyaApi api) {
public IHookerPanel(Option option, MontoyaApi api, HttpHookService service) {
this.option = option;
this.api = api;
this.service = service;
}

public void start(Option option){
Expand Down

0 comments on commit 1bbb64f

Please sign in to comment.