mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-11-25 14:57:00 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
551078b279 | ||
|
|
31bc383741 |
No files matched your search
@@ -14,6 +14,7 @@ import io.vertx.ext.web.client.HttpRequest;
|
||||
import io.vertx.ext.web.client.HttpResponse;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
@@ -27,6 +28,9 @@ import java.util.regex.Pattern;
|
||||
* 永硕E盘(主 ysepan.com / ys168.com,备 cccpan.com / ysupan.com / uupan.net / ysok.net)
|
||||
* <p>
|
||||
* 空间分享形如 https://{space}.ysepan.com/ ,需空间访问密码时通过 sharePassword 传入。
|
||||
* <p>
|
||||
* 2026-11 起密码空间改为入口页:令牌在 {@code data-verification-token},登录名在 {@code data-dlmc}。
|
||||
* {@code ys168.com} 会 302 到 {@code ysepan.com},密码验证必须发往跳转后的主机。
|
||||
*/
|
||||
public class YsTool extends PanBase {
|
||||
|
||||
@@ -34,10 +38,17 @@ public class YsTool extends PanBase {
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
|
||||
+ "(KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36";
|
||||
|
||||
private static final Pattern ANTIFORGERY_PATTERN =
|
||||
/** 旧版入口脚本:{@code RequestVerificationToken': '...'} */
|
||||
private static final Pattern ANTIFORGERY_LEGACY_PATTERN =
|
||||
Pattern.compile("RequestVerificationToken'\\s*:\\s*'([^']+)'");
|
||||
/** 新版入口页:{@code data-verification-token="..."} */
|
||||
private static final Pattern ANTIFORGERY_ATTR_PATTERN =
|
||||
Pattern.compile("data-verification-token\\s*=\\s*([\"'])([^\"']+)\\1");
|
||||
private static final Pattern DLMC_ATTR_PATTERN =
|
||||
Pattern.compile("data-dlmc\\s*=\\s*([\"'])([^\"']+)\\1");
|
||||
private static final Pattern HTXX_PATTERN =
|
||||
Pattern.compile("window\\.htxx\\s*=\\s*(\\{.*?});", Pattern.DOTALL);
|
||||
private static final int MAX_HOME_REDIRECTS = 5;
|
||||
private static final Pattern JWT_COOKIE_PATTERN =
|
||||
Pattern.compile("jwttk_[^=]+=([^;\\s]+)");
|
||||
|
||||
@@ -141,21 +152,49 @@ public class YsTool extends PanBase {
|
||||
}
|
||||
|
||||
private Future<Session> ensureSession() {
|
||||
Promise<Session> p = Promise.promise();
|
||||
String space = spaceName();
|
||||
if (StringUtils.isBlank(space)) {
|
||||
p.fail(baseMsg() + " - 空间名为空");
|
||||
return p.future();
|
||||
return Future.failedFuture(baseMsg() + " - 空间名为空");
|
||||
}
|
||||
// 手动跟随跳转,才能把后续密码验证发到最终主机(ys168.com -> ysepan.com)
|
||||
return fetchSpaceHome(spaceOrigin() + "/", 0)
|
||||
.compose(home -> handleSpaceHome(home.origin(), space, home.response()));
|
||||
}
|
||||
|
||||
String origin = spaceOrigin();
|
||||
clientSession.getAbs(origin + "/")
|
||||
/**
|
||||
* 关闭自动重定向并逐跳记录最终地址。Cookie(含 .AspNetCore.Antiforgery.* 与 jwttk_*)
|
||||
* 仍由同一个 clientSession 保存,供密码验证和接口请求带上。
|
||||
*/
|
||||
private Future<FetchedHome> fetchSpaceHome(String url, int redirects) {
|
||||
Promise<FetchedHome> p = Promise.promise();
|
||||
clientSession.getAbs(url)
|
||||
.followRedirects(false)
|
||||
.putHeader("User-Agent", BROWSER_UA)
|
||||
.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
.send()
|
||||
.compose(res -> handleSpaceHome(origin, space, res))
|
||||
.onSuccess(p::complete)
|
||||
.onFailure(p::fail);
|
||||
.onSuccess(res -> {
|
||||
int status = res.statusCode();
|
||||
if (status >= 300 && status < 400 && status != 304) {
|
||||
if (redirects >= MAX_HOME_REDIRECTS) {
|
||||
p.fail(baseMsg() + " - 空间首页重定向次数过多");
|
||||
return;
|
||||
}
|
||||
String next = resolveRedirect(url, res.getHeader("Location"));
|
||||
if (next == null) {
|
||||
p.fail(baseMsg() + " - 空间首页重定向无效");
|
||||
return;
|
||||
}
|
||||
fetchSpaceHome(next, redirects + 1).onComplete(p);
|
||||
return;
|
||||
}
|
||||
String origin = originOf(url);
|
||||
if (origin == null) {
|
||||
p.fail(baseMsg() + " - 空间地址无效");
|
||||
return;
|
||||
}
|
||||
p.complete(new FetchedHome(origin, res));
|
||||
})
|
||||
.onFailure(t -> p.fail(baseMsg() + " - 打开空间失败: " + t.getMessage()));
|
||||
return p.future();
|
||||
}
|
||||
|
||||
@@ -172,8 +211,8 @@ public class YsTool extends PanBase {
|
||||
return Future.succeededFuture(toSession(origin, space, jwt, htxx));
|
||||
}
|
||||
|
||||
// 需要空间密码
|
||||
if (html.contains("VerifyPassword") || html.contains("loginPopup")) {
|
||||
// 需要空间密码(新版入口页或旧版 loginPopup / VerifyPassword)
|
||||
if (isPasswordPage(html)) {
|
||||
String pwd = shareLinkInfo.getSharePassword();
|
||||
if (StringUtils.isBlank(pwd)) {
|
||||
return Future.failedFuture(baseMsg() + " - 空间需要访问密码");
|
||||
@@ -182,21 +221,25 @@ public class YsTool extends PanBase {
|
||||
if (StringUtils.isBlank(antiforgery)) {
|
||||
return Future.failedFuture(baseMsg() + " - 无法获取防伪令牌");
|
||||
}
|
||||
return verifyPassword(origin, space, pwd, antiforgery)
|
||||
.compose(verifiedJwt -> reloadSpace(origin, space, verifiedJwt));
|
||||
String dlmc = StringUtils.defaultIfBlank(extractDlmc(html), space);
|
||||
// 新版 VerifyPassword 只返回 {"success":true},jwttk 在入口页就已经下发
|
||||
return verifyPassword(origin, dlmc, pwd, antiforgery)
|
||||
.compose(verifiedJwt -> reloadSpace(origin, space,
|
||||
StringUtils.defaultIfBlank(verifiedJwt, jwt)));
|
||||
}
|
||||
|
||||
return Future.failedFuture(baseMsg() + " - 无法解析空间信息");
|
||||
}
|
||||
|
||||
private Future<String> verifyPassword(String origin, String space, String pwd, String antiforgery) {
|
||||
private Future<String> verifyPassword(String origin, String dlmc, String pwd, String antiforgery) {
|
||||
Promise<String> p = Promise.promise();
|
||||
JsonObject body = new JsonObject()
|
||||
.put("password", pwd)
|
||||
.put("dlmc", space)
|
||||
.put("dlmc", dlmc)
|
||||
.put("remember", false);
|
||||
|
||||
clientSession.postAbs(origin + "/?handler=VerifyPassword")
|
||||
clientSession.postAbs(verifyPasswordEndpoint(origin))
|
||||
.followRedirects(false)
|
||||
.putHeader("User-Agent", BROWSER_UA)
|
||||
.putHeader("Content-Type", "application/json")
|
||||
.putHeader("Origin", origin)
|
||||
@@ -205,18 +248,18 @@ public class YsTool extends PanBase {
|
||||
.sendJsonObject(body)
|
||||
.onSuccess(res -> {
|
||||
try {
|
||||
if (res.statusCode() >= 400) {
|
||||
p.fail(baseMsg() + " - " + passwordFailureMessage(res.statusCode(), res.bodyAsString()));
|
||||
return;
|
||||
}
|
||||
JsonObject json = res.bodyAsJsonObject();
|
||||
if (json == null || !Boolean.TRUE.equals(json.getBoolean("success"))) {
|
||||
String msg = json != null ? json.getString("message", "密码错误") : "密码验证失败";
|
||||
p.fail(baseMsg() + " - " + msg);
|
||||
return;
|
||||
}
|
||||
String jwt = extractJwt(res);
|
||||
if (StringUtils.isBlank(jwt)) {
|
||||
p.fail(baseMsg() + " - 密码验证成功但未返回会话令牌");
|
||||
return;
|
||||
}
|
||||
p.complete(jwt);
|
||||
// 响应里若刷新了 jwttk 就用新的;否则调用方回退到入口页令牌
|
||||
p.complete(StringUtils.defaultString(extractJwt(res)));
|
||||
} catch (Exception e) {
|
||||
p.fail(baseMsg() + " - 密码验证响应异常: " + e.getMessage());
|
||||
}
|
||||
@@ -226,27 +269,18 @@ public class YsTool extends PanBase {
|
||||
}
|
||||
|
||||
private Future<Session> reloadSpace(String origin, String space, String verifiedJwt) {
|
||||
Promise<Session> p = Promise.promise();
|
||||
clientSession.getAbs(origin + "/")
|
||||
.putHeader("User-Agent", BROWSER_UA)
|
||||
.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
.send()
|
||||
.onSuccess(res -> {
|
||||
String html = res.bodyAsString() != null ? res.bodyAsString() : "";
|
||||
JsonObject htxx = extractHtxx(html);
|
||||
String jwt = StringUtils.defaultIfBlank(extractJwt(res), verifiedJwt);
|
||||
if (htxx == null || StringUtils.isBlank(htxx.getString("qqdz"))) {
|
||||
p.fail(baseMsg() + " - 密码验证后仍无法进入空间");
|
||||
return;
|
||||
}
|
||||
if (StringUtils.isBlank(jwt)) {
|
||||
p.fail(baseMsg() + " - 密码验证后无法获取会话令牌");
|
||||
return;
|
||||
}
|
||||
p.complete(toSession(origin, space, jwt, htxx));
|
||||
})
|
||||
.onFailure(t -> p.fail(baseMsg() + " - 重新加载空间失败: " + t.getMessage()));
|
||||
return p.future();
|
||||
return fetchSpaceHome(origin + "/", 0).compose(home -> {
|
||||
String html = home.response().bodyAsString() != null ? home.response().bodyAsString() : "";
|
||||
JsonObject htxx = extractHtxx(html);
|
||||
String jwt = StringUtils.defaultIfBlank(extractJwt(home.response()), verifiedJwt);
|
||||
if (htxx == null || StringUtils.isBlank(htxx.getString("qqdz"))) {
|
||||
return Future.failedFuture(baseMsg() + " - 密码验证后仍无法进入空间");
|
||||
}
|
||||
if (StringUtils.isBlank(jwt)) {
|
||||
return Future.failedFuture(baseMsg() + " - 密码验证后无法获取会话令牌");
|
||||
}
|
||||
return Future.succeededFuture(toSession(home.origin(), space, jwt, htxx));
|
||||
});
|
||||
}
|
||||
|
||||
private Session toSession(String origin, String space, String jwt, JsonObject htxx) {
|
||||
@@ -620,24 +654,134 @@ public class YsTool extends PanBase {
|
||||
}
|
||||
|
||||
private String spaceOrigin() {
|
||||
String shareUrl = shareLinkInfo.getShareUrl();
|
||||
if (StringUtils.isNotBlank(shareUrl)) {
|
||||
try {
|
||||
java.net.URI uri = java.net.URI.create(shareUrl);
|
||||
String scheme = uri.getScheme() != null ? uri.getScheme() : "https";
|
||||
return scheme + "://" + uri.getHost();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
String fromUrl = originOf(shareLinkInfo.getShareUrl());
|
||||
if (fromUrl != null) {
|
||||
return fromUrl;
|
||||
}
|
||||
return "https://" + spaceName() + ".ysepan.com";
|
||||
}
|
||||
|
||||
private static String extractAntiforgery(String html) {
|
||||
Matcher m = ANTIFORGERY_PATTERN.matcher(html);
|
||||
return m.find() ? m.group(1) : null;
|
||||
static boolean isPasswordPage(String html) {
|
||||
if (StringUtils.isBlank(html)) {
|
||||
return false;
|
||||
}
|
||||
return html.contains("VerifyPassword")
|
||||
|| html.contains("loginPopup")
|
||||
|| html.contains("data-verification-token")
|
||||
|| html.contains("id=\"entryConfig\"")
|
||||
|| html.contains("id=\"loginForm\"")
|
||||
|| html.contains("id='entryConfig'")
|
||||
|| html.contains("id='loginForm'");
|
||||
}
|
||||
|
||||
private static JsonObject extractHtxx(String html) {
|
||||
/**
|
||||
* 优先新版 data-verification-token,其次旧版脚本里的 RequestVerificationToken。
|
||||
*/
|
||||
static String extractAntiforgery(String html) {
|
||||
if (html == null) {
|
||||
return null;
|
||||
}
|
||||
Matcher attr = ANTIFORGERY_ATTR_PATTERN.matcher(html);
|
||||
if (attr.find()) {
|
||||
return attr.group(2);
|
||||
}
|
||||
Matcher legacy = ANTIFORGERY_LEGACY_PATTERN.matcher(html);
|
||||
return legacy.find() ? legacy.group(1) : null;
|
||||
}
|
||||
|
||||
static String extractDlmc(String html) {
|
||||
if (html == null) {
|
||||
return null;
|
||||
}
|
||||
Matcher matcher = DLMC_ATTR_PATTERN.matcher(html);
|
||||
return matcher.find() ? matcher.group(2) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把密码接口的失败响应转成可展示的短句。上游对错误密码返回 HTTP 400,
|
||||
* 正文是 {@code code=invalid_password}。不回传响应原文,避免带上令牌。
|
||||
*/
|
||||
static String passwordFailureMessage(int statusCode, String body) {
|
||||
try {
|
||||
if (body != null && body.trim().startsWith("{")) {
|
||||
JsonObject json = new JsonObject(body);
|
||||
String code = StringUtils.defaultString(json.getString("code"));
|
||||
String message = safePasswordMessage(json.getString("message"));
|
||||
if ("invalid_password".equals(code)) {
|
||||
return message != null ? message : "密码错误";
|
||||
}
|
||||
if (message != null) {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return "密码验证失败 HTTP " + statusCode;
|
||||
}
|
||||
|
||||
private static String safePasswordMessage(String message) {
|
||||
if (StringUtils.isBlank(message)) {
|
||||
return null;
|
||||
}
|
||||
String trimmed = message.trim();
|
||||
if (trimmed.length() > 80) {
|
||||
return null;
|
||||
}
|
||||
String lower = trimmed.toLowerCase();
|
||||
if (lower.contains("eyj") || trimmed.contains("CfDJ") || lower.contains("jwttk")
|
||||
|| lower.contains("cookie") || lower.contains("token")) {
|
||||
return null;
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
/** 密码验证地址。origin 必须是重定向结束后的主机,不能用 ys168.com 原站。 */
|
||||
static String verifyPasswordEndpoint(String origin) {
|
||||
return origin + "/?handler=VerifyPassword";
|
||||
}
|
||||
|
||||
static String resolveRedirect(String currentUrl, String location) {
|
||||
if (StringUtils.isBlank(currentUrl) || StringUtils.isBlank(location)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
URI resolved = URI.create(currentUrl.trim()).resolve(location.trim());
|
||||
if (resolved.getScheme() == null || resolved.getHost() == null) {
|
||||
return null;
|
||||
}
|
||||
return resolved.toString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static String originOf(String url) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
URI uri = URI.create(url.trim());
|
||||
if (uri.getScheme() == null || uri.getHost() == null) {
|
||||
return null;
|
||||
}
|
||||
String scheme = uri.getScheme();
|
||||
int port = uri.getPort();
|
||||
boolean defaultPort = port < 0
|
||||
|| ("https".equalsIgnoreCase(scheme) && port == 443)
|
||||
|| ("http".equalsIgnoreCase(scheme) && port == 80);
|
||||
if (defaultPort) {
|
||||
return scheme + "://" + uri.getHost();
|
||||
}
|
||||
return scheme + "://" + uri.getHost() + ":" + port;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static JsonObject extractHtxx(String html) {
|
||||
if (html == null) {
|
||||
return null;
|
||||
}
|
||||
Matcher m = HTXX_PATTERN.matcher(html);
|
||||
if (!m.find()) {
|
||||
return null;
|
||||
@@ -686,6 +830,9 @@ public class YsTool extends PanBase {
|
||||
return URLEncoder.encode(StringUtils.defaultString(value), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private record FetchedHome(String origin, HttpResponse<Buffer> response) {
|
||||
}
|
||||
|
||||
private record Session(String origin, String space, String jwt, String apiBase) {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package cn.qaiu.parser.impl;
|
||||
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* 永硕入口页解析与重定向的离线测试,不访问网络。
|
||||
*/
|
||||
public class YsToolPasswordPageTest {
|
||||
|
||||
private static final String NEW_GATE = ""
|
||||
+ "<div id=\"entryConfig\" style=\"display:none\" data-dlmc=\"qaiu\"\n"
|
||||
+ " data-verification-token=\"new-token_value.1\"></div>\n"
|
||||
+ "<form id=\"loginForm\"></form>\n";
|
||||
|
||||
private static final String OLD_GATE = ""
|
||||
+ "<script>var cfg = { RequestVerificationToken': 'legacy-token' };</script>\n"
|
||||
+ "<div id=\"loginPopup\"></div>\n"
|
||||
+ "<form action=\"/?handler=VerifyPassword\"></form>\n";
|
||||
|
||||
private static final String OPEN_SPACE = ""
|
||||
+ "<script>window.htxx = {\"dlmc\":\"fufu1\",\"qqdz\":\"https://c6.ysepan.com/api/\","
|
||||
+ "\"yh\":{\"isgly\":false},\"ym\":\"ysepan.com\"};</script>";
|
||||
|
||||
@Test
|
||||
public void testNewPasswordPageTokenAndDlmc() {
|
||||
assertTrue(YsTool.isPasswordPage(NEW_GATE));
|
||||
assertEquals("new-token_value.1", YsTool.extractAntiforgery(NEW_GATE));
|
||||
assertEquals("qaiu", YsTool.extractDlmc(NEW_GATE));
|
||||
assertNull(YsTool.extractHtxx(NEW_GATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLegacyPasswordPageStillParsed() {
|
||||
assertTrue(YsTool.isPasswordPage(OLD_GATE));
|
||||
assertEquals("legacy-token", YsTool.extractAntiforgery(OLD_GATE));
|
||||
assertNull(YsTool.extractDlmc(OLD_GATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewTokenWinsWhenBothFormatsExist() {
|
||||
String mixed = NEW_GATE + OLD_GATE;
|
||||
assertEquals("new-token_value.1", YsTool.extractAntiforgery(mixed));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleQuotedAttributes() {
|
||||
String html = "<div id='entryConfig' data-dlmc='sohehe4' "
|
||||
+ "data-verification-token='quoted-token'></div>";
|
||||
assertTrue(YsTool.isPasswordPage(html));
|
||||
assertEquals("quoted-token", YsTool.extractAntiforgery(html));
|
||||
assertEquals("sohehe4", YsTool.extractDlmc(html));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSpaceIsNotPasswordPageAndKeepsNestedHtxx() {
|
||||
assertFalse(YsTool.isPasswordPage(OPEN_SPACE));
|
||||
JsonObject htxx = YsTool.extractHtxx(OPEN_SPACE);
|
||||
assertEquals("fufu1", htxx.getString("dlmc"));
|
||||
assertEquals("https://c6.ysepan.com/api/", htxx.getString("qqdz"));
|
||||
assertFalse(htxx.getJsonObject("yh").getBoolean("isgly"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testYs168RedirectTargetsYsepanPasswordEndpoint() {
|
||||
String next = YsTool.resolveRedirect("https://qaiu.ys168.com/", "https://qaiu.ysepan.com");
|
||||
assertEquals("https://qaiu.ysepan.com", next);
|
||||
assertEquals("https://qaiu.ysepan.com", YsTool.originOf(next));
|
||||
assertEquals("https://qaiu.ysepan.com/?handler=VerifyPassword",
|
||||
YsTool.verifyPasswordEndpoint(YsTool.originOf(next)));
|
||||
assertFalse(YsTool.verifyPasswordEndpoint(YsTool.originOf(next)).contains("ys168.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtocolRelativeAndRelativeRedirects() {
|
||||
String protocolRelative = YsTool.resolveRedirect(
|
||||
"https://qaiu.ys168.com/", "//qaiu.ysepan.com/");
|
||||
assertEquals("https://qaiu.ysepan.com/", protocolRelative);
|
||||
assertEquals("https://qaiu.ysepan.com", YsTool.originOf(protocolRelative));
|
||||
|
||||
String relative = YsTool.resolveRedirect(
|
||||
"https://qaiu.ysupan.com/", "/login.aspx?d=qaiu");
|
||||
assertEquals("https://qaiu.ysupan.com/login.aspx?d=qaiu", relative);
|
||||
assertEquals("https://qaiu.ysupan.com", YsTool.originOf(relative));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonDefaultPortKeptInOrigin() {
|
||||
assertEquals("https://y.ys168.com:8100", YsTool.originOf("https://y.ys168.com:8100/"));
|
||||
assertEquals("https://qaiu.ysepan.com", YsTool.originOf("https://qaiu.ysepan.com:443/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrongPasswordUsesUpstreamMessageWithoutSecrets() {
|
||||
String body = "{\"success\":false,\"code\":\"invalid_password\",\"message\":\"密码错误\"}";
|
||||
assertEquals("密码错误", YsTool.passwordFailureMessage(400, body));
|
||||
|
||||
String leaked = "{\"success\":false,\"code\":\"invalid_password\",\"message\":\"eyJhbGciOiJIUzI1NiJ9.secret\"}";
|
||||
assertEquals("密码错误", YsTool.passwordFailureMessage(400, leaked));
|
||||
|
||||
String other = "{\"success\":false,\"message\":\"空间已关闭\"}";
|
||||
assertEquals("空间已关闭", YsTool.passwordFailureMessage(400, other));
|
||||
|
||||
assertEquals("密码验证失败 HTTP 500", YsTool.passwordFailureMessage(500, "<html>CfDJ8secret</html>"));
|
||||
assertEquals("密码验证失败 HTTP 400", YsTool.passwordFailureMessage(400, ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlankRedirectIsRejected() {
|
||||
assertNull(YsTool.resolveRedirect("https://qaiu.ys168.com/", " "));
|
||||
assertNull(YsTool.resolveRedirect("https://qaiu.ys168.com/", null));
|
||||
assertNull(YsTool.originOf("not a url"));
|
||||
assertFalse(YsTool.isPasswordPage(null));
|
||||
assertNull(YsTool.extractAntiforgery(null));
|
||||
}
|
||||
}
|
||||
@@ -97,23 +97,13 @@ public class YsToolTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfficialSampleUrlRealDownload() throws Exception {
|
||||
String official = "https://ys-l.ysepan.com/wap/yssl/A95UIe495EkSKE/Bc8hF8Nsbl2I4Ec6vAm5EGe9HU36iC/lu20.jpg";
|
||||
String withForcePrefix = "https://ys-l.ysepan.com/wap/yssl/_A95UIe495EkSKE/Bc8hF8Nsbl2I4Ec6vAm5EGe9HU36iC/lu20.jpg";
|
||||
|
||||
Buffer ok = download(official, "https://yssl.ysepan.com/");
|
||||
assertTrue("官方直链应能下载到 JPEG", ok.length() > 1000);
|
||||
assertEquals((byte) 0xFF, ok.getByte(0));
|
||||
assertEquals((byte) 0xD8, ok.getByte(1));
|
||||
|
||||
int forceStatus = webClient.getAbs(withForcePrefix)
|
||||
.putHeader("User-Agent", "Mozilla/5.0")
|
||||
.putHeader("Referer", "https://yssl.ysepan.com/")
|
||||
.send()
|
||||
.toCompletionStage().toCompletableFuture()
|
||||
.get(30, TimeUnit.SECONDS)
|
||||
.statusCode();
|
||||
assertNotEquals("带 _ 前缀的直链应失败(文件不存在)", 200, forceStatus);
|
||||
public void testYsslOpenSpaceRealDownload() throws Exception {
|
||||
// 旧样例 lu20.jpg 已从示例空间删除(直链 404)。改为现场列出示例空间里的小文件再下载。
|
||||
FileInfo file = firstSmallFile("https://yssl.ysepan.com/", null, 2_000_000);
|
||||
assertNotNull("示例空间应至少有一个可下载的小文件", file);
|
||||
Buffer body = downloadParsedFile(file, "https://yssl.ysepan.com/");
|
||||
assertEquals(file.getSize().longValue(), body.length());
|
||||
System.out.println("yssl real download ok, file=" + file.getFileName() + ", size=" + body.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -249,46 +239,175 @@ public class YsToolTest {
|
||||
assertTrue("sohehe4 应有多个目录", dirs.size() >= 3);
|
||||
assertTrue(dirs.stream().allMatch(d -> "folder".equals(d.getFileType())));
|
||||
|
||||
String dirId = dirs.stream()
|
||||
.filter(d -> d.getFileName() != null && d.getFileName().contains("留言"))
|
||||
.map(FileInfo::getFileId)
|
||||
FileInfo file = null;
|
||||
for (FileInfo dir : dirs) {
|
||||
if (!"folder".equals(dir.getFileType())) {
|
||||
continue;
|
||||
}
|
||||
file = firstSmallFileInDir("https://sohehe4.ysepan.com/", "1234", dir.getFileId(), 5_000_000);
|
||||
if (file != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertNotNull("sohehe4 应能找到可下载的小文件", file);
|
||||
Buffer body = downloadParsedFile(file, "https://sohehe4.ysepan.com/");
|
||||
assertEquals(file.getSize().longValue(), body.length());
|
||||
System.out.println("sohehe4 real download ok, file=" + file.getFileName()
|
||||
+ ", size=" + body.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQaiuRequiresPassword() throws Exception {
|
||||
ParserCreate create = ParserCreate.fromShareUrl("https://qaiu.ysepan.com/");
|
||||
create.getShareLinkInfo().getOtherParam().put("domainName", "http://localhost");
|
||||
try {
|
||||
create.createTool().parseFileList()
|
||||
.toCompletionStage().toCompletableFuture()
|
||||
.get(60, TimeUnit.SECONDS);
|
||||
fail("未提供密码时应拒绝进入");
|
||||
} catch (java.util.concurrent.ExecutionException e) {
|
||||
assertTrue(e.getCause().getMessage(), e.getCause().getMessage().contains("空间需要访问密码"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFufu1Ys168RedirectOpenSpace() throws Exception {
|
||||
List<FileInfo> roots = fileList("https://fufu1.ys168.com/", null);
|
||||
assertNotNull(roots);
|
||||
assertTrue(roots.stream().anyMatch(f -> "游戏3".equals(f.getFileName())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQaiuYs168PasswordRedirectAndRealDownload() throws Exception {
|
||||
List<FileInfo> dirs = fileList("https://qaiu.ys168.com/", "qaiuys168");
|
||||
assertNotNull(dirs);
|
||||
assertFalse(dirs.isEmpty());
|
||||
FileInfo app = dirs.stream()
|
||||
.filter(d -> d.getFileName() != null && d.getFileName().contains("app"))
|
||||
.findFirst()
|
||||
.orElse(dirs.get(0).getFileId());
|
||||
.orElse(dirs.get(0));
|
||||
|
||||
ParserCreate filesCreate = ParserCreate.fromShareUrl("https://sohehe4.ysepan.com/");
|
||||
filesCreate.getShareLinkInfo().setSharePassword("1234");
|
||||
filesCreate.getShareLinkInfo().getOtherParam().put("domainName", "http://localhost");
|
||||
filesCreate.getShareLinkInfo().getOtherParam().put("dirId", dirId);
|
||||
|
||||
List<FileInfo> files = filesCreate.createTool().parseFileList()
|
||||
.toCompletionStage().toCompletableFuture()
|
||||
.get(60, TimeUnit.SECONDS);
|
||||
|
||||
assertNotNull(files);
|
||||
assertFalse(files.isEmpty());
|
||||
|
||||
FileInfo file = files.stream()
|
||||
List<FileInfo> files = fileList("https://qaiu.ys168.com/", "qaiuys168", app.getFileId());
|
||||
FileInfo zip = files.stream()
|
||||
.filter(f -> "file".equals(f.getFileType()))
|
||||
.filter(f -> f.getSize() != null && f.getSize() > 0 && f.getSize() < 5_000_000)
|
||||
.filter(f -> f.getFileName() != null && f.getFileName().contains("Pycharm"))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (file != null) {
|
||||
String param = file.getParserUrl().substring(file.getParserUrl().lastIndexOf('/') + 1);
|
||||
JsonObject paramJson = new JsonObject(CommonUtils.urlBase64Decode(param));
|
||||
String downloadUrl = paramJson.getString("downloadUrl");
|
||||
assertNotNull(downloadUrl);
|
||||
assertFalse(downloadUrl.contains("/_"));
|
||||
assertNotNull("ys168 重定向后应能列出 Pycharm 压缩包", zip);
|
||||
|
||||
Buffer body = download(downloadUrl, "https://sohehe4.ysepan.com/");
|
||||
assertEquals(file.getSize().longValue(), body.length());
|
||||
System.out.println("sohehe4 real download ok, file=" + file.getFileName()
|
||||
+ ", size=" + body.length());
|
||||
} else {
|
||||
System.out.println("sohehe4 dir=" + dirId + " entries=" + files.size()
|
||||
+ " (no small file for real download sample)");
|
||||
String param = zip.getParserUrl().substring(zip.getParserUrl().lastIndexOf('/') + 1);
|
||||
JsonObject paramJson = new JsonObject(CommonUtils.urlBase64Decode(param));
|
||||
String downloadUrl = paramJson.getString("downloadUrl");
|
||||
assertNotNull(downloadUrl);
|
||||
assertTrue(downloadUrl.contains("ysepan.com"));
|
||||
assertFalse(downloadUrl.contains("ys168.com"));
|
||||
assertFalse(downloadUrl.contains("/_"));
|
||||
|
||||
Buffer body = download(downloadUrl, "https://qaiu.ysepan.com/");
|
||||
assertEquals(zip.getSize().longValue(), body.length());
|
||||
assertEquals('P', (char) body.getByte(0));
|
||||
assertEquals('K', (char) body.getByte(1));
|
||||
System.out.println("qaiu ys168 redirect download ok, size=" + body.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBackupDomainsOpenAndPasswordSpaces() throws Exception {
|
||||
for (String host : new String[]{"cccpan.com", "uupan.net", "ysok.net"}) {
|
||||
List<FileInfo> openDirs = fileList("https://fufu1." + host + "/", null);
|
||||
assertNotNull(host, openDirs);
|
||||
assertFalse(host + " 无密码空间目录为空", openDirs.isEmpty());
|
||||
assertTrue(host, openDirs.stream().anyMatch(d -> "游戏3".equals(d.getFileName())));
|
||||
|
||||
List<FileInfo> lockedDirs = fileList("https://qaiu." + host + "/", "qaiuys168");
|
||||
assertNotNull(host, lockedDirs);
|
||||
assertFalse(host + " 密码空间目录为空", lockedDirs.isEmpty());
|
||||
assertTrue(host + " " + lockedDirs.stream().map(FileInfo::getFileName).toList(),
|
||||
lockedDirs.stream().anyMatch(d -> d.getFileName() != null && d.getFileName().contains("app")));
|
||||
}
|
||||
}
|
||||
|
||||
private static List<FileInfo> fileList(String url, String pwd) throws Exception {
|
||||
return fileList(url, pwd, null, null);
|
||||
}
|
||||
|
||||
private static List<FileInfo> fileList(String url, String pwd, String dirId) throws Exception {
|
||||
return fileList(url, pwd, dirId, null);
|
||||
}
|
||||
|
||||
private static List<FileInfo> fileList(String url, String pwd, String dirId, String zml) throws Exception {
|
||||
ParserCreate create = ParserCreate.fromShareUrl(url);
|
||||
if (pwd != null) {
|
||||
create.getShareLinkInfo().setSharePassword(pwd);
|
||||
}
|
||||
create.getShareLinkInfo().getOtherParam().put("domainName", "http://localhost");
|
||||
if (dirId != null) {
|
||||
create.getShareLinkInfo().getOtherParam().put("dirId", dirId);
|
||||
}
|
||||
if (zml != null) {
|
||||
create.getShareLinkInfo().getOtherParam().put("zml", zml);
|
||||
}
|
||||
return create.createTool().parseFileList()
|
||||
.toCompletionStage().toCompletableFuture()
|
||||
.get(60, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/** 在目录及其 zml 子目录中找一个体积合适的真实文件。 */
|
||||
private static FileInfo firstSmallFile(String url, String pwd, long maxSize) throws Exception {
|
||||
List<FileInfo> dirs = fileList(url, pwd);
|
||||
assertNotNull(dirs);
|
||||
assertFalse(url + " 目录为空", dirs.isEmpty());
|
||||
for (FileInfo dir : dirs) {
|
||||
if (!"folder".equals(dir.getFileType())) {
|
||||
continue;
|
||||
}
|
||||
FileInfo file = firstSmallFileInDir(url, pwd, dir.getFileId(), maxSize);
|
||||
if (file != null) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static FileInfo firstSmallFileInDir(String url, String pwd, String dirId, long maxSize) throws Exception {
|
||||
List<FileInfo> entries = fileList(url, pwd, dirId);
|
||||
FileInfo direct = smallFile(entries, maxSize);
|
||||
if (direct != null) {
|
||||
return direct;
|
||||
}
|
||||
for (FileInfo entry : entries) {
|
||||
if (!"folder".equals(entry.getFileType()) || entry.getFileId() == null
|
||||
|| !entry.getFileId().contains(":")) {
|
||||
continue;
|
||||
}
|
||||
FileInfo nested = smallFile(fileList(url, pwd, dirId, entry.getFileName()), maxSize);
|
||||
if (nested != null) {
|
||||
return nested;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static FileInfo smallFile(List<FileInfo> entries, long maxSize) {
|
||||
if (entries == null) {
|
||||
return null;
|
||||
}
|
||||
return entries.stream()
|
||||
.filter(f -> "file".equals(f.getFileType()))
|
||||
.filter(f -> f.getSize() != null && f.getSize() > 0 && f.getSize() < maxSize)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private static Buffer downloadParsedFile(FileInfo file, String referer) throws Exception {
|
||||
assertNotNull(file.getParserUrl());
|
||||
String param = file.getParserUrl().substring(file.getParserUrl().lastIndexOf('/') + 1);
|
||||
JsonObject paramJson = new JsonObject(CommonUtils.urlBase64Decode(param));
|
||||
String downloadUrl = paramJson.getString("downloadUrl");
|
||||
assertNotNull(downloadUrl);
|
||||
assertFalse(downloadUrl.contains("/_"));
|
||||
return download(downloadUrl, referer);
|
||||
}
|
||||
|
||||
private static Buffer download(String url, String referer) throws Exception {
|
||||
return webClient.getAbs(url)
|
||||
.putHeader("User-Agent",
|
||||
|
||||
Reference in new issue
Block a user