Skip to content

Commit

Permalink
Merge pull request #197 from PBH-BTN/master
Browse files Browse the repository at this point in the history
v4.3.0
  • Loading branch information
Ghost-chu authored Jun 28, 2024
2 parents ac79b3b + 53c8949 commit 0c83123
Show file tree
Hide file tree
Showing 37 changed files with 2,030 additions and 82 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<groupId>com.ghostchu.peerbanhelper</groupId>
<artifactId>peerbanhelper</artifactId>
<version>4.2.4</version>
<version>4.3.0</version>
<packaging>takari-jar</packaging>

<name>PeerBanHelper</name>

<properties>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>com.ghostchu.peerbanhelper.Main</mainClass>
<mainClass>com.ghostchu.peerbanhelper.MainJumpLoader</mainClass>
<maven.build.timestamp.format>yyyyMMdd-HHmmss</maven.build.timestamp.format>
<flatlafVersion>3.4.1</flatlafVersion>
<hutool.version>5.8.27</hutool.version>
Expand Down Expand Up @@ -485,5 +485,11 @@
<artifactId>libby-standalone</artifactId> <!-- Replace bukkit if you're using another platform -->
<version>2.0.2-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
</dependencies>
</project>
47 changes: 47 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/MainJumpLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.ghostchu.peerbanhelper;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MainJumpLoader {
public static void main(String[] args) {
// Do something before real Main class
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
setupCharsets();
}
Main.main(args);
}

private static void setupCharsets() {
try {
invokeCommand("cmd.exe /c chcp 65001", Collections.emptyMap());
} catch (Exception e) {
e.printStackTrace();
}
}

public static int invokeCommand(String command, Map<String, String> env) throws IOException, ExecutionException, InterruptedException, TimeoutException {
StringTokenizer st = new StringTokenizer(command);
String[] cmdarray = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++) {
cmdarray[i] = st.nextToken();
}
ProcessBuilder builder = new ProcessBuilder(cmdarray)
.inheritIO();
Map<String, String> liveEnv = builder.environment();
liveEnv.putAll(env);
Process p = builder.start();
Process process = p.onExit().get(10, TimeUnit.SECONDS);
if (process.isAlive()) {
process.destroy();
return -9999;
}
return process.exitValue();
}

}
13 changes: 10 additions & 3 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ghostchu.peerbanhelper.downloader.Downloader;
import com.ghostchu.peerbanhelper.downloader.DownloaderLastStatus;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.BiglyBT;
import com.ghostchu.peerbanhelper.downloader.impl.deluge.Deluge;
import com.ghostchu.peerbanhelper.downloader.impl.qbittorrent.QBittorrent;
import com.ghostchu.peerbanhelper.downloader.impl.transmission.Transmission;
import com.ghostchu.peerbanhelper.event.LivePeersUpdatedEvent;
Expand Down Expand Up @@ -168,6 +169,7 @@ public Downloader createDownloader(String client, ConfigurationSection downloade
case "transmission" ->
downloader = Transmission.loadFromConfig(client, pbhServerAddress, downloaderSection);
case "biglybt" -> downloader = BiglyBT.loadFromConfig(client, downloaderSection);
case "deluge" -> downloader = Deluge.loadFromConfig(client, downloaderSection);
}
return downloader;

Expand All @@ -180,6 +182,7 @@ public Downloader createDownloader(String client, JsonObject downloaderSection)
case "transmission" ->
downloader = Transmission.loadFromConfig(client, pbhServerAddress, downloaderSection);
case "biglybt" -> downloader = BiglyBT.loadFromConfig(client, downloaderSection);
case "deluge" -> downloader = Deluge.loadFromConfig(client, downloaderSection);
}
return downloader;

Expand Down Expand Up @@ -433,7 +436,7 @@ public void banWave() {
});
});

needRelaunched.put(downloader, relaunch);
needRelaunched.put(downloader, relaunch);
} catch (Exception e) {
log.error("Unable to complete peer ban task, report to PBH developer!!!");
}
Expand Down Expand Up @@ -573,8 +576,12 @@ public Map<Downloader, Map<Torrent, List<Peer>>> collectPeers() {
Map<Downloader, Map<Torrent, List<Peer>>> peers = new HashMap<>();
try (var service = Executors.newVirtualThreadPerTaskExecutor()) {
downloaders.forEach(downloader -> service.submit(() -> {
Map<Torrent, List<Peer>> p = collectPeers(downloader);
peers.put(downloader, p);
try {
Map<Torrent, List<Peer>> p = collectPeers(downloader);
peers.put(downloader, p);
} catch (Exception e) {
log.warn(Lang.DOWNLOADER_UNHANDLED_EXCEPTION, e);
}
}));
}
return peers;
Expand Down
40 changes: 6 additions & 34 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ghostchu.peerbanhelper.util.rule.MatchResult;
import com.ghostchu.peerbanhelper.util.rule.Rule;
import com.ghostchu.peerbanhelper.util.rule.RuleParser;
import com.ghostchu.peerbanhelper.util.rule.matcher.IPMatcher;
import inet.ipaddr.IPAddress;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -69,13 +70,7 @@ public String matcherIdentifier() {

public Map<String, List<Rule>> parseIPRule(Map<String, List<String>> raw) {
Map<String, List<Rule>> rules = new HashMap<>();
raw.forEach((k, v) -> {
List<Rule> addresses = new ArrayList<>();
for (String s : v) {
addresses.add(new BtnRuleIpMatcher(IPAddressUtil.getIPAddress(s)));
}
rules.put(k, addresses);
});
raw.forEach((k, v) -> rules.put(k, List.of(new BtnRuleIpMatcher(k, k, v.stream().map(IPAddressUtil::getIPAddress).toList()))));
return rules;
}

Expand All @@ -85,37 +80,14 @@ public Map<String, List<Rule>> parseRule(Map<String, List<String>> raw) {
return rules;
}

public static class BtnRuleIpMatcher implements Rule {
private IPAddress ipAddress;

public BtnRuleIpMatcher(IPAddress ipAddress) {
this.ipAddress = ipAddress;
if (this.ipAddress.isIPv4Convertible()) {
this.ipAddress = this.ipAddress.toIPv4();
}
}
public static class BtnRuleIpMatcher extends IPMatcher {

@Override
public @NotNull MatchResult match(@NotNull String content) {
Main.getServer().getHitRateMetric().addQuery(this);
IPAddress contentAddr = IPAddressUtil.getIPAddress(content);
if (contentAddr.isIPv4Convertible()) {
contentAddr = contentAddr.toIPv4();
}
MatchResult result = (ipAddress.contains(contentAddr) || ipAddress.equals(contentAddr)) ? MatchResult.TRUE : MatchResult.DEFAULT;
if (result != MatchResult.DEFAULT) {
Main.getServer().getHitRateMetric().addHit(this);
}
return result;
}

@Override
public Map<String, Object> metadata() {
return Map.of("ip", this.ipAddress.toString());
public BtnRuleIpMatcher(String ruleId, String ruleName, List<IPAddress> ruleData) {
super(ruleId, ruleName, ruleData);
}

@Override
public String matcherName() {
public @NotNull String matcherName() {
return "BTN-IP";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ private void validate() {
}
}

@UpdateScript(version = 9)
public void firewallIntegration() {
conf.set("firewall-integration", null);
conf.set("firewall-integration.windows-adv-firewall", true);
}

@UpdateScript(version = 8)
public void webToken() {
conf.set("server.token", UUID.randomUUID().toString());
Expand Down
Loading

0 comments on commit 0c83123

Please sign in to comment.