Skip to content

Commit

Permalink
Merge pull request #696 from PBH-BTN/master
Browse files Browse the repository at this point in the history
7.1.1
  • Loading branch information
Ghost-chu authored Nov 6, 2024
2 parents 0ff1c09 + 8411fbb commit 7a312a2
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 60 deletions.
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>7.1.0</version>
<version>7.1.1</version>
<packaging>jar</packaging>

<name>PeerBanHelper</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public void start() throws SQLException {
registerBanListInvokers();
setupScriptEngine();
registerModules();
registerHttpServer();
setupIPDB();
registerHttpServer();
resetKnownDownloaders();
loadBanListToMemory();
registerTimer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ private void setupHttpClient() {
.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.userAgent(userAgent)
.defaultHeader("Accept-Encoding", "gzip,deflate")
.defaultHeader("Content-Type", "application/json")
.defaultHeader("BTN-AppID", appId)
.defaultHeader("BTN-AppSecret", appSecret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean identifierAlertExistsIncludeRead(String identifier) throws SQLExc
public int deleteOldAlerts(Timestamp before) throws SQLException {
var builder = deleteBuilder();
builder.setWhere(
queryBuilder().where().lt("createAt", new SelectArg(before))
queryBuilder().where().lt("createAt",before)
.and()
.isNotNull("readAt")
);
Expand All @@ -68,7 +68,7 @@ public void markAllAsRead() throws SQLException {
public void markAsRead(String identifier) throws SQLException {
update(updateBuilder()
.updateColumnValue("identifier", new SelectArg(identifier))
.updateColumnValue("readAt", new SelectArg(new Timestamp(System.currentTimeMillis())))
.updateColumnValue("readAt", new Timestamp(System.currentTimeMillis()))
.prepare());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void syncPendingTasks(Deque<BatchHandleTasks> tasks) throws SQLException

public Page<PeerRecordEntity> getPendingSubmitPeerRecords(Pageable pageable, Timestamp afterThan) throws SQLException {
var queryBuilder = queryBuilder().where()
.gt("lastTimeSeen", new SelectArg(afterThan))
.gt("lastTimeSeen", afterThan)
.or()
.isNull("lastTimeSeen")
.queryBuilder()
Expand Down Expand Up @@ -101,7 +101,7 @@ public synchronized PeerRecordEntity createIfNotExists(PeerRecordEntity data) th
PeerRecordEntity existing = queryBuilder().where()
.eq("address", new SelectArg(data.getAddress()))
.and()
.eq("torrent_id", new SelectArg(data.getTorrent().getId()))
.eq("torrent_id", data.getTorrent().getId())
.and()
.eq("downloader", new SelectArg(data.getDownloader()))
.queryForFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public List<ProgressCheatBlocker.ClientTask> fetchFromDatabase(ProgressCheatBloc
.where()
.eq("torrentId", new SelectArg(client.getTorrentId()))
.and()
.ge("lastTimeSeen", new SelectArg(after))
.ge("lastTimeSeen", after)
.query();
return entities.stream().filter(
entity -> address.contains(IPAddressUtil.getIPAddress(entity.getAddress()))
Expand Down Expand Up @@ -107,7 +107,7 @@ private ProgressCheatBlockerPersistEntity findExists(String ip, String torrentId

public int cleanupDatabase(Timestamp timestamp) throws SQLException {
var builder = deleteBuilder();
var where = builder.where().lt("lastTimeSeen", new SelectArg(timestamp));
var where = builder.where().lt("lastTimeSeen", timestamp);
builder.setWhere(where);
return builder.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ghostchu.peerbanhelper.database.dao.AbstractPBHDao;
import com.ghostchu.peerbanhelper.database.table.TrafficJournalEntity;
import com.ghostchu.peerbanhelper.util.MiscUtil;
import com.ghostchu.peerbanhelper.util.MsgUtil;
import com.j256.ormlite.stmt.SelectArg;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -73,9 +74,9 @@ public void fixTimezone(TrafficJournalDao.TrafficData data) {
public List<TrafficData> getAllDownloadersOverallData(Timestamp start, Timestamp end) throws Exception {
try (var results = queryBuilder().selectRaw("timestamp", "SUM(dataOverallUploaded) AS totalUploaded", "SUM(dataOverallDownloaded) AS totalDownloaded")
.where()
.ge("timestamp",new SelectArg( start.getTime()))
.ge("timestamp", start.getTime())
.and()
.le("timestamp",new SelectArg( end.getTime()))
.le("timestamp", end.getTime())
.queryBuilder()
.groupBy("timestamp")
.queryRaw()) {
Expand All @@ -86,11 +87,11 @@ public List<TrafficData> getAllDownloadersOverallData(Timestamp start, Timestamp
public List<TrafficData> getSpecificDownloaderOverallData(String downloadName, Timestamp start, Timestamp end) throws Exception {
try (var results = queryBuilder().selectRaw("timestamp", "SUM(dataOverallUploaded) AS totalUploaded", "SUM(dataOverallDownloaded) AS totalDownloaded")
.where()
.ge("timestamp", new SelectArg(start.getTime()))
.ge("timestamp", start.getTime())
.and()
.le("timestamp", new SelectArg(end.getTime()))
.le("timestamp", end.getTime())
.and()
.eq("downloader", new SelectArg(downloadName))
.eq("downloader", MsgUtil.escapeSql(downloadName))
.queryBuilder()
.groupBy("timestamp")
.queryRaw()) {
Expand All @@ -104,7 +105,7 @@ public synchronized TrafficJournalEntity createIfNotExists(TrafficJournalEntity
return null;
}
TrafficJournalEntity existing = queryBuilder().where()
.eq("timestamp", new SelectArg(data.getTimestamp()))
.eq("timestamp", data.getTimestamp())
.and()
.eq("downloader", new SelectArg(data.getDownloader())).queryBuilder().queryForFirst();
if (existing == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public BiglyBT(String name, Config config, AlertManager alertManager) {
.followRedirects(HttpClient.Redirect.ALWAYS)
.defaultHeader("Authorization", "Bearer " + config.getToken())
.defaultHeader("Content-Type", "application/json")
.defaultHeader("Accept-Encoding", "gzip,deflate")
.connectTimeout(Duration.of(10, ChronoUnit.SECONDS))
.headersTimeout(Duration.of(10, ChronoUnit.SECONDS))
.readTimeout(Duration.of(30, ChronoUnit.SECONDS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public BitComet(String name, Config config, AlertManager alertManager) {
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
Methanol.Builder builder = Methanol.newBuilder()
.version(HttpClient.Version.valueOf(config.getHttpVersion()))
.defaultHeader("Accept-Encoding", "gzip,deflate")
.defaultHeader("Content-Type", "application/json")
.defaultHeader("Client-Type", "BitComet WebUI")
.defaultHeader("User-Agent", "PeerBanHelper BitComet Adapter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public AbstractQbittorrent(String name, QBittorrentConfig config, AlertManager a
Methanol.Builder builder = Methanol
.newBuilder()
.version(HttpClient.Version.valueOf(config.getHttpVersion()))
.defaultHeader("Accept-Encoding", "gzip,deflate")
.followRedirects(HttpClient.Redirect.ALWAYS)
.connectTimeout(Duration.of(10, ChronoUnit.SECONDS))
.headersTimeout(Duration.of(10, ChronoUnit.SECONDS))
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ghostchu/peerbanhelper/ipdb/IPDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -291,11 +292,10 @@ private void setupHttpClient() {
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.ALWAYS)
.userAgent(userAgent)
.defaultHeader("Accept-Encoding", "gzip,deflate")
.requestTimeout(Duration.of(2, ChronoUnit.MINUTES))
.connectTimeout(Duration.of(15, ChronoUnit.SECONDS))
.headersTimeout(Duration.of(15, ChronoUnit.SECONDS))
.readTimeout(Duration.of(30, ChronoUnit.SECONDS))
.requestTimeout(Duration.of(2, ChronoUnit.MINUTES))
.readTimeout(Duration.of(30, ChronoUnit.SECONDS), Executors.newScheduledThreadPool(1, Thread.ofVirtual().factory()))
.authenticator(new Authenticator() {
@Override
public PasswordAuthentication requestPasswordAuthenticationInstance(String host, InetAddress addr, int port, String protocol, String prompt, String scheme, URL url, RequestorType reqType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.ghostchu.peerbanhelper.util.MiscUtil;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.ghostchu.peerbanhelper.wrapper.PeerAddress;
import com.j256.ormlite.stmt.SelectArg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -54,9 +53,9 @@ private void cleanup() {
builder.setWhere(builder
.where()
.le("banAt",
new SelectArg(new Timestamp(LocalDateTime.now().minusDays(keepDays)
new Timestamp(LocalDateTime.now().minusDays(keepDays)
.toInstant(MiscUtil.getSystemZoneOffset())
.toEpochMilli()))));
.toEpochMilli())));
log.info(tlUI(Lang.CLEANED_BANLOGS, builder.delete()));
} catch (Exception e) {
log.error("Unable to cleanup expired banlogs", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.eventbus.Subscribe;
import com.j256.ormlite.stmt.SelectArg;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -191,7 +190,7 @@ private void cleanup() {
try {
var deleteBuilder = peerRecordDao.deleteBuilder();
var where = deleteBuilder.where()
.lt("lastTimeSeen", new SelectArg(dataRetentionTime));
.lt("lastTimeSeen", dataRetentionTime);
deleteBuilder.setWhere(where);
int deleted = deleteBuilder.delete();
log.info(tlUI(Lang.AMM_CLEANED_UP, deleted));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ private void handleTraffic(Context ctx) throws Exception {
private void handleTrafficClassic(Context ctx) throws Exception {
var timeQueryModel = WebUtil.parseTimeQueryModel(ctx);
String downloader = ctx.queryParam("downloader");
var records = trafficJournalDao.getDayOffsetData(downloader, timeQueryModel.startAt(), timeQueryModel.endAt(), d -> fixTimezone(ctx, d));
var records = trafficJournalDao.getDayOffsetData(downloader,
timeQueryModel.startAt(),
timeQueryModel.endAt(), d -> fixTimezone(ctx, d));
ctx.json(new StdResp(true, null, records));
}

Expand All @@ -106,18 +108,18 @@ private void handlePeerTrends(Context ctx) throws Exception {
var queryConnected = peerRecordDao.queryBuilder()
.selectColumns("id", "lastTimeSeen")
.where()
.ge("lastTimeSeen", new SelectArg(timeQueryModel.startAt()))
.ge("lastTimeSeen", timeQueryModel.startAt())
.and()
.le("lastTimeSeen", new SelectArg(timeQueryModel.endAt()));
.le("lastTimeSeen", timeQueryModel.endAt());
var queryBanned = historyDao.queryBuilder()
.selectColumns("id", "banAt")
.where()
.ge("banAt", new SelectArg(timeQueryModel.startAt()))
.ge("banAt", timeQueryModel.startAt())
.and()
.le("banAt", new SelectArg(timeQueryModel.endAt()));
.le("banAt", timeQueryModel.endAt());
if (downloader != null && !downloader.isBlank()) {
queryConnected.and().eq("downloader", new SelectArg(downloader));
queryBanned.and().eq("downloader", new SelectArg( downloader));
queryBanned.and().eq("downloader", new SelectArg(downloader));
}
try (var it = queryConnected.iterator()) {
while (it.hasNext()) {
Expand Down Expand Up @@ -161,16 +163,16 @@ private void handleGeoIP(Context ctx) throws Exception {
.distinct()
.selectColumns("id", "ip")
.where()
.ge("banAt", new SelectArg(timeQueryModel.startAt()))
.ge("banAt", timeQueryModel.startAt())
.and()
.le("banAt", new SelectArg(timeQueryModel.endAt()));
.le("banAt", timeQueryModel.endAt());
var queryConnected = peerRecordDao.queryBuilder()
.distinct()
.selectColumns("id", "address")
.where()
.ge("lastTimeSeen", new SelectArg(timeQueryModel.startAt()))
.ge("lastTimeSeen", timeQueryModel.startAt())
.and()
.le("lastTimeSeen", new SelectArg(timeQueryModel.endAt()));
.le("lastTimeSeen", timeQueryModel.endAt());
if (downloader != null && !downloader.isBlank()) {
queryBanned.and().eq("downloader", new SelectArg(downloader));
queryConnected.and().eq("downloader", new SelectArg(downloader));
Expand All @@ -183,6 +185,7 @@ private void handleGeoIP(Context ctx) throws Exception {
public boolean hasNext() {
return bannedOnly ? itBanned.hasNext() : itConnected.hasNext();
}

@Override
public String next() {
return bannedOnly ? itBanned.next().getIp() : itConnected.next().getAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ private void handleBanTrends(Context ctx) throws Exception {
var queryBanned = historyDao.queryBuilder()
.selectColumns("id", "banAt")
.where()
.ge("banAt", new SelectArg(timeQueryModel.startAt()))
.ge("banAt", timeQueryModel.startAt())
.and()
.le("banAt", new SelectArg(timeQueryModel.endAt()));
.le("banAt", timeQueryModel.endAt());
if (downloader != null && !downloader.isBlank()) {
queryBanned.and().eq("downloader", new SelectArg(downloader));
}
Expand All @@ -73,9 +73,9 @@ private void handleBanTrends(Context ctx) throws Exception {
}
}
ctx.json(new StdResp(true, null, bannedPeerTrends.entrySet().stream()
.map((e) -> new PBHChartController.SimpleLongIntKV(e.getKey(), e.getValue().intValue()))
.sorted(Comparator.comparingLong(PBHChartController.SimpleLongIntKV::key))
.toList()
.map((e) -> new PBHChartController.SimpleLongIntKV(e.getKey(), e.getValue().intValue()))
.sorted(Comparator.comparingLong(PBHChartController.SimpleLongIntKV::key))
.toList()
));
}

Expand All @@ -87,8 +87,8 @@ private void handleHistoryDateAccess(Context ctx) throws Exception {
if (field == null) {
throw new IllegalArgumentException("startAt cannot be null");
}
if(field.equalsIgnoreCase("banAt")){
if("day".equals(type)){
if (field.equalsIgnoreCase("banAt")) {
if ("day".equals(type)) {
// 劫持单独处理以加快首屏请求
handlePeerBans(ctx);
return;
Expand Down Expand Up @@ -176,7 +176,7 @@ private void handleHistoryNumberAccess(Context ctx) throws Exception {
double filter = Double.parseDouble(Objects.requireNonNullElse(ctx.queryParam("filter"), "0.0"));
String downloader = ctx.queryParam("downloader");
Integer substringLength = null;
if("peerId".equalsIgnoreCase(field)){
if ("peerId".equalsIgnoreCase(field)) {
substringLength = 8;
}
List<HistoryDao.UniversalFieldNumResult> results = switch (type) {
Expand All @@ -194,16 +194,16 @@ private void handlePeerBans(Context ctx) throws Exception {
try (var it = historyDao.queryBuilder()
.selectColumns("id", "banAt")
.where()
.ge("banAt", new SelectArg(timeQueryModel.startAt()))
.ge("banAt", timeQueryModel.startAt())
.and()
.le("banAt", new SelectArg(timeQueryModel.endAt()))
.le("banAt", timeQueryModel.endAt())
.iterator()) {
while (it.hasNext()) {
var startOfDay = MiscUtil.getStartOfToday(it.next().getBanAt().getTime());
bannedPeerTrends.computeIfAbsent(startOfDay, k -> new AtomicInteger()).addAndGet(1);
}
}
ctx.json(new StdResp(true, null, bannedPeerTrends.entrySet().stream().map((e)-> new HistoryDao.UniversalFieldDateResult(e.getKey(), e.getValue().intValue() , 0)).toList()));
ctx.json(new StdResp(true, null, bannedPeerTrends.entrySet().stream().map((e) -> new HistoryDao.UniversalFieldDateResult(e.getKey(), e.getValue().intValue(), 0)).toList()));
}

private void handleBasicCounter(Context ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ private void handleTorrentQuery(Context ctx) throws SQLException {
for (TorrentEntity result : torrentEntityPage.getResults()) {
var peerBanCount = historyDao.queryBuilder()
.where()
.eq("torrent_id", new SelectArg(result.getId()))
.eq("torrent_id", result.getId())
.countOf();
var peerAccessCount = peerRecordDao.queryBuilder()
.orderBy("lastTimeSeen", false)
.where()
.eq("torrent_id", new SelectArg(result.getId()))
.eq("torrent_id", result.getId())
.countOf();
infoList.add(new TorrentInfo(result.getInfoHash(), result.getName(), result.getSize(), peerBanCount, peerAccessCount));
}
Expand All @@ -135,12 +135,12 @@ private void handleTorrentInfo(Context ctx) throws SQLException {
var t = torrent.get();
var peerBanCount = historyDao.queryBuilder()
.where()
.eq("torrent_id", new SelectArg(t.getId()))
.eq("torrent_id",t.getId())
.countOf();
var peerAccessCount = peerRecordDao.queryBuilder()
.orderBy("lastTimeSeen", false)
.where()
.eq("torrent_id", new SelectArg(t.getId()))
.eq("torrent_id", t.getId())
.countOf();

ctx.json(new StdResp(true, null, new TorrentInfo(t.getInfoHash(),
Expand All @@ -158,7 +158,7 @@ private void handleConnectHistory(Context ctx) throws SQLException {
Pageable pageable = new Pageable(ctx);
var t = torrent.get();
var queryBuilder = peerRecordDao.queryBuilder().orderBy("lastTimeSeen", false);
var queryWhere = queryBuilder.where().eq("torrent_id", new SelectArg(t));
var queryWhere = queryBuilder.where().eq("torrent_id", t);
queryBuilder.setWhere(queryWhere);
Page<PeerRecordEntity> page = peerRecordDao.queryByPaging(queryBuilder, pageable);
ctx.json(new StdResp(true, null, page));
Expand Down
Loading

0 comments on commit 7a312a2

Please sign in to comment.