fix(parser): 分享链接提取码不再污染 shareKey,Cloudreve 探测避免提前失败

完整分享 URL 中的 ?pwd= / ?password= / ?code= 以及 # 片段不再被吞进
shareKey;小飞机、123、蓝奏等粘贴带提取码的链接时可正确带上密码。
同时收紧小飞机与蓝奏优享的路径正则。

CeTool 版本探测改用 tryParseJson:上游返回 HTML 时不再经由 asJson()
调用 fail(),以免阻断 nextParser 责任链(表现为「json格式异常」)。
This commit is contained in:
qaiu committed 2026-11-27 15:44:28 +08:00
1 parent fcf8f3d71f
commit e4db064a71
8 files changed
+426 -28

No files matched your search

@@ -0,0 +1,131 @@
package cn.qaiu.parser;
import cn.qaiu.WebClientVertxInit;
import cn.qaiu.entity.ShareLinkInfo;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpResponse;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* 探测场景必须用 tryParseJson:asJson 会 fail(),从而阻断 nextParser。
*/
public class PanBaseTryParseJsonTest {
private static Vertx vertx;
@BeforeClass
public static void setUpClass() {
// 不在用例结束后关闭 Vertx:共享 WebClient 绑定该实例,关闭会导致同 JVM 后续测试出现 Client is closed
vertx = Vertx.vertx();
WebClientVertxInit.init(vertx);
}
private static final class ProbeTool extends PanBase {
ProbeTool() {
super();
this.shareLinkInfo = ShareLinkInfo.newBuilder()
.type("ce")
.panName("Cloudreve")
.shareKey("k")
.shareUrl("https://example.com/s/k")
.build();
}
@Override
public io.vertx.core.Future<String> parse() {
return promise.future();
}
JsonObject probe(HttpResponse<?> res) {
return tryParseJson(res);
}
JsonObject parseOrFail(HttpResponse<?> res) {
return asJson(res);
}
boolean promiseCompleted() {
return promise.future().isComplete();
}
boolean promiseFailed() {
return promise.future().failed();
}
String failureMessage() {
return promise.future().cause() == null ? null : promise.future().cause().getMessage();
}
}
@SuppressWarnings("unchecked")
private static HttpResponse<Buffer> stub(String contentEncoding, String body) {
Buffer buffer = body == null ? null : Buffer.buffer(body);
return (HttpResponse<Buffer>) Proxy.newProxyInstance(
HttpResponse.class.getClassLoader(),
new Class<?>[]{HttpResponse.class},
(proxy, method, args) -> {
switch (method.getName()) {
case "getHeader":
String name = args != null && args.length > 0 ? String.valueOf(args[0]) : "";
return "Content-Encoding".equalsIgnoreCase(name) ? contentEncoding : null;
case "body":
return buffer;
case "bodyAsString":
return body;
case "bodyAsJsonObject":
if (body == null) {
return null;
}
return new JsonObject(body);
default:
if (method.isDefault()) {
return InvocationHandler.invokeDefault(proxy, method, args);
}
Class<?> type = method.getReturnType();
if (type == boolean.class) {
return false;
}
if (type == int.class || type == long.class) {
return 0;
}
return null;
}
});
}
@Test
public void tryParseJsonReturnsNullForHtmlWithoutFailingPromise() {
ProbeTool tool = new ProbeTool();
JsonObject json = tool.probe(stub(null, "<html><body>not json</body></html>"));
assertNull(json);
assertFalse(tool.promiseCompleted());
}
@Test
public void tryParseJsonParsesObject() {
ProbeTool tool = new ProbeTool();
JsonObject json = tool.probe(stub(null, "{\"code\":0,\"data\":\"ok\"}"));
assertEquals(0, json.getInteger("code").intValue());
assertFalse(tool.promiseCompleted());
}
@Test
public void asJsonFailsPromiseOnHtml() {
ProbeTool tool = new ProbeTool();
JsonObject json = tool.parseOrFail(stub(null, "<!DOCTYPE html>"));
assertTrue(json == null || json.isEmpty());
assertTrue(tool.promiseFailed());
assertTrue(tool.failureMessage(), tool.failureMessage().contains("json格式异常"));
}
}
@@ -0,0 +1,71 @@
package cn.qaiu.parser;
import cn.qaiu.entity.ShareLinkInfo;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* 完整分享链接里的提取码不应被吞进 shareKey。
*/
public class ShareUrlQueryTest {
@Test
public void feijipanCodeQueryBecomesPassword() {
ShareLinkInfo info = ParserCreate.fromShareUrl("https://share.feijipan.com/s/SjfEaY2z?code=064d")
.getShareLinkInfo();
assertEquals("fj", info.getType());
assertEquals("SjfEaY2z", info.getShareKey());
assertEquals("064d", info.getSharePassword());
assertTrue(info.getStandardUrl().contains("SjfEaY2z"));
assertTrue("standard url should not keep the query in the key",
!info.getStandardUrl().contains("code=064d") || !info.getStandardUrl().contains("SjfEaY2z?"));
}
@Test
public void yePwdQueryIsExtractedWithoutChangingKey() {
ShareLinkInfo info = ParserCreate.fromShareUrl("https://www.123278.com/s/lN7UVv-pbYJ.html?pwd=abcd")
.getShareLinkInfo();
assertEquals("ye", info.getType());
assertEquals("lN7UVv-pbYJ", info.getShareKey());
assertEquals("abcd", info.getSharePassword());
}
@Test
public void lanzouQueryPasswordAndWebpageDoNotPolluteKey() {
ShareLinkInfo info = ParserCreate.fromShareUrl("https://a.lanzouw.com/iabc?webpage=wp1&pwd=secret")
.getShareLinkInfo();
assertEquals("lz", info.getType());
assertEquals("iabc", info.getShareKey());
assertEquals("secret", info.getSharePassword());
assertTrue(info.getShareUrl().contains("webpage=wp1"));
}
@Test
public void ilanzouHashAndCodeQuery() {
ShareLinkInfo info = ParserCreate.fromShareUrl("https://www.ilanzou.com/s/RkR09zIL?code=abc#/list")
.getShareLinkInfo();
assertEquals("iz", info.getType());
assertEquals("RkR09zIL", info.getShareKey());
assertEquals("abc", info.getSharePassword());
}
@Test
public void ucPublicQueryIsNotTreatedAsPassword() {
ShareLinkInfo info = ParserCreate.fromShareUrl("https://drive.uc.cn/s/e623b6da278e4?public=1#/list/share")
.getShareLinkInfo();
assertEquals("uc", info.getType());
assertEquals("e623b6da278e4", info.getShareKey());
assertTrue(info.getSharePassword() == null || info.getSharePassword().isEmpty());
}
@Test
public void plainLanzouUrlUnchanged() {
ShareLinkInfo info = ParserCreate.fromShareUrl("https://wwn.lanzouy.com/ihLkw1gezutg")
.getShareLinkInfo();
assertEquals("lz", info.getType());
assertEquals("ihLkw1gezutg", info.getShareKey());
assertTrue(info.getSharePassword() == null || info.getSharePassword().isEmpty());
}
}
@@ -0,0 +1,40 @@
package cn.qaiu.util;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ShareUrlParamsTest {
@Test
public void cleanShareKeyStripsQueryAndHash() {
assertEquals("SjfEaY2z", ShareUrlParams.cleanShareKey("SjfEaY2z?code=064d"));
assertEquals("lN7UVv-pbYJ", ShareUrlParams.cleanShareKey("lN7UVv-pbYJ?pwd=abcd#/list"));
assertEquals("iabc", ShareUrlParams.cleanShareKey("iabc?webpage=wp1&pwd=secret"));
assertEquals("s/someShareKey", ShareUrlParams.cleanShareKey("s/someShareKey"));
assertEquals("key", ShareUrlParams.cleanShareKey("key"));
assertNull(ShareUrlParams.cleanShareKey(null));
assertEquals("", ShareUrlParams.cleanShareKey(""));
}
@Test
public void extractPasswordFromCommonQueryNames() {
assertEquals("064d", ShareUrlParams.extractSharePassword("https://share.feijipan.com/s/SjfEaY2z?code=064d"));
assertEquals("Pbdl", ShareUrlParams.extractSharePassword("https://www.123684.com/s/6gXfvd-7H9kh?pwd=Pbdl#/list"));
assertEquals("secret", ShareUrlParams.extractSharePassword(
"https://a.lanzouw.com/iabc?webpage=wp1&pwd=secret"));
assertEquals("I4KG7H", ShareUrlParams.extractSharePassword(
"https://www.vyuyun.com/s/QMa6ie?password=I4KG7H"));
assertEquals("测码", ShareUrlParams.extractSharePassword("https://example.com/s/k?pwd=%E6%B5%8B%E7%A0%81"));
assertNull(ShareUrlParams.extractSharePassword("https://drive.uc.cn/s/abc?public=1#/list/share"));
assertNull(ShareUrlParams.extractSharePassword("https://www.123pan.com/s/abc"));
assertNull(ShareUrlParams.extractSharePassword("https://www.123pan.com/s/abc?pwd="));
}
@Test
public void prefersPwdOverCode() {
assertEquals("fromPwd", ShareUrlParams.extractSharePassword(
"https://example.com/s/k?code=fromCode&pwd=fromPwd"));
}
}