mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-11-25 06:47:00 +00:00
Parse password-protected Ctfile shares with anonymous ALTCHA
A password share's first response is often labeled Turnstile, but a fresh ALTCHA token plus the passcode returns the file. Try that before any login. Keep the v4 client only as a fallback when Turnstile remains and a token is configured. Live checks download the sample PDF and a directory zip. Co-authored-by: qaiu <[email protected]>
This commit is contained in:
7 files changed
+172
-49
No files matched your search
@@ -546,7 +546,7 @@ auths:
|
||||
username: 你的用户名
|
||||
password: 你的密码
|
||||
# authorization: 浏览器登录后的 authorToken / sso-token
|
||||
# 城通带访问密码的分享需要登录令牌。免密分享不用配。
|
||||
# 城通免密和带密码分享都先匿名解析。仅当仍被 Turnstile 拦住时才需要登录令牌。
|
||||
# 登录 https://pan.ctfile.com 后,从 Session Storage 复制 refresh_token。
|
||||
# ct:
|
||||
# refresh_token: 登录后的 refresh_token
|
||||
|
||||
@@ -21,9 +21,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城通网盘。免密分享走网页接口 {@code api.ctfile.com/url/*} 和 ALTCHA v2。
|
||||
* 带访问密码的分享在网页接口上会被 Turnstile 拦住,改走官方客户端 {@code /v4},
|
||||
* 需要在 {@code auths.ct.refresh_token} 配置自己的登录令牌。
|
||||
* 城通网盘。免密和带访问密码的分享都先走匿名网页接口和 ALTCHA v2。
|
||||
* 官方客户端 {@code /v4} 只在匿名结果仍是 Turnstile、且调用方自己配置了登录令牌时回退。
|
||||
*/
|
||||
public class CtTool extends PanBase {
|
||||
|
||||
@@ -167,15 +166,17 @@ public class CtTool extends PanBase {
|
||||
return new ParsedDownload(link.url, java.util.Map.of(), link.file, "");
|
||||
}
|
||||
|
||||
private boolean requiresV4() {
|
||||
if (shareLinkInfo.getOtherParam() != null) {
|
||||
Object fileId = shareLinkInfo.getOtherParam().get("ctV4FileId");
|
||||
if (fileId != null && !fileId.toString().isBlank()) {
|
||||
return true;
|
||||
}
|
||||
/** 只有 v4 目录列表生成的文件参数才直接走客户端协议。访问密码本身不触发登录。 */
|
||||
static boolean requiresV4(ShareLinkInfo info) {
|
||||
if (info == null || info.getOtherParam() == null) {
|
||||
return false;
|
||||
}
|
||||
String password = shareLinkInfo.getSharePassword();
|
||||
return password != null && !password.isBlank();
|
||||
Object fileId = info.getOtherParam().get("ctV4FileId");
|
||||
return fileId != null && !fileId.toString().isBlank();
|
||||
}
|
||||
|
||||
private boolean requiresV4() {
|
||||
return requiresV4(shareLinkInfo);
|
||||
}
|
||||
|
||||
private static boolean turnstile(CtfileException error) {
|
||||
@@ -206,12 +207,6 @@ public class CtTool extends PanBase {
|
||||
|
||||
private List<FileInfo> listDirectory(String shareKey) throws Exception {
|
||||
String password = shareLinkInfo.getSharePassword();
|
||||
if (password != null && !password.isBlank()) {
|
||||
if (!CtfileV4Client.hasCredentials(shareLinkInfo)) {
|
||||
throw new CtfileException(CtfileV4Client.missingAccountMessage());
|
||||
}
|
||||
return new CtfileV4Client(shareLinkInfo).listShare(shareKey, ShareTarget.from(shareLinkInfo).folderId, maxListPages());
|
||||
}
|
||||
ShareTarget target = ShareTarget.from(shareLinkInfo);
|
||||
int maxPages = maxListPages();
|
||||
try (CtfileSession session = CtfileSession.open(shareLinkInfo)) {
|
||||
|
||||
@@ -69,7 +69,9 @@ final class CtfileSession implements AutoCloseable {
|
||||
remember(decision, password, null);
|
||||
return body;
|
||||
}
|
||||
if (decision.kind == Kind.ALTCHA) {
|
||||
// 带密码分享的首次响应经常标成 turnstile,但提交一次 ALTCHA 后正确密码会直接 200。
|
||||
// 过期的 access_pass 同样会回到这个分支,丢弃后重新计算。
|
||||
if (shouldSolveAltcha(decision)) {
|
||||
String cached = cachedPass(decision.ownerId, password);
|
||||
if (cached != null) {
|
||||
JsonObject retry = getResource(kind, id, target, password, null, null, cached);
|
||||
@@ -88,27 +90,32 @@ final class CtfileSession implements AutoCloseable {
|
||||
String token = AltchaV2Solver.payload(challenge, solution);
|
||||
JsonObject retry = getResource(kind, id, target, password, token, null, null);
|
||||
Decision solved = classify(retry, 200, password);
|
||||
if (solved.kind != Kind.OK) {
|
||||
if (solved.kind == Kind.ALTCHA) {
|
||||
throw new CtfileException("ALTCHA 人机验证未通过,请重试");
|
||||
}
|
||||
throw new CtfileException(solved.message);
|
||||
}
|
||||
remember(solved, password, null);
|
||||
return retry;
|
||||
}
|
||||
if (decision.kind == Kind.TURNSTILE && turnstileToken != null && !turnstileToken.isBlank()) {
|
||||
JsonObject retry = getResource(kind, id, target, password, null, turnstileToken, null);
|
||||
Decision again = classify(retry, 200, password);
|
||||
if (again.kind == Kind.OK) {
|
||||
remember(again, password, null);
|
||||
if (solved.kind == Kind.OK) {
|
||||
remember(solved, password, null);
|
||||
return retry;
|
||||
}
|
||||
throw new CtfileException(again.message);
|
||||
if (solved.kind == Kind.ALTCHA) {
|
||||
throw new CtfileException("ALTCHA 人机验证未通过,请重试");
|
||||
}
|
||||
if (solved.kind == Kind.TURNSTILE && turnstileToken != null && !turnstileToken.isBlank()) {
|
||||
JsonObject withTurnstile = getResource(kind, id, target, password, null, turnstileToken, null);
|
||||
Decision afterTurnstile = classify(withTurnstile, 200, password);
|
||||
if (afterTurnstile.kind == Kind.OK) {
|
||||
remember(afterTurnstile, password, null);
|
||||
return withTurnstile;
|
||||
}
|
||||
throw new CtfileException(afterTurnstile.message);
|
||||
}
|
||||
throw new CtfileException(solved.message);
|
||||
}
|
||||
throw new CtfileException(decision.message);
|
||||
}
|
||||
|
||||
/** 匿名分享优先解 ALTCHA。首次响应写成 turnstile 时,正确密码仍可能被 ALTCHA 放行。 */
|
||||
static boolean shouldSolveAltcha(Decision decision) {
|
||||
return decision != null && (decision.kind == Kind.ALTCHA || decision.kind == Kind.TURNSTILE);
|
||||
}
|
||||
|
||||
DirectLink download(String id, ShareTarget target, String password, JsonObject resource) throws Exception {
|
||||
JsonObject data = dataOf(resource);
|
||||
JsonObject file = data == null ? null : data.getJsonObject("file");
|
||||
|
||||
@@ -22,8 +22,8 @@ import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 城通官方客户端协议 {@code /v4}。带访问密码的分享在网页接口上会被 Turnstile 拦住,
|
||||
* 登录自己的免费账号后改走这里列目录和取直链。
|
||||
* 城通官方客户端协议 {@code /v4}。匿名 ALTCHA 仍被 Turnstile 拦住时的可选回退,
|
||||
* 需要调用方自己的登录令牌。带访问密码的分享默认不走这里。
|
||||
*/
|
||||
final class CtfileV4Client {
|
||||
|
||||
@@ -50,10 +50,10 @@ final class CtfileV4Client {
|
||||
}
|
||||
|
||||
static String missingAccountMessage() {
|
||||
return "未配置城通账号。带访问密码的分享需要登录后的令牌:"
|
||||
+ "在 app-dev.yml 配置 auths.ct.refresh_token,或请求里用 auth 传入。"
|
||||
return "匿名解析仍被 Cloudflare Turnstile 拦住,且未配置城通账号。"
|
||||
+ "可在 app-dev.yml 配置 auths.ct.refresh_token 作为可选回退,或在请求 auth 中传入。"
|
||||
+ "refresh_token 来自浏览器登录 pan.ctfile.com 后 Session Storage 中的 refresh_token;"
|
||||
+ "也可以同时填写 token 作为当前 access token。目录分享的类型是 ctd,未单独配置时会回退使用 ct。";
|
||||
+ "token 可填当前 access token。目录类型 ctd 未单独配置时回退使用 ct。";
|
||||
}
|
||||
|
||||
DirectLink downloadShare(String shareKey, boolean directory) throws Exception {
|
||||
|
||||
@@ -11,13 +11,18 @@ import org.junit.AfterClass;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -36,7 +41,9 @@ public class CtToolLiveTest {
|
||||
private static final String PASSWORD_FILE_WITHOUT_PASSWORD =
|
||||
"https://url94.ctfile.com/f/64115194-17569800420720-06c697";
|
||||
private static final String PASSWORD_DIR =
|
||||
"https://url94.ctfile.com/d/64115194-164803691-48508c?p=7609&d=164803691&fk=decb36";
|
||||
"https://url94.ctfile.com/d/64115194-164803691-48508c?p=7609";
|
||||
private static final String PASSWORD_FILE_WRONG =
|
||||
"https://url94.ctfile.com/f/64115194-17569800420720-06c697?p=0000";
|
||||
|
||||
private static Vertx vertx;
|
||||
|
||||
@@ -122,19 +129,75 @@ public class CtToolLiveTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void passwordShareIsTurnstileAndMissingShareIsNotFound() throws Exception {
|
||||
public void passwordFileDownloadsAnonymously() throws Exception {
|
||||
liveOnly();
|
||||
String withPassword = failure(tool(PASSWORD_FILE).parse());
|
||||
assertTrue(withPassword, withPassword.contains("未配置城通账号"));
|
||||
assertFalse(withPassword, withPassword.contains("已过期"));
|
||||
assertFalse(withPassword, withPassword.contains("密码不正确"));
|
||||
CtTool tool = tool(PASSWORD_FILE);
|
||||
String url = tool.parse().toCompletionStage().toCompletableFuture().get(120, TimeUnit.SECONDS);
|
||||
FileInfo info = (FileInfo) tool.getShareLinkInfo().getOtherParam().get("fileInfo");
|
||||
assertEquals("欢迎使用微盘.pdf", info.getFileName());
|
||||
assertEquals(Long.valueOf(4005749L), info.getSize());
|
||||
byte[] body = download(url);
|
||||
assertEquals(4005749, body.length);
|
||||
assertTrue(new String(body, 0, 5, StandardCharsets.ISO_8859_1).startsWith("%PDF-"));
|
||||
assertTrue(new String(body, body.length - 16, 16, StandardCharsets.ISO_8859_1).contains("%%EOF"));
|
||||
assertEquals("cce8357689762fd1acd1f73fcd7e83d62b4e9bcee6fccf0c9b8f46d26ee74902", sha256(body));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void passwordDirectoryListsAndDownloadsZipAnonymously() throws Exception {
|
||||
liveOnly();
|
||||
CtTool directory = tool(PASSWORD_DIR);
|
||||
List<FileInfo> files = directory.parseFileList().toCompletionStage().toCompletableFuture()
|
||||
.get(120, TimeUnit.SECONDS);
|
||||
assertEquals(3, files.size());
|
||||
FileInfo zip = null;
|
||||
for (FileInfo file : files) {
|
||||
assertNotNull(file.getParserUrl());
|
||||
assertTrue(file.getParserUrl().contains("pwd=7609") || decodedParam(file).getString("pwd").equals("7609"));
|
||||
if ("osm_model_20260602_153158.zip".equals(file.getFileName())) {
|
||||
zip = file;
|
||||
}
|
||||
}
|
||||
assertNotNull(zip);
|
||||
assertEquals(Long.valueOf(158842L), zip.getSize());
|
||||
ShareLinkInfo child = ShareLinkInfo.newBuilder()
|
||||
.type("ct")
|
||||
.panName("城通网盘")
|
||||
.shareKey("pending")
|
||||
.shareUrl("https://ctfile.com/file/pending")
|
||||
.build();
|
||||
child.getOtherParam().put("paramJson", decodedParam(zip));
|
||||
child.getOtherParam().put("domainName", "https://nfd.example");
|
||||
CtTool downloader = new CtTool(child);
|
||||
String url = downloader.parseById().toCompletionStage().toCompletableFuture().get(120, TimeUnit.SECONDS);
|
||||
byte[] body = download(url);
|
||||
assertEquals(158842, body.length);
|
||||
assertEquals('P', (char) body[0]);
|
||||
assertEquals('K', (char) body[1]);
|
||||
int entries = 0;
|
||||
try (ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(body))) {
|
||||
ZipEntry entry;
|
||||
while ((entry = zipIn.getNextEntry()) != null) {
|
||||
byte[] ignored = zipIn.readAllBytes();
|
||||
assertTrue(entry.getName() != null);
|
||||
assertTrue(ignored.length >= 0);
|
||||
zipIn.closeEntry();
|
||||
entries++;
|
||||
}
|
||||
}
|
||||
assertTrue(entries > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingPasswordAndMissingShareStayDistinct() throws Exception {
|
||||
liveOnly();
|
||||
String withoutPassword = failure(tool(PASSWORD_FILE_WITHOUT_PASSWORD).parse());
|
||||
assertTrue(withoutPassword, withoutPassword.contains("需要访问密码"));
|
||||
assertFalse(withoutPassword, withoutPassword.contains("未配置城通账号"));
|
||||
|
||||
CtTool directory = (CtTool) ParserCreate.fromShareUrl(PASSWORD_DIR).createTool();
|
||||
String directoryFailure = failure(directory.parseFileList());
|
||||
assertTrue(directoryFailure, directoryFailure.contains("未配置城通账号"));
|
||||
String wrongPassword = failure(tool(PASSWORD_FILE_WRONG).parse());
|
||||
assertTrue(wrongPassword, wrongPassword.contains("访问密码不正确"));
|
||||
assertFalse(wrongPassword, wrongPassword.contains("未配置城通账号"));
|
||||
|
||||
String missingFile = failure(tool("https://474b.com/file/4015376-1").parse());
|
||||
assertTrue(missingFile, missingFile.contains("不存在或已过期"));
|
||||
@@ -143,6 +206,40 @@ public class CtToolLiveTest {
|
||||
assertTrue(missingOwner, missingOwner.contains("不存在或已过期"));
|
||||
}
|
||||
|
||||
private static JsonObject decodedParam(FileInfo file) {
|
||||
String url = file.getParserUrl();
|
||||
return new JsonObject(decodeUrlBase64(url.substring(url.lastIndexOf('/') + 1)));
|
||||
}
|
||||
|
||||
private static String decodeUrlBase64(String value) {
|
||||
String padded = value;
|
||||
int mod = padded.length() % 4;
|
||||
if (mod != 0) {
|
||||
padded += "=".repeat(4 - mod);
|
||||
}
|
||||
return new String(java.util.Base64.getUrlDecoder().decode(padded), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte[] download(String url) throws Exception {
|
||||
HttpResponse<byte[]> response = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build()
|
||||
.send(HttpRequest.newBuilder(URI.create(url)).timeout(Duration.ofSeconds(120)).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
assertEquals(200, response.statusCode());
|
||||
return response.body();
|
||||
}
|
||||
|
||||
private static String sha256(byte[] body) throws Exception {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256").digest(body);
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (byte value : digest) {
|
||||
hex.append(String.format("%02x", value));
|
||||
}
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
private static void liveOnly() {
|
||||
Assume.assumeTrue("set -Dctfile.live=true to call ctfile", Boolean.getBoolean("ctfile.live"));
|
||||
if (vertx == null) {
|
||||
|
||||
@@ -57,6 +57,29 @@ public class CtfileResponseTest {
|
||||
"{\"code\":200,\"data\":{\"access_pass\":\"v1.aaa.bbb\",\"capabilities\":{\"ownerId\":4015376}}}"), 200, "");
|
||||
assertEquals(Kind.OK, ok.kind);
|
||||
assertEquals("v1.aaa.bbb", ok.accessPass);
|
||||
|
||||
Decision labeledTurnstile = CtfileSession.classify(new JsonObject(
|
||||
"{\"code\":423,\"data\":{\"error_type\":\"captcha_required\",\"captcha_type\":\"turnstile\","
|
||||
+ "\"capabilities\":{\"passcodeRequired\":true}},\"msg\":\"Share passcode needs a new human check.\"}"),
|
||||
423, "pw");
|
||||
assertTrue(CtfileSession.shouldSolveAltcha(labeledTurnstile));
|
||||
assertFalse(CtfileSession.shouldSolveAltcha(needPassword));
|
||||
Decision wrongAfterAltcha = CtfileSession.classify(new JsonObject(
|
||||
"{\"code\":423,\"data\":{\"error_type\":\"passcode_incorrect\",\"captcha_type\":\"turnstile\","
|
||||
+ "\"capabilities\":{\"passcodeRequired\":true}},\"msg\":\"Share passcode is incorrect.\"}"),
|
||||
423, "0000");
|
||||
assertEquals(Kind.BAD_PASSWORD, wrongAfterAltcha.kind);
|
||||
assertFalse(CtfileSession.shouldSolveAltcha(wrongAfterAltcha));
|
||||
|
||||
ShareLinkInfo passwordShare = ShareLinkInfo.newBuilder()
|
||||
.type("ct")
|
||||
.shareKey("11-22-ab")
|
||||
.sharePassword("pw")
|
||||
.shareUrl("https://example.test/f/11-22-ab?p=pw")
|
||||
.build();
|
||||
assertFalse(CtTool.requiresV4(passwordShare));
|
||||
passwordShare.getOtherParam().put("ctV4FileId", "f22");
|
||||
assertTrue(CtTool.requiresV4(passwordShare));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -145,7 +145,8 @@ auths:
|
||||
username:
|
||||
password:
|
||||
# authorization:
|
||||
# 城通:免密分享不需要登录。带访问密码的文件和目录要自己的客户端令牌。
|
||||
# 城通:免密和带访问密码的分享都先匿名解 ALTCHA,通常不用登录。
|
||||
# 只有匿名结果仍是 Turnstile 时,下面的令牌才会作为可选回退。
|
||||
# 浏览器打开 https://pan.ctfile.com 并登录,F12 → Application → Session Storage,
|
||||
# 复制 refresh_token。token 可空,填了就当作当前 access token,过期后用 refresh_token 刷新。
|
||||
# 刷新若轮换了 refresh_token,进程内会写回这份配置和 TokenCache;重启前请把新值保存回本文件。
|
||||
|
||||
Reference in new issue
Block a user