Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCB 支持检查未完成/不完整任务的过量下载 #748

Merged
merged 14 commits into from
Nov 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public List<Torrent> getTorrents() {
detail.getName(),
detail.getTorrent().getInfoHash(),
detail.getTorrent().getSize(),
-1, // TODO
detail.getStats().getCompletedInThousandNotation() / 1000d,
detail.getStats().getRtUploadSpeed(),
detail.getStats().getRtDownloadSpeed(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public List<Torrent> getTorrents() {
torrent.getTask().getTaskName(),
torrent.getTaskDetail().getInfohash() != null ? torrent.getTaskDetail().getInfohash() : torrent.getTaskDetail().getInfohashV2(),
torrent.getTaskDetail().getTotalSize(),
torrent.getTask().getSelectedDownloadedSize(),
torrent.getTaskStatus().getDownloadPermillage() / 1000.0d,
torrent.getTask().getUploadRate(),
torrent.getTask().getDownloadRate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public static class TaskDTO {
private long totalSize;
// @SerializedName("selected_size")
// private long selectedSize;
// @SerializedName("selected_downloaded_size")
// private long selectedDownloadedSize;
@SerializedName("selected_downloaded_size")
private long selectedDownloadedSize;
@SerializedName("download_rate")
private long downloadRate;
@SerializedName("upload_rate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public List<Torrent> getTorrents() {
activeTorrent.getInfoHash(),
activeTorrent.getProgress() / 100.0d,
activeTorrent.getSize(),
activeTorrent.getCompletedSize(),
activeTorrent.getUploadPayloadRate(),
activeTorrent.getDownloadPayloadRate(),
peers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class DelugeTorrent implements Torrent {
private String hash;
private double progress;
private long size;
private long completedSize;
private long rtUploadSpeed;
private long rtDownloadSpeed;
private List<Peer> peers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public final class QBittorrentTorrent implements Torrent {
@SerializedName("category")
private String category;

// @SerializedName("completed")
// private Long completed;
@SerializedName("completed")
private Long completed;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add documentation and consider null safety

The completed field is crucial for the anti-leech functionality but lacks documentation. Additionally, using Long instead of long introduces potential null pointer risks in getCompletedSize().

Consider these improvements:

+/**
+ * The amount of data (in bytes) that has been successfully completed/downloaded.
+ * Used for tracking partial downloads and implementing anti-leech measures.
+ */
 @SerializedName("completed")
-private Long completed;
+private long completed;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@SerializedName("completed")
private Long completed;
/**
* The amount of data (in bytes) that has been successfully completed/downloaded.
* Used for tracking partial downloads and implementing anti-leech measures.
*/
@SerializedName("completed")
private long completed;

//
// @SerializedName("completion_on")
// private Long completionOn;
Expand Down Expand Up @@ -190,6 +190,11 @@ public long getSize() {
return totalSize;
}

@Override
public long getCompletedSize() {
return completed;
}
paulzzh marked this conversation as resolved.
Show resolved Hide resolved

@Override
public long getRtUploadSpeed() {
return upspeed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public long getSize() {
return backend.getTotalSize();
}

@Override
public long getCompletedSize() {
return -1; // TODO
}

@Override
public long getRtUploadSpeed() {
return backend.getRateUpload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ private void reloadConfig() {
final long actualUploaded = Math.max(peer.getUploaded(), Math.max(clientTask.getLastReportUploaded(), prefixTrackingUploadedIncreaseTotal));
try {
final long torrentSize = torrent.getSize();
final long completedSize = torrent.getCompletedSize();
paulzzh marked this conversation as resolved.
Show resolved Hide resolved
// 过滤
if (torrentSize <= 0) {
return pass();
Expand All @@ -253,17 +254,21 @@ private void reloadConfig() {
final double actualProgress = (double) actualUploaded / torrentSize; // 实际进度
final double clientProgress = peer.getProgress(); // 客户端汇报进度
// actualUploaded = -1 代表客户端不支持统计此 Peer 总上传量
if (actualUploaded != -1 && blockExcessiveClients && (actualUploaded > torrentSize)) {
// 下载过量,检查
long maxAllowedExcessiveThreshold = (long) (torrentSize * excessiveThreshold);
if (actualUploaded > maxAllowedExcessiveThreshold) {
clientTask.setBanDelayWindowEndAt(0L);
progressRecorder.invalidate(client); // 封禁时,移除缓存
return new CheckResult(getClass(), PeerAction.BAN, banDuration, new TranslationComponent(Lang.PCB_RULE_REACHED_MAX_ALLOWED_EXCESSIVE_THRESHOLD),
if (actualUploaded != -1 && blockExcessiveClients) {
long referenceSize = (completedSize > 0 && actualUploaded > completedSize) ? completedSize : torrentSize;
// 下载量超过种子大小或者超过已下载大小,检查
if (actualUploaded > referenceSize) {
long maxAllowedExcessiveThreshold = (long) (referenceSize * excessiveThreshold);
if (actualUploaded > maxAllowedExcessiveThreshold) {
clientTask.setBanDelayWindowEndAt(0L);
progressRecorder.invalidate(client); // Remove from cache upon banning
return new CheckResult(getClass(), PeerAction.BAN, banDuration,
new TranslationComponent(Lang.PCB_RULE_REACHED_MAX_ALLOWED_EXCESSIVE_THRESHOLD),
new TranslationComponent(Lang.MODULE_PCB_EXCESSIVE_DOWNLOAD,
torrentSize,
actualUploaded,
maxAllowedExcessiveThreshold));
referenceSize,
actualUploaded,
maxAllowedExcessiveThreshold));
}
}
}
// 如果客户端报告自己进度更多,则跳过检查
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/ghostchu/peerbanhelper/torrent/Torrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ public interface Torrent {
double getProgress();

/**
* 获取目前该 Torrent 的共计大小
* 获取该 Torrent 的总大小
*
* @return 共计大小
* @return 总大小
*/
long getSize();

/**
* 获取该 Torrent 已保存的数据量 (也就是最大可以提供的上传量)
*
* @return 已保存的数据量
*/
long getCompletedSize();

/**
* 实时下载速度
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class TorrentImpl implements Torrent {
private String id;
private String name;
private long size;
private long completedSize;

public TorrentImpl(String id, String name, String hash, long size, double progress, long rtUploadSpeed, long rtDownloadSpeed, boolean privateTorrent) {
public TorrentImpl(String id, String name, String hash, long size, long completedSize, double progress, long rtUploadSpeed, long rtDownloadSpeed, boolean privateTorrent) {
this.id = id;
this.name = name;
this.hash = hash;
this.size = size;
this.completedSize = completedSize;
paulzzh marked this conversation as resolved.
Show resolved Hide resolved
this.progress = progress;
this.rtUploadSpeed = rtUploadSpeed;
this.rtDownloadSpeed = rtDownloadSpeed;
Expand Down Expand Up @@ -49,6 +51,11 @@ public long getSize() {
return size;
}

@Override
public long getCompletedSize() {
return completedSize;
}

@Override
public long getRtUploadSpeed() {
return rtUploadSpeed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public final class TorrentWrapper {
private String id;
private long size;
private long completedSize;
private String name;
private String hash;
private double progress;
Expand All @@ -20,6 +21,7 @@ public final class TorrentWrapper {
public TorrentWrapper(Torrent torrent) {
this.id = torrent.getId();
this.size = torrent.getSize();
this.completedSize = torrent.getCompletedSize();
this.name = torrent.getName();
this.hash = torrent.getHash();
this.progress = torrent.getProgress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static class ActiveTorrentsResponseDTO {
private Double progress;
@SerializedName("size")
private Long size;
@SerializedName("completed_size")
private Long completedSize;
@SerializedName("upload_payload_rate")
private Long uploadPayloadRate;
@SerializedName("download_payload_rate")
Expand Down
Loading