Skip to content

Commit

Permalink
同步 PBH 更改,等待发版
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Aug 19, 2024
1 parent 771e146 commit 2adb93c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static TorrentRecord getTorrentRecord(Torrent torrent) {
if (torrent == null) return null;
return new TorrentRecord(
torrent.getName(),
torrent.getHash() == null ? null : java.util.Base64.getEncoder().encodeToString(torrent.getHash()),
torrent.getHash() == null ? null : bytesToHex(torrent.getHash()),
torrent.getSize(),
torrent.getCreationDate(),
torrent.getCreatedBy(),
Expand Down Expand Up @@ -180,7 +180,7 @@ private void handleBatchUnban(Context ctx) {

public void handleDownload(Context ctx) {
String arg = ctx.pathParam("infoHash");
byte[] infoHash = java.util.Base64.getDecoder().decode(arg);
byte[] infoHash = hexToByteArray(arg);
try {
Download download = pluginInterface.getDownloadManager().getDownload(infoHash);
if (download == null) {
Expand All @@ -196,7 +196,7 @@ public void handleDownload(Context ctx) {

public void handlePeers(Context ctx) {
String arg = ctx.pathParam("infoHash");
byte[] infoHash = java.util.Base64.getDecoder().decode(arg);
byte[] infoHash = hexToByteArray(arg);
try {
Download download = pluginInterface.getDownloadManager().getDownload(infoHash);
if (download == null) {
Expand Down Expand Up @@ -297,7 +297,7 @@ private DownloadRecord getDownloadRecord(Download download) {
download.isComplete(),
download.isChecking(),
download.isMoving(),
download.getDownloadPeerId() == null ? null : java.util.Base64.getEncoder().encodeToString(download.getDownloadPeerId()).toLowerCase(Locale.ROOT),
download.getDownloadPeerId() == null ? null :bytesToHex(download.getDownloadPeerId()).toLowerCase(Locale.ROOT),
download.isRemoved());
}

Expand Down Expand Up @@ -348,7 +348,7 @@ private PeerRecord getPeerRecord(Peer peer) {
return new PeerRecord(
peer.isMyPeer(),
peer.getState(),
peer.getId() == null ? null : Base64.getEncoder().encodeToString(peer.getId()),
peer.getId() == null ? null : bytesToHex(peer.getId()),
peer.getIp(),
peer.getTCPListenPort(),
peer.getUDPListenPort(),
Expand Down Expand Up @@ -391,4 +391,51 @@ private PeerStatsRecord getPeerStatsRecord(PeerStats stats) {
);
}


public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
String hex = Integer.toHexString(aByte & 0xFF);
if (hex.length() < 2) {
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}

/**
* hex字符串转byte数组
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public static byte[] hexToByteArray(String inHex) {
int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1) {
//奇数
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {
//偶数
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}

/**
* Hex字符串转byte
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
public static byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class PeerRecord {
private boolean myPeer;
private int state;
private String peerIdBase64;
private String peerId;
private String ip;
private int tcpListenPort;
private int udpListenPort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@NoArgsConstructor
public class TorrentRecord {
private String name;
private String hashBase64;
private String infoHash;
private long size;
private long creationDate;
private String createdBy;
Expand Down

0 comments on commit 2adb93c

Please sign in to comment.