Skip to content

Commit

Permalink
允许暂停下载器 #833
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Dec 26, 2024
1 parent 316556d commit 38c5b0b
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import com.ghostchu.simplereloadlib.Reloadable;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.hash.Hashing;
import com.google.gson.JsonObject;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.EvalMode;
Expand Down Expand Up @@ -676,8 +675,10 @@ public void updateDownloader(@NotNull Downloader downloader, boolean updateBanLi
try {
var loginResult = downloader.login();
if (!loginResult.success()) {
log.error(tlUI(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint(), tlUI(loginResult.getMessage())));
downloader.setLastStatus(DownloaderLastStatus.ERROR, loginResult.getMessage());
if(loginResult.getStatus() != DownloaderLoginResult.Status.PAUSED) {
log.error(tlUI(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint(), tlUI(loginResult.getMessage())));
downloader.setLastStatus(DownloaderLastStatus.ERROR, loginResult.getMessage());
}
return;
} else {
downloader.setLastStatus(DownloaderLastStatus.HEALTHY, loginResult.getMessage());
Expand Down Expand Up @@ -768,10 +769,12 @@ public Map<Torrent, List<Peer>> collectPeers(Downloader downloader) {
Map<Torrent, List<Peer>> peers = new ConcurrentHashMap<>();
var loginResult = downloader.login();
if (!loginResult.success()) {
log.error(tlUI(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint(), tlUI(loginResult.getMessage())));
downloader.setLastStatus(DownloaderLastStatus.ERROR, loginResult.getMessage());
if (loginResult.getStatus() == DownloaderLoginResult.Status.MISSING_COMPONENTS || loginResult.getStatus() == DownloaderLoginResult.Status.REQUIRE_TAKE_ACTIONS) {
downloader.setLastStatus(DownloaderLastStatus.NEED_TAKE_ACTION, loginResult.getMessage());
if(loginResult.getStatus() != DownloaderLoginResult.Status.PAUSED){
log.error(tlUI(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint(), tlUI(loginResult.getMessage())));
downloader.setLastStatus(DownloaderLastStatus.ERROR, loginResult.getMessage());
if (loginResult.getStatus() == DownloaderLoginResult.Status.MISSING_COMPONENTS || loginResult.getStatus() == DownloaderLoginResult.Status.REQUIRE_TAKE_ACTIONS) {
downloader.setLastStatus(DownloaderLastStatus.NEED_TAKE_ACTION, loginResult.getMessage());
}
}
return Collections.emptyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public AbstractDownloader(String name, AlertManager alertManager) {

@Override
public DownloaderLoginResult login() {
if(isPaused()){
return new DownloaderLoginResult(DownloaderLoginResult.Status.PAUSED, new TranslationComponent(Lang.DOWNLOADER_PAUSED));
}
if (nextLoginTry >= System.currentTimeMillis()) {
alertManager.publishAlert(true,
AlertLevel.WARN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public interface Downloader extends AutoCloseable {
*/
DownloaderLoginResult login();

boolean isPaused();

void setPaused(boolean paused);

/**
* 一个执行调度任务的窗口,该方法总是在 banWave 中调用
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public boolean success() {

public enum Status {
SUCCESS,
PAUSED,
INCORRECT_CREDENTIAL,
MISSING_COMPONENTS,
NETWORK_ERROR,
EXCEPTION,
REQUIRE_TAKE_ACTIONS
REQUIRE_TAKE_ACTIONS,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ public String getType() {
return "BiglyBT";
}

@Override
public boolean isPaused() {
return config.isPaused();
}

@Override
public void setPaused(boolean paused) {
config.setPaused(paused);
}

@Override
public void setBanList(@NotNull Collection<PeerAddress> fullList, @Nullable Collection<BanMetadata> added, @Nullable Collection<BanMetadata> removed, boolean applyFullList) {
if (removed != null && removed.isEmpty() && added != null && config.isIncrementBan() && !applyFullList) {
Expand Down Expand Up @@ -301,6 +311,7 @@ public static class Config {
private boolean incrementBan;
private boolean verifySsl;
private boolean ignorePrivate;
private boolean paused;

public static Config readFromYaml(ConfigurationSection section) {
Config config = new Config();
Expand All @@ -314,6 +325,7 @@ public static Config readFromYaml(ConfigurationSection section) {
config.setHttpVersion(section.getString("http-version", "HTTP_1_1"));
config.setVerifySsl(section.getBoolean("verify-ssl", true));
config.setIgnorePrivate(section.getBoolean("ignore-private", false));
config.setPaused(section.getBoolean("paused", false));
return config;
}

Expand All @@ -326,6 +338,7 @@ public YamlConfiguration saveToYaml() {
section.set("increment-ban", incrementBan);
section.set("ignore-private", ignorePrivate);
section.set("verify-ssl", verifySsl);
section.set("paused", paused);
return section;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ public String getType() {
return "BitComet";
}

@Override
public boolean isPaused() {
return config.isPaused();
}

@Override
public void setPaused(boolean paused) {
config.setPaused(paused);
}

public boolean isLoggedIn() {
try {
queryNeedReConfigureIpFilter();
Expand Down Expand Up @@ -460,6 +470,7 @@ public static class Config {
private String httpVersion;
private boolean verifySsl;
private boolean ignorePrivate;
private boolean paused;

public static Config readFromYaml(ConfigurationSection section) {
Config config = new Config();
Expand All @@ -474,6 +485,7 @@ public static Config readFromYaml(ConfigurationSection section) {
config.setHttpVersion(section.getString("http-version", "HTTP_1_1"));
config.setVerifySsl(section.getBoolean("verify-ssl", true));
config.setIgnorePrivate(section.getBoolean("ignore-private", false));
config.setPaused(section.getBoolean("paused", false));
return config;
}

Expand All @@ -487,6 +499,7 @@ public YamlConfiguration saveToYaml() {
section.set("http-version", httpVersion);
section.set("verify-ssl", verifySsl);
section.set("ignore-private", ignorePrivate);
section.set("paused", paused);
return section;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public String getType() {
return "Deluge";
}

@Override
public boolean isPaused() {
return config.isPaused();
}

@Override
public void setPaused(boolean paused) {
config.setPaused(paused);
}

@Override
public DownloaderLoginResult login0() {
try {
Expand Down Expand Up @@ -244,6 +254,7 @@ public static class Config {
private String rpcUrl;
private boolean incrementBan;
private boolean ignorePrivate;
private boolean paused;

public static Config readFromYaml(ConfigurationSection section) {
Config config = new Config();
Expand All @@ -258,6 +269,7 @@ public static Config readFromYaml(ConfigurationSection section) {
config.setVerifySsl(section.getBoolean("verify-ssl", true));
config.setIncrementBan(section.getBoolean("increment-ban", true));
config.setIgnorePrivate(section.getBoolean("ignore-private", false));
config.setPaused(section.getBoolean("paused", false));
return config;
}

Expand All @@ -271,6 +283,7 @@ public YamlConfiguration saveToYaml() {
section.set("increment-ban", incrementBan);
section.set("verify-ssl", verifySsl);
section.set("ignore-private", ignorePrivate);
section.set("paused", paused);
return section;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface QBittorrentConfig {

boolean isIgnorePrivate();

boolean isPaused();

void setPaused(boolean paused);

void setType(String type);

void setEndpoint(String endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public QBittorrent(String name, QBittorrentConfig config, AlertManager alertMana
super(name, config, alertManager);
}

@Override
public boolean isPaused() {
return config.isPaused();
}

@Override
public void setPaused(boolean paused) {
config.setPaused(paused);
}

public static QBittorrent loadFromConfig(String name, JsonObject section, AlertManager alertManager) {
QBittorrentConfigImpl config = JsonUtil.getGson().fromJson(section.toString(), QBittorrentConfigImpl.class);
return new QBittorrent(name, config, alertManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class QBittorrentConfigImpl implements QBittorrentConfig {
private boolean useShadowBan;
private boolean verifySsl;
private boolean ignorePrivate;
private boolean paused;

public static QBittorrentConfigImpl readFromYaml(ConfigurationSection section) {
QBittorrentConfigImpl config = new QBittorrentConfigImpl();
Expand All @@ -41,6 +42,7 @@ public static QBittorrentConfigImpl readFromYaml(ConfigurationSection section) {
config.setUseShadowBan(section.getBoolean("use-shadow-ban", false));
config.setVerifySsl(section.getBoolean("verify-ssl", true));
config.setIgnorePrivate(section.getBoolean("ignore-private", false));
config.setPaused(section.getBoolean("paused", false));
return config;
}

Expand All @@ -58,6 +60,7 @@ public YamlConfiguration saveToYaml() {
section.set("use-shadow-ban", useShadowBan);
section.set("verify-ssl", verifySsl);
section.set("ignore-private", ignorePrivate);
section.set("paused", paused);
return section;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public QBittorrentEE(String name, QBittorrentEEConfigImpl config, AlertManager a
}
}

@Override
public boolean isPaused() {
return config.isPaused();
}

@Override
public void setPaused(boolean paused) {
config.setPaused(paused);
}

public static QBittorrentEE loadFromConfig(String name, JsonObject section, AlertManager alertManager) {
QBittorrentEEConfigImpl config = JsonUtil.getGson().fromJson(section.toString(), QBittorrentEEConfigImpl.class);
return new QBittorrentEE(name, config, alertManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class QBittorrentEEConfigImpl implements QBittorrentConfig {
private boolean verifySsl;
private boolean useShadowBan;
private boolean ignorePrivate;
private boolean paused;

public static QBittorrentEEConfigImpl readFromYaml(ConfigurationSection section) {
QBittorrentEEConfigImpl config = new QBittorrentEEConfigImpl();
Expand All @@ -41,6 +42,7 @@ public static QBittorrentEEConfigImpl readFromYaml(ConfigurationSection section)
config.setUseShadowBan(section.getBoolean("use-shadow-ban", false));
config.setVerifySsl(section.getBoolean("verify-ssl", true));
config.setIgnorePrivate(section.getBoolean("ignore-private", false));
config.setPaused(section.getBoolean("paused", false));
return config;
}

Expand All @@ -58,6 +60,7 @@ public YamlConfiguration saveToYaml() {
section.set("use-shadow-ban", useShadowBan);
section.set("verify-ssl", verifySsl);
section.set("ignore-private", ignorePrivate);
section.set("paused", paused);
return section;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public String getType() {
return "Transmission";
}

@Override
public boolean isPaused() {
return config.isPaused();
}

@Override
public void setPaused(boolean paused) {
config.setPaused(paused);
}

@SneakyThrows(InterruptedException.class)
@Override
public DownloaderLoginResult login0() {
Expand Down Expand Up @@ -280,6 +290,7 @@ public static class Config {
private boolean verifySsl;
private String rpcUrl;
private boolean ignorePrivate;
private boolean paused;

public static Transmission.Config readFromYaml(ConfigurationSection section) {
Transmission.Config config = new Transmission.Config();
Expand All @@ -294,6 +305,7 @@ public static Transmission.Config readFromYaml(ConfigurationSection section) {
config.setHttpVersion(section.getString("http-version", "HTTP_1_1"));
config.setVerifySsl(section.getBoolean("verify-ssl", true));
config.setIgnorePrivate(section.getBoolean("ignore-private", false));
config.setPaused(section.getBoolean("paused", false));
return config;
}

Expand All @@ -307,6 +319,7 @@ public YamlConfiguration saveToYaml() {
section.set("http-version", httpVersion);
section.set("verify-ssl", verifySsl);
section.set("ignore-private", ignorePrivate);
section.set("paused", paused);
return section;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void handleDownloaderStatus(@NotNull Context ctx, String downloaderName)
.count();

JsonObject config = downloader.saveDownloaderJson();
ctx.json(new StdResp(true, null, new DownloaderStatus(lastStatus, tl(locale, downloader.getLastStatusMessage() == null ? new TranslationComponent(Lang.STATUS_TEXT_UNKNOWN) : downloader.getLastStatusMessage()), activeTorrents, activePeers, config)));
ctx.json(new StdResp(true, null, new DownloaderStatus(lastStatus, tl(locale, downloader.getLastStatusMessage() == null ? new TranslationComponent(Lang.STATUS_TEXT_UNKNOWN) : downloader.getLastStatusMessage()), activeTorrents, activePeers, config, downloader.isPaused())));
}

private void handleDownloaderList(@NotNull Context ctx) {
Expand All @@ -281,7 +281,7 @@ record DraftDownloader(String name, JsonObject config) {

record DownloaderStatus(DownloaderLastStatus lastStatus, String lastStatusMessage,
long activeTorrents,
long activePeers, JsonObject config) {
long activePeers, JsonObject config, boolean paused) {

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ghostchu/peerbanhelper/text/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public enum Lang {
FREE_LICENSE_DESCRIPTION,
FREE_LICENSE_SOURCE,
FREE_LICENSE_LICENSE_TO,
FREE_LICENSE_RENEW_STILL_ACTIVE, PBH_PLUS_LICENSE_UPDATED, DOWNLOADER_QB_DISABLE_SAME_IP_MULTI_CONNECTION_FAILED;
FREE_LICENSE_RENEW_STILL_ACTIVE, PBH_PLUS_LICENSE_UPDATED, DOWNLOADER_QB_DISABLE_SAME_IP_MULTI_CONNECTION_FAILED, DOWNLOADER_PAUSED;

public String getKey() {
return name();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/en_us/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,4 @@ FREE_LICENSE_RENEW_STILL_ACTIVE: "The current license is still active and does n
FREE_LICENSE_SOURCE: "Locally generated free license."
FREE_LICENSE_LICENSE_TO: "{} for personal use."
FREE_LICENSE_DESCRIPTION: "Local generated renewable free license, for personal use only."
DOWNLOADER_PAUSED: Downloader paused
3 changes: 2 additions & 1 deletion src/main/resources/lang/messages_fallback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,5 @@ FREE_LICENSE_RENEW_STILL_ACTIVE: "当前许可证仍在有效期内,无需重
FREE_LICENSE_SOURCE: "本地生成的免费许可证"
FREE_LICENSE_LICENSE_TO: "{} 用于个人用途"
FREE_LICENSE_DESCRIPTION: "本地生成的循环免费许可证,仅用于个人用途"
DOWNLOADER_QB_DISABLE_SAME_IP_MULTI_CONNECTION_FAILED: "禁用 {} ({}) 的高级设置 “允许来自不同 IP 地址的多重连接” 失败:{} - {}!此功能必须关闭,否则将导致 PCB 封禁误判。"
DOWNLOADER_QB_DISABLE_SAME_IP_MULTI_CONNECTION_FAILED: "禁用 {} ({}) 的高级设置 “允许来自不同 IP 地址的多重连接” 失败:{} - {}!此功能必须关闭,否则将导致 PCB 封禁误判。"
DOWNLOADER_PAUSED: "下载器已暂停"
3 changes: 2 additions & 1 deletion src/main/resources/lang/zh_cn/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,5 @@ FREE_LICENSE_RENEW_SUCCESS: "新的许可证已生成并激活"
FREE_LICENSE_RENEW_STILL_ACTIVE: "当前许可证仍在有效期内,无需重新生成"
FREE_LICENSE_SOURCE: "本地生成的免费许可证"
FREE_LICENSE_LICENSE_TO: "{} 用于个人用途"
FREE_LICENSE_DESCRIPTION: "本地生成的循环免费许可证,仅用于个人用途"
FREE_LICENSE_DESCRIPTION: "本地生成的循环免费许可证,仅用于个人用途"
DOWNLOADER_PAUSED: "下载器已暂停"

0 comments on commit 38c5b0b

Please sign in to comment.