Skip to content

Commit

Permalink
Merge pull request #289 from PBH-BTN/master
Browse files Browse the repository at this point in the history
v5.1.0
  • Loading branch information
Ghost-chu authored Jul 25, 2024
2 parents 1666566 + 53e0fc4 commit 9eb338a
Show file tree
Hide file tree
Showing 57 changed files with 1,853 additions and 347 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/jvm-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ name: Java CI

on:
push:
branches: [ "master", "major-refactor", "release" ]
branches: [ "master", "release", "v6" ]
pull_request:
branches: [ "master", "major-refactor", "release" ]
branches: [ "master", "release", "v6" ]
workflow_dispatch:
release:
types:
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
type=raw,ci
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v6.4.1
uses: docker/build-push-action@v6.5.0
with:
context: .
file: ./Dockerfile-CI
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/jvm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
type=raw,latest
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v6.4.1
uses: docker/build-push-action@v6.5.0
with:
context: .
file: ./Dockerfile
Expand Down Expand Up @@ -114,7 +114,7 @@ jobs:
type=raw,latest
type=sha
- name: Build and push Aliyun ACR
uses: docker/build-push-action@v6.4.1
uses: docker/build-push-action@v6.5.0
with:
context: .
file: ./Dockerfile
Expand Down
4 changes: 2 additions & 2 deletions install4j/lang/custom.utf8
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ launcher.peerbanhelper.nogui=PeerBanHelper(无GUI, 控制台)
launcher.peerbanhelper.service=PeerBanHelper(服务)
checkbox.followsystemstartup=登录时自动启动到系统托盘
peerbanhelper.description=PeerBanHelper
checkbox.registersystemservice=注册为系统服务
systemservice.note=将 PeerBanHelper 安装为系统服务时,Windows 系统下配置文件将被存储在 C:\Windows\System32\config\systemprofile\AppData\Local\PeerBanHelper 位置,访问此位置需要您拥有管理员权限。
checkbox.registersystemservice=注册为系统服务(除非你知道这是做什么的,否则不要选中)
systemservice.note=将 PeerBanHelper 安装为系统服务时,Windows 系统下配置文件将被存储在 C:\Windows\System32\config\systemprofile\AppData\Local\PeerBanHelper 位置,访问此位置需要您拥有管理员权限。\nPeerBanHelper 将在系统启动时自动启动。
2 changes: 1 addition & 1 deletion install4j/lang/en-US.utf8
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ launcher.peerbanhelper.nogui=PeerBanHelper(NoGUI, Console)
launcher.peerbanhelper.service=PeerBanHelper(Service)
checkbox.followsystemstartup=Boot automatically to the system tray when logged in
peerbanhelper.description=PeerBanHelper
checkbox.registersystemservice=Register as system service
checkbox.registersystemservice=Register as system service (DO NOT CHECK UNLESS YOU KNOW WHAT YOU'RE DOING)
systemservice.note=When you install PeerBanHelper as system service,under Windows operation system, the configuration and files will store at C:\Windows\System32\config\systemprofile\AppData\Local\PeerBanHelper. Access this location will require Administrator privileges.
2 changes: 1 addition & 1 deletion 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>5.0.7</version>
<version>5.1.0</version>
<packaging>takari-jar</packaging>

<name>PeerBanHelper</name>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/ghostchu/peerbanhelper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private static void initGUI(String[] args) {
}
if ("javafx".equals(guiType)) {
try {
if (!loadJavaFxDependencies()) {
if (!loadDependencies("/libraries/javafx.maven")) {
guiType = "swing";
}
} catch (IOException e) {
Expand All @@ -264,8 +264,8 @@ public static String getUserAgent() {
return "PeerBanHelper/" + meta.getVersion() + " BTN-Protocol/0.0.0-dev";
}

private static boolean loadJavaFxDependencies() throws IOException {
try (var is = Main.class.getResourceAsStream("/libraries/javafx.maven")) {
public static boolean loadDependencies(String mavenManifestPath) throws IOException {
try (var is = Main.class.getResourceAsStream(mavenManifestPath)) {
String str = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
String[] libraries = str.split("\n");
String osName = System.getProperty("os.name").toLowerCase();
Expand Down Expand Up @@ -372,7 +372,7 @@ public static void unregisterBean(String beanName) {
ConfigurableApplicationContext configurableApplicationContext = applicationContext;
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
defaultListableBeanFactory.removeBeanDefinition(beanName);

}


}
2 changes: 1 addition & 1 deletion src/main/java/com/ghostchu/peerbanhelper/MainJavaFx.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void start(Stage st) throws Exception {
stage = st;
FXMLLoader fxmlLoader = new FXMLLoader(MainJavaFx.class.getResource("/javafx/main_window.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
st.setTitle(String.format("PeerBanHelper (JavaFx) - Loading..."));
st.setTitle("PeerBanHelper (JavaFx) - Loading...");
st.setScene(scene);
st.setWidth(1000);
st.setHeight(600);
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,9 @@ public class PeerBanHelperServer {
private YamlConfiguration mainConfig;
@Autowired
private ModuleMatchCache moduleMatchCache;
@Autowired
@Qualifier("banListFile")
private File banListFile;
private ScheduledExecutorService BAN_WAVE_SERVICE;
@Getter
private Map<PeerAddress, PeerMetadata> LIVE_PEERS = new HashMap<>();
private Map<PeerAddress, List<PeerMetadata>> LIVE_PEERS = new HashMap<>();
@Autowired
@Qualifier("persistMetrics")
private BasicMetrics metrics;
Expand Down Expand Up @@ -153,9 +150,10 @@ public void start() throws SQLException {
banListInvoker.forEach(BanListInvoker::reset);
GENERAL_SCHEDULER.scheduleWithFixedDelay(this::saveBanList, 10 * 1000, BANLIST_SAVE_INTERVAL, TimeUnit.MILLISECONDS);
Main.getEventBus().post(new PBHServerStartedEvent(this));
if (downloaders.isEmpty()) {

if (webContainer.getToken() == null || webContainer.getToken().isBlank()) {
for (int i = 0; i < 50; i++) {
log.error(tlUI(Lang.NEW_SETUP_NO_DOWNLOADERS, getWebContainer() == null ? "ERROR" : getWebContainer().getToken()));
log.error(tlUI(Lang.PBH_OOBE_REQUIRED, "http://localhost:" + webContainer.javalin().port()));
}
}
}
Expand Down Expand Up @@ -491,15 +489,17 @@ private List<BanDetail> checkBans(Map<Torrent, List<Peer>> provided) {
}

private void updateLivePeers(Map<Downloader, Map<Torrent, List<Peer>>> peers) {
Map<PeerAddress, PeerMetadata> livePeers = new HashMap<>(128);
Map<PeerAddress, List<PeerMetadata>> livePeers = new HashMap<>(128);
peers.forEach((downloader, tasks) ->
tasks.forEach((torrent, peer) ->
peer.forEach(p -> {
PeerAddress address = p.getPeerAddress();
List<PeerMetadata> data = livePeers.getOrDefault(address, new ArrayList<>());
PeerMetadata metadata = new PeerMetadata(
downloader.getName(),
torrent, p);
livePeers.put(address, metadata);
data.add(metadata);
livePeers.put(address, data);
}
)));
LIVE_PEERS = Map.copyOf(livePeers);
Expand Down Expand Up @@ -577,6 +577,9 @@ private void registerModules() {
moduleManager.register(RuleSubController.class);
moduleManager.register(PBHAuthenticateController.class);
moduleManager.register(PBHLogsController.class);
moduleManager.register(ActiveMonitoringModule.class);
moduleManager.register(PBHPlusController.class);
moduleManager.register(PBHOOBEController.class);
}

public Map<Downloader, Map<Torrent, List<Peer>>> collectPeers() {
Expand Down Expand Up @@ -795,7 +798,7 @@ private BanMetadata unbanPeer(@NotNull PeerAddress address) {
return metadata;
}

public Map<PeerAddress, PeerMetadata> getLivePeersSnapshot() {
public Map<PeerAddress, List<PeerMetadata>> getLivePeersSnapshot() {
return LIVE_PEERS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ private void submit() {

private List<BtnPeer> generatePing() {
List<BtnPeer> btnPeers = new ArrayList<>();
btnNetwork.getServer().getLivePeersSnapshot().forEach((pa, pm) -> {
btnPeers.add(BtnPeer.from(pm.getTorrent(), pm.getPeer()));
});
btnNetwork.getServer().getLivePeersSnapshot().forEach((pa, pm) ->
pm.forEach(meta -> btnPeers.add(BtnPeer.from(meta.getTorrent(), meta.getPeer()))));
return btnPeers;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.ghostchu.peerbanhelper.config;

import com.ghostchu.peerbanhelper.Main;
import com.ghostchu.peerbanhelper.text.Lang;
import lombok.extern.slf4j.Slf4j;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;

import java.io.File;
import java.util.UUID;

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

@Slf4j
public class MainConfigUpdateScript {
private final YamlConfiguration conf;
Expand All @@ -21,11 +18,16 @@ public MainConfigUpdateScript(YamlConfiguration conf) {
}

private void validate() {
String token = conf.getString("server.token");
if (token == null || token.isBlank() || token.length() < 8) {
conf.set("server.token", UUID.randomUUID().toString());
log.info(tlUI(Lang.TOO_WEAK_TOKEN));
}
// String token = conf.getString("server.token");
// if (token == null || token.isBlank() || token.length() < 8) {
// conf.set("server.token", UUID.randomUUID().toString());
// log.info(tlUI(Lang.TOO_WEAK_TOKEN));
// }
}

@UpdateScript(version = 15)
public void pbhPlusKeyConfig() {
conf.set("pbh-plus-key", "");
}

@UpdateScript(version = 14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ public ProfileUpdateScript(YamlConfiguration conf) {
this.conf = conf;
}


@UpdateScript(version = 14)
public void activeMonitoringAndNetTypeAndPCBPersist() {
conf.set("module.active-monitoring.enabled", false);
conf.set("module.active-monitoring.data-retention-time", 5184000000L);
conf.set("module.active-monitoring.data-cleanup-interval", 604800000L);

conf.set("module.ip-address-blocker.net-type.wideband", false);
conf.set("module.ip-address-blocker.net-type.base-station", false);
conf.set("module.ip-address-blocker.net-type.government-and-enterprise-line", false);
conf.set("module.ip-address-blocker.net-type.business-platform", false);
conf.set("module.ip-address-blocker.net-type.backbone-network", false);
conf.set("module.ip-address-blocker.net-type.ip-private-network", false);
conf.set("module.ip-address-blocker.net-type.internet-cafe", false);
conf.set("module.ip-address-blocker.net-type.iot", false);
conf.set("module.ip-address-blocker.net-type.datacenter", false);

conf.set("module.progress-cheat-blocker.enable-persist", true);
conf.set("module.progress-cheat-blocker.persist-duration", 1209600000);
}

@UpdateScript(version = 13)
public void skip() {
}

@UpdateScript(version = 12)
public void patchProgressCheckBlocker() {
conf.set("module.progress-cheat-blocker.ban-duration", "default");
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/ghostchu/peerbanhelper/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

@Getter
@Slf4j
@Component
public class Database {
private JdbcSingleConnectionSource dataSource;
Expand Down Expand Up @@ -41,8 +44,16 @@ public void setupDatabase(File file) throws SQLException {
config.setMaxLifetime(60000); // 60 Sec
config.setMaximumPoolSize(1); // 50 Connections (including idle connections)
this.hikari = new HikariDataSource(config);
this.dataSource = new JdbcSingleConnectionSource("jdbc:sqlite:" + file, new SqliteDatabaseType(), this.hikari.getConnection());

Connection rawConnection = this.hikari.getConnection();
if (System.getProperty("disableSQLitePragmaSettings") == null) {
try (var stmt = rawConnection.createStatement()) {
stmt.executeUpdate("PRAGMA synchronous = NORMAL");
stmt.executeUpdate("PRAGMA journal_mode = TRUNCATE");
} catch (Exception e) {
log.warn("Unable to set SQLite optimized PRAGMA arguments", e);
}
}
this.dataSource = new JdbcSingleConnectionSource("jdbc:sqlite:" + file, new SqliteDatabaseType(), rawConnection);
// this.dataSource = new DataSourceConnectionSource( new HikariDataSource(config), new SqliteDatabaseType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ private void createTables() throws SQLException {
TableUtils.createTableIfNotExists(database.getDataSource(), BanListEntity.class);
TableUtils.createTableIfNotExists(database.getDataSource(), RuleSubInfoEntity.class);
TableUtils.createTableIfNotExists(database.getDataSource(), RuleSubLogEntity.class);
TableUtils.createTableIfNotExists(database.getDataSource(), PeerRecordEntity.class);
TableUtils.createTableIfNotExists(database.getDataSource(), ProgressCheatBlockerPersistEntity.class);
}

private void performUpgrade() throws SQLException {
Expand All @@ -49,7 +51,7 @@ private void performUpgrade() throws SQLException {
var historyDao = DaoManager.createDao(getDataSource(), HistoryEntity.class);
historyDao.executeRaw("ALTER TABLE " + historyDao.getTableName() + " ADD COLUMN downloader VARCHAR DEFAULT ''");
} catch (Exception err) {
log.error("Unable to upgrade database schema", err);
//log.error("Unable to upgrade database schema", err);
}
v = 3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Map<String, Long> getBannedIps(int n) throws Exception {
try (GenericRawResults<String[]> banLogs = queryBuilder()
.selectRaw("ip, COUNT(*) AS count")
.groupBy("ip")
.orderBy("count", false)
.orderByRaw("count DESC")
.limit((long) n)
.where().ge("banAt", twoWeeksAgo)
.queryRaw()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class ModuleDao extends AbstractPBHDao<ModuleEntity, Long> {
public ModuleDao(@Autowired Database database) throws SQLException {
super(database.getDataSource(), ModuleEntity.class);
setObjectCache(true);
}

@Override
Expand Down
Loading

0 comments on commit 9eb338a

Please sign in to comment.