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:
Cursor Agentandqaiu committed 2026-11-24 07:38:46 +00:00
1 parent 62fa23542d
commit 8ed0ec4036
7 files changed
+172 -49

No files matched your search

@@ -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 {