4399游戏盒 v7.2.1.31版本的 MD5 值为:951717f45e3fda95c8a358caf4ca93e1
以下内容为反编译后的 TrUtil.java 源代码,内容仅作参考
package com.download.verify;
import android.text.TextUtils;
import com.download.DownloadConfigKey;
import com.download.DownloadInfoHelper;
import com.download.DownloadModel;
import com.download.constance.K;
import com.download.log.NetLogHandler;
import com.download.stream.DownloadRandomAccessFile;
import com.download.utils.DownloadUtils;
import com.download.utils.FileUtil;
import com.download.verify.parse.TrInfo;
import com.download.verify.parse.TrParser;
import com.framework.config.Config;
import com.framework.utils.FileUtils;
import com.framework.utils.io.RandomAccessFileWrapper;
import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.json.JSONArray;
import org.json.JSONException;
public class TrUtil {
public static final int PIECE_LEN = 20;
public static String buildUrlFromTemplate(DownloadModel downloadModel) {
String str = (String) downloadModel.getExtra(K.key.DOWNLOAD_TR_ID, "");
NetLogHandler.writeLog("tr id {}", str);
if (TextUtils.isEmpty(str)) {
return "";
}
String str2 = (String) Config.getValue(DownloadConfigKey.DOWNLOAD_TR_TEMPLATE);
if (TextUtils.isEmpty(str2)) {
str2 = "https://cdn.4399sj.com/android/box/game/v1.0/torrent-file-id-%s.html";
}
return String.format(str2, str);
}
public static String buildTrFilePath(DownloadModel downloadModel) {
String str = (String) downloadModel.getExtra(K.key.TR_FILE, "");
if (!TextUtils.isEmpty(str)) {
return str;
}
File buildDownloadPath = DownloadInfoHelper.buildDownloadPath(downloadModel);
if (!buildDownloadPath.exists()) {
buildDownloadPath.mkdirs();
}
String str2 = buildDownloadPath.getAbsolutePath() + File.separator + downloadModel.getMAppName() + ".tr";
downloadModel.putExtra(K.key.TR_FILE, str2);
return str2;
}
public static boolean checkTrFile(DownloadModel downloadModel) {
boolean z;
RandomAccessFileWrapper randomAccessFileWrapper;
Throwable th;
String str;
String dataMd5;
try {
randomAccessFileWrapper = new RandomAccessFileWrapper(buildTrFilePath(downloadModel), "r");
try {
TrInfo parse = TrParser.parse(FileUtil.read(randomAccessFileWrapper, 0L, (int) randomAccessFileWrapper.length()));
str = (String) downloadModel.getExtra(K.key.DOWNLOAD_TR_PIECE_MD5, "");
dataMd5 = DownloadUtils.getDataMd5(parse.getPiecesBlob());
NetLogHandler.writeLog("TR文件分片MD5:{}, 头部分片MD5:{}", dataMd5, str);
} catch (Throwable th2) {
th = th2;
z = false;
}
} catch (Throwable th3) {
z = false;
randomAccessFileWrapper = null;
th = th3;
}
if (!TextUtils.isEmpty(str)) {
if (!str.equals(dataMd5)) {
z = false;
if (!z) {
try {
NetLogHandler.onTRFileError(null, downloadModel, "");
} catch (Throwable th4) {
th = th4;
try {
NetLogHandler.writeLog("校验种子文件分片信息时发生IO错误: {}", th);
downloadModel.putExtra(K.key.DOWNLOAD_TR_SUCCESS, false);
NetLogHandler.onTRFileError(th, downloadModel, "");
FileUtils.closeSilent(randomAccessFileWrapper);
return z;
} catch (Throwable th5) {
FileUtils.closeSilent(randomAccessFileWrapper);
throw th5;
}
}
}
FileUtils.closeSilent(randomAccessFileWrapper);
return z;
}
}
z = true;
if (!z) {
}
FileUtils.closeSilent(randomAccessFileWrapper);
return z;
}
public static void saveTr(DownloadModel downloadModel, String str, byte[] bArr) throws IOException {
JSONArray jSONArray = new JSONArray();
String[] formatMultiPiece = formatMultiPiece(bArr);
for (int i = 0; i < formatMultiPiece.length; i++) {
String str2 = formatMultiPiece[i];
if (str2.startsWith("00") && str2.matches("^0+$")) {
throw new IOException("种子分片 index " + i + ", sha值全0 , 非法的种子内容");
}
jSONArray.put(str2);
}
downloadModel.putExtra(str, jSONArray, false);
}
public static void saveTr(DownloadModel downloadModel, TrDownloadType trDownloadType, byte[] bArr, byte[] bArr2) throws IOException {
DownloadRandomAccessFile downloadRandomAccessFile;
Throwable th;
if (trDownloadType == TrDownloadType.ALL) {
try {
downloadRandomAccessFile = new DownloadRandomAccessFile(new File(buildTrFilePath(downloadModel)));
try {
downloadRandomAccessFile.write(bArr, 0, bArr.length);
downloadRandomAccessFile.closeWithFlushAndSync();
} catch (Throwable th2) {
th = th2;
if (downloadRandomAccessFile != null) {
downloadRandomAccessFile.closeWithFlushAndSync();
}
throw th;
}
} catch (Throwable th3) {
downloadRandomAccessFile = null;
th = th3;
}
} else {
JSONArray jSONArray = new JSONArray();
String[] formatMultiPiece = formatMultiPiece(bArr2);
for (int i = 0; i < formatMultiPiece.length; i++) {
String str = formatMultiPiece[i];
NetLogHandler.writeLogWithName("pieceInfo", "种子hash {}片:{}", Integer.valueOf(i), str);
if (str.startsWith("00") && str.matches("^0+$")) {
throw new IOException("种子分片 index " + i + ", sha值全0 , 非法的种子内容");
}
jSONArray.put(str);
}
downloadModel.putExtra(K.key.DOWNLOAD_TR_ALL_PIECE_SHA_VALUE, jSONArray, false);
}
}
public static String formatPiece(byte[] bArr, int i, int i2) {
if (bArr == null) {
throw new NullPointerException("param piece is null");
}
StringBuilder sb = new StringBuilder();
while (i < i2) {
String hexString = Integer.toHexString(bArr[i] & 255);
if (hexString.length() == 1) {
sb.append("0");
}
sb.append(hexString);
i++;
}
return sb.toString();
}
public static String formatPiece(byte[] bArr) {
return formatPiece(bArr, 0, bArr.length);
}
public static String[] formatMultiPiece(byte[] bArr) {
if (bArr == null) {
throw new NullPointerException("param allPiece is null");
}
String[] strArr = new String[bArr.length / 20];
for (int i = 0; i < strArr.length; i++) {
int i2 = i * 20;
strArr[i] = formatPiece(bArr, i2, i2 + 20);
}
return strArr;
}
public static byte[] sha(byte[] bArr) {
try {
return MessageDigest.getInstance("SHA-1").digest(bArr);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static String loadShaCodeFromModel(DownloadModel downloadModel, int i) throws JSONException, IOException {
JSONArray jSONArray = (JSONArray) downloadModel.getExtra(K.key.DOWNLOAD_TR_ALL_PIECE_SHA_VALUE);
if (jSONArray == null || jSONArray.length() <= 0) {
return null;
}
return jSONArray.getString(i);
}
public static String loadShaCodeFromFile(RandomAccessFileWrapper randomAccessFileWrapper, int i) throws IOException {
byte[] bArr = new byte[20];
try {
randomAccessFileWrapper.seek(i * 20);
randomAccessFileWrapper.read(bArr, 0, bArr.length);
return formatPiece(bArr);
} catch (IOException e) {
NetLogHandler.writeLog("调用loadShaCode读取分片sha值失败 {} ", e.getMessage());
throw new IOException("从tr文件获取内容失败", e);
}
}
public static byte[] decrypt(byte[] bArr) {
for (int i = 0; i < bArr.length; i++) {
bArr[i] = (byte) (bArr[i] ^ 170);
}
return bArr;
}
public static boolean isTrSoValid(File file) {
if (file != null && file.exists()) {
return true;
}
NetLogHandler.writeLog("TR so 文件不存在 {}", file);
return false;
}
}