Skip to content

Commit

Permalink
小修小补
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Sep 8, 2024
1 parent cdee70c commit b5b7ee7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 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>BiglyBT-PBH-Plugin</artifactId>
<version>1.2.1</version>
<version>1.2.5</version>
<!-- <packaging>takari-jar</packaging>-->
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.biglybt.core.config.COConfigurationManager;
import com.biglybt.core.networkmanager.Transport;
import com.biglybt.pif.*;
import com.biglybt.pif.PluginConfig;
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.download.Download;
import com.biglybt.pif.download.DownloadException;
Expand All @@ -14,8 +17,6 @@
import com.biglybt.pif.tag.Tag;
import com.biglybt.pif.torrent.Torrent;
import com.biglybt.pif.ui.config.IntParameter;
import com.biglybt.pif.ui.config.Parameter;
import com.biglybt.pif.ui.config.ParameterListener;
import com.biglybt.pif.ui.config.StringParameter;
import com.biglybt.pif.ui.model.BasicPluginConfigModel;
import com.biglybt.pifimpl.local.peers.PeerImpl;
Expand All @@ -36,6 +37,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

@Slf4j
public class Plugin implements UnloadablePlugin {
public static final Gson GSON = new Gson();
Expand Down Expand Up @@ -81,6 +83,7 @@ public static String bytesToHex(byte[] bytes) {

/**
* hex字符串转byte数组
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
Expand All @@ -106,6 +109,7 @@ public static byte[] hexToByteArray(String inHex) {

/**
* Hex字符串转byte
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
Expand All @@ -116,13 +120,15 @@ public static byte hexToByte(String inHex) {
public void runIPFilterOperation(Runnable runnable) throws IPFilterException {
BAN_LIST_OPERATION_LOCK.lock();
try {
var originalPersistentSetting = COConfigurationManager.getBooleanParameter("Ip Filter Banning Persistent" );
var originalPersistentSetting = COConfigurationManager.getBooleanParameter("Ip Filter Banning Persistent");
//originalPersistentSetting = false;
try {
COConfigurationManager.setParameter("Ip Filter Banning Persistent", false);
COConfigurationManager.setParameter("Ip Filter Ban Block Limit", 256);
runnable.run();
}finally {
} finally {
COConfigurationManager.setParameter("Ip Filter Banning Persistent", originalPersistentSetting);
if(originalPersistentSetting){
if (originalPersistentSetting) {
this.pluginInterface.getIPFilter().save();
}
}
Expand All @@ -142,16 +148,16 @@ public void unload() throws PluginException {
public void initialize(PluginInterface pluginInterface) {
this.pluginInterface = pluginInterface;
this.cfg = pluginInterface.getPluginconfig();
this.port = cfg.getPluginIntParameter("web.port",7759);
this.port = cfg.getPluginIntParameter("web.port", 7759);
this.token = cfg.getPluginStringParameter("web.token", UUID.randomUUID().toString());
configModel = pluginInterface.getUIManager().createBasicPluginConfigModel("peerbanhelper.configui");
listenPortParam = configModel.addIntParameter2("api-port", "peerbanhelper.port", port);
listenPortParam.addListener(lis-> {
listenPortParam.addListener(lis -> {
this.port = listenPortParam.getValue();
saveAndReload();
});
accessKeyParam = configModel.addStringParameter2("api-token", "peerbanhelper.token", token);
accessKeyParam.addListener(lis-> {
accessKeyParam.addListener(lis -> {
this.token = accessKeyParam.getValue();
saveAndReload();
});
Expand All @@ -163,7 +169,7 @@ private void saveAndReload() {
cfg.setPluginParameter("web.port", port);
try {
this.cfg.save();
}catch (Exception e){
} catch (Exception e) {
throw new RuntimeException(e);
}
reloadPlugin();
Expand All @@ -175,11 +181,11 @@ private void reloadPlugin() {
}
webContainer = new JavalinWebContainer();
webContainer.start("0.0.0.0",
port,
token);
port,
token);
log.info("PBH-Adapter WebServer started with: port={}, token={}",
port,
token);
token);
initEndpoints(webContainer.javalin());
}

Expand Down Expand Up @@ -232,7 +238,7 @@ private void handleBanListApplied(Context context) throws IPFilterException {
IPFilter ipFilter = pluginInterface.getIPFilter();
AtomicInteger success = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
runIPFilterOperation(()->{
runIPFilterOperation(() -> {
for (String s : banBean.getIps()) {
try {
ipFilter.ban(s, PBH_IDENTIFIER);
Expand Down Expand Up @@ -280,7 +286,7 @@ private void handleBatchUnban(Context ctx) throws IPFilterException {
UnBanBean banBean = ctx.bodyAsClass(UnBanBean.class);
AtomicInteger unbanned = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
runIPFilterOperation(()->{
runIPFilterOperation(() -> {
for (String ip : banBean.getIps()) {
try {
pluginInterface.getIPFilter().unban(ip);
Expand Down Expand Up @@ -320,6 +326,10 @@ public void handlePeers(Context ctx) {
ctx.status(HttpStatus.NOT_FOUND);
return;
}
if(download.getPeerManager() == null){
ctx.status(HttpStatus.NOT_FOUND);
return;
}
ctx.json(getPeerManagerRecord(download.getPeerManager()));
} catch (DownloadException e) {
ctx.status(HttpStatus.NOT_FOUND);
Expand All @@ -330,7 +340,7 @@ public void handleBanListReplacement(Context ctx) throws IPFilterException {
BanListReplacementBean replacementBean = ctx.bodyAsClass(BanListReplacementBean.class);
AtomicInteger success = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
runIPFilterOperation(()->{
runIPFilterOperation(() -> {
IPFilter ipFilter = pluginInterface.getIPFilter();
for (IPBanned blockedIP : ipFilter.getBannedIPs()) {
if (PBH_IDENTIFIER.equals(blockedIP.getBannedTorrentName()) || replacementBean.isIncludeNonPBHEntries()) {
Expand Down Expand Up @@ -464,7 +474,8 @@ private PeerRecord getPeerRecord(Peer peer) {
if (peer instanceof PeerImpl) {
client = ((PeerImpl) peer).getDelegate().getClientNameFromExtensionHandshake();
}
if(peer.getIp().endsWith(".i2p") || peer.getIp().endsWith(".onion")|| peer.getIp().endsWith(".tor")) return null;
if (peer.getIp().endsWith(".i2p") || peer.getIp().endsWith(".onion") || peer.getIp().endsWith(".tor"))
return null;
return new PeerRecord(
peer.isMyPeer(),
peer.getState(),
Expand Down
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.4
plugin.version=1.2.5

0 comments on commit b5b7ee7

Please sign in to comment.