From 3b449078674af6cd4972a4cb9a95be8504492a73 Mon Sep 17 00:00:00 2001 From: Ghost_chu Date: Thu, 12 Sep 2024 00:03:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E6=9C=89=E7=A8=8B=E5=BA=8F=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20BBT=20=E8=BF=9E=E6=8E=A5=E5=99=A8=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E6=97=B6=EF=BC=8C=E6=98=BE=E7=A4=BA=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biglybt/PBHClientIDGenerator.java | 52 +++++++++++++++++++ .../downloaderplug/biglybt/Plugin.java | 21 ++++++-- .../biglybt/network/ConnectorData.java | 14 +++++ src/main/resources/plugin.properties | 2 +- 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/PBHClientIDGenerator.java create mode 100644 src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/network/ConnectorData.java diff --git a/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/PBHClientIDGenerator.java b/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/PBHClientIDGenerator.java new file mode 100644 index 0000000..0d070c3 --- /dev/null +++ b/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/PBHClientIDGenerator.java @@ -0,0 +1,52 @@ +package com.ghostchu.peerbanhelper.downloaderplug.biglybt; + +import com.biglybt.pif.clientid.ClientIDException; +import com.biglybt.pif.clientid.ClientIDGenerator; +import lombok.Getter; + +import java.util.Objects; +import java.util.Properties; +@Getter +public class PBHClientIDGenerator implements ClientIDGenerator { + private final static String APPEND_USER_AGENT_TEMPLATE = ";%s/%s (%s)"; + private final static String APPEND_CLIENT_NAME_TEMPLATE = " (🛡%s/%s)"; + private final ClientIDGenerator parent; + private final Plugin plugin; + + public PBHClientIDGenerator(Plugin plugin, ClientIDGenerator parent) { + this.plugin = plugin; + this.parent = parent; + } + + @Override + public byte[] generatePeerID(byte[] hash, boolean for_tracker) throws ClientIDException { + return parent.generatePeerID(hash, for_tracker); + } + + @Override + public void generateHTTPProperties(byte[] hash, Properties properties) throws ClientIDException { + parent.generateHTTPProperties(hash, properties); + var userAgent = properties.get(ClientIDGenerator.PR_USER_AGENT); + var connectorData = plugin.getConnectorData(); + if(connectorData == null) return; + var ourUserAgent = String.format(APPEND_USER_AGENT_TEMPLATE, connectorData.getSoftware(), connectorData.getVersion(), connectorData.getAbbrev()); + userAgent += ourUserAgent; + properties.put(ClientIDGenerator.PR_USER_AGENT, userAgent); + } + + @Override + public String[] filterHTTP(byte[] hash, String[] lines_in) { + return lines_in; + } + + @Override + public Object getProperty(byte[] hash, String property_name) { + var def = parent.getProperty(hash, property_name); + if (Objects.equals(property_name, ClientIDGenerator.PR_CLIENT_NAME)) { + var connectorData = plugin.getConnectorData(); + if(connectorData == null) return def; + return def + String.format(APPEND_CLIENT_NAME_TEMPLATE, connectorData.getSoftware(), connectorData.getVersion()); + } + return def; + } +} diff --git a/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/Plugin.java b/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/Plugin.java index b865e27..4ddcd48 100644 --- a/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/Plugin.java +++ b/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/Plugin.java @@ -6,7 +6,7 @@ import com.biglybt.pif.PluginException; import com.biglybt.pif.PluginInterface; import com.biglybt.pif.UnloadablePlugin; -import com.biglybt.pif.config.PluginConfigSource; +import com.biglybt.pif.clientid.ClientIDGenerator; import com.biglybt.pif.download.Download; import com.biglybt.pif.download.DownloadException; import com.biglybt.pif.download.DownloadStats; @@ -19,7 +19,9 @@ import com.biglybt.pif.ui.config.IntParameter; import com.biglybt.pif.ui.config.StringParameter; import com.biglybt.pif.ui.model.BasicPluginConfigModel; +import com.biglybt.pifimpl.local.clientid.ClientIDManagerImpl; import com.biglybt.pifimpl.local.peers.PeerImpl; +import com.ghostchu.peerbanhelper.downloaderplug.biglybt.network.ConnectorData; import com.ghostchu.peerbanhelper.downloaderplug.biglybt.network.bean.clientbound.BanBean; import com.ghostchu.peerbanhelper.downloaderplug.biglybt.network.bean.clientbound.BanListReplacementBean; import com.ghostchu.peerbanhelper.downloaderplug.biglybt.network.bean.clientbound.UnBanBean; @@ -30,6 +32,7 @@ import io.javalin.Javalin; import io.javalin.http.Context; import io.javalin.http.HttpStatus; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import java.util.*; @@ -39,6 +42,7 @@ import java.util.stream.Collectors; @Slf4j +@Getter public class Plugin implements UnloadablePlugin { public static final Gson GSON = new Gson(); private static final String PBH_IDENTIFIER = ""; @@ -51,7 +55,8 @@ public class Plugin implements UnloadablePlugin { private int port; private String token; private PluginConfig cfg; - private PluginConfigSource configSource; + private ConnectorData connectorData; + private ClientIDGenerator clientIDGeneratorOriginal; private static TorrentRecord getTorrentRecord(Torrent torrent) { if (torrent == null) return null; @@ -142,6 +147,9 @@ public void unload() throws PluginException { if (webContainer != null) { webContainer.stop(); } + if(clientIDGeneratorOriginal != null){ + ClientIDManagerImpl.getSingleton().setGenerator(clientIDGeneratorOriginal, true); + } } @Override @@ -162,6 +170,8 @@ public void initialize(PluginInterface pluginInterface) { saveAndReload(); }); saveAndReload(); + clientIDGeneratorOriginal = ClientIDManagerImpl.getSingleton().getGenerator(); + ClientIDManagerImpl.getSingleton().setGenerator(new PBHClientIDGenerator(this, clientIDGeneratorOriginal), true); } private void saveAndReload() { @@ -191,6 +201,7 @@ private void reloadPlugin() { private void initEndpoints(Javalin javalin) { javalin.get("/metadata", this::handleMetadata) + .post("/setconnector", this::handleSetConnector) .get("/statistics", this::handleStatistics) .get("/downloads", this::handleDownloads) .get("/download/{infoHash}", this::handleDownload) @@ -201,6 +212,10 @@ private void initEndpoints(Javalin javalin) { .delete("/bans", this::handleBatchUnban); } + private void handleSetConnector(Context context) { + this.connectorData = context.bodyAsClass(ConnectorData.class); + } + private void handleStatistics(Context context) { var stats = pluginInterface.getDownloadManager().getStats(); context.json( @@ -326,7 +341,7 @@ public void handlePeers(Context ctx) { ctx.status(HttpStatus.NOT_FOUND); return; } - if(download.getPeerManager() == null){ + if (download.getPeerManager() == null) { ctx.status(HttpStatus.NOT_FOUND); return; } diff --git a/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/network/ConnectorData.java b/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/network/ConnectorData.java new file mode 100644 index 0000000..61373ed --- /dev/null +++ b/src/main/java/com/ghostchu/peerbanhelper/downloaderplug/biglybt/network/ConnectorData.java @@ -0,0 +1,14 @@ +package com.ghostchu.peerbanhelper.downloaderplug.biglybt.network; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ConnectorData { + private String software; + private String version; + private String abbrev; +} diff --git a/src/main/resources/plugin.properties b/src/main/resources/plugin.properties index 3975747..0d46f1e 100644 --- a/src/main/resources/plugin.properties +++ b/src/main/resources/plugin.properties @@ -2,4 +2,4 @@ plugin.class=com.ghostchu.peerbanhelper.downloaderplug.biglybt.Plugin plugin.name=PeerBanHelper-Adapter plugin.langfile=com.ghostchu.peerbanhelper.downloaderplug.biglybt.Messages plugin.id=peerbanhelperadatper -plugin.version=1.2.5 +plugin.version=1.2.6