Skip to content

Commit

Permalink
Merge pull request #688 from PBH-BTN/dev
Browse files Browse the repository at this point in the history
v7.1 Freezing
  • Loading branch information
Ghost-chu authored Nov 6, 2024
2 parents a1167ac + 2f39fca commit 2384c3b
Show file tree
Hide file tree
Showing 73 changed files with 9,239 additions and 1,170 deletions.
9 changes: 6 additions & 3 deletions install4j/project.install4j
Original file line number Diff line number Diff line change
Expand Up @@ -663,15 +663,18 @@ return console.askYesNo(message, true);
</styles>
</installerGui>
<mediaSets>
<windows name="Windows x64" id="38" mediaFileName="${compiler:sys.shortName}_${compiler:sys.jreBundleArch}_${compiler:sys.version}" architecture="64">
<windows name="Windows x64" id="38" mediaFileName="${compiler:sys.shortName}_${compiler:sys.platform}_${compiler:sys.version}" architecture="64">
<jreBundle installOnlyIfNecessary="true" />
</windows>
<windows name="Windows x64 NoJava" id="109" mediaFileName="${compiler:sys.shortName}_windows_nojava_${compiler:sys.version}" architecture="64">
<windows name="Windows x64 NoJava" id="109" mediaFileName="${compiler:sys.shortName}_${compiler:sys.platform}_${compiler:sys.version}_nojava" architecture="64">
<jreBundle jreBundleSource="none" />
</windows>
<windows name="Windows arm64" id="141" mediaFileName="${compiler:sys.shortName}_${compiler:sys.jreBundleArch}_${compiler:sys.version}" architecture="arm64">
<windows name="Windows arm64" id="141" mediaFileName="${compiler:sys.shortName}_${compiler:sys.platform}_${compiler:sys.version}" architecture="arm64">
<jreBundle installOnlyIfNecessary="true" />
</windows>
<windows name="Windows arm64 NoJava" id="112" mediaFileName="${compiler:sys.shortName}_${compiler:sys.platform}_${compiler:sys.version}_nojava" architecture="arm64">
<jreBundle jreBundleSource="none" />
</windows>
<macosFolder name="macOS Folder" id="98" mediaFileName="${compiler:sys.shortName}_macos_${compiler:sys.jreBundleArch}_${compiler:sys.version}" architecture="universal" />
<macosFolder name="macOS Folder NoJava" id="120" mediaFileName="${compiler:sys.shortName}_macos_nojava_${compiler:sys.version}" architecture="universal">
<jreBundle jreBundleSource="none" />
Expand Down
2 changes: 1 addition & 1 deletion pkg/deb/usr/lib/systemd/system/peerbanhelper.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ User=peerbanhelper
WorkingDirectory=/etc/peerbanhelper
ExecStart=/usr/bin/java -Dpbh.release=debian -Dpbh.datadir=/etc/peerbanhelper -Xmx512M -XX:+UseG1GC -XX:+UseStringDeduplication -XX:+ShrinkHeapInSteps -jar /usr/lib/peerbanhelper/PeerBanHelper.jar
Restart=on-failure
StandardOutput=null
[Install]
WantedBy=multi-user.target

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.ghostchu.peerbanhelper</groupId>
<artifactId>peerbanhelper</artifactId>
<version>7.0.2</version>
<version>7.1.0</version>
<packaging>jar</packaging>

<name>PeerBanHelper</name>
Expand Down Expand Up @@ -310,7 +310,7 @@
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>6.0.0</version>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>com.github.mizosoft.methanol</groupId>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
Expand All @@ -22,6 +23,7 @@
import org.bspfsystems.yamlconfiguration.configuration.InvalidConfigurationException;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
Expand Down Expand Up @@ -148,6 +150,15 @@ public static void setupLogback() {

appender.start(); // 启动 appender
}

try{
var targetLevel = System.getProperty("pbh.log.level");
if(targetLevel != null) {
var rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) rootLogger;
logbackLogger.setLevel(Level.toLevel(targetLevel));
}
}catch (Throwable ignored){}
}

public static ReloadResult reloadModule() {
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.ghostchu.peerbanhelper.event.PBHServerStartedEvent;
import com.ghostchu.peerbanhelper.event.PeerBanEvent;
import com.ghostchu.peerbanhelper.event.PeerUnbanEvent;
import com.ghostchu.peerbanhelper.exchange.ExchangeMap;
import com.ghostchu.peerbanhelper.invoker.BanListInvoker;
import com.ghostchu.peerbanhelper.invoker.impl.CommandExec;
import com.ghostchu.peerbanhelper.invoker.impl.IPFilterInvoker;
Expand All @@ -28,11 +29,17 @@
import com.ghostchu.peerbanhelper.module.impl.webapi.*;
import com.ghostchu.peerbanhelper.peer.Peer;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.text.TextManager;
import com.ghostchu.peerbanhelper.text.TranslationComponent;
import com.ghostchu.peerbanhelper.torrent.Torrent;
import com.ghostchu.peerbanhelper.util.*;
import com.ghostchu.peerbanhelper.util.encrypt.ActivationKeyUtil;
import com.ghostchu.peerbanhelper.util.encrypt.RSAUtils;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.util.paging.Pageable;
import com.ghostchu.peerbanhelper.util.rule.ModuleMatchCache;
import com.ghostchu.peerbanhelper.util.time.ExceptedTime;
import com.ghostchu.peerbanhelper.util.time.InfoHashUtil;
import com.ghostchu.peerbanhelper.util.time.TimeoutProtect;
import com.ghostchu.peerbanhelper.web.JavalinWebContainer;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
Expand All @@ -44,10 +51,15 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.gson.JsonObject;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.EvalMode;
import com.googlecode.aviator.Options;
import com.googlecode.aviator.runtime.JavaMethodReflectionFunctionMissing;
import inet.ipaddr.IPAddress;
import io.javalin.util.JavalinBindException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
import org.bspfsystems.yamlconfiguration.configuration.MemoryConfiguration;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
Expand All @@ -62,6 +74,7 @@
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.math.MathContext;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.*;
Expand Down Expand Up @@ -171,6 +184,7 @@ public void start() throws SQLException {
log.info(tlUI(Lang.MOTD, Main.getMeta().getVersion()));
loadDownloaders();
registerBanListInvokers();
setupScriptEngine();
registerModules();
registerHttpServer();
setupIPDB();
Expand All @@ -190,6 +204,57 @@ public void start() throws SQLException {

}

private void setupScriptEngine() {
AviatorEvaluator.getInstance().setCachedExpressionByDefault(true);
// ASM 性能优先
AviatorEvaluator.getInstance().setOption(Options.EVAL_MODE, EvalMode.ASM);
// EVAL 性能优先
AviatorEvaluator.getInstance().setOption(Options.OPTIMIZE_LEVEL, AviatorEvaluator.EVAL);
// 降低浮点计算精度
AviatorEvaluator.getInstance().setOption(Options.MATH_CONTEXT, MathContext.DECIMAL32);
// 启用变量语法糖
AviatorEvaluator.getInstance().setOption(Options.ENABLE_PROPERTY_SYNTAX_SUGAR, true);
// // 表达式允许序列化和反序列化
// AviatorEvaluator.getInstance().setOption(Options.SERIALIZABLE, true);
// 启用反射方法查找
AviatorEvaluator.getInstance().setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
// 注册反射调用
registerFunctions(IPAddressUtil.class);
registerFunctions(HTTPUtil.class);
registerFunctions(JsonUtil.class);
registerFunctions(Lang.class);
registerFunctions(StrUtil.class);
registerFunctions(PeerBanHelperServer.class);
registerFunctions(InfoHashUtil.class);
registerFunctions(CommonUtil.class);
registerFunctions(ByteUtil.class);
registerFunctions(MiscUtil.class);
registerFunctions(MsgUtil.class);
registerFunctions(SharedObject.class);
registerFunctions(UrlEncoderDecoder.class);
registerFunctions(URLUtil.class);
registerFunctions(WebUtil.class);
registerFunctions(RSAUtils.class);
registerFunctions(ActivationKeyUtil.class);
registerFunctions(Pageable.class);
registerFunctions(TextManager.class);
registerFunctions(ExchangeMap.class);
registerFunctions(Main.class);
}

private void registerFunctions(Class<?> clazz) {
try {
AviatorEvaluator.addInstanceFunctions(StringUtils.uncapitalize(clazz.getSimpleName()), clazz);
} catch (IllegalAccessException | NoSuchMethodException e) {
log.error("Internal error: failed on register instance functions: {}", clazz.getName(), e);
}
try {
AviatorEvaluator.addStaticFunctions(StringUtils.capitalize(clazz.getSimpleName()), clazz);
} catch (IllegalAccessException | NoSuchMethodException e) {
log.error("Internal error: failed on register static functions: {}", clazz.getName(), e);
}
}

private void sendSnapshotAlert() {
if (Main.getMeta().isSnapshotOrBeta()) {
alertManager.publishAlert(false, AlertLevel.INFO, "unstable-alert", new TranslationComponent(Lang.ALERT_SNAPSHOT), new TranslationComponent(Lang.ALERT_SNAPSHOT_DESCRIPTION));
Expand Down Expand Up @@ -564,6 +629,7 @@ public void banWave() {
banWaveWatchDog.feed();
metrics.recordCheck();
banWaveLock.unlock();
System.gc();
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ghostchu.peerbanhelper.btn;

import com.ghostchu.peerbanhelper.PeerBanHelperServer;
import com.ghostchu.peerbanhelper.scriptengine.ScriptEngine;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.util.rule.ModuleMatchCache;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -22,7 +23,8 @@ public class BtnConfig {
private String userAgent;
@Autowired
private ModuleMatchCache matchCache;

@Autowired
private ScriptEngine scriptEngine;
@Bean
public BtnNetwork btnNetwork() {
ConfigurationSection section = server.getMainConfig().getConfigurationSection("btn");
Expand All @@ -32,7 +34,7 @@ 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, userAgent, configUrl, submit, appId, appSecret, matchCache);
BtnNetwork btnNetwork = new BtnNetwork(server, scriptEngine, userAgent, configUrl, submit, appId, appSecret, matchCache);
log.info(tlUI(Lang.BTN_NETWORK_ENABLED));
return btnNetwork;
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ghostchu.peerbanhelper.PeerBanHelperServer;
import com.ghostchu.peerbanhelper.btn.ability.*;
import com.ghostchu.peerbanhelper.database.dao.impl.PeerRecordDao;
import com.ghostchu.peerbanhelper.scriptengine.ScriptEngine;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.rule.ModuleMatchCache;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class BtnNetwork {
private static final int PBH_BTN_PROTOCOL_IMPL_VERSION = 8;
@Getter
private final Map<Class<? extends BtnAbility>, BtnAbility> abilities = new HashMap<>();
private final ScriptEngine scriptEngine;
@Getter
private ScheduledExecutorService executeService = null;
private String configUrl;
Expand All @@ -52,8 +54,9 @@ public class BtnNetwork {
private PeerRecordDao peerRecordDao;
private ModuleMatchCache moduleMatchCache;

public BtnNetwork(PeerBanHelperServer server, 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) {
this.server = server;
this.scriptEngine = scriptEngine;
this.userAgent = userAgent;
this.configUrl = configUrl;
this.submit = submit;
Expand Down Expand Up @@ -109,7 +112,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, ability.get("rules").getAsJsonObject()));
abilities.put(BtnAbilityRules.class, new BtnAbilityRules(this, scriptEngine, ability.get("rules").getAsJsonObject()));
}
if (ability.has("reconfigure")) {
abilities.put(BtnAbilityReconfigure.class, new BtnAbilityReconfigure(this, ability.get("reconfigure").getAsJsonObject()));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public class BtnRule {
private Map<String, List<String>> ipRules;
@SerializedName("port")
private Map<String, List<Integer>> portRules;
@SerializedName("script")
private Map<String, String> scriptRules;
}
48 changes: 43 additions & 5 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ghostchu.peerbanhelper.btn;

import com.ghostchu.peerbanhelper.scriptengine.CompiledScript;
import com.ghostchu.peerbanhelper.scriptengine.ScriptEngine;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.text.TranslationComponent;
import com.ghostchu.peerbanhelper.util.IPAddressUtil;
Expand All @@ -10,27 +12,53 @@
import com.ghostchu.peerbanhelper.util.rule.matcher.IPMatcher;
import inet.ipaddr.IPAddress;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ghostchu.peerbanhelper.text.TextManager.tlUI;

@Data
@Slf4j
public class BtnRuleParsed {
private final ScriptEngine scriptEngine;
private String version;
private Map<String, List<Rule>> peerIdRules;
private Map<String, List<Rule>> clientNameRules;
private Map<String, List<Rule>> ipRules;
private Map<String, IPMatcher> ipRules;
private Map<String, List<Rule>> portRules;
private Map<String, CompiledScript> scriptRules;

public BtnRuleParsed(BtnRule btnRule) {
public BtnRuleParsed(ScriptEngine scriptEngine, BtnRule btnRule) {
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());
}

private Map<String, CompiledScript> compileScripts(Map<String, String> scriptRules) {
Map<String, CompiledScript> scripts = new HashMap<>();
log.info(tlUI(Lang.BTN_RULES_SCRIPT_COMPILING, scriptRules.size()));
long startAt = System.currentTimeMillis();
scriptRules.forEach((name, content) -> {
try {
var script = scriptEngine.compileScript(null, name, content);
if (script != null) {
scripts.put(name, script);
}
} catch (Exception e) {
log.error("Unable to load BTN script {}", name, e);
}
});
log.info(tlUI(Lang.BTN_RULES_SCRIPT_COMPILED, scripts.size(), System.currentTimeMillis() - startAt));
return scripts;
}

private Map<String, List<Rule>> parsePortRule(Map<String, List<Integer>> portRules) {
Expand Down Expand Up @@ -66,9 +94,10 @@ public String matcherIdentifier() {
return rules;
}

public Map<String, List<Rule>> parseIPRule(Map<String, List<String>> raw) {
Map<String, List<Rule>> rules = new HashMap<>();
raw.forEach((k, v) -> rules.put(k, List.of(new BtnRuleIpMatcher(version, k, k, v.stream().map(IPAddressUtil::getIPAddress).toList()))));

public Map<String, IPMatcher> parseIPRule(Map<String, List<String>> raw) {
Map<String, IPMatcher> rules = new HashMap<>();
raw.forEach((k, v) -> rules.put(k,new IPMatcher(version, k, v.stream().map(IPAddressUtil::getIPAddress).toList())));
return rules;
}

Expand All @@ -78,6 +107,15 @@ public Map<String, List<Rule>> parseRule(Map<String, List<String>> raw) {
return rules;
}

public long size(){
// check all categories and all value's collections's size
return peerIdRules.values().stream().mapToLong(List::size).sum() +
clientNameRules.values().stream().mapToLong(List::size).sum() +
ipRules.values().stream().mapToLong(IPMatcher::size).sum() +
portRules.values().stream().mapToLong(List::size).sum() +
scriptRules.size();
}

public static class BtnRuleIpMatcher extends IPMatcher {

private final String version;
Expand Down
Loading

0 comments on commit 2384c3b

Please sign in to comment.