mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-11-27 15:47:00 +00:00
fix(parser): 分享链接提取码不再污染 shareKey,Cloudreve 探测避免提前失败
完整分享 URL 中的 ?pwd= / ?password= / ?code= 以及 # 片段不再被吞进 shareKey;小飞机、123、蓝奏等粘贴带提取码的链接时可正确带上密码。 同时收紧小飞机与蓝奏优享的路径正则。 CeTool 版本探测改用 tryParseJson:上游返回 HTML 时不再经由 asJson() 调用 fail(),以免阻断 nextParser 责任链(表现为「json格式异常」)。
This commit is contained in:
1 parent
fcf8f3d71f
commit
e4db064a71
8 files changed
+426
-28
No files matched your search
@@ -394,6 +394,33 @@ public abstract class PanBase implements IPanTool, Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试将响应体解析为 JsonObject,失败时返回 null,且不调用 fail()。
|
||||
* 用于版本探测等允许非 JSON(例如 HTML)的场景,避免提前终止解析并阻断 nextParser()。
|
||||
*
|
||||
* @param res HttpResponse
|
||||
* @return JsonObject,若响应体不是合法 JSON 则返回 null
|
||||
*/
|
||||
protected JsonObject tryParseJson(HttpResponse<?> res) {
|
||||
String contentEncoding = res.getHeader("Content-Encoding");
|
||||
try {
|
||||
if ("gzip".equalsIgnoreCase(contentEncoding) && res.body() instanceof Buffer gzipBody
|
||||
&& gzipBody.length() >= 2
|
||||
&& (gzipBody.getByte(0) & 0xff) == 0x1f
|
||||
&& (gzipBody.getByte(1) & 0xff) == 0x8b) {
|
||||
String decompressed = decompressGzip(gzipBody);
|
||||
return new JsonObject(decompressed);
|
||||
} else {
|
||||
return res.bodyAsJsonObject();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.debug("响应体不是合法JSON,跳过: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* body To text的封装, 会自动处理异常, 会自动解压gzip
|
||||
/**
|
||||
* body To text的封装, 会自动处理异常, 会自动解压gzip
|
||||
* @param res HttpResponse
|
||||
|
||||
@@ -25,7 +25,7 @@ public enum PanDomainTemplate {
|
||||
|
||||
// https://www.ilanzou.com/s/
|
||||
IZ("蓝奏云优享",
|
||||
compile("https://www\\.ilanzou\\.com/s/(?<KEY>.+)"),
|
||||
compile("https://www\\.ilanzou\\.com/s/(?<KEY>[^/?#]+)(?:[?#].*)?"),
|
||||
"https://www.ilanzou.com/s/{shareKey}",
|
||||
IzTool.class),
|
||||
// 网盘定义
|
||||
@@ -111,7 +111,7 @@ public enum PanDomainTemplate {
|
||||
// https://www.feijix.com/s/
|
||||
// https://share.feijipan.com/s/
|
||||
FJ("小飞机网盘",
|
||||
compile("https://(share\\.feijipan\\.com|www\\.feijix\\.com)/s/(?<KEY>.+)"),
|
||||
compile("https://(share\\.feijipan\\.com|www\\.feijix\\.com)/s/(?<KEY>[^/?#]+)(?:[?#].*)?"),
|
||||
"https://www.feijix.com/s/{shareKey}",
|
||||
FjTool.class),
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.qaiu.parser.custom.CustomParserConfig;
|
||||
import cn.qaiu.parser.custom.CustomParserRegistry;
|
||||
import cn.qaiu.parser.customjs.JsParserExecutor;
|
||||
|
||||
import cn.qaiu.util.ShareUrlParams;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
@@ -96,6 +97,8 @@ public class ParserCreate {
|
||||
}
|
||||
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||
|
||||
applyShareUrlParams(shareLinkInfo, shareUrl);
|
||||
|
||||
// 设置标准URL
|
||||
if (customParserConfig.getStandardUrlTemplate() != null) {
|
||||
String standardUrl = customParserConfig.getStandardUrlTemplate()
|
||||
@@ -126,6 +129,10 @@ public class ParserCreate {
|
||||
Matcher matcher = this.panDomainTemplate.getPattern().matcher(shareUrl);
|
||||
if (matcher.find()) {
|
||||
String k0 = matcher.group(KEY);
|
||||
String cleanedKey = ShareUrlParams.cleanShareKey(k0);
|
||||
if (StringUtils.isNotEmpty(cleanedKey)) {
|
||||
k0 = cleanedKey;
|
||||
}
|
||||
String shareKey = URLEncoder.encode(k0, StandardCharsets.UTF_8);
|
||||
|
||||
// 返回规范化的标准链接
|
||||
@@ -142,6 +149,15 @@ public class ParserCreate {
|
||||
|
||||
shareLinkInfo.setShareUrl(shareUrl);
|
||||
shareLinkInfo.setShareKey(shareKey);
|
||||
if (StringUtils.isBlank(shareLinkInfo.getSharePassword())) {
|
||||
String pwdFromUrl = ShareUrlParams.extractSharePassword(shareUrl);
|
||||
if (StringUtils.isNotEmpty(pwdFromUrl)) {
|
||||
shareLinkInfo.setSharePassword(pwdFromUrl);
|
||||
if (standardUrl != null && standardUrl.contains("{pwd}")) {
|
||||
standardUrl = standardUrl.replace("{pwd}", pwdFromUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isGenericBuiltInParser(panDomainTemplate)) {
|
||||
shareLinkInfo.setStandardUrl(standardUrl);
|
||||
}
|
||||
@@ -290,6 +306,8 @@ public class ParserCreate {
|
||||
}
|
||||
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||
|
||||
applyShareUrlParams(shareLinkInfo, shareUrl);
|
||||
|
||||
// 设置标准URL(如果有模板)
|
||||
if (customConfig.getStandardUrlTemplate() != null) {
|
||||
String standardUrl = customConfig.getStandardUrlTemplate()
|
||||
@@ -410,6 +428,23 @@ public class ParserCreate {
|
||||
return customParserConfig;
|
||||
}
|
||||
|
||||
|
||||
private static void applyShareUrlParams(ShareLinkInfo shareLinkInfo, String shareUrl) {
|
||||
String key = shareLinkInfo.getShareKey();
|
||||
if (StringUtils.isNotEmpty(key)) {
|
||||
String cleaned = ShareUrlParams.cleanShareKey(key);
|
||||
if (StringUtils.isNotEmpty(cleaned)) {
|
||||
shareLinkInfo.setShareKey(cleaned);
|
||||
}
|
||||
}
|
||||
if (StringUtils.isBlank(shareLinkInfo.getSharePassword())) {
|
||||
String pwdFromUrl = ShareUrlParams.extractSharePassword(shareUrl);
|
||||
if (StringUtils.isNotEmpty(pwdFromUrl)) {
|
||||
shareLinkInfo.setSharePassword(pwdFromUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isGenericBuiltInParser(PanDomainTemplate panDomainTemplate) {
|
||||
return GENERIC_BUILT_IN_PARSERS.contains(panDomainTemplate);
|
||||
}
|
||||
|
||||
@@ -81,18 +81,14 @@ public class CeTool extends PanBase {
|
||||
// 禁止跟随重定向:assertPublicHost 只校验初始 host,自动 30x 会绕过 SSRF 防护
|
||||
clientNoRedirects.getAbs(pingUrlV4).send().onSuccess(res -> {
|
||||
if (res.statusCode() == 200) {
|
||||
try {
|
||||
JsonObject json = asJson(res);
|
||||
// v4 ping 成功且返回有效JSON,使用 Ce4Tool
|
||||
if (json != null && !json.isEmpty()) {
|
||||
log.debug("检测到Cloudreve 4.x (通过v4 ping)");
|
||||
delegateToCe4Tool();
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// JSON解析失败,继续尝试 v3
|
||||
log.debug("v4 ping返回非JSON响应,尝试v3");
|
||||
// 使用 tryParseJson 而非 asJson,避免非JSON响应(如HTML)提前终止整个解析流程
|
||||
JsonObject json = tryParseJson(res);
|
||||
if (json != null && !json.isEmpty()) {
|
||||
log.debug("检测到Cloudreve 4.x (通过v4 ping)");
|
||||
delegateToCe4Tool();
|
||||
return;
|
||||
}
|
||||
log.debug("v4 ping返回非JSON响应,尝试v3");
|
||||
}
|
||||
// v4 ping失败或返回非JSON,尝试 v3
|
||||
tryV3Ping(baseUrl, key, pwd);
|
||||
@@ -111,18 +107,14 @@ public class CeTool extends PanBase {
|
||||
|
||||
clientNoRedirects.getAbs(pingUrlV3).send().onSuccess(res -> {
|
||||
if (res.statusCode() == 200) {
|
||||
try {
|
||||
JsonObject json = asJson(res);
|
||||
// v3 ping 成功且返回有效JSON,进一步验证是否为 v3
|
||||
if (json != null && !json.isEmpty()) {
|
||||
// 尝试调用 v3 share API 来确认
|
||||
verifyV3AndParse(baseUrl, key, pwd);
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// JSON解析失败,不是Cloudreve盘
|
||||
log.debug("v3 ping返回非JSON响应,不是Cloudreve盘");
|
||||
// 使用 tryParseJson 而非 asJson,避免非JSON响应(如HTML)提前终止整个解析流程
|
||||
JsonObject json = tryParseJson(res);
|
||||
if (json != null && !json.isEmpty()) {
|
||||
// 尝试调用 v3 share API 来确认
|
||||
verifyV3AndParse(baseUrl, key, pwd);
|
||||
return;
|
||||
}
|
||||
log.debug("v3 ping返回非JSON响应,不是Cloudreve盘");
|
||||
}
|
||||
// v3 ping失败,不是Cloudreve盘
|
||||
log.debug("v3 ping失败,尝试下一个解析器");
|
||||
@@ -148,9 +140,9 @@ public class CeTool extends PanBase {
|
||||
httpRequest.send().onSuccess(res -> {
|
||||
try {
|
||||
if (res.statusCode() == 200) {
|
||||
JsonObject jsonObject = asJson(res);
|
||||
JsonObject jsonObject = tryParseJson(res);
|
||||
// 检查响应格式是否符合 v3 API
|
||||
if (jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0) {
|
||||
if (jsonObject != null && jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0) {
|
||||
// v3 share API 成功,确认是 v3 版本
|
||||
// 设置文件信息
|
||||
setFileInfo(jsonObject);
|
||||
@@ -184,9 +176,9 @@ public class CeTool extends PanBase {
|
||||
httpRequest.send().onSuccess(res -> {
|
||||
try {
|
||||
if (res.statusCode() == 200) {
|
||||
JsonObject jsonObject = asJson(res);
|
||||
JsonObject jsonObject = tryParseJson(res);
|
||||
// 检查响应格式是否符合 v4 API
|
||||
if (jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0) {
|
||||
if (jsonObject != null && jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0) {
|
||||
// v4 share API 成功,使用 Ce4Tool
|
||||
log.debug("确认是Cloudreve 4.x (通过v4 share API)");
|
||||
delegateToCe4Tool();
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package cn.qaiu.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 从分享链接中整理 shareKey、提取常见提取码查询参数。
|
||||
* 许多网盘模板用贪婪的 KEY 分组匹配路径,会把 ?pwd= / ?code= 或 # 片段吞进 shareKey。
|
||||
*/
|
||||
public final class ShareUrlParams {
|
||||
|
||||
private static final String[] PASSWORD_QUERY_KEYS = {"pwd", "password", "code"};
|
||||
|
||||
private ShareUrlParams() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉误捕获的查询串和 hash 片段,保留路径中的 shareKey。
|
||||
* 若清理后为空则返回原始值,避免把合法 key 清掉。
|
||||
*/
|
||||
public static String cleanShareKey(String key) {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return key;
|
||||
}
|
||||
String cleaned = key;
|
||||
int hashIndex = cleaned.indexOf('#');
|
||||
if (hashIndex >= 0) {
|
||||
cleaned = cleaned.substring(0, hashIndex);
|
||||
}
|
||||
int queryIndex = cleaned.indexOf('?');
|
||||
if (queryIndex >= 0) {
|
||||
cleaned = cleaned.substring(0, queryIndex);
|
||||
}
|
||||
return StringUtils.isEmpty(cleaned) ? key : cleaned;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从完整分享 URL(或误带在 key 里的查询串)提取提取码。
|
||||
* 优先 {@code pwd},其次 {@code password}、{@code code}(小飞机等)。
|
||||
*/
|
||||
public static String extractSharePassword(String shareUrl) {
|
||||
if (StringUtils.isBlank(shareUrl)) {
|
||||
return null;
|
||||
}
|
||||
String query = queryString(shareUrl);
|
||||
if (StringUtils.isEmpty(query)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> params = parseQuery(query);
|
||||
for (String name : PASSWORD_QUERY_KEYS) {
|
||||
String value = params.get(name);
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static String queryString(String url) {
|
||||
String noHash = url;
|
||||
int hashIndex = url.indexOf('#');
|
||||
if (hashIndex >= 0) {
|
||||
noHash = url.substring(0, hashIndex);
|
||||
}
|
||||
int queryIndex = noHash.indexOf('?');
|
||||
if (queryIndex < 0 || queryIndex == noHash.length() - 1) {
|
||||
return null;
|
||||
}
|
||||
return noHash.substring(queryIndex + 1);
|
||||
}
|
||||
|
||||
static Map<String, String> parseQuery(String query) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
String[] pairs = query.split("&");
|
||||
for (String pair : pairs) {
|
||||
if (pair.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
int eq = pair.indexOf('=');
|
||||
String rawName = eq >= 0 ? pair.substring(0, eq) : pair;
|
||||
String rawValue = eq >= 0 ? pair.substring(eq + 1) : "";
|
||||
String name = decode(rawName).toLowerCase();
|
||||
if (name.isEmpty() || params.containsKey(name)) {
|
||||
continue;
|
||||
}
|
||||
params.put(name, decode(rawValue));
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
private static String decode(String value) {
|
||||
try {
|
||||
return URLDecoder.decode(value, StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user