Skip to content

Commit

Permalink
当有程序使用 BBT 连接器连接控制时,显示额外图标
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Sep 11, 2024
1 parent b5b7ee7 commit 3b44907
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.*;
Expand All @@ -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 = "<PeerBanHelper>";
Expand All @@ -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;
Expand Down Expand Up @@ -142,6 +147,9 @@ public void unload() throws PluginException {
if (webContainer != null) {
webContainer.stop();
}
if(clientIDGeneratorOriginal != null){
ClientIDManagerImpl.getSingleton().setGenerator(clientIDGeneratorOriginal, true);
}
}

@Override
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3b44907

Please sign in to comment.