Skip to content

Commit

Permalink
Merge branch 'master' of github.com:PBH-BTN/PeerBanHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaojianli committed Nov 6, 2024
2 parents 41452f9 + 8b95baf commit 4fa7059
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/ghostchu/peerbanhelper/btn/BtnConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public BtnNetwork btnNetwork() {
var submit = server.getMainConfig().getBoolean("btn.submit");
var appId = server.getMainConfig().getString("btn.app-id");
var appSecret = server.getMainConfig().getString("btn.app-secret");
BtnNetwork btnNetwork = new BtnNetwork(server, scriptEngine, userAgent, configUrl, submit, appId, appSecret, matchCache);
var scriptExecute = server.getMainConfig().getBoolean("btn.allow-script-execute");
BtnNetwork btnNetwork = new BtnNetwork(server, scriptEngine, userAgent, configUrl, submit, appId, appSecret, matchCache, scriptExecute);
log.info(tlUI(Lang.BTN_NETWORK_ENABLED));
return btnNetwork;
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class BtnNetwork {
@Getter
private final Map<Class<? extends BtnAbility>, BtnAbility> abilities = new HashMap<>();
private final ScriptEngine scriptEngine;
private final boolean scriptExecute;
@Getter
private ScheduledExecutorService executeService = null;
private String configUrl;
Expand All @@ -54,7 +55,7 @@ public class BtnNetwork {
private PeerRecordDao peerRecordDao;
private ModuleMatchCache moduleMatchCache;

public BtnNetwork(PeerBanHelperServer server, ScriptEngine scriptEngine, String userAgent, String configUrl, boolean submit, String appId, String appSecret, ModuleMatchCache moduleMatchCache) {
public BtnNetwork(PeerBanHelperServer server, ScriptEngine scriptEngine, String userAgent, String configUrl, boolean submit, String appId, String appSecret, ModuleMatchCache moduleMatchCache, boolean scriptExecute) {
this.server = server;
this.scriptEngine = scriptEngine;
this.userAgent = userAgent;
Expand All @@ -63,6 +64,7 @@ public BtnNetwork(PeerBanHelperServer server, ScriptEngine scriptEngine, String
this.appId = appId.trim();
this.appSecret = appSecret.trim();
this.moduleMatchCache = moduleMatchCache;
this.scriptExecute = scriptExecute;
setupHttpClient();
resetScheduler();
checkIfNeedRetryConfig();
Expand Down Expand Up @@ -112,7 +114,7 @@ public void configBtnNetwork() {
// abilities.put(BtnAbilitySubmitRulesHitRate.class, new BtnAbilitySubmitRulesHitRate(this, ability.get("submit_hitrate").getAsJsonObject()));
// }
if (ability.has("rules")) {
abilities.put(BtnAbilityRules.class, new BtnAbilityRules(this, scriptEngine, ability.get("rules").getAsJsonObject()));
abilities.put(BtnAbilityRules.class, new BtnAbilityRules(this, scriptEngine, ability.get("rules").getAsJsonObject(), scriptExecute));
}
if (ability.has("reconfigure")) {
abilities.put(BtnAbilityReconfigure.class, new BtnAbilityReconfigure(this, ability.get("reconfigure").getAsJsonObject()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public class BtnRuleParsed {
private Map<String, List<Rule>> portRules;
private Map<String, CompiledScript> scriptRules;

public BtnRuleParsed(ScriptEngine scriptEngine, BtnRule btnRule) {
public BtnRuleParsed(ScriptEngine scriptEngine, BtnRule btnRule, boolean scriptExecute) {
this.scriptEngine = scriptEngine;
this.version = btnRule.getVersion();
this.ipRules = parseIPRule(btnRule.getIpRules());
this.portRules = parsePortRule(btnRule.getPortRules());
this.peerIdRules = parseRule(btnRule.getPeerIdRules());
this.clientNameRules = parseRule(btnRule.getClientNameRules());
this.scriptRules = compileScripts(btnRule.getScriptRules());
this.scriptRules = scriptExecute ? compileScripts(btnRule.getScriptRules()) : new HashMap<>();
}

private Map<String, CompiledScript> compileScripts(Map<String, String> scriptRules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ public class BtnAbilityRules extends AbstractBtnAbility {
private final long randomInitialDelay;
private final File btnCacheFile = new File(Main.getDataDirectory(), "btn.cache");
private final ScriptEngine scriptEngine;
private final boolean scriptExecute;
@Getter
private BtnRuleParsed btnRule;


public BtnAbilityRules(BtnNetwork btnNetwork, ScriptEngine scriptEngine, JsonObject ability) {
public BtnAbilityRules(BtnNetwork btnNetwork, ScriptEngine scriptEngine, JsonObject ability, boolean scriptExecute) {
this.btnNetwork = btnNetwork;
this.scriptEngine = scriptEngine;
this.interval = ability.get("interval").getAsLong();
this.endpoint = ability.get("endpoint").getAsString();
this.randomInitialDelay = ability.get("random_initial_delay").getAsLong();
this.scriptExecute = scriptExecute;
setLastStatus(true, new TranslationComponent(Lang.BTN_STAND_BY));
}

Expand All @@ -60,7 +62,7 @@ private void loadCacheFile() throws IOException {
} else {
try {
BtnRule btnRule = JsonUtil.getGson().fromJson(Files.readString(btnCacheFile.toPath()), BtnRule.class);
this.btnRule = new BtnRuleParsed(scriptEngine, btnRule);
this.btnRule = new BtnRuleParsed(scriptEngine, btnRule, scriptExecute);
} catch (Throwable ignored) {
}
}
Expand Down Expand Up @@ -122,7 +124,7 @@ private void updateRule() {
} else {
try {
BtnRule btr = JsonUtil.getGson().fromJson(r.body(), BtnRule.class);
this.btnRule = new BtnRuleParsed(scriptEngine, btr);
this.btnRule = new BtnRuleParsed(scriptEngine, btr, scriptExecute);
Main.getEventBus().post(new BtnRuleUpdateEvent());
try {
Files.writeString(btnCacheFile.toPath(), r.body(), StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ private void validate() {
// }
}

@UpdateScript(version = 23)
public void btnScriptExecuteSwitch() {
conf.set("btn.allow-script-execute", false);
}


@UpdateScript(version = 22)
public void miscChanges() {
conf.set("privacy", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public ProfileUpdateScript(YamlConfiguration conf) {
this.conf = conf;
}

@UpdateScript(version = 23)
public void scriptExecuteSwitch() {
conf.set("module.btn.allow-script-execute", false);
}


@UpdateScript(version = 22)
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config-version: 22
config-version: 23
# 设置程序语言
# Set the program language
# default 跟随操作系统 (Follow the operating system)
Expand Down Expand Up @@ -77,6 +77,11 @@ btn:
# The BTN instance URL, you need find a BTN instance
# By default, PBH-BTN official BTN instance will be used
config-url: "https://sparkle.ghostchu.com/ping/config"
# 是否允许 PeerBanHelper 接收来自 BTN 服务器的 Aviator 脚本
# 请仅在受信任的 BTN 服务器上启用此功能,运行来自未知来源的脚本可能会导致设备遭到攻击
# Allow PeerBanHelper to receive Aviator script from BTN server
# Enable this option only on trusted BTN server, running script from unknown source may cause your device under attack
allow-script-execute: false
# 封禁列表处理
# PBH 能够除了调用 BT 客户端的封禁 API 外,还能够进行如下操作,以便适配更多其它客户端
# Banlist invoker
Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/profile.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config-version: 23
config-version: 22
# Check interval (Timeunit: ms)
# 检查频率(单位:毫秒)
check-interval: 5000
Expand Down Expand Up @@ -271,8 +271,6 @@ module:
enabled: true
# 封禁时间,单位:毫秒,使用 default 则跟随全局设置
ban-duration: 259200000
# 是否允许执行从 BTN 服务器下发的 Aviator Script,如果您不信任 BTN 服务器,请勿开启!
allow-script-execute: false
# 多拨封禁
# Multi-dialing blocker
multi-dialing-blocker:
Expand Down

0 comments on commit 4fa7059

Please sign in to comment.