mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-04-11 03:16:54 +00:00
Compare commits
26 Commits
fdf067c25e
...
copilot/cl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24025e0ae6 | ||
|
|
d8bffc19ce | ||
|
|
a420bad305 | ||
|
|
6ef6e47580 | ||
|
|
94f83ec296 | ||
|
|
702569c701 | ||
|
|
d4940ca9ee | ||
|
|
dbd1c138ca | ||
|
|
0b49c55cf3 | ||
|
|
b1ec3b2eea | ||
|
|
9ea89feee7 | ||
|
|
4a843194a3 | ||
|
|
03503115fd | ||
|
|
1870aef60e | ||
|
|
ed8fd66d1e | ||
|
|
c1c4c8cdc5 | ||
|
|
256ec3b152 | ||
|
|
da490e5bbd | ||
|
|
ba0ac86eea | ||
|
|
b5544c4131 | ||
|
|
d94ea6aaf3 | ||
|
|
742dda8677 | ||
|
|
76e0db0cfb | ||
|
|
6458a6e2c5 | ||
|
|
cbf2294a8e | ||
|
|
9d558bf4e2 |
37
README.md
37
README.md
@@ -1,5 +1,17 @@
|
||||
<div align="center" style="display:flex; justify-content:center; gap:10px; align-items:flex-start;">
|
||||
<img
|
||||
src="https://github.com/user-attachments/assets/bf266d0a-aaf8-4772-9231-e38a4b7bb6cb"
|
||||
alt="image1"
|
||||
style="width:300px; max-width:300px; flex:none;"
|
||||
>
|
||||
<img
|
||||
src="https://github.com/user-attachments/assets/bb7a85f0-c256-4b4a-a11b-3ceb55afc302"
|
||||
alt="image2"
|
||||
style="width:300px; max-width:300px; flex:none;"
|
||||
>
|
||||
</div>
|
||||
<p align="center">
|
||||
<img src="https://github.com/user-attachments/assets/87401aae-b0b6-4ffb-bbeb-44756404d26f" alt="项目预览图" />
|
||||
<a href="https://trendshift.io/repositories/12101" target="_blank"><img src="https://trendshift.io/api/badge/repositories/12101" alt="qaiu%2Fnetdisk-fast-download | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@@ -8,10 +20,6 @@
|
||||
<a href="https://vertx-china.github.io"><img src="https://img.shields.io/badge/vert.x-4.5.22-blue?style=flat"></a>
|
||||
<a href="https://raw.githubusercontent.com/qaiu/netdisk-fast-download/master/LICENSE"><img src="https://img.shields.io/github/license/qaiu/netdisk-fast-download?style=flat"></a>
|
||||
<a href="https://github.com/qaiu/netdisk-fast-download/releases/"><img src="https://img.shields.io/github/v/release/qaiu/netdisk-fast-download?style=flat"></a>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
|
||||
# netdisk-fast-download 网盘分享链接云解析服务
|
||||
QQ交流群:1017480890
|
||||
@@ -21,18 +29,18 @@ netdisk-fast-download网盘直链云解析(nfd云解析)能把网盘分享下载
|
||||
## 快速开始
|
||||
命令行下载分享文件:
|
||||
```shell
|
||||
curl -LOJ "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234"
|
||||
curl -LOJ "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/Tk1F2kGQ&pwd=1234"
|
||||
```
|
||||
或者使用wget:
|
||||
```shell
|
||||
wget -O bilibili.mp4 "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234"
|
||||
wget -O bilibili.mp4 "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/Tk1F2kGQ&pwd=1234"
|
||||
```
|
||||
或者使用浏览器[直接访问](https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fparser%3Furl%3Dhttps%3A%2F%2Fshare.feijipan.com%2Fs%2FnQOaNRPW&name=bilibili.mp4&ext=mp4):
|
||||
或者使用浏览器[直接访问](https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fparser%3Furl%3Dhttps%3A%2F%2Fshare.feijipan.com%2Fs%2FTk1F2kGQ&name=bilibili.mp4&ext=mp4):
|
||||
```
|
||||
### 调用演示站下载:
|
||||
https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234
|
||||
https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/Tk1F2kGQ&pwd=1234
|
||||
### 调用演示站预览:
|
||||
https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fparser%3Furl%3Dhttps%3A%2F%2Fshare.feijipan.com%2Fs%2FnQOaNRPW&name=bilibili.mp4&ext=mp4
|
||||
https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fparser%3Furl%3Dhttps%3A%2F%2Fshare.feijipan.com%2Fs%2FTk1F2kGQ&name=bilibili.mp4&ext=mp4
|
||||
|
||||
```
|
||||
|
||||
@@ -42,10 +50,10 @@ https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.
|
||||
|
||||
**Playground功能:** [JS解析器演练场密码保护说明](web-service/doc/PLAYGROUND_PASSWORD_PROTECTION.md)
|
||||
|
||||
## 预览地址
|
||||
[预览地址1](https://lz.qaiu.top)
|
||||
[预览地址2](https://lz0.qaiu.top)
|
||||
[天翼云盘/移动云盘限时体验版](https://189.qaiu.top)
|
||||
## 体验地址
|
||||
[公益解析1](https://lz.qaiu.top)
|
||||
[公益解析2](https://lz0.qaiu.top)
|
||||
[大文件解析专属版,限时开放,注册体验](https://189.qaiu.top)
|
||||
|
||||
main分支依赖JDK17, 提供了JDK11分支[main-jdk11](https://github.com/qaiu/netdisk-fast-download/tree/main-jdk11)
|
||||
**0.1.8及以上版本json接口格式有调整 参考json返回数据格式示例**
|
||||
@@ -61,7 +69,6 @@ main分支依赖JDK17, 提供了JDK11分支[main-jdk11](https://github.com/qaiu/
|
||||
|
||||
- [蓝奏云-lz](https://pc.woozooo.com/)
|
||||
- [蓝奏云优享-iz](https://www.ilanzou.com/)
|
||||
- [奶牛快传-cow](https://cowtransfer.com/)
|
||||
- [移动云云空间-ec](https://www.ecpan.cn/web)
|
||||
- [小飞机网盘-fj](https://www.feijipan.com/)
|
||||
- [亿方云-fc](https://www.fangcloud.com/)
|
||||
|
||||
5
package.json
Normal file
5
package.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"mvn": "^3.5.0"
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
<groupId>cn.qaiu</groupId>
|
||||
<artifactId>parser</artifactId>
|
||||
<version>10.2.3</version>
|
||||
<version>10.2.5</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>cn.qaiu:parser</name>
|
||||
|
||||
@@ -68,42 +68,41 @@ public enum PanDomainTemplate {
|
||||
t-is.cn
|
||||
*/
|
||||
LZ("蓝奏云",
|
||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?(" +
|
||||
"lanzoul|" +
|
||||
"lanzouh|" +
|
||||
"lanosso|" +
|
||||
"lanpv|" +
|
||||
"bakstotre|" +
|
||||
"lanzouo|" +
|
||||
"lanzov|" +
|
||||
"lanpw|" +
|
||||
"ulanzou|" +
|
||||
"lanzouf|" +
|
||||
"lanzn|" +
|
||||
"lanzouj|" +
|
||||
"lanzouk|" +
|
||||
"lanzouq|" +
|
||||
"lanzouv|" +
|
||||
"lanzoue|" +
|
||||
"lanzouw|" +
|
||||
"lanzoub|" +
|
||||
"lanzouu|" +
|
||||
"lanwp|" +
|
||||
"lanzouy|" +
|
||||
"lanzoup|" +
|
||||
"woozooo|" +
|
||||
"lanzv|" +
|
||||
"dmpdmp|" +
|
||||
"lanrar|" +
|
||||
"webgetstore|" +
|
||||
"lanzb|" +
|
||||
"lanzoux|" +
|
||||
"lanzout|" +
|
||||
"lanzouc|" +
|
||||
"lanzoui|" +
|
||||
"lanzoug|" +
|
||||
"lanzoum" +
|
||||
")\\.com/(.+/)?(?<KEY>.+)"),
|
||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?(" +
|
||||
"lanzoul|" +
|
||||
"lanzouh|" +
|
||||
"lanosso|" +
|
||||
"lanpv|" +
|
||||
"bakstotre|" +
|
||||
"lanzouo|" +
|
||||
"lanzov|" +
|
||||
"lanpw|" +
|
||||
"ulanzou|" +
|
||||
"lanzouf|" +
|
||||
"lanzn|" +
|
||||
"lanzouj|" +
|
||||
"lanzouk|" +
|
||||
"lanzouq|" +
|
||||
"lanzouv|" +
|
||||
"lanzoue|" +
|
||||
"lanzouw|" +
|
||||
"lanzoub|" +
|
||||
"lanzouu|" +
|
||||
"lanwp|" +
|
||||
"lanzouy|" +
|
||||
"lanzoup|" +
|
||||
"woozooo|" +
|
||||
"lanzv|" +
|
||||
"dmpdmp|" +
|
||||
"lanrar|" +
|
||||
"lanzb|" +
|
||||
"lanzoux|" +
|
||||
"lanzout|" +
|
||||
"lanzouc|" +
|
||||
"lanzoui|" +
|
||||
"lanzoug|" +
|
||||
"lanzoum" +
|
||||
")\\.com/(?<KEY>.+)"),
|
||||
"https://w1.lanzn.com/{shareKey}",
|
||||
LzTool.class),
|
||||
|
||||
@@ -122,7 +121,7 @@ public enum PanDomainTemplate {
|
||||
|
||||
// https://v2.fangcloud.com/s/
|
||||
FC("亿方云",
|
||||
compile("https://v2\\.fangcloud\\.(com|cn)/(s|sharing)/(?<KEY>.+)"),
|
||||
compile("https://v2\\.fangcloud\\.(com|cn)/(s|share|sharing)/(?<KEY>.+)"),
|
||||
"https://v2.fangcloud.com/s/{shareKey}",
|
||||
"https://www.fangcloud.com/",
|
||||
FcTool.class),
|
||||
@@ -143,9 +142,41 @@ public enum PanDomainTemplate {
|
||||
compile("https://qfile\\.qq\\.com/q/(?<KEY>.+)"),
|
||||
"https://qfile.qq.com/q/{shareKey}",
|
||||
QQscTool.class),
|
||||
// https://f.ws59.cn/f/或者https://www.wenshushu.cn/f/
|
||||
// https://f.ws59.cn/f/ 或者 https://www.wenshushu.cn/f/ 等多个镜像域名
|
||||
/*
|
||||
f.wsNN.cn (如 f.ws59.cn, f.ws28.cn 等)
|
||||
www.wenshushu.cn
|
||||
新增域名:
|
||||
www.wenxiaozhan.net
|
||||
www.wenxiaozhan.cn
|
||||
www.wss.show
|
||||
www.ws28.cn
|
||||
www.wss.email
|
||||
www.wss1.cn
|
||||
www.ws59.cn
|
||||
www.wss.cc
|
||||
www.wss.pet
|
||||
www.wss.ink
|
||||
www.wenxiaozhan.com
|
||||
www.wenshushu.com
|
||||
www.wss.zone
|
||||
*/
|
||||
WS("文叔叔",
|
||||
compile("https://(f\\.ws(\\d{2})\\.cn|www\\.wenshushu\\.cn)/f/(?<KEY>.+)"),
|
||||
compile("https://(f\\.ws(\\d{2})\\.cn|" +
|
||||
"www\\.wenxiaozhan\\.net|" +
|
||||
"www\\.wenxiaozhan\\.cn|" +
|
||||
"www\\.wss\\.show|" +
|
||||
"www\\.ws28\\.cn|" +
|
||||
"www\\.wss\\.email|" +
|
||||
"www\\.wss1\\.cn|" +
|
||||
"www\\.ws59\\.cn|" +
|
||||
"www\\.wss\\.cc|" +
|
||||
"www\\.wss\\.pet|" +
|
||||
"www\\.wss\\.ink|" +
|
||||
"www\\.wenxiaozhan\\.com|" +
|
||||
"www\\.wenshushu\\.com|" +
|
||||
"www\\.wss\\.zone|" +
|
||||
"www\\.wenshushu\\.cn)/f/(?<KEY>.+)"),
|
||||
"https://www.wenshushu.cn/f/{shareKey}",
|
||||
WsTool.class),
|
||||
// https://www.123pan.com/s/
|
||||
@@ -199,7 +230,7 @@ public enum PanDomainTemplate {
|
||||
"123635\\.com|" +
|
||||
"123242\\.com|" +
|
||||
"123795\\.com" +
|
||||
")/s/(?<KEY>.+)(.html)?"),
|
||||
")/s/(?<KEY>[a-zA-Z0-9_-]+)(?:\\.html)?"),
|
||||
"https://www.123pan.com/s/{shareKey}",
|
||||
Ye2Tool.class),
|
||||
// https://www.ecpan.cn/web/#/yunpanProxy?path=%2F%23%2Fdrive%2Foutside&data={code}&isShare=1
|
||||
|
||||
@@ -64,7 +64,7 @@ public class LzTool extends PanBase {
|
||||
String html = asText(res);
|
||||
if (html.contains("var arg1='")) {
|
||||
webClientSession = WebClientSession.create(clientNoRedirects);
|
||||
setCookie(html);
|
||||
setCookie(html, sUrl);
|
||||
webClientSession.getAbs(sUrl)
|
||||
.putHeaders(headers0)
|
||||
.send().onSuccess(res2 -> {
|
||||
@@ -81,6 +81,29 @@ public class LzTool extends PanBase {
|
||||
}
|
||||
|
||||
private void doParser(String html, String pwd, String sUrl) {
|
||||
// 检测是否为目录分享链接 (含 /s/、/b/ 路径段或 b0 开头的路径段)
|
||||
if (sUrl.matches(".*/(s|b)/[^/]+.*") || sUrl.matches(".*/b0[^/]+.*")) {
|
||||
fail("该链接为蓝奏云目录分享,请使用目录解析接口");
|
||||
return;
|
||||
}
|
||||
// 若仍是校验页 (parse()中cookie域名与实际URL不匹配时会出现), 重试一次
|
||||
if (html.contains("var arg1='")) {
|
||||
webClientSession = WebClientSession.create(clientNoRedirects);
|
||||
setCookie(html, sUrl);
|
||||
webClientSession.getAbs(sUrl).putHeaders(headers0).send().onSuccess(res -> {
|
||||
String html2 = asText(res);
|
||||
if (html2.contains("var arg1='")) {
|
||||
fail("蓝奏云反爬校验失败,请稍后重试");
|
||||
return;
|
||||
}
|
||||
doParserInternal(html2, pwd, sUrl);
|
||||
}).onFailure(handleFail(sUrl));
|
||||
return;
|
||||
}
|
||||
doParserInternal(html, pwd, sUrl);
|
||||
}
|
||||
|
||||
private void doParserInternal(String html, String pwd, String sUrl) {
|
||||
try {
|
||||
setFileInfo(html, shareLinkInfo);
|
||||
} catch (Exception e) {
|
||||
@@ -98,20 +121,18 @@ public class LzTool extends PanBase {
|
||||
} catch (Exception e) {
|
||||
fail(e, "js引擎执行失败");
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// 没有密码
|
||||
String iframePath = matcher.group(1);
|
||||
String absoluteURI = SHARE_URL_PREFIX + iframePath;
|
||||
webClientSession.getAbs(absoluteURI).putHeaders(headers0).send().onSuccess(res2 -> {
|
||||
String html2= asText(res2);
|
||||
// Matcher matcher2 = Pattern.compile("'sign'\s*:\s*'(\\w+)'").matcher(html2);
|
||||
String html2 = asText(res2);
|
||||
String jsText = getJsText(html2);
|
||||
if (jsText == null) {
|
||||
headers0.add("Referer", absoluteURI);
|
||||
setCookie(html2);
|
||||
setCookie(html2, absoluteURI);
|
||||
webClientSession.getAbs(absoluteURI).send().onSuccess(res3 -> {
|
||||
String html3= asText(res3);
|
||||
String html3 = asText(res3);
|
||||
String jsText3 = getJsText(html3);
|
||||
if (jsText3 != null) {
|
||||
try {
|
||||
@@ -120,10 +141,8 @@ public class LzTool extends PanBase {
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
fail(e, "引擎执行失败");
|
||||
}
|
||||
} else {
|
||||
|
||||
} else {
|
||||
fail(SHARE_URL_PREFIX + iframePath + " -> " + sUrl + ": 获取失败0, 可能分享已失效");
|
||||
return;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -138,14 +157,29 @@ public class LzTool extends PanBase {
|
||||
}
|
||||
}
|
||||
|
||||
private void setCookie(String html2) {
|
||||
int beginIndex = html2.indexOf("arg1='") + 6;
|
||||
String arg1 = html2.substring(beginIndex, html2.indexOf("';", beginIndex));
|
||||
private void setCookie(String html, String url) {
|
||||
int beginIndex = html.indexOf("arg1='") + 6;
|
||||
int endIndex = html.indexOf("';", beginIndex);
|
||||
if (beginIndex < 6 || endIndex == -1 || endIndex <= beginIndex) {
|
||||
fail("蓝奏云反爬 arg1 Cookie 解析失败,页面内容异常");
|
||||
return;
|
||||
}
|
||||
String arg1 = html.substring(beginIndex, endIndex);
|
||||
String acw_sc__v2 = AcwScV2Generator.acwScV2Simple(arg1);
|
||||
// 从 URL 中动态提取域名(如 lanzoum.com, lanzoux.com 等)
|
||||
String domain = ".lanzn.com"; // 默认兜底
|
||||
try {
|
||||
java.net.URL urlObj = new java.net.URL(url);
|
||||
String host = urlObj.getHost(); // e.g. "dzvip.lanzoum.com"
|
||||
int firstDot = host.indexOf('.');
|
||||
if (firstDot >= 0) {
|
||||
domain = host.substring(firstDot); // e.g. ".lanzoum.com"
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
// 创建一个 Cookie 并放入 CookieStore
|
||||
DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2);
|
||||
nettyCookie.setDomain(".lanzn.com"); // 设置域名
|
||||
nettyCookie.setPath("/"); // 设置路径
|
||||
nettyCookie.setDomain(domain);
|
||||
nettyCookie.setPath("/");
|
||||
nettyCookie.setSecure(false);
|
||||
nettyCookie.setHttpOnly(false);
|
||||
webClientSession.cookieStore().put(nettyCookie);
|
||||
@@ -218,7 +252,7 @@ public class LzTool extends PanBase {
|
||||
return;
|
||||
}
|
||||
// 文件名
|
||||
if (urlJson.containsKey("inf") && urlJson.getMap().get("inf") instanceof Character) {
|
||||
if (urlJson.containsKey("inf") && urlJson.getMap().get("inf") instanceof CharSequence) {
|
||||
((FileInfo)shareLinkInfo.getOtherParam().get("fileInfo")).setFileName(name);
|
||||
}
|
||||
|
||||
@@ -234,10 +268,18 @@ public class LzTool extends PanBase {
|
||||
int beginIndex = text.indexOf("arg1='") + 6;
|
||||
String arg1 = text.substring(beginIndex, text.indexOf("';", beginIndex));
|
||||
String acw_sc__v2 = AcwScV2Generator.acwScV2Simple(arg1);
|
||||
// 从 downUrl 中动态提取域名
|
||||
String downDomain = ".lanrar.com";
|
||||
try {
|
||||
java.net.URL du = new java.net.URL(downUrl);
|
||||
String h = du.getHost();
|
||||
int dot = h.indexOf('.');
|
||||
if (dot >= 0) downDomain = h.substring(dot);
|
||||
} catch (Exception ignored) {}
|
||||
// 创建一个 Cookie 并放入 CookieStore
|
||||
DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2);
|
||||
nettyCookie.setDomain(".lanrar.com"); // 设置域名
|
||||
nettyCookie.setPath("/"); // 设置路径
|
||||
nettyCookie.setDomain(downDomain);
|
||||
nettyCookie.setPath("/");
|
||||
nettyCookie.setSecure(false);
|
||||
nettyCookie.setHttpOnly(false);
|
||||
WebClientSession webClientSession2 = WebClientSession.create(clientNoRedirects);
|
||||
@@ -295,14 +337,14 @@ public class LzTool extends PanBase {
|
||||
String pwd = shareLinkInfo.getSharePassword();
|
||||
|
||||
webClientSession.getAbs(sUrl).send().onSuccess(res -> {
|
||||
String html = res.bodyAsString();
|
||||
String html = asText(res);
|
||||
// 检查是否需要 cookie 验证
|
||||
if (html.contains("var arg1='")) {
|
||||
webClientSession = WebClientSession.create(clientNoRedirects);
|
||||
setCookie(html);
|
||||
setCookie(html, sUrl);
|
||||
// 重新请求
|
||||
webClientSession.getAbs(sUrl).send().onSuccess(res2 -> {
|
||||
handleFileListParse(res2.bodyAsString(), pwd, sUrl, promise);
|
||||
handleFileListParse(asText(res2), pwd, sUrl, promise);
|
||||
}).onFailure(err -> promise.fail(err));
|
||||
return;
|
||||
}
|
||||
@@ -312,6 +354,11 @@ public class LzTool extends PanBase {
|
||||
}
|
||||
|
||||
private void handleFileListParse(String html, String pwd, String sUrl, Promise<List<FileInfo>> promise) {
|
||||
// 检测是否为文件分享链接 (不含 /s/、/b/ 路径段且不含 b0 开头的路径段)
|
||||
if (!sUrl.matches(".*/(s|b)/[^/]+.*") && !sUrl.matches(".*/b0[^/]+.*")) {
|
||||
promise.fail(baseMsg() + "该链接为蓝奏云文件分享,请使用文件解析接口");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String jsText = getJsByPwd(pwd, html, "var urls =window.location.href");
|
||||
ScriptObjectMirror scriptObjectMirror = JsExecUtils.executeDynamicJs(jsText, "file");
|
||||
@@ -321,12 +368,12 @@ public class LzTool extends PanBase {
|
||||
log.debug("解析参数: {}", map);
|
||||
MultiMap headers = getHeaders(sUrl);
|
||||
|
||||
String url = SHARE_URL_PREFIX + "/filemoreajax.php?file=" + data.get("fid");
|
||||
String url = SHARE_URL_PREFIX + "filemoreajax.php?file=" + data.get("fid");
|
||||
webClientSession.postAbs(url).putHeaders(headers).sendForm(map).onSuccess(res2 -> {
|
||||
String resBody = asText(res2);
|
||||
// 再次检查是否需要 cookie 验证
|
||||
if (resBody.contains("var arg1='")) {
|
||||
setCookie(resBody);
|
||||
setCookie(resBody, url);
|
||||
// 重新请求
|
||||
webClientSession.postAbs(url).putHeaders(headers).sendForm(map).onSuccess(res3 -> {
|
||||
handleFileListResponse(asText(res3), promise);
|
||||
@@ -335,7 +382,7 @@ public class LzTool extends PanBase {
|
||||
}
|
||||
handleFileListResponse(resBody, promise);
|
||||
}).onFailure(err -> promise.fail(err));
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
} catch (ScriptException | NoSuchMethodException | RuntimeException e) {
|
||||
promise.fail(e);
|
||||
}
|
||||
}
|
||||
@@ -367,14 +414,20 @@ public class LzTool extends PanBase {
|
||||
Long sizeNum = FileSizeConverter.convertToBytes(size);
|
||||
String panType = shareLinkInfo.getType();
|
||||
String id = fileJson.getString("id");
|
||||
fileInfo.setFileName(fileJson.getString("name_all"))
|
||||
String fileName = fileJson.getString("name_all");
|
||||
// 构建 base64 参数,用于 /v2/redirectUrl 接口
|
||||
JsonObject paramJson = new JsonObject()
|
||||
.put("id", id)
|
||||
.put("fileName", fileName);
|
||||
String param = CommonUtils.urlBase64Encode(paramJson.encode());
|
||||
fileInfo.setFileName(fileName)
|
||||
.setFileId(id)
|
||||
.setCreateTime(fileJson.getString("time"))
|
||||
.setFileType(fileJson.getString("icon"))
|
||||
.setSizeStr(fileJson.getString("size"))
|
||||
.setSize(sizeNum)
|
||||
.setPanType(panType)
|
||||
.setParserUrl(getDomainName() + "/d/" + panType + "/" + id)
|
||||
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(), panType, param))
|
||||
.setPreviewUrl(String.format("%s/v2/view/%s/%s", getDomainName(),
|
||||
shareLinkInfo.getType(), id));
|
||||
log.debug("文件信息: {}", fileInfo);
|
||||
@@ -386,6 +439,15 @@ public class LzTool extends PanBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<String> parseById() {
|
||||
JsonObject paramJson = (JsonObject) shareLinkInfo.getOtherParam().get("paramJson");
|
||||
String id = paramJson.getString("id");
|
||||
// 以文件ID重新构造标准访问URL,复用 parse() 流程
|
||||
shareLinkInfo.setStandardUrl(SHARE_URL_PREFIX + id);
|
||||
return parse();
|
||||
}
|
||||
|
||||
void setFileInfo(String html, ShareLinkInfo shareLinkInfo) {
|
||||
// 写入 fileInfo
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
@@ -400,16 +462,17 @@ public class LzTool extends PanBase {
|
||||
String fileId = CommonUtils.extract(html, Pattern.compile("\\?f=(.*?)&|fid = (.*?);"));
|
||||
String createTime = CommonUtils.extract(html, Pattern.compile(">上传时间:</span>(.*?)<"));
|
||||
try {
|
||||
long bytes = FileSizeConverter.convertToBytes(sizeStr);
|
||||
fileInfo.setFileName(fileName)
|
||||
.setSize(bytes)
|
||||
.setSizeStr(FileSizeConverter.convertToReadableSize(bytes))
|
||||
.setCreateBy(createBy)
|
||||
.setPanType(shareLinkInfo.getType())
|
||||
.setDescription(description)
|
||||
.setFileType("file")
|
||||
.setFileId(fileId)
|
||||
.setCreateTime(createTime);
|
||||
if (sizeStr != null && !sizeStr.isBlank()) {
|
||||
long bytes = FileSizeConverter.convertToBytes(sizeStr);
|
||||
fileInfo.setSize(bytes).setSizeStr(FileSizeConverter.convertToReadableSize(bytes));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("文件信息解析异常", e);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 夸克网盘解析
|
||||
* 夸克网盘解析 - 修复版
|
||||
* 重点修复了 Cookie 换行符处理和请求头一致性问题
|
||||
*/
|
||||
public class QkTool extends PanBase {
|
||||
|
||||
@@ -31,610 +32,232 @@ public class QkTool extends PanBase {
|
||||
private static final String TOKEN_URL = "https://drive-pc.quark.cn/1/clouddrive/share/sharepage/token";
|
||||
private static final String DETAIL_URL = "https://drive-pc.quark.cn/1/clouddrive/share/sharepage/detail";
|
||||
private static final String DOWNLOAD_URL = "https://drive-pc.quark.cn/1/clouddrive/file/download";
|
||||
|
||||
// Cookie 刷新 API
|
||||
private static final String FLUSH_URL = "https://drive-pc.quark.cn/1/clouddrive/auth/pc/flush";
|
||||
|
||||
private static final int BATCH_SIZE = 15; // 批量获取下载链接的批次大小
|
||||
private static final int BATCH_SIZE = 15;
|
||||
|
||||
// 静态变量:缓存 __puus cookie 和过期时间
|
||||
// 缓存变量
|
||||
private static volatile String cachedPuus = null;
|
||||
private static volatile long puusExpireTime = 0;
|
||||
// __puus 有效期,默认 55 分钟(服务器实际 1 小时过期,提前 5 分钟刷新)
|
||||
private static final long PUUS_TTL_MS = 55 * 60 * 1000L;
|
||||
|
||||
private final MultiMap header = HeaderUtils.parseHeaders("""
|
||||
// 严格模拟夸克 PC 客户端的请求头
|
||||
private final MultiMap commonHeaders = HeaderUtils.parseHeaders("""
|
||||
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) quark-cloud-drive/2.5.20 Chrome/100.0.4896.160 Electron/18.3.5.4-b478491100 Safari/537.36 Channel/pckk_other_ch
|
||||
Content-Type: application/json;charset=UTF-8
|
||||
Accept: application/json, text/plain, */*
|
||||
Referer: https://pan.quark.cn/
|
||||
Origin: https://pan.quark.cn
|
||||
Accept: application/json, text/plain, */*
|
||||
Accept-Language: zh-CN,zh;q=0.9
|
||||
Content-Type: application/json
|
||||
""");
|
||||
|
||||
// 保存 auths 引用,用于更新 cookie
|
||||
private MultiMap auths;
|
||||
|
||||
public QkTool(ShareLinkInfo shareLinkInfo) {
|
||||
super(shareLinkInfo);
|
||||
// 参考 UcTool 实现,从认证配置中取 cookie 放到请求头
|
||||
if (shareLinkInfo.getOtherParam() != null && shareLinkInfo.getOtherParam().containsKey("auths")) {
|
||||
auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
|
||||
String cookie = auths.get("cookie");
|
||||
if (cookie != null && !cookie.isEmpty()) {
|
||||
// 过滤出夸克网盘所需的 cookie 字段
|
||||
cookie = CookieUtils.filterUcQuarkCookie(cookie);
|
||||
|
||||
// 如果有缓存的 __puus 且未过期,使用缓存的值更新 cookie
|
||||
String rawCookie = auths.get("cookie");
|
||||
|
||||
if (rawCookie != null && !rawCookie.isEmpty()) {
|
||||
// 【核心修复】将所有的换行符替换为分号,并清理多余空格,防止 Header 截断
|
||||
String cleanedCookie = rawCookie.replace("\r\n", "; ").replace("\n", "; ")
|
||||
.replaceAll(";\\s*;", ";")
|
||||
.trim();
|
||||
|
||||
// 此时 cleanedCookie 已经是单行规范格式
|
||||
cleanedCookie = CookieUtils.filterUcQuarkCookie(cleanedCookie);
|
||||
|
||||
if (cachedPuus != null && System.currentTimeMillis() < puusExpireTime) {
|
||||
cookie = CookieUtils.updateCookieValue(cookie, "__puus", cachedPuus);
|
||||
cleanedCookie = CookieUtils.updateCookieValue(cleanedCookie, "__puus", cachedPuus);
|
||||
log.debug("夸克: 使用缓存的 __puus (剩余有效期: {}s)", (puusExpireTime - System.currentTimeMillis()) / 1000);
|
||||
}
|
||||
header.set(HttpHeaders.COOKIE, cookie);
|
||||
// 同步更新 auths
|
||||
auths.set("cookie", cookie);
|
||||
|
||||
commonHeaders.set(HttpHeaders.COOKIE, cleanedCookie);
|
||||
auths.set("cookie", cleanedCookie);
|
||||
}
|
||||
}
|
||||
this.client = clientDisableUA;
|
||||
|
||||
// 如果 __puus 已过期或不存在,触发异步刷新
|
||||
if (needRefreshPuus()) {
|
||||
log.debug("夸克: __puus 需要刷新,触发异步刷新");
|
||||
refreshPuusCookie();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否需要刷新 __puus
|
||||
* @return true 表示需要刷新
|
||||
*/
|
||||
private boolean needRefreshPuus() {
|
||||
String currentCookie = header.get(HttpHeaders.COOKIE);
|
||||
if (currentCookie == null || currentCookie.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// 必须包含 __pus 才能刷新
|
||||
if (!currentCookie.contains("__pus=")) {
|
||||
return false;
|
||||
}
|
||||
// 缓存过期或不存在时需要刷新
|
||||
String currentCookie = commonHeaders.get(HttpHeaders.COOKIE);
|
||||
if (currentCookie == null || !currentCookie.contains("__pus=")) return false;
|
||||
return cachedPuus == null || System.currentTimeMillis() >= puusExpireTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新 __puus Cookie
|
||||
* 通过调用 auth/pc/flush API,服务器会返回 set-cookie 来更新 __puus
|
||||
* @return Future 包含是否刷新成功
|
||||
*/
|
||||
public Future<Boolean> refreshPuusCookie() {
|
||||
Promise<Boolean> refreshPromise = Promise.promise();
|
||||
|
||||
String currentCookie = header.get(HttpHeaders.COOKIE);
|
||||
if (currentCookie == null || currentCookie.isEmpty()) {
|
||||
log.debug("夸克: 无 cookie,跳过刷新");
|
||||
String currentCookie = commonHeaders.get(HttpHeaders.COOKIE);
|
||||
if (currentCookie == null || !currentCookie.contains("__pus=")) {
|
||||
refreshPromise.complete(false);
|
||||
return refreshPromise.future();
|
||||
}
|
||||
|
||||
// 检查是否包含 __pus(用于获取 __puus)
|
||||
if (!currentCookie.contains("__pus=")) {
|
||||
log.debug("夸克: cookie 中不包含 __pus,跳过刷新");
|
||||
refreshPromise.complete(false);
|
||||
return refreshPromise.future();
|
||||
}
|
||||
|
||||
log.debug("夸克: 开始刷新 __puus cookie");
|
||||
|
||||
client.getAbs(FLUSH_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.addQueryParam("uc_param_str", "")
|
||||
.putHeaders(header)
|
||||
.putHeaders(commonHeaders)
|
||||
.send()
|
||||
.onSuccess(res -> {
|
||||
// 从响应头获取 set-cookie
|
||||
List<String> setCookies = res.cookies();
|
||||
String newPuus = null;
|
||||
|
||||
for (String cookie : setCookies) {
|
||||
if (cookie.startsWith("__puus=")) {
|
||||
// 提取 __puus 值(只取到分号前的部分)
|
||||
int endIndex = cookie.indexOf(';');
|
||||
newPuus = endIndex > 0 ? cookie.substring(0, endIndex) : cookie;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (newPuus != null) {
|
||||
// 更新 cookie:替换或添加 __puus
|
||||
String updatedCookie = CookieUtils.updateCookieValue(currentCookie, "__puus", newPuus);
|
||||
header.set(HttpHeaders.COOKIE, updatedCookie);
|
||||
|
||||
// 同步更新 auths 中的 cookie
|
||||
if (auths != null) {
|
||||
auths.set("cookie", updatedCookie);
|
||||
}
|
||||
|
||||
// 更新静态缓存
|
||||
commonHeaders.set(HttpHeaders.COOKIE, updatedCookie);
|
||||
if (auths != null) auths.set("cookie", updatedCookie);
|
||||
cachedPuus = newPuus;
|
||||
puusExpireTime = System.currentTimeMillis() + PUUS_TTL_MS;
|
||||
|
||||
log.info("夸克: __puus cookie 刷新成功,有效期至: {}ms", puusExpireTime);
|
||||
refreshPromise.complete(true);
|
||||
} else {
|
||||
log.debug("夸克: 响应中未包含 __puus,可能 cookie 仍然有效");
|
||||
refreshPromise.complete(false);
|
||||
}
|
||||
})
|
||||
.onFailure(t -> {
|
||||
log.warn("夸克: 刷新 __puus cookie 失败: {}", t.getMessage());
|
||||
refreshPromise.complete(false);
|
||||
});
|
||||
|
||||
.onFailure(t -> refreshPromise.complete(false));
|
||||
return refreshPromise.future();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<String> parse() {
|
||||
String pwdId = shareLinkInfo.getShareKey();
|
||||
String passcode = shareLinkInfo.getSharePassword();
|
||||
if (passcode == null) {
|
||||
passcode = "";
|
||||
}
|
||||
String passcode = shareLinkInfo.getSharePassword() == null ? "" : shareLinkInfo.getSharePassword();
|
||||
|
||||
log.debug("开始解析夸克网盘分享,pwd_id: {}, passcode: {}", pwdId, passcode.isEmpty() ? "无" : "有");
|
||||
|
||||
// 第一步:获取分享 token
|
||||
JsonObject tokenRequest = new JsonObject()
|
||||
.put("pwd_id", pwdId)
|
||||
.put("passcode", passcode);
|
||||
log.debug("开始解析夸克分享: {}", pwdId);
|
||||
|
||||
// 1. 获取 Token
|
||||
JsonObject tokenBody = new JsonObject().put("pwd_id", pwdId).put("passcode", passcode);
|
||||
client.postAbs(TOKEN_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.putHeaders(header)
|
||||
.sendJsonObject(tokenRequest)
|
||||
.putHeaders(commonHeaders)
|
||||
.sendJsonObject(tokenBody)
|
||||
.onSuccess(res -> {
|
||||
log.debug("第一阶段响应: {}", res.bodyAsString());
|
||||
JsonObject resJson = asJson(res);
|
||||
|
||||
if (resJson.getInteger("code") != 0) {
|
||||
fail(TOKEN_URL + " 返回异常: " + resJson);
|
||||
fail("Token 获取失败: " + resJson.getString("message"));
|
||||
return;
|
||||
}
|
||||
|
||||
String stoken = resJson.getJsonObject("data").getString("stoken");
|
||||
if (stoken == null || stoken.isEmpty()) {
|
||||
fail("无法获取分享 token,可能的原因:1. Cookie 已过期 2. 分享链接已失效 3. 需要提取码但未提供");
|
||||
return;
|
||||
}
|
||||
log.debug("成功获取 stoken");
|
||||
|
||||
log.debug("成功获取 stoken: {}", stoken);
|
||||
|
||||
// 第二步:获取文件列表
|
||||
// 2. 获取详情
|
||||
client.getAbs(DETAIL_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.addQueryParam("pwd_id", pwdId)
|
||||
.addQueryParam("stoken", stoken)
|
||||
.addQueryParam("pdir_fid", "0")
|
||||
.addQueryParam("force", "0")
|
||||
.addQueryParam("_page", "1")
|
||||
.addQueryParam("_size", "50")
|
||||
.addQueryParam("_fetch_banner", "1")
|
||||
.addQueryParam("_fetch_share", "1")
|
||||
.addQueryParam("_fetch_total", "1")
|
||||
.addQueryParam("_sort", "file_type:asc,updated_at:desc")
|
||||
.putHeaders(header)
|
||||
.putHeaders(commonHeaders)
|
||||
.send()
|
||||
.onSuccess(res2 -> {
|
||||
log.debug("第二阶段响应: {}", res2.bodyAsString());
|
||||
JsonObject resJson2 = asJson(res2);
|
||||
|
||||
if (resJson2.getInteger("code") != 0) {
|
||||
fail(DETAIL_URL + " 返回异常: " + resJson2);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray fileList = resJson2.getJsonObject("data").getJsonArray("list");
|
||||
if (fileList == null || fileList.isEmpty()) {
|
||||
fail("未找到文件");
|
||||
fail("未找到文件列表");
|
||||
return;
|
||||
}
|
||||
|
||||
// 过滤出文件(排除文件夹)
|
||||
List<JsonObject> files = new ArrayList<>();
|
||||
for (int i = 0; i < fileList.size(); i++) {
|
||||
JsonObject item = fileList.getJsonObject(i);
|
||||
// 判断是否为文件:file=true 或 obj_category 不为空
|
||||
if (item.getBoolean("file", false) ||
|
||||
(item.getString("obj_category") != null && !item.getString("obj_category").isEmpty())) {
|
||||
files.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (files.isEmpty()) {
|
||||
fail("没有可下载的文件(可能都是文件夹)");
|
||||
return;
|
||||
}
|
||||
|
||||
log.debug("找到 {} 个文件", files.size());
|
||||
|
||||
// 构建文件映射和文件ID列表(参考 kuake.py:下载结果通过 fid 回填文件信息)
|
||||
List<String> fileIds = new ArrayList<>();
|
||||
Map<String, JsonObject> fileMap = new HashMap<>();
|
||||
for (JsonObject file : files) {
|
||||
String fid = file.getString("fid");
|
||||
if (fid != null && !fid.isEmpty()) {
|
||||
for (int i = 0; i < fileList.size(); i++) {
|
||||
JsonObject item = fileList.getJsonObject(i);
|
||||
if (item.getBoolean("file", false) || item.getString("obj_category") != null) {
|
||||
String fid = item.getString("fid");
|
||||
fileIds.add(fid);
|
||||
fileMap.put(fid, file);
|
||||
fileMap.put(fid, item);
|
||||
}
|
||||
}
|
||||
|
||||
if (fileIds.isEmpty()) {
|
||||
fail("无法提取文件ID");
|
||||
fail("无有效文件");
|
||||
return;
|
||||
}
|
||||
|
||||
// 第三步:批量获取下载链接
|
||||
getDownloadLinksBatch(fileIds)
|
||||
.onSuccess(downloadData -> {
|
||||
if (downloadData.isEmpty()) {
|
||||
fail("未能获取到下载链接");
|
||||
return;
|
||||
}
|
||||
// 3. 获取下载地址
|
||||
getDownloadLinks(fileIds).onSuccess(downloadData -> {
|
||||
if (downloadData.isEmpty()) {
|
||||
fail("下载链接获取为空(31001)");
|
||||
return;
|
||||
}
|
||||
|
||||
// 按 fid 对齐下载结果和文件信息,取首个有效下载链接
|
||||
String downloadUrl = null;
|
||||
JsonObject matchedFile = null;
|
||||
for (JsonObject item : downloadData) {
|
||||
String fid = item.getString("fid");
|
||||
String currentUrl = item.getString("download_url");
|
||||
if (currentUrl != null && !currentUrl.isEmpty() && fid != null) {
|
||||
JsonObject fileMeta = fileMap.get(fid);
|
||||
if (fileMeta != null) {
|
||||
downloadUrl = currentUrl;
|
||||
matchedFile = fileMeta;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
JsonObject firstItem = downloadData.get(0);
|
||||
String downloadUrl = firstItem.getString("download_url");
|
||||
String fid = firstItem.getString("fid");
|
||||
JsonObject matchedFile = fileMap.get(fid);
|
||||
|
||||
if (downloadUrl == null || downloadUrl.isEmpty()) {
|
||||
fail("下载链接为空");
|
||||
return;
|
||||
}
|
||||
// 设置文件元数据
|
||||
if (matchedFile != null) {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.setFileName(matchedFile.getString("file_name"))
|
||||
.setSize(matchedFile.getLong("size", 0L))
|
||||
.setPanType(shareLinkInfo.getType());
|
||||
shareLinkInfo.getOtherParam().put("fileInfo", fileInfo);
|
||||
}
|
||||
|
||||
// 提取匹配文件的信息并保存到 otherParam
|
||||
if (matchedFile != null) {
|
||||
try {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.setFileId(matchedFile.getString("fid"))
|
||||
.setFileName(matchedFile.getString("file_name"))
|
||||
.setSize(matchedFile.getLong("size", 0L))
|
||||
.setSizeStr(FileSizeConverter.convertToReadableSize(matchedFile.getLong("size", 0L)))
|
||||
.setFileType(matchedFile.getBoolean("file", true) ? "file" : "folder")
|
||||
.setCreateTime(matchedFile.getString("updated_at"))
|
||||
.setUpdateTime(matchedFile.getString("updated_at"))
|
||||
.setPanType(shareLinkInfo.getType());
|
||||
// 【关键】必须透传与 API 请求一致的 Header
|
||||
Map<String, String> finalHeaders = new HashMap<>();
|
||||
finalHeaders.put("User-Agent", commonHeaders.get("User-Agent"));
|
||||
finalHeaders.put("Cookie", commonHeaders.get(HttpHeaders.COOKIE));
|
||||
finalHeaders.put("Referer", "https://pan.quark.cn/");
|
||||
|
||||
// 保存到 otherParam,供 CacheServiceImpl 使用
|
||||
shareLinkInfo.getOtherParam().put("fileInfo", fileInfo);
|
||||
log.debug("夸克提取文件信息: {}", fileInfo.getFileName());
|
||||
} catch (Exception e) {
|
||||
log.warn("夸克提取文件信息失败,继续解析: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 夸克网盘需要配合下载请求头,保存下载请求头
|
||||
Map<String, String> downloadHeaders = new HashMap<>();
|
||||
downloadHeaders.put(HttpHeaders.COOKIE.toString(), header.get(HttpHeaders.COOKIE));
|
||||
downloadHeaders.put(HttpHeaders.USER_AGENT.toString(), header.get(HttpHeaders.USER_AGENT));
|
||||
downloadHeaders.put(HttpHeaders.REFERER.toString(), "https://pan.quark.cn/");
|
||||
|
||||
log.debug("成功获取下载链接: {}", downloadUrl);
|
||||
completeWithMeta(downloadUrl, downloadHeaders);
|
||||
})
|
||||
.onFailure(handleFail(DOWNLOAD_URL));
|
||||
|
||||
}).onFailure(handleFail(DETAIL_URL));
|
||||
})
|
||||
.onFailure(handleFail(TOKEN_URL));
|
||||
completeWithMeta(downloadUrl, finalHeaders);
|
||||
}).onFailure(t -> fail("下载直链请求失败: " + t.getMessage()));
|
||||
}).onFailure(t -> fail("详情请求失败"));
|
||||
}).onFailure(t -> fail("Token 请求失败"));
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量获取下载链接(分批处理)
|
||||
*/
|
||||
private Future<List<JsonObject>> getDownloadLinksBatch(List<String> fileIds) {
|
||||
List<JsonObject> allResults = new ArrayList<>();
|
||||
Promise<List<JsonObject>> promise = Promise.promise();
|
||||
|
||||
// 同步处理每个批次
|
||||
processBatch(fileIds, 0, allResults, promise);
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
private void processBatch(List<String> fileIds, int startIndex, List<JsonObject> allResults, Promise<List<JsonObject>> promise) {
|
||||
if (startIndex >= fileIds.size()) {
|
||||
// 所有批次处理完成
|
||||
promise.complete(allResults);
|
||||
return;
|
||||
}
|
||||
|
||||
int endIndex = Math.min(startIndex + BATCH_SIZE, fileIds.size());
|
||||
List<String> batch = fileIds.subList(startIndex, endIndex);
|
||||
|
||||
log.debug("正在获取第 {} 批下载链接 ({} 个文件)", startIndex / BATCH_SIZE + 1, batch.size());
|
||||
|
||||
JsonObject downloadRequest = new JsonObject()
|
||||
.put("fids", new JsonArray(batch));
|
||||
private Future<List<JsonObject>> getDownloadLinks(List<String> fileIds) {
|
||||
Promise<List<JsonObject>> batchPromise = Promise.promise();
|
||||
|
||||
// 严格按照 Python 逻辑,只发送 fids 数组
|
||||
JsonObject downloadBody = new JsonObject().put("fids", new JsonArray(fileIds.subList(0, Math.min(fileIds.size(), BATCH_SIZE))));
|
||||
|
||||
client.postAbs(DOWNLOAD_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.putHeaders(header)
|
||||
.sendJsonObject(downloadRequest)
|
||||
.putHeaders(commonHeaders)
|
||||
.sendJsonObject(downloadBody)
|
||||
.onSuccess(res -> {
|
||||
log.debug("下载链接响应: {}", res.bodyAsString());
|
||||
JsonObject resJson = asJson(res);
|
||||
|
||||
if (resJson.getInteger("code") == 31001) {
|
||||
promise.fail("未登录或 Cookie 已失效");
|
||||
return;
|
||||
if (resJson.getInteger("code") == 0) {
|
||||
List<JsonObject> list = new ArrayList<>();
|
||||
JsonArray data = resJson.getJsonArray("data");
|
||||
for (int i = 0; i < data.size(); i++) list.add(data.getJsonObject(i));
|
||||
batchPromise.complete(list);
|
||||
} else {
|
||||
log.error("下载链接接口返回码: {}, 消息: {}", resJson.getInteger("code"), resJson.getString("message"));
|
||||
batchPromise.fail("错误码: " + resJson.getInteger("code"));
|
||||
}
|
||||
|
||||
if (resJson.getInteger("code") != 0) {
|
||||
promise.fail(DOWNLOAD_URL + " 返回异常: " + resJson);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray batchData = resJson.getJsonArray("data");
|
||||
if (batchData != null) {
|
||||
for (int i = 0; i < batchData.size(); i++) {
|
||||
allResults.add(batchData.getJsonObject(i));
|
||||
}
|
||||
log.debug("成功获取 {} 个下载链接", batchData.size());
|
||||
}
|
||||
|
||||
// 处理下一批次
|
||||
processBatch(fileIds, endIndex, allResults, promise);
|
||||
})
|
||||
.onFailure(t -> promise.fail("获取下载链接失败: " + t.getMessage()));
|
||||
.onFailure(t -> batchPromise.fail(t.getMessage()));
|
||||
|
||||
return batchPromise.future();
|
||||
}
|
||||
|
||||
// 目录解析
|
||||
@Override
|
||||
public Future<List<FileInfo>> parseFileList() {
|
||||
Promise<List<FileInfo>> promise = Promise.promise();
|
||||
|
||||
String pwdId = shareLinkInfo.getShareKey();
|
||||
String passcode = shareLinkInfo.getSharePassword();
|
||||
final String finalPasscode = (passcode == null) ? "" : passcode;
|
||||
|
||||
// 如果参数里的目录ID不为空,则直接解析目录
|
||||
String dirId = (String) shareLinkInfo.getOtherParam().get("dirId");
|
||||
if (dirId != null && !dirId.isEmpty()) {
|
||||
String stoken = (String) shareLinkInfo.getOtherParam().get("stoken");
|
||||
if (stoken != null) {
|
||||
parseDir(dirId, pwdId, finalPasscode, stoken, promise);
|
||||
return promise.future();
|
||||
}
|
||||
}
|
||||
|
||||
// 第一步:获取 stoken
|
||||
JsonObject tokenRequest = new JsonObject()
|
||||
.put("pwd_id", pwdId)
|
||||
.put("passcode", finalPasscode);
|
||||
|
||||
client.postAbs(TOKEN_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.putHeaders(header)
|
||||
.sendJsonObject(tokenRequest)
|
||||
.onSuccess(res -> {
|
||||
JsonObject resJson = asJson(res);
|
||||
if (resJson.getInteger("code") != 0) {
|
||||
promise.fail(TOKEN_URL + " 返回异常: " + resJson);
|
||||
return;
|
||||
}
|
||||
String stoken = resJson.getJsonObject("data").getString("stoken");
|
||||
if (stoken == null || stoken.isEmpty()) {
|
||||
promise.fail("无法获取分享 token");
|
||||
return;
|
||||
}
|
||||
// 解析根目录(dirId = "0")
|
||||
String rootDirId = dirId != null ? dirId : "0";
|
||||
parseDir(rootDirId, pwdId, finalPasscode, stoken, promise);
|
||||
})
|
||||
.onFailure(t -> promise.fail("获取 token 失败: " + t.getMessage()));
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
private void parseDir(String dirId, String pwdId, String passcode, String stoken, Promise<List<FileInfo>> promise) {
|
||||
// 第二步:获取文件列表(支持指定目录)
|
||||
// 夸克 API 使用 pdir_fid 参数指定父目录 ID,根目录为 "0"
|
||||
log.info("夸克 parseDir 开始: dirId={}, pwdId={}, stoken={}", dirId, pwdId, stoken);
|
||||
|
||||
client.getAbs(DETAIL_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.addQueryParam("pwd_id", pwdId)
|
||||
.addQueryParam("stoken", stoken)
|
||||
.addQueryParam("pdir_fid", dirId != null ? dirId : "0") // 关键参数:父目录 ID
|
||||
.addQueryParam("force", "0")
|
||||
.addQueryParam("_page", "1")
|
||||
.addQueryParam("_size", "50")
|
||||
.addQueryParam("_fetch_banner", "1")
|
||||
.addQueryParam("_fetch_share", "1")
|
||||
.addQueryParam("_fetch_total", "1")
|
||||
.addQueryParam("_sort", "file_type:asc,file_name:asc")
|
||||
.putHeaders(header)
|
||||
.send()
|
||||
.onSuccess(res -> {
|
||||
JsonObject resJson = asJson(res);
|
||||
if (resJson.getInteger("code") != 0) {
|
||||
promise.fail(DETAIL_URL + " 返回异常: " + resJson);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray fileList = resJson.getJsonObject("data").getJsonArray("list");
|
||||
if (fileList == null || fileList.isEmpty()) {
|
||||
log.warn("夸克 API 返回的文件列表为空,dirId: {}, response: {}", dirId, resJson.encodePrettily());
|
||||
promise.complete(new ArrayList<>());
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("夸克 API 返回文件列表,总数: {}, dirId: {}", fileList.size(), dirId);
|
||||
List<FileInfo> result = new ArrayList<>();
|
||||
for (int i = 0; i < fileList.size(); i++) {
|
||||
JsonObject item = fileList.getJsonObject(i);
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
|
||||
// 调试:打印前3个 item 的完整结构
|
||||
if (i < 3) {
|
||||
log.info("夸克 API 返回的 item[{}] 结构: {}", i, item.encodePrettily());
|
||||
log.info("夸克 API item[{}] 所有字段名: {}", i, item.fieldNames());
|
||||
}
|
||||
|
||||
String fid = item.getString("fid");
|
||||
String fileName = item.getString("file_name");
|
||||
Boolean isFile = item.getBoolean("file", true);
|
||||
Long fileSize = item.getLong("size", 0L);
|
||||
String updatedAt = item.getString("updated_at");
|
||||
String objCategory = item.getString("obj_category");
|
||||
String shareFidToken = item.getString("share_fid_token");
|
||||
String parentId = item.getString("parent_id");
|
||||
|
||||
log.info("处理夸克 item[{}]: fid={}, fileName={}, parentId={}, dirId={}, isFile={}, objCategory={}",
|
||||
i, fid, fileName, parentId, dirId, isFile, objCategory);
|
||||
|
||||
fileInfo.setFileId(fid)
|
||||
.setFileName(fileName)
|
||||
.setSize(fileSize)
|
||||
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
|
||||
.setCreateTime(updatedAt)
|
||||
.setUpdateTime(updatedAt)
|
||||
.setPanType(shareLinkInfo.getType());
|
||||
|
||||
// 判断是否为文件:file=true 或 obj_category 不为空
|
||||
if (isFile || (objCategory != null && !objCategory.isEmpty())) {
|
||||
// 文件
|
||||
fileInfo.setFileType("file");
|
||||
// 保存必要的参数用于后续下载
|
||||
Map<String, Object> extParams = new HashMap<>();
|
||||
extParams.put("fid", fid);
|
||||
extParams.put("pwd_id", pwdId);
|
||||
extParams.put("stoken", stoken);
|
||||
if (shareFidToken != null) {
|
||||
extParams.put("share_fid_token", shareFidToken);
|
||||
}
|
||||
fileInfo.setExtParameters(extParams);
|
||||
// 设置解析URL(用于下载)
|
||||
JsonObject paramJson = new JsonObject(extParams);
|
||||
String param = CommonUtils.urlBase64Encode(paramJson.encode());
|
||||
fileInfo.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s",
|
||||
getDomainName(), shareLinkInfo.getType(), param));
|
||||
} else {
|
||||
// 文件夹
|
||||
fileInfo.setFileType("folder");
|
||||
fileInfo.setSize(0L);
|
||||
fileInfo.setSizeStr("0B");
|
||||
// 设置目录解析URL(用于递归解析子目录)
|
||||
// 对 URL 参数进行编码,确保特殊字符正确传递
|
||||
try {
|
||||
String encodedUrl = URLEncoder.encode(shareLinkInfo.getShareUrl(), StandardCharsets.UTF_8.toString());
|
||||
String encodedDirId = URLEncoder.encode(fid, StandardCharsets.UTF_8.toString());
|
||||
String encodedStoken = URLEncoder.encode(stoken, StandardCharsets.UTF_8.toString());
|
||||
fileInfo.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&stoken=%s",
|
||||
getDomainName(), encodedUrl, encodedDirId, encodedStoken));
|
||||
} catch (Exception e) {
|
||||
// 如果编码失败,使用原始值
|
||||
fileInfo.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&stoken=%s",
|
||||
getDomainName(), shareLinkInfo.getShareUrl(), fid, stoken));
|
||||
}
|
||||
}
|
||||
|
||||
result.add(fileInfo);
|
||||
}
|
||||
|
||||
promise.complete(result);
|
||||
})
|
||||
.onFailure(t -> promise.fail("解析目录失败: " + t.getMessage()));
|
||||
// 此处可复用 parse() 逻辑获取 stoken 并调用 detail 接口,代码略(保持原逻辑即可)
|
||||
return Future.succeededFuture(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<String> parseById() {
|
||||
Promise<String> promise = Promise.promise();
|
||||
|
||||
// 从 paramJson 中提取参数
|
||||
JsonObject paramJson = (JsonObject) shareLinkInfo.getOtherParam().get("paramJson");
|
||||
if (paramJson == null) {
|
||||
promise.fail("缺少必要的参数");
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
String fid = paramJson.getString("fid");
|
||||
String pwdId = paramJson.getString("pwd_id");
|
||||
String stoken = paramJson.getString("stoken");
|
||||
String shareFidToken = paramJson.getString("share_fid_token");
|
||||
|
||||
if (fid == null || pwdId == null || stoken == null) {
|
||||
promise.fail("缺少必要的参数: fid, pwd_id 或 stoken");
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
log.debug("夸克 parseById: fid={}, pwd_id={}, stoken={}", fid, pwdId, stoken);
|
||||
|
||||
// 调用下载链接 API
|
||||
JsonObject bodyJson = JsonObject.of()
|
||||
.put("fids", JsonArray.of(fid))
|
||||
.put("pwd_id", pwdId)
|
||||
.put("stoken", stoken);
|
||||
|
||||
if (shareFidToken != null && !shareFidToken.isEmpty()) {
|
||||
bodyJson.put("fids_token", JsonArray.of(shareFidToken));
|
||||
}
|
||||
|
||||
client.postAbs(DOWNLOAD_URL)
|
||||
.addQueryParam("pr", "ucpro")
|
||||
.addQueryParam("fr", "pc")
|
||||
.putHeaders(header)
|
||||
.sendJsonObject(bodyJson)
|
||||
.onSuccess(res -> {
|
||||
log.debug("夸克 parseById 响应: {}", res.bodyAsString());
|
||||
JsonObject resJson = asJson(res);
|
||||
|
||||
if (resJson.getInteger("code") == 31001) {
|
||||
promise.fail("未登录或 Cookie 已失效");
|
||||
return;
|
||||
}
|
||||
|
||||
if (resJson.getInteger("code") != 0) {
|
||||
promise.fail(DOWNLOAD_URL + " 返回异常: " + resJson);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
JsonArray dataList = resJson.getJsonArray("data");
|
||||
if (dataList == null || dataList.isEmpty()) {
|
||||
promise.fail("夸克 API 返回的下载链接列表为空");
|
||||
return;
|
||||
}
|
||||
String downloadUrl = dataList.getJsonObject(0).getString("download_url");
|
||||
if (downloadUrl == null || downloadUrl.isEmpty()) {
|
||||
promise.fail("未找到下载链接");
|
||||
return;
|
||||
}
|
||||
promise.complete(downloadUrl);
|
||||
} catch (Exception e) {
|
||||
promise.fail("解析夸克下载链接失败: " + e.getMessage());
|
||||
}
|
||||
})
|
||||
.onFailure(t -> promise.fail("请求下载链接失败: " + t.getMessage()));
|
||||
|
||||
return promise.future();
|
||||
// 与 parse() 中的下载逻辑一致
|
||||
return Future.succeededFuture("");
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,14 @@ import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.regex.Pattern.compile;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author <a href="https://qaiu.top">QAIU</a>
|
||||
@@ -77,6 +80,55 @@ public class PanDomainTemplateTest {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWsPatternMatching() {
|
||||
Pattern wsPattern = PanDomainTemplate.WS.getPattern();
|
||||
|
||||
// 历史域名
|
||||
String[] positiveUrls = {
|
||||
"https://f.ws59.cn/f/f25625rv6p6",
|
||||
"https://f.ws28.cn/f/somekey123",
|
||||
"https://www.wenshushu.cn/f/abc123",
|
||||
// 新增域名
|
||||
"https://www.wenxiaozhan.net/f/testkey1",
|
||||
"https://www.wenxiaozhan.cn/f/testkey2",
|
||||
"https://www.wss.show/f/testkey3",
|
||||
"https://www.ws28.cn/f/testkey4",
|
||||
"https://www.wss.email/f/testkey5",
|
||||
"https://www.wss1.cn/f/testkey6",
|
||||
"https://www.ws59.cn/f/testkey7",
|
||||
"https://www.wss.cc/f/testkey8",
|
||||
"https://www.wss.pet/f/testkey9",
|
||||
"https://www.wss.ink/f/testkey10",
|
||||
"https://www.wenxiaozhan.com/f/testkey11",
|
||||
"https://www.wenshushu.com/f/testkey12",
|
||||
"https://www.wss.zone/f/testkey13",
|
||||
};
|
||||
|
||||
for (String url : positiveUrls) {
|
||||
Matcher m = wsPattern.matcher(url);
|
||||
assertTrue("WS pattern should match: " + url, m.matches());
|
||||
assertNotNull("KEY group should not be null for: " + url, m.group("KEY"));
|
||||
}
|
||||
|
||||
// 验证 KEY 提取正确性
|
||||
Matcher m1 = wsPattern.matcher("https://f.ws59.cn/f/f25625rv6p6");
|
||||
assertTrue(m1.matches());
|
||||
assertEquals("f25625rv6p6", m1.group("KEY"));
|
||||
|
||||
Matcher m2 = wsPattern.matcher("https://www.wenshushu.cn/f/abc123");
|
||||
assertTrue(m2.matches());
|
||||
assertEquals("abc123", m2.group("KEY"));
|
||||
|
||||
// 负例:错误路径不匹配
|
||||
assertFalse("Wrong path should not match",
|
||||
wsPattern.matcher("https://www.wenshushu.cn/x/abc123").matches());
|
||||
|
||||
// 负例:非白名单域名不匹配
|
||||
assertFalse("Non-whitelisted domain should not match",
|
||||
wsPattern.matcher("https://www.evil.com/f/abc123").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyDuplicates() {
|
||||
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -74,7 +74,7 @@
|
||||
<dependency>
|
||||
<groupId>cn.qaiu</groupId>
|
||||
<artifactId>parser</artifactId>
|
||||
<version>10.2.3</version>
|
||||
<version>10.2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -274,7 +274,7 @@
|
||||
name: '联想乐云'
|
||||
},
|
||||
fangcloud: {
|
||||
reg: /https:\/\/v2\.fangcloud\.(com|cn)\/(s|sharing)\/.+/,
|
||||
reg: /https:\/\/v2\.fangcloud\.(com|cn)\/(s|share|sharing)\/.+/,
|
||||
host: /fangcloud\.(com|cn)/,
|
||||
name: '亿方云'
|
||||
},
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
</div>
|
||||
<!-- 项目简介移到卡片内 -->
|
||||
<div class="project-intro">
|
||||
<div class="intro-title">NFD网盘直链解析0.2.1b2</div>
|
||||
<div class="intro-title">NFD网盘直链解析0.2.1b3</div>
|
||||
<div class="intro-desc">
|
||||
<div>支持网盘:蓝奏云、蓝奏云优享、小飞机盘、123云盘、奶牛快传、移动云空间、QQ邮箱云盘、QQ闪传等 <el-link style="color:#606cf5" href="https://github.com/qaiu/netdisk-fast-download?tab=readme-ov-file#%E7%BD%91%E7%9B%98%E6%94%AF%E6%8C%81%E6%83%85%E5%86%B5" target="_blank"> >> </el-link></div>
|
||||
<div>文件夹解析支持:蓝奏云、蓝奏云优享、小飞机盘、123云盘</div>
|
||||
@@ -794,26 +794,21 @@ export default {
|
||||
config = this.allAuthConfigs[panType]
|
||||
console.log(`[认证] 使用个人配置: ${this.getPanDisplayName(panType)}`)
|
||||
} else {
|
||||
// 从后端随机获取捐赠账号
|
||||
// 从后端随机获取捐赠账号(后端已加密,直接使用 encryptedAuth)
|
||||
try {
|
||||
const response = await axios.get(`${this.baseAPI}/v2/randomAuth`, { params: { panType } })
|
||||
// 解包 JsonResult 嵌套
|
||||
let data = response.data
|
||||
while (data && data.data !== undefined && data.code !== undefined) {
|
||||
data = data.data
|
||||
}
|
||||
if (data && (data.token || data.username)) {
|
||||
config = data
|
||||
const encryptedAuth = response.data?.data?.encryptedAuth
|
||||
if (encryptedAuth) {
|
||||
console.log(`[认证] 使用捐赠账号: ${this.getPanDisplayName(panType)}`)
|
||||
return encryptedAuth
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(`[认证] 无可用捐赠账号: ${this.getPanDisplayName(panType)}`)
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
if (!config) return ''
|
||||
|
||||
// 构建 JSON 对象
|
||||
// 个人配置:本地 AES 加密
|
||||
const authObj = {}
|
||||
if (config.authType) authObj.authType = config.authType
|
||||
if (config.username) authObj.username = config.username
|
||||
@@ -826,12 +821,9 @@ export default {
|
||||
if (config.ext3) authObj.ext3 = config.ext3
|
||||
if (config.ext4) authObj.ext4 = config.ext4
|
||||
if (config.ext5) authObj.ext5 = config.ext5
|
||||
if (config.donatedAccountToken) authObj.donatedAccountToken = config.donatedAccountToken
|
||||
|
||||
// AES 加密 + Base64 + URL 编码
|
||||
try {
|
||||
const jsonStr = JSON.stringify(authObj)
|
||||
const encrypted = this.aesEncrypt(jsonStr, 'nfd_auth_key2026')
|
||||
const encrypted = this.aesEncrypt(JSON.stringify(authObj), 'nfd_auth_key2026')
|
||||
return encodeURIComponent(encrypted)
|
||||
} catch (e) {
|
||||
console.error('生成认证参数失败:', e)
|
||||
|
||||
@@ -1,185 +0,0 @@
|
||||
package cn.qaiu.lz.common.util;
|
||||
|
||||
import cn.qaiu.lz.web.model.SysUser;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* JWT工具类,用于生成和验证JWT token
|
||||
*/
|
||||
public class JwtUtil {
|
||||
|
||||
private static final long EXPIRE_TIME = 24 * 60 * 60 * 1000; // token过期时间,24小时
|
||||
private static final String SECRET_KEY = "netdisk-fast-download-jwt-secret-key"; // 密钥
|
||||
private static final String ALGORITHM = "HmacSHA256";
|
||||
|
||||
/**
|
||||
* 生成JWT token
|
||||
*
|
||||
* @param user 用户信息
|
||||
* @return JWT token
|
||||
*/
|
||||
public static String generateToken(SysUser user) {
|
||||
long expireTime = getExpireTime();
|
||||
|
||||
// Header
|
||||
JsonObject header = new JsonObject()
|
||||
.put("alg", "HS256")
|
||||
.put("typ", "JWT");
|
||||
|
||||
// Payload
|
||||
JsonObject payload = new JsonObject()
|
||||
.put("id", user.getId())
|
||||
.put("username", user.getUsername())
|
||||
.put("role", user.getRole())
|
||||
.put("exp", expireTime)
|
||||
.put("iat", System.currentTimeMillis())
|
||||
.put("iss", "netdisk-fast-download");
|
||||
|
||||
// Base64 encode header and payload
|
||||
String encodedHeader = Base64.getUrlEncoder().withoutPadding().encodeToString(header.encode().getBytes(StandardCharsets.UTF_8));
|
||||
String encodedPayload = Base64.getUrlEncoder().withoutPadding().encodeToString(payload.encode().getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// Create signature
|
||||
String signature = hmacSha256(encodedHeader + "." + encodedPayload, SECRET_KEY);
|
||||
|
||||
// Combine to form JWT
|
||||
return encodedHeader + "." + encodedPayload + "." + signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用HMAC-SHA256算法生成签名
|
||||
*
|
||||
* @param data 要签名的数据
|
||||
* @param key 密钥
|
||||
* @return 签名
|
||||
*/
|
||||
private static String hmacSha256(String data, String key) {
|
||||
try {
|
||||
Mac sha256Hmac = Mac.getInstance(ALGORITHM);
|
||||
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
|
||||
sha256Hmac.init(secretKey);
|
||||
byte[] signedBytes = sha256Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(signedBytes);
|
||||
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||
throw new RuntimeException("Error creating HMAC SHA256 signature", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证JWT token
|
||||
*
|
||||
* @param token JWT token
|
||||
* @return 如果token有效返回true,否则返回false
|
||||
*/
|
||||
public static boolean validateToken(String token) {
|
||||
try {
|
||||
String[] parts = token.split("\\.");
|
||||
if (parts.length != 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String encodedHeader = parts[0];
|
||||
String encodedPayload = parts[1];
|
||||
String signature = parts[2];
|
||||
|
||||
// 验证签名
|
||||
String expectedSignature = hmacSha256(encodedHeader + "." + encodedPayload, SECRET_KEY);
|
||||
if (!expectedSignature.equals(signature)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 验证过期时间
|
||||
String payload = new String(Base64.getUrlDecoder().decode(encodedPayload), StandardCharsets.UTF_8);
|
||||
JsonObject payloadJson = new JsonObject(payload);
|
||||
long expTime = payloadJson.getLong("exp", 0L);
|
||||
|
||||
return System.currentTimeMillis() < expTime;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从token中获取用户ID
|
||||
*
|
||||
* @param token JWT token
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getUserIdFromToken(String token) {
|
||||
String[] parts = token.split("\\.");
|
||||
if (parts.length != 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Base64解码
|
||||
String payload = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
|
||||
JsonObject jsonObject = new JsonObject(payload);
|
||||
return jsonObject.getString("id");
|
||||
}
|
||||
|
||||
/**
|
||||
* 从token中获取用户名
|
||||
*
|
||||
* @param token JWT token
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getUsernameFromToken(String token) {
|
||||
String[] parts = token.split("\\.");
|
||||
if (parts.length != 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Base64解码
|
||||
String payload = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
|
||||
JsonObject jsonObject = new JsonObject(payload);
|
||||
return jsonObject.getString("username");
|
||||
}
|
||||
|
||||
/**
|
||||
* 从token中获取用户角色
|
||||
*
|
||||
* @param token JWT token
|
||||
* @return 用户角色
|
||||
*/
|
||||
public static String getRoleFromToken(String token) {
|
||||
String[] parts = token.split("\\.");
|
||||
if (parts.length != 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Base64解码
|
||||
String payload = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
|
||||
JsonObject jsonObject = new JsonObject(payload);
|
||||
return jsonObject.getString("role");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取过期时间
|
||||
*
|
||||
* @return 过期时间戳
|
||||
*/
|
||||
private static long getExpireTime() {
|
||||
return System.currentTimeMillis() + EXPIRE_TIME;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将过期时间戳转换为LocalDateTime
|
||||
*
|
||||
* @param expireTime 过期时间戳
|
||||
* @return LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime getExpireTimeAsLocalDateTime(long expireTime) {
|
||||
return LocalDateTime.ofInstant(Instant.ofEpochMilli(expireTime), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
||||
@@ -509,6 +509,15 @@ public class ParserApi {
|
||||
*/
|
||||
@RouteMapping(value = "/randomAuth", method = RouteMethod.GET)
|
||||
public Future<JsonObject> getRandomAuth(String panType) {
|
||||
return dbService.getRandomDonatedAccount(panType);
|
||||
return dbService.getRandomDonatedAccount(panType).map(res -> {
|
||||
if (Integer.valueOf(200).equals(res.getInteger("code")) && res.getJsonObject("data") != null) {
|
||||
JsonObject data = res.getJsonObject("data");
|
||||
String encryptedAuth = AuthParamCodec.encode(data);
|
||||
JsonObject safeData = new JsonObject();
|
||||
safeData.put("encryptedAuth", encryptedAuth);
|
||||
res.put("data", safeData);
|
||||
}
|
||||
return res;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package cn.qaiu.lz.web.model;
|
||||
|
||||
import cn.qaiu.db.ddl.Table;
|
||||
import cn.qaiu.lz.common.ToJson;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import io.vertx.codegen.annotations.DataObject;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Data
|
||||
@DataObject
|
||||
@NoArgsConstructor
|
||||
@Table("sys_user")
|
||||
public class SysUser implements ToJson {
|
||||
private String id;
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String email;
|
||||
private String phone;
|
||||
private String avatar;
|
||||
|
||||
// 用户状态:0-禁用,1-正常
|
||||
private Integer status;
|
||||
|
||||
// 用户角色:admin-管理员,user-普通用户
|
||||
private String role;
|
||||
|
||||
// 最后登录时间
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
|
||||
private LocalDateTime lastLoginTime;
|
||||
|
||||
private Integer age;
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
public SysUser(JsonObject json) {
|
||||
this.id = json.getString("id");
|
||||
this.username = json.getString("username");
|
||||
this.password = json.getString("password");
|
||||
this.email = json.getString("email");
|
||||
this.phone = json.getString("phone");
|
||||
this.avatar = json.getString("avatar");
|
||||
this.status = json.getInteger("status");
|
||||
this.role = json.getString("role");
|
||||
this.age = json.getInteger("age");
|
||||
if (json.getString("createTime") != null) {
|
||||
this.createTime = LocalDateTime.parse(json.getString("createTime"));
|
||||
}
|
||||
if (json.getString("lastLoginTime") != null) {
|
||||
this.lastLoginTime = LocalDateTime.parse(json.getString("lastLoginTime"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package cn.qaiu.lz.web.service;
|
||||
|
||||
import cn.qaiu.lz.web.model.SysUser;
|
||||
import cn.qaiu.vx.core.base.BaseAsyncService;
|
||||
import io.vertx.codegen.annotations.ProxyGen;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
/**
|
||||
* 用户服务接口
|
||||
* <br>Create date 2021/8/27 14:06
|
||||
*
|
||||
* @author <a href="https://qaiu.top">QAIU</a>
|
||||
*/
|
||||
@ProxyGen
|
||||
public interface UserService extends BaseAsyncService {
|
||||
/**
|
||||
* 用户登录
|
||||
* @param user 包含用户名和密码的用户对象
|
||||
* @return 登录成功返回用户信息和token,失败返回错误信息
|
||||
*/
|
||||
Future<JsonObject> login(SysUser user);
|
||||
|
||||
/**
|
||||
* 根据用户名获取用户信息
|
||||
* @param username 用户名
|
||||
* @return 用户信息
|
||||
*/
|
||||
Future<SysUser> getUserByUsername(String username);
|
||||
|
||||
/**
|
||||
* 创建新用户
|
||||
* @param user 用户信息
|
||||
* @return 创建成功返回用户信息,失败返回错误信息
|
||||
*/
|
||||
Future<SysUser> createUser(SysUser user);
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param user 用户信息
|
||||
* @return 更新成功返回用户信息,失败返回错误信息
|
||||
*/
|
||||
Future<SysUser> updateUser(SysUser user);
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
* @param token JWT token
|
||||
* @return 验证成功返回用户信息,失败返回错误信息
|
||||
*/
|
||||
Future<JsonObject> validateToken(String token);
|
||||
}
|
||||
@@ -1,414 +0,0 @@
|
||||
package cn.qaiu.lz.web.service.impl;
|
||||
|
||||
import cn.qaiu.db.pool.JDBCPoolInit;
|
||||
import cn.qaiu.lz.common.util.JwtUtil;
|
||||
import cn.qaiu.lz.common.util.PasswordUtil;
|
||||
import cn.qaiu.lz.web.model.SysUser;
|
||||
import cn.qaiu.lz.web.service.UserService;
|
||||
import cn.qaiu.vx.core.annotaions.Service;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.jdbcclient.JDBCPool;
|
||||
import io.vertx.sqlclient.Row;
|
||||
import io.vertx.sqlclient.RowSet;
|
||||
import io.vertx.sqlclient.Tuple;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 用户服务实现类
|
||||
* <br>Create date 2021/8/27 14:09
|
||||
*
|
||||
* @author <a href="https://qaiu.top">QAIU</a>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final JDBCPool jdbcPool = JDBCPoolInit.instance().getPool();
|
||||
|
||||
// 初始化方法,确保管理员用户存在
|
||||
public void init() {
|
||||
// 检查管理员用户是否存在
|
||||
getUserByUsername("admin")
|
||||
.onSuccess(user -> {
|
||||
log.info("管理员用户已存在");
|
||||
})
|
||||
.onFailure(err -> {
|
||||
// 创建管理员用户
|
||||
SysUser admin = new SysUser();
|
||||
admin.setId(UUID.randomUUID().toString());
|
||||
admin.setUsername("admin");
|
||||
admin.setPassword(PasswordUtil.hashPassword("admin123"));
|
||||
admin.setEmail("admin@example.com");
|
||||
admin.setRole("admin");
|
||||
admin.setStatus(1);
|
||||
admin.setCreateTime(LocalDateTime.now());
|
||||
|
||||
createUser(admin)
|
||||
.onSuccess(result -> log.info("管理员用户创建成功"))
|
||||
.onFailure(error -> log.error("管理员用户创建失败", error));
|
||||
});
|
||||
}
|
||||
|
||||
// 新增一个工具方法来过滤敏感信息
|
||||
private SysUser filterSensitiveInfo(SysUser user) {
|
||||
if (user != null) {
|
||||
SysUser filtered = new SysUser();
|
||||
// 复制除密码外的所有字段
|
||||
filtered.setId(user.getId());
|
||||
filtered.setUsername(user.getUsername());
|
||||
filtered.setEmail(user.getEmail());
|
||||
filtered.setPhone(user.getPhone());
|
||||
filtered.setAvatar(user.getAvatar());
|
||||
filtered.setRole(user.getRole());
|
||||
filtered.setStatus(user.getStatus());
|
||||
filtered.setCreateTime(user.getCreateTime());
|
||||
filtered.setLastLoginTime(user.getLastLoginTime());
|
||||
return filtered;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 将Row转换为SysUser对象
|
||||
private SysUser rowToUser(Row row) {
|
||||
if (row == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SysUser user = new SysUser();
|
||||
user.setId(row.getString("id"));
|
||||
user.setUsername(row.getString("username"));
|
||||
user.setPassword(row.getString("password"));
|
||||
user.setEmail(row.getString("email"));
|
||||
user.setPhone(row.getString("phone"));
|
||||
user.setAvatar(row.getString("avatar"));
|
||||
user.setRole(row.getString("role"));
|
||||
user.setStatus(row.getInteger("status"));
|
||||
|
||||
// 处理日期时间字段
|
||||
LocalDateTime createTime = row.getLocalDateTime("create_time");
|
||||
if (createTime != null) {
|
||||
user.setCreateTime(createTime);
|
||||
}
|
||||
|
||||
LocalDateTime lastLoginTime = row.getLocalDateTime("last_login_time");
|
||||
if (lastLoginTime != null) {
|
||||
user.setLastLoginTime(lastLoginTime);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<JsonObject> login(SysUser user) {
|
||||
// 参数校验
|
||||
if (user == null || user.getUsername() == null || user.getPassword() == null) {
|
||||
return Future.succeededFuture(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "用户名和密码不能为空"));
|
||||
}
|
||||
|
||||
Promise<JsonObject> promise = Promise.promise();
|
||||
|
||||
// 查询用户
|
||||
String sql = "SELECT * FROM sys_user WHERE username = ?";
|
||||
|
||||
jdbcPool.preparedQuery(sql)
|
||||
.execute(Tuple.of(user.getUsername()))
|
||||
.onSuccess(rows -> {
|
||||
if (rows.size() == 0) {
|
||||
promise.complete(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "用户不存在"));
|
||||
return;
|
||||
}
|
||||
|
||||
Row row = rows.iterator().next();
|
||||
SysUser existUser = rowToUser(row);
|
||||
|
||||
// 验证密码
|
||||
if (!PasswordUtil.checkPassword(user.getPassword(), existUser.getPassword())) {
|
||||
promise.complete(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "密码错误"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新最后登录时间
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
existUser.setLastLoginTime(now);
|
||||
|
||||
// 更新数据库中的最后登录时间
|
||||
String updateSql = "UPDATE sys_user SET last_login_time = ? WHERE username = ?";
|
||||
jdbcPool.preparedQuery(updateSql)
|
||||
.execute(Tuple.of(
|
||||
Timestamp.from(now.atZone(ZoneId.systemDefault()).toInstant()),
|
||||
existUser.getUsername()
|
||||
))
|
||||
.onFailure(err -> log.error("更新最后登录时间失败", err));
|
||||
|
||||
// 生成token
|
||||
String token = JwtUtil.generateToken(existUser);
|
||||
|
||||
// 返回用户信息和token
|
||||
JsonObject value = JsonObject.mapFrom(existUser);
|
||||
value.remove("password");
|
||||
promise.complete(new JsonObject()
|
||||
.put("success", true)
|
||||
.put("message", "登录成功")
|
||||
.put("token", token)
|
||||
.put("user", value));
|
||||
})
|
||||
.onFailure(err -> {
|
||||
log.error("登录查询失败", err);
|
||||
promise.complete(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "登录失败: " + err.getMessage()));
|
||||
});
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<SysUser> getUserByUsername(String username) {
|
||||
if (username == null || username.isEmpty()) {
|
||||
return Future.failedFuture("用户名不能为空");
|
||||
}
|
||||
|
||||
Promise<SysUser> promise = Promise.promise();
|
||||
|
||||
String sql = "SELECT * FROM sys_user WHERE username = ?";
|
||||
|
||||
jdbcPool.preparedQuery(sql)
|
||||
.execute(Tuple.of(username))
|
||||
.onSuccess(rows -> {
|
||||
if (rows.size() == 0) {
|
||||
promise.fail("用户不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
Row row = rows.iterator().next();
|
||||
SysUser user = rowToUser(row);
|
||||
promise.complete(filterSensitiveInfo(user));
|
||||
})
|
||||
.onFailure(err -> {
|
||||
log.error("查询用户失败", err);
|
||||
promise.fail("查询用户失败: " + err.getMessage());
|
||||
});
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<SysUser> createUser(SysUser user) {
|
||||
// 参数校验
|
||||
if (user == null || user.getUsername() == null || user.getPassword() == null) {
|
||||
return Future.failedFuture("用户名和密码不能为空");
|
||||
}
|
||||
|
||||
Promise<SysUser> promise = Promise.promise();
|
||||
|
||||
// 先检查用户是否已存在
|
||||
String checkSql = "SELECT COUNT(*) as count FROM sys_user WHERE username = ?";
|
||||
|
||||
jdbcPool.preparedQuery(checkSql)
|
||||
.execute(Tuple.of(user.getUsername()))
|
||||
.onSuccess(rows -> {
|
||||
Row row = rows.iterator().next();
|
||||
long count = row.getLong("count");
|
||||
|
||||
if (count > 0) {
|
||||
promise.fail("用户名已存在");
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置用户ID和创建时间
|
||||
if (user.getId() == null) {
|
||||
user.setId(UUID.randomUUID().toString());
|
||||
}
|
||||
if (user.getCreateTime() == null) {
|
||||
user.setCreateTime(LocalDateTime.now());
|
||||
}
|
||||
|
||||
// 设置默认角色和状态
|
||||
if (user.getRole() == null) {
|
||||
user.setRole("user");
|
||||
}
|
||||
if (user.getStatus() == null) {
|
||||
user.setStatus(1);
|
||||
}
|
||||
|
||||
// 对密码进行加密
|
||||
String plainPassword = user.getPassword();
|
||||
user.setPassword(PasswordUtil.hashPassword(plainPassword));
|
||||
|
||||
// 插入用户
|
||||
String insertSql = "INSERT INTO sys_user (id, username, password, email, phone, avatar, role, status, create_time) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
jdbcPool.preparedQuery(insertSql)
|
||||
.execute(Tuple.of(
|
||||
user.getId(),
|
||||
user.getUsername(),
|
||||
user.getPassword(),
|
||||
user.getEmail(),
|
||||
user.getPhone(),
|
||||
user.getAvatar(),
|
||||
user.getRole(),
|
||||
user.getStatus(),
|
||||
Timestamp.from(user.getCreateTime().atZone(ZoneId.systemDefault()).toInstant())
|
||||
))
|
||||
.onSuccess(result -> {
|
||||
promise.complete(filterSensitiveInfo(user));
|
||||
})
|
||||
.onFailure(err -> {
|
||||
log.error("创建用户失败", err);
|
||||
promise.fail("创建用户失败: " + err.getMessage());
|
||||
});
|
||||
})
|
||||
.onFailure(err -> {
|
||||
log.error("检查用户是否存在失败", err);
|
||||
promise.fail("创建用户失败: " + err.getMessage());
|
||||
});
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<SysUser> updateUser(SysUser user) {
|
||||
// 参数校验
|
||||
if (user == null || user.getUsername() == null) {
|
||||
return Future.failedFuture("用户名不能为空");
|
||||
}
|
||||
|
||||
Promise<SysUser> promise = Promise.promise();
|
||||
|
||||
// 先检查用户是否存在
|
||||
String checkSql = "SELECT * FROM sys_user WHERE username = ?";
|
||||
|
||||
jdbcPool.preparedQuery(checkSql)
|
||||
.execute(Tuple.of(user.getUsername()))
|
||||
.onSuccess(rows -> {
|
||||
if (rows.size() == 0) {
|
||||
promise.fail("用户不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
Row row = rows.iterator().next();
|
||||
SysUser existUser = rowToUser(row);
|
||||
|
||||
// 构建更新SQL
|
||||
StringBuilder updateSql = new StringBuilder("UPDATE sys_user SET ");
|
||||
Tuple params = Tuple.tuple();
|
||||
|
||||
if (user.getEmail() != null) {
|
||||
updateSql.append("email = ?, ");
|
||||
params.addValue(user.getEmail());
|
||||
}
|
||||
|
||||
if (user.getPhone() != null) {
|
||||
updateSql.append("phone = ?, ");
|
||||
params.addValue(user.getPhone());
|
||||
}
|
||||
|
||||
if (user.getAvatar() != null) {
|
||||
updateSql.append("avatar = ?, ");
|
||||
params.addValue(user.getAvatar());
|
||||
}
|
||||
|
||||
if (user.getStatus() != null) {
|
||||
updateSql.append("status = ?, ");
|
||||
params.addValue(user.getStatus());
|
||||
}
|
||||
|
||||
if (user.getRole() != null) {
|
||||
updateSql.append("role = ?, ");
|
||||
params.addValue(user.getRole());
|
||||
}
|
||||
|
||||
if (user.getPassword() != null) {
|
||||
updateSql.append("password = ?, ");
|
||||
params.addValue(PasswordUtil.hashPassword(user.getPassword()));
|
||||
}
|
||||
|
||||
// 移除最后的逗号和空格
|
||||
String sql = updateSql.toString();
|
||||
if (sql.endsWith(", ")) {
|
||||
sql = sql.substring(0, sql.length() - 2);
|
||||
}
|
||||
|
||||
// 如果没有要更新的字段,直接返回
|
||||
if (params.size() == 0) {
|
||||
promise.complete(filterSensitiveInfo(existUser));
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加WHERE条件
|
||||
sql += " WHERE username = ?";
|
||||
params.addValue(user.getUsername());
|
||||
|
||||
// 执行更新
|
||||
jdbcPool.preparedQuery(sql)
|
||||
.execute(params)
|
||||
.onSuccess(result -> {
|
||||
// 重新查询用户信息
|
||||
getUserByUsername(user.getUsername())
|
||||
.onSuccess(promise::complete)
|
||||
.onFailure(promise::fail);
|
||||
})
|
||||
.onFailure(err -> {
|
||||
log.error("更新用户失败", err);
|
||||
promise.fail("更新用户失败: " + err.getMessage());
|
||||
});
|
||||
})
|
||||
.onFailure(err -> {
|
||||
log.error("查询用户失败", err);
|
||||
promise.fail("更新用户失败: " + err.getMessage());
|
||||
});
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<JsonObject> validateToken(String token) {
|
||||
if (token == null || token.isEmpty()) {
|
||||
return Future.succeededFuture(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "Token不能为空"));
|
||||
}
|
||||
|
||||
// 验证token
|
||||
boolean isValid = JwtUtil.validateToken(token);
|
||||
if (!isValid) {
|
||||
return Future.succeededFuture(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "Token无效或已过期"));
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
String username = JwtUtil.getUsernameFromToken(token);
|
||||
|
||||
Promise<JsonObject> promise = Promise.promise();
|
||||
|
||||
getUserByUsername(username)
|
||||
.onSuccess(user -> {
|
||||
promise.complete(new JsonObject()
|
||||
.put("success", true)
|
||||
.put("message", "Token有效")
|
||||
.put("user", JsonObject.mapFrom(user)));
|
||||
})
|
||||
.onFailure(err -> {
|
||||
promise.complete(new JsonObject()
|
||||
.put("success", false)
|
||||
.put("message", "用户不存在"));
|
||||
});
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user