Skip to content

Commit

Permalink
Merge pull request #262 from PBH-BTN/master
Browse files Browse the repository at this point in the history
v5.0.3
  • Loading branch information
Ghost-chu authored Jul 18, 2024
2 parents 1ba7c2d + 856fcb6 commit 955b02a
Show file tree
Hide file tree
Showing 26 changed files with 158 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/jvm-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
type=raw,ci
type=sha
- name: Build and push Docker image
uses: docker/[email protected].0
uses: docker/[email protected].1
with:
context: .
file: ./Dockerfile-CI
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/jvm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
type=raw,latest
type=sha
- name: Build and push Docker image
uses: docker/[email protected].0
uses: docker/[email protected].1
with:
context: .
file: ./Dockerfile
Expand Down Expand Up @@ -114,7 +114,7 @@ jobs:
type=raw,latest
type=sha
- name: Build and push Aliyun ACR
uses: docker/[email protected].0
uses: docker/[email protected].1
with:
context: .
file: ./Dockerfile
Expand Down
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
> [!TIP]
> 您只需要正确连接 PBH 到下载器就可以正常工作,大多数情况下,并不需要额外配置
> [!TIP]
> 为获得最佳效果,建议配合我们维护的 IP 规则库 [PBH-BTN/BTN-Collected-Rules](https://github.com/PBH-BTN/BTN-Collected-Rules)[BTN 网络](https://github.com/PBH-BTN/PeerBanHelper/wiki/BTN-%E7%BD%91%E7%BB%9C) 一起食用,不过这是完全可选的。
| 主界面 | 封禁列表 | 封禁日志 | 封禁统计 | 规则统计 | 规则订阅 |
|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| <img width="1280" alt="homepage" src="https://github.com/PBH-BTN/PeerBanHelper/assets/19235246/d7f7ea9f-70df-40f1-a782-260450972bc9"> | <img width="1280" alt="banlist" src="https://github.com/PBH-BTN/PeerBanHelper/assets/19235246/c3e139e6-eb82-423f-b083-1839713ec801"> | <img width="1280" alt="banlogs" src="https://github.com/PBH-BTN/PeerBanHelper/assets/19235246/00d8efcc-0dd7-4e05-bdeb-9444e14739d6"> | <img width="1280" alt="maxban" src="https://github.com/PBH-BTN/PeerBanHelper/assets/30802565/ae78ebb9-67f7-481a-9afc-7ced2c6a2534"> | <img width="1280" alt="banMetrics" src="https://github.com/PBH-BTN/PeerBanHelper/assets/19235246/9e4cd7b7-aaff-4b66-8d1d-ad4ef3466b1f"> | <img width="1280" alt="banMetrics" src="https://github.com/PBH-BTN/PeerBanHelper/assets/19235246/dc312186-9643-4f23-9d53-7b8e0852f228"> |
Expand Down Expand Up @@ -318,26 +321,16 @@ client:

![image](https://github.com/Ghost-chu/PeerBanHelper/assets/30802565/20d49093-bf99-41f6-971f-c0c574d493af)

### 非本机(例如:NAS上、Docker里)的 PBH 的 WebUI 打不开怎么办?

打开 `config.yml`,找到 `address`,它的默认值是 `127.0.0.1`(只有PBH所在机器可以访问),请更改为 `0.0.0.0` 开启外部访问。
**对于下面的 prefix 选项,除非你知道那是干什么的,否则请保持默认,或者询问其它用户**

### 管理 Token 在哪里?

在 config.yml 中:
您可以从 GUI 界面的 WebUI->复制Token 获得 Token。通过 GUI 唤起浏览器打开的 WebUI 将自动填写 Token。如果是首次使用,也会在日志中显示您的 Token。
除此之外,您还可以从 config.yml 中找到 Token ↓

```yaml
# Http 服务器设置
server:
# 监听端口
http: 9898
# 客户端远程 URL 设置
# Docker 网络请改 host 模式使用或者设置容器端口暴露
# 当客户端需要与 PBH 通信时,客户端的 URL 会被更改为 http://<address>:<http-port>/<client-api-route>
address: 127.0.0.1
# 在 PBH 需要给下载器传递地址时,将使用此地址传递,请确保此地址最终可被下载器访问,请【不要】以 / 结尾
prefix: http://127.0.0.1:9898
...<省略>...
token: "*************************" # <-- 你的管理 Token
```

Expand Down
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>peerbanhelper</artifactId>
<version>5.0.2</version>
<version>5.0.3</version>
<packaging>takari-jar</packaging>

<name>PeerBanHelper</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ private void registerModules() {
//moduleManager.register(new ActiveProbing(this, profile));
moduleManager.register(AutoRangeBan.class);
moduleManager.register(BtnNetworkOnline.class);
moduleManager.register(DownloaderCIDRBlockList.class);
moduleManager.register(BlockListController.class);
moduleManager.register(IPBlackRuleList.class);
moduleManager.register(PBHMetricsController.class);
moduleManager.register(PBHBanController.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public List<Peer> getPeers(Torrent torrent) {
for (PeerRecord peer : peerManagerRecord.getPeers()) {
peersList.add(new PeerImpl(
new PeerAddress(peer.getIp(), peer.getPort()),
peer.getIp(),
new String(Base64.getDecoder().decode(peer.getPeerIdBase64()), StandardCharsets.ISO_8859_1),
peer.getClient(),
peer.getStats().getRtDownloadSpeed(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public final class DelugePeer implements Peer {
private long uploadSpeed;
private double progress;
private PeerFlag flags;

@Override
public String getRawIp() {
return peerAddress.getIp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class QBPeer implements Peer {
@SerializedName("uploaded")
private Long uploaded;
private transient PeerAddress peerAddress;
private String rawIp;

public QBPeer() {
}
Expand Down Expand Up @@ -97,6 +98,11 @@ public PeerFlag getFlags() {
return new PeerFlag(flags);
}

@Override
public String getRawIp() {
return rawIp == null ? ip : rawIp;
}

@Override
public String toString() {
return "SingleTorrentPeer{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.ghostchu.peerbanhelper.torrent.Torrent;
import com.ghostchu.peerbanhelper.torrent.TorrentImpl;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.IPAddressUtil;
import com.ghostchu.peerbanhelper.util.JsonUtil;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.ghostchu.peerbanhelper.wrapper.PeerAddress;
Expand All @@ -21,7 +20,6 @@
import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import inet.ipaddr.IPAddress;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -256,25 +254,25 @@ public List<Peer> getPeers(Torrent torrent) {
qbPeer.setClient(mid);
}
}
qbPeer.setRawIp(s);
peersList.add(qbPeer);
}
return peersList;
}

private void setBanListIncrement(Collection<BanMetadata> added) {
added.forEach(meta -> {
IPAddress ipAddress = IPAddressUtil.getIPAddress(meta.getPeer().getAddress().getIp());
String peers;
if (ipAddress.isIPv4()) {
peers = meta.getPeer().getAddress().getIp() + ":" + meta.getPeer().getAddress().getPort();
} else {
peers = "[" + meta.getPeer().getAddress().getIp() + "]" + ":" + meta.getPeer().getAddress().getPort();
}
Map<String, StringJoiner> banTasks = new HashMap<>();
added.forEach(p -> {
StringJoiner joiner = banTasks.getOrDefault(p.getTorrent().getHash(), new StringJoiner("|"));
joiner.add(p.getPeer().getRawIp());
banTasks.put(p.getTorrent().getHash(), joiner);
});
banTasks.forEach((hash, peers) -> {
try {
HttpResponse<String> request = httpClient.send(MutableRequest
.POST(apiEndpoint + "/transfer/banPeers", FormBodyPublisher.newBuilder()
.query("hash", meta.getTorrent().getHash())
.query("peers", peers).build())
.query("hash", hash)
.query("peers", peers.toString()).build())
.header("Content-Type", "application/x-www-form-urlencoded")
, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
if (request.statusCode() != 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public PeerFlag getFlags() {
return new PeerFlag(backend.getFlagStr());
}

@Override
public String getRawIp() {
return backend.getAddress();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Transmission(String name, String blocklistUrl, Config config) {
}

private static String generateBlocklistUrl(String pbhServerAddress) {
return pbhServerAddress + "/blocklist/transmission";
return pbhServerAddress + "/blocklist/p2p-plain-format";
}

public static Transmission loadFromConfig(String name, String pbhServerAddress, ConfigurationSection section) {
Expand Down Expand Up @@ -115,6 +115,7 @@ public String getType() {
return "Transmission";
}

@SneakyThrows(InterruptedException.class)
@Override
public DownloaderLoginResult login() {
RqSessionGet get = new RqSessionGet();
Expand All @@ -123,6 +124,17 @@ public DownloaderLoginResult login() {
if (version.startsWith("0.") || version.startsWith("1.") || version.startsWith("2.")) {
return new DownloaderLoginResult(DownloaderLoginResult.Status.EXCEPTION, new TranslationComponent(Lang.DOWNLOADER_TR_KNOWN_INCOMPATIBILITY, "API Version"));
}
if (!resp.getArgs().getBlocklistEnabled() || !resp.getArgs().getBlocklistUrl().startsWith(blocklistUrl)) {
RqSessionSet set = RqSessionSet.builder()
.blocklistUrl(blocklistUrl + "?t=" + System.currentTimeMillis()) // 更改 URL 来确保更改生效
.blocklistEnabled(true)
.build();
TypedResponse<RsSessionGet> sessionSetResp = client.execute(set);
if (!sessionSetResp.isSuccess()) {
log.error(tlUI(Lang.DOWNLOADER_TR_INCORRECT_BANLIST_API_RESP), sessionSetResp.getResult());
}
Thread.sleep(3000);
}
return new DownloaderLoginResult(DownloaderLoginResult.Status.SUCCESS, new TranslationComponent(Lang.STATUS_TEXT_OK));
}

Expand All @@ -145,15 +157,6 @@ public List<Peer> getPeers(Torrent torrent) {
@SneakyThrows
@Override
public void setBanList(Collection<PeerAddress> fullList, @Nullable Collection<BanMetadata> added, @Nullable Collection<BanMetadata> removed) {
RqSessionSet set = RqSessionSet.builder()
.blocklistUrl(blocklistUrl + "?t=" + System.currentTimeMillis()) // 更改 URL 来确保更改生效
.blocklistEnabled(true)
.build();
TypedResponse<RsSessionGet> sessionSetResp = client.execute(set);
if (!sessionSetResp.isSuccess()) {
log.error(tlUI(Lang.DOWNLOADER_TR_INCORRECT_BANLIST_API_RESP), sessionSetResp.getResult());
}
Thread.sleep(3000); // Transmission 在这里疑似有崩溃问题?
RqBlockList updateBlockList = new RqBlockList();
TypedResponse<RsBlockList> updateBlockListResp = client.execute(updateBlockList);
if (!updateBlockListResp.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ghostchu.peerbanhelper.torrent.Torrent;
import com.ghostchu.peerbanhelper.web.JavalinWebContainer;
import com.ghostchu.peerbanhelper.web.Role;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.ghostchu.peerbanhelper.wrapper.PeerAddress;
import inet.ipaddr.IPAddress;
import io.javalin.http.Context;
Expand Down Expand Up @@ -85,7 +86,8 @@ private void reloadConfig() {
if (peerAddress.isIPv4Convertible()) {
peerAddress = peerAddress.toIPv4();
}
for (PeerAddress bannedPeer : getServer().getBannedPeers().keySet()) {
for (Map.Entry<PeerAddress, BanMetadata> bannedPeerEntry : getServer().getBannedPeers().entrySet()) {
PeerAddress bannedPeer = bannedPeerEntry.getKey();
IPAddress bannedAddress = bannedPeer.getAddress().withoutPrefixLength();
if (bannedAddress.isIPv4Convertible()) {
bannedAddress = bannedAddress.toIPv4();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ghostchu.peerbanhelper.module.impl.webapi;

import com.ghostchu.peerbanhelper.module.AbstractFeatureModule;
import com.ghostchu.peerbanhelper.util.IPAddressUtil;
import com.ghostchu.peerbanhelper.web.JavalinWebContainer;
import com.ghostchu.peerbanhelper.web.Role;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.ghostchu.peerbanhelper.wrapper.PeerAddress;
import inet.ipaddr.IPAddress;
import io.javalin.http.HttpStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -17,7 +19,7 @@
import java.util.UUID;

@Component
public class DownloaderCIDRBlockList extends AbstractFeatureModule {
public class BlockListController extends AbstractFeatureModule {
@Autowired
private JavalinWebContainer webContainer;
@Override
Expand All @@ -28,7 +30,7 @@ public boolean isConfigurable() {
@Override
public void onEnable() {
webContainer.javalin()
.get("/blocklist/transmission", ctx -> {
.get("/blocklist/p2p-plain-format", ctx -> {
StringBuilder builder = new StringBuilder();
for (Map.Entry<PeerAddress, BanMetadata> pair : getServer().getBannedPeers().entrySet()) {
String ruleName = UUID.randomUUID().toString().replace("-", "");
Expand All @@ -38,6 +40,17 @@ public void onEnable() {
}
ctx.status(HttpStatus.OK);
ctx.result(builder.toString());
}, Role.ANYONE)
.get("/blocklist/dat-emule", ctx -> {
StringBuilder builder = new StringBuilder();
for (Map.Entry<PeerAddress, BanMetadata> pair : getServer().getBannedPeers().entrySet()) {
IPAddress ipAddress = IPAddressUtil.getIPAddress(pair.getKey().getIp());
if (ipAddress == null) continue;
String fullIp = ipAddress.toFullString();
builder.append(fullIp).append(" - ").append(fullIp).append(" , 000 , ").append(UUID.randomUUID().toString().replace("-", "")).append("\n");
}
ctx.status(HttpStatus.OK);
ctx.result(builder.toString());
}, Role.ANYONE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private void handleDownloaderTorrents(@NotNull Context ctx, String downloaderNam
List<TorrentWrapper> torrentWrappers = getServer().getLivePeersSnapshot()
.values().stream().filter(p -> p.getDownloader().equals(downloader.getName()))
.map(PeerMetadata::getTorrent)
.distinct()
.sorted((o1, o2) -> Long.compare(o2.getRtUploadSpeed(), o1.getRtUploadSpeed()))
.toList();
ctx.status(HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.ghostchu.peerbanhelper.web.JavalinWebContainer;
import com.ghostchu.peerbanhelper.web.Role;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.ghostchu.peerbanhelper.wrapper.PeerAddress;
import io.javalin.http.Context;
import io.javalin.http.HttpStatus;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -166,8 +167,9 @@ private void handleRules(Context ctx) {
if (obj.getKey().matcherName() != null) {
ruleType = obj.getKey().matcherName();
}
dict.put(obj.getKey().matcherIdentifier(), tl(locale, ruleType));
return new RuleData(obj.getKey().matcherIdentifier(), obj.getValue().getHitCounter(), obj.getValue().getQueryCounter(), obj.getKey().metadata());
// 返回特定计算值作为字典键,这样不需要修改前端
dict.put(tl(locale, ruleType), tl(locale, ruleType));
return new RuleData(tl(locale, ruleType), obj.getValue().getHitCounter(), obj.getValue().getQueryCounter(), obj.getKey().metadata());
})
.sorted((o1, o2) -> Long.compare(o2.getHit(), o1.getHit()))
.toList();
Expand All @@ -184,6 +186,7 @@ private void handleBasicCounter(Context ctx) {
map.put("peerBanCounter", metrics.getPeerBanCounter());
map.put("peerUnbanCounter", metrics.getPeerUnbanCounter());
map.put("banlistCounter", getServer().getBannedPeers().size());
map.put("bannedIpCounter", getServer().getBannedPeers().keySet().stream().map(PeerAddress::getIp).distinct().count());
ctx.status(HttpStatus.OK);
ctx.json(map);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/peer/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ default String getCacheKey() {
//return "pa=" + this.getPeerAddress().toString() + ",pid=" + this.getPeerId() + ",pname=" + this.getClientName();
return getPeerAddress().getIp() + ':' + getPeerAddress().getPort();
}

/**
* 获取该 Peer 的原始 IP 表示法,用于返回给下载器封禁 Peer
*
* @return 原始IP
*/
String getRawIp();
}
9 changes: 8 additions & 1 deletion src/main/java/com/ghostchu/peerbanhelper/peer/PeerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@Setter
public class PeerImpl implements Peer {
private PeerAddress peerAddress;
private String rawIp;
private String peerId;
private String clientName;
private long downloadSpeed;
Expand All @@ -15,8 +16,9 @@ public class PeerImpl implements Peer {
private double progress;
private PeerFlag flags;

public PeerImpl(PeerAddress peerAddress, String peerId, String clientName, long downloadSpeed, long downloaded, long uploadSpeed, long uploaded, double progress, PeerFlag flags) {
public PeerImpl(PeerAddress peerAddress, String rawIp, String peerId, String clientName, long downloadSpeed, long downloaded, long uploadSpeed, long uploaded, double progress, PeerFlag flags) {
this.peerAddress = peerAddress;
this.rawIp = rawIp;
this.peerId = peerId;
this.clientName = clientName;
this.downloadSpeed = downloadSpeed;
Expand Down Expand Up @@ -71,4 +73,9 @@ public double getProgress() {
public PeerFlag getFlags() {
return flags;
}

@Override
public String getRawIp() {
return rawIp;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/ghostchu/peerbanhelper/text/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ public enum Lang {
USER_SCRIPT_RULE,
USER_MANUALLY_BAN_RULE,
USER_MANUALLY_BAN_REASON,
SCHEDULED_OPERATIONS
SCHEDULED_OPERATIONS,
ARB_BANNED_REASON

;

Expand Down
Loading

0 comments on commit 955b02a

Please sign in to comment.