Files
HaE/src/main/java/hae/utils/ConfigLoader.java

253 lines
8.3 KiB
Java
Raw Normal View History

2024-05-24 15:00:49 +08:00
package hae.utils;
2024-05-06 12:56:56 +08:00
import burp.api.montoya.MontoyaApi;
import hae.Config;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
2024-05-12 19:02:38 +08:00
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
2024-05-23 12:00:13 +08:00
import java.nio.file.Path;
2024-05-12 19:02:38 +08:00
import java.nio.file.Paths;
import java.util.*;
2024-05-06 12:56:56 +08:00
public class ConfigLoader {
private final MontoyaApi api;
private final Yaml yaml;
private final String configFilePath;
private final String rulesFilePath;
public ConfigLoader(MontoyaApi api) {
this.api = api;
DumperOptions dop = new DumperOptions();
dop.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Representer representer = new Representer(dop);
this.yaml = new Yaml(representer, dop);
String configPath = determineConfigPath();
2024-05-12 19:02:38 +08:00
this.configFilePath = String.format("%s/%s", configPath, "Config.yml");
2024-05-06 12:56:56 +08:00
this.rulesFilePath = String.format("%s/%s", configPath, "Rules.yml");
// 构造函数,初始化配置
File HaEConfigPathFile = new File(configPath);
if (!(HaEConfigPathFile.exists() && HaEConfigPathFile.isDirectory())) {
HaEConfigPathFile.mkdirs();
}
File configFilePath = new File(this.configFilePath);
if (!(configFilePath.exists() && configFilePath.isFile())) {
initConfig();
}
File rulesFilePath = new File(this.rulesFilePath);
if (!(rulesFilePath.exists() && rulesFilePath.isFile())) {
2024-12-21 15:34:45 +08:00
initRules();
2024-05-06 12:56:56 +08:00
}
Config.globalRules = getRules();
}
2025-03-21 21:22:11 +08:00
private static boolean isValidConfigPath(String configPath) {
File configPathFile = new File(configPath);
return configPathFile.exists() && configPathFile.isDirectory();
}
2024-05-06 12:56:56 +08:00
private String determineConfigPath() {
// 优先级1用户根目录
String userConfigPath = String.format("%s/.config/HaE", System.getProperty("user.home"));
if (isValidConfigPath(userConfigPath)) {
return userConfigPath;
}
// 优先级2Jar包所在目录
String jarPath = api.extension().filename();
String jarDirectory = new File(jarPath).getParent();
String jarConfigPath = String.format("%s/.config/HaE", jarDirectory);
if (isValidConfigPath(jarConfigPath)) {
return jarConfigPath;
}
return userConfigPath;
}
public void initConfig() {
Map<String, Object> r = new LinkedHashMap<>();
2024-08-23 22:03:31 +08:00
r.put("ExcludeSuffix", getExcludeSuffix());
r.put("BlockHost", getBlockHost());
r.put("ExcludeStatus", getExcludeStatus());
2024-09-19 17:08:46 +08:00
r.put("LimitSize", getLimitSize());
2024-08-23 22:03:31 +08:00
r.put("HaEScope", getScope());
2024-05-06 12:56:56 +08:00
try {
Writer ws = new OutputStreamWriter(Files.newOutputStream(Paths.get(configFilePath)), StandardCharsets.UTF_8);
yaml.dump(r, ws);
ws.close();
} catch (Exception ignored) {
}
}
public String getRulesFilePath() {
return rulesFilePath;
}
// 获取规则配置
public Map<String, Object[][]> getRules() {
Map<String, Object[][]> rules = new HashMap<>();
try {
InputStream inputStream = Files.newInputStream(Paths.get(getRulesFilePath()));
DumperOptions dop = new DumperOptions();
dop.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Representer representer = new Representer(dop);
Map<String, Object> rulesMap = new Yaml(representer, dop).load(inputStream);
Object rulesObj = rulesMap.get("rules");
if (rulesObj instanceof List) {
List<Map<String, Object>> groupData = (List<Map<String, Object>>) rulesObj;
for (Map<String, Object> groupFields : groupData) {
ArrayList<Object[]> data = new ArrayList<>();
Object ruleObj = groupFields.get("rule");
if (ruleObj instanceof List) {
List<Map<String, Object>> ruleData = (List<Map<String, Object>>) ruleObj;
for (Map<String, Object> ruleFields : ruleData) {
2025-03-21 21:22:11 +08:00
Object[] valuesArray = new Object[Config.ruleFields.length];
for (int i = 0; i < Config.ruleFields.length; i++) {
valuesArray[i] = ruleFields.get(Config.ruleFields[i].toLowerCase().replace("-", "_"));
2024-05-06 12:56:56 +08:00
}
data.add(valuesArray);
}
}
Object[][] dataArray = data.toArray(new Object[data.size()][]);
rules.put(groupFields.get("group").toString(), dataArray);
}
}
return rules;
2024-05-12 19:02:38 +08:00
} catch (Exception ignored) {
2024-05-06 12:56:56 +08:00
}
return rules;
}
2024-07-23 09:21:30 +08:00
public String getBlockHost() {
2024-08-12 10:34:26 +08:00
return getValueFromConfig("BlockHost", Config.host);
2024-05-23 12:00:13 +08:00
}
2025-03-21 21:22:11 +08:00
public void setBlockHost(String blockHost) {
setValueToConfig("BlockHost", blockHost);
}
2024-05-23 12:00:13 +08:00
public String getExcludeSuffix() {
2024-08-12 10:34:26 +08:00
return getValueFromConfig("ExcludeSuffix", Config.suffix);
}
2025-03-21 21:22:11 +08:00
public void setExcludeSuffix(String excludeSuffix) {
setValueToConfig("ExcludeSuffix", excludeSuffix);
}
2024-08-12 10:34:26 +08:00
public String getExcludeStatus() {
return getValueFromConfig("ExcludeStatus", Config.status);
2024-07-23 09:21:30 +08:00
}
2025-03-21 21:22:11 +08:00
public void setExcludeStatus(String status) {
setValueToConfig("ExcludeStatus", status);
}
2024-09-19 17:08:46 +08:00
public String getLimitSize() {
return getValueFromConfig("LimitSize", Config.size);
}
2025-03-21 21:22:11 +08:00
public void setLimitSize(String size) {
setValueToConfig("LimitSize", size);
}
2024-07-23 09:21:30 +08:00
public String getScope() {
return getValueFromConfig("HaEScope", Config.scopeOptions);
}
2025-03-21 21:22:11 +08:00
public void setScope(String scope) {
setValueToConfig("HaEScope", scope);
}
2024-11-16 18:06:49 +08:00
public boolean getMode() {
return getValueFromConfig("HaEModeStatus", Config.modeStatus).equals("true");
}
2025-03-21 21:22:11 +08:00
public void setMode(String mode) {
setValueToConfig("HaEModeStatus", mode);
}
2024-08-23 22:03:31 +08:00
private String getValueFromConfig(String name, String defaultValue) {
2024-05-23 12:00:13 +08:00
File yamlSetting = new File(configFilePath);
if (!yamlSetting.exists() || !yamlSetting.isFile()) {
2024-08-23 22:03:31 +08:00
return defaultValue;
2024-05-23 12:00:13 +08:00
}
try (InputStream inorder = Files.newInputStream(Paths.get(configFilePath))) {
Map<String, Object> r = new Yaml().load(inorder);
2024-07-23 09:21:30 +08:00
if (r.containsKey(name)) {
return r.get(name).toString();
2024-05-23 12:00:13 +08:00
}
2024-05-06 12:56:56 +08:00
} catch (Exception ignored) {
}
2024-05-23 12:00:13 +08:00
2024-08-23 22:03:31 +08:00
return defaultValue;
2024-05-23 12:00:13 +08:00
}
2024-07-23 09:21:30 +08:00
private void setValueToConfig(String name, String value) {
2024-05-23 12:00:13 +08:00
Map<String, Object> currentConfig = loadCurrentConfig();
2024-07-23 09:21:30 +08:00
currentConfig.put(name, value);
2024-05-23 12:00:13 +08:00
try (Writer ws = new OutputStreamWriter(Files.newOutputStream(Paths.get(configFilePath)), StandardCharsets.UTF_8)) {
yaml.dump(currentConfig, ws);
2024-05-24 15:00:49 +08:00
} catch (Exception ignored) {
2024-05-23 12:00:13 +08:00
}
}
2024-07-23 09:21:30 +08:00
private Map<String, Object> loadCurrentConfig() {
Path path = Paths.get(configFilePath);
if (!Files.exists(path)) {
return new LinkedHashMap<>(); // 返回空的Map表示没有当前配置
}
2024-05-23 12:00:13 +08:00
2024-07-23 09:21:30 +08:00
try (InputStream in = Files.newInputStream(path)) {
return yaml.load(in);
} catch (Exception e) {
return new LinkedHashMap<>(); // 读取失败时也返回空的Map
2024-05-23 12:00:13 +08:00
}
}
2024-12-21 15:34:45 +08:00
public boolean initRules() {
boolean ret = copyRulesToFile(this.rulesFilePath);
if (!ret) {
2024-05-23 12:00:13 +08:00
api.extension().unload();
}
2024-12-21 15:34:45 +08:00
return ret;
2024-05-23 12:00:13 +08:00
}
private boolean copyRulesToFile(String targetFilePath) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("rules/Rules.yml");
File targetFile = new File(targetFilePath);
try (inputStream; OutputStream outputStream = new FileOutputStream(targetFile)) {
if (inputStream != null) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
return true;
}
2024-05-24 15:00:49 +08:00
} catch (Exception ignored) {
2024-05-23 12:00:13 +08:00
}
return false;
2024-05-06 12:56:56 +08:00
}
}