Skip to content

Commit

Permalink
Merge pull request #461 from PBH-BTN/master
Browse files Browse the repository at this point in the history
v6.2.0
  • Loading branch information
Ghost-chu authored Sep 11, 2024
2 parents 9124617 + 2c03edd commit 8ee6940
Show file tree
Hide file tree
Showing 33 changed files with 423 additions and 75 deletions.
41 changes: 23 additions & 18 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>6.1.4</version>
<version>6.2.0</version>
<packaging>takari-jar</packaging>

<name>PeerBanHelper</name>
Expand All @@ -17,7 +17,7 @@
<mainClass>com.ghostchu.peerbanhelper.MainJumpLoader</mainClass>
<maven.build.timestamp.format>yyyyMMdd-HHmmss</maven.build.timestamp.format>
<flatlafVersion>3.5.1</flatlafVersion>
<javafx.version>23-ea+27</javafx.version>
<javafx.version>24-ea+5</javafx.version>
<javafx.scope>provided</javafx.scope>
<install4j.home>/opt/install4j</install4j.home>
<ormlite.version>6.1</ormlite.version>
Expand Down Expand Up @@ -142,7 +142,7 @@
<plugin>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-lifecycle-plugin</artifactId>
<version>2.1.7</version>
<version>2.2.0</version>
<extensions>true</extensions>
<configuration>
<proc>proc</proc>
Expand Down Expand Up @@ -474,7 +474,7 @@
<dependency>
<groupId>org.bspfsystems</groupId>
<artifactId>yamlconfiguration</artifactId>
<version>2.0.2</version>
<version>3.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -529,12 +529,12 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.23.1</version>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>com.formdev</groupId>
Expand Down Expand Up @@ -587,18 +587,18 @@
<version>${javafx.version}</version>
<scope>${javafx.scope}</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>${javafx.version}</version>
<scope>${javafx.scope}</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>${javafx.version}</version>
<scope>${javafx.scope}</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-media</artifactId>-->
<!-- <version>${javafx.version}</version>-->
<!-- <scope>${javafx.scope}</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-web</artifactId>-->
<!-- <version>${javafx.version}</version>-->
<!-- <scope>${javafx.scope}</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
Expand Down Expand Up @@ -680,6 +680,11 @@
<artifactId>preferencesfx-core</artifactId>
<version>11.17.0</version>
</dependency>
<dependency>
<groupId>com.rollbar</groupId>
<artifactId>rollbar-java</artifactId>
<version>1.10.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.pixelduke</groupId>-->
<!-- <artifactId>transit</artifactId>-->
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/ghostchu/lib/jni/EcoMode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ghostchu.lib.jni;

import com.ghostchu.peerbanhelper.Main;
import com.ghostchu.peerbanhelper.telemetry.rollbar.RollbarErrorReporter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
Expand All @@ -10,8 +12,15 @@
import java.util.Locale;

@Slf4j
@Component
public class EcoMode {
public static boolean ecoMode(boolean enable) {
private final RollbarErrorReporter rollbarErrorReporter;

public EcoMode(RollbarErrorReporter rollbarErrorReporter) {
this.rollbarErrorReporter = rollbarErrorReporter;
}

public boolean ecoMode(boolean enable) {
String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if (!os.startsWith("win")) {
throw new IllegalStateException("Only Windows OS support EcoMode API");
Expand All @@ -26,13 +35,15 @@ public static boolean ecoMode(boolean enable) {
Files.copy(Main.class.getResourceAsStream("/native/windows/ghost-common-jni_vc2015_amd64.dll"), tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
System.load(tmpFile.getAbsolutePath());
} catch (IOException e) {
} catch (Exception e) {
log.error("Unable load JNI native libraries", e);
rollbarErrorReporter.warning(e);
}
try {
String data = setEcoMode(enable);
return data.equals("SUCCESS");
} catch (Throwable e) {
rollbarErrorReporter.warning(e);
return false;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/ghostchu/peerbanhelper/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@Configuration
@ComponentScan(value = "com.ghostchu.peerbanhelper", excludeFilters = @ComponentScan.Filter(IgnoreScan.class))
@ComponentScan(value = "com.ghostchu.lib.jni", excludeFilters = @ComponentScan.Filter(IgnoreScan.class))
@Slf4j
public class AppConfig {
@Bean
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.ghostchu.peerbanhelper.module.impl.rule.*;
import com.ghostchu.peerbanhelper.module.impl.webapi.*;
import com.ghostchu.peerbanhelper.peer.Peer;
import com.ghostchu.peerbanhelper.telemetry.ErrorReporter;
import com.ghostchu.peerbanhelper.telemetry.rollbar.RollbarErrorReporter;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.text.TranslationComponent;
import com.ghostchu.peerbanhelper.torrent.Torrent;
Expand Down Expand Up @@ -86,8 +88,9 @@ public class PeerBanHelperServer implements Reloadable {
private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
@Getter
private final List<BanListInvoker> banListInvoker = new ArrayList<>();
private String pbhServerAddress;
private final ScheduledExecutorService GENERAL_SCHEDULER = Executors.newScheduledThreadPool(8, Thread.ofVirtual().factory());
private final Lock banWaveLock = new ReentrantLock();
private String pbhServerAddress;
@Getter
private YamlConfiguration profileConfig;
@Getter
Expand All @@ -101,7 +104,6 @@ public class PeerBanHelperServer implements Reloadable {
@Autowired
private ModuleMatchCache moduleMatchCache;
private ScheduledExecutorService BAN_WAVE_SERVICE;
private final Lock banWaveLock = new ReentrantLock();
@Getter
private Map<PeerAddress, List<PeerMetadata>> LIVE_PEERS = new HashMap<>();
@Autowired
Expand All @@ -128,6 +130,10 @@ public class PeerBanHelperServer implements Reloadable {
private HitRateMetric hitRateMetric = new HitRateMetric();
@Autowired
private BanListDao banListDao;
@Autowired
private ErrorReporter errorReporter;
@Autowired
private RollbarErrorReporter rollbarErrorReporter;

public PeerBanHelperServer() {
reloadConfig();
Expand Down Expand Up @@ -341,6 +347,7 @@ private void saveBanList() {
log.info(tlUI(Lang.SAVED_BANLIST, count));
} catch (Exception e) {
log.error(tlUI(Lang.SAVE_BANLIST_FAILED), e);
rollbarErrorReporter.error(e);
}
}

Expand Down Expand Up @@ -390,6 +397,7 @@ private void watchDogHungry() {
threadDump.append(MsgUtil.threadInfoToString(threadInfo));
}
log.info(threadDump.toString());
errorReporter.error("Timed out when complete the banWave", Map.of("thread_dump", threadDump));
registerBanWaveTimer();
Main.getGuiManager().createNotification(Level.WARNING, tlUI(Lang.BAN_WAVE_WATCH_DOG_TITLE), tlUI(Lang.BAN_WAVE_WATCH_DOG_DESCRIPTION));
}
Expand All @@ -399,6 +407,7 @@ private void watchDogHungry() {
* 启动新的一轮封禁序列
*/
public void banWave() {
rollbarErrorReporter.handleUncaughtErrors();
try {
if (!banWaveLock.tryLock(3, TimeUnit.SECONDS)) {
return;
Expand Down Expand Up @@ -467,25 +476,28 @@ public void banWave() {
List<Torrent> relaunch = Collections.synchronizedList(new ArrayList<>());
details.forEach(detail -> {
protect.getService().submit(() -> {
if (detail.result().action() == PeerAction.BAN) {
if (detail.result().action() == PeerAction.BAN || detail.result().action() == PeerAction.BAN_FOR_DISCONNECT) {
long actualBanDuration = banDuration;
if (detail.banDuration() > 0) {
actualBanDuration = detail.banDuration();
}
BanMetadata banMetadata = new BanMetadata(detail.result().moduleContext().getName(), downloader.getName(),
System.currentTimeMillis(), System.currentTimeMillis() + actualBanDuration,
System.currentTimeMillis(), System.currentTimeMillis() + actualBanDuration, detail.result().action() == PeerAction.BAN_FOR_DISCONNECT,
detail.torrent(), detail.peer(), detail.result().rule(), detail.result().reason());
bannedPeers.add(banMetadata);
relaunch.add(detail.torrent());
banPeer(banMetadata, detail.torrent(), detail.peer());
log.warn(tlUI(Lang.BAN_PEER, detail.peer().getPeerAddress(), detail.peer().getPeerId(), detail.peer().getClientName(), detail.peer().getProgress(), detail.peer().getUploaded(), detail.peer().getDownloaded(), detail.torrent().getName(), tl(DEF_LOCALE, detail.result().reason())));
if (detail.result().action() != PeerAction.BAN_FOR_DISCONNECT) {
log.warn(tlUI(Lang.BAN_PEER, detail.peer().getPeerAddress(), detail.peer().getPeerId(), detail.peer().getClientName(), detail.peer().getProgress(), detail.peer().getUploaded(), detail.peer().getDownloaded(), detail.torrent().getName(), tl(DEF_LOCALE, detail.result().reason())));
}
}
});
});

needRelaunched.put(downloader, relaunch);
} catch (Exception e) {
log.error("Unable to complete peer ban task, report to PBH developer!!!");
rollbarErrorReporter.error(e);
}
});
}
Expand Down Expand Up @@ -523,6 +535,7 @@ public void banWave() {
Thread.currentThread().interrupt();
} catch (Throwable throwable) {
log.error("Unable to complete scheduled tasks", throwable);
rollbarErrorReporter.error(throwable);
} finally {
banWaveWatchDog.feed();
metrics.recordCheck();
Expand Down Expand Up @@ -761,7 +774,7 @@ public CheckResult checkBan(@NotNull Torrent torrent, @NotNull Peer peer, @NotNu
result = r;
break; // 立刻离开循环,处理跳过
}
if (r.action() == PeerAction.BAN) {
if (r.action() == PeerAction.BAN || r.action() == PeerAction.BAN_FOR_DISCONNECT) {
result = r;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ private List<BtnBan> generateBans() {
if (e.getValue().getBanAt() <= lastReport) {
continue;
}
if(e.getValue().isBanForDisconnect()){
continue;
}
BtnBan btnBan = new BtnBan();
btnBan.setBtnBan(e.getValue().getContext().equals(BtnNetworkOnline.class.getName()));
btnBan.setPeer(e.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ public MainConfigUpdateScript(YamlConfiguration conf) {
}

private void validate() {
String installationId = conf.getString("installation-id");
if(installationId == null || installationId.isBlank()){
conf.set("installation-id", UUID.randomUUID().toString());
}
// 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 = 19)
public void telemetryErrorReporting() {
conf.set("privacy.error-reporting", true);
}

@UpdateScript(version = 18)
public void noMaxmindDownload() {
conf.set("ip-database.account-id", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public ProfileUpdateScript(YamlConfiguration conf) {
this.conf = conf;
}

@UpdateScript(version = 19)
public void fastPcbTesting() {
conf.set("module.progress-cheat-blocker.fast-pcb-test-percentage", 0.1d);
conf.set("module.progress-cheat-blocker.fast-pcb-test-block-duration", 15000);
}

@UpdateScript(version = 18)
public void banDelayWait() {
conf.set("module.progress-cheat-blocker.max-wait-duration", 30000);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ghostchu.peerbanhelper.database;

import com.ghostchu.peerbanhelper.database.table.*;
import com.ghostchu.peerbanhelper.telemetry.rollbar.RollbarErrorReporter;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.logger.Level;
Expand All @@ -19,9 +20,11 @@
@Slf4j
public class DatabaseHelper {
private final Database database;
private final RollbarErrorReporter rollbarErrorReporter;

public DatabaseHelper(@Autowired Database database) throws SQLException {
public DatabaseHelper(@Autowired Database database, RollbarErrorReporter rollbarErrorReporter) throws SQLException {
this.database = database;
this.rollbarErrorReporter = rollbarErrorReporter;
Logger.setGlobalLogLevel(Level.WARNING);
createTables();
performUpgrade();
Expand All @@ -44,14 +47,15 @@ private void createTables() throws SQLException {

private void performUpgrade() throws SQLException {
Dao<MetadataEntity, String> metadata = DaoManager.createDao(getDataSource(), MetadataEntity.class);
MetadataEntity version = metadata.createIfNotExists(new MetadataEntity("version", "5"));
MetadataEntity version = metadata.createIfNotExists(new MetadataEntity("version", "6"));
int v = Integer.parseInt(version.getValue());
if (v < 3) {
try {
// so something
var historyDao = DaoManager.createDao(getDataSource(), HistoryEntity.class);
historyDao.executeRaw("ALTER TABLE " + historyDao.getTableName() + " ADD COLUMN downloader VARCHAR DEFAULT ''");
} catch (Exception err) {
rollbarErrorReporter.error(err);
//log.error("Unable to upgrade database schema", err);
}
v = 3;
Expand All @@ -66,6 +70,11 @@ private void performUpgrade() throws SQLException {
TableUtils.createTableIfNotExists(database.getDataSource(), ProgressCheatBlockerPersistEntity.class);
v = 5;
}
if (v == 5) {
TableUtils.dropTable(getDataSource(), ProgressCheatBlockerPersistEntity.class, true);
TableUtils.createTableIfNotExists(database.getDataSource(), ProgressCheatBlockerPersistEntity.class);
v = 6;
}
version.setValue(String.valueOf(v));
metadata.update(version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public List<ProgressCheatBlocker.ClientTask> fetchFromDatabase(ProgressCheatBloc
entity.getFirstTimeSeen().getTime(),
entity.getLastTimeSeen().getTime(),
entity.getDownloader(),
entity.getBanDelayWindowEndAt().getTime()
entity.getBanDelayWindowEndAt().getTime(),
entity.getFastPcbTestExecuteAt()
)
).collect(Collectors.toCollection(CopyOnWriteArrayList::new)); // 可变 List,需要并发安全
}
Expand All @@ -70,7 +71,8 @@ public void flushDatabase(List<ProgressCheatBlocker.ClientTaskRecord> records) t
new Timestamp(System.currentTimeMillis()),
new Timestamp(System.currentTimeMillis()),
task.getDownloader(),
new Timestamp(task.getBanDelayWindowEndAt())
new Timestamp(task.getBanDelayWindowEndAt()),
task.getFastPcbTestExecuteAt()
);
create(entity);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public final class ProgressCheatBlockerPersistEntity {
private String downloader;
@DatabaseField(canBeNull = false)
private Timestamp banDelayWindowEndAt;
@DatabaseField(canBeNull = false)
private long fastPcbTestExecuteAt;
}
Loading

0 comments on commit 8ee6940

Please sign in to comment.