feat(box): 升级版本号并优化代码执行功能

-将版本号从 0.0.2 修改为 0.1.2
- 移除了异常时抛出的 RuntimeException
- 新增了 C 语言和 Java代码的执行功能
- 优化了 Python 代码的执行方式- 添加了代码编辑器的前端界面
- 新增了 QQ音乐文件解密工具的 UI 界面
- 添加了 C++ 解密库的框架
This commit is contained in:
tzdwindows 7
2025-05-24 09:36:48 +08:00
parent f632b9584d
commit e475e84851
53 changed files with 42629 additions and 9 deletions

View File

@@ -37,7 +37,7 @@ import java.util.Map;
*/
public class AxisInnovatorsBox {
private static final Logger logger = LogManager.getLogger(AxisInnovatorsBox.class);
private static final String VERSIONS = "0.0.2";
private static final String VERSIONS = "0.1.2";
private static final String[] AUTHOR = new String[]{
"tzdwindows 7"
};
@@ -71,7 +71,6 @@ public class AxisInnovatorsBox {
LibraryLoad.loadLibrary("ThrowSafely");
} catch (Exception e) {
logger.error("Failed to load the 'FridaNative' library", e);
throw new RuntimeException(e);
}
}
@@ -421,7 +420,6 @@ public class AxisInnovatorsBox {
logger.error("In unexpected errors", e);
main.ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.organizingCrashReports(e);
throw new RuntimeException(e);
}
}

View File

@@ -1,6 +1,7 @@
package com.axis.innovators.box;
import com.axis.innovators.box.browser.MainApplication;
import com.axis.innovators.box.browser.WindowRegistry;
import com.axis.innovators.box.decompilation.gui.ModernJarViewer;
import com.axis.innovators.box.tools.ArgsParser;
import com.axis.innovators.box.tools.FolderCleaner;

View File

@@ -57,6 +57,9 @@ public class CefAppManager {
try {
settings.windowless_rendering_enabled = false;
settings.javascript_flags = "--expose-gc";
settings.cache_path = FolderCreator.getLibraryFolder() + "/jcef/cache";
settings.root_cache_path = FolderCreator.getLibraryFolder() + "/jcef/cache";
settings.persist_session_cookies = true;
settings.log_severity = CefSettings.LogSeverity.LOGSEVERITY_VERBOSE;
String subprocessPath = FolderCreator.getLibraryFolder() + "/jcef/lib/win64/jcef_helper.exe";

View File

@@ -1,5 +1,6 @@
package com.axis.innovators.box.browser;
import com.axis.innovators.box.browser.util.CodeExecutor;
import com.axis.innovators.box.tools.FolderCreator;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -8,6 +9,11 @@ import org.cef.browser.CefFrame;
import org.cef.browser.CefMessageRouter;
import org.cef.callback.CefQueryCallback;
import org.cef.handler.CefMessageRouterHandlerAdapter;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Value;
import org.json.JSONObject;
import org.python.util.PythonInterpreter;
import org.tzd.lm.LM;
import javax.swing.*;
@@ -28,13 +34,14 @@ import java.util.concurrent.atomic.AtomicReference;
* 这是一个简单的示例程序用于展示如何使用JCEF来创建一个简单的浏览器窗口。
* @author tzdwindows 7
*/
public class MainApplication {
private static final ExecutorService executor = Executors.newCachedThreadPool();
private static long modelHandle;
private static long ctxHandle;
private static boolean isSystem = true;
public static void main(String[] args) {
//popupHTMLWindow();
}
@@ -79,6 +86,118 @@ public class MainApplication {
});
}
public static void popupCCodeEditorWindow() {
AtomicReference<BrowserWindow> window = new AtomicReference<>();
SwingUtilities.invokeLater(() -> {
WindowRegistry.getInstance().createNewWindow("main", builder ->
window.set(builder.title("TzdC 代码编辑器")
.icon(new ImageIcon(Objects.requireNonNull(MainApplication.class.getClassLoader().getResource("icons/logo.png"))).getImage())
.size(1487, 836)
.htmlPath(FolderCreator.getJavaScriptFolder() + "\\" + "CCodeEditor.html")
.operationHandler(createOperationHandler())
.build())
);
CefMessageRouter msgRouter = window.get().getMsgRouter();
if (msgRouter != null) {
msgRouter.addHandler(new CefMessageRouterHandlerAdapter() {
@Override
public boolean onQuery(CefBrowser browser, CefFrame frame, long queryId,
String request, boolean persistent, CefQueryCallback callback) {
try {
JSONObject requestJson = new JSONObject(request);
if ("executeCode".equals(requestJson.optString("type"))) {
String code = requestJson.optString("code");
String language = requestJson.optString("language");
// 调用代码执行逻辑
String result = CodeExecutor.executeCode(code, language,null);
JSONObject response = new JSONObject();
response.put("status", "success");
response.put("output", result);
callback.success(response.toString());
return true;
}
} catch (Exception e) {
JSONObject error = new JSONObject();
error.put("status", "error");
error.put("message", e.getMessage());
callback.failure(500, error.toString());
}
return false;
}
@Override
public void onQueryCanceled(CefBrowser browser, CefFrame frame, long queryId) {
// 处理请求取消
}
}, true);
}
});
}
public static void popupCodeEditorWindow() {
AtomicReference<BrowserWindow> window = new AtomicReference<>();
SwingUtilities.invokeLater(() -> {
WindowRegistry.getInstance().createNewWindow("main", builder ->
window.set(builder.title("代码编辑器")
.icon(new ImageIcon(Objects.requireNonNull(MainApplication.class.getClassLoader().getResource("icons/logo.png"))).getImage())
.size(1487, 836)
.htmlPath(FolderCreator.getJavaScriptFolder() + "\\" + "CodeEditor.html")
.operationHandler(createOperationHandler())
.build())
);
CefMessageRouter msgRouter = window.get().getMsgRouter();
if (msgRouter != null) {
msgRouter.addHandler(new CefMessageRouterHandlerAdapter() {
@Override
public boolean onQuery(CefBrowser browser, CefFrame frame, long queryId,
String request, boolean persistent, CefQueryCallback callback) {
try {
JSONObject requestJson = new JSONObject(request);
if ("executeCode".equals(requestJson.optString("type"))) {
String code = requestJson.optString("code");
String language = requestJson.optString("language");
// 调用代码执行逻辑
String result = executeCode(code, language);
JSONObject response = new JSONObject();
response.put("status", "success");
response.put("output", result);
callback.success(response.toString());
return true;
}
} catch (Exception e) {
JSONObject error = new JSONObject();
error.put("status", "error");
error.put("message", e.getMessage());
callback.failure(500, error.toString());
}
return false;
}
@Override
public void onQueryCanceled(CefBrowser browser, CefFrame frame, long queryId) {
// 处理请求取消
}
}, true);
}
});
}
public static String executeCode(String code, String language) {
return CodeExecutor.executeCode(code, language,new CodeExecutor.OutputListener() {
@Override
public void onOutput(String newOutput) {}
});
}
private static Value executeC(Context context, String code) {
return context.eval("c", code);
}
/**
* 弹出html预览窗口
*/
@@ -107,11 +226,11 @@ public class MainApplication {
Path filePath = Paths.get(path);
//// 验证文件存在性
//if (!Files.exists(filePath)) {
// callback.failure(404, "{\"code\":404,\"message\":\"文件未找到\"}");
// return true;
//}
// 验证文件存在性
if (!Files.exists(filePath)) {
callback.failure(404, "{\"code\":404,\"message\":\"文件未找到\"}");
return true;
}
// 读取文件内容
String content = Files.readString(filePath, StandardCharsets.UTF_8);

View File

@@ -1,5 +1,14 @@
package com.axis.innovators.box.browser;
import com.axis.innovators.box.tools.FolderCreator;
import org.cef.CefApp;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.handler.CefLoadHandlerAdapter;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.*;
import java.util.function.Consumer;
@@ -58,6 +67,8 @@ public class WindowRegistry {
config.accept(builder);
BrowserWindow window = builder.build();
registerWindow(window);
loadExtLibsPath(window);
}
/**
@@ -70,5 +81,59 @@ public class WindowRegistry {
config.accept(builder);
BrowserWindowJDialog window = builder.build();
registerChildWindow(window);
loadExtLibsPath(window);
}
private void loadExtLibsPath(BrowserWindow window) {
CefBrowser cefBrowser = window.getBrowser();
if (cefBrowser != null)
// 使用 CefClient 的调度方法(如果可用)或直接添加 LoadHandler
cefBrowser.getClient().addLoadHandler(new CefLoadHandlerAdapter() {
@Override
public void onLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode) {
if (frame.isMain()) {
try {
String extLibsPath = FolderCreator.getJavaScriptFolder() + "\\" + "extLibs";
File extLibsDir = new File(extLibsPath);
if (!extLibsDir.exists() || !extLibsDir.isDirectory()) {
throw new IOException("extLibs目录无效: " + extLibsPath);
}
String script = "window.extLibsPath = " + JSONObject.valueToString(extLibsPath) + ";";
browser.executeJavaScript(script, frame.getURL(), 0);
} catch (Exception e) {
System.err.println("注入extLibsPath失败: " + e.getMessage());
e.printStackTrace();
}
}
}
});
}
private void loadExtLibsPath(BrowserWindowJDialog window) {
CefBrowser cefBrowser = window.getBrowser();
if (cefBrowser != null)
// 使用 CefClient 的调度方法(如果可用)或直接添加 LoadHandler
cefBrowser.getClient().addLoadHandler(new CefLoadHandlerAdapter() {
@Override
public void onLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode) {
if (frame.isMain()) {
try {
String extLibsPath = FolderCreator.getJavaScriptFolder() + "\\" + "extLibs";
File extLibsDir = new File(extLibsPath);
if (!extLibsDir.exists() || !extLibsDir.isDirectory()) {
throw new IOException("extLibs目录无效: " + extLibsPath);
}
String script = "window.extLibsPath = " + JSONObject.valueToString(extLibsPath) + ";";
browser.executeJavaScript(script, frame.getURL(), 0);
} catch (Exception e) {
System.err.println("注入extLibsPath失败: " + e.getMessage());
e.printStackTrace();
}
}
}
});
}
}

View File

@@ -0,0 +1,392 @@
package com.axis.innovators.box.browser.util;
import com.axis.innovators.box.tools.LibraryLoad;
import org.jnc.DllExtractor;
import org.jnc.windows.JncNative;
import org.python.util.PythonInterpreter;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
public class CodeExecutor {
// 用于捕获输出的回调接口
public interface OutputListener {
void onOutput(String newOutput);
}
public static String executeCode(String code, String language, OutputListener listener) {
switch (language.toLowerCase()) {
case "python":
return executePythonNative(code, listener);
case "c":
case "cpp":
return executeC(code, listener);
case "java":
return executeJavaCode(code, listener);
default:
return "不支持的语言类型: " + language;
}
}
public static String executeJavaCode(String code, OutputListener listener) {
Path tempDir = null;
try {
// ===== 1. 创建临时目录 =====
tempDir = Files.createTempDirectory("javaCode");
// ===== 2. 写入Java源文件强制UTF-8=====
Path javaFile = tempDir.resolve("Main.java");
Files.writeString(javaFile,
code,
StandardCharsets.UTF_8
);
// ===== 3. 编译时指定编码 =====
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, StandardCharsets.UTF_8);
List<String> options = new ArrayList<>();
options.add("-encoding");
options.add("UTF-8");
options.add("-d");
options.add(tempDir.toString());
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
null,
options,
null,
fileManager.getJavaFileObjects(javaFile)
);
if (!task.call()) {
return "编译失败";
}
// ===== 4. 执行配置 =====
String javaExe = Path.of(System.getProperty("java.home"), "bin", "java").toString();
ProcessBuilder pb = new ProcessBuilder(
javaExe,
"-Dfile.encoding=UTF-8",
"-Dsun.stdout.encoding=UTF-8", // 针对OpenJDK的特殊设置
"-Dsun.stderr.encoding=UTF-8",
"-cp",
tempDir.toString(),
"Main"
);
// ===== 5. 设置环境变量 =====
Map<String, String> env = pb.environment();
env.put("JAVA_TOOL_OPTIONS", "-Dfile.encoding=UTF-8");
env.put("LANG", "en_US.UTF-8"); // Linux/macOS
env.put("LC_ALL", "en_US.UTF-8");
// ===== 6. 输出处理 =====
pb.redirectErrorStream(true);
Process process = pb.start();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder output = new StringBuilder();
char[] buffer = new char[4096];
int charsRead;
while ((charsRead = reader.read(buffer)) != -1) {
String chunk = new String(buffer, 0, charsRead);
output.append(chunk);
if (listener != null) {
// 处理控制台编码转换Windows专用
if (System.getProperty("os.name").startsWith("Windows")) {
chunk = new String(chunk.getBytes(StandardCharsets.UTF_8), "GBK");
}
listener.onOutput(chunk);
}
}
int exitCode = process.waitFor();
return output.toString() + "\n退出码: " + exitCode;
}
} catch (Exception e) {
return "执行错误: " + e.getMessage();
} finally {
// 清理代码...
}
}
/**
* 需要用户安装python环境
*/
private static String executePythonNative(String code, OutputListener listener) {
try {
Path pythonFile = Files.createTempFile("script_", ".py");
Files.writeString(pythonFile,
"# -*- coding: utf-8 -*-\n" + code,
StandardCharsets.UTF_8
);
ProcessBuilder pb = new ProcessBuilder("python", pythonFile.toString())
.redirectErrorStream(true);
Map<String, String> env = pb.environment();
env.put("PYTHONIOENCODING", "UTF-8");
env.put("PYTHONUTF8", "1");
Process process = pb.start();
StringBuilder output = new StringBuilder();
Thread outputThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
String finalLine = line + "\n";
output.append(finalLine);
if (listener != null) {
listener.onOutput(finalLine);
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
outputThread.start();
// 等待执行完成
int exitCode = process.waitFor();
outputThread.join();
// 清理文件
Files.deleteIfExists(pythonFile);
return String.format("退出码: %d\n输出内容:\n%s", exitCode, output);
} catch (Exception e) {
return "执行错误: " + e.getMessage();
}
}
private static String executeC(String code, OutputListener listener) {
Path tempDir = null;
Path cFile = null;
Path exeFile = null;
try {
// 创建临时工作目录
tempDir = Files.createTempDirectory("c_compile_");
// 生成C源代码文件
cFile = tempDir.resolve("program.c");
Files.writeString(cFile, code, StandardCharsets.UTF_8);
// 生成可执行文件路径
exeFile = tempDir.resolve("program.exe");
// 1. 编译代码 -------------------------------------------------
String tccPath =System.getProperty("user.dir") + "/library/tcc/tcc.exe";
Process compileProcess;
if (listener != null) {
compileProcess = new ProcessBuilder(
tccPath,
"-o", exeFile.toString(),
cFile.toString()
)
.directory(tempDir.toFile())
.redirectErrorStream(true)
.start();
// 捕获编译输出
StringBuilder compileOutput = new StringBuilder();
Thread compileOutputThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(compileProcess.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
compileOutput.append(line).append("\n");
if (listener != null) {
listener.onOutput("[编译输出] " + line + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
compileOutputThread.start();
// 等待编译完成
int compileExitCode = compileProcess.waitFor();
compileOutputThread.join(1000);
if (compileExitCode != 0) {
return "编译失败:\n" + compileOutput;
}
// 2. 执行程序 -------------------------------------------------
Process executeProcess = new ProcessBuilder(exeFile.toString())
.directory(tempDir.toFile())
.redirectErrorStream(true)
.start();
// 实时输出处理
AtomicReference<StringBuilder> execOutput = new AtomicReference<>(new StringBuilder());
Thread executeOutputThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(executeProcess.getInputStream(), StandardCharsets.UTF_8))) {
char[] buffer = new char[1024];
int charsRead;
while ((charsRead = reader.read(buffer)) != -1) {
String outputChunk = new String(buffer, 0, charsRead);
execOutput.get().append(outputChunk);
if (listener != null) {
listener.onOutput(outputChunk);
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
executeOutputThread.start();
// 等待执行完成最多10秒
boolean finished = executeProcess.waitFor(10, TimeUnit.SECONDS);
executeOutputThread.join(1000);
if (!finished) {
executeProcess.destroyForcibly();
return "执行超时\n部分输出:\n" + execOutput.get();
}
// 获取最终输出
String finalOutput = execOutput.get().toString();
int exitCode = executeProcess.exitValue();
return String.format("执行结果: %s\n退出码: %d\n输出内容:\n%s",
exitCode == 0 ? "成功" : "失败",
exitCode,
finalOutput);
} else {
new ProcessBuilder(
tccPath,
"-o", exeFile.toString(),
cFile.toString()
)
.directory(tempDir.toFile())
.redirectErrorStream(true)
.start();
new ProcessBuilder(
"cmd.exe",
"/c",
"start",
"\"Tzd输出窗口\"",
"cmd.exe",
"/K",
"chcp 65001 & ",
exeFile.toString()
).start();
return String.format("执行结果: %s\n退出码: %d\n输出内容:\n%s",
"成功",
0,
"");
}
} catch (Exception e) {
return "执行错误: " + e.getMessage();
} finally {
// 清理临时文件
try {
if (listener != null){
if (cFile != null) Files.deleteIfExists(cFile);
if (exeFile != null) Files.deleteIfExists(exeFile);
if (tempDir != null) Files.deleteIfExists(tempDir);
}
} catch (IOException e) {
System.err.println("临时文件清理失败: " + e.getMessage());
}
}
}
private static String captureProcessOutput(Process process, OutputListener listener)
throws IOException {
StringBuilder totalOutput = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
totalOutput.append(line).append("\n");
if (listener != null) {
listener.onOutput(line + "\n");
}
}
}
return totalOutput.toString();
}
// 输出监控线程
private static class OutputMonitor implements Runnable {
private final BufferedReader reader;
private final OutputListener listener;
private volatile boolean running = true;
private final StringBuilder totalOutput = new StringBuilder();
public OutputMonitor(BufferedReader reader, OutputListener listener) {
this.reader = reader;
this.listener = listener;
}
public void run() {
try {
while (running) {
if (reader.ready()) {
String line = reader.readLine();
if (line != null) {
totalOutput.append(line).append("\n");
if (listener != null) {
listener.onOutput(line + "\n");
}
}
} else {
Thread.sleep(50);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop() {
running = false;
}
public String getTotalOutput() {
return totalOutput.toString();
}
}
// 使用方法
public static void main(String[] args) {
String pythonCode = "#include <stdio.h>\n" +
"\n" +
"int main() {\n" +
" while (1){\n" +
" printf(\"Hello World\\n\");\n" +
"}\n" +
" return 0;\n" +
"}";
executeCode(pythonCode, "c", null);
}
}

View File

@@ -48,6 +48,7 @@ public class RegistrationTool {
}
}));
MainWindow.ToolCategory programmingToolsCategory = new MainWindow.ToolCategory("编程工具",
"programming/programming.png",
"编程工具");
@@ -62,6 +63,26 @@ public class RegistrationTool {
}
}));
programmingToolsCategory.addTool(new MainWindow.ToolItem("C语言编辑器", "programming/LanguageEditor/file-editing.png",
"C语言编译器智能化的idea" +
"\n作者tzdwindows 7", ++id, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// Window owner = SwingUtilities.windowForComponent((Component) e.getSource());
MainApplication.popupCCodeEditorWindow();
}
}));
programmingToolsCategory.addTool(new MainWindow.ToolItem("多语言在线执行(当遇到无限循环时会抛出错误)", "programming/LanguageEditor/file-editing.png",
"多语言在线执行,当遇到无限循环时会抛出错误" +
"\n作者tzdwindows 7", ++id, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// Window owner = SwingUtilities.windowForComponent((Component) e.getSource());
MainApplication.popupCodeEditorWindow();
}
}));
MainWindow.ToolCategory aICategory = new MainWindow.ToolCategory("AI工具",
"ai/ai.png",
"人工智能/大语言模型");