Skip to content

Commit

Permalink
替换 banlist 为内部实现,并添加一个封禁计数器
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Dec 29, 2024
1 parent 470962c commit 0167629
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 64 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
<artifactId>slf4j-simple</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId>
<version>5.5.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.biglybt</groupId>-->
<!-- <artifactId>biglybt-core</artifactId>-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.ghostchu.peerbanhelper.downloaderplug.biglybt;

import com.biglybt.core.config.COConfigurationManager;
import com.biglybt.core.networkmanager.NetworkConnectionFactory;
import com.biglybt.core.networkmanager.Transport;
import com.biglybt.pif.PluginConfig;
import com.biglybt.pif.PluginException;
import com.biglybt.pif.PluginInterface;
import com.biglybt.pif.UnloadablePlugin;
import com.biglybt.pif.clientid.ClientIDGenerator;
Expand All @@ -19,6 +18,7 @@
import com.biglybt.pif.torrent.Torrent;
import com.biglybt.pif.ui.config.BooleanParameter;
import com.biglybt.pif.ui.config.IntParameter;
import com.biglybt.pif.ui.config.LabelParameter;
import com.biglybt.pif.ui.config.StringParameter;
import com.biglybt.pif.ui.model.BasicPluginConfigModel;
import com.biglybt.pifimpl.local.clientid.ClientIDManagerImpl;
Expand All @@ -31,6 +31,8 @@
import com.ghostchu.peerbanhelper.downloaderplug.biglybt.network.bean.serverbound.MetadataCallbackBean;
import com.ghostchu.peerbanhelper.downloaderplug.biglybt.network.wrapper.*;
import com.google.gson.Gson;
import inet.ipaddr.IPAddressString;
import inet.ipaddr.format.util.DualIPv4v6Tries;
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.HttpStatus;
Expand All @@ -39,6 +41,7 @@

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
Expand All @@ -61,6 +64,9 @@ public class Plugin implements UnloadablePlugin {
private ClientIDGenerator clientIDGeneratorOriginal;
private boolean useClientIdModifier;
private BooleanParameter clientIdModifier;
private final DualIPv4v6Tries banList = new DualIPv4v6Tries();
private final AtomicLong connectionBlockCounter = new AtomicLong(0);
private LabelParameter connectionBlockCounterLabel;

private static TorrentRecord getTorrentRecord(Torrent torrent) {
if (torrent == null) return null;
Expand Down Expand Up @@ -126,28 +132,8 @@ public static byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}

public void runIPFilterOperation(Runnable runnable) throws IPFilterException {
BAN_LIST_OPERATION_LOCK.lock();
try {
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 {
COConfigurationManager.setParameter("Ip Filter Banning Persistent", originalPersistentSetting);
if (originalPersistentSetting) {
this.pluginInterface.getIPFilter().save();
}
}
} finally {
BAN_LIST_OPERATION_LOCK.unlock();
}
}

@Override
public void unload() throws PluginException {
public void unload() {
if (webContainer != null) {
webContainer.stop();
}
Expand Down Expand Up @@ -179,9 +165,24 @@ public void initialize(PluginInterface pluginInterface) {
this.useClientIdModifier = clientIdModifier.getValue();
saveAndReload();
});
connectionBlockCounterLabel = configModel.addLabelParameter2("peerbanhelper.connectionBlockCounter");
updateCounterLabel();
saveAndReload();
clientIDGeneratorOriginal = ClientIDManagerImpl.getSingleton().getGenerator();
ClientIDManagerImpl.getSingleton().setGenerator(new PBHClientIDGenerator(this, clientIDGeneratorOriginal), true);
NetworkConnectionFactory.addListener(networkConnection -> {
var sockAddress = networkConnection.getEndpoint().getNotionalAddress();
var ip = sockAddress.getAddress().getHostAddress();
if (banList.elementContains(new IPAddressString(ip).getAddress())) {
networkConnection.close("IP Blocked by PeerBanHelper");
connectionBlockCounter.incrementAndGet();
updateCounterLabel();
}
});
}

private void updateCounterLabel() {
connectionBlockCounterLabel.setLabelText("Blocked " + connectionBlockCounter.get() + " connect attempts since last restart.");
}

private void saveAndReload() {
Expand Down Expand Up @@ -259,22 +260,19 @@ private void handleMetadata(Context context) {
context.json(callbackBean);
}

private void handleBanListApplied(Context context) throws IPFilterException {
private void handleBanListApplied(Context context) {
BanBean banBean = context.bodyAsClass(BanBean.class);
IPFilter ipFilter = pluginInterface.getIPFilter();
AtomicInteger success = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
runIPFilterOperation(() -> {
for (String s : banBean.getIps()) {
try {
ipFilter.ban(s, PBH_IDENTIFIER);
success.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
for (String s : banBean.getIps()) {
try {
banList.add(new IPAddressString(s).getAddress());
success.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
});
}
cleanupPeers(banBean.getIps());
context.status(HttpStatus.OK);
context.json(new BatchOperationCallbackBean(success.get(), failed.get()));
Expand Down Expand Up @@ -320,17 +318,15 @@ private void handleBatchUnban(Context ctx) throws IPFilterException {
UnBanBean banBean = ctx.bodyAsClass(UnBanBean.class);
AtomicInteger unbanned = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
runIPFilterOperation(() -> {
for (String ip : banBean.getIps()) {
try {
pluginInterface.getIPFilter().unban(ip);
unbanned.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
for (String ip : banBean.getIps()) {
try {
banList.remove(new IPAddressString(ip).getAddress());
unbanned.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
});
}
ctx.status(HttpStatus.OK);
ctx.json(new BatchOperationCallbackBean(unbanned.get(), failed.get()));
}
Expand Down Expand Up @@ -370,27 +366,22 @@ public void handlePeers(Context ctx) {
}
}

public void handleBanListReplacement(Context ctx) throws IPFilterException {
public void handleBanListReplacement(Context ctx) {
BanListReplacementBean replacementBean = ctx.bodyAsClass(BanListReplacementBean.class);
AtomicInteger success = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
runIPFilterOperation(() -> {
IPFilter ipFilter = pluginInterface.getIPFilter();
for (IPBanned blockedIP : ipFilter.getBannedIPs()) {
if (PBH_IDENTIFIER.equals(blockedIP.getBannedTorrentName()) || replacementBean.isIncludeNonPBHEntries()) {
ipFilter.unban(blockedIP.getBannedIP());
}
}
for (String s : replacementBean.getReplaceWith()) {
try {
ipFilter.ban(s, PBH_IDENTIFIER);
success.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
IPFilter ipFilter = pluginInterface.getIPFilter();
banList.getIPv4Trie().clear();
banList.getIPv6Trie().clear();
for (String s : replacementBean.getReplaceWith()) {
try {
banList.add(new IPAddressString(s).getAddress());
success.incrementAndGet();
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
});
}
cleanupPeers(replacementBean.getReplaceWith());
ctx.status(HttpStatus.OK);
ctx.json(new BatchOperationCallbackBean(success.get(), failed.get()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
peerbanhelper.configui=PeerBanHelper Adapter - Config UI
peerbanhelper.port=Port
peerbanhelper.token=Token
peerbanhelper.clientIdModifier=Use Client ID Modifier (This will add a shield emoji after Client Name like BiglyBT 3.1.0.0 (\ud83d\udee1PeerBanHelper/6.1.4))
peerbanhelper.clientIdModifier=Use Client ID Modifier (This will add a shield emoji after Client Name like BiglyBT 3.1.0.0 (\ud83d\udee1PeerBanHelper/6.1.4))
peerbanhelper.connectionBlockCounter=PeerBanHelper has been blocked {0} malicious connect attempts since last startup.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
peerbanhelper.configui=PeerBanHelper \u9002\u914d\u5668 - \u914d\u7f6e\u754c\u9762
peerbanhelper.port=\u7aef\u53e3
peerbanhelper.token=Token
peerbanhelper.clientIdModifier=\u4f7f\u7528\u5ba2\u6237\u7aef id \u4fee\u6539\uff08\u8fd9\u4f1a\u5728\u5ba2\u6237\u7aef\u540d\u79f0\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5c0f\u76fe\u724c\u56fe\u6807\uff0c\u5982\uff1aBiglyBT 3.1.0.0 \uff08\ud83d\udee1PeerBanHelper/6.1.4\uff09\uff09
peerbanhelper.clientIdModifier=\u4f7f\u7528\u5ba2\u6237\u7aef id \u4fee\u6539\uff08\u8fd9\u4f1a\u5728\u5ba2\u6237\u7aef\u540d\u79f0\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5c0f\u76fe\u724c\u56fe\u6807\uff0c\u5982\uff1aBiglyBT 3.1.0.0 \uff08\ud83d\udee1PeerBanHelper/6.1.4\uff09\uff09
peerbanhelper.connectionBlockCounter=\u81ea\u4e0a\u6b21\u542f\u52a8\u4ee5\u6765\uff0cPeerBanHelper \u5df2\u963b\u6b62\u4e86 {0} \u6b21\u6076\u610f\u8fde\u63a5\u5c1d\u8bd5

0 comments on commit 0167629

Please sign in to comment.