Simplify and optimize Ce4Tool and CeTool version detection logic

Co-authored-by: qaiu <29825328+qaiu@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-10 09:59:48 +00:00
parent f1dd9fc0ee
commit d23b11577e
2 changed files with 53 additions and 131 deletions

View File

@@ -12,13 +12,12 @@ import java.net.URL;
/** /**
* <a href="https://github.com/cloudreve/Cloudreve">Cloudreve 4.x 自建网盘解析</a> <br> * <a href="https://github.com/cloudreve/Cloudreve">Cloudreve 4.x 自建网盘解析</a> <br>
* Cloudreve 4.x API 版本解析器 * Cloudreve 4.x API 版本解析器 <br>
* 此解析器专门处理Cloudreve 4.x版本的API使用新的下载流程
*/ */
public class Ce4Tool extends PanBase { public class Ce4Tool extends PanBase {
// Cloudreve 4.x uses /api/v3/ prefix for most APIs // Cloudreve 4.x uses /api/v3/ prefix for most APIs
private static final String PING_API_V3_PATH = "/api/v3/site/ping";
private static final String PING_API_V4_PATH = "/api/v4/site/ping";
private static final String FILE_URL_API_PATH = "/api/v3/file/url"; private static final String FILE_URL_API_PATH = "/api/v3/file/url";
private static final String SHARE_API_PATH = "/api/v3/share/info/"; private static final String SHARE_API_PATH = "/api/v3/share/info/";
@@ -34,8 +33,8 @@ public class Ce4Tool extends PanBase {
URL url = new URL(shareLinkInfo.getShareUrl()); URL url = new URL(shareLinkInfo.getShareUrl());
String baseUrl = url.getProtocol() + "://" + url.getHost(); String baseUrl = url.getProtocol() + "://" + url.getHost();
// First, detect API version by pinging // 获取分享信息
detectVersion(baseUrl, key, pwd); getShareInfo(baseUrl, key, pwd);
} catch (Exception e) { } catch (Exception e) {
fail(e, "URL解析错误"); fail(e, "URL解析错误");
} }
@@ -43,61 +42,7 @@ public class Ce4Tool extends PanBase {
} }
/** /**
* Detect Cloudreve version by pinging /api/v3/site/ping or /api/v4/site/ping * 获取Cloudreve 4.x分享信息
*/
private void detectVersion(String baseUrl, String key, String pwd) {
String pingUrlV3 = baseUrl + PING_API_V3_PATH;
// Try v3 ping first (which also works for 4.x)
clientSession.getAbs(pingUrlV3).send().onSuccess(res -> {
if (res.statusCode() == 200) {
try {
JsonObject pingResponse = asJson(res);
// If we get a valid JSON response, this is a Cloudreve instance
// Check if it's 4.x by trying the share API
getShareInfo(baseUrl, key, pwd);
} catch (Exception e) {
// Not a valid JSON response, try v4 ping
tryV4Ping(baseUrl, key, pwd);
}
} else if (res.statusCode() == 404) {
// Try v4 ping
tryV4Ping(baseUrl, key, pwd);
} else {
// Not a Cloudreve instance, try next parser
nextParser();
}
}).onFailure(t -> {
// Network error or not accessible, try next parser
nextParser();
});
}
private void tryV4Ping(String baseUrl, String key, String pwd) {
String pingUrlV4 = baseUrl + PING_API_V4_PATH;
clientSession.getAbs(pingUrlV4).send().onSuccess(res -> {
if (res.statusCode() == 200) {
try {
JsonObject pingResponse = asJson(res);
// Valid v4 response
getShareInfo(baseUrl, key, pwd);
} catch (Exception e) {
// Not a Cloudreve instance
nextParser();
}
} else {
// Not a Cloudreve instance
nextParser();
}
}).onFailure(t -> {
// Not accessible, try next parser
nextParser();
});
}
/**
* Get share information from Cloudreve 4.x
*/ */
private void getShareInfo(String baseUrl, String key, String pwd) { private void getShareInfo(String baseUrl, String key, String pwd) {
String shareApiUrl = baseUrl + SHARE_API_PATH + key; String shareApiUrl = baseUrl + SHARE_API_PATH + key;
@@ -114,49 +59,45 @@ public class Ce4Tool extends PanBase {
if (jsonObject.containsKey("code")) { if (jsonObject.containsKey("code")) {
int code = jsonObject.getInteger("code"); int code = jsonObject.getInteger("code");
if (code == 0) { if (code == 0) {
// Success, get file info and download URL // 成功,获取文件信息和下载链接
JsonObject data = jsonObject.getJsonObject("data"); JsonObject data = jsonObject.getJsonObject("data");
if (data != null) { if (data != null) {
// Get file path or use default // 获取文件路径,如果没有则使用默认路径
String filePath = "/"; String filePath = "/";
if (data.containsKey("path")) { if (data.containsKey("path")) {
filePath = data.getString("path"); filePath = data.getString("path");
} }
// For 4.x, we need to get the download URL via POST /api/v3/file/url // 对于4.x需要通过 POST /api/v3/file/url 获取下载链接
getDownloadUrl(baseUrl, key, filePath); getDownloadUrl(baseUrl, filePath);
} else { } else {
fail("分享信息获取失败: data字段为空"); fail("分享信息获取失败: data字段为空");
} }
} else { } else {
// Error code, might be wrong password or invalid share // 错误码,可能是密码错误或分享失效
String msg = jsonObject.getString("msg", "未知错误"); String msg = jsonObject.getString("msg", "未知错误");
fail("分享验证失败: {}", msg); fail("分享验证失败: {}", msg);
} }
} else { } else {
// Not a Cloudreve 4.x response, try next parser // 响应格式不符合预期
nextParser(); fail("响应格式不符合Cloudreve 4.x规范");
} }
} else { } else {
// HTTP error, not a valid Cloudreve instance // HTTP错误
nextParser(); fail("获取分享信息失败: HTTP {}", res.statusCode());
} }
} catch (Exception e) { } catch (Exception e) {
// JSON parsing error, not a Cloudreve instance fail(e, "解析分享信息响应失败");
nextParser();
} }
}).onFailure(t -> { }).onFailure(handleFail(shareApiUrl));
// Network error, try next parser
nextParser();
});
} }
/** /**
* Get download URL via POST /api/v3/file/url (Cloudreve 4.x API) * 通过 POST /api/v3/file/url 获取下载链接 (Cloudreve 4.x API)
*/ */
private void getDownloadUrl(String baseUrl, String key, String filePath) { private void getDownloadUrl(String baseUrl, String filePath) {
String fileUrlApi = baseUrl + FILE_URL_API_PATH; String fileUrlApi = baseUrl + FILE_URL_API_PATH;
// Prepare request body for Cloudreve 4.x // 准备Cloudreve 4.x的请求体
JsonObject requestBody = new JsonObject() JsonObject requestBody = new JsonObject()
.put("uris", new JsonArray().add(filePath)) .put("uris", new JsonArray().add(filePath))
.put("download", true); .put("download", true);
@@ -182,7 +123,7 @@ public class Ce4Tool extends PanBase {
fail("下载链接列表为空"); fail("下载链接列表为空");
} }
} else { } else {
fail("响应中不包含urls字段"); fail("响应中不包含urls字段: {}", jsonObject.encodePrettily());
} }
} else { } else {
fail("获取下载链接失败: HTTP {}", res.statusCode()); fail("获取下载链接失败: HTTP {}", res.statusCode());

View File

@@ -35,13 +35,7 @@ public class CeTool extends PanBase {
public Future<String> parse() { public Future<String> parse() {
String key = shareLinkInfo.getShareKey(); String key = shareLinkInfo.getShareKey();
String pwd = shareLinkInfo.getSharePassword(); String pwd = shareLinkInfo.getSharePassword();
// https://pan.huang1111.cn/s/wDz5TK
// https://pan.huang1111.cn/s/y12bI6 -> https://pan.huang1111
// .cn/api/v3/share/download/y12bI6?path=undefined%2Fundefined;
// 类型解析 -> /ce/pan.huang1111.cn_s_wDz5TK
// parser接口 -> /parser?url=https://pan.huang1111.cn/s/wDz5TK
try { try {
// // 处理URL
URL url = new URL(shareLinkInfo.getShareUrl()); URL url = new URL(shareLinkInfo.getShareUrl());
String baseUrl = url.getProtocol() + "://" + url.getHost(); String baseUrl = url.getProtocol() + "://" + url.getHost();
@@ -55,8 +49,8 @@ public class CeTool extends PanBase {
/** /**
* 检测Cloudreve版本并选择合适的解析器 * 检测Cloudreve版本并选择合适的解析器
* 先调用 /api/v3/site/ping 或 /api/v4/site/ping 判断是哪个版本 * 先调用 /api/v3/site/ping 判断哪个API 如果/v3 或者/v4 能查询到json响应可以判断是哪个版本
* 如果都返回404说明不是Cloudreve盘则调用nextParser * 不然返回404说明不是ce盘直接nextParser
*/ */
private void detectVersionAndParse(String baseUrl, String key, String pwd) { private void detectVersionAndParse(String baseUrl, String key, String pwd) {
String pingUrlV3 = baseUrl + PING_API_V3_PATH; String pingUrlV3 = baseUrl + PING_API_V3_PATH;
@@ -65,42 +59,43 @@ public class CeTool extends PanBase {
clientSession.getAbs(pingUrlV3).send().onSuccess(res -> { clientSession.getAbs(pingUrlV3).send().onSuccess(res -> {
if (res.statusCode() == 200) { if (res.statusCode() == 200) {
try { try {
JsonObject pingResponse = asJson(res); asJson(res);
// 获取到JSON响应检查是否是4.x版本 // v3 ping成功可能是3.x或4.x尝试3.x的download API来判断
// 4.x的ping响应可能有不同的结构我们通过share API来判断 String shareApiUrl = baseUrl + SHARE_API_PATH + key;
checkVersionByShareApi(baseUrl, key, pwd); String downloadApiUrl = baseUrl + DOWNLOAD_API_PATH + key + "?path=undefined/undefined;";
checkIfV3(shareApiUrl, downloadApiUrl, pwd);
} catch (Exception e) { } catch (Exception e) {
// JSON解析失败尝试v4 ping // JSON解析失败尝试v4 ping
tryV4PingAndParse(baseUrl, key, pwd); tryV4Ping(baseUrl, key, pwd);
} }
} else if (res.statusCode() == 404) { } else if (res.statusCode() == 404) {
// v3 ping不存在尝试v4 // v3 ping不存在尝试v4
tryV4PingAndParse(baseUrl, key, pwd); tryV4Ping(baseUrl, key, pwd);
} else { } else {
// 其他错误不是Cloudreve盘 // 其他错误不是Cloudreve盘
nextParser(); nextParser();
} }
}).onFailure(t -> { }).onFailure(t -> {
// 网络错误,尝试下一个解析器 // 网络错误或不可达尝试v4 ping
nextParser(); tryV4Ping(baseUrl, key, pwd);
}); });
} }
private void tryV4PingAndParse(String baseUrl, String key, String pwd) { private void tryV4Ping(String baseUrl, String key, String pwd) {
String pingUrlV4 = baseUrl + PING_API_V4_PATH; String pingUrlV4 = baseUrl + PING_API_V4_PATH;
clientSession.getAbs(pingUrlV4).send().onSuccess(res -> { clientSession.getAbs(pingUrlV4).send().onSuccess(res -> {
if (res.statusCode() == 200) { if (res.statusCode() == 200) {
try { try {
JsonObject pingResponse = asJson(res); asJson(res);
// v4 ping成功使用Ce4Tool // v4 ping成功使用Ce4Tool
delegateToCe4Tool(); delegateToCe4Tool();
} catch (Exception e) { } catch (Exception e) {
// 不是Cloudreve盘 // JSON解析失败不是Cloudreve盘
nextParser(); nextParser();
} }
} else { } else {
// 不是Cloudreve盘 // v4 ping失败不是Cloudreve盘
nextParser(); nextParser();
} }
}).onFailure(t -> { }).onFailure(t -> {
@@ -110,11 +105,9 @@ public class CeTool extends PanBase {
} }
/** /**
* 通过Share API的响应来判断版本 * 检查是否是3.x版本通过尝试调用3.x的API
* 3.x和4.x的share API响应格式可能不同
*/ */
private void checkVersionByShareApi(String baseUrl, String key, String pwd) { private void checkIfV3(String shareApiUrl, String downloadApiUrl, String pwd) {
String shareApiUrl = baseUrl + SHARE_API_PATH + key;
HttpRequest<Buffer> httpRequest = clientSession.getAbs(shareApiUrl); HttpRequest<Buffer> httpRequest = clientSession.getAbs(shareApiUrl);
if (pwd != null) { if (pwd != null) {
httpRequest.addQueryParam("password", pwd); httpRequest.addQueryParam("password", pwd);
@@ -123,12 +116,22 @@ public class CeTool extends PanBase {
httpRequest.send().onSuccess(res -> { httpRequest.send().onSuccess(res -> {
try { try {
if (res.statusCode() == 200 && res.bodyAsJsonObject().containsKey("code")) { if (res.statusCode() == 200 && res.bodyAsJsonObject().containsKey("code")) {
JsonObject jsonObject = asJson(res); // share API成功尝试download API
// 检查响应结构来判断版本 clientSession.putAbs(downloadApiUrl).send().onSuccess(res2 -> {
// 如果share API成功但download API返回404说明是4.x if (res2.statusCode() == 200 || res2.statusCode() == 400) {
// 这里我们先尝试3.x的download API // 3.x版本的download API存在
String downloadApiUrl = baseUrl + DOWNLOAD_API_PATH + key + "?path=undefined/undefined;"; getDownURL(downloadApiUrl);
checkDownloadApi(downloadApiUrl, baseUrl, key, pwd); } else if (res2.statusCode() == 404 || res2.statusCode() == 405) {
// download API不存在说明是4.x
delegateToCe4Tool();
} else {
// 其他错误可能是4.x
delegateToCe4Tool();
}
}).onFailure(t -> {
// 请求失败尝试4.x
delegateToCe4Tool();
});
} else { } else {
nextParser(); nextParser();
} }
@@ -140,28 +143,6 @@ public class CeTool extends PanBase {
}); });
} }
/**
* 检查3.x的download API是否存在
* 如果不存在说明是4.x版本
*/
private void checkDownloadApi(String downloadApiUrl, String baseUrl, String key, String pwd) {
clientSession.putAbs(downloadApiUrl).send().onSuccess(res -> {
if (res.statusCode() == 404 || res.statusCode() == 405) {
// download API不存在或方法不允许说明是4.x
delegateToCe4Tool();
} else if (res.statusCode() == 200) {
// 3.x版本继续使用当前逻辑
getDownURL(downloadApiUrl);
} else {
// 其他错误
fail("无法确定Cloudreve版本或接口调用失败");
}
}).onFailure(t -> {
// 尝试使用4.x
delegateToCe4Tool();
});
}
/** /**
* 转发到Ce4Tool处理4.x版本 * 转发到Ce4Tool处理4.x版本
*/ */