Compare commits

..
1 Commits
Author SHA1 Message Date
q ecb60b1fae Fixed 蓝奏云目录解析cookie验证问题 2026-02-03 13:26:40 +08:00
112 changed files with 2407 additions and 13043 deletions

No files matched your search

-11
View File
@@ -41,9 +41,7 @@ gradlew.bat
unused.txt unused.txt
/web-service/src/main/generated/ /web-service/src/main/generated/
/db /db
/netdisk-fast-download/
/webroot/nfd-front/ /webroot/nfd-front/
/netdisk-fast-download/webroot/nfd-front/
package-lock.json package-lock.json
# Maven generated files # Maven generated files
@@ -82,12 +80,3 @@ yarn-error.log*
*.iml *.iml
*.ipr *.ipr
*.iws *.iws
# Build directories
**/target/
**/build/
**/classes/
**/out/
**/${project.build.directory}/
**/${project.basedir}/target/
**/${basedir}/target/
+1 -2
View File
@@ -1,5 +1,4 @@
{ {
"java.compile.nullAnalysis.mode": "automatic", "java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "interactive", "java.configuration.updateBuildConfiguration": "interactive"
"java.debug.settings.onBuildFailureProceed": true
} }
+65 -94
View File
@@ -1,60 +1,38 @@
# 一款网盘分享链接云解析快速下载服务 <p align="center">
QQ交流群:1017480890 <img src="https://github.com/user-attachments/assets/87401aae-b0b6-4ffb-bbeb-44756404d26f" alt="项目预览图" />
</p>
<p align="center"> <p align="center">
<a href="https://github.com/qaiu/netdisk-fast-download/actions/workflows/maven.yml"><img src="https://img.shields.io/github/actions/workflow/status/qaiu/netdisk-fast-download/maven.yml?branch=v0.1.9b8a&style=flat"></a> <a href="https://github.com/qaiu/netdisk-fast-download/actions/workflows/maven.yml"><img src="https://img.shields.io/github/actions/workflow/status/qaiu/netdisk-fast-download/maven.yml?branch=v0.1.9b8a&style=flat"></a>
<a href="https://www.oracle.com/cn/java/technologies/downloads"><img src="https://img.shields.io/badge/jdk-%3E%3D17-blue"></a> <a href="https://www.oracle.com/cn/java/technologies/downloads"><img src="https://img.shields.io/badge/jdk-%3E%3D17-blue"></a>
<a href="https://vertx-china.github.io"><img src="https://img.shields.io/badge/vert.x-4.5.24-blue?style=flat"></a> <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://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> <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>
<a href="https://atomgit.com/QAIU/netdisk-fast-download"><img src="https://atomgit.com/QAIU/netdisk-fast-download/star/badge.svg" alt="AtomGit"></a>
<a href="https://oosmetrics.com/repo/qaiu/netdisk-fast-download"><img src="https://api.oosmetrics.com/api/v1/badge/achievement/826aa27a-6e59-4de5-b7fa-cd189f484035.svg"></a>
<p align="center">
<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>
<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>
## 国内镜像
本项目同步托管于 **AtomGit**,国内访问更流畅:👉 [https://atomgit.com/QAIU/netdisk-fast-download](https://atomgit.com/QAIU/netdisk-fast-download)
## 介绍
> netdisk-fast-download网盘直链解析可以把云盘分享链接转为直链,可广泛应用于各类下载站,资源站,个人博客,图床,APP下载更新,视频点播等领域。支持市面各大主流云盘的文件分享以及文件夹分享链接,已支持蓝奏云/蓝奏云优享/移动云云空间/小飞机盘/亿方云/123云盘/Cloudreve等,支持加密分享,以及部分网盘文件夹分享。
[官方文档](https://nfd-parser.github.io/) # netdisk-fast-download 网盘分享链接云解析服务
[API接入](https://nfdparser.apifox.cn/) QQ交流群:1017480890
[公益解析,lz站](https://lz.qaiu.top)
[公益解析,lz0站](https://lz0.qaiu.top) netdisk-fast-download网盘直链云解析(nfd云解析)能把网盘分享下载链接转化为直链,支持多款云盘,已支持蓝奏云/蓝奏云优享/奶牛快传/移动云云空间/小飞机盘/亿方云/123云盘/Cloudreve等,支持加密分享,以及部分网盘文件夹分享。
[专业版](https://189.qaiu.top)
## 快速开始 ## 快速开始
命令行下载分享文件: 命令行下载分享文件:
```shell ```shell
curl -LOJ "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/Tk1F2kGQ&pwd=1234" curl -LOJ "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234"
``` ```
或者使用wget: 或者使用wget:
```shell ```shell
wget -O bilibili.mp4 "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/Tk1F2kGQ&pwd=1234" wget -O bilibili.mp4 "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&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%2FTk1F2kGQ&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%2FnQOaNRPW&name=bilibili.mp4&ext=mp4):
``` ```
### 调用演示站下载: ### 调用演示站下载:
https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/Tk1F2kGQ&pwd=1234 https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&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%2FTk1F2kGQ&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%2FnQOaNRPW&name=bilibili.mp4&ext=mp4
``` ```
@@ -62,14 +40,20 @@ https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.
**JavaScript解析器文档:** [JavaScript解析器开发指南](parser/doc/JAVASCRIPT_PARSER_GUIDE.md) | [自定义解析器扩展指南](parser/doc/CUSTOM_PARSER_GUIDE.md) | [快速开始](parser/doc/CUSTOM_PARSER_QUICKSTART.md) **JavaScript解析器文档:** [JavaScript解析器开发指南](parser/doc/JAVASCRIPT_PARSER_GUIDE.md) | [自定义解析器扩展指南](parser/doc/CUSTOM_PARSER_GUIDE.md) | [快速开始](parser/doc/CUSTOM_PARSER_QUICKSTART.md)
**Playground功能:** [JS解析器演练场密码保护说明](web-service/doc/PLAYGROUND_PASSWORD_PROTECTION.md) **Playground功能:** [JS解析器演练场密码保护说明](PLAYGROUND_PASSWORD_PROTECTION.md)
## 预览地址
[预览地址1](https://lz.qaiu.top)
[预览地址2](https://lz0.qaiu.top)
[天翼云盘/移动云盘限时体验版](https://189.qaiu.top)
**注意⚠️小飞机解析有IP限制,多数云服务商的大陆IP会被拦截(可以自行配置代理),和本程序无关** main分支依赖JDK17, 提供了JDK11分支[main-jdk11](https://github.com/qaiu/netdisk-fast-download/tree/main-jdk11)
**注意⚠️收到很多用户反馈,小飞机近期封号频繁,请尽可能选择其他网盘分享** **0.1.8及以上版本json接口格式有调整 参考json返回数据格式示例**
**注意⚠️请不要过度依赖 lz.qaiu.top,建议本地搭建或者云服务器自行搭建。请求量过多的话服务器可能会被云盘厂商限制,遇到解析失败的分享链接不要着急提issues,请先检查分享是否有效。** **小飞机解析有IP限制,多数云服务商的大陆IP会被拦截(可以自行配置代理),和本程序无关**
**注意: 请不要过度依赖lz.qaiu.top预览地址服务,建议本地搭建或者云服务器自行搭建。解析次数过多IP会被部分网盘厂商限制,不推荐做公共解析。**
## 网盘支持情况: ## 网盘支持情况:
> 20230905 奶牛云直链做了防盗链,需加入请求头:Referer: https://cowtransfer.com/
> 20230824 123云盘解析大文件(>100MB)失效,需要登录 > 20230824 123云盘解析大文件(>100MB)失效,需要登录
> 20230722 UC网盘解析失效,需要登录 > 20230722 UC网盘解析失效,需要登录
@@ -77,11 +61,13 @@ https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.
- [蓝奏云-lz](https://pc.woozooo.com/) - [蓝奏云-lz](https://pc.woozooo.com/)
- [蓝奏云优享-iz](https://www.ilanzou.com/) - [蓝奏云优享-iz](https://www.ilanzou.com/)
- [奶牛快传-cow](https://cowtransfer.com/)
- [移动云云空间-ec](https://www.ecpan.cn/web) - [移动云云空间-ec](https://www.ecpan.cn/web)
- [小飞机网盘-fj](https://www.feijipan.com/) - [小飞机网盘-fj](https://www.feijipan.com/)
- [亿方云-fc](https://www.fangcloud.com/) - [亿方云-fc](https://www.fangcloud.com/)
- [123云盘-ye](https://www.123pan.com/) - [123云盘-ye](https://www.123pan.com/)
- ~[115网盘(失效)-p115](https://115.com/)~ - ~[115网盘(失效)-p115](https://115.com/)~
- ~[118网盘(已停服)-p118](https://www.118pan.com/)~
- [文叔叔-ws](https://www.wenshushu.cn/) - [文叔叔-ws](https://www.wenshushu.cn/)
- [联想乐云-le](https://lecloud.lenovo.com/) - [联想乐云-le](https://lecloud.lenovo.com/)
- [QQ邮箱云盘-qqw](https://mail.qq.com/) - [QQ邮箱云盘-qqw](https://mail.qq.com/)
@@ -94,14 +80,18 @@ https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.
- [Cloudreve自建网盘-ce](https://github.com/cloudreve/Cloudreve) - [Cloudreve自建网盘-ce](https://github.com/cloudreve/Cloudreve)
- ~[微雨云存储-pvvy](https://www.vyuyun.com/)~ - ~[微雨云存储-pvvy](https://www.vyuyun.com/)~
- [超星云盘(需要referer: https://pan-yz.chaoxing.com)-pcx](https://pan-yz.chaoxing.com) - [超星云盘(需要referer: https://pan-yz.chaoxing.com)-pcx](https://pan-yz.chaoxing.com)
- [飞书云盘-fs](https://www.feishu.cn/)
- [WPS云文档-pwps](https://www.kdocs.cn/) - [WPS云文档-pwps](https://www.kdocs.cn/)
- [汽水音乐-qishui_music](https://music.douyin.com/qishui/) - [汽水音乐-qishui_music](https://music.douyin.com/qishui/)
- [咪咕音乐-migu](https://music.migu.cn/) - [咪咕音乐-migu](https://music.migu.cn/)
- [一刻相册-baidu_photo](https://photo.baidu.com/)
- Google云盘-pgd - Google云盘-pgd
- Onedrive-pod - Onedrive-pod
- Dropbox-pdp - Dropbox-pdp
- iCloud-pic - iCloud-pic
### 仅专属版提供
- [移动云盘-p139](https://yun.139.com/)
- [联通云盘-pwo](https://pan.wo.cn/)
- [天翼云盘-p189](https://cloud.189.cn/)
## API接口 ## API接口
@@ -150,56 +140,6 @@ GET /json/getFileList?url={分享链接}&pwd={密码}
- `{网盘标识}` 参考支持的网盘列表 - `{网盘标识}` 参考支持的网盘列表
- `your_host` 替换为您的域名或 IP - `your_host` 替换为您的域名或 IP
### 认证参数(v0.2.1+)
[可以使用在线认证参数加密](https://qaiu.top/nfd-auth.html)
部分网盘(如夸克、UC)需要登录后的 Cookie 才能解析和下载。可通过 `auth` 参数传递认证信息:
**参数格式**:`auth` 参数值为 AES 加密后的 JSON 字符串,经过 Base64 编码和 URL 编码
**加密方式**:
- 算法:AES/ECB/PKCS5Padding
- 密钥:`nfd_auth_key2026`(16字节)
- 流程:JSON → AES加密 → Base64 → URL编码
**JSON 结构**:
```json
{
"authType": "cookie", // 认证类型: cookie/accesstoken/authorization/password/custom
"token": "your_cookie_here", // Cookie 或 Token 内容
"username": "", // 用户名(password 类型时使用)
"password": "", // 密码(password 类型时使用)
"ext1": "", // 扩展字段1(custom 类型时使用)
"ext2": "" // 扩展字段2(custom 类型时使用)
}
```
**网盘认证要求**:
| 网盘 | 认证要求 | 说明 |
|------|---------|------|
| 夸克网盘(QK) | **必须** | 必须配置 Cookie 才能解析 |
| UC网盘(UC) | **必须** | 必须配置 Cookie 才能解析 |
| 小飞机网盘(FJ) | 可选 | 大文件(>100MB)需要认证 |
| 蓝奏优享(IZ) | 可选 | 大文件需要认证 |
**使用示例**:
```
GET /parser?url={分享链接}&pwd={密码}&auth={加密后的认证参数}
```
> 💡 提示:Web 界面已内置认证配置功能,可自动处理加密过程,无需手动构造参数。
> [可以使用在线认证参数加密](https://qaiu.top/nfd-auth.html)
#### 密钥作用说明
- `server.authEncryptKey`
- 作用:用于 `auth` 参数的 AES 加解密
- 要求:16位(AES-128)
- `server.donatedAccountFailureTokenSignKey`
- 作用:用于“捐赠账号失败计数 token”的 HMAC 签名/验签
- 目的:防止客户端伪造失败计数请求
- 建议:使用高强度随机字符串,且不要与 `authEncryptKey` 相同
### 特殊说明 ### 特殊说明
- 移动云云空间的 `分享key` 取分享链接中的 `data` 参数值 - 移动云云空间的 `分享key` 取分享链接中的 `data` 参数值
@@ -333,15 +273,15 @@ json返回数据格式示例:
| 网盘名称 | 免登陆下载分享 | 加密分享 | 初始网盘空间 | 单文件大小限制 | | 网盘名称 | 免登陆下载分享 | 加密分享 | 初始网盘空间 | 单文件大小限制 |
|-------------|---------|----------|-----------|-----------------| |-------------|---------|----------|-----------|-----------------|
| 蓝奏云 | √ | √ | 不限空间 | 100M | | 蓝奏云 | √ | √ | 不限空间 | 100M |
| 奶牛快传 | √ | X | 10G | 不限大小 |
| 移动云云空间(个人版) | √ | √(密码可忽略) | 5G(个人) | 不限大小 | | 移动云云空间(个人版) | √ | √(密码可忽略) | 5G(个人) | 不限大小 |
| 小飞机网盘 | √ | √ | 10G | 不限大小 | | 小飞机网盘 | √ | √(密码可忽略) | 10G | 不限大小 |
| 360亿方云 | √ | √ | 100G(须实名) | 不限大小 | | 360亿方云 | √ | √(密码可忽略) | 100G(须实名) | 不限大小 |
| 123云盘 | √ | √ | 2T | 100G(>100M需要登录) | | 123云盘 | √ | √ | 2T | 100G(>100M需要登录) |
| 文叔叔 | √ | √ | 10G | 5GB | | 文叔叔 | √ | √ | 10G | 5GB |
| WPS云文档 | √ | X | 5G(免费) | 10M(免费)/2G(会员) | | WPS云文档 | √ | X | 5G(免费) | 10M(免费)/2G(会员) |
| 夸克网盘 | x | √ | 10G | 不限大小 | | 夸克网盘 | x | √ | 10G | 不限大小 |
| UC网盘 | x | √ | 10G | 不限大小 | | UC网盘 | x | √ | 10G | 不限大小 |
| 飞书云盘 | √ | X | 15G | 不限大小 |
# 打包部署 # 打包部署
@@ -494,6 +434,23 @@ auths:
**注意:** 目前仅支持 123(ye)的认证配置。 **注意:** 目前仅支持 123(ye)的认证配置。
## 开发计划
### v0.1.8~v0.1.9 ✓
- API添加文件信息(专属版/开源版)
- 目录解析(专属版/开源版)
- 文件预览功能(专属版/开源版)
- 文件夹预览功能(开源版)
- 友好的错误提示和一键反馈功能(开源版)
- 带cookie/token/username/pwd参数解析大文件(专属版)
### v0.2.x
- web后台管理--认证配置/分享链接管理(开源版/专属版)
- 123/小飞机/蓝奏优享等大文件解析(开源版)
- 直链分享(开源版/专属版)
- aria2/idm+/curl/wget链接生成(开源版/专属版)
- IP限流配置(开源版/专属版)
- refere防盗链,API鉴权防盗链(专属版)
- 123/小飞机/蓝奏优享/蓝奏文件夹解析API,天翼云盘/移动云盘文件夹解析API(专属版)
- 用户管理面板--营销推广系统(专属版)
**技术栈:** **技术栈:**
Jdk17+Vert.x4 Jdk17+Vert.x4
@@ -518,6 +475,20 @@ Core模块集成Vert.x实现类似spring的注解式路由API
</p> </p>
### 关于赞助定制专属版
1. 专属版提供对小飞机,蓝奏优享大文件解析的支持, 提供天翼云盘/移动云盘/联通云盘的解析支持。
2. 可提供托管服务:包含部署服务和云服务器环境。
3. 可提供功能定制开发。
您可能需要提供一定的资金赞助支持定制专属版, 请添加以下任意一个联系方式详谈赞助模式:
<p>qq: 197575894</p>
<p>wechat: imcoding_</p>
<!--
![image](https://github.com/qaiu/netdisk-fast-download/assets/29825328/54276aee-cc3f-4ebd-8973-2e15f6295819)
[手机端支付宝打赏跳转链接](https://qr.alipay.com/fkx01882dnoxxtjenhlxt53)
-->
-6
View File
@@ -73,12 +73,6 @@
<version>${jackson.version}</version> <version>${jackson.version}</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
@@ -0,0 +1,73 @@
package cn.qaiu.vx.core.verticle.conf;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.impl.JsonUtil;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
/**
* Converter and mapper for {@link cn.qaiu.vx.core.verticle.conf.HttpProxyConf}.
* NOTE: This class has been automatically generated from the {@link cn.qaiu.vx.core.verticle.conf.HttpProxyConf} original class using Vert.x codegen.
*/
public class HttpProxyConfConverter {
private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER;
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER;
static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, HttpProxyConf obj) {
for (java.util.Map.Entry<String, Object> member : json) {
switch (member.getKey()) {
case "password":
if (member.getValue() instanceof String) {
obj.setPassword((String)member.getValue());
}
break;
case "port":
if (member.getValue() instanceof Number) {
obj.setPort(((Number)member.getValue()).intValue());
}
break;
case "preProxyOptions":
if (member.getValue() instanceof JsonObject) {
obj.setPreProxyOptions(new io.vertx.core.net.ProxyOptions((io.vertx.core.json.JsonObject)member.getValue()));
}
break;
case "timeout":
if (member.getValue() instanceof Number) {
obj.setTimeout(((Number)member.getValue()).intValue());
}
break;
case "username":
if (member.getValue() instanceof String) {
obj.setUsername((String)member.getValue());
}
break;
}
}
}
static void toJson(HttpProxyConf obj, JsonObject json) {
toJson(obj, json.getMap());
}
static void toJson(HttpProxyConf obj, java.util.Map<String, Object> json) {
if (obj.getPassword() != null) {
json.put("password", obj.getPassword());
}
if (obj.getPort() != null) {
json.put("port", obj.getPort());
}
if (obj.getPreProxyOptions() != null) {
json.put("preProxyOptions", obj.getPreProxyOptions().toJson());
}
if (obj.getTimeout() != null) {
json.put("timeout", obj.getTimeout());
}
if (obj.getUsername() != null) {
json.put("username", obj.getUsername());
}
}
}
+11 -77
View File
@@ -3,13 +3,12 @@ package cn.qaiu.vx.core;
import cn.qaiu.vx.core.util.CommonUtil; import cn.qaiu.vx.core.util.CommonUtil;
import cn.qaiu.vx.core.util.ConfigUtil; import cn.qaiu.vx.core.util.ConfigUtil;
import cn.qaiu.vx.core.util.VertxHolder; import cn.qaiu.vx.core.util.VertxHolder;
import cn.qaiu.vx.core.verticle.HttpProxyVerticle;
import cn.qaiu.vx.core.verticle.PostExecVerticle;
import cn.qaiu.vx.core.verticle.ReverseProxyVerticle; import cn.qaiu.vx.core.verticle.ReverseProxyVerticle;
import cn.qaiu.vx.core.verticle.RouterVerticle; import cn.qaiu.vx.core.verticle.RouterVerticle;
import cn.qaiu.vx.core.verticle.ServiceVerticle; import cn.qaiu.vx.core.verticle.ServiceVerticle;
import io.vertx.core.*; import io.vertx.core.*;
import io.vertx.core.dns.AddressResolverOptions; import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.impl.launcher.commands.VersionCommand;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.LocalMap; import io.vertx.core.shareddata.LocalMap;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -18,7 +17,6 @@ import org.slf4j.LoggerFactory;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.UUID;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import static cn.qaiu.vx.core.util.ConfigConstant.*; import static cn.qaiu.vx.core.util.ConfigConstant.*;
@@ -56,7 +54,6 @@ public final class Deploy {
public void start(String[] args, Handler<JsonObject> handle) { public void start(String[] args, Handler<JsonObject> handle) {
this.mainThread = Thread.currentThread(); this.mainThread = Thread.currentThread();
this.handle = handle; this.handle = handle;
if (args.length > 0 && args[0].startsWith("app-")) { if (args.length > 0 && args[0].startsWith("app-")) {
// 启动参数dev或者prod // 启动参数dev或者prod
path.append("-").append(args[0].replace("app-","")); path.append("-").append(args[0].replace("app-",""));
@@ -107,7 +104,7 @@ public final class Deploy {
System.out.printf(logoTemplate, System.out.printf(logoTemplate,
CommonUtil.getAppVersion(), CommonUtil.getAppVersion(),
"4x", VersionCommand.getVersion(),
conf.getString("copyright"), conf.getString("copyright"),
year year
); );
@@ -126,12 +123,12 @@ public final class Deploy {
var vertxOptions = vertxConfigELPS == 0 ? var vertxOptions = vertxConfigELPS == 0 ?
new VertxOptions() : new VertxOptions(vertxConfig); new VertxOptions() : new VertxOptions(vertxConfig);
// vertxOptions.setAddressResolverOptions( vertxOptions.setAddressResolverOptions(
// new AddressResolverOptions(). new AddressResolverOptions().
// addServer("114.114.114.114"). addServer("114.114.114.114").
// addServer("114.114.115.115"). addServer("114.114.115.115").
// addServer("8.8.8.8"). addServer("8.8.8.8").
// addServer("8.8.4.4")); addServer("8.8.4.4"));
LOGGER.info("vertxConfigEventLoopPoolSize: {}, eventLoopPoolSize: {}, workerPoolSize: {}", vertxConfigELPS, LOGGER.info("vertxConfigEventLoopPoolSize: {}, eventLoopPoolSize: {}, workerPoolSize: {}", vertxConfigELPS,
vertxOptions.getEventLoopPoolSize(), vertxOptions.getEventLoopPoolSize(),
vertxOptions.getWorkerPoolSize()); vertxOptions.getWorkerPoolSize());
@@ -156,39 +153,12 @@ public final class Deploy {
var future2 = vertx.deployVerticle(ServiceVerticle.class, getWorkDeploymentOptions("Service")); var future2 = vertx.deployVerticle(ServiceVerticle.class, getWorkDeploymentOptions("Service"));
var future3 = vertx.deployVerticle(ReverseProxyVerticle.class, getWorkDeploymentOptions("proxy")); var future3 = vertx.deployVerticle(ReverseProxyVerticle.class, getWorkDeploymentOptions("proxy"));
Future.all(future1, future2, future3)
JsonObject jsonObject = ((JsonObject) localMap.get(GLOBAL_CONFIG)).getJsonObject("proxy-server"); .onSuccess(this::deployWorkVerticalSuccess)
if (jsonObject != null) { .onFailure(this::deployVerticalFailed);
genPwd(jsonObject);
var future4 = vertx.deployVerticle(HttpProxyVerticle.class, getWorkDeploymentOptions("proxy"));
future4.onSuccess(LOGGER::info);
future4.onFailure(e -> LOGGER.error("Other handle error", e));
Future.all(future1, future2, future3, future4)
.onSuccess(this::deployWorkVerticalSuccess)
.onFailure(this::deployVerticalFailed);
} else {
Future.all(future1, future2, future3)
.onSuccess(this::deployWorkVerticalSuccess)
.onFailure(this::deployVerticalFailed);
}
}).onFailure(e -> LOGGER.error("Other handle error", e)); }).onFailure(e -> LOGGER.error("Other handle error", e));
} }
private static void genPwd(JsonObject jsonObject) {
if (jsonObject.getBoolean("randUserPwd")) {
var username = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
var password = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
jsonObject.put("username", username);
jsonObject.put("password", password);
}
LOGGER.info("=============server info=================");
LOGGER.info("\nport: {}\nusername: {}\npassword: {}",
jsonObject.getString("port"),
jsonObject.getString("username"),
jsonObject.getString("password"));
LOGGER.info("==============server info================");
}
/** /**
* 部署失败 * 部署失败
* *
@@ -208,42 +178,6 @@ public final class Deploy {
var t1 = ((double) (System.currentTimeMillis() - startTime)) / 1000; var t1 = ((double) (System.currentTimeMillis() - startTime)) / 1000;
var t2 = ((double) System.currentTimeMillis() - ManagementFactory.getRuntimeMXBean().getStartTime()) / 1000; var t2 = ((double) System.currentTimeMillis() - ManagementFactory.getRuntimeMXBean().getStartTime()) / 1000;
LOGGER.info("web服务启动成功 -> 用时: {}s, jvm启动用时: {}s", t1, t2); LOGGER.info("web服务启动成功 -> 用时: {}s, jvm启动用时: {}s", t1, t2);
// 检查是否处于安装引导模式(数据库未配置)
Object installMode = VertxHolder.getVertxInstance().sharedData()
.getLocalMap(LOCAL).get("installMode");
if (Boolean.TRUE.equals(installMode)) {
LOGGER.info("系统处于安装引导模式,等待用户完成数据库配置后再启动后置初始化...");
return;
}
// 正常模式:部署 PostExecVerticle 执行 AppRun 实现
deployPostExec();
}
/**
* 部署 PostExecVerticle(执行所有 AppRun 实现)
* 安装引导完成后也可手动调用此方法触发后置初始化
*/
public void deployPostExec() {
var vertx = VertxHolder.getVertxInstance();
var postExecFuture = vertx.deployVerticle(PostExecVerticle.class, getWorkDeploymentOptions("postExec", 2));
postExecFuture.onSuccess(id -> {
LOGGER.info("PostExecVerticle 部署成功,AppRun 实现执行完成");
}).onFailure(e -> {
LOGGER.error("PostExecVerticle 部署失败", e);
});
}
/**
* 重新部署 ServiceVerticle,重新注册因 DB 未就绪而失败的服务到 EventBus
* 安装引导完成、DB 初始化后调用
*/
public void redeployServices() {
var vertx = VertxHolder.getVertxInstance();
vertx.deployVerticle(ServiceVerticle.class, getWorkDeploymentOptions("Service"))
.onSuccess(id -> LOGGER.info("ServiceVerticle 重新部署成功,DB 相关服务已注册"))
.onFailure(e -> LOGGER.error("ServiceVerticle 重新部署失败", e));
} }
/** /**
@@ -9,7 +9,6 @@ import java.lang.annotation.*;
public @interface HandleSortFilter { public @interface HandleSortFilter {
/** /**
* 注册顺序,数字越大越先注册<br> * 注册顺序,数字越大越先注册<br>
* 前置拦截器会先执行后注册即数字小的, 后置拦截器会先执行先注册的即数字大的<br>
* 值<0时会过滤掉该处理器 * 值<0时会过滤掉该处理器
*/ */
int value() default 0; int value() default 0;
@@ -1,12 +0,0 @@
package cn.qaiu.vx.core.base;
import io.vertx.core.json.JsonObject;
public interface AppRun {
/**
* 执行方法
* @param config 启动配置文件
*/
void execute(JsonObject config);
}
@@ -38,20 +38,6 @@ public interface BaseHttpApi {
handleAfterInterceptor(ctx, jsonResult.toJsonObject()); handleAfterInterceptor(ctx, jsonResult.toJsonObject());
} }
default void doFireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject, int statusCode) {
if (!ctx.response().ended()) {
fireJsonObjectResponse(ctx, jsonObject, statusCode);
}
handleAfterInterceptor(ctx, jsonObject);
}
default <T> void doFireJsonResultResponse(RoutingContext ctx, JsonResult<T> jsonResult, int statusCode) {
if (!ctx.response().ended()) {
fireJsonResultResponse(ctx, jsonResult, statusCode);
}
handleAfterInterceptor(ctx, jsonResult.toJsonObject());
}
default Set<AfterInterceptor> getAfterInterceptor() { default Set<AfterInterceptor> getAfterInterceptor() {
@@ -1,23 +0,0 @@
package cn.qaiu.vx.core.base;
import cn.qaiu.vx.core.annotaions.HandleSortFilter;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 默认的AppRun实现示例
* <br>Create date 2024-01-01 00:00:00
*
* @author <a href="https://qaiu.top">QAIU</a>
*/
@HandleSortFilter
public class DefaultAppRun implements AppRun {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAppRun.class);
@Override
public void execute(JsonObject config) {
LOGGER.info("======> AppRun实现类开始执行,配置数: {}", config.size());
}
}
@@ -23,6 +23,8 @@ import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.*; import io.vertx.ext.web.handler.*;
import io.vertx.ext.web.handler.sockjs.SockJSHandler; import io.vertx.ext.web.handler.sockjs.SockJSHandler;
import io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions; import io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.sstore.SessionStore;
import javassist.CtClass; import javassist.CtClass;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
@@ -74,15 +76,15 @@ public class RouterHandlerFactory implements BaseHttpApi {
// 主路由 // 主路由
Router mainRouter = Router.router(VertxHolder.getVertxInstance()); Router mainRouter = Router.router(VertxHolder.getVertxInstance());
mainRouter.route().handler(ctx -> { mainRouter.route().handler(ctx -> {
String realPath = ctx.request().uri(); String realPath = ctx.request().uri();;
if (realPath.startsWith(REROUTE_PATH_PREFIX)) { if (realPath.startsWith(REROUTE_PATH_PREFIX)) {
// vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath // vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath
String rePath = realPath.replace(REROUTE_PATH_PREFIX, ""); String rePath = realPath.substring(REROUTE_PATH_PREFIX.length());
ctx.reroute(rePath); ctx.reroute(rePath);
return; return;
} }
LOGGER.debug("New request:{}, {}, {}", LOGGER.debug("The HTTP service request address information ===>path:{}, uri:{}, method:{}",
ctx.request().path(), ctx.request().absoluteURI(), ctx.request().method()); ctx.request().path(), ctx.request().absoluteURI(), ctx.request().method());
ctx.response().headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); ctx.response().headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
ctx.response().headers().add(DATE, LocalDateTime.now().format(ISO_LOCAL_DATE_TIME)); ctx.response().headers().add(DATE, LocalDateTime.now().format(ISO_LOCAL_DATE_TIME));
@@ -98,6 +100,16 @@ public class RouterHandlerFactory implements BaseHttpApi {
// 配置文件上传路径 // 配置文件上传路径
mainRouter.route().handler(BodyHandler.create().setUploadsDirectory("uploads")); mainRouter.route().handler(BodyHandler.create().setUploadsDirectory("uploads"));
// 配置Session管理 - 用于演练场登录状态持久化
// 30天过期时间(毫秒)
SessionStore sessionStore = LocalSessionStore.create(VertxHolder.getVertxInstance());
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
.setSessionTimeout(30L * 24 * 60 * 60 * 1000) // 30天
.setSessionCookieName("SESSIONID") // Cookie名称
.setCookieHttpOnlyFlag(true) // 防止XSS攻击
.setCookieSecureFlag(false); // 非HTTPS环境设置为false
mainRouter.route().handler(sessionHandler);
// 拦截器 // 拦截器
Set<Handler<RoutingContext>> interceptorSet = getInterceptorSet(); Set<Handler<RoutingContext>> interceptorSet = getInterceptorSet();
Route route0 = mainRouter.route("/*"); Route route0 = mainRouter.route("/*");
@@ -177,10 +189,10 @@ public class RouterHandlerFactory implements BaseHttpApi {
if (ctx.response().ended()) return; if (ctx.response().ended()) return;
// 超时处理器状态码503 // 超时处理器状态码503
if (ctx.statusCode() == 503 || ctx.failure() == null) { if (ctx.statusCode() == 503 || ctx.failure() == null) {
doFireJsonResultResponse(ctx, JsonResult.error("未知异常, 请联系管理员"), 503); doFireJsonResultResponse(ctx, JsonResult.error("未知异常, 请联系管理员", 500));
} else { } else {
ctx.failure().printStackTrace(); ctx.failure().printStackTrace();
doFireJsonResultResponse(ctx, JsonResult.error(ctx.failure().getMessage()), 500); doFireJsonResultResponse(ctx, JsonResult.error(ctx.failure().getMessage(), 500));
} }
}); });
} else if (method.isAnnotationPresent(SockRouteMapper.class)) { } else if (method.isAnnotationPresent(SockRouteMapper.class)) {
@@ -234,7 +246,7 @@ public class RouterHandlerFactory implements BaseHttpApi {
*/ */
private Set<Handler<RoutingContext>> getInterceptorSet() { private Set<Handler<RoutingContext>> getInterceptorSet() {
// 配置拦截 // 配置拦截
return getBeforeInterceptor().stream().map(BeforeInterceptor::doHandle).collect(Collectors.toCollection(LinkedHashSet::new)); return getBeforeInterceptor().stream().map(BeforeInterceptor::doHandle).collect(Collectors.toSet());
} }
/** /**
@@ -303,19 +315,18 @@ public class RouterHandlerFactory implements BaseHttpApi {
final MultiMap queryParams = ctx.queryParams(); final MultiMap queryParams = ctx.queryParams();
// 解析body-json参数 // 解析body-json参数
if (HttpHeaderValues.APPLICATION_JSON.toString().equals(ctx.parsedHeaders().contentType().value())) { // 只处理POST/PUT/PATCH等有body的请求方法,避免GET请求读取body导致"Request has already been read"错误
String httpMethod = ctx.request().method().name();
if (("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod))
&& HttpHeaderValues.APPLICATION_JSON.toString().equals(ctx.parsedHeaders().contentType().value())
&& ctx.body() != null && ctx.body().asJsonObject() != null) {
JsonObject body = ctx.body().asJsonObject(); JsonObject body = ctx.body().asJsonObject();
if (body != null) { if (body != null) {
methodParametersTemp.forEach((k, v) -> { methodParametersTemp.forEach((k, v) -> {
String typeName = v.getRight().getName();
// 直接绑定 JsonObject 类型参数
if (JsonObject.class.getName().equals(typeName)) {
parameterValueList.put(k, body);
}
// 只解析已配置包名前缀的实体类 // 只解析已配置包名前缀的实体类
else if (CommonUtil.matchRegList(entityPackagesReg.getList(), typeName)) { if (CommonUtil.matchRegList(entityPackagesReg.getList(), v.getRight().getName())) {
try { try {
Class<?> aClass = Class.forName(typeName); Class<?> aClass = Class.forName(v.getRight().getName());
JsonObject data = CommonUtil.getSubJsonForEntity(body, aClass); JsonObject data = CommonUtil.getSubJsonForEntity(body, aClass);
if (!data.isEmpty()) { if (!data.isEmpty()) {
Object entity = data.mapTo(aClass); Object entity = data.mapTo(aClass);
@@ -324,20 +335,12 @@ public class RouterHandlerFactory implements BaseHttpApi {
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}); });
} else {
// body 可能是 JsonArray
JsonArray bodyArray = ctx.body().asJsonArray();
if (bodyArray != null) {
methodParametersTemp.forEach((k, v) -> {
if (JsonArray.class.getName().equals(v.getRight().getName())) {
parameterValueList.put(k, bodyArray);
}
});
}
} }
} else if (ctx.body() != null) { } else if (("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod))
&& ctx.body() != null) {
queryParams.addAll(ParamUtil.paramsToMap(ctx.body().asString())); queryParams.addAll(ParamUtil.paramsToMap(ctx.body().asString()));
} }
@@ -367,20 +370,6 @@ public class RouterHandlerFactory implements BaseHttpApi {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} else if (parameterValueList.get(k) == null
&& JsonObject.class.getName().equals(v.getRight().getName())) {
// 兜底: content-type 非 application/json 时尝试从 body 解析 JsonObject
if (ctx.body() != null) {
JsonObject jo = ctx.body().asJsonObject();
if (jo != null) parameterValueList.put(k, jo);
}
} else if (parameterValueList.get(k) == null
&& JsonArray.class.getName().equals(v.getRight().getName())) {
// 兜底: content-type 非 application/json 时尝试从 body 解析 JsonArray
if (ctx.body() != null) {
JsonArray ja = ctx.body().asJsonArray();
if (ja != null) parameterValueList.put(k, ja);
}
} }
}); });
// 调用handle 获取响应对象 // 调用handle 获取响应对象
@@ -390,25 +379,25 @@ public class RouterHandlerFactory implements BaseHttpApi {
Object data = ReflectionUtil.invokeWithArguments(method, instance, parameterValueArray); Object data = ReflectionUtil.invokeWithArguments(method, instance, parameterValueArray);
if (data != null) { if (data != null) {
if (data instanceof JsonResult jsonResult) { if (data instanceof JsonResult) {
doFireJsonResultResponse(ctx, (JsonResult<?>) data, jsonResult.getCode()); doFireJsonResultResponse(ctx, (JsonResult<?>) data);
} }
if (data instanceof JsonObject) { if (data instanceof JsonObject) {
doFireJsonObjectResponse(ctx, ((JsonObject) data)); doFireJsonObjectResponse(ctx, ((JsonObject) data));
} else if (data instanceof Future) { // 处理异步响应 } else if (data instanceof Future) { // 处理异步响应
((Future<?>) data).onSuccess(res -> { ((Future<?>) data).onSuccess(res -> {
if (res instanceof JsonResult jsonResult) { if (res instanceof JsonResult) {
doFireJsonResultResponse(ctx, jsonResult, jsonResult.getCode()); doFireJsonResultResponse(ctx, (JsonResult<?>) res);
} }
if (res instanceof JsonObject) { if (res instanceof JsonObject) {
doFireJsonObjectResponse(ctx, ((JsonObject) res)); doFireJsonObjectResponse(ctx, ((JsonObject) res));
} else if (res != null) { } else if (res != null) {
doFireJsonResultResponse(ctx, JsonResult.data(res)); doFireJsonResultResponse(ctx, JsonResult.data(res));
} else { } else {
doFireJsonResultResponse(ctx, JsonResult.data(null)); handleAfterInterceptor(ctx, null);
} }
}).onFailure(e -> doFireJsonResultResponse(ctx, JsonResult.error(e.getMessage()), 500)); }).onFailure(e -> doFireJsonResultResponse(ctx, JsonResult.error(e.getMessage())));
} else { } else {
doFireJsonResultResponse(ctx, JsonResult.data(data)); doFireJsonResultResponse(ctx, JsonResult.data(data));
} }
@@ -423,7 +412,7 @@ public class RouterHandlerFactory implements BaseHttpApi {
err = e.getCause().getMessage(); err = e.getCause().getMessage();
} }
} }
doFireJsonResultResponse(ctx, JsonResult.error(err), 500); doFireJsonResultResponse(ctx, JsonResult.error(err));
} }
} }
@@ -3,12 +3,10 @@ package cn.qaiu.vx.core.interceptor;
import io.vertx.core.Handler; import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.RoutingContext;
import static cn.qaiu.vx.core.util.ResponseUtil.sendError;
/** /**
* 前置拦截器接口 * 前置拦截器接口
* <p>
* 注意:Vert.x是异步非阻塞框架,不能在Event Loop中使用synchronized等阻塞操作!
* 所有操作都应该是非阻塞的,使用Vert.x的上下文数据存储机制保证线程安全。
* </p>
* *
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
*/ */
@@ -16,25 +14,28 @@ public interface BeforeInterceptor extends Handler<RoutingContext> {
String IS_NEXT = "RoutingContextIsNext"; String IS_NEXT = "RoutingContextIsNext";
default Handler<RoutingContext> doHandle() { default Handler<RoutingContext> doHandle() {
return ctx -> { return ctx -> {
// 【优化】移除synchronized锁,Vert.x的RoutingContext本身就是线程安全的 // 加同步锁
// 每个请求都有独立的RoutingContext,不需要额外加锁 synchronized (BeforeInterceptor.class) {
ctx.put(IS_NEXT, false); ctx.put(IS_NEXT, false);
handle(ctx); // 调用具体的处理逻辑 BeforeInterceptor.this.handle(ctx);
// 确保如果没有调用doNext()并且响应未结束,则返回错误 if (!(Boolean) ctx.get(IS_NEXT) && !ctx.response().ended()) {
// if (!(Boolean) ctx.get(IS_NEXT) && !ctx.response().ended()) { sendError(ctx, 403);
// sendError(ctx, 403); }
// } }
}; };
} }
default void doNext(RoutingContext context) { default void doNext(RoutingContext context) {
// 【优化】移除synchronized锁 // 设置上下文状态为可以继续执行
// RoutingContext的put和next操作是线程安全的,不需要额外同步 // 添加同步锁保障多线程下执行时序
context.put(IS_NEXT, true); synchronized (BeforeInterceptor.class) {
context.next(); // 继续执行下一个处理器 context.put(IS_NEXT, true);
context.next();
}
} }
void handle(RoutingContext context); // 实现具体的拦截处理逻辑 void handle(RoutingContext context);
}
}
@@ -30,7 +30,7 @@ public class JsonResult<T> implements Serializable {
private int code = SUCCESS_CODE;//状态码 private int code = SUCCESS_CODE;//状态码
private String msg = SUCCESS_MESSAGE;//消息 private String msg = SUCCESS_MESSAGE; //消息
private boolean success = true; //是否成功 private boolean success = true; //是否成功
@@ -1,7 +1,7 @@
/** /**
* ModuleGen cn.qaiu.vx.core * ModuleGen cn.qaiu.vx.core
*/ */
@ModuleGen(name = "vertx-http-proxy", groupPackage = "cn.qaiu.vx.core") @ModuleGen(name = "vertx-http-proxy", groupPackage = "cn.qaiu.vx.core", useFutures = true)
package cn.qaiu.vx.core; package cn.qaiu.vx.core;
import io.vertx.codegen.annotations.ModuleGen; import io.vertx.codegen.annotations.ModuleGen;
@@ -5,7 +5,7 @@ import io.vertx.serviceproxy.ServiceProxyBuilder;
/** /**
* @author Xu Haidong * @author Xu Haidong
* @date 2018/8/15 * Create at 2018/8/15
*/ */
public final class AsyncServiceUtil { public final class AsyncServiceUtil {
@@ -13,7 +13,6 @@ import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.List; import java.util.List;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@@ -118,7 +117,7 @@ public class CommonUtil {
return set.stream().filter(c1 -> { return set.stream().filter(c1 -> {
HandleSortFilter s1 = c1.getAnnotation(HandleSortFilter.class); HandleSortFilter s1 = c1.getAnnotation(HandleSortFilter.class);
if (s1 != null) { if (s1 != null) {
return s1.value() >= 0; return s1.value() > 0;
} else { } else {
return true; return true;
} }
@@ -139,7 +138,7 @@ public class CommonUtil {
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}).collect(Collectors.toCollection(LinkedHashSet::new)); }).collect(Collectors.toSet());
} }
private static String appVersion; private static String appVersion;
@@ -4,13 +4,9 @@ import io.vertx.config.ConfigRetriever;
import io.vertx.config.ConfigRetrieverOptions; import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions; import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/** /**
* 异步读取配置工具类 * 异步读取配置工具类
* <br>Create date 2021/9/2 1:23 * <br>Create date 2021/9/2 1:23
@@ -28,29 +24,7 @@ public class ConfigUtil {
* @return JsonObject的Future * @return JsonObject的Future
*/ */
public static Future<JsonObject> readConfig(String format, String path, Vertx vertx) { public static Future<JsonObject> readConfig(String format, String path, Vertx vertx) {
// 支持 classpath: 前缀从类路径读取,否则从文件系统读取 // 读取yml配置
if (path != null && path.startsWith("classpath:")) {
String resource = path.substring("classpath:".length());
// 使用 executeBlocking(Callable) 直接返回 Future<JsonObject>
return vertx.executeBlocking(() -> {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (is == null) {
throw new RuntimeException("classpath resource not found: " + resource);
}
try (InputStream in = is) {
byte[] bytes = in.readAllBytes();
String content = new String(bytes, StandardCharsets.UTF_8);
if ("json".equalsIgnoreCase(format)) {
return new JsonObject(content);
} else {
throw new RuntimeException("unsupported classpath format: " + format);
}
}
});
}
Promise<JsonObject> promise = Promise.promise();
ConfigStoreOptions store = new ConfigStoreOptions() ConfigStoreOptions store = new ConfigStoreOptions()
.setType("file") .setType("file")
.setFormat(format) .setFormat(format)
@@ -59,22 +33,10 @@ public class ConfigUtil {
ConfigRetriever retriever = ConfigRetriever ConfigRetriever retriever = ConfigRetriever
.create(vertx, new ConfigRetrieverOptions().addStore(store)); .create(vertx, new ConfigRetrieverOptions().addStore(store));
// 异步获取配置 return retriever.getConfig();
// 成功直接完成 promise
retriever.getConfig()
.onSuccess(promise::complete)
.onFailure(err -> {
// 配置读取失败,直接返回失败 Future
promise.fail(new RuntimeException(
"读取配置文件失败: " + path, err));
retriever.close();
});
return promise.future();
} }
/** /**
* 异步读取Yaml配置文件 * 异步读取Yaml配置文件
* *
@@ -1,20 +0,0 @@
package cn.qaiu.vx.core.util;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import java.util.concurrent.ExecutionException;
public class FutureUtils {
public static <T> T getResult(Future<T> future) {
try {
return future.toCompletionStage().toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
public static <T> T getResult(Promise<T> promise) {
return promise.future().toCompletionStage().toCompletableFuture().join();
}
}
@@ -16,7 +16,7 @@ import java.time.format.DateTimeFormatter;
/** /**
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
* @date 2023/10/14 9:07 * Create at 2023/10/14 9:07
*/ */
public class JacksonConfig { public class JacksonConfig {
@@ -36,8 +36,6 @@ import static cn.qaiu.vx.core.util.ConfigConstant.BASE_LOCATIONS;
*/ */
public final class ReflectionUtil { public final class ReflectionUtil {
// 缓存Reflections实例,避免重复扫描(每次扫描约35K+值,耗时1-3秒,占用大量内存)
private static final Map<String, Reflections> REFLECTIONS_CACHE = new java.util.concurrent.ConcurrentHashMap<>();
/** /**
* 以默认配置的基础包路径获取反射器 * 以默认配置的基础包路径获取反射器
@@ -49,48 +47,52 @@ public final class ReflectionUtil {
} }
/** /**
* 获取反射器(带缓存) * 获取反射器
* *
* @param packageAddress Package address String * @param packageAddress Package address String
* @return Reflections object * @return Reflections object
*/ */
public static Reflections getReflections(String packageAddress) { public static Reflections getReflections(String packageAddress) {
return REFLECTIONS_CACHE.computeIfAbsent(packageAddress, key -> { List<String> packageAddressList;
List<String> packageAddressList; if (packageAddress.contains(",")) {
if (key.contains(",")) { packageAddressList = Arrays.asList(packageAddress.split(","));
packageAddressList = Arrays.asList(key.split(",")); } else if (packageAddress.contains(";")) {
} else if (key.contains(";")) { packageAddressList = Arrays.asList(packageAddress.split(";"));
packageAddressList = Arrays.asList(key.split(";")); } else {
} else { packageAddressList = Collections.singletonList(packageAddress);
packageAddressList = Collections.singletonList(key); }
}
return createReflections(packageAddressList); return getReflections(packageAddressList);
});
} }
/** /**
* 获取反射器(带缓存) * 获取反射器
* *
* @param packageAddresses Package address List * @param packageAddresses Package address List
* @return Reflections object * @return Reflections object
*/ */
public static Reflections getReflections(List<String> packageAddresses) { public static Reflections getReflections(List<String> packageAddresses) {
String cacheKey = String.join(",", packageAddresses); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
return REFLECTIONS_CACHE.computeIfAbsent(cacheKey, key -> createReflections(packageAddresses)); FilterBuilder filterBuilder = new FilterBuilder();
} packageAddresses.forEach(str -> {
Collection<URL> urls = ClasspathHelper.forPackage(str.trim());
configurationBuilder.addUrls(urls);
filterBuilder.includePackage(str.trim());
});
private static Reflections createReflections(List<String> packageAddresses) { // 采坑记录 2021-05-08
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder() // 发现注解api层 没有继承父类时 这里反射一直有问题(Scanner SubTypesScanner was not configured)
.addClassLoaders(Thread.currentThread().getContextClassLoader()) // 因此这里需要手动配置各种Scanner扫描器 -- https://blog.csdn.net/qq_29499107/article/details/106889781
.forPackages(packageAddresses.toArray(new String[0])) configurationBuilder.setScanners(
.setScanners( Scanners.SubTypes.filterResultsBy(s -> true), //允许getAllTypes获取所有Object的子类, 不设置为false则 getAllTypes
Scanners.SubTypes.filterResultsBy(s -> true), //允许getAllTypes获取所有Object的子类, 不设置为false则 getAllTypes // 会报错.默认为true.
// 会报错.默认为true. new MethodParameterNamesScanner(), //设置方法参数名称 扫描器,否则调用getConstructorParamNames 会报错
new MethodParameterNamesScanner(), //设置方法参数名称 扫描器,否则调用getConstructorParamNames 会报错 Scanners.MethodsAnnotated, //设置方法注解 扫描器, 否则getConstructorsAnnotatedWith,getMethodsAnnotatedWith 会报错
Scanners.MethodsAnnotated, //设置方法注解 扫描器, 否则getConstructorsAnnotatedWith,getMethodsAnnotatedWith 会报错 new MemberUsageScanner(), //设置 member 扫描器,否则 getMethodUsage 会报错
new MemberUsageScanner(), //设置 member 扫描器,否则 getMethodUsage 会报错 Scanners.TypesAnnotated //设置类注解 扫描器 ,否则 getTypesAnnotatedWith 会报错
Scanners.TypesAnnotated //设置类注解 扫描器 ,否则 getTypesAnnotatedWith 会报错 );
);
configurationBuilder.filterInputsBy(filterBuilder);
return new Reflections(configurationBuilder); return new Reflections(configurationBuilder);
} }
@@ -13,7 +13,6 @@ public class ResponseUtil {
public static void redirect(HttpServerResponse response, String url) { public static void redirect(HttpServerResponse response, String url) {
response.putHeader(CONTENT_TYPE, "text/html; charset=utf-8") response.putHeader(CONTENT_TYPE, "text/html; charset=utf-8")
.putHeader("Referrer-Policy", "no-referrer")
.putHeader(HttpHeaders.LOCATION, url).setStatusCode(302).end(); .putHeader(HttpHeaders.LOCATION, url).setStatusCode(302).end();
} }
@@ -23,22 +22,14 @@ public class ResponseUtil {
} }
public static void fireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject) { public static void fireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject) {
fireJsonObjectResponse(ctx, jsonObject, 200);
}
public static void fireJsonObjectResponse(HttpServerResponse ctx, JsonObject jsonObject) {
fireJsonObjectResponse(ctx, jsonObject, 200);
}
public static void fireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject, int statusCode) {
ctx.response().putHeader(CONTENT_TYPE, "application/json; charset=utf-8") ctx.response().putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
.setStatusCode(statusCode) .setStatusCode(200)
.end(jsonObject.encode()); .end(jsonObject.encode());
} }
public static void fireJsonObjectResponse(HttpServerResponse ctx, JsonObject jsonObject, int statusCode) { public static void fireJsonObjectResponse(HttpServerResponse ctx, JsonObject jsonObject) {
ctx.putHeader(CONTENT_TYPE, "application/json; charset=utf-8") ctx.putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
.setStatusCode(statusCode) .setStatusCode(200)
.end(jsonObject.encode()); .end(jsonObject.encode());
} }
@@ -46,10 +37,6 @@ public class ResponseUtil {
fireJsonObjectResponse(ctx, jsonResult.toJsonObject()); fireJsonObjectResponse(ctx, jsonResult.toJsonObject());
} }
public static <T> void fireJsonResultResponse(RoutingContext ctx, JsonResult<T> jsonResult, int statusCode) {
fireJsonObjectResponse(ctx, jsonResult.toJsonObject(), statusCode);
}
public static <T> void fireJsonResultResponse(HttpServerResponse ctx, JsonResult<T> jsonResult) { public static <T> void fireJsonResultResponse(HttpServerResponse ctx, JsonResult<T> jsonResult) {
fireJsonObjectResponse(ctx, jsonResult.toJsonObject()); fireJsonObjectResponse(ctx, jsonResult.toJsonObject());
} }
@@ -1,77 +1,50 @@
package cn.qaiu.vx.core.verticle; package cn.qaiu.vx.core.verticle;
import io.vertx.core.AbstractVerticle; import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.http.*; import io.vertx.core.http.*;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.NetClient; import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions; import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.ProxyOptions; import io.vertx.core.net.ProxyOptions;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.Base64; import java.util.Base64;
import static cn.qaiu.vx.core.util.ConfigConstant.GLOBAL_CONFIG;
import static cn.qaiu.vx.core.util.ConfigConstant.LOCAL;
/** /**
* *
*/ */
public class HttpProxyVerticle extends AbstractVerticle { public class HttpProxyVerticle extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyVerticle.class);
private HttpClient httpClient; private HttpClient httpClient;
private NetClient netClient; private NetClient netClient;
private JsonObject proxyPreConf;
private JsonObject proxyServerConf;
@Override @Override
public void start() { public void start() {
proxyServerConf = ((JsonObject)vertx.sharedData().getLocalMap(LOCAL).get(GLOBAL_CONFIG)).getJsonObject("proxy-server"); ProxyOptions proxyOptions = new ProxyOptions().setHost("127.0.0.1").setPort(7890);
proxyPreConf = ((JsonObject)vertx.sharedData().getLocalMap(LOCAL).get(GLOBAL_CONFIG)).getJsonObject("proxy-pre");
Integer serverPort = proxyServerConf.getInteger("port");
ProxyOptions proxyOptions = null;
if (proxyPreConf != null && StringUtils.isNotBlank(proxyPreConf.getString("ip"))) {
proxyOptions = new ProxyOptions(proxyPreConf);
}
// 初始化 HTTP 客户端,用于向目标服务器发送 HTTP 请求 // 初始化 HTTP 客户端,用于向目标服务器发送 HTTP 请求
HttpClientOptions httpClientOptions = new HttpClientOptions(); HttpClientOptions httpClientOptions = new HttpClientOptions();
if (proxyOptions != null) { httpClient = vertx.createHttpClient(httpClientOptions.setProxyOptions(proxyOptions));
httpClientOptions.setProxyOptions(proxyOptions);
}
httpClient = vertx.createHttpClient(httpClientOptions);
// 创建并启动 HTTP 代理服务器,监听指定端口 // 创建并启动 HTTP 代理服务器,监听指定端口
HttpServerOptions httpServerOptions = new HttpServerOptions(); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setClientAuth(ClientAuth.REQUIRED));
if (proxyServerConf.containsKey("username") &&
StringUtils.isNotBlank(proxyServerConf.getString("username"))) {
httpServerOptions.setClientAuth(ClientAuth.REQUIRED);
}
HttpServer server = vertx.createHttpServer();
server.requestHandler(this::handleClientRequest); server.requestHandler(this::handleClientRequest);
// 初始化 NetClient,用于在 CONNECT 请求中建立 TCP 连接隧道 // 初始化 NetClient,用于在 CONNECT 请求中建立 TCP 连接隧道
NetClientOptions netClientOptions = new NetClientOptions(); netClient = vertx.createNetClient(new NetClientOptions()
.setProxyOptions(proxyOptions)
if (proxyOptions != null) {
httpClientOptions.setProxyOptions(proxyOptions);
}
netClient = vertx.createNetClient(netClientOptions
.setConnectTimeout(15000) .setConnectTimeout(15000)
.setTrustAll(true)); .setTrustAll(true));
// 启动 HTTP 代理服务器 // 启动 HTTP 代理服务器
server.listen(serverPort) server.listen(7891, ar -> {
.onSuccess(res-> LOGGER.info("HTTP Proxy server started on port {}", serverPort)) if (ar.succeeded()) {
.onFailure(err-> LOGGER.error("Failed to start HTTP Proxy server: " + err.getMessage())); System.out.println("HTTP Proxy server started on port 7891");
} else {
System.err.println("Failed to start HTTP Proxy server: " + ar.cause());
}
});
} }
// 处理 HTTP CONNECT 请求,用于代理 HTTPS 流量 // 处理 HTTP CONNECT 请求,用于代理 HTTPS 流量
@@ -93,54 +66,49 @@ public class HttpProxyVerticle extends AbstractVerticle {
} }
clientRequest.pause(); clientRequest.pause();
// 通过 NetClient 连接目标服务器并创建隧道 // 通过 NetClient 连接目标服务器并创建隧道
netClient.connect(targetPort, targetHost) netClient.connect(targetPort, targetHost, connectionAttempt -> {
.onSuccess(targetSocket -> { if (connectionAttempt.succeeded()) {
// Upgrade client connection to NetSocket and implement bidirectional data flow NetSocket targetSocket = connectionAttempt.result();
clientRequest.toNetSocket()
.onSuccess(clientSocket -> {
// Set up bidirectional data forwarding
clientSocket.handler(targetSocket::write);
targetSocket.handler(clientSocket::write);
// Close the other socket when one side closes // 升级客户端连接到 NetSocket 并实现双向数据流
clientSocket.closeHandler(v -> targetSocket.close()); clientRequest.toNetSocket().onComplete(clientSocketAttempt -> {
targetSocket.closeHandler(v -> clientSocket.close()); if (clientSocketAttempt.succeeded()) {
}) NetSocket clientSocket = clientSocketAttempt.result();
.onFailure(clientSocketAttempt -> {
System.err.println("Failed to upgrade client connection to socket: " + clientSocketAttempt.getMessage()); // 设置双向数据流转发
targetSocket.close(); clientSocket.handler(targetSocket::write);
clientRequest.response().setStatusCode(500).end("Internal Server Error"); targetSocket.handler(clientSocket::write);
});
}) // 关闭其中一方时关闭另一方
.onFailure(connectionAttempt -> { clientSocket.closeHandler(v -> targetSocket.close());
System.err.println("Failed to connect to target: " + connectionAttempt.getMessage()); targetSocket.closeHandler(v -> clientSocket.close());
clientRequest.response().setStatusCode(502).end("Bad Gateway: Unable to connect to target"); } else {
System.err.println("Failed to upgrade client connection to socket: " + clientSocketAttempt.cause().getMessage());
targetSocket.close();
clientRequest.response().setStatusCode(500).end("Internal Server Error");
}
}); });
} else {
System.err.println("Failed to connect to target: " + connectionAttempt.cause().getMessage());
clientRequest.response().setStatusCode(502).end("Bad Gateway: Unable to connect to target");
}
});
} }
// 处理客户端的 HTTP 请求 // 处理客户端的 HTTP 请求
private void handleClientRequest(HttpServerRequest clientRequest) { private void handleClientRequest(HttpServerRequest clientRequest) {
// 打印来源ip和访问目标URI String s = clientRequest.headers().get("Proxy-Authorization");
LOGGER.debug("source: {}, target: {}", clientRequest.remoteAddress().toString(), clientRequest.uri()); if (s == null) {
if (proxyServerConf.containsKey("username") && clientRequest.response().setStatusCode(403).end();
StringUtils.isNotBlank(proxyServerConf.getString("username"))) { return;
String s = clientRequest.headers().get("Proxy-Authorization");
if (s == null) {
clientRequest.response().setStatusCode(403).end();
return;
}
String[] split = new String(Base64.getDecoder().decode(s.replace("Basic ", ""))).split(":");
if (split.length > 1) {
// TODO
String username = proxyServerConf.getString("username");
String password = proxyServerConf.getString("password");
if (!split[0].equals(username) || !split[1].equals(password)) {
LOGGER.info("-----auth failed------\nusername: {}\npassword: {}", username, password);
clientRequest.response().setStatusCode(403).end();
return;
}
}
} }
String[] split = new String(Base64.getDecoder().decode(s.replace("Basic ", ""))).split(":");
if (split.length > 1) {
System.out.println(split[0]);
System.out.println(split[1]);
// TODO
}
if (clientRequest.method() == HttpMethod.CONNECT) { if (clientRequest.method() == HttpMethod.CONNECT) {
// 处理 CONNECT 请求 // 处理 CONNECT 请求
@@ -161,7 +129,7 @@ public class HttpProxyVerticle extends AbstractVerticle {
} }
String targetHost = hostHeader.split(":")[0]; String targetHost = hostHeader.split(":")[0];
int targetPort = extractPortFromUrl(clientRequest.uri()); // 默认为 HTTP 的端口 int targetPort = 80; // 默认为 HTTP 的端口
clientRequest.pause(); // 暂停客户端请求的读取,避免数据丢失 clientRequest.pause(); // 暂停客户端请求的读取,避免数据丢失
httpClient.request(clientRequest.method(), targetPort, targetHost, clientRequest.uri()) httpClient.request(clientRequest.method(), targetPort, targetHost, clientRequest.uri())
@@ -172,19 +140,16 @@ public class HttpProxyVerticle extends AbstractVerticle {
clientRequest.headers().forEach(header -> request.putHeader(header.getKey(), header.getValue())); clientRequest.headers().forEach(header -> request.putHeader(header.getKey(), header.getValue()));
// 将客户端请求的 body 转发给目标服务器 // 将客户端请求的 body 转发给目标服务器
clientRequest.bodyHandler(body -> clientRequest.bodyHandler(body -> request.send(body, ar -> {
request.send(body) if (ar.succeeded()) {
.onSuccess(response -> { var response = ar.result();
clientRequest.response().setStatusCode(response.statusCode()); clientRequest.response().setStatusCode(response.statusCode());
clientRequest.response().headers().setAll(response.headers()); clientRequest.response().headers().setAll(response.headers());
response.body() response.body().onSuccess(b-> clientRequest.response().end(b));
.onSuccess(b -> clientRequest.response().end(b)) } else {
.onFailure(err -> clientRequest.response() clientRequest.response().setStatusCode(502).end("Bad Gateway: Unable to reach target");
.setStatusCode(502).end("Bad Gateway: Unable to reach target")); }
}) }));
.onFailure(err -> clientRequest.response()
.setStatusCode(502).end("Bad Gateway: Unable to reach target"))
);
}) })
.onFailure(err -> { .onFailure(err -> {
err.printStackTrace(); err.printStackTrace();
@@ -192,43 +157,28 @@ public class HttpProxyVerticle extends AbstractVerticle {
}); });
} }
/**
* 从 URL 中提取端口号
*
* @param urlString URL 字符串
* @return 提取的端口号,如果没有指定端口,则返回默认端口
*/
public static int extractPortFromUrl(String urlString) {
try {
URI uri = new URI(urlString);
int port = uri.getPort();
// 如果 URL 没有指定端口,使用默认端口
if (port == -1) {
if ("https".equalsIgnoreCase(uri.getScheme())) {
port = 443; // HTTPS 默认端口
} else {
port = 80; // HTTP 默认端口
}
}
return port;
} catch (Exception e) {
e.printStackTrace();
// 出现异常时返回 -1,表示提取失败
return -1;
}
}
@Override @Override
public void stop() { public void stop() {
// 停止 HTTP 客户端以释放资源 // 停止 HTTP 客户端以释放资源
if (httpClient != null) { if (httpClient != null) {
httpClient.close(); httpClient.close();
} }
if (netClient != null) {
netClient.close();
}
} }
/**
* TODO add Deploy
* @param args
*/
public static void main(String[] args) {
// 配置 DNS 解析器,使用多个 DNS 服务器来提升解析速度
Vertx vertx = Vertx.vertx(new VertxOptions()
.setAddressResolverOptions(new AddressResolverOptions()
.addServer("114.114.114.114")
.addServer("114.114.115.115")
.addServer("8.8.8.8")
.addServer("8.8.4.4")));
// 部署 Verticle 并启动动态 HTTP 代理服务器
vertx.deployVerticle(new HttpProxyVerticle());
}
} }
@@ -1,68 +0,0 @@
package cn.qaiu.vx.core.verticle;
import cn.qaiu.vx.core.base.AppRun;
import cn.qaiu.vx.core.base.DefaultAppRun;
import cn.qaiu.vx.core.util.CommonUtil;
import cn.qaiu.vx.core.util.ReflectionUtil;
import cn.qaiu.vx.core.util.SharedDataUtil;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 后置执行Verticle - 在core启动后立即执行AppRun实现
* <br>Create date 2024-01-01 00:00:00
*
* @author <a href="https://qaiu.top">QAIU</a>
*/
public class PostExecVerticle extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(PostExecVerticle.class);
private static final Set<AppRun> appRunImplementations;
private static final AtomicBoolean lock = new AtomicBoolean(false);
static {
Reflections reflections = ReflectionUtil.getReflections();
Set<Class<? extends AppRun>> subTypesOf = reflections.getSubTypesOf(AppRun.class);
subTypesOf.add(DefaultAppRun.class);
appRunImplementations = CommonUtil.sortClassSet(subTypesOf);
if (appRunImplementations.isEmpty()) {
LOGGER.warn("未找到 AppRun 接口的实现类");
} else {
LOGGER.info("找到 {} 个 AppRun 接口的实现类", appRunImplementations.size());
}
}
@Override
public void start(Promise<Void> startPromise) {
if (!lock.compareAndSet(false, true)) {
return;
}
LOGGER.info("PostExecVerticle 开始执行...");
if (appRunImplementations != null && !appRunImplementations.isEmpty()) {
appRunImplementations.forEach(appRun -> {
try {
LOGGER.info("执行 AppRun 实现: {}", appRun.getClass().getName());
JsonObject globalConfig = SharedDataUtil.getJsonConfig("globalConfig");
appRun.execute(globalConfig);
LOGGER.info("AppRun 实现 {} 执行完成", appRun.getClass().getName());
} catch (Exception e) {
LOGGER.error("执行 AppRun 实现 {} 时发生错误",appRun.getClass().getName(), e);
}
});
} else {
LOGGER.info("未找到 AppRun 接口的实现类");
}
LOGGER.info("PostExecVerticle 执行完成");
startPromise.complete();
}
}
@@ -5,10 +5,8 @@ import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.Promise; import io.vertx.core.Promise;
import io.vertx.core.http.HttpClient; import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.core.net.PemKeyCertOptions; import io.vertx.core.net.PemKeyCertOptions;
@@ -17,9 +15,6 @@ import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler; import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.proxy.handler.ProxyHandler; import io.vertx.ext.web.proxy.handler.ProxyHandler;
import io.vertx.httpproxy.HttpProxy; import io.vertx.httpproxy.HttpProxy;
import io.vertx.httpproxy.ProxyContext;
import io.vertx.httpproxy.ProxyInterceptor;
import io.vertx.httpproxy.ProxyResponse;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -27,16 +22,12 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* <p>反向代理服务</p> * <p>反向代理服务</p>
* <p>可以根据配置文件自动生成代理服务</p> * <p>可以根据配置文件自动生成代理服务</p>
* <p>可以配置多个服务, 配置文件见示例</p> * <p>可以配置多个服务, 配置文件见示例</p>
* <p>【优化】支持高并发场景,连接池复用,避免线程阻塞</p>
* <br>Create date 2021/9/2 0:41 * <br>Create date 2021/9/2 0:41
* *
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
@@ -55,83 +46,14 @@ public class ReverseProxyVerticle extends AbstractVerticle {
public static String REROUTE_PATH_PREFIX = "/__rrvpspp"; //re_route_vert_proxy_server_path_prefix 硬编码 public static String REROUTE_PATH_PREFIX = "/__rrvpspp"; //re_route_vert_proxy_server_path_prefix 硬编码
/**
* 【优化】HttpClient连接池,按host:port缓存复用,避免每个请求都创建新连接
*/
private final Map<String, HttpClient> httpClientPool = new ConcurrentHashMap<>();
/**
* 【优化】高并发场景下的HttpClient配置
*/
private static final int MAX_POOL_SIZE = 100; // 最大连接池大小
private static final int MAX_WAIT_QUEUE_SIZE = 500; // 最大等待队列大小
private static final int CONNECT_TIMEOUT = 30000; // 连接超时30秒
private static final int IDLE_TIMEOUT = 60; // 空闲超时60秒
private static final boolean KEEP_ALIVE = true; // 启用Keep-Alive
private static final boolean PIPELINING = true; // 启用HTTP管线化
@Override @Override
public void start(Promise<Void> startPromise) { public void start(Promise<Void> startPromise) {
CONFIG.onSuccess(this::handleProxyConfList).onFailure(e -> { CONFIG.onSuccess(this::handleProxyConfList);
LOGGER.info("web代理配置已禁用,当前仅支持API调用");
});
// createFileListener // createFileListener
startPromise.complete(); startPromise.complete();
} }
/**
* 【优化】Verticle停止时清理HttpClient连接池
*/
@Override
public void stop(Promise<Void> stopPromise) {
LOGGER.info("Stopping ReverseProxyVerticle, closing {} HttpClient connections...", httpClientPool.size());
httpClientPool.values().forEach(client -> {
try {
client.close();
} catch (Exception e) {
LOGGER.warn("Error closing HttpClient: {}", e.getMessage());
}
});
httpClientPool.clear();
stopPromise.complete();
}
/**
* 【优化】获取或创建HttpClient,实现连接池复用
* @param host 目标主机
* @param port 目标端口
* @return HttpClient实例
*/
private HttpClient getOrCreateHttpClient(String host, int port) {
String key = host + ":" + port;
return httpClientPool.computeIfAbsent(key, k -> {
LOGGER.info("Creating new HttpClient for {}", key);
HttpClientOptions options = new HttpClientOptions()
.setMaxPoolSize(MAX_POOL_SIZE) // 连接池大小
.setMaxWaitQueueSize(MAX_WAIT_QUEUE_SIZE) // 等待队列大小
.setConnectTimeout(CONNECT_TIMEOUT) // 连接超时
.setIdleTimeout(IDLE_TIMEOUT) // 空闲超时
.setKeepAlive(KEEP_ALIVE) // Keep-Alive
.setKeepAliveTimeout(120) // Keep-Alive超时120秒
.setPipelining(PIPELINING) // HTTP管线化
.setPipeliningLimit(10) // 管线化限制
.setDecompressionSupported(true) // 支持解压响应
.setTcpKeepAlive(true) // TCP Keep-Alive
.setTcpNoDelay(true) // 禁用Nagle算法,降低延迟
.setTcpFastOpen(true) // 启用TCP Fast Open
.setTcpQuickAck(true) // 启用TCP Quick ACK
.setReuseAddress(true) // 允许地址重用
.setReusePort(true); // 允许端口重用
return vertx.createHttpClient(options);
});
}
/**
* 全局可信上游代理 IP 集合(如 nginx),仅这些 IP 的 X-Forwarded-For 会被信任
*/
private Set<String> globalTrustedProxies = new HashSet<>();
/** /**
* 获取主配置文件 * 获取主配置文件
* *
@@ -139,15 +61,6 @@ public class ReverseProxyVerticle extends AbstractVerticle {
*/ */
private void handleProxyConfList(JsonObject config) { private void handleProxyConfList(JsonObject config) {
serverName = config.getString("server-name"); serverName = config.getString("server-name");
// 解析全局 trusted-proxies
JsonArray trustedArr = config.getJsonArray("trusted-proxies");
if (trustedArr != null) {
trustedArr.forEach(ip -> {
if (ip instanceof String) {
globalTrustedProxies.add(((String) ip).trim());
}
});
}
JsonArray proxyConfList = config.getJsonArray("proxy"); JsonArray proxyConfList = config.getJsonArray("proxy");
if (proxyConfList != null) { if (proxyConfList != null) {
proxyConfList.forEach(proxyConf -> { proxyConfList.forEach(proxyConf -> {
@@ -158,44 +71,6 @@ public class ReverseProxyVerticle extends AbstractVerticle {
} }
} }
/**
* 解析真实客户端 IP。
* 若直连来源在可信代理列表中,优先取 X-Real-IP,其次取 X-Forwarded-For 第一个值;
* 否则直接使用直连对端地址。
*/
private String resolveClientIp(HttpServerRequest request) {
String peerIp = request.remoteAddress().host();
if (globalTrustedProxies.contains(peerIp)) {
String realIp = request.getHeader("X-Real-IP");
if (StringUtils.isNotBlank(realIp)) {
return realIp.trim();
}
String xff = request.getHeader("X-Forwarded-For");
if (StringUtils.isNotBlank(xff)) {
return xff.split(",")[0].trim();
}
}
return peerIp;
}
/**
* 解析 proxy-set-headers 中的 nginx 风格变量。
* 支持:$remote_addr、$proxy_add_x_forwarded_for、$scheme、$host;
* 其他值作为字面量直接使用。
*/
private String resolveHeaderVariable(String tpl, HttpServerRequest req, String clientIp) {
return switch (tpl) {
case "$remote_addr" -> clientIp;
case "$proxy_add_x_forwarded_for" -> {
String existing = req.getHeader("X-Forwarded-For");
yield StringUtils.isNotBlank(existing) ? existing + ", " + clientIp : clientIp;
}
case "$scheme" -> req.isSSL() ? "https" : "http";
case "$host" -> req.getHeader("Host");
default -> tpl;
};
}
/** /**
* 处理单个反向代理配置 * 处理单个反向代理配置
* *
@@ -222,25 +97,18 @@ public class ReverseProxyVerticle extends AbstractVerticle {
proxyConf.put("page404", DEFAULT_PATH_404); proxyConf.put("page404", DEFAULT_PATH_404);
} }
final HttpClient httpClient = VertxHolder.getVertxInstance().createHttpClient();
Router proxyRouter = Router.router(vertx); Router proxyRouter = Router.router(vertx);
// Add Server name header // Add Server name header
proxyRouter.route().handler(ctx -> { proxyRouter.route().handler(ctx -> {
String realPath = ctx.request().uri();
if (realPath.startsWith(REROUTE_PATH_PREFIX)) {
// vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath
String rePath = realPath.replace(REROUTE_PATH_PREFIX, "");
ctx.reroute(rePath);
return;
}
ctx.response().putHeader("Server", serverName); ctx.response().putHeader("Server", serverName);
ctx.next(); ctx.next();
}); });
// http api proxy // http api proxy
if (proxyConf.containsKey("location")) { if (proxyConf.containsKey("location")) {
handleLocation(proxyConf.getJsonArray("location"), proxyRouter); handleLocation(proxyConf.getJsonArray("location"), httpClient, proxyRouter);
} }
// static server // static server
@@ -249,9 +117,7 @@ public class ReverseProxyVerticle extends AbstractVerticle {
} }
// Send page404 page // Send page404 page
proxyRouter.errorHandler(404, ctx -> { proxyRouter.errorHandler(404, ctx -> ctx.response().sendFile(proxyConf.getString("page404")));
ctx.response().sendFile(proxyConf.getString("page404"));
});
HttpServer server = getHttpsServer(proxyConf); HttpServer server = getHttpsServer(proxyConf);
server.requestHandler(proxyRouter); server.requestHandler(proxyRouter);
@@ -263,16 +129,8 @@ public class ReverseProxyVerticle extends AbstractVerticle {
private HttpServer getHttpsServer(JsonObject proxyConf) { private HttpServer getHttpsServer(JsonObject proxyConf) {
HttpServerOptions httpServerOptions = new HttpServerOptions() HttpServerOptions httpServerOptions = new HttpServerOptions()
// 【优化】高并发服务器配置 .setCompressionSupported(true);
.setTcpKeepAlive(true) // TCP Keep-Alive
.setTcpNoDelay(true) // 禁用Nagle算法
.setCompressionSupported(true) // 启用压缩
.setAcceptBacklog(50000) // 增加积压队列到50000
.setIdleTimeout(120) // 空闲超时120秒
.setTcpFastOpen(true) // 启用TCP Fast Open
.setTcpQuickAck(true) // 启用TCP Quick ACK
.setReuseAddress(true) // 允许地址重用
.setReusePort(true); // 允许端口重用
if (proxyConf.containsKey("ssl")) { if (proxyConf.containsKey("ssl")) {
JsonObject sslConfig = proxyConf.getJsonObject("ssl"); JsonObject sslConfig = proxyConf.getJsonObject("ssl");
@@ -326,6 +184,7 @@ public class ReverseProxyVerticle extends AbstractVerticle {
} else { } else {
staticHandler = StaticHandler.create(); staticHandler = StaticHandler.create();
} }
if (staticConf.containsKey("directory-listing")) { if (staticConf.containsKey("directory-listing")) {
staticHandler.setDirectoryListing(staticConf.getBoolean("directory-listing")); staticHandler.setDirectoryListing(staticConf.getBoolean("directory-listing"));
} else if (staticConf.containsKey("index")) { } else if (staticConf.containsKey("index")) {
@@ -338,9 +197,10 @@ public class ReverseProxyVerticle extends AbstractVerticle {
* 处理Location配置 代理请求Location(和nginx类似?) * 处理Location配置 代理请求Location(和nginx类似?)
* *
* @param locationsConf location配置 * @param locationsConf location配置
* @param httpClient 客户端
* @param proxyRouter 代理路由 * @param proxyRouter 代理路由
*/ */
private void handleLocation(JsonArray locationsConf, Router proxyRouter) { private void handleLocation(JsonArray locationsConf, HttpClient httpClient, Router proxyRouter) {
locationsConf.stream().map(e -> (JsonObject) e).forEach(location -> { locationsConf.stream().map(e -> (JsonObject) e).forEach(location -> {
// 代理规则 // 代理规则
@@ -356,33 +216,9 @@ public class ReverseProxyVerticle extends AbstractVerticle {
String originPath = url.getPath(); String originPath = url.getPath();
LOGGER.info("path {}, originPath {}, to {}:{}", path, originPath, host, port); LOGGER.info("path {}, originPath {}, to {}:{}", path, originPath, host, port);
// 【优化】使用连接池获取HttpClient,避免每个location都创建新连接 // 注意这里不能origin多个代理地址, 一个实例只能代理一个origin
final HttpClient httpClient = getOrCreateHttpClient(host, port);
final HttpProxy httpProxy = HttpProxy.reverseProxy(httpClient); final HttpProxy httpProxy = HttpProxy.reverseProxy(httpClient);
httpProxy.origin(port, host); httpProxy.origin(port, host);
// proxy-set-headers 支持(nginx 风格变量替换)
if (location.containsKey("proxy-set-headers")) {
final JsonObject headerConf = location.getJsonObject("proxy-set-headers");
httpProxy.addInterceptor(new ProxyInterceptor() {
@Override
public Future<ProxyResponse> handleProxyRequest(ProxyContext ctx) {
HttpServerRequest incoming = ctx.request().proxiedRequest();
String clientIp = resolveClientIp(incoming);
headerConf.forEach(entry -> {
Object val = entry.getValue();
if (val != null) {
String resolved = resolveHeaderVariable(val.toString(), incoming, clientIp);
if (resolved != null) {
ctx.request().putHeader(entry.getKey(), resolved);
}
}
});
return ProxyInterceptor.super.handleProxyRequest(ctx);
}
});
}
if (StringUtils.isEmpty(path)) { if (StringUtils.isEmpty(path)) {
return; return;
} }
@@ -391,65 +227,24 @@ public class ReverseProxyVerticle extends AbstractVerticle {
if (StringUtils.isEmpty(originPath) || path.equals(originPath)) { if (StringUtils.isEmpty(originPath) || path.equals(originPath)) {
Route route = path.startsWith("~") ? proxyRouter.routeWithRegex(path.substring(1)) Route route = path.startsWith("~") ? proxyRouter.routeWithRegex(path.substring(1))
: proxyRouter.route(path); : proxyRouter.route(path);
// 【优化】为代理处理器添加超时
route.handler(ProxyHandler.create(httpProxy)); route.handler(ProxyHandler.create(httpProxy));
} else { } else {
// 配置 /api/, / => 请求 /api/test 代理后 /test // 配置 /api/, / => 请求 /api/test 代理后 /test
// 配置 /api/, /xxx => 请求 /api/test 代理后 /xxx/test // 配置 /api/, /xxx => 请求 /api/test 代理后 /xxx/test
final String path0 = path; final String path0 = path;
final String originPath0 = REROUTE_PATH_PREFIX + originPath; final String originPath0 = REROUTE_PATH_PREFIX + originPath;
proxyRouter.route(originPath0 + "*").handler(ProxyHandler.create(httpProxy)); proxyRouter.route(originPath0 + "*").handler(ProxyHandler.create(httpProxy));
proxyRouter.route(path0 + "*").handler(ctx -> { proxyRouter.route(path0 + "*").handler(ctx -> {
String realPath = ctx.request().uri(); String realPath = ctx.request().uri();
if (realPath.startsWith(path0)) { if (realPath.startsWith(path0)) {
// vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath // vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath
String rePath = realPath.replaceAll("^" + path0, originPath0); String rePath = realPath.replaceAll("^" + path0, originPath0);
ctx.reroute(rePath); ctx.reroute(rePath);
} else { } else {
ctx.next(); ctx.next();
} }
}); });
// 计算唯一后缀,避免多个 location 冲突
// String uniqueKey = (host + ":" + port + "|" + path).replaceAll("[^a-zA-Z0-9:_|/]", "");
// String uniqueSuffix = Integer.toHexString(uniqueKey.hashCode());
//
//// 规格化 originPath
// //String originPath = url.getPath(); // 原值
// if (StringUtils.isBlank(originPath)) originPath = "/";
//
//// 处理 index.html 的情况:用于首页兜底,其它子路径仍按目录穿透
// String indexFile;
// if (originPath.endsWith(".html")) {
// indexFile = originPath; // 例如 /index.html
// originPath = "/"; // 目录穿透基准改为根
// } else {
// indexFile = null;
// }
//
//// 唯一内部挂载前缀
// final String originMount = REROUTE_PATH_PREFIX + uniqueSuffix + originPath;
//
//// 1) 目标挂载:所有被重写的请求最终到这里走 ProxyHandler
// proxyRouter.route(originMount + "*").handler(ProxyHandler.create(httpProxy));
//
//// 2) 从外部前缀 -> 内部挂载 的重写
// final String path0 = path;
// proxyRouter.route(path0 + "*").handler(ctx -> {
// String uri = ctx.request().uri();
// if (!uri.startsWith(path0)) { ctx.next(); return; }
//
// // 首页兜底:访问 /n2 或 /n2/ 时,重写到 index.html(如果配置了)
// if (indexFile != null && (uri.equals(path0) || uri.equals(path0.substring(0, path0.length()-1)))) {
// String rePath = originMount.endsWith("/") ? (originMount + indexFile.substring(1)) : (originMount + indexFile);
// ctx.reroute(rePath);
// return;
// }
//
// // 一般穿透:/n2/xxx -> originMount + xxx
// String rePath = uri.replaceFirst("^" + path0, originMount);
// ctx.reroute(rePath);
// });
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
@@ -49,18 +49,6 @@ public class RouterVerticle extends AbstractVerticle {
options = new HttpServerOptions(); options = new HttpServerOptions();
} }
options.setPort(port); options.setPort(port);
// 【优化】高并发服务器配置
options.setTcpKeepAlive(true) // TCP Keep-Alive
.setTcpNoDelay(true) // 禁用Nagle算法,降低延迟
.setCompressionSupported(true) // 启用压缩
.setAcceptBacklog(50000) // 增加积压队列到50000,防止高并发时连接被拒绝
.setIdleTimeout(120) // 空闲超时120秒
.setTcpFastOpen(true) // 启用TCP Fast Open
.setTcpQuickAck(true) // 启用TCP Quick ACK
.setReuseAddress(true) // 允许地址重用
.setReusePort(true); // 允许端口重用
server = vertx.createHttpServer(options); server = vertx.createHttpServer(options);
server.requestHandler(router).webSocketHandler(s->{}).listen() server.requestHandler(router).webSocketHandler(s->{}).listen()
@@ -29,23 +29,20 @@ public class ServiceVerticle extends AbstractVerticle {
Reflections reflections = ReflectionUtil.getReflections(); Reflections reflections = ReflectionUtil.getReflections();
handlers = reflections.getTypesAnnotatedWith(Service.class); handlers = reflections.getTypesAnnotatedWith(Service.class);
} }
@Override @Override
public void start(Promise<Void> startPromise) { public void start(Promise<Void> startPromise) {
ServiceBinder binder = new ServiceBinder(vertx); ServiceBinder binder = new ServiceBinder(vertx);
if (null != handlers && handlers.size() > 0) { if (null != handlers && handlers.size() > 0) {
// handlers转为拼接类列表,xxx,yyy,zzz
StringBuilder serviceNames = new StringBuilder();
handlers.forEach(asyncService -> { handlers.forEach(asyncService -> {
try { try {
serviceNames.append(asyncService.getName()).append("|");
BaseAsyncService asInstance = (BaseAsyncService) ReflectionUtil.newWithNoParam(asyncService); BaseAsyncService asInstance = (BaseAsyncService) ReflectionUtil.newWithNoParam(asyncService);
binder.setAddress(asInstance.getAddress()).register(asInstance.getAsyncInterfaceClass(), asInstance); binder.setAddress(asInstance.getAddress()).register(asInstance.getAsyncInterfaceClass(), asInstance);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Failed to register service: {}", asyncService.getName(), e); LOGGER.error(e.getMessage());
} }
}); });
LOGGER.info("registered async services -> id: {}", ID.getAndIncrement());
LOGGER.info("registered async services -> id: {}, name: {}", ID.getAndIncrement(), serviceNames.toString());
} }
startPromise.complete(); startPromise.complete();
} }
@@ -8,14 +8,14 @@ import io.vertx.core.net.ProxyOptions;
import java.util.UUID; import java.util.UUID;
@DataObject @DataObject
//@JsonGen(publicConverter = false) @JsonGen(publicConverter = false)
public class HttpProxyConf { public class HttpProxyConf {
public static final String DEFAULT_USERNAME = UUID.randomUUID().toString(); public static final String DEFAULT_USERNAME = UUID.randomUUID().toString();
public static final String DEFAULT_PASSWORD = UUID.randomUUID().toString(); public static final String DEFAULT_PASSWORD = UUID.randomUUID().toString();
public static final Integer DEFAULT_PORT = 6432; public static final Integer DEFAULT_PORT = 6402;
public static final Integer DEFAULT_TIMEOUT = 15000; public static final Integer DEFAULT_TIMEOUT = 15000;
@@ -1,134 +0,0 @@
package cn.qaiu.vx.core.test;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.junit.Assert;
import org.junit.Test;
/**
* 单元测试:验证 RouterHandlerFactory 关于 JsonObject/JsonArray 参数绑定的核心分支逻辑是否正确
* (不启动整个 Vert.x 服务器,直接用 Vert.x JsonObject/JsonArray API 模拟验证关键逻辑)
*/
public class JsonBodyBindingLogicTest {
// === 模拟 handlerMethod 中的 JSON body 绑定逻辑 ===
/**
* 模拟:content-type = application/json,body 是 JsonObject
* 期望:JsonObject 类型参数被正确绑定
*/
@Test
public void testJsonObjectBinding() {
String bodyStr = "{\"name\":\"test\",\"value\":123}";
// 模拟 ctx.body().asJsonObject()
JsonObject body = parseAsJsonObject(bodyStr);
Assert.assertNotNull("body 应能解析为 JsonObject", body);
// 模拟绑定逻辑中的类型判断
String targetType = JsonObject.class.getName();
boolean matched = JsonObject.class.getName().equals(targetType);
Assert.assertTrue("JsonObject 类型应命中绑定分支", matched);
// 模拟结果
Object bound = body; // parameterValueList.put(k, body)
Assert.assertNotNull("JsonObject 参数应被绑定(非null)", bound);
Assert.assertEquals("name字段应为test", "test", ((JsonObject) bound).getString("name"));
Assert.assertEquals("value字段应为123", 123, (int) ((JsonObject) bound).getInteger("value"));
System.out.println("[PASS] testJsonObjectBinding: JsonObject 绑定成功 -> " + bound);
}
/**
* 模拟:content-type = application/json,body 是 JsonArray
* 期望:JsonArray 类型参数被正确绑定
*/
@Test
public void testJsonArrayBinding() {
String bodyStr = "[1,2,3]";
// body 解析为 JsonObject 应返回 null
JsonObject bodyAsObj = parseAsJsonObject(bodyStr);
Assert.assertNull("JsonArray body 解析为 JsonObject 应为 null", bodyAsObj);
// 进入 else 分支,解析为 JsonArray
JsonArray bodyArr = parseAsJsonArray(bodyStr);
Assert.assertNotNull("body 应能解析为 JsonArray", bodyArr);
String targetType = JsonArray.class.getName();
boolean matched = JsonArray.class.getName().equals(targetType);
Assert.assertTrue("JsonArray 类型应命中绑定分支", matched);
Object bound = bodyArr;
Assert.assertNotNull("JsonArray 参数应被绑定(非null)", bound);
Assert.assertEquals("数组大小应为3", 3, ((JsonArray) bound).size());
System.out.println("[PASS] testJsonArrayBinding: JsonArray 绑定成功, size=" + ((JsonArray) bound).size());
}
/**
* 验证旧代码的 bug:条件 ctx.body().asJsonObject() != null 会把 JsonArray body 排除在外
* 新代码只判断 content-type,在 body==null 时才进 else 分支处理 JsonArray
*/
@Test
public void testOldConditionBug() {
String jsonArrayBody = "[1,2,3]";
// 旧代码条件:content-type==json && asJsonObject()!=null
// 对于 JsonArray body,asJsonObject() 返回 null,整个 if 跳过
JsonObject wrongParsed = parseAsJsonObject(jsonArrayBody);
boolean oldConditionPassed = wrongParsed != null; // 旧代码的第二个条件
Assert.assertFalse("旧代码 bug: JsonArray body 会导致 asJsonObject()==null,整个分支跳过", oldConditionPassed);
// 新代码:先进 if,body==null 再走 else 解析 JsonArray
boolean newConditionFirst = true; // content-type 匹配
JsonObject newBody = parseAsJsonObject(jsonArrayBody);
boolean newBodyIsNull = newBody == null; // null -> 进 else
Assert.assertTrue("新代码: body 解析为 null 时应走 else 分支解析 JsonArray", newBodyIsNull);
JsonArray newArr = parseAsJsonArray(jsonArrayBody);
Assert.assertNotNull("新代码: else 分支正确解析出 JsonArray", newArr);
System.out.println("[PASS] testOldConditionBug: 修复验证通过,新代码正确处理 JsonArray body");
}
/**
* 验证:JsonObject 参数旧代码没有绑定分支(只处理实体类)
*/
@Test
public void testOldMissingJsonObjectBranch() {
String bodyStr = "{\"key\":\"value\"}";
JsonObject body = parseAsJsonObject(bodyStr);
// 旧代码只调用 matchRegList(entityPackagesReg, typeName)
// 对于 io.vertx.core.json.JsonObject,该方法返回 false,不会被绑定
String typeName = JsonObject.class.getName(); // "io.vertx.core.json.JsonObject"
// entityPackagesReg 一般是 "cn.qaiu.*" 这类,不会匹配 io.vertx
boolean oldWouldBind = typeName.startsWith("cn.qaiu"); // 模拟旧代码逻辑
Assert.assertFalse("旧代码 bug: JsonObject 参数不会被绑定", oldWouldBind);
// 新代码:增加了 JsonObject 类型判断
boolean newWouldBind = JsonObject.class.getName().equals(typeName);
Assert.assertTrue("新代码: JsonObject 参数应能被绑定", newWouldBind);
System.out.println("[PASS] testOldMissingJsonObjectBranch: 修复验证通过");
}
// ===== 辅助方法:模拟 Vert.x RequestBody 的 asJsonObject/asJsonArray 行为 =====
private JsonObject parseAsJsonObject(String str) {
try {
return new JsonObject(str);
} catch (Exception e) {
return null;
}
}
private JsonArray parseAsJsonArray(String str) {
try {
return new JsonArray(str);
} catch (Exception e) {
return null;
}
}
}
@@ -1,125 +0,0 @@
package cn.qaiu.vx.core.test;
import cn.qaiu.vx.core.util.VertxHolder;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* 集成测试: 验证 RouterHandlerFactory 对 JsonObject/JsonArray 参数绑定逻辑是否正确
*
* 运行方式: mvn test-compile -pl core && java -cp "core/target/test-classes:core/target/classes:..." \
* cn.qaiu.vx.core.test.RouterHandlerBindingTest
*
* 或直接在 IDE 中运行 main 方法。
*/
public class RouterHandlerBindingTest {
static final int TEST_PORT = 18989;
public static void main(String[] args) throws Exception {
System.out.println("=== RouterHandler JsonObject/JsonArray 绑定测试 ===\n");
// 1. 先初始化 Vert.x 与 VertxHolder ——必须在加载 RouterHandlerFactory 之前
Vertx vertx = Vertx.vertx();
VertxHolder.init(vertx);
// 2. 向 SharedData 注入最小化配置
// baseLocations 指向测试包,使 Reflections 只扫描 TestJsonHandler
vertx.sharedData().getLocalMap("local").put("customConfig", new JsonObject()
.put("baseLocations", "cn.qaiu.vx.core.test")
.put("routeTimeOut", 30000)
.put("entityPackagesReg", new JsonArray()));
// ReverseProxyVerticle.<clinit> 需要 globalConfig.proxyConf(非空字符串即可)
vertx.sharedData().getLocalMap("local").put("globalConfig", new JsonObject()
.put("proxyConf", "proxy.yml"));
// 3. 创建 Router(此时才触发 BaseHttpApi.reflections 静态字段初始化)
// 用反射延迟加载,确保上面的 SharedData 已就绪
cn.qaiu.vx.core.handlerfactory.RouterHandlerFactory factory =
new cn.qaiu.vx.core.handlerfactory.RouterHandlerFactory("api");
io.vertx.ext.web.Router router = factory.createRouter();
// 4. 启动 HTTP 服务器
CountDownLatch latch = new CountDownLatch(1);
vertx.createHttpServer()
.requestHandler(router)
.listen(TEST_PORT, res -> {
if (res.succeeded()) {
System.out.println("✔ 测试服务器启动成功 port=" + TEST_PORT);
} else {
System.err.println("✘ 服务器启动失败: " + res.cause().getMessage());
}
latch.countDown();
});
if (!latch.await(5, TimeUnit.SECONDS)) {
System.err.println("服务器启动超时");
vertx.close();
System.exit(1);
}
Thread.sleep(100); // 等 Vert.x 就绪
// 5. 执行测试
boolean allPassed = true;
allPassed &= testJsonObject();
allPassed &= testJsonArray();
// 6. 关闭
CountDownLatch closeLatch = new CountDownLatch(1);
vertx.close(v -> closeLatch.countDown());
closeLatch.await(3, TimeUnit.SECONDS);
System.out.println("\n" + (allPassed ? "✅ 全部测试通过!" : "❌ 存在测试失败!"));
System.exit(allPassed ? 0 : 1);
}
// ---------- 子测试 ----------
private static boolean testJsonObject() throws Exception {
String bodyStr = "{\"name\":\"test\",\"value\":123}";
String respBody = post("/api/test/json-object", bodyStr);
System.out.println("[JsonObject] 响应: " + respBody);
JsonObject result = new JsonObject(respBody);
JsonObject data = result.getJsonObject("data");
boolean bound = data != null && Boolean.TRUE.equals(data.getBoolean("bound"));
System.out.println("[JsonObject] " + (bound
? "PASS ✅ body 正确绑定为 JsonObject"
: "FAIL ❌ body 未绑定 (null)"));
return bound;
}
private static boolean testJsonArray() throws Exception {
String bodyStr = "[1,2,3]";
String respBody = post("/api/test/json-array", bodyStr);
System.out.println("[JsonArray] 响应: " + respBody);
JsonObject result = new JsonObject(respBody);
JsonObject data = result.getJsonObject("data");
boolean bound = data != null
&& Boolean.TRUE.equals(data.getBoolean("bound"))
&& Integer.valueOf(3).equals(data.getInteger("size"));
System.out.println("[JsonArray] " + (bound
? "PASS ✅ body 正确绑定为 JsonArray, size=3"
: "FAIL ❌ body 未绑定 或 size 不对"));
return bound;
}
private static String post(String path, String body) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + TEST_PORT + path))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
return client.send(req, HttpResponse.BodyHandlers.ofString()).body();
}
}
@@ -1,36 +0,0 @@
package cn.qaiu.vx.core.test;
import cn.qaiu.vx.core.annotaions.RouteHandler;
import cn.qaiu.vx.core.annotaions.RouteMapping;
import cn.qaiu.vx.core.enums.MIMEType;
import cn.qaiu.vx.core.enums.RouteMethod;
import cn.qaiu.vx.core.model.JsonResult;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
/**
* 用于测试 RouterHandlerFactory 对 JsonObject/JsonArray 参数绑定的测试 Handler
*/
@RouteHandler("test")
public class TestJsonHandler {
/** POST /api/test/json-object Body: {"name":"test","value":123} */
@RouteMapping(value = "/json-object", method = RouteMethod.POST, requestMIMEType = MIMEType.APPLICATION_JSON)
public Future<JsonResult> testJsonObject(JsonObject body) {
// 只返回是否绑定成功及已知字段值,不嵌套原始 body 避免 toJsonObject() 循环
boolean bound = body != null;
String nameVal = bound ? body.getString("name", "") : "";
return Future.succeededFuture(JsonResult.data(new io.vertx.core.json.JsonObject()
.put("bound", bound)
.put("name", nameVal)));
}
/** POST /api/test/json-array Body: [1,2,3] */
@RouteMapping(value = "/json-array", method = RouteMethod.POST, requestMIMEType = MIMEType.APPLICATION_JSON)
public Future<JsonResult> testJsonArray(JsonArray body) {
return Future.succeededFuture(JsonResult.data(new io.vertx.core.json.JsonObject()
.put("bound", body != null)
.put("size", body != null ? body.size() : -1)));
}
}
-5
View File
@@ -1,5 +0,0 @@
{
"dependencies": {
"mvn": "^3.5.0"
}
}
-26
View File
@@ -20,32 +20,6 @@
|------|------|------|------| |------|------|------|------|
| url | string | ✅ 是 | 分享链接(需URL编码) | | url | string | ✅ 是 | 分享链接(需URL编码) |
| pwd | string | ❌ 否 | 分享密码 | | pwd | string | ❌ 否 | 分享密码 |
| auth | string | ❌ 否 | 认证参数(AES加密后的JSON,用于需要登录的网盘) |
### 认证参数说明(v0.2.1+)
部分网盘(如夸克QK、UC网盘)需要登录后的 Cookie 才能解析。`auth` 参数用于传递认证信息:
**加密方式**:
- 算法:AES/ECB/PKCS5Padding
- 密钥:`nfd_auth_key2026`(16字节)
- 流程:JSON → AES加密 → Base64 → URL编码
**JSON 结构**:
```json
{
"authType": "cookie", // 认证类型: cookie/accesstoken/authorization
"token": "your_cookie_here" // Cookie 或 Token 内容
}
```
**网盘认证要求**:
| 网盘 | 认证要求 |
|------|---------|
| 夸克网盘(QK) | **必须** |
| UC网盘(UC) | **必须** |
| 小飞机网盘(FJ) | 大文件需要 |
| 蓝奏优享(IZ) | 大文件需要 |
### 请求示例 ### 请求示例
-341
View File
@@ -1,341 +0,0 @@
# 认证参数传递指南 (Auth Parameter Guide)
## 概述
本文档描述了网盘解析接口中携带认证参数的方法。通过 `auth` 参数,可以在解析请求时传递临时认证信息(如 Cookie、Token、用户名密码等),使解析器能够访问需要登录或授权的网盘资源。
## 网盘认证要求
| 网盘 | 类型代码 | 认证要求 | 说明 |
|------|---------|---------|------|
| 夸克网盘 | QK | **必须** | 必须配置 Cookie 才能解析和下载 |
| UC网盘 | UC | **必须** | 必须配置 Cookie 才能解析和下载 |
| 小飞机网盘 | FJ | 可选 | 大文件(>100MB)需要配置认证信息 |
| 蓝奏优享 | IZ | 可选 | 大文件需要配置认证信息 |
| 其他网盘 | - | 不需要 | 无需认证即可解析 |
> 💡 **如何获取 Cookie**: 在浏览器中登录对应网盘,打开开发者工具(F12),切换到 Network 标签,刷新页面,在请求头中找到 Cookie 字段并复制完整内容。
## 认证参数格式
### 编码流程
```
JSON对象 → AES加密 → Base64编码 → URL编码
```
### 解码流程
```
URL解码 → Base64解码 → AES解密 → JSON对象
```
### 加密配置
- **加密算法**: AES/ECB/PKCS5Padding
- **密钥长度**: 16位(128位)
- **默认密钥**: `nfd_auth_key2026`(可在 `app-dev.yml` 中通过 `server.authEncryptKey` 配置)
### 密钥作用说明(重要)
当前系统中涉及两类不同用途的密钥:
1. `server.authEncryptKey`
- 用途:加解密 `auth` 参数(前端/调用方传入的认证信息)
- 影响范围:`/parser`、`/json/parser`、`/v2/linkInfo` 等接口中的 `auth` 参数
- 注意:这是 **AES 对称加密密钥**,要求 16 位
2. `server.donatedAccountFailureTokenSignKey`
- 用途:签名和验签“捐赠账号失败计数 token”(用于防伪造、失败计数)
- 影响范围:捐赠账号失败计数与自动失效逻辑
- 注意:这是 **HMAC 签名密钥**,与 `authEncryptKey` 已解耦,建议使用高强度随机字符串
> 建议:生产环境务必同时自定义这两个密钥,且不要设置为相同值。
## JSON 模型定义
### AuthParam 对象
```json
{
"authType": "string", // 认证类型(必填)
"username": "string", // 用户名
"password": "string", // 密码
"token": "string", // Token/AccessToken/Cookie值
"cookie": "string", // Cookie 字符串
"auth": "string", // Authorization 头内容
"ext1": "string", // 扩展字段1(格式: key:value)
"ext2": "string", // 扩展字段2(格式: key:value)
"ext3": "string", // 扩展字段3(格式: key:value)
"ext4": "string", // 扩展字段4(格式: key:value)
"ext5": "string" // 扩展字段5(格式: key:value)
}
```
### 认证类型 (authType)
| authType | 说明 | 主要字段 |
|----------|------|---------|
| `accesstoken` | 使用 AccessToken 认证 | `token` |
| `cookie` | 使用 Cookie 认证 | `token` (存放 cookie 值) |
| `authorization` | 使用 Authorization 头认证 | `token` |
| `password` / `username_password` | 用户名密码认证 | `username`, `password` |
| `custom` | 自定义认证(使用扩展字段) | `token`, `ext1`-`ext5` |
### 示例 JSON
#### 1. Token 认证
```json
{
"authType": "accesstoken",
"token": "your_access_token_here"
}
```
#### 2. Cookie 认证
```json
{
"authType": "cookie",
"token": "session_id=abc123; user_token=xyz789"
}
```
#### 3. 用户名密码认证
```json
{
"authType": "password",
"username": "your_username",
"password": "your_password"
}
```
#### 4. 自定义认证
```json
{
"authType": "custom",
"token": "main_token",
"ext1": "refresh_token:your_refresh_token",
"ext2": "device_id:device123"
}
```
## 接口调用示例
### 基础接口
#### 1. 解析并重定向 (GET /parser)
```
GET /parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
```
**参数说明:**
- `url`: 网盘分享链接(必填)
- `pwd`: 提取码(可选)
- `auth`: 加密后的认证参数(可选)
**响应:** 302 重定向到直链
#### 2. 解析返回 JSON (GET /json/parser)
```
GET /json/parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
```
**响应示例:**
```json
{
"shareKey": "lz:xxxx",
"directLink": "https://...",
"cacheHit": false,
"expires": "2026-02-05 12:00:00",
"expiration": 1738728000000
}
```
#### 3. 获取链接信息 (GET /v2/linkInfo)
```
GET /v2/linkInfo?url={分享链接}&pwd={提取码}&auth={加密认证参数}
```
**响应:** 返回下载链接、API 链接、预览链接等信息
## 各语言加密示例
### Java
```java
import cn.qaiu.lz.common.util.AuthParamCodec;
import cn.qaiu.lz.web.model.AuthParam;
// 方式1: 使用 AuthParam 对象
AuthParam authParam = AuthParam.builder()
.authType("accesstoken")
.token("your_token_here")
.build();
String encrypted = AuthParamCodec.encode(authParam);
// 方式2: 快速编码
String encrypted = AuthParamCodec.quickEncode("accesstoken", "your_token_here");
// 方式3: 用户名密码
String encrypted = AuthParamCodec.quickEncodePassword("username", "password");
// 解码
AuthParam decoded = AuthParamCodec.decode(encrypted);
```
### JavaScript (浏览器/Node.js)
```javascript
// 使用 CryptoJS 库
const CryptoJS = require('crypto-js');
const AUTH_KEY = 'nfd_auth_key2026';
// 加密
function encodeAuthParam(authObj) {
const jsonStr = JSON.stringify(authObj);
const encrypted = CryptoJS.AES.encrypt(jsonStr, CryptoJS.enc.Utf8.parse(AUTH_KEY), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
const base64 = encrypted.toString();
return encodeURIComponent(base64);
}
// 解密
function decodeAuthParam(encryptedAuth) {
const base64 = decodeURIComponent(encryptedAuth);
const decrypted = CryptoJS.AES.decrypt(base64, CryptoJS.enc.Utf8.parse(AUTH_KEY), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));
}
// 使用示例
const auth = encodeAuthParam({
authType: 'accesstoken',
token: 'your_token_here'
});
const url = `http://127.0.0.1:6400/parser?url=${shareUrl}&auth=${auth}`;
```
### Python
```python
import json
import base64
from urllib.parse import quote, unquote
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
AUTH_KEY = b'nfd_auth_key2026'
def encode_auth_param(auth_obj):
"""加密认证参数"""
json_str = json.dumps(auth_obj, ensure_ascii=False)
cipher = AES.new(AUTH_KEY, AES.MODE_ECB)
padded = pad(json_str.encode('utf-8'), AES.block_size)
encrypted = cipher.encrypt(padded)
base64_str = base64.b64encode(encrypted).decode('utf-8')
return quote(base64_str)
def decode_auth_param(encrypted_auth):
"""解密认证参数"""
base64_str = unquote(encrypted_auth)
encrypted = base64.b64decode(base64_str)
cipher = AES.new(AUTH_KEY, AES.MODE_ECB)
decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
return json.loads(decrypted.decode('utf-8'))
# 使用示例
auth = encode_auth_param({
'authType': 'accesstoken',
'token': 'your_token_here'
})
url = f'http://127.0.0.1:6400/parser?url={share_url}&auth={auth}'
```
### cURL 命令行
```bash
# 假设已加密的 auth 参数为 ENCRYPTED_AUTH
curl -L "http://127.0.0.1:6400/parser?url=https://www.lanzoux.com/xxxx&auth=ENCRYPTED_AUTH"
# 获取 JSON 响应
curl "http://127.0.0.1:6400/json/parser?url=https://www.lanzoux.com/xxxx&auth=ENCRYPTED_AUTH"
```
## 解析器使用认证信息
解析器可以从 `shareLinkInfo.otherParam.get("auths")` 获取 MultiMap 格式的认证信息:
```java
// 在解析器中获取认证信息
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
if (auths != null) {
String authType = auths.get("authType");
String token = auths.get("token");
String username = auths.get("username");
String password = auths.get("password");
// 根据 authType 使用相应的认证方式
switch (authType) {
case "accesstoken":
// 使用 token 认证
break;
case "password":
// 使用用户名密码登录
break;
// ...
}
}
```
## 注意事项
1. **安全性**:
- 不要在日志中打印完整的认证参数
- 认证参数通过 HTTPS 传输更安全
- 密钥应妥善保管,建议在生产环境中更换默认密钥
2. **缓存策略**:
- 带有临时认证参数的请求目前不会被缓存
- 每次请求都会重新解析
3. **兼容性**:
- `auth` 参数与原有的 `pwd` 参数可以同时使用
- 不提供 `auth` 参数时,使用后台配置的认证信息
4. **扩展字段**:
- `ext1`-`ext5` 使用 `key:value` 格式
- 适用于需要传递多个自定义参数的场景
## 配置说明
在 `app-dev.yml` 中配置密钥:
```yaml
server:
# auth参数加密密钥(16位AES密钥)
authEncryptKey: 'your_custom_key16'
# 捐赠账号失败计数token签名密钥(HMAC)
# 建议使用较长随机字符串,并与 authEncryptKey 不同
donatedAccountFailureTokenSignKey: 'your_random_hmac_sign_key'
```
### 密钥管理建议
- 不要在公开仓库提交生产密钥
- 建议通过环境变量或私有配置注入
- 调整 `authEncryptKey` 会影响 `auth` 参数兼容性
- 调整 `donatedAccountFailureTokenSignKey` 会使已签发的失败计数 token 失效(短期可接受)
## 更新日志
- **2026-02-05**: 初始版本,支持 accesstoken、cookie、password、custom 认证类型
@@ -1,118 +0,0 @@
# 认证参数传递指南 (简化版)
## JSON 对象模型
### AuthParam 对象
```json
{
"authType": "string", // 认证类型(必填)
"username": "string", // 用户名
"password": "string", // 密码
"token": "string", // Token/AccessToken/Cookie值
"cookie": "string", // Cookie 字符串
"auth": "string", // Authorization 头内容
"ext1": "string", // 扩展字段1(格式: key:value)
"ext2": "string", // 扩展字段2(格式: key:value)
"ext3": "string", // 扩展字段3(格式: key:value)
"ext4": "string", // 扩展字段4(格式: key:value)
"ext5": "string" // 扩展字段5(格式: key:value)
}
```
## 认证类型
| authType | 说明 | 主要字段 |
|----------|------|---------|
| `accesstoken` | AccessToken 认证 | `token` |
| `cookie` | Cookie 认证 | `token` |
| `authorization` | Authorization 头认证 | `token` |
| `password` | 用户名密码认证 | `username`, `password` |
| `custom` | 自定义认证 | `token`, `ext1`-`ext5` |
## 示例
### Token 认证
```json
{
"authType": "accesstoken",
"token": "your_access_token_here"
}
```
### Cookie 认证
```json
{
"authType": "cookie",
"token": "session_id=abc123; user_token=xyz789"
}
```
### 用户名密码
```json
{
"authType": "password",
"username": "your_username",
"password": "your_password"
}
```
### 自定义认证
```json
{
"authType": "custom",
"token": "main_token",
"ext1": "refresh_token:your_refresh_token",
"ext2": "device_id:device123"
}
```
## 使用说明
1. **编码流程**: JSON对象 → AES加密 → Base64编码 → URL编码
2. **加密配置**: AES/ECB/PKCS5Padding, 密钥: `nfd_auth_key2026` (16位)
3. **接口调用**: `GET /parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}`
## 接口调用示例
### 基础接口
#### 1. 解析并重定向 (GET /parser)
```
GET /parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
```
**参数说明:**
- `url`: 网盘分享链接(必填)
- `pwd`: 提取码(可选)
- `auth`: 加密后的认证参数(可选)
**响应:** 302 重定向到直链
#### 2. 解析返回 JSON (GET /json/parser)
```
GET /json/parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
```
**响应示例:**
```json
{
"shareKey": "lz:xxxx",
"directLink": "https://...",
"cacheHit": false,
"expires": "2026-02-05 12:00:00",
"expiration": 1738728000000
}
```
#### 3. 获取链接信息 (GET /v2/linkInfo)
```
GET /v2/linkInfo?url={分享链接}&pwd={提取码}&auth={加密认证参数}
```
**响应:** 返回下载链接、API 链接、预览链接等信息
+5 -5
View File
@@ -12,7 +12,7 @@
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<artifactId>parser</artifactId> <artifactId>parser</artifactId>
<version>10.2.5</version> <version>10.2.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>cn.qaiu:parser</name> <name>cn.qaiu:parser</name>
@@ -52,19 +52,19 @@
</distributionManagement> </distributionManagement>
<properties> <properties>
<revision>0.2.1</revision> <revision>0.1.8</revision>
<java.version>17</java.version> <java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Versions --> <!-- Versions -->
<vertx.version>4.5.24</vertx.version> <vertx.version>4.5.22</vertx.version>
<org.reflections.version>0.10.2</org.reflections.version> <org.reflections.version>0.10.2</org.reflections.version>
<lombok.version>1.18.38</lombok.version> <lombok.version>1.18.38</lombok.version>
<slf4j.version>2.0.16</slf4j.version> <slf4j.version>2.0.5</slf4j.version>
<commons-lang3.version>3.18.0</commons-lang3.version> <commons-lang3.version>3.18.0</commons-lang3.version>
<jackson.version>2.18.6</jackson.version> <jackson.version>2.14.2</jackson.version>
<logback.version>1.5.19</logback.version> <logback.version>1.5.19</logback.version>
<junit.version>4.13.2</junit.version> <junit.version>4.13.2</junit.version>
</properties> </properties>
@@ -56,15 +56,7 @@ public abstract class PanBase implements IPanTool {
protected WebClient clientNoRedirects = WebClient.create(WebClientVertxInit.get(), protected WebClient clientNoRedirects = WebClient.create(WebClientVertxInit.get(),
new WebClientOptions().setFollowRedirects(false)); new WebClientOptions().setFollowRedirects(false));
/**
* Http client disable UserAgent
*/
protected WebClient clientDisableUA = WebClient.create(WebClientVertxInit.get()
, new WebClientOptions().setUserAgentEnabled(false)
);
protected ShareLinkInfo shareLinkInfo; protected ShareLinkInfo shareLinkInfo;
/** /**
* 子类重写此构造方法不需要添加额外逻辑 * 子类重写此构造方法不需要添加额外逻辑
@@ -68,43 +68,42 @@ public enum PanDomainTemplate {
t-is.cn t-is.cn
*/ */
LZ("蓝奏云", LZ("蓝奏云",
compile("https://(?:[a-zA-Z\\d-]+\\.)?(?:" + compile("https://(?:[a-zA-Z\\d-]+\\.)?(" +
"(?:lanzoul|" + "lanzoul|" +
"lanzouh|" + "lanzouh|" +
"lanosso|" + "lanosso|" +
"lanpv|" + "lanpv|" +
"bakstotre|" + "bakstotre|" +
"lanzouo|" + "lanzouo|" +
"lanzov|" + "lanzov|" +
"lanpw|" + "lanpw|" +
"ulanzou|" + "ulanzou|" +
"lanzouf|" + "lanzouf|" +
"lanzn|" + "lanzn|" +
"lanzouj|" + "lanzouj|" +
"lanzouk|" + "lanzouk|" +
"lanzouq|" + "lanzouq|" +
"lanzouv|" + "lanzouv|" +
"lanzoue|" + "lanzoue|" +
"lanzouw|" + "lanzouw|" +
"lanzoub|" + "lanzoub|" +
"lanzouu|" + "lanzouu|" +
"lanwp|" + "lanwp|" +
"lanzouy|" + "lanzouy|" +
"lanzoup|" + "lanzoup|" +
"woozooo|" + "woozooo|" +
"lanzv|" + "lanzv|" +
"dmpdmp|" + "dmpdmp|" +
"lanrar|" + "lanrar|" +
"webgetstore|" + "webgetstore|" +
"lanzb|" + "lanzb|" +
"lanzoux|" + "lanzoux|" +
"lanzout|" + "lanzout|" +
"lanzouc|" + "lanzouc|" +
"lanzoui|" + "lanzoui|" +
"lanzoug|" + "lanzoug|" +
"lanzoum)\\.com" + "lanzoum" +
"|t-is\\.cn" + ")\\.com/(.+/)?(?<KEY>.+)"),
")/(?<KEY>.+)"),
"https://w1.lanzn.com/{shareKey}", "https://w1.lanzn.com/{shareKey}",
LzTool.class), LzTool.class),
@@ -115,15 +114,15 @@ public enum PanDomainTemplate {
"https://www.feijix.com/s/{shareKey}", "https://www.feijix.com/s/{shareKey}",
FjTool.class), FjTool.class),
// https://lecloud.lenovo.com/share/ https://lecloud.lenovo.com/mshare/ // https://lecloud.lenovo.com/share/
LE("联想乐云", LE("联想乐云",
compile("https://lecloud\\.lenovo\\.com/m?share/(?<KEY>.+)"), compile("https://lecloud?\\.lenovo\\.com/share/(?<KEY>.+)"),
"https://lecloud.lenovo.com/share/{shareKey}", "https://lecloud.lenovo.com/share/{shareKey}",
LeTool.class), LeTool.class),
// https://v2.fangcloud.com/s/ // https://v2.fangcloud.com/s/
FC("亿方云", FC("亿方云",
compile("https://v2\\.fangcloud\\.(com|cn)/(s|share|sharing)/(?<KEY>.+)"), compile("https://v2\\.fangcloud\\.(com|cn)/(s|sharing)/(?<KEY>.+)"),
"https://v2.fangcloud.com/s/{shareKey}", "https://v2.fangcloud.com/s/{shareKey}",
"https://www.fangcloud.com/", "https://www.fangcloud.com/",
FcTool.class), FcTool.class),
@@ -144,41 +143,9 @@ public enum PanDomainTemplate {
compile("https://qfile\\.qq\\.com/q/(?<KEY>.+)"), compile("https://qfile\\.qq\\.com/q/(?<KEY>.+)"),
"https://qfile.qq.com/q/{shareKey}", "https://qfile.qq.com/q/{shareKey}",
QQscTool.class), 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("文叔叔", WS("文叔叔",
compile("https://(f\\.ws(\\d{2})\\.cn|" + compile("https://(f\\.ws(\\d{2})\\.cn|www\\.wenshushu\\.cn)/f/(?<KEY>.+)"),
"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}", "https://www.wenshushu.cn/f/{shareKey}",
WsTool.class), WsTool.class),
// https://www.123pan.com/s/ // https://www.123pan.com/s/
@@ -232,7 +199,7 @@ public enum PanDomainTemplate {
"123635\\.com|" + "123635\\.com|" +
"123242\\.com|" + "123242\\.com|" +
"123795\\.com" + "123795\\.com" +
")/s/(?<KEY>[a-zA-Z0-9_-]+)(?:\\.html)?"), ")/s/(?<KEY>.+)(.html)?"),
"https://www.123pan.com/s/{shareKey}", "https://www.123pan.com/s/{shareKey}",
Ye2Tool.class), Ye2Tool.class),
// https://www.ecpan.cn/web/#/yunpanProxy?path=%2F%23%2Fdrive%2Foutside&data={code}&isShare=1 // https://www.ecpan.cn/web/#/yunpanProxy?path=%2F%23%2Fdrive%2Foutside&data={code}&isShare=1
@@ -243,7 +210,7 @@ public enum PanDomainTemplate {
EcTool.class), EcTool.class),
// https://cowtransfer.com/s/ // https://cowtransfer.com/s/
COW("奶牛快传", COW("奶牛快传",
compile("https://(?:[a-zA-Z\\d-]+\\.)?cowtransfer\\.com/s/(?<KEY>.+)"), compile("https://(.*)cowtransfer\\.com/s/(?<KEY>.+)"),
"https://cowtransfer.com/s/{shareKey}", "https://cowtransfer.com/s/{shareKey}",
CowTool.class), CowTool.class),
CT("城通网盘", CT("城通网盘",
@@ -251,6 +218,11 @@ public enum PanDomainTemplate {
"(?<KEY>[0-9a-zA-Z_-]+)(\\?p=(?<PWD>\\w+))?"), "(?<KEY>[0-9a-zA-Z_-]+)(\\?p=(?<PWD>\\w+))?"),
"https://474b.com/file/{shareKey}", "https://474b.com/file/{shareKey}",
CtTool.class), CtTool.class),
// https://xxx.118pan.com/bxxx
P118("118网盘",
compile("https://(?:[a-zA-Z\\d-]+\\.)?118pan\\.com/b(?<KEY>.+)"),
"https://qaiu.118pan.com/b{shareKey}",
P118Tool.class),
// https://www.vyuyun.com/s/QMa6ie?password=I4KG7H // https://www.vyuyun.com/s/QMa6ie?password=I4KG7H
// https://www.vyuyun.com/s/QMa6ie/file?password=I4KG7H // https://www.vyuyun.com/s/QMa6ie/file?password=I4KG7H
PVYY("微雨云存储", PVYY("微雨云存储",
@@ -266,7 +238,7 @@ public enum PanDomainTemplate {
PodTool.class), PodTool.class),
// 404网盘 https://drive.google.com/file/d/xxx/view?usp=sharing // 404网盘 https://drive.google.com/file/d/xxx/view?usp=sharing
PGD("GoogleDrive", PGD("GoogleDrive",
compile("https://(?:[a-zA-Z\\d-]+\\.)?drive\\.google\\.com/file/d/(?<KEY>.+)/view(\\?usp=(sharing|drive_link))?"), compile("https://drive\\.google\\.com/file/d/(?<KEY>.+)/view(\\?usp=(sharing|drive_link))?"),
"https://drive.google.com/file/d/{shareKey}/view?usp=sharing", "https://drive.google.com/file/d/{shareKey}/view?usp=sharing",
PgdTool.class), PgdTool.class),
// iCloud https://www.icloud.com.cn/iclouddrive/xxx#fonts // iCloud https://www.icloud.com.cn/iclouddrive/xxx#fonts
@@ -276,11 +248,11 @@ public enum PanDomainTemplate {
PicTool.class), PicTool.class),
// https://www.dropbox.com/scl/fi/cwnbms1yn8u6rcatzyta7/emqx-5.0.26-el7-amd64.tar.gz?rlkey=3uoi4bxz5mv93jmlaws0nlol1&e=8&st=fe0lclc2&dl=0 // https://www.dropbox.com/scl/fi/cwnbms1yn8u6rcatzyta7/emqx-5.0.26-el7-amd64.tar.gz?rlkey=3uoi4bxz5mv93jmlaws0nlol1&e=8&st=fe0lclc2&dl=0
PDB("dropbox", PDB("dropbox",
compile("https://www\\.dropbox\\.com/scl/fi/(?<KEY>\\w+)/.+?rlkey=(?<PWD>\\w+).*"), compile("https://www.dropbox.com/scl/fi/(?<KEY>\\w+)/.+?rlkey=(?<PWD>\\w+).*"),
"https://www.dropbox.com/scl/fi/{shareKey}/?rlkey={pwd}&dl=0", "https://www.dropbox.com/scl/fi/{shareKey}/?rlkey={pwd}&dl=0",
PdbTool.class), PdbTool.class),
P115("115网盘", P115("115网盘",
compile("https://(115|anxia)\\.com/s/(?<KEY>\\w+)(\\?password=(?<PWD>\\w+))?([&#].*)?"), compile("https://(115|anxia).com/s/(?<KEY>\\w+)(\\?password=(?<PWD>\\w+))?([&#].*)?"),
"https://115.com/s/{shareKey}?password={pwd}", "https://115.com/s/{shareKey}?password={pwd}",
P115Tool.class), P115Tool.class),
// 链接:https://www.yunpan.com/surl_yD7wz4VgU9v(提取码:fc70) // 链接:https://www.yunpan.com/surl_yD7wz4VgU9v(提取码:fc70)
@@ -291,7 +263,7 @@ public enum PanDomainTemplate {
// https://pan-yz.cldisk.com/external/m/file/953658049102462976 // https://pan-yz.cldisk.com/external/m/file/953658049102462976
Pcx("超星云盘(需要referer头)", Pcx("超星云盘(需要referer头)",
compile("https://pan-yz\\.(chaoxing\\.com|cldisk\\.com)/external/m/file/(?<KEY>\\w+)(\\?.*)?"), compile("https://pan-yz\\.cldisk\\.com/external/m/file/(?<KEY>\\w+)"),
"https://pan-yz.cldisk.com/external/m/file/{shareKey}", "https://pan-yz.cldisk.com/external/m/file/{shareKey}",
PcxTool.class), PcxTool.class),
// WPS:分享格式:https://www.kdocs.cn/l/ck0azivLlDi3 ;API格式:https://www.kdocs.cn/api/office/file/{shareKey}/download // WPS:分享格式:https://www.kdocs.cn/l/ck0azivLlDi3 ;API格式:https://www.kdocs.cn/api/office/file/{shareKey}/download
@@ -300,27 +272,6 @@ public enum PanDomainTemplate {
compile("https://(?:[a-zA-Z\\d-]+\\.)?kdocs\\.cn/l/(?<KEY>.+)"), compile("https://(?:[a-zA-Z\\d-]+\\.)?kdocs\\.cn/l/(?<KEY>.+)"),
"https://www.kdocs.cn/l/{shareKey}", "https://www.kdocs.cn/l/{shareKey}",
PwpsTool.class), PwpsTool.class),
// https://fast.uc.cn/s/33197dd53ace4
// https://drive.uc.cn/s/e623b6da278e4?public=1#/list/share
UC("UC网盘",
compile("https://(fast|drive)\\.uc\\.cn/s/(?<KEY>\\w+)(\\?public=\\d+)?([&#].*)?"),
"https://drive.uc.cn/s/{shareKey}",
UcTool.class),
// https://pan.quark.cn/s/6a325cdaec58
QK("夸克网盘",
compile("https://pan\\.quark\\.cn/s/(?<KEY>\\w+)([&#].*)?"),
"https://pan.quark.cn/s/{shareKey}",
QkTool.class),
// https://xxx.feishu.cn/file/VnCxbt35KoowKoxldO3c3C7VnMc
// https://xxx.feishu.cn/drive/folder/RQSKf8EQ4l7dMedqzHucpMbancg
FS("飞书云盘",
compile("https://[^.]+\\.feishu\\.cn/(?:file|drive/folder)/(?<KEY>[A-Za-z0-9_-]+)(\\?.*)?"),
"https://feishu.cn/file/{shareKey}",
"https://www.feishu.cn/",
FsTool.class),
// =====================音乐类解析 分享链接标志->MxxS (单歌曲/普通音质)========================== // =====================音乐类解析 分享链接标志->MxxS (单歌曲/普通音质)==========================
// http://163cn.tv/xxx // http://163cn.tv/xxx
MNES("网易云音乐分享", MNES("网易云音乐分享",
@@ -329,7 +280,7 @@ public enum PanDomainTemplate {
MnesTool.class), MnesTool.class),
// https://music.163.com/#/song?id=xxx // https://music.163.com/#/song?id=xxx
MNE("网易云音乐歌曲详情", MNE("网易云音乐歌曲详情",
compile("https://(y\\.)?music\\.163\\.com/(?:#/|m/)?song\\?id=(?<KEY>.+)(&.*)?"), compile("https://(y.)?music\\.163\\.com/(#|m/)?song\\?id=(?<KEY>.+)(&.*)?"),
"https://music.163.com/#/song?id={shareKey}", "https://music.163.com/#/song?id={shareKey}",
MnesTool.MneTool.class), MnesTool.MneTool.class),
// https://c6.y.qq.com/base/fcgi-bin/u?__=xxx // https://c6.y.qq.com/base/fcgi-bin/u?__=xxx
@@ -350,7 +301,7 @@ public enum PanDomainTemplate {
MkgsTool.class), MkgsTool.class),
// https://www.kugou.com/share/2bi8Fe9CSV3.html?id=2bi8Fe9CSV3#6ed9gna4" // https://www.kugou.com/share/2bi8Fe9CSV3.html?id=2bi8Fe9CSV3#6ed9gna4"
MKGS2("酷狗音乐分享2", MKGS2("酷狗音乐分享2",
compile("https://(?:[a-zA-Z\\d-]+\\.)?kugou\\.com/share/(?<KEY>.+)\\.html.*"), compile("https://(?:[a-zA-Z\\d-]+\\.)?kugou\\.com/share/(?<KEY>.+).html.*"),
"https://www.kugou.com/share/{shareKey}.html", "https://www.kugou.com/share/{shareKey}.html",
MkgsTool.Mkgs2Tool.class), MkgsTool.Mkgs2Tool.class),
// https://www.kugou.com/mixsong/2bi8Fe9CSV3 // https://www.kugou.com/mixsong/2bi8Fe9CSV3
@@ -11,13 +11,6 @@ import java.util.concurrent.ConcurrentHashMap;
/** /**
* 客户端下载链接生成器工厂类 * 客户端下载链接生成器工厂类
* <p>
* 支持的客户端类型:
* <ul>
* <li>CURL - cURL 命令,支持 Cookie</li>
* <li>ARIA2 - Aria2 命令,支持 Cookie</li>
* <li>THUNDER - 迅雷协议,不支持 Cookie</li>
* </ul>
* *
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21 * Create at 2025/01/21
@@ -32,10 +25,16 @@ public class ClientLinkGeneratorFactory {
// 静态初始化块,注册默认的生成器 // 静态初始化块,注册默认的生成器
static { static {
try { try {
// 注册默认生成器 - 只保留3种(按需求) // 注册默认生成器 - 按指定顺序注册
register(new CurlLinkGenerator()); // cURL 命令,支持 Cookie register(new Aria2LinkGenerator());
register(new Aria2LinkGenerator()); // Aria2 命令,支持 Cookie register(new MotrixLinkGenerator());
register(new ThunderLinkGenerator()); // 迅雷协议,不支持 Cookie register(new BitCometLinkGenerator());
register(new ThunderLinkGenerator());
register(new WgetLinkGenerator());
register(new CurlLinkGenerator());
register(new IdmLinkGenerator());
register(new FdmLinkGenerator());
register(new PowerShellLinkGenerator());
log.info("客户端链接生成器工厂初始化完成,已注册 {} 个生成器", generators.size()); log.info("客户端链接生成器工厂初始化完成,已注册 {} 个生成器", generators.size());
} catch (Exception e) { } catch (Exception e) {
@@ -2,32 +2,27 @@ package cn.qaiu.parser.clientlink;
/** /**
* 客户端下载工具类型枚举 * 客户端下载工具类型枚举
* <p>
* 支持的客户端类型:
* <ul>
* <li>CURL - cURL 命令行工具,支持 Cookie</li>
* <li>ARIA2 - 多线程下载器,支持 Cookie</li>
* <li>THUNDER - 迅雷下载器,不支持 Cookie(使用迅雷协议)</li>
* </ul>
* *
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21 * Create at 2025/01/21
*/ */
public enum ClientLinkType { public enum ClientLinkType {
CURL("curl", "cURL 命令", true, "命令行下载工具,支持Cookie"), ARIA2("aria2", "Aria2"),
ARIA2("aria2", "Aria2", true, "多线程下载器,支持Cookie"), MOTRIX("motrix", "Motrix"),
THUNDER("thunder", "迅雷", false, "迅雷下载器,不支持Cookie"); BITCOMET("bitcomet", "比特彗星"),
THUNDER("thunder", "迅雷"),
WGET("wget", "wget 命令"),
CURL("curl", "cURL 命令"),
IDM("idm", "IDM"),
FDM("fdm", "Free Download Manager"),
POWERSHELL("powershell", "PowerShell");
private final String code; private final String code;
private final String displayName; private final String displayName;
private final boolean supportsCookie;
private final String description;
ClientLinkType(String code, String displayName, boolean supportsCookie, String description) { ClientLinkType(String code, String displayName) {
this.code = code; this.code = code;
this.displayName = displayName; this.displayName = displayName;
this.supportsCookie = supportsCookie;
this.description = description;
} }
public String getCode() { public String getCode() {
@@ -38,14 +33,6 @@ public enum ClientLinkType {
return displayName; return displayName;
} }
public boolean isSupportsCookie() {
return supportsCookie;
}
public String getDescription() {
return description;
}
@Override @Override
public String toString() { public String toString() {
return displayName; return displayName;
@@ -7,13 +7,6 @@ import java.util.Map;
/** /**
* 客户端下载链接生成工具类 * 客户端下载链接生成工具类
* 提供便捷的静态方法来生成各种客户端下载链接 * 提供便捷的静态方法来生成各种客户端下载链接
* <p>
* 支持的客户端类型:
* <ul>
* <li>CURL - cURL 命令,支持 Cookie</li>
* <li>ARIA2 - Aria2 命令,支持 Cookie</li>
* <li>THUNDER - 迅雷协议,不支持 Cookie</li>
* </ul>
* *
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21 * Create at 2025/01/21
@@ -42,7 +35,7 @@ public class ClientLinkUtils {
} }
/** /**
* 生成 curl 命令(支持 Cookie) * 生成 curl 命令
* *
* @param info ShareLinkInfo 对象 * @param info ShareLinkInfo 对象
* @return curl 命令字符串 * @return curl 命令字符串
@@ -52,7 +45,17 @@ public class ClientLinkUtils {
} }
/** /**
* 生成 aria2 命令(支持 Cookie) * 生成 wget 命令
*
* @param info ShareLinkInfo 对象
* @return wget 命令字符串
*/
public static String generateWgetCommand(ShareLinkInfo info) {
return generateClientLink(info, ClientLinkType.WGET);
}
/**
* 生成 aria2 命令
* *
* @param info ShareLinkInfo 对象 * @param info ShareLinkInfo 对象
* @return aria2 命令字符串 * @return aria2 命令字符串
@@ -62,7 +65,7 @@ public class ClientLinkUtils {
} }
/** /**
* 生成迅雷链接(不支持 Cookie) * 生成迅雷链接
* *
* @param info ShareLinkInfo 对象 * @param info ShareLinkInfo 对象
* @return 迅雷协议链接 * @return 迅雷协议链接
@@ -71,6 +74,56 @@ public class ClientLinkUtils {
return generateClientLink(info, ClientLinkType.THUNDER); return generateClientLink(info, ClientLinkType.THUNDER);
} }
/**
* 生成 IDM 链接
*
* @param info ShareLinkInfo 对象
* @return IDM 协议链接
*/
public static String generateIdmLink(ShareLinkInfo info) {
return generateClientLink(info, ClientLinkType.IDM);
}
/**
* 生成比特彗星链接
*
* @param info ShareLinkInfo 对象
* @return 比特彗星协议链接
*/
public static String generateBitCometLink(ShareLinkInfo info) {
return generateClientLink(info, ClientLinkType.BITCOMET);
}
/**
* 生成 Motrix 导入格式
*
* @param info ShareLinkInfo 对象
* @return Motrix JSON 格式字符串
*/
public static String generateMotrixFormat(ShareLinkInfo info) {
return generateClientLink(info, ClientLinkType.MOTRIX);
}
/**
* 生成 FDM 导入格式
*
* @param info ShareLinkInfo 对象
* @return FDM 格式字符串
*/
public static String generateFdmFormat(ShareLinkInfo info) {
return generateClientLink(info, ClientLinkType.FDM);
}
/**
* 生成 PowerShell 命令
*
* @param info ShareLinkInfo 对象
* @return PowerShell 命令字符串
*/
public static String generatePowerShellCommand(ShareLinkInfo info) {
return generateClientLink(info, ClientLinkType.POWERSHELL);
}
/** /**
* 检查 ShareLinkInfo 是否包含有效的下载元数据 * 检查 ShareLinkInfo 是否包含有效的下载元数据
* *
@@ -41,8 +41,6 @@ public class Aria2LinkGenerator implements ClientLinkGenerator {
parts.add("--continue"); // 支持断点续传 parts.add("--continue"); // 支持断点续传
parts.add("--max-tries=3"); // 最大重试次数 parts.add("--max-tries=3"); // 最大重试次数
parts.add("--retry-wait=5"); // 重试等待时间 parts.add("--retry-wait=5"); // 重试等待时间
parts.add("-s 8"); // 分成8片段下载
parts.add("-x 8"); // 每个服务器使用8个连接
// 添加URL // 添加URL
parts.add("\"" + meta.getUrl() + "\""); parts.add("\"" + meta.getUrl() + "\"");
@@ -0,0 +1,69 @@
package cn.qaiu.parser.clientlink.impl;
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
/**
* 比特彗星协议链接生成器
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class BitCometLinkGenerator implements ClientLinkGenerator {
@Override
public String generate(DownloadLinkMeta meta) {
if (!supports(meta)) {
return null;
}
try {
// 比特彗星支持 HTTP 下载,格式类似 IDM
String encodedUrl = Base64.getEncoder().encodeToString(
meta.getUrl().getBytes(StandardCharsets.UTF_8)
);
StringBuilder link = new StringBuilder("bitcomet:///?url=").append(encodedUrl);
// 添加请求头
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
StringBuilder headerStr = new StringBuilder();
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
if (headerStr.length() > 0) {
headerStr.append("\\r\\n");
}
headerStr.append(entry.getKey()).append(": ").append(entry.getValue());
}
String encodedHeaders = Base64.getEncoder().encodeToString(
headerStr.toString().getBytes(StandardCharsets.UTF_8)
);
link.append("&header=").append(encodedHeaders);
}
// 添加文件名
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
String encodedFileName = Base64.getEncoder().encodeToString(
meta.getFileName().getBytes(StandardCharsets.UTF_8)
);
link.append("&filename=").append(encodedFileName);
}
return link.toString();
} catch (Exception e) {
// 如果编码失败,返回简单的URL
return "bitcomet:///?url=" + meta.getUrl();
}
}
@Override
public ClientLinkType getType() {
return ClientLinkType.BITCOMET;
}
}
@@ -0,0 +1,56 @@
package cn.qaiu.parser.clientlink.impl;
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import java.util.Map;
/**
* Free Download Manager 导入格式生成器
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class FdmLinkGenerator implements ClientLinkGenerator {
@Override
public String generate(DownloadLinkMeta meta) {
if (!supports(meta)) {
return null;
}
// FDM 支持简单的文本格式导入
StringBuilder result = new StringBuilder();
result.append("URL=").append(meta.getUrl()).append("\n");
// 添加文件名
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
result.append("Filename=").append(meta.getFileName()).append("\n");
}
// 添加请求头
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
result.append("Headers=");
boolean first = true;
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
if (!first) {
result.append("; ");
}
result.append(entry.getKey()).append(": ").append(entry.getValue());
first = false;
}
result.append("\n");
}
result.append("Referer=").append(meta.getReferer() != null ? meta.getReferer() : "").append("\n");
result.append("User-Agent=").append(meta.getUserAgent() != null ? meta.getUserAgent() : "").append("\n");
return result.toString();
}
@Override
public ClientLinkType getType() {
return ClientLinkType.FDM;
}
}
@@ -0,0 +1,69 @@
package cn.qaiu.parser.clientlink.impl;
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
/**
* IDM 协议链接生成器
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class IdmLinkGenerator implements ClientLinkGenerator {
@Override
public String generate(DownloadLinkMeta meta) {
if (!supports(meta)) {
return null;
}
try {
// 对URL进行Base64编码
String encodedUrl = Base64.getEncoder().encodeToString(
meta.getUrl().getBytes(StandardCharsets.UTF_8)
);
StringBuilder link = new StringBuilder("idm:///?url=").append(encodedUrl);
// 添加请求头
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
StringBuilder headerStr = new StringBuilder();
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
if (headerStr.length() > 0) {
headerStr.append("\\r\\n");
}
headerStr.append(entry.getKey()).append(": ").append(entry.getValue());
}
String encodedHeaders = Base64.getEncoder().encodeToString(
headerStr.toString().getBytes(StandardCharsets.UTF_8)
);
link.append("&header=").append(encodedHeaders);
}
// 添加文件名
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
String encodedFileName = Base64.getEncoder().encodeToString(
meta.getFileName().getBytes(StandardCharsets.UTF_8)
);
link.append("&filename=").append(encodedFileName);
}
return link.toString();
} catch (Exception e) {
// 如果编码失败,返回简单的URL
return "idm:///?url=" + meta.getUrl();
}
}
@Override
public ClientLinkType getType() {
return ClientLinkType.IDM;
}
}
@@ -0,0 +1,53 @@
package cn.qaiu.parser.clientlink.impl;
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import io.vertx.core.json.JsonObject;
import java.util.Map;
/**
* Motrix 导入格式生成器
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class MotrixLinkGenerator implements ClientLinkGenerator {
@Override
public String generate(DownloadLinkMeta meta) {
if (!supports(meta)) {
return null;
}
// 使用 Vert.x JsonObject 构建 JSON
JsonObject taskJson = new JsonObject();
taskJson.put("url", meta.getUrl());
// 添加文件名
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
taskJson.put("filename", meta.getFileName());
}
// 添加请求头
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
JsonObject headersJson = new JsonObject();
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
headersJson.put(entry.getKey(), entry.getValue());
}
taskJson.put("headers", headersJson);
}
// 设置输出文件名
String outputFile = meta.getFileName() != null ? meta.getFileName() : "";
taskJson.put("out", outputFile);
return taskJson.encodePrettily();
}
@Override
public ClientLinkType getType() {
return ClientLinkType.MOTRIX;
}
}
@@ -0,0 +1,98 @@
package cn.qaiu.parser.clientlink.impl;
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* PowerShell 命令生成器
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class PowerShellLinkGenerator implements ClientLinkGenerator {
@Override
public String generate(DownloadLinkMeta meta) {
if (!supports(meta)) {
return null;
}
List<String> lines = new ArrayList<>();
// 创建 WebRequestSession
lines.add("$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession");
// 设置 User-Agent(如果存在)
String userAgent = meta.getUserAgent();
if (userAgent == null && meta.getHeaders() != null) {
userAgent = meta.getHeaders().get("User-Agent");
}
if (userAgent != null && !userAgent.trim().isEmpty()) {
lines.add("$session.UserAgent = \"" + escapePowerShellString(userAgent) + "\"");
}
// 构建 Invoke-WebRequest 命令
List<String> invokeParams = new ArrayList<>();
invokeParams.add("Invoke-WebRequest");
invokeParams.add("-UseBasicParsing");
invokeParams.add("-Uri \"" + escapePowerShellString(meta.getUrl()) + "\"");
// 添加 WebSession
invokeParams.add("-WebSession $session");
// 添加请求头
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
List<String> headerLines = new ArrayList<>();
headerLines.add("-Headers @{");
boolean first = true;
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
if (!first) {
headerLines.add("");
}
headerLines.add(" \"" + escapePowerShellString(entry.getKey()) + "\"=\"" +
escapePowerShellString(entry.getValue()) + "\"");
first = false;
}
headerLines.add("}");
// 将头部参数添加到主命令中
invokeParams.add(String.join("`\n", headerLines));
}
// 设置输出文件(如果指定了文件名)
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
invokeParams.add("-OutFile \"" + escapePowerShellString(meta.getFileName()) + "\"");
}
// 将所有参数连接起来
String invokeCommand = String.join(" `\n", invokeParams);
lines.add(invokeCommand);
return String.join("\n", lines);
}
/**
* 转义 PowerShell 字符串中的特殊字符
*/
private String escapePowerShellString(String str) {
if (str == null) {
return "";
}
return str.replace("`", "``")
.replace("\"", "`\"")
.replace("$", "`$");
}
@Override
public ClientLinkType getType() {
return ClientLinkType.POWERSHELL;
}
}
@@ -0,0 +1,51 @@
package cn.qaiu.parser.clientlink.impl;
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* wget 命令生成器
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class WgetLinkGenerator implements ClientLinkGenerator {
@Override
public String generate(DownloadLinkMeta meta) {
if (!supports(meta)) {
return null;
}
List<String> parts = new ArrayList<>();
parts.add("wget");
// 添加请求头
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
parts.add("--header=\"" + entry.getKey() + ": " + entry.getValue() + "\"");
}
}
// 设置输出文件名
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
parts.add("-O");
parts.add("\"" + meta.getFileName() + "\"");
}
// 添加URL
parts.add("\"" + meta.getUrl() + "\"");
return String.join(" \\\n ", parts);
}
@Override
public ClientLinkType getType() {
return ClientLinkType.WGET;
}
}
@@ -3,7 +3,9 @@ package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo; import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo; import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase; import cn.qaiu.parser.PanBase;
import cn.qaiu.util.*; import cn.qaiu.util.AESUtils;
import cn.qaiu.util.FileSizeConverter;
import cn.qaiu.util.UUIDUtil;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.MultiMap; import io.vertx.core.MultiMap;
import io.vertx.core.Promise; import io.vertx.core.Promise;
@@ -11,14 +13,12 @@ import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest; import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.uritemplate.UriTemplate; import io.vertx.uritemplate.UriTemplate;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.UUID;
/** /**
* 小飞机网盘 * 小飞机网盘
@@ -26,113 +26,69 @@ import java.util.UUID;
* @version V016_230609 * @version V016_230609
*/ */
public class FjTool extends PanBase { public class FjTool extends PanBase {
public static final String REFERER_URL = "https://share.feijipan.com/";
public static final String API_URL0 = "https://api.feijipan.com"; private static final String API_URL_PREFIX = "https://api.feejii.com/ws/";
private static final String API_URL_PREFIX = "https://api.feijipan.com/ws/";
private static final String FIRST_REQUEST_URL = API_URL_PREFIX + "recommend/list?devType=6&devModel=Chrome" + private static final String FIRST_REQUEST_URL = API_URL_PREFIX + "recommend/list?devType=6&devModel=Chrome" +
"&uuid={uuid}&extra=2&timestamp={ts}&shareId={shareId}&type=0&offset=1&limit=60"; "&uuid={uuid}&extra=2&timestamp={ts}&shareId={shareId}&type=0&offset=1&limit=60";
/// recommend/list?devType=6&devModel=Chrome&uuid={uuid}&extra=2&timestamp={ts}&shareId={shareId}&type=0&offset=1&limit=60
// recommend/list?devType=6&devModel=Chrome&uuid={uuid}&extra=2&timestamp={ts}&shareId=JoUTkZYj&type=0&offset=1&limit=60
private static final String LOGIN_URL = API_URL_PREFIX +
"login?uuid={uuid}&devType=6&devCode={uuid}&devModel=chrome&devVersion=127&appVersion=&timestamp={ts}&appToken=&extra=2";
private static final String TOKEN_VERIFY_URL = API_URL0 +
"/app/user/info/map?devType=6&devModel=Chrome&uuid={uuid}&extra=2&timestamp={ts}";
private static final String SECOND_REQUEST_URL = API_URL_PREFIX + "file/redirect?downloadId={fidEncode}&enable=1" + private static final String SECOND_REQUEST_URL = API_URL_PREFIX + "file/redirect?downloadId={fidEncode}&enable=1" +
"&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}"; "&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}";
// https://api.feijipan.com/ws/file/redirect?downloadId={fidEncode}&enable=1&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}
//https://api.feijipan.com/ws/file/redirect?
// downloadId=DBD34FFEDB71708FA5C284527F78E9EC104A9667FFEEA62CB6E00B54A3E0F5BB
// &enable=1
// &devType=6
// &uuid=rTaNVSgmwY5MbEEuiMmQL
// &timestamp=839E6B5E19223B8DF730A52F44062D48
// &auth=F799422BCD9D05D7CCC5C9C53C1092C7029B420536135C3B4B7E064F49459DCC
// &shareId=4wF7grHR
private static final String SECOND_REQUEST_URL_VIP = API_URL_PREFIX +
"file/redirect?downloadId={fidEncode}&enable=1&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}";
private static final String VIP_REQUEST_URL = API_URL_PREFIX + "/buy/vip/list?devType=6&devModel=Chrome&uuid" + private static final String VIP_REQUEST_URL = API_URL_PREFIX + "/buy/vip/list?devType=6&devModel=Chrome&uuid" +
"={uuid}&extra=2&timestamp={ts}"; "={uuid}&extra=2&timestamp={ts}";
// https://api.feijipan.com/ws/buy/vip/list?devType=6&devModel=Chrome&uuid=WQAl5yBy1naGudJEILBvE&extra=2&timestamp=E2C53155F6D09417A27981561134CB73 // https://api.feijipan.com/ws/buy/vip/list?devType=6&devModel=Chrome&uuid=WQAl5yBy1naGudJEILBvE&extra=2&timestamp=E2C53155F6D09417A27981561134CB73
// https://api.feijipan.com/ws/share/list?devType=6&devModel=Chrome&uuid=pwRWqwbk1J-KMTlRZowrn&extra=2&timestamp=C5F8A68C53121AB21FA35BA3529E8758&shareId=fmAuOh3m&folderId=28986333&offset=1&limit=60
private static final String FILE_LIST_URL = API_URL_PREFIX + "/share/list?devType=6&devModel=Chrome&uuid" + private static final String FILE_LIST_URL = API_URL_PREFIX + "/share/list?devType=6&devModel=Chrome&uuid" +
"={uuid}&extra=2&timestamp={ts}&shareId={shareId}&folderId" + "={uuid}&extra=2&timestamp={ts}&shareId={shareId}&folderId" +
"={folderId}&offset=1&limit=60"; "={folderId}&offset=1&limit=60";
private static final MultiMap header; private static final MultiMap header;
private static final MultiMap header0;
long nowTs = System.currentTimeMillis(); long nowTs = System.currentTimeMillis();
String tsEncode = AESUtils.encrypt2Hex(Long.toString(nowTs)); String tsEncode = AESUtils.encrypt2Hex(Long.toString(nowTs));
String uuid = UUIDUtil.fjUuid(); // 也可以使用 UUID.randomUUID().toString() String uuid = UUIDUtil.fjUuid(); // 也可以使用 UUID.randomUUID().toString()
static { static {
header0 = MultiMap.caseInsensitiveMultiMap(); header = MultiMap.caseInsensitiveMultiMap();
header0.set("Accept-Encoding", "gzip, deflate"); header.set("Accept", "application/json, text/plain, */*");
header0.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); header.set("Accept-Encoding", "gzip, deflate, br, zstd");
header0.set("Cache-Control", "no-cache"); header.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
header0.set("Connection", "keep-alive"); header.set("Cache-Control", "no-cache");
header0.set("Content-Length", "0"); header.set("Connection", "keep-alive");
header0.set("DNT", "1"); header.set("Content-Length", "0");
header0.set("Pragma", "no-cache"); header.set("DNT", "1");
header0.set("Referer", "https://www.feijipan.com/"); header.set("Host", "api.feijipan.com");
header0.set("Sec-Fetch-Dest", "empty"); header.set("Origin", "https://www.feijix.com");
header0.set("Sec-Fetch-Mode", "cors"); header.set("Pragma", "no-cache");
header0.set("Sec-Fetch-Site", "cross-site"); header.set("Referer", "https://www.feijix.com/");
header0.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"); header.set("Sec-Fetch-Dest", "empty");
header0.set("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\""); header.set("Sec-Fetch-Mode", "cors");
header0.set("sec-ch-ua-mobile", "?0"); header.set("Sec-Fetch-Site", "cross-site");
header0.set("sec-ch-ua-platform", "\"Windows\""); header.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
header.set("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
header = HeaderUtils.parseHeaders(""" header.set("sec-ch-ua-mobile", "?0");
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 header.set("sec-ch-ua-platform", "\"Windows\"");
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Cache-Control: no-cache
Connection: keep-alive
DNT: 1
Pragma: no-cache
Referer: https://www.feijix.com/
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0
sec-ch-ua: "Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
""");
} }
// String uuid = UUID.randomUUID().toString().toLowerCase(); // 也可以使用 UUID.randomUUID().toString()
static String token = null;
static String userId = null;
public static boolean authFlag = true;
public FjTool(ShareLinkInfo shareLinkInfo) { public FjTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo); super(shareLinkInfo);
} }
@Override
public Future<String> parse() { public Future<String> parse() {
String shareId = shareLinkInfo.getShareKey(); // String.valueOf(AESUtils.idEncrypt(dataKey)); // 240530 此处shareId又改为了原始的shareId
long nowTs = System.currentTimeMillis(); // String.valueOf(AESUtils.idEncrypt(dataKey));
String tsEncode = AESUtils.encrypt2Hex(Long.toString(nowTs)); final String shareId = shareLinkInfo.getShareKey();
if (shareLinkInfo.getOtherParam().containsKey("auths")) {
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
// 获取用户id
if (auths.contains("userId")) {
FjTool.userId = auths.get("userId");
log.info("已配置用户ID: {}", FjTool.userId);
} else {
log.warn("未配置用户ID, 可能会导致解析失败");
}
}
// 24.5.12 飞机盘 规则修改 需要固定UUID先请求会员接口, 再请求后续接口 // 24.5.12 飞机盘 规则修改 需要固定UUID先请求会员接口, 再请求后续接口
String url = StringUtils.isBlank(shareLinkInfo.getSharePassword()) ? FIRST_REQUEST_URL String url = StringUtils.isBlank(shareLinkInfo.getSharePassword()) ? FIRST_REQUEST_URL
@@ -142,312 +98,77 @@ public class FjTool extends PanBase {
.setTemplateParam("uuid", uuid) .setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode) .setTemplateParam("ts", tsEncode)
.send().onSuccess(r0 -> { // 忽略res .send().onSuccess(r0 -> { // 忽略res
// 第一次请求 获取文件信息 // 第一次请求 获取文件信息
// POST https://api.feijipan.com/ws/recommend/list?devType=6&devModel=Chrome&extra=2&shareId=146731&type=0&offset=1&limit=60 // POST https://api.feijipan.com/ws/recommend/list?devType=6&devModel=Chrome&extra=2&shareId=146731&type=0&offset=1&limit=60
client.postAbs(UriTemplate.of(url)) client.postAbs(UriTemplate.of(url))
.putHeaders(header0) .putHeaders(header)
.setTemplateParam("shareId", shareId) .setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid) .setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode) .setTemplateParam("ts", tsEncode)
.send().onSuccess(res -> { .send().onSuccess(res -> {
JsonObject resJson = asJson(res);
JsonObject resJson;
try {
resJson = asJson(res);
} catch (Exception e) {
log.error("获取文件信息失败: {}", res.bodyAsString());
return;
}
if (resJson.getInteger("code") != 200) { if (resJson.getInteger("code") != 200) {
fail(FIRST_REQUEST_URL + " 返回异常: " + resJson); fail(FIRST_REQUEST_URL + " 返回异常: " + resJson);
return; return;
} }
if (resJson.getJsonArray("list").isEmpty()) {
fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson);
return;
}
if (!resJson.containsKey("list") || resJson.getJsonArray("list").isEmpty()) { if (!resJson.containsKey("list") || resJson.getJsonArray("list").isEmpty()) {
fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson); fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson);
return; return;
} }
// 文件Id // 文件Id
JsonObject fileInfo = resJson.getJsonArray("list").getJsonObject(0); JsonObject fileInfo = resJson.getJsonArray("list").getJsonObject(0);
// 如果是目录返回目录ID // 如果是目录返回目录ID
if (!fileInfo.containsKey("fileList") || fileInfo.getJsonArray("fileList").isEmpty()) {
fail(FIRST_REQUEST_URL + " 文件列表为空: " + fileInfo);
return;
}
JsonObject fileList = fileInfo.getJsonArray("fileList").getJsonObject(0); JsonObject fileList = fileInfo.getJsonArray("fileList").getJsonObject(0);
if (fileList.getInteger("fileType") == 2) { if (fileList.getInteger("fileType") == 2) {
promise.complete(fileList.getInteger("folderId").toString()); promise.complete(fileList.getInteger("folderId").toString());
return; return;
} }
// 提取文件信息
extractFileInfo(fileList, fileInfo);
getDownURL(resJson);
}).onFailure(handleFail("请求1"));
}).onFailure(handleFail("请求1")); String fileId = fileInfo.getString("fileIds");
String userId = fileInfo.getString("userId");
// 其他参数
long nowTs2 = System.currentTimeMillis();
String tsEncode2 = AESUtils.encrypt2Hex(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2Hex(fileId + "|" + userId);
String auth = AESUtils.encrypt2Hex(fileId + "|" + nowTs2);
// 第二次请求
HttpRequest<Buffer> httpRequest =
clientNoRedirects.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", shareId);
// System.out.println(httpRequest.toString());
httpRequest.send().onSuccess(res2 -> {
MultiMap headers = res2.headers();
if (!headers.contains("Location")) {
fail(SECOND_REQUEST_URL + " 未找到重定向URL: \n" + res.headers());
return;
}
promise.complete(headers.get("Location"));
}).onFailure(handleFail(SECOND_REQUEST_URL));
}).onFailure(handleFail(FIRST_REQUEST_URL));
}).onFailure(handleFail(FIRST_REQUEST_URL));
return promise.future(); return promise.future();
} }
private void getDownURL(JsonObject resJson) {
String dataKey = shareLinkInfo.getShareKey();
// 文件Id
JsonObject fileInfo = resJson.getJsonArray("list").getJsonObject(0);
String fileId = fileInfo.getString("fileIds");
String userId = fileInfo.getString("userId");
// 其他参数
long nowTs2 = System.currentTimeMillis();
String tsEncode2 = AESUtils.encrypt2Hex(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2Hex(fileId + "|" + FjTool.userId);
String auth = AESUtils.encrypt2Hex(fileId + "|" + nowTs2);
// 检查是否有认证信息
if (shareLinkInfo.getOtherParam().containsKey("auths")) {
// 检查是否为临时认证(临时认证每次都尝试登录)
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
// 如果是临时认证,或者是后台配置且authFlag为true,则尝试使用认证
if (isTempAuth || authFlag) {
log.debug("尝试使用认证信息解析, isTempAuth={}, authFlag={}", isTempAuth, authFlag);
HttpRequest<Buffer> httpRequest =
clientNoRedirects.getAbs(UriTemplate.of(SECOND_REQUEST_URL_VIP))
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey)
;
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
if (token == null) {
// 执行登录
login(tsEncode2, auths).onFailure(failRes-> {
log.warn("登录失败: {}", failRes.getMessage());
fail(failRes.getMessage());
}).onSuccess(r-> {
httpRequest.setTemplateParam("fidEncode", AESUtils.encrypt2Hex(fileId + "|" + FjTool.userId))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
});
} else {
// 验证token
client.postAbs(UriTemplate.of(TOKEN_VERIFY_URL))
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header0).send().onSuccess(res -> {
if (asJson(res).getInteger("code") != 200) {
login(tsEncode2, auths).onFailure(failRes -> {
log.warn("重新登录失败: {}", failRes.getMessage());
fail(failRes.getMessage());
}).onSuccess(r-> {
httpRequest
.setTemplateParam("fidEncode", fidEncode)
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
});
} else {
httpRequest
.setTemplateParam("fidEncode", AESUtils.encrypt2Hex(fileId + "|" + FjTool.userId))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
}
}).onFailure(handleFail("Token验证"));
}
} else {
// authFlag 为 false,使用免登录解析
log.debug("authFlag=false,使用免登录解析");
clientNoRedirects.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey).send()
.onSuccess(this::down).onFailure(handleFail("请求2"));
}
} else {
// 没有认证信息,使用免登录解析
log.debug("无认证信息,使用免登录解析");
clientNoRedirects.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey).send()
.onSuccess(this::down).onFailure(handleFail("请求2"));
}
}
private Future<Void> login(String tsEncode2, MultiMap auths) {
Promise<Void> promise1 = Promise.promise();
// 如果配置了用户ID 则不登录
if (FjTool.userId != null) {
promise1.complete();
return promise1.future();
}
client.postAbs(UriTemplate.of(LOGIN_URL))
.setTemplateParam("uuid",uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header0)
.sendJsonObject(JsonObject.of("loginName", auths.get("username"), "loginPwd", auths.get("password")))
.onSuccess(res2->{
JsonObject json = asJson(res2);
if (json.getInteger("code") == 200) {
token = json.getJsonObject("data").getString("appToken");
header0.set("appToken", token);
log.info("登录成功 token: {}", token);
client.postAbs(UriTemplate.of(TOKEN_VERIFY_URL))
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header0).send().onSuccess(res -> {
if (asJson(res).getInteger("code") == 200) {
if (FjTool.userId == null) {
FjTool.userId = asJson(res).getJsonObject("map").getString("userId");
}
log.info("验证成功 userId: {}", FjTool.userId);
promise1.complete();
} else {
promise1.fail("验证失败: " + res.bodyAsString());
}
});
} else {
// 检查是否为临时认证
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
if (isTempAuth) {
// 临时认证失败,直接返回错误,不影响后台配置的认证
log.warn("临时认证失败: {}", json.getString("msg"));
promise1.fail("临时认证失败: " + json.getString("msg"));
} else {
// 后台配置的认证失败,设置authFlag并返回失败,让下次请求使用免登陆解析
log.warn("后台配置认证失败: {}, authFlag将设为false,请重新解析", json.getString("msg"));
authFlag = false;
promise1.fail("认证失败: " + json.getString("msg") + ", 请重新解析将使用免登陆模式");
}
}
}).onFailure(err -> {
log.error("登录请求异常: {}", err.getMessage());
promise1.fail("登录请求异常: " + err.getMessage());
});
return promise1.future();
}
/**
* 从接口返回数据中提取文件信息
*/
private void extractFileInfo(JsonObject fileList, JsonObject shareInfo) {
try {
// 文件名
String fileName = fileList.getString("fileName");
shareLinkInfo.getOtherParam().put("fileName", fileName);
// 文件大小 (KB -> Bytes)
Long fileSize = fileList.getLong("fileSize", 0L) * 1024;
shareLinkInfo.getOtherParam().put("fileSize", fileSize);
shareLinkInfo.getOtherParam().put("fileSizeFormat", FileSizeConverter.convertToReadableSize(fileSize));
// 文件图标
String fileIcon = fileList.getString("fileIcon");
if (StringUtils.isNotBlank(fileIcon)) {
shareLinkInfo.getOtherParam().put("fileIcon", fileIcon);
}
// 文件ID
Long fileId = fileList.getLong("fileId");
if (fileId != null) {
shareLinkInfo.getOtherParam().put("fileId", fileId.toString());
}
// 文件类型 (1=文件, 2=目录)
Integer fileType = fileList.getInteger("fileType", 1);
shareLinkInfo.getOtherParam().put("fileType", fileType == 1 ? "file" : "folder");
// 下载次数
Integer downloads = fileList.getInteger("fileDownloads", 0);
shareLinkInfo.getOtherParam().put("downloadCount", downloads);
// 点赞数
Integer likes = fileList.getInteger("fileLikes", 0);
shareLinkInfo.getOtherParam().put("likeCount", likes);
// 评论数
Integer comments = fileList.getInteger("fileComments", 0);
shareLinkInfo.getOtherParam().put("commentCount", comments);
// 评分
Double stars = fileList.getDouble("fileStars", 0.0);
shareLinkInfo.getOtherParam().put("stars", stars);
// 更新时间
String updateTime = fileList.getString("updTime");
if (StringUtils.isNotBlank(updateTime)) {
shareLinkInfo.getOtherParam().put("updateTime", updateTime);
}
// 创建时间
String createTime = null;
// 分享信息
if (shareInfo != null) {
// 分享ID
Integer shareId = shareInfo.getInteger("shareId");
if (shareId != null) {
shareLinkInfo.getOtherParam().put("shareId", shareId.toString());
}
// 上传时间
String addTime = shareInfo.getString("addTime");
if (StringUtils.isNotBlank(addTime)) {
shareLinkInfo.getOtherParam().put("createTime", addTime);
createTime = addTime;
}
// 预览次数
Integer previewNum = shareInfo.getInteger("previewNum", 0);
shareLinkInfo.getOtherParam().put("previewCount", previewNum);
// 用户信息
JsonObject userMap = shareInfo.getJsonObject("map");
if (userMap != null) {
String userName = userMap.getString("userName");
if (StringUtils.isNotBlank(userName)) {
shareLinkInfo.getOtherParam().put("userName", userName);
}
// VIP信息
Integer isVip = userMap.getInteger("isVip", 0);
shareLinkInfo.getOtherParam().put("isVip", isVip == 1);
}
}
// 创建 FileInfo 对象并存入 otherParam
FileInfo fileInfoObj = new FileInfo()
.setPanType(shareLinkInfo.getType())
.setFileName(fileName)
.setFileId(fileId != null ? fileId.toString() : null)
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setFileType(fileType == 1 ? "file" : "folder")
.setFileIcon(fileIcon)
.setDownloadCount(downloads)
.setCreateTime(createTime)
.setUpdateTime(updateTime);
shareLinkInfo.getOtherParam().put("fileInfo", fileInfoObj);
log.debug("提取文件信息成功: fileName={}, fileSize={}, downloads={}",
fileName, fileSize, downloads);
} catch (Exception e) {
log.warn("提取文件信息失败: {}", e.getMessage());
}
}
private void down(HttpResponse<Buffer> res2) {
MultiMap headers = res2.headers();
if (!headers.contains("Location") || headers.get("Location") == null) {
fail("找不到下载链接可能服务器已被禁止或者配置的认证信息有误: " + res2.bodyAsString());
return;
}
promise.complete(headers.get("Location"));
}
// 目录解析
@Override @Override
public Future<List<FileInfo>> parseFileList() { public Future<List<FileInfo>> parseFileList() {
Promise<List<FileInfo>> promise0 = Promise.promise(); Promise<List<FileInfo>> promise = Promise.promise();
String shareId = shareLinkInfo.getShareKey(); // String.valueOf(AESUtils.idEncrypt(dataKey)); String shareId = shareLinkInfo.getShareKey(); // String.valueOf(AESUtils.idEncrypt(dataKey));
@@ -455,52 +176,42 @@ public class FjTool extends PanBase {
String dirId = (String) shareLinkInfo.getOtherParam().get("dirId"); String dirId = (String) shareLinkInfo.getOtherParam().get("dirId");
if (dirId != null && !dirId.isEmpty()) { if (dirId != null && !dirId.isEmpty()) {
uuid = shareLinkInfo.getOtherParam().get("uuid").toString(); uuid = shareLinkInfo.getOtherParam().get("uuid").toString();
parserDir(dirId, shareId, promise0); parserDir(dirId, shareId, promise);
return promise0.future(); return promise.future();
} }
parse().onSuccess(id -> { parse().onSuccess(id -> {
parserDir(id, shareId, promise0); if (id != null && id.matches("^[a-zA-Z0-9]+$")) {
parserDir(id, shareId, promise);
} else {
promise.fail("解析目录ID失败");
}
}).onFailure(failRes -> { }).onFailure(failRes -> {
log.error("解析目录失败: {}", failRes.getMessage()); log.error("解析目录失败: {}", failRes.getMessage());
promise0.fail(failRes); promise.fail(failRes);
}); });
return promise0.future(); return promise.future();
} }
private void parserDir(String id, String shareId, Promise<List<FileInfo>> promise) { private void parserDir(String id, String shareId, Promise<List<FileInfo>> promise) {
// id以http开头直接返回 封装数组返回
if (id != null && (id.startsWith("http://") || id.startsWith("https://"))) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(id)
.setFileId(id)
.setFileType("file")
.setParserUrl(id)
.setPanType(shareLinkInfo.getType());
List<FileInfo> result = new ArrayList<>();
result.add(fileInfo);
promise.complete(result);
return;
}
log.debug("开始解析目录: {}, shareId: {}, uuid: {}, ts: {}", id, shareId, uuid, tsEncode); log.debug("开始解析目录: {}, shareId: {}, uuid: {}, ts: {}", id, shareId, uuid, tsEncode);
// 开始解析目录: 164312216, shareId: bPMsbg5K, uuid: 0fmVWTx2Ea4zFwkpd7KXf, ts: 20865d7b7f00828279f437cd1f097860 // 开始解析目录: 164312216, shareId: bPMsbg5K, uuid: 0fmVWTx2Ea4zFwkpd7KXf, ts: 20865d7b7f00828279f437cd1f097860
// 拿到目录ID // 拿到目录ID
client.postAbs(UriTemplate.of(FILE_LIST_URL)) client.postAbs(UriTemplate.of(FILE_LIST_URL))
.putHeaders(header0) .putHeaders(header)
.setTemplateParam("shareId", shareId) .setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid) .setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode) .setTemplateParam("ts", tsEncode)
.setTemplateParam("folderId", id) .setTemplateParam("folderId", id)
.send().onSuccess(res -> { .send().onSuccess(res -> {
JsonArray list; JsonObject jsonObject;
try { try {
JsonObject jsonObject = asJson(res); jsonObject = asJson(res);
System.out.println(jsonObject.encodePrettily());
list = jsonObject.getJsonArray("list");
} catch (Exception e) { } catch (Exception e) {
log.error("解析目录失败: {}", res.bodyAsString()); promise.fail(FIRST_REQUEST_URL + " 解析JSON失败: " + res.bodyAsString());
return; return;
} }
// System.out.println(jsonObject.encodePrettily());
JsonArray list = jsonObject.getJsonArray("list");
ArrayList<FileInfo> result = new ArrayList<>(); ArrayList<FileInfo> result = new ArrayList<>();
list.forEach(item->{ list.forEach(item->{
JsonObject fileJson = (JsonObject) item; JsonObject fileJson = (JsonObject) item;
@@ -513,7 +224,7 @@ public class FjTool extends PanBase {
// 其他参数 // 其他参数
long nowTs2 = System.currentTimeMillis(); long nowTs2 = System.currentTimeMillis();
String tsEncode2 = AESUtils.encrypt2Hex(Long.toString(nowTs2)); String tsEncode2 = AESUtils.encrypt2Hex(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2Hex(fileId + "|" + FjTool.userId); String fidEncode = AESUtils.encrypt2Hex(fileId + "|" + userId);
String auth = AESUtils.encrypt2Hex(fileId + "|" + nowTs2); String auth = AESUtils.encrypt2Hex(fileId + "|" + nowTs2);
// 回传用到的参数 // 回传用到的参数
@@ -528,7 +239,8 @@ public class FjTool extends PanBase {
"ts", tsEncode2, "ts", tsEncode2,
"auth", auth, "auth", auth,
"shareId", shareId); "shareId", shareId);
String param = CommonUtils.urlBase64Encode(entries.encode()); byte[] encode = Base64.getEncoder().encode(entries.encode().getBytes());
String param = new String(encode);
if (fileJson.getInteger("fileType") == 2) { if (fileJson.getInteger("fileType") == 2) {
// 如果是目录 // 如果是目录
@@ -568,15 +280,17 @@ public class FjTool extends PanBase {
result.add(fileInfo); result.add(fileInfo);
}); });
promise.complete(result); promise.complete(result);
}); }).onFailure(failRes -> {
log.error("解析目录请求失败: {}", failRes.getMessage());
promise.fail(failRes);
});;
} }
@Override @Override
public Future<String> parseById() { public Future<String> parseById() {
// 第二次请求 // 第二次请求
JsonObject paramJson = (JsonObject)shareLinkInfo.getOtherParam().get("paramJson"); JsonObject paramJson = (JsonObject)shareLinkInfo.getOtherParam().get("paramJson");
clientNoRedirects.getAbs(UriTemplate.of(SECOND_REQUEST_URL_VIP)) clientNoRedirects.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.setTemplateParam("fidEncode", paramJson.getString("fidEncode")) .setTemplateParam("fidEncode", paramJson.getString("fidEncode"))
.setTemplateParam("uuid", paramJson.getString("uuid")) .setTemplateParam("uuid", paramJson.getString("uuid"))
.setTemplateParam("ts", paramJson.getString("ts")) .setTemplateParam("ts", paramJson.getString("ts"))
@@ -585,16 +299,11 @@ public class FjTool extends PanBase {
.putHeaders(header).send().onSuccess(res2 -> { .putHeaders(header).send().onSuccess(res2 -> {
MultiMap headers = res2.headers(); MultiMap headers = res2.headers();
if (!headers.contains("Location")) { if (!headers.contains("Location")) {
fail(SECOND_REQUEST_URL_VIP + " 未找到重定向URL: \n" + res2.headers()); fail(SECOND_REQUEST_URL + " 未找到重定向URL: \n" + res2.headers());
return; return;
} }
promise.complete(headers.get("Location")); promise.complete(headers.get("Location"));
}).onFailure(handleFail(SECOND_REQUEST_URL_VIP)); }).onFailure(handleFail(SECOND_REQUEST_URL));
return promise.future(); return promise.future();
} }
public static void resetToken() {
token = null;
authFlag = true;
}
} }
@@ -1,492 +0,0 @@
package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase;
import cn.qaiu.util.CommonUtils;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <a href="https://www.feishu.cn/">飞书云盘</a>
* <p>
* 支持飞书公开分享文件和文件夹的解析。
* <ul>
* <li>文件链接: https://xxx.feishu.cn/file/{token}</li>
* <li>文件夹链接: https://xxx.feishu.cn/drive/folder/{token}</li>
* </ul>
* 飞书下载需要先获取匿名会话Cookie,然后使用Cookie请求下载接口。
* </p>
*/
public class FsTool extends PanBase {
private static final String UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
+ "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36";
/**
* 飞书 obj_type: type=12 表示上传文件可下载
*/
private static final int OBJ_TYPE_FILE = 12;
/**
* v3 列表 API 支持的 obj_type
*/
private static final int[] LIST_OBJ_TYPES = {
0, 2, 22, 44, 3, 30, 8, 11, 12, 84, 123, 124
};
/** 每页返回条目数 */
private static final int PAGE_SIZE = 50;
/**
* 从分享链接中提取 tenant 的正则
*/
private static final Pattern TENANT_PATTERN =
Pattern.compile("https://([^.]+)\\.feishu\\.cn/");
/** 解析 Content-Disposition: filename*=UTF-8''xxx */
private static final Pattern CD_FILENAME_STAR_PATTERN =
Pattern.compile("filename\\*=UTF-8''(.+?)(?:;|$)");
/** 解析 Content-Disposition: filename="xxx" 或 filename=xxx */
private static final Pattern CD_FILENAME_PATTERN =
Pattern.compile("filename=\"?([^\";]+)\"?");
/** 解析 Content-Range 中的总大小 */
private static final Pattern CONTENT_RANGE_SIZE_PATTERN =
Pattern.compile("/(\\d+)");
public FsTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo);
}
@Override
public Future<String> parse() {
String shareUrl = shareLinkInfo.getShareUrl();
String tenant = extractTenant(shareUrl);
String token = shareLinkInfo.getShareKey();
if (tenant == null || token == null) {
fail("无法从链接中提取tenant或token: {}", shareUrl);
return promise.future();
}
boolean isFolder = shareUrl.contains("/drive/folder/");
if (isFolder) {
fetchSessionAndParseFolder(tenant, token, shareUrl);
} else {
fetchSessionAndParseFile(tenant, token, shareUrl);
}
return promise.future();
}
/**
* 获取匿名session后解析文件
*/
private void fetchSessionAndParseFile(String tenant, String token, String shareUrl) {
clientSession.getAbs(shareUrl)
.putHeader("User-Agent", UA)
.putHeader("Accept", "text/html,*/*")
.send()
.onSuccess(res -> {
String dlUrl = buildDownloadUrl(tenant, token);
// Range探测获取文件名和大小
clientSession.getAbs(dlUrl)
.putHeader("User-Agent", UA)
.putHeader("Referer", shareUrl)
.putHeader("Range", "bytes=0-0")
.send()
.onSuccess(probeRes -> {
String fileName = parseFileNameFromContentDisposition(
probeRes.getHeader("Content-Disposition"));
Map<String, String> headers = new HashMap<>();
headers.put("Referer", shareUrl);
headers.put("User-Agent", UA);
String cookies = extractCookiesFromResponse(probeRes);
if (cookies != null && !cookies.isEmpty()) {
headers.put("Cookie", cookies);
}
if (fileName != null) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(fileName);
fileInfo.setFileId(token);
fileInfo.setFileType("file");
fileInfo.setPanType(shareLinkInfo.getType());
fileInfo.setParserUrl(buildRedirectUrl(shareUrl, token));
parseSizeFromContentRange(
probeRes.getHeader("Content-Range"), fileInfo);
shareLinkInfo.getOtherParam().put("fileInfo", fileInfo);
}
completeWithMeta(dlUrl, headers);
})
.onFailure(handleFail("探测文件信息失败"));
})
.onFailure(handleFail("获取匿名会话失败"));
}
/**
* 获取匿名session后解析文件夹(取第一个可下载文件)
*/
private void fetchSessionAndParseFolder(String tenant, String folderToken,
String shareUrl) {
clientSession.getAbs(shareUrl)
.putHeader("User-Agent", UA)
.putHeader("Accept", "text/html,*/*")
.send()
.onSuccess(res ->
listFolderAll(tenant, folderToken, "").onSuccess(items -> {
if (items.isEmpty()) {
fail("文件夹中没有可下载的文件");
return;
}
FileInfo first = items.get(0);
String objToken = first.getFileId();
String dlUrl = buildDownloadUrl(tenant, objToken);
String referer = "https://" + tenant
+ ".feishu.cn/drive/folder/" + folderToken;
Map<String, String> headers = new HashMap<>();
headers.put("Referer", referer);
headers.put("User-Agent", UA);
shareLinkInfo.getOtherParam().put("fileInfo", first);
completeWithMeta(dlUrl, headers);
}).onFailure(t -> fail("列出文件夹内容失败: {}", t.getMessage())))
.onFailure(handleFail("获取匿名会话失败"));
}
@Override
public Future<List<FileInfo>> parseFileList() {
Promise<List<FileInfo>> listPromise = Promise.promise();
String shareUrl = shareLinkInfo.getShareUrl();
String tenant = extractTenant(shareUrl);
String token = shareLinkInfo.getShareKey();
if (tenant == null || token == null) {
listPromise.fail("无法从链接中提取tenant或token: " + shareUrl);
return listPromise.future();
}
boolean isFolder = shareUrl.contains("/drive/folder/");
clientSession.getAbs(shareUrl)
.putHeader("User-Agent", UA)
.putHeader("Accept", "text/html,*/*")
.send()
.onSuccess(res -> {
if (isFolder) {
listFolderAll(tenant, token, "")
.onSuccess(listPromise::complete)
.onFailure(listPromise::fail);
} else {
probeSingleFile(tenant, token, shareUrl)
.onSuccess(fileInfo -> {
List<FileInfo> list = new ArrayList<>();
list.add(fileInfo);
listPromise.complete(list);
})
.onFailure(listPromise::fail);
}
})
.onFailure(t -> listPromise.fail("获取匿名会话失败: " + t.getMessage()));
return listPromise.future();
}
/**
* 分页获取文件夹所有可下载文件
*/
private Future<List<FileInfo>> listFolderAll(String tenant, String folderToken,
String pageLabel) {
Promise<List<FileInfo>> p = Promise.promise();
listFolderPage(tenant, folderToken, pageLabel).onSuccess(pageResult -> {
List<FileInfo> items = new ArrayList<>(pageResult.items);
if (pageResult.hasMore) {
listFolderAll(tenant, folderToken, pageResult.nextLabel)
.onSuccess(moreItems -> {
items.addAll(moreItems);
p.complete(items);
})
.onFailure(p::fail);
} else {
p.complete(items);
}
}).onFailure(p::fail);
return p.future();
}
/**
* 列出文件夹内容(单页)
*/
private Future<FolderPageResult> listFolderPage(String tenant, String folderToken,
String pageLabel) {
Promise<FolderPageResult> p = Promise.promise();
String baseUrl = "https://" + tenant + ".feishu.cn";
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(baseUrl)
.append("/space/api/explorer/v3/children/list/")
.append("?length=").append(PAGE_SIZE)
.append("&asc=1&rank=5&token=").append(folderToken);
for (int type : LIST_OBJ_TYPES) {
urlBuilder.append("&obj_type=").append(type);
}
if (pageLabel != null && !pageLabel.isEmpty()) {
urlBuilder.append("&last_label=").append(pageLabel);
}
String url = urlBuilder.toString();
String referer = baseUrl + "/drive/folder/" + folderToken;
clientSession.getAbs(url)
.putHeader("User-Agent", UA)
.putHeader("Accept", "application/json, text/plain, */*")
.putHeader("Referer", referer)
.send()
.onSuccess(res -> {
try {
JsonObject json = asJson(res);
int code = json.getInteger("code", -1);
if (code != 0) {
p.fail("飞书API错误: " + json.getString("msg"));
return;
}
JsonObject data = json.getJsonObject("data");
JsonObject entities = data.getJsonObject("entities",
new JsonObject());
JsonObject nodes = entities.getJsonObject("nodes",
new JsonObject());
JsonArray nodeList = data.getJsonArray("node_list",
new JsonArray());
List<FileInfo> items = new ArrayList<>();
for (int i = 0; i < nodeList.size(); i++) {
String nid = nodeList.getString(i);
JsonObject node = nodes.getJsonObject(nid,
new JsonObject());
int objType = node.getInteger("type", -1);
String objToken = node.getString("obj_token", "");
String name = node.getString("name", "unknown");
// 排除文件夹自身节点
if (objToken.equals(folderToken)) {
continue;
}
// 只返回可下载的文件(type=12)
if (objType == OBJ_TYPE_FILE) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(name);
fileInfo.setFileId(objToken);
fileInfo.setPanType(shareLinkInfo.getType());
fileInfo.setFileType("file");
JsonObject extra = node.getJsonObject("extra",
new JsonObject());
try {
long size = Long.parseLong(
extra.getString("size", "0"));
fileInfo.setSize(size);
} catch (NumberFormatException e) {
log.warn("无法解析文件大小: {}", extra.getString("size"), e);
}
fileInfo.setParserUrl(buildRedirectUrl(
shareLinkInfo.getShareUrl(), objToken));
// 添加下载所需的请求头到extParameters
Map<String, Object> extParams = new HashMap<>();
Map<String, String> downloadHeaders = new HashMap<>();
downloadHeaders.put("Referer", referer);
downloadHeaders.put("User-Agent", UA);
extParams.put("downloadHeaders", downloadHeaders);
fileInfo.setExtParameters(extParams);
items.add(fileInfo);
}
}
boolean hasMore = data.getBoolean("has_more", false);
String nextLabel = data.getString("last_label", "");
p.complete(new FolderPageResult(items, hasMore, nextLabel));
} catch (Exception e) {
p.fail("解析文件列表响应失败: " + e.getMessage());
}
})
.onFailure(t -> p.fail("请求文件列表失败: " + t.getMessage()));
return p.future();
}
/**
* 探测单个文件信息
*/
private Future<FileInfo> probeSingleFile(String tenant, String token,
String referer) {
Promise<FileInfo> p = Promise.promise();
String dlUrl = buildDownloadUrl(tenant, token);
clientSession.getAbs(dlUrl)
.putHeader("User-Agent", UA)
.putHeader("Referer", referer)
.putHeader("Range", "bytes=0-0")
.send()
.onSuccess(probeRes -> {
FileInfo fileInfo = new FileInfo();
String fileName = parseFileNameFromContentDisposition(
probeRes.getHeader("Content-Disposition"));
if (fileName != null) {
fileInfo.setFileName(fileName);
}
parseSizeFromContentRange(
probeRes.getHeader("Content-Range"), fileInfo);
fileInfo.setFileId(token);
fileInfo.setPanType(shareLinkInfo.getType());
fileInfo.setFileType("file");
fileInfo.setParserUrl(buildRedirectUrl(referer, token));
// 添加下载所需的请求头到extParameters
Map<String, Object> extParams = new HashMap<>();
Map<String, String> downloadHeaders = new HashMap<>();
downloadHeaders.put("Referer", referer);
downloadHeaders.put("User-Agent", UA);
extParams.put("downloadHeaders", downloadHeaders);
fileInfo.setExtParameters(extParams);
p.complete(fileInfo);
})
.onFailure(t -> p.fail("探测文件失败: " + t.getMessage()));
return p.future();
}
@Override
public Future<String> parseById() {
Promise<String> parsePromise = Promise.promise();
try {
JsonObject paramJson = (JsonObject) shareLinkInfo.getOtherParam().get("paramJson");
String shareUrl = paramJson.getString("shareUrl");
String objToken = paramJson.getString("objToken");
String tenant = extractTenant(shareUrl);
if (shareUrl == null || objToken == null || tenant == null) {
parsePromise.fail("飞书目录文件下载参数不完整");
return parsePromise.future();
}
parsePromise.complete(buildDownloadUrl(tenant, objToken));
} catch (Exception e) {
parsePromise.fail("解析飞书目录文件参数失败: " + e.getMessage());
}
return parsePromise.future();
}
// ─── 工具方法 ────────────────────────────────────────
private String buildRedirectUrl(String shareUrl, String objToken) {
JsonObject paramJson = new JsonObject()
.put("shareUrl", shareUrl)
.put("objToken", objToken);
return String.format("%s/v2/redirectUrl/%s/%s",
getDomainName(),
shareLinkInfo.getType(),
CommonUtils.urlBase64Encode(paramJson.encode()));
}
private String buildDownloadUrl(String tenant, String objToken) {
return "https://" + tenant
+ ".feishu.cn/space/api/box/stream/download/all/" + objToken;
}
private String extractTenant(String url) {
if (url == null) return null;
Matcher m = TENANT_PATTERN.matcher(url);
if (m.find()) {
return m.group(1);
}
return null;
}
/**
* 从Content-Disposition头解析文件名。
* 支持 filename*=UTF-8''xxx 和 filename="xxx" 两种格式。
*/
private String parseFileNameFromContentDisposition(String cd) {
if (cd == null || cd.isEmpty()) return null;
// 优先解析 filename*=UTF-8''xxx
Matcher m1 = CD_FILENAME_STAR_PATTERN.matcher(cd);
if (m1.find()) {
try {
return URLDecoder.decode(m1.group(1).trim(), StandardCharsets.UTF_8);
} catch (Exception ignored) {
}
}
// 降级解析 filename="xxx" 或 filename=xxx
Matcher m2 = CD_FILENAME_PATTERN.matcher(cd);
if (m2.find()) {
try {
return URLDecoder.decode(m2.group(1).trim(), StandardCharsets.UTF_8);
} catch (Exception ignored) {
}
}
return null;
}
private void parseSizeFromContentRange(String cr, FileInfo fileInfo) {
if (cr != null) {
Matcher m = CONTENT_RANGE_SIZE_PATTERN.matcher(cr);
if (m.find()) {
fileInfo.setSize(Long.parseLong(m.group(1)));
}
}
}
private String extractCookiesFromResponse(
io.vertx.ext.web.client.HttpResponse<?> response) {
List<String> setCookies = response.cookies();
if (setCookies == null || setCookies.isEmpty()) return null;
StringBuilder sb = new StringBuilder();
for (String cookie : setCookies) {
String nameValue = cookie.split(";")[0].trim();
if (!sb.isEmpty()) sb.append("; ");
sb.append(nameValue);
}
return sb.toString();
}
/**
* 文件夹分页结果
*/
private record FolderPageResult(List<FileInfo> items, boolean hasMore,
String nextLabel) {
}
}
@@ -1,55 +0,0 @@
package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.IPanTool;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
/**
* 蓝奏云优享解析器选择器
* 根据配置的鉴权方式选择不同的解析器:
* - 如果配置了 username 和 password,则使用 IzToolWithAuth (支持大文件)
* - 否则使用 IzTool (免登录,仅支持小文件)
*/
public class IzSelectorTool implements IPanTool {
private final IPanTool selectedTool;
public IzSelectorTool(ShareLinkInfo shareLinkInfo) {
if (shareLinkInfo.getOtherParam().containsKey("auths")) {
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
// 检查是否配置了账号密码
if (auths.contains("username") && auths.contains("password")) {
String username = auths.get("username");
String password = auths.get("password");
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
// 使用 IzToolWithAuth (账密登录,支持大文件)
this.selectedTool = new IzToolWithAuth(shareLinkInfo);
return;
}
}
}
// 无认证信息或认证信息无效,使用免登录版本(仅支持小文件)
this.selectedTool = new IzTool(shareLinkInfo);
}
@Override
public Future<String> parse() {
return selectedTool.parse();
}
@Override
public Future<List<FileInfo>> parseFileList() {
return selectedTool.parseFileList();
}
@Override
public Future<String> parseById() {
return selectedTool.parseById();
}
}
@@ -5,50 +5,35 @@ import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase; import cn.qaiu.parser.PanBase;
import cn.qaiu.util.AESUtils; import cn.qaiu.util.AESUtils;
import cn.qaiu.util.AcwScV2Generator; import cn.qaiu.util.AcwScV2Generator;
import cn.qaiu.util.CommonUtils;
import cn.qaiu.util.FileSizeConverter; import cn.qaiu.util.FileSizeConverter;
import io.netty.handler.codec.http.cookie.DefaultCookie; import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.MultiMap; import io.vertx.core.MultiMap;
import io.vertx.core.Promise; import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClientSession; import io.vertx.ext.web.client.WebClientSession;
import io.vertx.uritemplate.UriTemplate; import io.vertx.uritemplate.UriTemplate;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.UUID;
/** /**
* 蓝奏云优享 * 蓝奏云优享
* * v019b22
*/ */
public class IzTool extends PanBase { public class IzTool extends PanBase {
private static final String API_URL0 = "https://api.ilanzou.com/"; WebClientSession webClientSession = WebClientSession.create(clientNoRedirects);
private static final String API_URL_PREFIX = "https://api.ilanzou.com/unproved/"; private static final String API_URL_PREFIX = "https://api.ilanzou.com/unproved/";
private static final String FIRST_REQUEST_URL = API_URL_PREFIX + "recommend/list?devType=6&devModel=Chrome" + private static final String FIRST_REQUEST_URL = API_URL_PREFIX + "recommend/list?devType=6&devModel=Chrome" +
"&uuid={uuid}&extra=2&timestamp={ts}&shareId={shareId}&type=0&offset=1&limit=60"; "&uuid={uuid}&extra=2&timestamp={ts}&shareId={shareId}&type=0&offset=1&limit=60";
private static final String LOGIN_URL = API_URL_PREFIX +
"login?uuid={uuid}&devType=6&devCode={uuid}&devModel=chrome&devVersion=127&appVersion=&timestamp={ts}&appToken=&extra=2";
// https://api.ilanzou.com/proved/user/info/map?devType=3&devModel=Chrome&uuid=TInRHH3QzRaMo-Ajl2PkJ&extra=2&timestamp=EC2C6E7F45EB21338A17A7621E0BB437
private static final String TOKEN_VERIFY_URL = API_URL0 +
"proved/user/info/map?devType=6&devModel=Chrome&uuid={uuid}&extra=2&timestamp={ts}";
private static final String SECOND_REQUEST_URL = API_URL_PREFIX + "file/redirect?downloadId={fidEncode}&enable=1" + private static final String SECOND_REQUEST_URL = API_URL_PREFIX + "file/redirect?downloadId={fidEncode}&enable=1" +
"&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}"; "&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}";
// downloadId=x&enable=1&devType=6&uuid=x&timestamp=x&auth=x&shareId=lGFndCM
private static final String SECOND_REQUEST_URL_VIP = API_URL_PREFIX + "file/redirect?uuid={uuid}&devType=6&devCode={uuid}" +
"&devModel=chrome&devVersion=127&appVersion=&timestamp={ts}&appToken={appToken}&enable=1&downloadId={fidEncode}&auth={auth}";
private static final String VIP_REQUEST_URL = API_URL_PREFIX + "/buy/vip/list?devType=6&devModel=Chrome&uuid" + private static final String VIP_REQUEST_URL = API_URL_PREFIX + "/buy/vip/list?devType=6&devModel=Chrome&uuid" +
"={uuid}&extra=2&timestamp={ts}"; "={uuid}&extra=2&timestamp={ts}";
@@ -57,15 +42,16 @@ public class IzTool extends PanBase {
"={uuid}&extra=2&timestamp={ts}&shareId={shareId}&folderId" + "={uuid}&extra=2&timestamp={ts}&shareId={shareId}&folderId" +
"={folderId}&offset=1&limit=60"; "={folderId}&offset=1&limit=60";
long nowTs = System.currentTimeMillis();
WebClientSession webClientSession = WebClientSession.create(clientNoRedirects); String tsEncode = AESUtils.encrypt2HexIz(Long.toString(nowTs));
String uuid = UUID.randomUUID().toString();
private static final MultiMap header; private static final MultiMap header;
static { static {
header = MultiMap.caseInsensitiveMultiMap(); header = MultiMap.caseInsensitiveMultiMap();
header.set("Accept", "application/json, text/plain, */*"); header.set("Accept", "application/json, text/plain, */*");
header.set("Accept-Encoding", "gzip, deflate"); header.set("Accept-Encoding", "gzip, deflate, br, zstd");
header.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); header.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
header.set("Cache-Control", "no-cache"); header.set("Cache-Control", "no-cache");
header.set("Connection", "keep-alive"); header.set("Connection", "keep-alive");
@@ -83,59 +69,38 @@ public class IzTool extends PanBase {
header.set("sec-ch-ua-mobile", "?0"); header.set("sec-ch-ua-mobile", "?0");
header.set("sec-ch-ua-platform", "\"Windows\""); header.set("sec-ch-ua-platform", "\"Windows\"");
} }
public IzTool(ShareLinkInfo shareLinkInfo) { public IzTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo); super(shareLinkInfo);
} }
String uuid = UUID.randomUUID().toString().toLowerCase(); // 也可以使用 UUID.randomUUID().toString() private void setCookie(String html) {
int beginIndex = html.indexOf("arg1='") + 6;
public static String token = null; String arg1 = html.substring(beginIndex, html.indexOf("';", beginIndex));
public static boolean authFlag = true; String acw_sc__v2 = AcwScV2Generator.acwScV2Simple(arg1);
// 创建一个 Cookie 并放入 CookieStore
public Future<String> parse() { DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2);
nettyCookie.setDomain(".ilanzou.com"); // 设置域名
String shareId = shareLinkInfo.getShareKey(); // String.valueOf(AESUtils.idEncrypt(dataKey)); nettyCookie.setPath("/"); // 设置路径
long nowTs = System.currentTimeMillis(); nettyCookie.setSecure(false);
String tsEncode = AESUtils.encrypt2HexIz(Long.toString(nowTs)); nettyCookie.setHttpOnly(false);
webClientSession.cookieStore().put(nettyCookie);
// 检查并输出认证状态
if (shareLinkInfo.getOtherParam().containsKey("auths")) {
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
log.info("文件解析检测到认证信息: isTempAuth={}, authFlag={}, token={}",
isTempAuth, authFlag, token != null ? "已登录(" + token.substring(0, Math.min(10, token.length())) + "...)" : "未登录");
// 如果需要认证但还没有token,先执行登录
if ((isTempAuth || authFlag) && token == null) {
log.info("文件解析需要登录,开始执行登录流程...");
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
return login(tsEncode, auths)
.compose(v -> {
log.info("文件解析预登录成功,继续解析流程");
return parseWithAuth(shareId, tsEncode);
})
.onFailure(err -> {
log.warn("文件解析预登录失败: {},尝试使用免登录模式", err.getMessage());
// 登录失败,继续使用免登录模式
});
} else if (token != null) {
log.info("文件解析使用已有token: {}...", token.substring(0, Math.min(10, token.length())));
}
} else {
log.debug("文件解析无认证信息,使用免登录模式");
}
return parseWithAuth(shareId, tsEncode);
} }
private Future<String> parseWithAuth(String shareId, String tsEncode) { public Future<String> parse() {
// 24.5.12 飞机盘 规则修改 需要固定UUID先请求会员接口, 再请求后续接口 String shareId = shareLinkInfo.getShareKey();
// 24.5.12 ilanzou改规则无需计算shareId
// String shareId = String.valueOf(AESUtils.idEncryptIz(dataKey));
// 第一次请求 获取文件信息
// POST https://api.ilanzou.com/ws/recommend/list?devType=6&devModel=Chrome&extra=2&shareId=146731&type=0&offset=1&limit=60
String url = StringUtils.isBlank(shareLinkInfo.getSharePassword()) ? FIRST_REQUEST_URL
: (FIRST_REQUEST_URL + "&code=" + shareLinkInfo.getSharePassword());
webClientSession.postAbs(UriTemplate.of(VIP_REQUEST_URL)) webClientSession.postAbs(UriTemplate.of(VIP_REQUEST_URL))
.setTemplateParam("uuid", uuid) .setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode) .setTemplateParam("ts", tsEncode)
.send().onSuccess(r0 -> { // 忽略res .send().onSuccess(r0 -> { // 忽略res
String url = StringUtils.isBlank(shareLinkInfo.getSharePassword()) ? FIRST_REQUEST_URL
: (FIRST_REQUEST_URL + "&code=" + shareLinkInfo.getSharePassword());
// 第一次请求 获取文件信息 // 第一次请求 获取文件信息
// POST https://api.feijipan.com/ws/recommend/list?devType=6&devModel=Chrome&extra=2&shareId=146731&type=0&offset=1&limit=60 // POST https://api.feijipan.com/ws/recommend/list?devType=6&devModel=Chrome&extra=2&shareId=146731&type=0&offset=1&limit=60
webClientSession.postAbs(UriTemplate.of(url)) webClientSession.postAbs(UriTemplate.of(url))
@@ -156,43 +121,36 @@ public class IzTool extends PanBase {
.setTemplateParam("uuid", uuid) .setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode) .setTemplateParam("ts", tsEncode)
.send().onSuccess(res2 -> { .send().onSuccess(res2 -> {
processFirstResponse(res2); handleParseResponse(asText(res2), shareId);
}).onFailure(handleFail("请求1-重试")); }).onFailure(handleFail(FIRST_REQUEST_URL));
return; return;
} }
processFirstResponse(res); handleParseResponse(resBody, shareId);
}).onFailure(handleFail("请求1")); }).onFailure(handleFail(FIRST_REQUEST_URL));
});
}).onFailure(handleFail("请求1"));
return promise.future(); return promise.future();
} }
/** private void handleParseResponse(String resBody, String shareId) {
* 设置 cookie JsonObject resJson;
*/ try {
private void setCookie(String html) { resJson = new JsonObject(resBody);
int beginIndex = html.indexOf("arg1='") + 6; } catch (Exception e) {
String arg1 = html.substring(beginIndex, html.indexOf("';", beginIndex)); fail(FIRST_REQUEST_URL + " 解析JSON失败: " + resBody);
String acw_sc__v2 = AcwScV2Generator.acwScV2Simple(arg1); return;
// 创建一个 Cookie 并放入 CookieStore }
DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2); if (resJson.isEmpty()) {
nettyCookie.setDomain(".ilanzou.com"); // 设置域名 fail(FIRST_REQUEST_URL + " 返回内容为空");
nettyCookie.setPath("/"); // 设置路径 return;
nettyCookie.setSecure(false); }
nettyCookie.setHttpOnly(false);
webClientSession.cookieStore().put(nettyCookie);
}
/**
* 处理第一次请求的响应
*/
private void processFirstResponse(HttpResponse<Buffer> res) {
JsonObject resJson = asJson(res);
if (resJson.getInteger("code") != 200) { if (resJson.getInteger("code") != 200) {
fail(FIRST_REQUEST_URL + " 返回异常: " + resJson); fail(FIRST_REQUEST_URL + " 返回异常: " + resJson);
return; return;
} }
if (resJson.getJsonArray("list").isEmpty()) {
fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson);
return;
}
if (!resJson.containsKey("list") || resJson.getJsonArray("list").isEmpty()) { if (!resJson.containsKey("list") || resJson.getJsonArray("list").isEmpty()) {
fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson); fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson);
return; return;
@@ -209,251 +167,30 @@ public class IzTool extends PanBase {
promise.complete(fileList.getInteger("folderId").toString()); promise.complete(fileList.getInteger("folderId").toString());
return; return;
} }
// 提取文件信息
extractFileInfo(fileList, fileInfo);
getDownURL(resJson);
}
private void getDownURL(JsonObject resJson) {
String dataKey = shareLinkInfo.getShareKey();
// 文件Id
JsonObject fileInfo = resJson.getJsonArray("list").getJsonObject(0);
String fileId = fileInfo.getString("fileIds"); String fileId = fileInfo.getString("fileIds");
String userId = fileInfo.getString("userId"); String userId = fileInfo.getString("userId");
// 其他参数 // 其他参数
long nowTs2 = System.currentTimeMillis(); // String fidEncode = AESUtils.encrypt2HexIz(fileId + "|");
String tsEncode2 = AESUtils.encrypt2HexIz(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2HexIz(fileId + "|" + userId); String fidEncode = AESUtils.encrypt2HexIz(fileId + "|" + userId);
String auth = AESUtils.encrypt2HexIz(fileId + "|" + nowTs2); String auth = AESUtils.encrypt2HexIz(fileId + "|" + nowTs);
// 第二次请求
// 检查是否有认证信息 webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
if (shareLinkInfo.getOtherParam().containsKey("auths")) { .setTemplateParam("fidEncode", fidEncode)
// 检查是否为临时认证(临时认证每次都尝试登录) .setTemplateParam("uuid", uuid)
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED"); .setTemplateParam("ts", tsEncode)
// 如果是临时认证,或者是后台配置且authFlag为true,则尝试使用认证 .setTemplateParam("auth", auth)
if (isTempAuth || authFlag) { .setTemplateParam("shareId", shareId)
log.debug("尝试使用认证信息解析, isTempAuth={}, authFlag={}", isTempAuth, authFlag); .putHeaders(header).send().onSuccess(res2 -> {
HttpRequest<Buffer> httpRequest = MultiMap headers = res2.headers();
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL_VIP)) if (!headers.contains("Location")) {
.setTemplateParam("fidEncode", fidEncode) fail(SECOND_REQUEST_URL + " 未找到重定向URL: \n" + headers);
.setTemplateParam("uuid", uuid) return;
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey);
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
if (token == null) {
// 执行登录
login(tsEncode2, auths).onFailure(failRes-> {
log.warn("登录失败: {}", failRes.getMessage());
fail(failRes.getMessage());
}).onSuccess(r-> {
httpRequest.setTemplateParam("appToken", header.get("appToken"))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
});
} else {
// 验证token
webClientSession.postAbs(UriTemplate.of(TOKEN_VERIFY_URL))
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header).send().onSuccess(res -> {
// log.info("res: {}",asJson(res));
if (asJson(res).getInteger("code") != 200) {
login(tsEncode2, auths).onFailure(failRes -> {
log.warn("重新登录失败: {}", failRes.getMessage());
fail(failRes.getMessage());
}).onSuccess(r-> {
httpRequest.setTemplateParam("appToken", header.get("appToken"))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
});
} else {
httpRequest.setTemplateParam("appToken", header.get("appToken"))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
}
}).onFailure(handleFail("Token验证"));
}
} else {
// authFlag 为 false,使用免登录解析
log.debug("authFlag=false,使用免登录解析");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey).send()
.onSuccess(this::down).onFailure(handleFail("请求2"));
}
} else {
// 没有认证信息,使用免登录解析
log.debug("无认证信息,使用免登录解析");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey).send()
.onSuccess(this::down).onFailure(handleFail("请求2"));
}
}
private Future<Void> login(String tsEncode2, MultiMap auths) {
Promise<Void> promise1 = Promise.promise();
webClientSession.postAbs(UriTemplate.of(LOGIN_URL))
.setTemplateParam("uuid",uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header)
.sendJsonObject(JsonObject.of("loginName", auths.get("username"), "loginPwd", auths.get("password")))
.onSuccess(res2->{
JsonObject json = asJson(res2);
if (json.getInteger("code") == 200) {
token = json.getJsonObject("data").getString("appToken");
header.set("appToken", token);
log.info("登录成功 token: {}", token);
promise1.complete();
} else {
// 检查是否为临时认证
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
if (isTempAuth) {
// 临时认证失败,直接返回错误,不影响后台配置的认证
log.warn("临时认证失败: {}", json.getString("msg"));
promise1.fail("临时认证失败: " + json.getString("msg"));
} else {
// 后台配置的认证失败,设置authFlag并返回失败,让下次请求使用免登陆解析
log.warn("后台配置认证失败: {}, authFlag将设为false,请重新解析", json.getString("msg"));
authFlag = false;
promise1.fail("认证失败: " + json.getString("msg") + ", 请重新解析将使用免登陆模式");
}
} }
}).onFailure(err -> { promise.complete(headers.get("Location"));
log.error("登录请求异常: {}", err.getMessage()); }).onFailure(handleFail(SECOND_REQUEST_URL));
promise1.fail("登录请求异常: " + err.getMessage());
});
return promise1.future();
} }
/**
* 从接口返回数据中提取文件信息
*/
private void extractFileInfo(JsonObject fileList, JsonObject shareInfo) {
try {
// 文件名
String fileName = fileList.getString("fileName");
shareLinkInfo.getOtherParam().put("fileName", fileName);
// 文件大小 (KB -> Bytes)
Long fileSize = fileList.getLong("fileSize", 0L) * 1024;
shareLinkInfo.getOtherParam().put("fileSize", fileSize);
shareLinkInfo.getOtherParam().put("fileSizeFormat", FileSizeConverter.convertToReadableSize(fileSize));
// 文件图标
String fileIcon = fileList.getString("fileIcon");
if (StringUtils.isNotBlank(fileIcon)) {
shareLinkInfo.getOtherParam().put("fileIcon", fileIcon);
}
// 文件ID
Long fileId = fileList.getLong("fileId");
if (fileId != null) {
shareLinkInfo.getOtherParam().put("fileId", fileId.toString());
}
// 文件类型 (1=文件, 2=目录)
Integer fileType = fileList.getInteger("fileType", 1);
shareLinkInfo.getOtherParam().put("fileType", fileType == 1 ? "file" : "folder");
// 下载次数
Integer downloads = fileList.getInteger("fileDownloads", 0);
shareLinkInfo.getOtherParam().put("downloadCount", downloads);
// 点赞数
Integer likes = fileList.getInteger("fileLikes", 0);
shareLinkInfo.getOtherParam().put("likeCount", likes);
// 评论数
Integer comments = fileList.getInteger("fileComments", 0);
shareLinkInfo.getOtherParam().put("commentCount", comments);
// 评分
Double stars = fileList.getDouble("fileStars", 0.0);
shareLinkInfo.getOtherParam().put("stars", stars);
// 更新时间
String updateTime = fileList.getString("updTime");
if (StringUtils.isNotBlank(updateTime)) {
shareLinkInfo.getOtherParam().put("updateTime", updateTime);
}
// 创建时间
String createTime = null;
// 分享信息
if (shareInfo != null) {
// 分享ID
Integer shareId = shareInfo.getInteger("shareId");
if (shareId != null) {
shareLinkInfo.getOtherParam().put("shareId", shareId.toString());
}
// 上传时间
String addTime = shareInfo.getString("addTime");
if (StringUtils.isNotBlank(addTime)) {
shareLinkInfo.getOtherParam().put("createTime", addTime);
createTime = addTime;
}
// 预览次数
Integer previewNum = shareInfo.getInteger("previewNum", 0);
shareLinkInfo.getOtherParam().put("previewCount", previewNum);
// 用户信息
JsonObject userMap = shareInfo.getJsonObject("map");
if (userMap != null) {
String userName = userMap.getString("userName");
if (StringUtils.isNotBlank(userName)) {
shareLinkInfo.getOtherParam().put("userName", userName);
}
// VIP信息
Integer isVip = userMap.getInteger("isVip", 0);
shareLinkInfo.getOtherParam().put("isVip", isVip == 1);
}
}
// 创建 FileInfo 对象并存入 otherParam
FileInfo fileInfoObj = new FileInfo()
.setPanType(shareLinkInfo.getType())
.setFileName(fileName)
.setFileId(fileList.getLong("fileId") != null ? fileList.getLong("fileId").toString() : null)
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setFileType(fileType == 1 ? "file" : "folder")
.setFileIcon(fileList.getString("fileIcon"))
.setDownloadCount(downloads)
.setCreateTime(createTime)
.setUpdateTime(updateTime);
shareLinkInfo.getOtherParam().put("fileInfo", fileInfoObj);
log.debug("提取文件信息成功: fileName={}, fileSize={}, downloads={}",
fileName, fileSize, downloads);
} catch (Exception e) {
log.warn("提取文件信息失败: {}", e.getMessage());
}
}
private void down(HttpResponse<Buffer> res2) {
MultiMap headers = res2.headers();
if (!headers.contains("Location") || StringUtils.isBlank(headers.get("Location"))) {
fail("找不到下载链接可能服务器已被禁止或者配置的认证信息有误");
return;
}
promise.complete(headers.get("Location"));
}
// 目录解析
@Override @Override
public Future<List<FileInfo>> parseFileList() { public Future<List<FileInfo>> parseFileList() {
Promise<List<FileInfo>> promise = Promise.promise(); Promise<List<FileInfo>> promise = Promise.promise();
@@ -468,7 +205,11 @@ public class IzTool extends PanBase {
return promise.future(); return promise.future();
} }
parse().onSuccess(id -> { parse().onSuccess(id -> {
parserDir(id, shareId, promise); if (id != null && id.matches("^[a-zA-Z0-9]+$")) {
parserDir(id, shareId, promise);
} else {
promise.fail("解析目录ID失败");
}
}).onFailure(failRes -> { }).onFailure(failRes -> {
log.error("解析目录失败: {}", failRes.getMessage()); log.error("解析目录失败: {}", failRes.getMessage());
promise.fail(failRes); promise.fail(failRes);
@@ -477,22 +218,6 @@ public class IzTool extends PanBase {
} }
private void parserDir(String id, String shareId, Promise<List<FileInfo>> promise) { private void parserDir(String id, String shareId, Promise<List<FileInfo>> promise) {
if (id != null && (id.startsWith("http://") || id.startsWith("https://"))) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(id)
.setFileId(id)
.setFileType("file")
.setParserUrl(id)
.setPanType(shareLinkInfo.getType());
List<FileInfo> result = new ArrayList<>();
result.add(fileInfo);
promise.complete(result);
return;
}
long nowTs = System.currentTimeMillis();
String tsEncode = AESUtils.encrypt2HexIz(Long.toString(nowTs));
log.debug("开始解析目录: {}, shareId: {}, uuid: {}, ts: {}", id, shareId, uuid, tsEncode); log.debug("开始解析目录: {}, shareId: {}, uuid: {}, ts: {}", id, shareId, uuid, tsEncode);
// 开始解析目录: 164312216, shareId: bPMsbg5K, uuid: 0fmVWTx2Ea4zFwkpd7KXf, ts: 20865d7b7f00828279f437cd1f097860 // 开始解析目录: 164312216, shareId: bPMsbg5K, uuid: 0fmVWTx2Ea4zFwkpd7KXf, ts: 20865d7b7f00828279f437cd1f097860
// 拿到目录ID // 拿到目录ID
@@ -503,134 +228,103 @@ public class IzTool extends PanBase {
.setTemplateParam("ts", tsEncode) .setTemplateParam("ts", tsEncode)
.setTemplateParam("folderId", id) .setTemplateParam("folderId", id)
.send().onSuccess(res -> { .send().onSuccess(res -> {
String resBody = asText(res); JsonObject jsonObject;
// 检查是否包含 cookie 验证 try {
if (resBody.contains("var arg1='")) { jsonObject = asJson(res);
log.debug("目录解析需要 cookie 验证,重新创建 session"); } catch (Exception e) {
webClientSession = WebClientSession.create(clientNoRedirects); promise.fail(FIRST_REQUEST_URL + " 解析JSON失败: " + res.bodyAsString());
setCookie(resBody);
// 重新请求目录列表
webClientSession.postAbs(UriTemplate.of(FILE_LIST_URL))
.putHeaders(header)
.setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode)
.setTemplateParam("folderId", id)
.send().onSuccess(res2 -> {
processDirResponse(res2, shareId, promise);
}).onFailure(err -> {
log.error("目录解析重试失败: {}", err.getMessage());
promise.fail("目录解析失败: " + err.getMessage());
});
return; return;
} }
processDirResponse(res, shareId, promise); // System.out.println(jsonObject.encodePrettily());
}).onFailure(err -> { JsonArray list = jsonObject.getJsonArray("list");
log.error("目录解析请求失败: {}", err.getMessage()); ArrayList<FileInfo> result = new ArrayList<>();
promise.fail("目录解析失败: " + err.getMessage()); list.forEach(item->{
JsonObject fileJson = (JsonObject) item;
FileInfo fileInfo = new FileInfo();
// 映射已知字段
String fileId = fileJson.getString("fileId");
String userId = fileJson.getString("userId");
// 回传用到的参数
//"fidEncode", paramJson.getString("fidEncode"))
//"uuid", paramJson.getString("uuid"))
//"ts", paramJson.getString("ts"))
//"auth", paramJson.getString("auth"))
//"shareId", paramJson.getString("shareId"))
String fidEncode = AESUtils.encrypt2HexIz(fileId + "|" + userId);
String auth = AESUtils.encrypt2HexIz(fileId + "|" + nowTs);
JsonObject entries = JsonObject.of(
"fidEncode", fidEncode,
"uuid", uuid,
"ts", tsEncode,
"auth", auth,
"shareId", shareId);
byte[] encode = Base64.getEncoder().encode(entries.encode().getBytes());
String param = new String(encode);
if (fileJson.getInteger("fileType") == 2) {
// 如果是目录
fileInfo.setFileName(fileJson.getString("name"))
.setFileId(fileJson.getString("folderId"))
.setCreateTime(fileJson.getString("updTime"))
.setFileType("folder")
.setSize(0L)
.setSizeStr("0B")
.setCreateBy(fileJson.getLong("userId").toString())
.setDownloadCount(fileJson.getInteger("fileDownloads"))
.setCreateTime(fileJson.getString("updTime"))
.setFileIcon(fileJson.getString("fileIcon"))
.setPanType(shareLinkInfo.getType())
// 设置目录解析的URL
.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&uuid=%s", getDomainName(),
shareLinkInfo.getShareUrl(), fileJson.getString("folderId"), uuid));
result.add(fileInfo);
return;
}
long fileSize = fileJson.getLong("fileSize") * 1024;
fileInfo.setFileName(fileJson.getString("fileName"))
.setFileId(fileId)
.setCreateTime(fileJson.getString("createTime"))
.setFileType("file")
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setCreateBy(fileJson.getLong("userId").toString())
.setDownloadCount(fileJson.getInteger("fileDownloads"))
.setCreateTime(fileJson.getString("updTime"))
.setFileIcon(fileJson.getString("fileIcon"))
.setPanType(shareLinkInfo.getType())
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(),
shareLinkInfo.getType(), param))
.setPreviewUrl(String.format("%s/v2/viewUrl/%s/%s", getDomainName(),
shareLinkInfo.getType(), param));
result.add(fileInfo);
});
promise.complete(result);
}).onFailure(failRes -> {
log.error("解析目录请求失败: {}", failRes.getMessage());
promise.fail(failRes);
}); });
} }
/**
* 处理目录解析响应
*/
private void processDirResponse(HttpResponse<Buffer> res, String shareId, Promise<List<FileInfo>> promise) {
try {
JsonObject jsonObject = asJson(res);
log.debug("目录解析响应: {}", jsonObject.encodePrettily());
if (!jsonObject.containsKey("list")) {
log.error("目录解析响应缺少 list 字段: {}", jsonObject);
promise.fail("目录解析失败: 响应格式错误");
return;
}
JsonArray list = jsonObject.getJsonArray("list");
ArrayList<FileInfo> result = new ArrayList<>();
list.forEach(item->{
JsonObject fileJson = (JsonObject) item;
FileInfo fileInfo = new FileInfo();
// 映射已知字段
String fileId = fileJson.getString("fileId");
String userId = fileJson.getString("userId");
// 其他参数 - 每个文件使用新的时间戳
long nowTs2 = System.currentTimeMillis();
String tsEncode2 = AESUtils.encrypt2HexIz(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2HexIz(fileId + "|" + userId);
String auth = AESUtils.encrypt2HexIz(fileId + "|" + nowTs2);
// 回传用到的参数
JsonObject entries = JsonObject.of(
"fidEncode", fidEncode,
"uuid", uuid,
"ts", tsEncode2,
"auth", auth,
"shareId", shareId);
String param = CommonUtils.urlBase64Encode(entries.encode());
if (fileJson.getInteger("fileType") == 2) {
// 如果是目录
fileInfo.setFileName(fileJson.getString("name"))
.setFileId(fileJson.getString("folderId"))
.setCreateTime(fileJson.getString("updTime"))
.setFileType("folder")
.setSize(0L)
.setSizeStr("0B")
.setCreateBy(fileJson.getLong("userId").toString())
.setDownloadCount(fileJson.getInteger("fileDownloads"))
.setCreateTime(fileJson.getString("updTime"))
.setFileIcon(fileJson.getString("fileIcon"))
.setPanType(shareLinkInfo.getType())
// 设置目录解析的URL
.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&uuid=%s", getDomainName(),
shareLinkInfo.getShareUrl(), fileJson.getString("folderId"), uuid));
result.add(fileInfo);
return;
}
long fileSize = fileJson.getLong("fileSize") * 1024;
fileInfo.setFileName(fileJson.getString("fileName"))
.setFileId(fileId)
.setCreateTime(fileJson.getString("createTime"))
.setFileType("file")
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setCreateBy(fileJson.getLong("userId").toString())
.setDownloadCount(fileJson.getInteger("fileDownloads"))
.setCreateTime(fileJson.getString("updTime"))
.setFileIcon(fileJson.getString("fileIcon"))
.setPanType(shareLinkInfo.getType())
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(),
shareLinkInfo.getType(), param))
.setPreviewUrl(String.format("%s/v2/viewUrl/%s/%s", getDomainName(),
shareLinkInfo.getType(), param));
result.add(fileInfo);
});
promise.complete(result);
} catch (Exception e) {
log.error("处理目录响应异常: {}", e.getMessage(), e);
promise.fail("目录解析失败: " + e.getMessage());
}
}
@Override @Override
public Future<String> parseById() { public Future<String> parseById() {
JsonObject paramJson = (JsonObject) shareLinkInfo.getOtherParam().get("paramJson"); // 第二次请求
// 使用免登录接口 JsonObject paramJson = (JsonObject)shareLinkInfo.getOtherParam().get("paramJson");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL)) webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", paramJson.getString("fidEncode")) .setTemplateParam("fidEncode", paramJson.getString("fidEncode"))
.setTemplateParam("uuid", paramJson.getString("uuid")) .setTemplateParam("uuid", paramJson.getString("uuid"))
.setTemplateParam("ts", paramJson.getString("ts")) .setTemplateParam("ts", paramJson.getString("ts"))
.setTemplateParam("auth", paramJson.getString("auth")) .setTemplateParam("auth", paramJson.getString("auth"))
.setTemplateParam("dataKey", paramJson.getString("shareId")) .setTemplateParam("shareId", paramJson.getString("shareId"))
.send().onSuccess(this::down).onFailure(handleFail("parseById")); .putHeaders(header).send().onSuccess(res2 -> {
MultiMap headers = res2.headers();
if (!headers.contains("Location")) {
fail(SECOND_REQUEST_URL + " 未找到重定向URL: \n" + res2.headers());
return;
}
promise.complete(headers.get("Location"));
}).onFailure(handleFail(SECOND_REQUEST_URL));
return promise.future(); return promise.future();
} }
public static void resetToken() {
token = null;
authFlag = true;
}
} }
@@ -1,658 +0,0 @@
package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase;
import cn.qaiu.util.AESUtils;
import cn.qaiu.util.AcwScV2Generator;
import cn.qaiu.util.CommonUtils;
import cn.qaiu.util.FileSizeConverter;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClientSession;
import io.vertx.uritemplate.UriTemplate;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* 蓝奏云优享 - 需要登录版本(支持大文件)
*/
public class IzToolWithAuth extends PanBase {
private static final String API_URL0 = "https://api.ilanzou.com/";
private static final String API_URL_PREFIX = "https://api.ilanzou.com/unproved/";
private static final String FIRST_REQUEST_URL = API_URL_PREFIX + "recommend/list?devType=6&devModel=Chrome" +
"&uuid={uuid}&extra=2&timestamp={ts}&shareId={shareId}&type=0&offset=1&limit=60";
private static final String LOGIN_URL = API_URL_PREFIX +
"login?uuid={uuid}&devType=6&devCode={uuid}&devModel=chrome&devVersion=127&appVersion=&timestamp={ts}&appToken=&extra=2";
// https://api.ilanzou.com/proved/user/info/map?devType=3&devModel=Chrome&uuid=TInRHH3QzRaMo-Ajl2PkJ&extra=2&timestamp=EC2C6E7F45EB21338A17A7621E0BB437
private static final String TOKEN_VERIFY_URL = API_URL0 +
"proved/user/info/map?devType=6&devModel=Chrome&uuid={uuid}&extra=2&timestamp={ts}";
private static final String SECOND_REQUEST_URL = API_URL_PREFIX + "file/redirect?downloadId={fidEncode}&enable=1" +
"&devType=6&uuid={uuid}&timestamp={ts}&auth={auth}&shareId={dataKey}";
private static final String SECOND_REQUEST_URL_VIP = API_URL_PREFIX + "file/redirect?uuid={uuid}&devType=6&devCode={uuid}" +
"&devModel=chrome&devVersion=127&appVersion=&timestamp={ts}&appToken={appToken}&enable=1&downloadId={fidEncode}&auth={auth}";
private static final String VIP_REQUEST_URL = API_URL_PREFIX + "/buy/vip/list?devType=6&devModel=Chrome&uuid" +
"={uuid}&extra=2&timestamp={ts}";
private static final String FILE_LIST_URL = API_URL_PREFIX + "/share/list?devType=6&devModel=Chrome&uuid" +
"={uuid}&extra=2&timestamp={ts}&shareId={shareId}&folderId" +
"={folderId}&offset=1&limit=60";
WebClientSession webClientSession = WebClientSession.create(clientNoRedirects);
private static final MultiMap header;
static {
header = MultiMap.caseInsensitiveMultiMap();
header.set("Accept", "application/json, text/plain, */*");
header.set("Accept-Encoding", "gzip, deflate");
header.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
header.set("Cache-Control", "no-cache");
header.set("Connection", "keep-alive");
header.set("Content-Length", "0");
header.set("DNT", "1");
header.set("Host", "api.ilanzou.com");
header.set("Origin", "https://www.ilanzou.com/");
header.set("Pragma", "no-cache");
header.set("Referer", "https://www.ilanzou.com/");
header.set("Sec-Fetch-Dest", "empty");
header.set("Sec-Fetch-Mode", "cors");
header.set("Sec-Fetch-Site", "cross-site");
header.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
header.set("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
header.set("sec-ch-ua-mobile", "?0");
header.set("sec-ch-ua-platform", "\"Windows\"");
}
public IzToolWithAuth(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo);
}
String uuid = UUID.randomUUID().toString().toLowerCase(); // 也可以使用 UUID.randomUUID().toString()
public static String token = null;
public static boolean authFlag = true;
public Future<String> parse() {
String shareId = shareLinkInfo.getShareKey(); // String.valueOf(AESUtils.idEncrypt(dataKey));
long nowTs = System.currentTimeMillis();
String tsEncode = AESUtils.encrypt2HexIz(Long.toString(nowTs));
// 24.5.12 飞机盘 规则修改 需要固定UUID先请求会员接口, 再请求后续接口
webClientSession.postAbs(UriTemplate.of(VIP_REQUEST_URL))
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode)
.send().onSuccess(r0 -> { // 忽略res
String url = StringUtils.isBlank(shareLinkInfo.getSharePassword()) ? FIRST_REQUEST_URL
: (FIRST_REQUEST_URL + "&code=" + shareLinkInfo.getSharePassword());
// 第一次请求 获取文件信息
// POST https://api.feijipan.com/ws/recommend/list?devType=6&devModel=Chrome&extra=2&shareId=146731&type=0&offset=1&limit=60
webClientSession.postAbs(UriTemplate.of(url))
.putHeaders(header)
.setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode)
.send().onSuccess(res -> {
String resBody = asText(res);
// 检查是否包含 cookie 验证
if (resBody.contains("var arg1='")) {
webClientSession = WebClientSession.create(clientNoRedirects);
setCookie(resBody);
// 重新请求
webClientSession.postAbs(UriTemplate.of(url))
.putHeaders(header)
.setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode)
.send().onSuccess(res2 -> {
processFirstResponse(res2);
}).onFailure(handleFail("请求1-重试"));
return;
}
processFirstResponse(res);
}).onFailure(handleFail("请求1"));
}).onFailure(handleFail("请求1"));
return promise.future();
}
/**
* 设置 cookie
*/
private void setCookie(String html) {
int beginIndex = html.indexOf("arg1='") + 6;
String arg1 = html.substring(beginIndex, html.indexOf("';", beginIndex));
String acw_sc__v2 = AcwScV2Generator.acwScV2Simple(arg1);
// 创建一个 Cookie 并放入 CookieStore
DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2);
nettyCookie.setDomain(".ilanzou.com"); // 设置域名
nettyCookie.setPath("/"); // 设置路径
nettyCookie.setSecure(false);
nettyCookie.setHttpOnly(false);
webClientSession.cookieStore().put(nettyCookie);
}
/**
* 处理第一次请求的响应
*/
private void processFirstResponse(HttpResponse<Buffer> res) {
JsonObject resJson = asJson(res);
if (resJson.getInteger("code") != 200) {
fail(FIRST_REQUEST_URL + " 返回异常: " + resJson);
return;
}
if (!resJson.containsKey("list") || resJson.getJsonArray("list").isEmpty()) {
fail(FIRST_REQUEST_URL + " 解析文件列表为空: " + resJson);
return;
}
// 文件Id
JsonObject fileInfo = resJson.getJsonArray("list").getJsonObject(0);
// 如果是目录返回目录ID
if (!fileInfo.containsKey("fileList") || fileInfo.getJsonArray("fileList").isEmpty()) {
fail(FIRST_REQUEST_URL + " 文件列表为空: " + fileInfo);
return;
}
JsonObject fileList = fileInfo.getJsonArray("fileList").getJsonObject(0);
if (fileList.getInteger("fileType") == 2) {
promise.complete(fileList.getInteger("folderId").toString());
return;
}
// 提取文件信息
extractFileInfo(fileList, fileInfo);
getDownURL(resJson);
}
private void getDownURL(JsonObject resJson) {
String dataKey = shareLinkInfo.getShareKey();
// 文件Id
JsonObject fileInfo = resJson.getJsonArray("list").getJsonObject(0);
String fileId = fileInfo.getString("fileIds");
String userId = fileInfo.getString("userId");
// 其他参数
long nowTs2 = System.currentTimeMillis();
String tsEncode2 = AESUtils.encrypt2HexIz(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2HexIz(fileId + "|" + userId);
String auth = AESUtils.encrypt2HexIz(fileId + "|" + nowTs2);
// 检查是否有认证信息
if (shareLinkInfo.getOtherParam().containsKey("auths")) {
// 检查是否为临时认证(临时认证每次都尝试登录)
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
// 如果是临时认证,或者是后台配置且authFlag为true,则尝试使用认证
if (isTempAuth || authFlag) {
log.debug("尝试使用认证信息解析, isTempAuth={}, authFlag={}", isTempAuth, authFlag);
HttpRequest<Buffer> httpRequest =
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL_VIP))
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey);
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
if (token == null) {
// 执行登录
login(tsEncode2, auths).onFailure(failRes-> {
log.warn("登录失败: {}", failRes.getMessage());
fail(failRes.getMessage());
}).onSuccess(r-> {
httpRequest.setTemplateParam("appToken", header.get("appToken"))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
});
} else {
// 验证token
webClientSession.postAbs(UriTemplate.of(TOKEN_VERIFY_URL))
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header).send().onSuccess(res -> {
// log.info("res: {}",asJson(res));
if (asJson(res).getInteger("code") != 200) {
login(tsEncode2, auths).onFailure(failRes -> {
log.warn("重新登录失败: {}", failRes.getMessage());
fail(failRes.getMessage());
}).onSuccess(r-> {
httpRequest.setTemplateParam("appToken", header.get("appToken"))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
});
} else {
httpRequest.setTemplateParam("appToken", header.get("appToken"))
.putHeaders(header);
httpRequest.send().onSuccess(this::down).onFailure(handleFail("请求2"));
}
}).onFailure(handleFail("Token验证"));
}
} else {
// authFlag 为 false,使用免登录解析
log.debug("authFlag=false,使用免登录解析");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey).send()
.onSuccess(this::down).onFailure(handleFail("请求2"));
}
} else {
// 没有认证信息,使用免登录解析
log.debug("无认证信息,使用免登录解析");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", fidEncode)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode2)
.setTemplateParam("auth", auth)
.setTemplateParam("dataKey", dataKey).send()
.onSuccess(this::down).onFailure(handleFail("请求2"));
}
}
private Future<Void> login(String tsEncode2, MultiMap auths) {
Promise<Void> promise1 = Promise.promise();
webClientSession.postAbs(UriTemplate.of(LOGIN_URL))
.setTemplateParam("uuid",uuid)
.setTemplateParam("ts", tsEncode2)
.putHeaders(header)
.sendJsonObject(JsonObject.of("loginName", auths.get("username"), "loginPwd", auths.get("password")))
.onSuccess(res2->{
JsonObject json = asJson(res2);
if (json.getInteger("code") == 200) {
token = json.getJsonObject("data").getString("appToken");
header.set("appToken", token);
log.info("登录成功 token: {}", token);
promise1.complete();
} else {
// 检查是否为临时认证
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
if (isTempAuth) {
// 临时认证失败,直接返回错误,不影响后台配置的认证
log.warn("临时认证失败: {}", json.getString("msg"));
promise1.fail("临时认证失败: " + json.getString("msg"));
} else {
// 后台配置的认证失败,设置authFlag并返回失败,让下次请求使用免登陆解析
log.warn("后台配置认证失败: {}, authFlag将设为false,请重新解析", json.getString("msg"));
authFlag = false;
promise1.fail("认证失败: " + json.getString("msg") + ", 请重新解析将使用免登陆模式");
}
}
}).onFailure(err -> {
log.error("登录请求异常: {}", err.getMessage());
promise1.fail("登录请求异常: " + err.getMessage());
});
return promise1.future();
}
/**
* 从接口返回数据中提取文件信息
*/
private void extractFileInfo(JsonObject fileList, JsonObject shareInfo) {
try {
// 文件名
String fileName = fileList.getString("fileName");
shareLinkInfo.getOtherParam().put("fileName", fileName);
// 文件大小 (KB -> Bytes)
Long fileSize = fileList.getLong("fileSize", 0L) * 1024;
shareLinkInfo.getOtherParam().put("fileSize", fileSize);
shareLinkInfo.getOtherParam().put("fileSizeFormat", FileSizeConverter.convertToReadableSize(fileSize));
// 文件图标
String fileIcon = fileList.getString("fileIcon");
if (StringUtils.isNotBlank(fileIcon)) {
shareLinkInfo.getOtherParam().put("fileIcon", fileIcon);
}
// 文件ID
Long fileId = fileList.getLong("fileId");
if (fileId != null) {
shareLinkInfo.getOtherParam().put("fileId", fileId.toString());
}
// 文件类型 (1=文件, 2=目录)
Integer fileType = fileList.getInteger("fileType", 1);
shareLinkInfo.getOtherParam().put("fileType", fileType == 1 ? "file" : "folder");
// 下载次数
Integer downloads = fileList.getInteger("fileDownloads", 0);
shareLinkInfo.getOtherParam().put("downloadCount", downloads);
// 点赞数
Integer likes = fileList.getInteger("fileLikes", 0);
shareLinkInfo.getOtherParam().put("likeCount", likes);
// 评论数
Integer comments = fileList.getInteger("fileComments", 0);
shareLinkInfo.getOtherParam().put("commentCount", comments);
// 评分
Double stars = fileList.getDouble("fileStars", 0.0);
shareLinkInfo.getOtherParam().put("stars", stars);
// 更新时间
String updateTime = fileList.getString("updTime");
if (StringUtils.isNotBlank(updateTime)) {
shareLinkInfo.getOtherParam().put("updateTime", updateTime);
}
// 创建时间
String createTime = null;
// 分享信息
if (shareInfo != null) {
// 分享ID
Integer shareId = shareInfo.getInteger("shareId");
if (shareId != null) {
shareLinkInfo.getOtherParam().put("shareId", shareId.toString());
}
// 上传时间
String addTime = shareInfo.getString("addTime");
if (StringUtils.isNotBlank(addTime)) {
shareLinkInfo.getOtherParam().put("createTime", addTime);
createTime = addTime;
}
// 预览次数
Integer previewNum = shareInfo.getInteger("previewNum", 0);
shareLinkInfo.getOtherParam().put("previewCount", previewNum);
// 用户信息
JsonObject userMap = shareInfo.getJsonObject("map");
if (userMap != null) {
String userName = userMap.getString("userName");
if (StringUtils.isNotBlank(userName)) {
shareLinkInfo.getOtherParam().put("userName", userName);
}
// VIP信息
Integer isVip = userMap.getInteger("isVip", 0);
shareLinkInfo.getOtherParam().put("isVip", isVip == 1);
}
}
// 创建 FileInfo 对象并存入 otherParam
FileInfo fileInfoObj = new FileInfo()
.setPanType(shareLinkInfo.getType())
.setFileName(fileName)
.setFileId(fileList.getLong("fileId") != null ? fileList.getLong("fileId").toString() : null)
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setFileType(fileType == 1 ? "file" : "folder")
.setFileIcon(fileList.getString("fileIcon"))
.setDownloadCount(downloads)
.setCreateTime(createTime)
.setUpdateTime(updateTime);
shareLinkInfo.getOtherParam().put("fileInfo", fileInfoObj);
log.debug("提取文件信息成功: fileName={}, fileSize={}, downloads={}",
fileName, fileSize, downloads);
} catch (Exception e) {
log.warn("提取文件信息失败: {}", e.getMessage());
}
}
private void down(HttpResponse<Buffer> res2) {
MultiMap headers = res2.headers();
if (!headers.contains("Location") || StringUtils.isBlank(headers.get("Location"))) {
fail("找不到下载链接可能服务器已被禁止或者配置的认证信息有误");
return;
}
promise.complete(headers.get("Location"));
}
// 目录解析
@Override
public Future<List<FileInfo>> parseFileList() {
Promise<List<FileInfo>> promise = Promise.promise();
String shareId = shareLinkInfo.getShareKey(); // String.valueOf(AESUtils.idEncrypt(dataKey));
// 如果参数里的目录ID不为空,则直接解析目录
String dirId = (String) shareLinkInfo.getOtherParam().get("dirId");
if (dirId != null && !dirId.isEmpty()) {
uuid = shareLinkInfo.getOtherParam().get("uuid").toString();
parserDir(dirId, shareId, promise);
return promise.future();
}
parse().onSuccess(id -> {
parserDir(id, shareId, promise);
}).onFailure(failRes -> {
log.error("解析目录失败: {}", failRes.getMessage());
promise.fail(failRes);
});
return promise.future();
}
private void parserDir(String id, String shareId, Promise<List<FileInfo>> promise) {
if (id != null && (id.startsWith("http://") || id.startsWith("https://"))) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(id)
.setFileId(id)
.setFileType("file")
.setParserUrl(id)
.setPanType(shareLinkInfo.getType());
List<FileInfo> result = new ArrayList<>();
result.add(fileInfo);
promise.complete(result);
return;
}
long nowTs = System.currentTimeMillis();
String tsEncode = AESUtils.encrypt2HexIz(Long.toString(nowTs));
log.debug("开始解析目录: {}, shareId: {}, uuid: {}, ts: {}", id, shareId, uuid, tsEncode);
// 检查是否需要登录(有认证信息且需要使用认证)
if (shareLinkInfo.getOtherParam().containsKey("auths")) {
boolean isTempAuth = shareLinkInfo.getOtherParam().containsKey("__TEMP_AUTH_ADDED");
log.debug("目录解析检查认证: isTempAuth={}, authFlag={}, token={}", isTempAuth, authFlag, token != null ? "已有" : "null");
if ((isTempAuth || authFlag) && token == null) {
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
log.info("目录解析需要登录,开始执行登录...");
// 先登录获取 token
login(tsEncode, auths)
.onFailure(err -> {
log.warn("目录解析登录失败,使用免登录模式: {}", err.getMessage());
// 登录失败,继续使用免登录
requestDirList(id, shareId, tsEncode, promise);
})
.onSuccess(r -> {
log.info("目录解析登录成功,token={}, 使用 VIP 模式", token != null ? token.substring(0, 10) + "..." : "null");
requestDirList(id, shareId, tsEncode, promise);
});
return;
} else if (token != null) {
log.debug("目录解析已有 token,直接使用 VIP 模式");
} else {
log.debug("目录解析: authFlag=false 或为临时认证但已失败,使用免登录模式");
}
} else {
log.debug("目录解析无认证信息,使用免登录模式");
}
// 无需登录或已登录,直接请求
requestDirList(id, shareId, tsEncode, promise);
}
/**
* 请求目录列表
*/
private void requestDirList(String id, String shareId, String tsEncode, Promise<List<FileInfo>> promise) {
webClientSession.postAbs(UriTemplate.of(FILE_LIST_URL))
.putHeaders(header)
.setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode)
.setTemplateParam("folderId", id)
.send().onSuccess(res -> {
String resBody = asText(res);
// 检查是否包含 cookie 验证
if (resBody.contains("var arg1='")) {
log.debug("目录解析需要 cookie 验证,重新创建 session");
webClientSession = WebClientSession.create(clientNoRedirects);
setCookie(resBody);
// 重新请求目录列表
webClientSession.postAbs(UriTemplate.of(FILE_LIST_URL))
.putHeaders(header)
.setTemplateParam("shareId", shareId)
.setTemplateParam("uuid", uuid)
.setTemplateParam("ts", tsEncode)
.setTemplateParam("folderId", id)
.send().onSuccess(res2 -> {
processDirResponse(res2, shareId, promise);
}).onFailure(err -> {
log.error("目录解析重试失败: {}", err.getMessage());
promise.fail("目录解析失败: " + err.getMessage());
});
return;
}
processDirResponse(res, shareId, promise);
}).onFailure(err -> {
log.error("目录解析请求失败: {}", err.getMessage());
promise.fail("目录解析失败: " + err.getMessage());
});
}
/**
* 处理目录解析响应
*/
private void processDirResponse(HttpResponse<Buffer> res, String shareId, Promise<List<FileInfo>> promise) {
try {
JsonObject jsonObject = asJson(res);
log.debug("目录解析响应: {}", jsonObject.encodePrettily());
if (!jsonObject.containsKey("list")) {
log.error("目录解析响应缺少 list 字段: {}", jsonObject);
promise.fail("目录解析失败: 响应格式错误");
return;
}
JsonArray list = jsonObject.getJsonArray("list");
ArrayList<FileInfo> result = new ArrayList<>();
list.forEach(item->{
JsonObject fileJson = (JsonObject) item;
FileInfo fileInfo = new FileInfo();
// 映射已知字段
String fileId = fileJson.getString("fileId");
String userId = fileJson.getString("userId");
// 其他参数 - 每个文件使用新的时间戳
long nowTs2 = System.currentTimeMillis();
String tsEncode2 = AESUtils.encrypt2HexIz(Long.toString(nowTs2));
String fidEncode = AESUtils.encrypt2HexIz(fileId + "|" + userId);
String auth = AESUtils.encrypt2HexIz(fileId + "|" + nowTs2);
// 回传用到的参数(包含 token)
JsonObject entries = JsonObject.of(
"fidEncode", fidEncode,
"uuid", uuid,
"ts", tsEncode2,
"auth", auth,
"shareId", shareId,
"appToken", token != null ? token : "");
String param = CommonUtils.urlBase64Encode(entries.encode());
if (fileJson.getInteger("fileType") == 2) {
// 如果是目录
fileInfo.setFileName(fileJson.getString("name"))
.setFileId(fileJson.getString("folderId"))
.setCreateTime(fileJson.getString("updTime"))
.setFileType("folder")
.setSize(0L)
.setSizeStr("0B")
.setCreateBy(fileJson.getLong("userId").toString())
.setDownloadCount(fileJson.getInteger("fileDownloads"))
.setCreateTime(fileJson.getString("updTime"))
.setFileIcon(fileJson.getString("fileIcon"))
.setPanType(shareLinkInfo.getType())
// 设置目录解析的URL
.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&uuid=%s", getDomainName(),
shareLinkInfo.getShareUrl(), fileJson.getString("folderId"), uuid));
result.add(fileInfo);
return;
}
long fileSize = fileJson.getLong("fileSize") * 1024;
fileInfo.setFileName(fileJson.getString("fileName"))
.setFileId(fileId)
.setCreateTime(fileJson.getString("createTime"))
.setFileType("file")
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setCreateBy(fileJson.getLong("userId").toString())
.setDownloadCount(fileJson.getInteger("fileDownloads"))
.setCreateTime(fileJson.getString("updTime"))
.setFileIcon(fileJson.getString("fileIcon"))
.setPanType(shareLinkInfo.getType())
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(),
shareLinkInfo.getType(), param))
.setPreviewUrl(String.format("%s/v2/viewUrl/%s/%s", getDomainName(),
shareLinkInfo.getType(), param));
result.add(fileInfo);
});
promise.complete(result);
} catch (Exception e) {
log.error("处理目录响应异常: {}", e.getMessage(), e);
promise.fail("目录解析失败: " + e.getMessage());
}
}
@Override
public Future<String> parseById() {
JsonObject paramJson = (JsonObject) shareLinkInfo.getOtherParam().get("paramJson");
String appToken = paramJson.getString("appToken", "");
// 如果有 token,使用 VIP 接口
if (StringUtils.isNotBlank(appToken)) {
log.debug("parseById 使用 VIP 接口, appToken={}", appToken.substring(0, Math.min(10, appToken.length())) + "...");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL_VIP))
.putHeaders(header)
.setTemplateParam("fidEncode", paramJson.getString("fidEncode"))
.setTemplateParam("uuid", paramJson.getString("uuid"))
.setTemplateParam("ts", paramJson.getString("ts"))
.setTemplateParam("auth", paramJson.getString("auth"))
.setTemplateParam("appToken", appToken)
.send().onSuccess(this::down).onFailure(handleFail("parseById-VIP"));
} else {
// 无 token,使用免登录接口
log.debug("parseById 使用免登录接口");
webClientSession.getAbs(UriTemplate.of(SECOND_REQUEST_URL))
.putHeaders(header)
.setTemplateParam("fidEncode", paramJson.getString("fidEncode"))
.setTemplateParam("uuid", paramJson.getString("uuid"))
.setTemplateParam("ts", paramJson.getString("ts"))
.setTemplateParam("auth", paramJson.getString("auth"))
.setTemplateParam("dataKey", paramJson.getString("shareId"))
.send().onSuccess(this::down).onFailure(handleFail("parseById"));
}
return promise.future();
}
public static void resetToken() {
token = null;
authFlag = true;
}
}
@@ -64,7 +64,7 @@ public class LzTool extends PanBase {
String html = asText(res); String html = asText(res);
if (html.contains("var arg1='")) { if (html.contains("var arg1='")) {
webClientSession = WebClientSession.create(clientNoRedirects); webClientSession = WebClientSession.create(clientNoRedirects);
setCookie(html, sUrl); setCookie(html);
webClientSession.getAbs(sUrl) webClientSession.getAbs(sUrl)
.putHeaders(headers0) .putHeaders(headers0)
.send().onSuccess(res2 -> { .send().onSuccess(res2 -> {
@@ -81,29 +81,6 @@ public class LzTool extends PanBase {
} }
private void doParser(String html, String pwd, String sUrl) { 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 { try {
setFileInfo(html, shareLinkInfo); setFileInfo(html, shareLinkInfo);
} catch (Exception e) { } catch (Exception e) {
@@ -121,18 +98,20 @@ public class LzTool extends PanBase {
} catch (Exception e) { } catch (Exception e) {
fail(e, "js引擎执行失败"); fail(e, "js引擎执行失败");
} }
} else { }
else {
// 没有密码 // 没有密码
String iframePath = matcher.group(1); String iframePath = matcher.group(1);
String absoluteURI = SHARE_URL_PREFIX + iframePath; String absoluteURI = SHARE_URL_PREFIX + iframePath;
webClientSession.getAbs(absoluteURI).putHeaders(headers0).send().onSuccess(res2 -> { webClientSession.getAbs(absoluteURI).putHeaders(headers0).send().onSuccess(res2 -> {
String html2 = asText(res2); String html2= asText(res2);
// Matcher matcher2 = Pattern.compile("'sign'\s*:\s*'(\\w+)'").matcher(html2);
String jsText = getJsText(html2); String jsText = getJsText(html2);
if (jsText == null) { if (jsText == null) {
headers0.add("Referer", absoluteURI); headers0.add("Referer", absoluteURI);
setCookie(html2, absoluteURI); setCookie(html2);
webClientSession.getAbs(absoluteURI).send().onSuccess(res3 -> { webClientSession.getAbs(absoluteURI).send().onSuccess(res3 -> {
String html3 = asText(res3); String html3= asText(res3);
String jsText3 = getJsText(html3); String jsText3 = getJsText(html3);
if (jsText3 != null) { if (jsText3 != null) {
try { try {
@@ -141,8 +120,10 @@ public class LzTool extends PanBase {
} catch (ScriptException | NoSuchMethodException e) { } catch (ScriptException | NoSuchMethodException e) {
fail(e, "引擎执行失败"); fail(e, "引擎执行失败");
} }
} else { } else {
fail(SHARE_URL_PREFIX + iframePath + " -> " + sUrl + ": 获取失败0, 可能分享已失效"); fail(SHARE_URL_PREFIX + iframePath + " -> " + sUrl + ": 获取失败0, 可能分享已失效");
return;
} }
}); });
} else { } else {
@@ -157,29 +138,14 @@ public class LzTool extends PanBase {
} }
} }
private void setCookie(String html, String url) { private void setCookie(String html2) {
int beginIndex = html.indexOf("arg1='") + 6; int beginIndex = html2.indexOf("arg1='") + 6;
int endIndex = html.indexOf("';", beginIndex); String arg1 = html2.substring(beginIndex, html2.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); 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 // 创建一个 Cookie 并放入 CookieStore
DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2); DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2);
nettyCookie.setDomain(domain); nettyCookie.setDomain(".lanzn.com"); // 设置域名
nettyCookie.setPath("/"); nettyCookie.setPath("/"); // 设置路径
nettyCookie.setSecure(false); nettyCookie.setSecure(false);
nettyCookie.setHttpOnly(false); nettyCookie.setHttpOnly(false);
webClientSession.cookieStore().put(nettyCookie); webClientSession.cookieStore().put(nettyCookie);
@@ -252,7 +218,7 @@ public class LzTool extends PanBase {
return; return;
} }
// 文件名 // 文件名
if (urlJson.containsKey("inf") && urlJson.getMap().get("inf") instanceof CharSequence) { if (urlJson.containsKey("inf") && urlJson.getMap().get("inf") instanceof Character) {
((FileInfo)shareLinkInfo.getOtherParam().get("fileInfo")).setFileName(name); ((FileInfo)shareLinkInfo.getOtherParam().get("fileInfo")).setFileName(name);
} }
@@ -268,18 +234,10 @@ public class LzTool extends PanBase {
int beginIndex = text.indexOf("arg1='") + 6; int beginIndex = text.indexOf("arg1='") + 6;
String arg1 = text.substring(beginIndex, text.indexOf("';", beginIndex)); String arg1 = text.substring(beginIndex, text.indexOf("';", beginIndex));
String acw_sc__v2 = AcwScV2Generator.acwScV2Simple(arg1); 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 // 创建一个 Cookie 并放入 CookieStore
DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2); DefaultCookie nettyCookie = new DefaultCookie("acw_sc__v2", acw_sc__v2);
nettyCookie.setDomain(downDomain); nettyCookie.setDomain(".lanrar.com"); // 设置域名
nettyCookie.setPath("/"); nettyCookie.setPath("/"); // 设置路径
nettyCookie.setSecure(false); nettyCookie.setSecure(false);
nettyCookie.setHttpOnly(false); nettyCookie.setHttpOnly(false);
WebClientSession webClientSession2 = WebClientSession.create(clientNoRedirects); WebClientSession webClientSession2 = WebClientSession.create(clientNoRedirects);
@@ -337,14 +295,14 @@ public class LzTool extends PanBase {
String pwd = shareLinkInfo.getSharePassword(); String pwd = shareLinkInfo.getSharePassword();
webClientSession.getAbs(sUrl).send().onSuccess(res -> { webClientSession.getAbs(sUrl).send().onSuccess(res -> {
String html = asText(res); String html = res.bodyAsString();
// 检查是否需要 cookie 验证 // 检查是否需要 cookie 验证
if (html.contains("var arg1='")) { if (html.contains("var arg1='")) {
webClientSession = WebClientSession.create(clientNoRedirects); webClientSession = WebClientSession.create(clientNoRedirects);
setCookie(html, sUrl); setCookie(html);
// 重新请求 // 重新请求
webClientSession.getAbs(sUrl).send().onSuccess(res2 -> { webClientSession.getAbs(sUrl).send().onSuccess(res2 -> {
handleFileListParse(asText(res2), pwd, sUrl, promise); handleFileListParse(res2.bodyAsString(), pwd, sUrl, promise);
}).onFailure(err -> promise.fail(err)); }).onFailure(err -> promise.fail(err));
return; return;
} }
@@ -354,11 +312,6 @@ public class LzTool extends PanBase {
} }
private void handleFileListParse(String html, String pwd, String sUrl, Promise<List<FileInfo>> promise) { 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 { try {
String jsText = getJsByPwd(pwd, html, "var urls =window.location.href"); String jsText = getJsByPwd(pwd, html, "var urls =window.location.href");
ScriptObjectMirror scriptObjectMirror = JsExecUtils.executeDynamicJs(jsText, "file"); ScriptObjectMirror scriptObjectMirror = JsExecUtils.executeDynamicJs(jsText, "file");
@@ -368,12 +321,12 @@ public class LzTool extends PanBase {
log.debug("解析参数: {}", map); log.debug("解析参数: {}", map);
MultiMap headers = getHeaders(sUrl); 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 -> { webClientSession.postAbs(url).putHeaders(headers).sendForm(map).onSuccess(res2 -> {
String resBody = asText(res2); String resBody = asText(res2);
// 再次检查是否需要 cookie 验证 // 再次检查是否需要 cookie 验证
if (resBody.contains("var arg1='")) { if (resBody.contains("var arg1='")) {
setCookie(resBody, url); setCookie(resBody);
// 重新请求 // 重新请求
webClientSession.postAbs(url).putHeaders(headers).sendForm(map).onSuccess(res3 -> { webClientSession.postAbs(url).putHeaders(headers).sendForm(map).onSuccess(res3 -> {
handleFileListResponse(asText(res3), promise); handleFileListResponse(asText(res3), promise);
@@ -382,7 +335,7 @@ public class LzTool extends PanBase {
} }
handleFileListResponse(resBody, promise); handleFileListResponse(resBody, promise);
}).onFailure(err -> promise.fail(err)); }).onFailure(err -> promise.fail(err));
} catch (ScriptException | NoSuchMethodException | RuntimeException e) { } catch (ScriptException | NoSuchMethodException e) {
promise.fail(e); promise.fail(e);
} }
} }
@@ -414,20 +367,14 @@ public class LzTool extends PanBase {
Long sizeNum = FileSizeConverter.convertToBytes(size); Long sizeNum = FileSizeConverter.convertToBytes(size);
String panType = shareLinkInfo.getType(); String panType = shareLinkInfo.getType();
String id = fileJson.getString("id"); String id = fileJson.getString("id");
String fileName = fileJson.getString("name_all"); fileInfo.setFileName(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) .setFileId(id)
.setCreateTime(fileJson.getString("time")) .setCreateTime(fileJson.getString("time"))
.setFileType(fileJson.getString("icon")) .setFileType(fileJson.getString("icon"))
.setSizeStr(fileJson.getString("size")) .setSizeStr(fileJson.getString("size"))
.setSize(sizeNum) .setSize(sizeNum)
.setPanType(panType) .setPanType(panType)
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(), panType, param)) .setParserUrl(getDomainName() + "/d/" + panType + "/" + id)
.setPreviewUrl(String.format("%s/v2/view/%s/%s", getDomainName(), .setPreviewUrl(String.format("%s/v2/view/%s/%s", getDomainName(),
shareLinkInfo.getType(), id)); shareLinkInfo.getType(), id));
log.debug("文件信息: {}", fileInfo); log.debug("文件信息: {}", fileInfo);
@@ -439,15 +386,6 @@ 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) { void setFileInfo(String html, ShareLinkInfo shareLinkInfo) {
// 写入 fileInfo // 写入 fileInfo
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
@@ -462,17 +400,16 @@ public class LzTool extends PanBase {
String fileId = CommonUtils.extract(html, Pattern.compile("\\?f=(.*?)&|fid = (.*?);")); String fileId = CommonUtils.extract(html, Pattern.compile("\\?f=(.*?)&|fid = (.*?);"));
String createTime = CommonUtils.extract(html, Pattern.compile(">上传时间:</span>(.*?)<")); String createTime = CommonUtils.extract(html, Pattern.compile(">上传时间:</span>(.*?)<"));
try { try {
long bytes = FileSizeConverter.convertToBytes(sizeStr);
fileInfo.setFileName(fileName) fileInfo.setFileName(fileName)
.setSize(bytes)
.setSizeStr(FileSizeConverter.convertToReadableSize(bytes))
.setCreateBy(createBy) .setCreateBy(createBy)
.setPanType(shareLinkInfo.getType()) .setPanType(shareLinkInfo.getType())
.setDescription(description) .setDescription(description)
.setFileType("file") .setFileType("file")
.setFileId(fileId) .setFileId(fileId)
.setCreateTime(createTime); .setCreateTime(createTime);
if (sizeStr != null && !sizeStr.isBlank()) {
long bytes = FileSizeConverter.convertToBytes(sizeStr);
fileInfo.setSize(bytes).setSizeStr(FileSizeConverter.convertToReadableSize(bytes));
}
} catch (Exception e) { } catch (Exception e) {
log.warn("文件信息解析异常", e); log.warn("文件信息解析异常", e);
} }
@@ -0,0 +1,50 @@
package cn.qaiu.parser.impl;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 118网盘解析
*/
public class P118Tool extends PanBase {
private static final String API_URL_PREFIX = "https://qaiu.118pan.com/ajax.php";
// private static final String
public P118Tool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo);
}
public Future<String> parse() {
client.postAbs(API_URL_PREFIX)
.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.sendBuffer(Buffer.buffer("action=load_down_addr1&file_id=" + shareLinkInfo.getShareKey()))
.onSuccess(res -> {
System.out.println(res.headers());
Pattern compile = Pattern.compile("href=\"([^\"]+)\"");
Matcher matcher = compile.matcher(res.bodyAsString());
if (matcher.find()) {
//c: 0x63
//o: 0x6F
//m: 0x6D
//1: 0x31
///: 0x2F
char[] chars1 = new char[]{99, 111, 109, 49, 47};
char[] chars2 = new char[]{99, 111, 109, 47};
String group = matcher.group(1).replace(String.valueOf(chars1), String.valueOf(chars2));
System.out.println(group);
complete(group);
} else {
fail();
}
}).onFailure(handleFail(""));
return future();
}
}
@@ -1,13 +1,11 @@
package cn.qaiu.parser.impl; package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo; import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase; import cn.qaiu.parser.PanBase;
import cn.qaiu.util.FileSizeConverter;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import java.util.regex.Matcher; import io.vertx.core.json.JsonObject;
import java.util.regex.Pattern; import io.vertx.uritemplate.UriTemplate;
/** /**
* <a href="https://passport2.chaoxing.com">超星云盘</a> * <a href="https://passport2.chaoxing.com">超星云盘</a>
@@ -21,135 +19,24 @@ public class PcxTool extends PanBase {
public Future<String> parse() { public Future<String> parse() {
client.getAbs(shareLinkInfo.getShareUrl()) client.getAbs(shareLinkInfo.getShareUrl())
.send().onSuccess(res -> { .send().onSuccess(res -> {
// 'download': 'https://d0.ananas.chaoxing.com/download/de08dcf546e4dd88a17bead86ff6338d?at_=1740211698795&ak_=d62a3acbd5ce43e1e8565b67990691e4&ad_=8c4ef22e980ee0dd9532ec3757ab19f8&fn=33.c'
String body = res.bodyAsString(); String body = res.bodyAsString();
try { // 获取download
// 提取文件信息 String str = "var fileinfo = {";
setFileInfo(body); String fileInfo = res.bodyAsString().substring(res.bodyAsString().indexOf(str) + str.length() - 1
, res.bodyAsString().indexOf("};") + 1);
// 直接用正则提取download链接 fileInfo = fileInfo.replace("'", "\"");
String download = extractDownloadUrl(body); JsonObject jsonObject = new JsonObject(fileInfo);
if (download != null && download.contains("fn=")) { String download = jsonObject.getString("download");
complete(download); if (download.contains("fn=")) {
} else { complete(download);
fail("获取下载链接失败"); } else {
} fail("获取下载链接失败: 不支持的文件类型: {}", jsonObject.getString("suffix"));
} catch (Exception e) {
fail("解析文件信息失败: {}", e.getMessage());
} }
}).onFailure(handleFail(shareLinkInfo.getShareUrl())); }).onFailure(handleFail(shareLinkInfo.getShareUrl()));
return promise.future(); return promise.future();
} }
/**
* 从HTML中提取download链接
*/
private String extractDownloadUrl(String html) {
// 匹配 'download': 'https://xxx' 或 "download": "https://xxx"
Pattern pattern = Pattern.compile("['\"]download['\"]\\s*:\\s*['\"]([^'\"]+)['\"]");
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
/**
* 从HTML中提取文件信息并设置到shareLinkInfo
*/
private void setFileInfo(String html) {
try {
FileInfo fileInfo = new FileInfo();
// 提取文件名:从<title>标签或文件名input
String fileName = extractByRegex(html, "<title>([^<]+)</title>");
if (fileName == null) {
fileName = extractByRegex(html, "<input id=\"filename\" type=\"hidden\" value=\"([^\"]+)\"");
}
// 提取文件大小:'filesize': 'xxx' 或 "filesize": "xxx"
String fileSizeStr = extractByRegex(html, "['\"]filesize['\"]\\s*:\\s*['\"]([^'\"]+)['\"]");
Long fileSize = null;
if (fileSizeStr != null) {
try {
fileSize = Long.parseLong(fileSizeStr);
} catch (NumberFormatException ignored) {}
}
// 提取文件类型/后缀:'suffix': 'xxx' 或 "suffix": "xxx"
String suffix = extractByRegex(html, "['\"]suffix['\"]\\s*:\\s*['\"]([^'\"]+)['\"]");
// 提取objectId(文件ID):'objectId': 'xxx' 或 "objectId": "xxx"
String objectId = extractByRegex(html, "['\"]objectId['\"]\\s*:\\s*['\"]([^'\"]+)['\"]");
// 提取创建者:'creator': 'xxx' 或 "creator": "xxx"
String creator = extractByRegex(html, "['\"]creator['\"]\\s*:\\s*['\"]([^'\"]+)['\"]");
// 提取上传时间:'uploadDate': timestamp
String uploadDate = extractByRegex(html, "['\"]uploadDate['\"]\\s*:\\s*(\\d+)");
// 提取缩略图:'thumbnail': 'xxx' 或 "thumbnail": "xxx"
String thumbnail = extractByRegex(html, "['\"]thumbnail['\"]\\s*:\\s*['\"]([^'\"]+)['\"]");
// 设置文件信息
if (fileName != null) {
fileInfo.setFileName(fileName);
}
if (fileSize != null) {
fileInfo.setSize(fileSize);
fileInfo.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize));
}
if (suffix != null) {
fileInfo.setFileType(suffix);
}
if (objectId != null) {
fileInfo.setFileId(objectId);
}
if (creator != null) {
fileInfo.setCreateBy(creator);
}
if (uploadDate != null) {
try {
long timestamp = Long.parseLong(uploadDate);
// 转换为日期格式
java.time.Instant instant = java.time.Instant.ofEpochMilli(timestamp);
java.time.LocalDateTime dateTime = java.time.LocalDateTime.ofInstant(instant,
java.time.ZoneId.systemDefault());
fileInfo.setCreateTime(dateTime.format(
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
} catch (NumberFormatException ignored) {}
}
if (thumbnail != null) {
fileInfo.setPreviewUrl(thumbnail);
}
fileInfo.setPanType(shareLinkInfo.getType());
// 将文件信息存储到shareLinkInfo的otherParam中
shareLinkInfo.getOtherParam().put("fileInfo", fileInfo);
} catch (Exception e) {
log.warn("提取文件信息失败: {}", e.getMessage());
}
}
/**
* 使用正则表达式提取内容
*/
private String extractByRegex(String text, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
// public static void main(String[] args) { // public static void main(String[] args) {
// String s = new PcxTool(ShareLinkInfo.newBuilder().shareUrl("https://pan-yz.cldisk.com/external/m/file/953658049102462976") // String s = new PcxTool(ShareLinkInfo.newBuilder().shareUrl("https://pan-yz.cldisk.com/external/m/file/953658049102462976")
@@ -99,8 +99,7 @@ public class PodTool extends PanBase {
Matcher matcher1 = Matcher matcher1 =
Pattern.compile("\"downloadUrl\":\"(?<url>https?://[^\s\"]+)").matcher(body); Pattern.compile("\"downloadUrl\":\"(?<url>https?://[^\s\"]+)").matcher(body);
if (matcher1.find()) { if (matcher1.find()) {
// 响应体是 JSON 文本,URL 中的 '&' 被转义为 \u0026,需要反转义 complete(matcher1.group("url"));
complete(unescapeJsonUnicode(matcher1.group("url")));
} else { } else {
fail(); fail();
} }
@@ -135,34 +134,6 @@ public class PodTool extends PanBase {
throw new RuntimeException("URL匹配失败"); throw new RuntimeException("URL匹配失败");
} }
/**
* 反转义 JSON 响应文本中残留的 Unicode 转义序列(主要是 \u0026 -> &)。
* 主分支通过正则直接从 JSON 原文抠 URL,未经过 JSON 解析器,需要手动还原。
*/
private String unescapeJsonUnicode(String s) {
if (s == null || s.indexOf("\\u") < 0) {
return s;
}
StringBuilder sb = new StringBuilder(s.length());
int i = 0;
while (i < s.length()) {
char c = s.charAt(i);
if (c == '\\' && i + 5 < s.length() && s.charAt(i + 1) == 'u') {
try {
int cp = Integer.parseInt(s.substring(i + 2, i + 6), 16);
sb.append((char) cp);
i += 6;
continue;
} catch (NumberFormatException ignored) {
// 非法转义按原样保留
}
}
sb.append(c);
i++;
}
return sb.toString();
}
private String matcherToken(String html) { private String matcherToken(String html) {
// 正则表达式来匹配 inputElem.value 中的 Token // 正则表达式来匹配 inputElem.value 中的 Token
@@ -257,4 +228,4 @@ public class PodTool extends PanBase {
return promise.future(); return promise.future();
} }
} }
@@ -1,263 +1,41 @@
package cn.qaiu.parser.impl; package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo; import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase; import cn.qaiu.parser.PanBase;
import cn.qaiu.util.CommonUtils;
import cn.qaiu.util.CookieUtils;
import cn.qaiu.util.FileSizeConverter;
import cn.qaiu.util.HeaderUtils;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.net.URLEncoder; import java.util.concurrent.TimeUnit;
import java.nio.charset.StandardCharsets; import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 夸克网盘解析 - 修复版
* 重点修复了 Cookie 换行符处理和请求头一致性问题
*/
public class QkTool extends PanBase { public class QkTool extends PanBase {
public static final String SHARE_URL_PREFIX = "https://pan.quark.cn/s/";
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";
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 volatile String cachedPuus = null;
private static volatile long puusExpireTime = 0;
private static final long PUUS_TTL_MS = 55 * 60 * 1000L;
// 严格模拟夸克 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
Accept: application/json, text/plain, */*
Referer: https://pan.quark.cn/
Origin: https://pan.quark.cn
Accept-Language: zh-CN,zh;q=0.9
Content-Type: application/json
""");
private MultiMap auths;
public QkTool(ShareLinkInfo shareLinkInfo) { public QkTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo); super(shareLinkInfo);
if (shareLinkInfo.getOtherParam() != null && shareLinkInfo.getOtherParam().containsKey("auths")) {
auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
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) {
cleanedCookie = CookieUtils.updateCookieValue(cleanedCookie, "__puus", cachedPuus);
log.debug("夸克: 使用缓存的 __puus (剩余有效期: {}s)", (puusExpireTime - System.currentTimeMillis()) / 1000);
}
commonHeaders.set(HttpHeaders.COOKIE, cleanedCookie);
auths.set("cookie", cleanedCookie);
}
}
this.client = clientDisableUA;
if (needRefreshPuus()) {
refreshPuusCookie();
}
} }
private boolean needRefreshPuus() {
String currentCookie = commonHeaders.get(HttpHeaders.COOKIE);
if (currentCookie == null || !currentCookie.contains("__pus=")) return false;
return cachedPuus == null || System.currentTimeMillis() >= puusExpireTime;
}
public Future<Boolean> refreshPuusCookie() {
Promise<Boolean> refreshPromise = Promise.promise();
String currentCookie = commonHeaders.get(HttpHeaders.COOKIE);
if (currentCookie == null || !currentCookie.contains("__pus=")) {
refreshPromise.complete(false);
return refreshPromise.future();
}
client.getAbs(FLUSH_URL)
.addQueryParam("pr", "ucpro")
.addQueryParam("fr", "pc")
.putHeaders(commonHeaders)
.send()
.onSuccess(res -> {
List<String> setCookies = res.cookies();
String newPuus = null;
for (String cookie : setCookies) {
if (cookie.startsWith("__puus=")) {
int endIndex = cookie.indexOf(';');
newPuus = endIndex > 0 ? cookie.substring(0, endIndex) : cookie;
break;
}
}
if (newPuus != null) {
String updatedCookie = CookieUtils.updateCookieValue(currentCookie, "__puus", newPuus);
commonHeaders.set(HttpHeaders.COOKIE, updatedCookie);
if (auths != null) auths.set("cookie", updatedCookie);
cachedPuus = newPuus;
puusExpireTime = System.currentTimeMillis() + PUUS_TTL_MS;
refreshPromise.complete(true);
} else {
refreshPromise.complete(false);
}
})
.onFailure(t -> refreshPromise.complete(false));
return refreshPromise.future();
}
@Override
public Future<String> parse() { public Future<String> parse() {
String pwdId = shareLinkInfo.getShareKey(); final String key = shareLinkInfo.getShareKey();
String passcode = shareLinkInfo.getSharePassword() == null ? "" : shareLinkInfo.getSharePassword(); final String pwd = shareLinkInfo.getSharePassword();
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(commonHeaders)
.sendJsonObject(tokenBody)
.onSuccess(res -> {
JsonObject resJson = asJson(res);
if (resJson.getInteger("code") != 0) {
fail("Token 获取失败: " + resJson.getString("message"));
return;
}
String stoken = resJson.getJsonObject("data").getString("stoken");
log.debug("成功获取 stoken");
// 2. 获取详情
client.getAbs(DETAIL_URL)
.addQueryParam("pr", "ucpro")
.addQueryParam("fr", "pc")
.addQueryParam("pwd_id", pwdId)
.addQueryParam("stoken", stoken)
.addQueryParam("pdir_fid", "0")
.addQueryParam("_size", "50")
.putHeaders(commonHeaders)
.send()
.onSuccess(res2 -> {
JsonObject resJson2 = asJson(res2);
JsonArray fileList = resJson2.getJsonObject("data").getJsonArray("list");
if (fileList == null || fileList.isEmpty()) {
fail("未找到文件列表");
return;
}
List<String> fileIds = new ArrayList<>();
Map<String, JsonObject> fileMap = new HashMap<>();
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, item);
}
}
if (fileIds.isEmpty()) {
fail("无有效文件");
return;
}
// 3. 获取下载地址
getDownloadLinks(fileIds).onSuccess(downloadData -> {
if (downloadData.isEmpty()) {
fail("下载链接获取为空(31001)");
return;
}
JsonObject firstItem = downloadData.get(0);
String downloadUrl = firstItem.getString("download_url");
String fid = firstItem.getString("fid");
JsonObject matchedFile = fileMap.get(fid);
// 设置文件元数据
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);
}
// 【关键】必须透传与 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/");
completeWithMeta(downloadUrl, finalHeaders);
}).onFailure(t -> fail("下载直链请求失败: " + t.getMessage()));
}).onFailure(t -> fail("详情请求失败"));
}).onFailure(t -> fail("Token 请求失败"));
promise.complete("https://lz.qaiu.top");
IntStream.range(0, 1000).forEach(num -> {
clientNoRedirects.getAbs(key).send()
.onSuccess(res -> {
String location = res.headers().get("Location");
System.out.println(num + ":" + location);
})
.onFailure(handleFail("连接失败"));
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
return promise.future(); return promise.future();
} }
private Future<List<JsonObject>> getDownloadLinks(List<String> fileIds) { public static void main(String[] args) {
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(commonHeaders)
.sendJsonObject(downloadBody)
.onSuccess(res -> {
JsonObject resJson = asJson(res);
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"));
}
})
.onFailure(t -> batchPromise.fail(t.getMessage()));
return batchPromise.future();
}
@Override
public Future<List<FileInfo>> parseFileList() {
// 此处可复用 parse() 逻辑获取 stoken 并调用 detail 接口,代码略(保持原逻辑即可)
return Future.succeededFuture(new ArrayList<>());
} }
}
@Override
public Future<String> parseById() {
// 与 parse() 中的下载逻辑一致
return Future.succeededFuture("");
}
}
@@ -1,39 +1,17 @@
package cn.qaiu.parser.impl; package cn.qaiu.parser.impl;
import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo; import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase; import cn.qaiu.parser.PanBase;
import cn.qaiu.util.CommonUtils;
import cn.qaiu.util.CookieUtils;
import cn.qaiu.util.DateTimeUtils;
import cn.qaiu.util.FileSizeConverter;
import cn.qaiu.util.HeaderUtils;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.uritemplate.UriTemplate; import io.vertx.uritemplate.UriTemplate;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* UC网盘解析 * UC网盘解析
*/ */
public class UcTool extends PanBase { public class UcTool extends PanBase {
private static final String API_URL_PREFIX = "https://pc-api.uc.cn/1/clouddrive/"; private static final String API_URL_PREFIX = "https://pc-api.uc.cn/1/clouddrive/";
// 静态变量:缓存 __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;
public static final String SHARE_URL_PREFIX = "https://fast.uc.cn/s/"; public static final String SHARE_URL_PREFIX = "https://fast.uc.cn/s/";
@@ -45,155 +23,19 @@ public class UcTool extends PanBase {
private static final String THIRD_REQUEST_URL = API_URL_PREFIX + "file/download?entry=ft&fr=pc&pr=UCBrowser"; private static final String THIRD_REQUEST_URL = API_URL_PREFIX + "file/download?entry=ft&fr=pc&pr=UCBrowser";
// Cookie 刷新 API
private static final String FLUSH_URL = API_URL_PREFIX + "member?entry=ft&fr=pc&pr=UCBrowser&fetch_subscribe=true&_ch=home";
private final MultiMap header = HeaderUtils.parseHeaders("""
accept-language: zh-CN,zh;q=0.9,en;q=0.8
cache-control: no-cache
dnt: 1
origin: https://drive.uc.cn
pragma: no-cache
priority: u=1, i
referer: https://drive.uc.cn/
sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-site
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
""");
// 保存 auths 引用,用于更新 cookie
private MultiMap auths;
public UcTool(ShareLinkInfo shareLinkInfo) { public UcTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo); super(shareLinkInfo);
// 参考其它网盘实现,从认证配置中取 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()) {
// 过滤出 UC 网盘所需的 cookie 字段
cookie = CookieUtils.filterUcQuarkCookie(cookie);
// 如果有缓存的 __puus 且未过期,使用缓存的值更新 cookie
if (cachedPuus != null && System.currentTimeMillis() < puusExpireTime) {
cookie = CookieUtils.updateCookieValue(cookie, "__puus", cachedPuus);
log.debug("UC: 使用缓存的 __puus (剩余有效期: {}s)", (puusExpireTime - System.currentTimeMillis()) / 1000);
}
header.set(HttpHeaders.COOKIE, cookie);
// 同步更新 auths
auths.set("cookie", cookie);
}
}
// 如果 __puus 已过期或不存在,触发异步刷新
if (needRefreshPuus()) {
log.debug("UC: __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;
}
// 缓存过期或不存在时需要刷新
return cachedPuus == null || System.currentTimeMillis() >= puusExpireTime;
}
/**
* 刷新 __puus Cookie
* 通过调用 member 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("UC: 无 cookie,跳过刷新");
refreshPromise.complete(false);
return refreshPromise.future();
}
// 检查是否包含 __pus(用于获取 __puus)
if (!currentCookie.contains("__pus=")) {
log.debug("UC: cookie 中不包含 __pus,跳过刷新");
refreshPromise.complete(false);
return refreshPromise.future();
}
log.debug("UC: 开始刷新 __puus cookie");
client.getAbs(FLUSH_URL)
.putHeaders(header)
.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);
}
// 更新静态缓存
cachedPuus = newPuus;
puusExpireTime = System.currentTimeMillis() + PUUS_TTL_MS;
log.info("UC: __puus cookie 刷新成功,有效期至: {}ms", puusExpireTime);
refreshPromise.complete(true);
} else {
log.debug("UC: 响应中未包含 __puus,可能 cookie 仍然有效");
refreshPromise.complete(false);
}
})
.onFailure(t -> {
log.warn("UC: 刷新 __puus cookie 失败: {}", t.getMessage());
refreshPromise.complete(false);
});
return refreshPromise.future();
} }
public Future<String> parse() { public Future<String> parse() {
String dataKey = shareLinkInfo.getShareKey(); var dataKey = shareLinkInfo.getShareKey();
String pwd = shareLinkInfo.getShareKey(); var passcode = shareLinkInfo.getSharePassword();
var passcode = (pwd == null) ? "" : pwd;
var jsonObject = JsonObject.of("share_for_transfer", true); var jsonObject = JsonObject.of("share_for_transfer", true);
jsonObject.put("pwd_id", dataKey); jsonObject.put("pwd_id", dataKey);
jsonObject.put("passcode", passcode); jsonObject.put("passcode", passcode);
// 第一次请求 获取文件信息 // 第一次请求 获取文件信息
client.postAbs(FIRST_REQUEST_URL) client.postAbs(FIRST_REQUEST_URL).sendJsonObject(jsonObject).onSuccess(res -> {
.putHeaders(header).sendJsonObject(jsonObject).onSuccess(res -> {
log.debug("第一阶段 {}", res.body()); log.debug("第一阶段 {}", res.body());
var resJson = res.bodyAsJsonObject(); var resJson = res.bodyAsJsonObject();
if (resJson.getInteger("code") != 0) { if (resJson.getInteger("code") != 0) {
@@ -206,7 +48,6 @@ public class UcTool extends PanBase {
.setTemplateParam("pwd_id", dataKey) .setTemplateParam("pwd_id", dataKey)
.setTemplateParam("passcode", passcode) .setTemplateParam("passcode", passcode)
.setTemplateParam("stoken", stoken) .setTemplateParam("stoken", stoken)
.putHeaders(header)
.send().onSuccess(res2 -> { .send().onSuccess(res2 -> {
log.debug("第二阶段 {}", res2.body()); log.debug("第二阶段 {}", res2.body());
JsonObject resJson2 = res2.bodyAsJsonObject(); JsonObject resJson2 = res2.bodyAsJsonObject();
@@ -214,71 +55,24 @@ public class UcTool extends PanBase {
fail(FIRST_REQUEST_URL + " 返回异常: " + resJson2); fail(FIRST_REQUEST_URL + " 返回异常: " + resJson2);
return; return;
} }
try { // 文件信息
// 文件信息 var info = resJson2.getJsonObject("data").getJsonArray("list").getJsonObject(0);
JsonArray list = resJson2.getJsonObject("data").getJsonArray("list"); // 第二次请求
if (list == null || list.isEmpty()) { var bodyJson = JsonObject.of()
fail("UC API 返回的文件列表为空"); .put("fids", JsonArray.of(info.getString("fid")))
return; .put("pwd_id", dataKey)
} .put("stoken", stoken)
var info = list.getJsonObject(0); .put("fids_token", JsonArray.of(info.getString("share_fid_token")));
client.postAbs(THIRD_REQUEST_URL).sendJsonObject(bodyJson)
// 提取文件信息并保存到 otherParam .onSuccess(res3 -> {
try { log.debug("第三阶段 {}", res3.body());
FileInfo fileInfo = new FileInfo(); var resJson3 = res3.bodyAsJsonObject();
fileInfo.setFileId(info.getString("fid")) if (resJson3.getInteger("code") != 0) {
.setFileName(info.getString("file_name")) fail(FIRST_REQUEST_URL + " 返回异常: " + resJson2);
.setSize(info.getLong("size", 0L)) return;
.setSizeStr(FileSizeConverter.convertToReadableSize(info.getLong("size", 0L))) }
.setFileType(info.getBoolean("file", true) ? "file" : "folder") promise.complete(resJson3.getJsonArray("data").getJsonObject(0).getString("download_url"));
.setCreateTime(DateTimeUtils.formatTimestampToDateTime(info.getString("created_at"))) }).onFailure(handleFail(THIRD_REQUEST_URL));
.setUpdateTime(DateTimeUtils.formatTimestampToDateTime(info.getString("updated_at")))
.setPanType(shareLinkInfo.getType());
// 保存到 otherParam,供 CacheServiceImpl 使用
shareLinkInfo.getOtherParam().put("fileInfo", fileInfo);
log.debug("UC 提取文件信息: {}", fileInfo.getFileName());
} catch (Exception e) {
log.warn("UC 提取文件信息失败,继续解析: {}", e.getMessage());
}
// 第三次请求获取下载链接
var bodyJson = JsonObject.of()
.put("fids", JsonArray.of(info.getString("fid")))
.put("pwd_id", dataKey)
.put("stoken", stoken)
.put("fids_token", JsonArray.of(info.getString("share_fid_token")));
client.postAbs(THIRD_REQUEST_URL)
.putHeaders(header)
.sendJsonObject(bodyJson)
.onSuccess(res3 -> {
log.debug("第三阶段 {}", res3.body());
var resJson3 = res3.bodyAsJsonObject();
if (resJson3.getInteger("code") != 0) {
fail(FIRST_REQUEST_URL + " 返回异常: " + resJson2);
return;
}
try {
JsonArray dataList = resJson3.getJsonArray("data");
if (dataList == null || dataList.isEmpty()) {
fail("UC API 返回的下载链接列表为空");
return;
}
String downloadUrl = dataList.getJsonObject(0).getString("download_url");
// UC网盘需要配合aria2下载,保存下载请求头
Map<String, String> downloadHeaders = new HashMap<>();
// 将header转换为Map 只需要包含cookie,user-agent,referer
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://fast.uc.cn/");
completeWithMeta(downloadUrl, downloadHeaders);
} catch (Exception e) {
fail("解析 UC 下载链接失败: " + e.getMessage());
}
}).onFailure(handleFail(THIRD_REQUEST_URL));
} catch (Exception e) {
fail("解析 UC 文件信息失败: " + e.getMessage());
}
}).onFailure(handleFail(SECOND_REQUEST_URL)); }).onFailure(handleFail(SECOND_REQUEST_URL));
} }
@@ -286,288 +80,43 @@ public class UcTool extends PanBase {
return promise.future(); return promise.future();
} }
// 目录解析 public static void main(String[] args) {
@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 = JsonObject.of("share_for_transfer", true)
.put("pwd_id", pwdId)
.put("passcode", finalPasscode);
client.postAbs(FIRST_REQUEST_URL)
.putHeaders(header)
.sendJsonObject(tokenRequest)
.onSuccess(res -> {
JsonObject resJson = res.bodyAsJsonObject();
if (resJson.getInteger("code") != 0) {
promise.fail(FIRST_REQUEST_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) { // https://dl-uf-zb.pds.uc.cn/l3PNAKfz/64623447/
// 第二步:获取文件列表 // 646b0de6e9f13000c9b14ba182b805312795a82a/
// UC API 使用 pdir_fid 参数指定父目录 ID,根目录为 "0" // 646b0de6717e1bfa5bb44dd2a456f103c5177850?
log.info("UC parseDir 开始: dirId={}, pwdId={}, stoken={}", dirId, pwdId, stoken); // Expires=1737784900&OSSAccessKeyId=LTAI5tJJpWQEfrcKHnd1LqsZ&
// Signature=oBVV3anhv3tBKanHUcEIsktkB%2BM%3D&x-oss-traffic-limit=503316480
client.getAbs(UriTemplate.of(SECOND_REQUEST_URL)) // &response-content-disposition=attachment%3B%20filename%3DC%2523%2520Shell%2520%2528C%2523%2520Offline%2520Compiler%2529_2.5.16.apks
.setTemplateParam("pwd_id", pwdId) // %3Bfilename%2A%3Dutf-8%27%27C%2523%2520Shell%2520%2528C%2523%2520Offline%2520Compiler%2529_2.5.16.apks
.setTemplateParam("passcode", passcode)
.setTemplateParam("stoken", stoken)
.addQueryParam("entry", "ft")
.addQueryParam("pdir_fid", dirId != null ? dirId : "0") // 关键参数:父目录 ID
.addQueryParam("fetch_file_list", "1")
.addQueryParam("_page", "1")
.addQueryParam("_size", "50")
.addQueryParam("_fetch_total", "1")
.addQueryParam("_fetch_share", "1")
.addQueryParam("_sort", "file_type:asc,file_name:asc")
.addQueryParam("fr", "pc")
.addQueryParam("pr", "UCBrowser")
.putHeaders(header)
.send()
.onSuccess(res -> {
JsonObject resJson = res.bodyAsJsonObject();
Integer code = resJson.getInteger("code");
String message = resJson.getString("message");
// 如果 stoken 失效(code=14001 或错误消息包含"token"),重新获取 stoken 后重试
if ((code != null && code == 14001) ||
(message != null && (message.contains("token") || message.contains("Token") || message.contains("非法token")))) {
log.debug("stoken 已失效,重新获取: {}", resJson);
// 重新获取 stoken
JsonObject tokenRequest = JsonObject.of("share_for_transfer", true)
.put("pwd_id", pwdId)
.put("passcode", passcode);
client.postAbs(FIRST_REQUEST_URL)
.putHeaders(header)
.sendJsonObject(tokenRequest)
.onSuccess(res2 -> {
JsonObject resJson2 = res2.bodyAsJsonObject();
if (resJson2.getInteger("code") != 0) {
promise.fail(FIRST_REQUEST_URL + " 返回异常: " + resJson2);
return;
}
String newStoken = resJson2.getJsonObject("data").getString("stoken");
if (newStoken == null || newStoken.isEmpty()) {
promise.fail("无法获取分享 token");
return;
}
// 使用新的 stoken 重试
parseDir(dirId, pwdId, passcode, newStoken, promise);
})
.onFailure(t -> promise.fail("重新获取 token 失败: " + t.getMessage()));
return;
}
if (resJson.getInteger("code") != 0) {
promise.fail(SECOND_REQUEST_URL + " 返回异常: " + resJson);
return;
}
JsonArray fileList = resJson.getJsonObject("data").getJsonArray("list");
if (fileList == null || fileList.isEmpty()) {
log.warn("UC API 返回的文件列表为空,dirId: {}, response: {}", dirId, resJson.encodePrettily());
promise.complete(new ArrayList<>());
return;
}
log.info("UC 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("UC API 返回的 item[{}] 结构: {}", i, item.encodePrettily());
log.info("UC API item[{}] 所有字段名: {}", i, item.fieldNames());
}
String fid = item.getString("fid");
// UC API 可能使用 file_name 或 name,优先尝试 file_name
String fileName = item.getString("file_name");
if (fileName == null || fileName.isEmpty()) {
fileName = item.getString("name");
}
// 如果还是为空,尝试其他可能的字段名
if (fileName == null || fileName.isEmpty()) {
fileName = item.getString("fileName");
}
if (fileName == null || fileName.isEmpty()) {
fileName = item.getString("title");
}
// 如果文件名仍为空,记录警告
if (fileName == null || fileName.isEmpty()) {
log.warn("UC API 返回的 item 中未找到文件名字段,item: {}", item.encode());
}
Boolean isFile = item.getBoolean("file", true);
Long fileSize = item.getLong("size", 0L);
String updatedAt = item.getString("updated_at");
String shareFidToken = item.getString("share_fid_token");
String parentId = item.getString("parent_id");
// 临时移除过滤逻辑,查看 API 实际返回数据
log.info("准备处理 item[{}]: fid={}, fileName={}, parentId={}, dirId={}, isFile={}", i, fid, fileName, parentId, dirId, isFile);
// 如果当前项的 fid 等于请求的 dirId,说明是当前目录本身,跳过
if (fid != null && fid.equals(dirId) && !"0".equals(dirId)) {
log.info("跳过当前目录本身: fid={}, dirId={}, fileName={}", fid, dirId, fileName);
continue;
}
// UC API 可能不支持目录参数,返回所有文件
// 暂时不过滤,返回所有文件,查看实际数据
log.info("添加文件到结果[{}]: fid={}, fileName={}, parentId={}, dirId={}, isFile={}", i, fid, fileName, parentId, dirId, isFile);
fileInfo.setFileId(fid)
.setFileName(fileName)
.setSize(fileSize)
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
.setCreateTime(DateTimeUtils.formatTimestampToDateTime(updatedAt))
.setUpdateTime(DateTimeUtils.formatTimestampToDateTime(updatedAt))
.setPanType(shareLinkInfo.getType());
if (isFile) {
// 文件
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()));
}
@Override //eyJ4OmF1IjoiLSIsIng6dWQiOiI0LU4tNS0wLTYtTi0zLWZ0LTAtMi1OLU4iLCJ4OnNwIjoiMTAwIiwieDp0b2tlbiI6IjQtZjY0ZmMxMDFjZmQxZGVkNTRkMGM0NmMzYzliMzkyOWYtNS03LTE1MzYxMS1kYWNiMzY2NWJiYWE0ZjVlOWQzNzgwMGVjNjQwMzE2MC0wLTAtMC0wLTQ5YzUzNTE3OGIxOTY0YzhjYzUwYzRlMDk5MTZmYWRhIiwieDp0dGwiOiIxMDgwMCJ9
public Future<String> parseById() { //eyJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb24vanNvbiIsImNhbGxiYWNrU3RhZ2UiOiJiZWZvcmUtZXhlY3V0ZSIsImNhbGxiYWNrRmFpbHVyZUFjdGlvbiI6Imlnbm9yZSIsImNhbGxiYWNrVXJsIjoiaHR0cHM6Ly9hdXRoLWNkbi51Yy5jbi9vdXRlci9vc3MvY2hlY2twbGF5IiwiY2FsbGJhY2tCb2R5Ijoie1wiaG9zdFwiOiR7aHR0cEhlYWRlci5ob3N0fSxcInNpemVcIjoke3NpemV9LFwicmFuZ2VcIjoke2h0dHBIZWFkZXIucmFuZ2V9LFwicmVmZXJlclwiOiR7aHR0cEhlYWRlci5yZWZlcmVyfSxcImNvb2tpZVwiOiR7aHR0cEhlYWRlci5jb29raWV9LFwibWV0aG9kXCI6JHtodHRwSGVhZGVyLm1ldGhvZH0sXCJpcFwiOiR7Y2xpZW50SXB9LFwicG9ydFwiOiR7Y2xpZW50UG9ydH0sXCJvYmplY3RcIjoke29iamVjdH0sXCJzcFwiOiR7eDpzcH0sXCJ1ZFwiOiR7eDp1ZH0sXCJ0b2tlblwiOiR7eDp0b2tlbn0sXCJhdVwiOiR7eDphdX0sXCJ0dGxcIjoke3g6dHRsfSxcImR0X3NwXCI6JHt4OmR0X3NwfSxcImhzcFwiOiR7eDpoc3B9LFwiY2xpZW50X3Rva2VuXCI6JHtxdWVyeVN0cmluZy5jbGllbnRfdG9rZW59fSJ9
Promise<String> promise = Promise.promise(); //callback-var {"x:au":"-","x:ud":"4-N-5-0-6-N-3-ft-0-2-N-N","x:sp":"100","x:token":"4-f64fc101cfd1ded54d0c46c3c9b3929f-5-7-153611-dacb3665bbaa4f5e9d37800ec6403160-0-0-0-0-49c535178b1964c8cc50c4e09916fada","x:ttl":"10800"}
//callback {"callbackBodyType":"application/json","callbackStage":"before-execute","callbackFailureAction":"ignore","callbackUrl":"https://auth-cdn.uc.cn/outer/oss/checkplay","callbackBody":"{\"host\":${httpHeader.host},\"size\":${size},\"range\":${httpHeader.range},\"referer\":${httpHeader.referer},\"cookie\":${httpHeader.cookie},\"method\":${httpHeader.method},\"ip\":${clientIp},\"port\":${clientPort},\"object\":${object},\"sp\":${x:sp},\"ud\":${x:ud},\"token\":${x:token},\"au\":${x:au},\"ttl\":${x:ttl},\"dt_sp\":${x:dt_sp},\"hsp\":${x:hsp},\"client_token\":${queryString.client_token}}"}
// 从 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("UC parseById: fid={}, pwd_id={}, stoken={}", fid, pwdId, stoken);
// 调用第三次请求获取下载链接
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(THIRD_REQUEST_URL)
.putHeaders(header)
.sendJsonObject(bodyJson)
.onSuccess(res -> {
log.debug("UC parseById 响应: {}", res.body());
JsonObject resJson = res.bodyAsJsonObject();
if (resJson.getInteger("code") != 0) {
promise.fail(THIRD_REQUEST_URL + " 返回异常: " + resJson);
return;
}
try {
JsonArray dataList = resJson.getJsonArray("data");
if (dataList == null || dataList.isEmpty()) {
promise.fail("UC 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("解析 UC 下载链接失败: " + e.getMessage());
}
})
.onFailure(t -> promise.fail("请求下载链接失败: " + t.getMessage()));
return promise.future();
}
// public static void main(String[] args) { /*
// // https://drive.uc.cn/s/12450d1694844?public=1 // callback-var
// new UcTool(ShareLinkInfo.newBuilder().shareKey("12450d1694844").build()).parse().onSuccess( {
// System.out::println "x:au": "-",
// ); "x:ud": "4-N-5-0-6-N-3-ft-0-2-N-N",
// } "x:sp": "100",
"x:token": "4-f64fc101cfd1ded54d0c46c3c9b3929f-5-7-153611-dacb3665bbaa4f5e9d37800ec6403160-0-0-0-0-49c535178b1964c8cc50c4e09916fada",
"x:ttl": "10800"
}
// callback
{
"callbackBodyType": "application/json",
"callbackStage": "before-execute",
"callbackFailureAction": "ignore",
"callbackUrl": "https://auth-cdn.uc.cn/outer/oss/checkplay",
"callbackBody": "{\"host\":${httpHeader.host},\"size\":${size},\"range\":${httpHeader.range},\"referer\":${httpHeader.referer},\"cookie\":${httpHeader.cookie},\"method\":${httpHeader.method},\"ip\":${clientIp},\"port\":${clientPort},\"object\":${object},\"sp\":${x:sp},\"ud\":${x:ud},\"token\":${x:token},\"au\":${x:au},\"ttl\":${x:ttl},\"dt_sp\":${x:dt_sp},\"hsp\":${x:hsp},\"client_token\":${queryString.client_token}}"
}
*/
new UcTool(ShareLinkInfo.newBuilder().shareUrl("https://fast.uc.cn/s/33197dd53ace4").shareKey("33197dd53ace4").build()).parse().onSuccess(
System.out::println
);
}
} }
@@ -1,185 +0,0 @@
package cn.qaiu.util;
import java.util.*;
/**
* Cookie 工具类
* 用于过滤和处理 Cookie 字符串
*/
public class CookieUtils {
/**
* UC/夸克网盘常用的 Cookie 字段
*/
public static final List<String> UC_QUARK_COOKIE_KEYS = Arrays.asList(
"__pus", // 主要的用户会话标识(最重要)
"__kp", // 用户标识
"__kps", // 会话密钥
"__ktd", // 会话令牌
"__uid", // 用户ID
"__puus" // 用户会话签名
);
/**
* 根据指定的 key 列表过滤 cookie
*
* @param cookieStr 原始 cookie 字符串,格式如 "key1=value1; key2=value2"
* @param keys 需要保留的 cookie key 列表
* @return 过滤后的 cookie 字符串,只包含指定的 key
*/
public static String filterCookie(String cookieStr, List<String> keys) {
if (cookieStr == null || cookieStr.isEmpty()) {
return "";
}
if (keys == null || keys.isEmpty()) {
return cookieStr;
}
// 将 keys 转为 Set 以提高查找效率
Set<String> keySet = new HashSet<>(keys);
StringBuilder result = new StringBuilder();
String[] cookies = cookieStr.split(";\\s*");
for (String cookie : cookies) {
if (cookie.isEmpty()) {
continue;
}
// 提取 cookie 的 key
int equalIndex = cookie.indexOf('=');
if (equalIndex > 0) {
String key = cookie.substring(0, equalIndex).trim();
// 如果 key 在需要的列表中,保留这个 cookie
if (keySet.contains(key)) {
if (result.length() > 0) {
result.append("; ");
}
result.append(cookie);
}
}
}
return result.toString();
}
/**
* 使用 UC/夸克网盘默认的 cookie 字段过滤
*
* @param cookieStr 原始 cookie 字符串
* @return 过滤后的 cookie 字符串
*/
public static String filterUcQuarkCookie(String cookieStr) {
return filterCookie(cookieStr, UC_QUARK_COOKIE_KEYS);
}
/**
* 从 cookie 字符串中提取指定 key 的值
*
* @param cookieStr cookie 字符串
* @param key 要提取的 cookie key
* @return cookie 值,如果不存在返回 null
*/
public static String getCookieValue(String cookieStr, String key) {
if (cookieStr == null || cookieStr.isEmpty() || key == null) {
return null;
}
String[] cookies = cookieStr.split(";\\s*");
for (String cookie : cookies) {
if (cookie.startsWith(key + "=")) {
int equalIndex = cookie.indexOf('=');
if (equalIndex > 0 && equalIndex < cookie.length() - 1) {
return cookie.substring(equalIndex + 1);
}
}
}
return null;
}
/**
* 检查 cookie 字符串中是否包含指定的 key
*
* @param cookieStr cookie 字符串
* @param key 要检查的 cookie key
* @return true 表示包含该 key
*/
public static boolean containsKey(String cookieStr, String key) {
return getCookieValue(cookieStr, key) != null;
}
/**
* 更新 cookie 字符串中的指定 cookie 值
*
* @param cookieStr 原始 cookie 字符串
* @param cookieName cookie 名称
* @param newValue 新的完整 cookie 值(格式:cookieName=value)
* @return 更新后的 cookie 字符串
*/
public static String updateCookieValue(String cookieStr, String cookieName, String newValue) {
if (cookieStr == null || cookieStr.isEmpty()) {
return newValue;
}
StringBuilder result = new StringBuilder();
String[] cookies = cookieStr.split(";\\s*");
boolean found = false;
for (String cookie : cookies) {
if (cookie.startsWith(cookieName + "=")) {
// 替换为新值
if (result.length() > 0) result.append("; ");
result.append(newValue);
found = true;
} else if (!cookie.isEmpty()) {
if (result.length() > 0) result.append("; ");
result.append(cookie);
}
}
// 如果原来没有这个 cookie,添加它
if (!found) {
if (result.length() > 0) result.append("; ");
result.append(newValue);
}
return result.toString();
}
/**
* 合并多个 cookie 字符串,后面的会覆盖前面的同名 cookie
*
* @param cookieStrings cookie 字符串数组
* @return 合并后的 cookie 字符串
*/
public static String mergeCookies(String... cookieStrings) {
if (cookieStrings == null || cookieStrings.length == 0) {
return "";
}
Map<String, String> cookieMap = new LinkedHashMap<>();
for (String cookieStr : cookieStrings) {
if (cookieStr == null || cookieStr.isEmpty()) {
continue;
}
String[] cookies = cookieStr.split(";\\s*");
for (String cookie : cookies) {
if (cookie.isEmpty()) {
continue;
}
int equalIndex = cookie.indexOf('=');
if (equalIndex > 0) {
String key = cookie.substring(0, equalIndex).trim();
cookieMap.put(key, cookie);
}
}
}
return String.join("; ", cookieMap.values());
}
}
@@ -1,111 +0,0 @@
package cn.qaiu.util;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* 日期时间工具类,用于转换各种时间戳格式为可读的日期字符串
*/
public class DateTimeUtils {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final DateTimeFormatter ISO_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
/**
* 将毫秒时间戳转换为 yyyy-MM-dd HH:mm:ss 格式
* @param millis 毫秒时间戳
* @return 格式化后的日期字符串
*/
public static String formatMillisToDateTime(long millis) {
return FORMATTER.format(Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()).toLocalDateTime());
}
/**
* 将毫秒时间戳字符串转换为 yyyy-MM-dd HH:mm:ss 格式
* @param millisStr 毫秒时间戳字符串(如 "1684737715067")
* @return 格式化后的日期字符串
*/
public static String formatMillisStringToDateTime(String millisStr) {
if (millisStr == null || millisStr.trim().isEmpty()) {
return "";
}
try {
long millis = Long.parseLong(millisStr.trim());
return formatMillisToDateTime(millis);
} catch (NumberFormatException e) {
// 如果解析失败,返回原始值
return millisStr;
}
}
/**
* 将秒级时间戳转换为 yyyy-MM-dd HH:mm:ss 格式
* @param seconds 秒级时间戳
* @return 格式化后的日期字符串
*/
public static String formatSecondsToDateTime(long seconds) {
return FORMATTER.format(Instant.ofEpochSecond(seconds).atZone(ZoneId.systemDefault()).toLocalDateTime());
}
/**
* 将秒级时间戳字符串转换为 yyyy-MM-dd HH:mm:ss 格式
* @param secondsStr 秒级时间戳字符串
* @return 格式化后的日期字符串
*/
public static String formatSecondsStringToDateTime(String secondsStr) {
if (secondsStr == null || secondsStr.trim().isEmpty()) {
return "";
}
try {
long seconds = Long.parseLong(secondsStr.trim());
return formatSecondsToDateTime(seconds);
} catch (NumberFormatException e) {
// 如果解析失败,返回原始值
return secondsStr;
}
}
/**
* 智能转换时间戳:自动判断是毫秒还是秒级
* 根据值的大小判断:如果大于等于 10000000000(即 2286-11-20),视为毫秒;否则视为秒级
* @param timestamp 时间戳字符串
* @return 格式化后的日期字符串
*/
public static String formatTimestampToDateTime(String timestamp) {
if (timestamp == null || timestamp.trim().isEmpty()) {
return "";
}
try {
long value = Long.parseLong(timestamp.trim());
// 10000000000 对应 2286-11-20(毫秒)或 1970-04-26(秒级)
// 使用 10^10 作为分界线
if (value >= 10_000_000_000L) {
return formatMillisToDateTime(value);
} else {
return formatSecondsToDateTime(value);
}
} catch (NumberFormatException e) {
// 如果是 ISO 8601 格式,尝试解析
return formatISODateTime(timestamp);
}
}
/**
* 解析并格式化 ISO 8601 格式的日期时间字符串
* @param isoDateTime ISO 8601 格式的日期时间字符串
* @return 格式化后的日期字符串
*/
public static String formatISODateTime(String isoDateTime) {
if (isoDateTime == null || isoDateTime.trim().isEmpty()) {
return "";
}
try {
OffsetDateTime offsetDateTime = OffsetDateTime.parse(isoDateTime, ISO_FORMATTER);
return FORMATTER.format(offsetDateTime.toLocalDateTime());
} catch (Exception e) {
// 如果格式化失败,直接返回原始值
return isoDateTime;
}
}
}
@@ -1,105 +1,105 @@
// // ==UserScript== // ==UserScript==
// // @name Fetch API示例解析器 // @name Fetch API示例解析器
// // @type fetch_demo // @type fetch_demo
// // @displayName Fetch演示 // @displayName Fetch演示
// // @description 演示如何在ES5环境中使用fetch API和async/await // @description 演示如何在ES5环境中使用fetch API和async/await
// // @match https?://example\.com/s/(?<KEY>\w+) // @match https?://example\.com/s/(?<KEY>\w+)
// // @author QAIU // @author QAIU
// // @version 1.0.0 // @version 1.0.0
// // ==/UserScript== // ==/UserScript==
// // 使用require导入类型定义(仅用于IDE类型提示) // 使用require导入类型定义(仅用于IDE类型提示)
// var types = require('./types'); var types = require('./types');
// /** @typedef {types.ShareLinkInfo} ShareLinkInfo */ /** @typedef {types.ShareLinkInfo} ShareLinkInfo */
// /** @typedef {types.JsHttpClient} JsHttpClient */ /** @typedef {types.JsHttpClient} JsHttpClient */
// /** @typedef {types.JsLogger} JsLogger */ /** @typedef {types.JsLogger} JsLogger */
// /** /**
// * 演示使用fetch API的解析器 * 演示使用fetch API的解析器
// * 注意:虽然源码中使用了ES6+语法(async/await),但在浏览器中会被编译为ES5 * 注意:虽然源码中使用了ES6+语法(async/await),但在浏览器中会被编译为ES5
// * *
// * @param {ShareLinkInfo} shareLinkInfo - 分享链接信息 * @param {ShareLinkInfo} shareLinkInfo - 分享链接信息
// * @param {JsHttpClient} http - HTTP客户端(传统方式) * @param {JsHttpClient} http - HTTP客户端(传统方式)
// * @param {JsLogger} logger - 日志对象 * @param {JsLogger} logger - 日志对象
// * @returns {string} 下载链接 * @returns {string} 下载链接
// */ */
// function parse(shareLinkInfo, http, logger) { function parse(shareLinkInfo, http, logger) {
// logger.info("=== Fetch API Demo ==="); logger.info("=== Fetch API Demo ===");
// // 方式1:使用传统的http对象(同步) // 方式1:使用传统的http对象(同步)
// logger.info("方式1: 使用传统http对象"); logger.info("方式1: 使用传统http对象");
// var response1 = http.get("https://httpbin.org/get"); var response1 = http.get("https://httpbin.org/get");
// logger.info("状态码: " + response1.statusCode()); logger.info("状态码: " + response1.statusCode());
// // 方式2:使用fetch API(基于Promise) // 方式2:使用fetch API(基于Promise)
// logger.info("方式2: 使用fetch API"); logger.info("方式2: 使用fetch API");
// // 注意:在ES5环境中,我们需要手动处理Promise // 注意:在ES5环境中,我们需要手动处理Promise
// // 这个示例展示了如何在ES5中使用fetch // 这个示例展示了如何在ES5中使用fetch
// var fetchPromise = fetch("https://httpbin.org/get"); var fetchPromise = fetch("https://httpbin.org/get");
// // 等待Promise完成(同步等待模拟) // 等待Promise完成(同步等待模拟)
// var result = null; var result = null;
// var error = null; var error = null;
// fetchPromise fetchPromise
// .then(function(response) { .then(function(response) {
// logger.info("Fetch响应状态: " + response.status); logger.info("Fetch响应状态: " + response.status);
// return response.text(); return response.text();
// }) })
// .then(function(text) { .then(function(text) {
// logger.info("Fetch响应内容: " + text.substring(0, 100) + "..."); logger.info("Fetch响应内容: " + text.substring(0, 100) + "...");
// result = "https://example.com/download/demo.file"; result = "https://example.com/download/demo.file";
// }) })
// ['catch'](function(err) { ['catch'](function(err) {
// logger.error("Fetch失败: " + err.message); logger.error("Fetch失败: " + err.message);
// error = err; error = err;
// }); });
// // 简单的等待循环(实际场景中不推荐,这里仅作演示) // 简单的等待循环(实际场景中不推荐,这里仅作演示)
// var timeout = 5000; // 5秒超时 var timeout = 5000; // 5秒超时
// var start = Date.now(); var start = Date.now();
// while (result === null && error === null && (Date.now() - start) < timeout) { while (result === null && error === null && (Date.now() - start) < timeout) {
// // 等待Promise完成 // 等待Promise完成
// java.lang.Thread.sleep(10); java.lang.Thread.sleep(10);
// } }
// if (error !== null) { if (error !== null) {
// throw error; throw error;
// } }
// if (result === null) { if (result === null) {
// throw new Error("Fetch超时"); throw new Error("Fetch超时");
// } }
// return result; return result;
// } }
// /** /**
// * 演示POST请求 * 演示POST请求
// */ */
// function demonstratePost(logger) { function demonstratePost(logger) {
// logger.info("=== 演示POST请求 ==="); logger.info("=== 演示POST请求 ===");
// var postPromise = fetch("https://httpbin.org/post", { var postPromise = fetch("https://httpbin.org/post", {
// method: "POST", method: "POST",
// headers: { headers: {
// "Content-Type": "application/json" "Content-Type": "application/json"
// }, },
// body: JSON.stringify({ body: JSON.stringify({
// key: "value", key: "value",
// demo: true demo: true
// }) })
// }); });
// postPromise postPromise
// .then(function(response) { .then(function(response) {
// return response.json(); return response.json();
// }) })
// .then(function(data) { .then(function(data) {
// logger.info("POST响应: " + JSON.stringify(data)); logger.info("POST响应: " + JSON.stringify(data));
// }) })
// ['catch'](function(err) { ['catch'](function(err) {
// logger.error("POST失败: " + err.message); logger.error("POST失败: " + err.message);
// }); });
// } }
@@ -7,14 +7,11 @@ import java.util.Arrays;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.regex.Pattern.compile; import static java.util.regex.Pattern.compile;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/** /**
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
@@ -80,254 +77,15 @@ 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 testLzPatternWebgetstore() {
Pattern lzPattern = PanDomainTemplate.LZ.getPattern();
// webgetstore.com 以前遗漏,现已补入
Matcher m1 = lzPattern.matcher("https://webgetstore.com/somekey");
assertTrue("LZ should match webgetstore.com", m1.find());
assertEquals("somekey", m1.group("KEY"));
Matcher m2 = lzPattern.matcher("https://www.webgetstore.com/somekey");
assertTrue("LZ should match www.webgetstore.com", m2.find());
assertEquals("somekey", m2.group("KEY"));
// t-is.cn 以前遗漏,现已补入
Matcher m3 = lzPattern.matcher("https://t-is.cn/somekey");
assertTrue("LZ should match t-is.cn", m3.find());
assertEquals("somekey", m3.group("KEY"));
Matcher m4 = lzPattern.matcher("https://www.t-is.cn/somekey");
assertTrue("LZ should match www.t-is.cn", m4.find());
assertEquals("somekey", m4.group("KEY"));
// 已有域名仍然正常匹配
Matcher m5 = lzPattern.matcher("https://www.lanzoul.com/somekey");
assertTrue("LZ should match existing domain lanzoul.com", m5.find());
assertEquals("somekey", m5.group("KEY"));
}
@Test
public void testLePatternFix() {
Pattern lePattern = PanDomainTemplate.LE.getPattern();
// /share/ 格式应匹配
Matcher m1 = lePattern.matcher("https://lecloud.lenovo.com/share/abc123");
assertTrue("LE should match /share/ format", m1.find());
assertEquals("abc123", m1.group("KEY"));
// /mshare/ 格式应匹配
Matcher m2 = lePattern.matcher("https://lecloud.lenovo.com/mshare/xyz789");
assertTrue("LE should match /mshare/ format", m2.find());
assertEquals("xyz789", m2.group("KEY"));
// leclou.lenovo.com (去掉'd') 不应匹配
assertFalse("LE should NOT match leclou.lenovo.com",
lePattern.matcher("https://leclou.lenovo.com/share/abc123").find());
// 错误路径不应匹配
assertFalse("LE should NOT match wrong path",
lePattern.matcher("https://lecloud.lenovo.com/s/abc123").find());
}
@Test
public void testCowPatternFix() {
Pattern cowPattern = PanDomainTemplate.COW.getPattern();
// 正常域名
Matcher m1 = cowPattern.matcher("https://cowtransfer.com/s/abc123");
assertTrue("COW should match cowtransfer.com", m1.find());
assertEquals("abc123", m1.group("KEY"));
Matcher m2 = cowPattern.matcher("https://share.cowtransfer.com/s/abc123");
assertTrue("COW should match share.cowtransfer.com", m2.find());
assertEquals("abc123", m2.group("KEY"));
// 潜在的URL注入:`(.*)` 是贪婪捕获组,可匹配 `evil.com/redirect/` 等前缀,
// 使形如 `https://evil.com/redirect/cowtransfer.com/s/key` 的 URL 被误识别。
// 修复后改为 `(?:[a-zA-Z\d-]+\.)?` 仅匹配一级合法子域名(可选),消除误匹配。
assertFalse("COW should NOT match redirect URLs containing cowtransfer.com in path",
cowPattern.matcher("https://evil.com/redirect/cowtransfer.com/s/abc").find());
}
@Test
public void testMnePatternFix() {
Pattern mnePattern = PanDomainTemplate.MNE.getPattern();
// 带 #/ 前缀的完整网页链接(修复前因 (y.) 未转义而存在 bug)
Matcher m1 = mnePattern.matcher("https://music.163.com/#/song?id=12345");
assertTrue("MNE should match #/song format", m1.find());
assertEquals("12345", m1.group("KEY"));
// 带 m/ 前缀的移动端链接
Matcher m2 = mnePattern.matcher("https://music.163.com/m/song?id=12345");
assertTrue("MNE should match m/song format", m2.find());
assertEquals("12345", m2.group("KEY"));
// y.music.163.com 子域名
Matcher m3 = mnePattern.matcher("https://y.music.163.com/song?id=12345");
assertTrue("MNE should match y.music.163.com", m3.find());
assertEquals("12345", m3.group("KEY"));
// 原 (y.) 中 `.` 未转义(`.` 匹配任意字符):对于 `yXmusic.163.com`,
// `(y.)` 会消费 `yX`(y + 任意字符),剩余 `music.163.com` 再被 `music\.163\.com` 匹配,导致误匹配。
// 修复后 `(y\.)` 要求字面 `.`,`yX` 中 X ≠ `.` 无法匹配,不再误匹配。
assertFalse("MNE should NOT match yXmusic.163.com (old (y.) could erroneously match via backtracking)",
mnePattern.matcher("https://yXmusic.163.com/song?id=12345").find());
}
@Test
public void testP115PatternFix() {
Pattern p115Pattern = PanDomainTemplate.P115.getPattern();
// 正常匹配
Matcher m1 = p115Pattern.matcher("https://115.com/s/abc123");
assertTrue("P115 should match 115.com", m1.find());
assertEquals("abc123", m1.group("KEY"));
Matcher m2 = p115Pattern.matcher("https://anxia.com/s/abc123");
assertTrue("P115 should match anxia.com", m2.find());
assertEquals("abc123", m2.group("KEY"));
// 原 .com 未转义时 115Xcom 会被误匹配(现已修复)
assertFalse("P115 should NOT match 115Xcom",
p115Pattern.matcher("https://115Xcom/s/abc123").find());
}
@Test
public void testPgdSubdomain() {
Pattern pgdPattern = PanDomainTemplate.PGD.getPattern();
// 标准链接
Matcher m1 = pgdPattern.matcher("https://drive.google.com/file/d/abc123/view?usp=sharing");
assertTrue("PGD should match standard drive.google.com", m1.find());
assertEquals("abc123", m1.group("KEY"));
// 带子域名的链接(修复后支持)
Matcher m2 = pgdPattern.matcher("https://adsd.drive.google.com/file/d/151bR-nk-tOBm9QAFaozJIVt2WYyCMkoz/view");
assertTrue("PGD should match subdomain.drive.google.com", m2.find());
assertEquals("151bR-nk-tOBm9QAFaozJIVt2WYyCMkoz", m2.group("KEY"));
}
@Test
public void testFsPatternMatching() {
Pattern fsPattern = PanDomainTemplate.FS.getPattern();
// 文件链接
Matcher m1 = fsPattern.matcher(
"https://kcncuknojm60.feishu.cn/file/VnCxbt35KoowKoxldO3c3C7VnMc");
assertTrue("FS should match file link", m1.matches());
assertEquals("VnCxbt35KoowKoxldO3c3C7VnMc", m1.group("KEY"));
// 文件链接带 ?from=from_copylink
Matcher m2 = fsPattern.matcher(
"https://kcncuknojm60.feishu.cn/file/VnCxbt35KoowKoxldO3c3C7VnMc?from=from_copylink");
assertTrue("FS should match file link with query param", m2.matches());
assertEquals("VnCxbt35KoowKoxldO3c3C7VnMc", m2.group("KEY"));
// 文件夹链接
Matcher m3 = fsPattern.matcher(
"https://kcncuknojm60.feishu.cn/drive/folder/RQSKf8EQ4l7dMedqzHucpMbancg");
assertTrue("FS should match folder link", m3.matches());
assertEquals("RQSKf8EQ4l7dMedqzHucpMbancg", m3.group("KEY"));
// 文件夹链接带 ?from=from_copylink
Matcher m4 = fsPattern.matcher(
"https://kcncuknojm60.feishu.cn/drive/folder/RQSKf8EQ4l7dMedqzHucpMbancg?from=from_copylink");
assertTrue("FS should match folder link with query param", m4.matches());
assertEquals("RQSKf8EQ4l7dMedqzHucpMbancg", m4.group("KEY"));
// 不同的 tenant 子域名
Matcher m5 = fsPattern.matcher(
"https://pokepangle.feishu.cn/file/VW30bpK74ontiTxvRg1cZcgvnGg");
assertTrue("FS should match different tenant", m5.matches());
assertEquals("VW30bpK74ontiTxvRg1cZcgvnGg", m5.group("KEY"));
// 负例: 非feishu域名不匹配
assertFalse("FS should NOT match non-feishu domain",
fsPattern.matcher("https://evil.com/file/abc123").matches());
// 负例: feishu.cn上的其他路径不匹配
assertFalse("FS should NOT match other feishu paths",
fsPattern.matcher("https://xxx.feishu.cn/docs/abc123").matches());
}
@Test
public void testFsFromShareUrl() {
// 测试文件链接解析
String fileUrl = "https://kcncuknojm60.feishu.cn/file/VnCxbt35KoowKoxldO3c3C7VnMc?from=from_copylink";
ParserCreate parserCreate = ParserCreate.fromShareUrl(fileUrl);
ShareLinkInfo info = parserCreate.getShareLinkInfo();
assertNotNull("ShareLinkInfo should not be null", info);
assertEquals("fs", info.getType());
assertEquals("飞书云盘", info.getPanName());
assertEquals("VnCxbt35KoowKoxldO3c3C7VnMc", info.getShareKey());
// 测试文件夹链接解析
String folderUrl = "https://kcncuknojm60.feishu.cn/drive/folder/RQSKf8EQ4l7dMedqzHucpMbancg";
ParserCreate parserCreate2 = ParserCreate.fromShareUrl(folderUrl);
ShareLinkInfo info2 = parserCreate2.getShareLinkInfo();
assertNotNull("ShareLinkInfo should not be null", info2);
assertEquals("fs", info2.getType());
assertEquals("飞书云盘", info2.getPanName());
assertEquals("RQSKf8EQ4l7dMedqzHucpMbancg", info2.getShareKey());
}
@Test @Test
public void verifyDuplicates() { public void verifyDuplicates() {
Matcher matcher = compile("https://(?:[a-zA-Z\\d-]+\\.)?drive\\.google\\.com/file/d/(?<KEY>.+)/view(\\?usp=(sharing|drive_link))?")
.matcher("https://adsd.drive.google.com/file/d/151bR-nk-tOBm9QAFaozJIVt2WYyCMkoz/view");
if (matcher.find()) {
System.out.println(matcher.group());
System.out.println(matcher.group("KEY"));
}
// 校验重复 // 校验重复
Set<String> collect = Set<String> collect =
Arrays.stream(PanDomainTemplate.values()).map(PanDomainTemplate::getRegex).collect(Collectors.toSet()); Arrays.stream(PanDomainTemplate.values()).map(PanDomainTemplate::getRegex).collect(Collectors.toSet());
@@ -1,209 +0,0 @@
package cn.qaiu.parser.auth;
import cn.qaiu.util.CookieUtils;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Cookie 工具测试
*/
public class AuthParamTest {
@Test
public void testCookieFilter() {
System.out.println("\n=== 测试 Cookie 过滤功能 ===");
// 测试 Cookie 过滤功能
String fullCookie = "__pus=abc123; __kp=def456; other_cookie=xyz; __puus=token789; random=test; __uid=user001";
String filtered = CookieUtils.filterUcQuarkCookie(fullCookie);
System.out.println("原始 Cookie: " + fullCookie);
System.out.println("过滤后 Cookie: " + filtered);
// 验证包含必要字段
assertTrue("应包含 __pus", filtered.contains("__pus=abc123"));
assertTrue("应包含 __kp", filtered.contains("__kp=def456"));
assertTrue("应包含 __puus", filtered.contains("__puus=token789"));
assertTrue("应包含 __uid", filtered.contains("__uid=user001"));
// 验证不包含不必要字段
assertFalse("不应包含 other_cookie", filtered.contains("other_cookie"));
assertFalse("不应包含 random", filtered.contains("random"));
System.out.println("✓ Cookie 过滤测试通过");
}
@Test
public void testCookieGetValue() {
System.out.println("\n=== 测试获取 Cookie 值 ===");
String cookie = "__pus=value1; __kp=value2; __puus=value3";
String pus = CookieUtils.getCookieValue(cookie, "__pus");
String kp = CookieUtils.getCookieValue(cookie, "__kp");
String puus = CookieUtils.getCookieValue(cookie, "__puus");
String notexist = CookieUtils.getCookieValue(cookie, "notexist");
System.out.println("Cookie: " + cookie);
System.out.println("__pus = " + pus);
System.out.println("__kp = " + kp);
System.out.println("__puus = " + puus);
System.out.println("notexist = " + notexist);
assertEquals("value1", pus);
assertEquals("value2", kp);
assertEquals("value3", puus);
assertNull(notexist);
System.out.println("✓ 获取 Cookie 值测试通过");
}
@Test
public void testCookieUpdate() {
System.out.println("\n=== 测试更新 Cookie ===");
String cookie = "__pus=old_pus; __kp=value2";
String updated = CookieUtils.updateCookieValue(cookie, "__puus", "__puus=new_puus_value");
System.out.println("更新前: " + cookie);
System.out.println("更新后: " + updated);
assertTrue("应包含新的 __puus", updated.contains("__puus=new_puus_value"));
assertTrue("应保留 __pus", updated.contains("__pus=old_pus"));
assertTrue("应保留 __kp", updated.contains("__kp=value2"));
// 测试替换已存在的值
String cookie2 = "__pus=old_value; __kp=value2; __puus=old_puus";
String updated2 = CookieUtils.updateCookieValue(cookie2, "__puus", "__puus=updated_puus");
System.out.println("替换测试 - 更新前: " + cookie2);
System.out.println("替换测试 - 更新后: " + updated2);
assertTrue("应包含更新的 __puus", updated2.contains("__puus=updated_puus"));
assertFalse("不应包含旧的 __puus", updated2.contains("__puus=old_puus"));
System.out.println("✓ 更新 Cookie 测试通过");
}
@Test
public void testCookieContainsKey() {
System.out.println("\n=== 测试检查 Cookie key 存在性 ===");
String cookie = "__pus=value1; __kp=value2; __uid=user123";
boolean hasPus = CookieUtils.containsKey(cookie, "__pus");
boolean hasKp = CookieUtils.containsKey(cookie, "__kp");
boolean hasPuus = CookieUtils.containsKey(cookie, "__puus");
boolean hasNotexist = CookieUtils.containsKey(cookie, "notexist");
System.out.println("Cookie: " + cookie);
System.out.println("containsKey(__pus): " + hasPus);
System.out.println("containsKey(__kp): " + hasKp);
System.out.println("containsKey(__puus): " + hasPuus);
System.out.println("containsKey(notexist): " + hasNotexist);
assertTrue(hasPus);
assertTrue(hasKp);
assertFalse(hasPuus);
assertFalse(hasNotexist);
System.out.println("✓ 检查 Cookie key 测试通过");
}
@Test
public void testEmptyCookieHandling() {
System.out.println("\n=== 测试空 Cookie 处理 ===");
// 测试空 Cookie 处理
String emptyFiltered = CookieUtils.filterUcQuarkCookie("");
String nullFiltered = CookieUtils.filterUcQuarkCookie(null);
String emptyValue = CookieUtils.getCookieValue("", "__pus");
String nullValue = CookieUtils.getCookieValue(null, "__pus");
System.out.println("filterUcQuarkCookie(''): '" + emptyFiltered + "'");
System.out.println("filterUcQuarkCookie(null): '" + nullFiltered + "'");
System.out.println("getCookieValue('', '__pus'): " + emptyValue);
System.out.println("getCookieValue(null, '__pus'): " + nullValue);
assertEquals("", emptyFiltered);
assertEquals("", nullFiltered);
assertNull(emptyValue);
assertNull(nullValue);
System.out.println("✓ 空 Cookie 处理测试通过");
}
@Test
public void testComplexCookieScenario() {
System.out.println("\n=== 测试复杂场景:模拟 UC/夸克 Cookie 处理流程 ===");
// 模拟从浏览器获取的完整 Cookie
String browserCookie = "session_id=xxx; __pus=main_token_here; other=value; " +
"__kp=key123; __kps=secret456; __ktd=token789; " +
"__uid=user001; random_cookie=test; __puus=old_signature";
System.out.println("1. 浏览器原始 Cookie:");
System.out.println(" " + browserCookie);
// 第一步:过滤出必要的字段
String filtered = CookieUtils.filterUcQuarkCookie(browserCookie);
System.out.println("\n2. 过滤后的 Cookie (只保留 UC/夸克必需字段):");
System.out.println(" " + filtered);
// 验证过滤结果
assertTrue("应包含 __pus", filtered.contains("__pus=main_token_here"));
assertTrue("应包含 __kp", filtered.contains("__kp=key123"));
assertTrue("应包含 __puus", filtered.contains("__puus=old_signature"));
assertFalse("不应包含 session_id", filtered.contains("session_id"));
assertFalse("不应包含 random_cookie", filtered.contains("random_cookie"));
// 第二步:模拟刷新 __puus (从服务器获取新的签名)
String newPuus = "__puus=refreshed_signature_from_server";
String updated = CookieUtils.updateCookieValue(filtered, "__puus", newPuus);
System.out.println("\n3. 刷新 __puus 后的 Cookie:");
System.out.println(" " + updated);
// 验证更新结果
assertTrue("应包含新的 __puus", updated.contains("__puus=refreshed_signature_from_server"));
assertFalse("不应包含旧的 __puus", updated.contains("__puus=old_signature"));
assertTrue("应保留 __pus", updated.contains("__pus=main_token_here"));
// 第三步:验证可以获取单个值
String pusValue = CookieUtils.getCookieValue(updated, "__pus");
String puusValue = CookieUtils.getCookieValue(updated, "__puus");
System.out.println("\n4. 提取单个 Cookie 值:");
System.out.println(" __pus = " + pusValue);
System.out.println(" __puus = " + puusValue);
assertEquals("main_token_here", pusValue);
assertEquals("refreshed_signature_from_server", puusValue);
System.out.println("\n✓ 复杂场景测试通过");
}
@Test
public void testAllUcQuarkCookieFields() {
System.out.println("\n=== 测试所有 UC/夸克 Cookie 必需字段 ===");
// 包含所有必需字段的 Cookie
String fullCookie = "__pus=token1; __kp=token2; __kps=token3; " +
"__ktd=token4; __uid=token5; __puus=token6; " +
"extra1=value1; extra2=value2";
String filtered = CookieUtils.filterUcQuarkCookie(fullCookie);
System.out.println("原始 Cookie: " + fullCookie);
System.out.println("过滤后: " + filtered);
System.out.println("\n验证必需字段:");
// 验证所有必需字段都被保留
String[] requiredFields = {"__pus", "__kp", "__kps", "__ktd", "__uid", "__puus"};
for (String field : requiredFields) {
boolean contains = CookieUtils.containsKey(filtered, field);
System.out.println(" - " + field + ": " + (contains ? "✓" : "✗"));
assertTrue("应包含 " + field, contains);
}
// 验证额外字段被过滤掉
assertFalse("不应包含 extra1", filtered.contains("extra1"));
assertFalse("不应包含 extra2", filtered.contains("extra2"));
System.out.println("\n✓ 所有字段测试通过");
}
}
@@ -1,209 +0,0 @@
package cn.qaiu.parser.auth;
import cn.qaiu.util.CookieUtils;
/**
* 手动测试 Cookie 工具类
*/
public class CookieUtilsManualTest {
public static void main(String[] args) {
System.out.println("========================================");
System.out.println(" Cookie 工具类手动测试");
System.out.println("========================================\n");
testCookieFilter();
testCookieGetValue();
testCookieUpdate();
testCookieContainsKey();
testEmptyCookieHandling();
testComplexScenario();
testAllUcQuarkCookieFields();
System.out.println("\n========================================");
System.out.println(" 所有测试通过! ✓");
System.out.println("========================================");
}
private static void testCookieFilter() {
System.out.println("=== 测试 Cookie 过滤功能 ===");
String fullCookie = "__pus=abc123; __kp=def456; other_cookie=xyz; __puus=token789; random=test; __uid=user001";
String filtered = CookieUtils.filterUcQuarkCookie(fullCookie);
System.out.println("原始 Cookie: " + fullCookie);
System.out.println("过滤后 Cookie: " + filtered);
assert filtered.contains("__pus=abc123") : "应包含 __pus";
assert filtered.contains("__kp=def456") : "应包含 __kp";
assert filtered.contains("__puus=token789") : "应包含 __puus";
assert !filtered.contains("other_cookie") : "不应包含 other_cookie";
assert !filtered.contains("random") : "不应包含 random";
System.out.println("✓ Cookie 过滤测试通过\n");
}
private static void testCookieGetValue() {
System.out.println("=== 测试获取 Cookie 值 ===");
String cookie = "__pus=value1; __kp=value2; __puus=value3";
String pus = CookieUtils.getCookieValue(cookie, "__pus");
String kp = CookieUtils.getCookieValue(cookie, "__kp");
String puus = CookieUtils.getCookieValue(cookie, "__puus");
String notexist = CookieUtils.getCookieValue(cookie, "notexist");
System.out.println("Cookie: " + cookie);
System.out.println("__pus = " + pus);
System.out.println("__kp = " + kp);
System.out.println("__puus = " + puus);
System.out.println("notexist = " + notexist);
assert "value1".equals(pus) : "__pus 应为 value1";
assert "value2".equals(kp) : "__kp 应为 value2";
assert "value3".equals(puus) : "__puus 应为 value3";
assert notexist == null : "notexist 应为 null";
System.out.println("✓ 获取 Cookie 值测试通过\n");
}
private static void testCookieUpdate() {
System.out.println("=== 测试更新 Cookie ===");
String cookie = "__pus=old_pus; __kp=value2";
String updated = CookieUtils.updateCookieValue(cookie, "__puus", "__puus=new_puus_value");
System.out.println("更新前: " + cookie);
System.out.println("更新后: " + updated);
assert updated.contains("__puus=new_puus_value") : "应包含新的 __puus";
assert updated.contains("__pus=old_pus") : "应保留 __pus";
assert updated.contains("__kp=value2") : "应保留 __kp";
// 测试替换已存在的值
String cookie2 = "__pus=old_value; __kp=value2; __puus=old_puus";
String updated2 = CookieUtils.updateCookieValue(cookie2, "__puus", "__puus=updated_puus");
System.out.println("替换测试 - 更新前: " + cookie2);
System.out.println("替换测试 - 更新后: " + updated2);
assert updated2.contains("__puus=updated_puus") : "应包含更新的 __puus";
assert !updated2.contains("__puus=old_puus") : "不应包含旧的 __puus";
System.out.println("✓ 更新 Cookie 测试通过\n");
}
private static void testCookieContainsKey() {
System.out.println("=== 测试检查 Cookie key 存在性 ===");
String cookie = "__pus=value1; __kp=value2; __uid=user123";
boolean hasPus = CookieUtils.containsKey(cookie, "__pus");
boolean hasKp = CookieUtils.containsKey(cookie, "__kp");
boolean hasPuus = CookieUtils.containsKey(cookie, "__puus");
boolean hasNotexist = CookieUtils.containsKey(cookie, "notexist");
System.out.println("Cookie: " + cookie);
System.out.println("containsKey(__pus): " + hasPus);
System.out.println("containsKey(__kp): " + hasKp);
System.out.println("containsKey(__puus): " + hasPuus);
System.out.println("containsKey(notexist): " + hasNotexist);
assert hasPus : "__pus 应存在";
assert hasKp : "__kp 应存在";
assert !hasPuus : "__puus 不应存在";
assert !hasNotexist : "notexist 不应存在";
System.out.println("✓ 检查 Cookie key 测试通过\n");
}
private static void testEmptyCookieHandling() {
System.out.println("=== 测试空 Cookie 处理 ===");
String emptyFiltered = CookieUtils.filterUcQuarkCookie("");
String nullFiltered = CookieUtils.filterUcQuarkCookie(null);
String emptyValue = CookieUtils.getCookieValue("", "__pus");
String nullValue = CookieUtils.getCookieValue(null, "__pus");
System.out.println("filterUcQuarkCookie(''): '" + emptyFiltered + "'");
System.out.println("filterUcQuarkCookie(null): '" + nullFiltered + "'");
System.out.println("getCookieValue('', '__pus'): " + emptyValue);
System.out.println("getCookieValue(null, '__pus'): " + nullValue);
assert "".equals(emptyFiltered) : "空字符串应返回空字符串";
assert "".equals(nullFiltered) : "null 应返回空字符串";
assert emptyValue == null : "空字符串的值应为 null";
assert nullValue == null : "null 的值应为 null";
System.out.println("✓ 空 Cookie 处理测试通过\n");
}
private static void testComplexScenario() {
System.out.println("=== 测试复杂场景:模拟 UC/夸克 Cookie 处理流程 ===");
// 模拟从浏览器获取的完整 Cookie
String browserCookie = "session_id=xxx; __pus=main_token_here; other=value; " +
"__kp=key123; __kps=secret456; __ktd=token789; " +
"__uid=user001; random_cookie=test; __puus=old_signature";
System.out.println("1. 浏览器原始 Cookie:");
System.out.println(" " + browserCookie);
// 第一步:过滤出必要的字段
String filtered = CookieUtils.filterUcQuarkCookie(browserCookie);
System.out.println("\n2. 过滤后的 Cookie (只保留 UC/夸克必需字段):");
System.out.println(" " + filtered);
assert filtered.contains("__pus=main_token_here") : "应包含 __pus";
assert filtered.contains("__kp=key123") : "应包含 __kp";
assert filtered.contains("__puus=old_signature") : "应包含 __puus";
assert !filtered.contains("session_id") : "不应包含 session_id";
assert !filtered.contains("random_cookie") : "不应包含 random_cookie";
// 第二步:模拟刷新 __puus
String newPuus = "__puus=refreshed_signature_from_server";
String updated = CookieUtils.updateCookieValue(filtered, "__puus", newPuus);
System.out.println("\n3. 刷新 __puus 后的 Cookie:");
System.out.println(" " + updated);
assert updated.contains("__puus=refreshed_signature_from_server") : "应包含新的 __puus";
assert !updated.contains("__puus=old_signature") : "不应包含旧的 __puus";
assert updated.contains("__pus=main_token_here") : "应保留 __pus";
// 第三步:验证可以获取单个值
String pusValue = CookieUtils.getCookieValue(updated, "__pus");
String puusValue = CookieUtils.getCookieValue(updated, "__puus");
System.out.println("\n4. 提取单个 Cookie 值:");
System.out.println(" __pus = " + pusValue);
System.out.println(" __puus = " + puusValue);
assert "main_token_here".equals(pusValue) : "__pus 应为 main_token_here";
assert "refreshed_signature_from_server".equals(puusValue) : "__puus 应为 refreshed_signature_from_server";
System.out.println("\n✓ 复杂场景测试通过\n");
}
private static void testAllUcQuarkCookieFields() {
System.out.println("=== 测试所有 UC/夸克 Cookie 必需字段 ===");
// 包含所有必需字段的 Cookie
String fullCookie = "__pus=token1; __kp=token2; __kps=token3; " +
"__ktd=token4; __uid=token5; __puus=token6; " +
"extra1=value1; extra2=value2";
String filtered = CookieUtils.filterUcQuarkCookie(fullCookie);
System.out.println("原始 Cookie: " + fullCookie);
System.out.println("过滤后: " + filtered);
System.out.println("\n验证必需字段:");
// 验证所有必需字段都被保留
String[] requiredFields = {"__pus", "__kp", "__kps", "__ktd", "__uid", "__puus"};
for (String field : requiredFields) {
boolean contains = CookieUtils.containsKey(filtered, field);
System.out.println(" - " + field + ": " + (contains ? "✓" : "✗"));
assert contains : "应包含 " + field;
}
// 验证额外字段被过滤掉
assert !filtered.contains("extra1") : "不应包含 extra1";
assert !filtered.contains("extra2") : "不应包含 extra2";
System.out.println("\n✓ 所有字段测试通过\n");
}
}
@@ -122,12 +122,12 @@ public class ClientLinkExample {
// 使用便捷工具类 // 使用便捷工具类
String curlCommand = ClientLinkUtils.generateCurlCommand(shareLinkInfo); String curlCommand = ClientLinkUtils.generateCurlCommand(shareLinkInfo);
String aria2Command = ClientLinkUtils.generateAria2Command(shareLinkInfo); String wgetCommand = ClientLinkUtils.generateWgetCommand(shareLinkInfo);
String thunderLink = ClientLinkUtils.generateThunderLink(shareLinkInfo); String thunderLink = ClientLinkUtils.generateThunderLink(shareLinkInfo);
log.info("=== 使用便捷工具类生成的链接 ==="); log.info("=== 使用便捷工具类生成的链接 ===");
log.info("cURL命令: {}", curlCommand); log.info("cURL命令: {}", curlCommand);
log.info("Aria2命令: {}", aria2Command); log.info("wget命令: {}", wgetCommand);
log.info("迅雷链接: {}", thunderLink); log.info("迅雷链接: {}", thunderLink);
} catch (Exception e) { } catch (Exception e) {
@@ -0,0 +1,262 @@
package cn.qaiu.parser.clientlink;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import cn.qaiu.parser.clientlink.impl.CurlLinkGenerator;
import cn.qaiu.parser.clientlink.impl.ThunderLinkGenerator;
import cn.qaiu.parser.clientlink.impl.Aria2LinkGenerator;
import cn.qaiu.parser.clientlink.impl.PowerShellLinkGenerator;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/**
* 客户端链接生成器功能测试
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class ClientLinkGeneratorTest {
private ShareLinkInfo shareLinkInfo;
private DownloadLinkMeta meta;
@Before
public void setUp() {
// 创建测试用的 ShareLinkInfo
shareLinkInfo = ShareLinkInfo.newBuilder()
.type("test")
.panName("测试网盘")
.shareUrl("https://example.com/share/test")
.build();
Map<String, Object> otherParam = new HashMap<>();
otherParam.put("downloadUrl", "https://example.com/file.zip");
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Test Browser)");
headers.put("Referer", "https://example.com/share/test");
headers.put("Cookie", "session=abc123");
otherParam.put("downloadHeaders", headers);
shareLinkInfo.setOtherParam(otherParam);
// 创建测试用的 DownloadLinkMeta
meta = new DownloadLinkMeta("https://example.com/file.zip");
meta.setFileName("test-file.zip");
meta.setHeaders(headers);
}
@Test
public void testCurlLinkGenerator() {
CurlLinkGenerator generator = new CurlLinkGenerator();
String result = generator.generate(meta);
assertNotNull("cURL命令不应为空", result);
assertTrue("应包含curl命令", result.contains("curl"));
assertTrue("应包含下载URL", result.contains("https://example.com/file.zip"));
assertTrue("应包含User-Agent头", result.contains("\"User-Agent: Mozilla/5.0 (Test Browser)\""));
assertTrue("应包含Referer头", result.contains("\"Referer: https://example.com/share/test\""));
assertTrue("应包含Cookie头", result.contains("\"Cookie: session=abc123\""));
assertTrue("应包含输出文件名", result.contains("\"test-file.zip\""));
assertTrue("应包含跟随重定向", result.contains("-L"));
assertEquals("类型应为CURL", ClientLinkType.CURL, generator.getType());
}
@Test
public void testThunderLinkGenerator() {
ThunderLinkGenerator generator = new ThunderLinkGenerator();
String result = generator.generate(meta);
assertNotNull("迅雷链接不应为空", result);
assertTrue("应以thunder://开头", result.startsWith("thunder://"));
// 验证Base64编码格式
String encodedPart = result.substring("thunder://".length());
assertNotNull("编码部分不应为空", encodedPart);
assertFalse("编码部分不应为空字符串", encodedPart.isEmpty());
assertEquals("类型应为THUNDER", ClientLinkType.THUNDER, generator.getType());
}
@Test
public void testAria2LinkGenerator() {
Aria2LinkGenerator generator = new Aria2LinkGenerator();
String result = generator.generate(meta);
assertNotNull("Aria2命令不应为空", result);
assertTrue("应包含aria2c命令", result.contains("aria2c"));
assertTrue("应包含下载URL", result.contains("https://example.com/file.zip"));
assertTrue("应包含User-Agent头", result.contains("--header=\"User-Agent: Mozilla/5.0 (Test Browser)\""));
assertTrue("应包含Referer头", result.contains("--header=\"Referer: https://example.com/share/test\""));
assertTrue("应包含输出文件名", result.contains("--out=\"test-file.zip\""));
assertTrue("应包含断点续传", result.contains("--continue"));
assertEquals("类型应为ARIA2", ClientLinkType.ARIA2, generator.getType());
}
@Test
public void testPowerShellLinkGenerator() {
PowerShellLinkGenerator generator = new PowerShellLinkGenerator();
String result = generator.generate(meta);
assertNotNull("PowerShell命令不应为空", result);
assertTrue("应包含WebRequestSession", result.contains("$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession"));
assertTrue("应包含Invoke-WebRequest", result.contains("Invoke-WebRequest"));
assertTrue("应包含-UseBasicParsing", result.contains("-UseBasicParsing"));
assertTrue("应包含下载URL", result.contains("https://example.com/file.zip"));
assertTrue("应包含User-Agent", result.contains("User-Agent"));
assertTrue("应包含Referer", result.contains("Referer"));
assertTrue("应包含Cookie", result.contains("Cookie"));
assertTrue("应包含输出文件", result.contains("test-file.zip"));
assertEquals("类型应为POWERSHELL", ClientLinkType.POWERSHELL, generator.getType());
}
@Test
public void testPowerShellLinkGeneratorWithoutHeaders() {
PowerShellLinkGenerator generator = new PowerShellLinkGenerator();
meta.setHeaders(new HashMap<>());
String result = generator.generate(meta);
assertNotNull("PowerShell命令不应为空", result);
assertTrue("应包含WebRequestSession", result.contains("$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession"));
assertTrue("应包含Invoke-WebRequest", result.contains("Invoke-WebRequest"));
assertTrue("应包含下载URL", result.contains("https://example.com/file.zip"));
assertFalse("不应包含Headers", result.contains("-Headers @{"));
}
@Test
public void testPowerShellLinkGeneratorWithoutFileName() {
PowerShellLinkGenerator generator = new PowerShellLinkGenerator();
meta.setFileName(null);
String result = generator.generate(meta);
assertNotNull("PowerShell命令不应为空", result);
assertTrue("应包含WebRequestSession", result.contains("$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession"));
assertTrue("应包含Invoke-WebRequest", result.contains("Invoke-WebRequest"));
assertTrue("应包含下载URL", result.contains("https://example.com/file.zip"));
assertFalse("不应包含OutFile", result.contains("-OutFile"));
}
@Test
public void testPowerShellLinkGeneratorWithSpecialCharacters() {
PowerShellLinkGenerator generator = new PowerShellLinkGenerator();
// 测试包含特殊字符的URL和请求头
meta.setUrl("https://example.com/file with spaces.zip");
Map<String, String> specialHeaders = new HashMap<>();
specialHeaders.put("Custom-Header", "Value with \"quotes\" and $variables");
meta.setHeaders(specialHeaders);
String result = generator.generate(meta);
assertNotNull("PowerShell命令不应为空", result);
assertTrue("应包含转义的URL", result.contains("https://example.com/file with spaces.zip"));
assertTrue("应包含转义的请求头", result.contains("Custom-Header"));
assertTrue("应包含转义的引号", result.contains("`\""));
}
@Test
public void testDownloadLinkMetaFromShareLinkInfo() {
DownloadLinkMeta metaFromInfo = DownloadLinkMeta.fromShareLinkInfo(shareLinkInfo);
assertNotNull("从ShareLinkInfo创建的DownloadLinkMeta不应为空", metaFromInfo);
assertEquals("URL应匹配", "https://example.com/file.zip", metaFromInfo.getUrl());
assertEquals("Referer应匹配", "https://example.com/share/test", metaFromInfo.getReferer());
assertEquals("User-Agent应匹配", "Mozilla/5.0 (Test Browser)", metaFromInfo.getUserAgent());
Map<String, String> headers = metaFromInfo.getHeaders();
assertNotNull("请求头不应为空", headers);
assertEquals("请求头数量应匹配", 3, headers.size());
assertEquals("User-Agent应匹配", "Mozilla/5.0 (Test Browser)", headers.get("User-Agent"));
assertEquals("Referer应匹配", "https://example.com/share/test", headers.get("Referer"));
assertEquals("Cookie应匹配", "session=abc123", headers.get("Cookie"));
}
@Test
public void testClientLinkGeneratorFactory() {
Map<ClientLinkType, String> allLinks = ClientLinkGeneratorFactory.generateAll(shareLinkInfo);
assertNotNull("生成的链接集合不应为空", allLinks);
assertFalse("生成的链接集合不应为空", allLinks.isEmpty());
// 检查是否生成了主要类型的链接
assertTrue("应生成cURL链接", allLinks.containsKey(ClientLinkType.CURL));
assertTrue("应生成迅雷链接", allLinks.containsKey(ClientLinkType.THUNDER));
assertTrue("应生成Aria2链接", allLinks.containsKey(ClientLinkType.ARIA2));
assertTrue("应生成wget链接", allLinks.containsKey(ClientLinkType.WGET));
assertTrue("应生成PowerShell链接", allLinks.containsKey(ClientLinkType.POWERSHELL));
// 验证生成的链接不为空
assertNotNull("cURL链接不应为空", allLinks.get(ClientLinkType.CURL));
assertNotNull("迅雷链接不应为空", allLinks.get(ClientLinkType.THUNDER));
assertNotNull("Aria2链接不应为空", allLinks.get(ClientLinkType.ARIA2));
assertNotNull("wget链接不应为空", allLinks.get(ClientLinkType.WGET));
assertNotNull("PowerShell链接不应为空", allLinks.get(ClientLinkType.POWERSHELL));
assertFalse("cURL链接不应为空字符串", allLinks.get(ClientLinkType.CURL).trim().isEmpty());
assertFalse("迅雷链接不应为空字符串", allLinks.get(ClientLinkType.THUNDER).trim().isEmpty());
assertFalse("Aria2链接不应为空字符串", allLinks.get(ClientLinkType.ARIA2).trim().isEmpty());
assertFalse("wget链接不应为空字符串", allLinks.get(ClientLinkType.WGET).trim().isEmpty());
assertFalse("PowerShell链接不应为空字符串", allLinks.get(ClientLinkType.POWERSHELL).trim().isEmpty());
}
@Test
public void testClientLinkUtils() {
String curlCommand = ClientLinkUtils.generateCurlCommand(shareLinkInfo);
String thunderLink = ClientLinkUtils.generateThunderLink(shareLinkInfo);
String aria2Command = ClientLinkUtils.generateAria2Command(shareLinkInfo);
String powershellCommand = ClientLinkUtils.generatePowerShellCommand(shareLinkInfo);
assertNotNull("cURL命令不应为空", curlCommand);
assertNotNull("迅雷链接不应为空", thunderLink);
assertNotNull("Aria2命令不应为空", aria2Command);
assertNotNull("PowerShell命令不应为空", powershellCommand);
assertTrue("cURL命令应包含curl", curlCommand.contains("curl"));
assertTrue("迅雷链接应以thunder://开头", thunderLink.startsWith("thunder://"));
assertTrue("Aria2命令应包含aria2c", aria2Command.contains("aria2c"));
assertTrue("PowerShell命令应包含Invoke-WebRequest", powershellCommand.contains("Invoke-WebRequest"));
// 测试元数据有效性检查
assertTrue("应检测到有效的下载元数据", ClientLinkUtils.hasValidDownloadMeta(shareLinkInfo));
// 测试无效元数据
ShareLinkInfo emptyInfo = ShareLinkInfo.newBuilder().build();
assertFalse("应检测到无效的下载元数据", ClientLinkUtils.hasValidDownloadMeta(emptyInfo));
}
@Test
public void testNullAndEmptyHandling() {
// 测试空URL
DownloadLinkMeta emptyMeta = new DownloadLinkMeta("");
CurlLinkGenerator generator = new CurlLinkGenerator();
String result = generator.generate(emptyMeta);
assertNull("空URL应返回null", result);
// 测试null元数据
result = generator.generate(null);
assertNull("null元数据应返回null", result);
// 测试null ShareLinkInfo
String curlResult = ClientLinkUtils.generateCurlCommand(null);
assertNull("null ShareLinkInfo应返回null", curlResult);
Map<ClientLinkType, String> allResult = ClientLinkUtils.generateAllClientLinks(null);
assertTrue("null ShareLinkInfo应返回空集合", allResult.isEmpty());
}
}
@@ -0,0 +1,68 @@
package cn.qaiu.parser.clientlink;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.clientlink.ClientLinkType;
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
import cn.qaiu.parser.clientlink.impl.PowerShellLinkGenerator;
import java.util.HashMap;
import java.util.Map;
/**
* PowerShell 生成器示例
*
* @author <a href="https://qaiu.top">QAIU</a>
* Create at 2025/01/21
*/
public class PowerShellExample {
public static void main(String[] args) {
// 创建测试数据
DownloadLinkMeta meta = new DownloadLinkMeta("https://example.com/file.zip");
meta.setFileName("test-file.zip");
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
headers.put("Referer", "https://example.com/share/test");
headers.put("Cookie", "session=abc123");
headers.put("Accept", "text/html,application/xhtml+xml");
meta.setHeaders(headers);
// 生成 PowerShell 命令
PowerShellLinkGenerator generator = new PowerShellLinkGenerator();
String powershellCommand = generator.generate(meta);
System.out.println("=== 生成的 PowerShell 命令 ===");
System.out.println(powershellCommand);
System.out.println();
// 测试特殊字符转义
meta.setUrl("https://example.com/file with spaces.zip");
Map<String, String> specialHeaders = new HashMap<>();
specialHeaders.put("Custom-Header", "Value with \"quotes\" and $variables");
meta.setHeaders(specialHeaders);
String escapedCommand = generator.generate(meta);
System.out.println("=== 包含特殊字符的 PowerShell 命令 ===");
System.out.println(escapedCommand);
System.out.println();
// 使用 ClientLinkUtils
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
.type("test")
.panName("测试网盘")
.shareUrl("https://example.com/share/test")
.build();
Map<String, Object> otherParam = new HashMap<>();
otherParam.put("downloadUrl", "https://example.com/file.zip");
otherParam.put("downloadHeaders", headers);
shareLinkInfo.setOtherParam(otherParam);
String utilsCommand = ClientLinkUtils.generatePowerShellCommand(shareLinkInfo);
System.out.println("=== 使用 ClientLinkUtils 生成的 PowerShell 命令 ===");
System.out.println(utilsCommand);
}
}
@@ -1,180 +0,0 @@
package cn.qaiu.parser.clientlink;
import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanDomainTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* UC和夸克网盘客户端链接生成测试
* 测试在有下载链接和请求头的情况下,是否能正确生成下载命令
*/
public class UcQkClientLinkTest {
public static void main(String[] args) {
System.out.println("========================================");
System.out.println(" UC/夸克网盘客户端链接生成测试");
System.out.println("========================================\n");
// 测试 UC 网盘
testUcClientLinks();
// 测试夸克网盘
testQkClientLinks();
System.out.println("\n========================================");
System.out.println(" 测试完成");
System.out.println("========================================");
}
private static void testUcClientLinks() {
System.out.println("=== 测试 UC 网盘客户端链接生成 ===\n");
// 创建 ShareLinkInfo (使用 Builder)
ShareLinkInfo info = ShareLinkInfo.newBuilder()
.type("uc")
.panName(PanDomainTemplate.UC.getDisplayName())
.shareKey("test123")
.build();
// 模拟下载链接(UC网盘的真实下载链接格式)
String downloadUrl = "https://pc-api.uc.cn/1/clouddrive/file/download?xxx";
info.getOtherParam().put("downloadUrl", downloadUrl);
// 模拟下载请求头(包含Cookie)
Map<String, String> headers = new HashMap<>();
headers.put("Cookie", "__pus=5e2bfe93fc55175482cd81dbafb41586AARGIGToqJ7RFMUETPbInASaHMcrrwTch6A6cjwBQQF0gKWZZxV20iixkInaK3AQrW+zsggDwifeq2BZ6fOBsj1N; __kp=72747319-24ad-44da-85a9-133fedd72818; __kps=AASxYmDMULu4nzmEK/wFzK3I; __ktd=dvy3qySVr8aXEqUuxMJydA==; __uid=AASxYmDMULu4nzmEK/wFzK3I; __puus=bdb2e15d24f1a15fe2b5e108b44f0805AAR498zI4bjrVRD3mNor9LX8YbixADr2C4YebqDb1fvtySVLiF3VgyASPRi/VSfMikDVd3yHUtbqP3ZwAteImXbevPo84hloWgCG0qCouDie3PKBIXq4+UxiXay2GHtst71wVq7ODiWV3OzzazpYgtGqTjep8F4BWtwdwtCjQz6l6OHVYy/LkTe3/6eeAreiRNU=");
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
headers.put("Referer", "https://drive.uc.cn/");
info.getOtherParam().put("downloadHeaders", headers);
// 设置文件信息(通过otherParam)
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName("测试文件.zip");
info.getOtherParam().put("fileInfo", fileInfo);
// 生成客户端链接
Map<ClientLinkType, String> clientLinks = ClientLinkGeneratorFactory.generateAll(info);
if (clientLinks.isEmpty()) {
System.out.println("❌ 未能生成任何客户端链接\n");
return;
}
System.out.println("✅ 成功生成 " + clientLinks.size() + " 个客户端链接:\n");
for (Map.Entry<ClientLinkType, String> entry : clientLinks.entrySet()) {
ClientLinkType type = entry.getKey();
String link = entry.getValue();
System.out.println("【" + type.getDisplayName() + "】");
System.out.println(link);
System.out.println();
// 验证链接格式
validateLink(type, link, "UC");
}
}
private static void testQkClientLinks() {
System.out.println("=== 测试夸克网盘客户端链接生成 ===\n");
// 创建 ShareLinkInfo (使用 Builder)
ShareLinkInfo info = ShareLinkInfo.newBuilder()
.type("qk")
.panName(PanDomainTemplate.QK.getDisplayName())
.shareKey("test456")
.build();
// 模拟下载链接(夸克网盘的真实下载链接格式)
String downloadUrl = "https://drive-pc.quark.cn/1/clouddrive/file/download?xxx";
info.getOtherParam().put("downloadUrl", downloadUrl);
// 模拟下载请求头(包含Cookie)
Map<String, String> headers = new HashMap<>();
headers.put("Cookie", "__pus=abc123def456; __kp=ghi789jkl012; __kps=mno345pqr678; __ktd=stu901vwx234; __uid=yza567bcd890; __puus=efg123hij456");
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
headers.put("Referer", "https://pan.quark.cn/");
info.getOtherParam().put("downloadHeaders", headers);
// 设置文件信息(通过otherParam)
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName("测试文件.mp4");
info.getOtherParam().put("fileInfo", fileInfo);
// 生成客户端链接
Map<ClientLinkType, String> clientLinks = ClientLinkGeneratorFactory.generateAll(info);
if (clientLinks.isEmpty()) {
System.out.println("❌ 未能生成任何客户端链接\n");
return;
}
System.out.println("✅ 成功生成 " + clientLinks.size() + " 个客户端链接:\n");
for (Map.Entry<ClientLinkType, String> entry : clientLinks.entrySet()) {
ClientLinkType type = entry.getKey();
String link = entry.getValue();
System.out.println("【" + type.getDisplayName() + "】");
System.out.println(link);
System.out.println();
// 验证链接格式
validateLink(type, link, "夸克");
}
}
private static void validateLink(ClientLinkType type, String link, String panName) {
boolean valid = true;
StringBuilder issues = new StringBuilder();
switch (type) {
case CURL:
if (!link.startsWith("curl ")) {
valid = false;
issues.append("不是以 'curl ' 开头; ");
}
if (!link.contains("--header \"Cookie:")) {
valid = false;
issues.append("缺少 Cookie 请求头; ");
}
if (!link.contains("--output")) {
valid = false;
issues.append("缺少输出文件名; ");
}
break;
case ARIA2:
if (!link.contains("aria2c")) {
valid = false;
issues.append("不包含 'aria2c'; ");
}
if (!link.contains("--header=\"Cookie:")) {
valid = false;
issues.append("缺少 Cookie 请求头; ");
}
if (!link.contains("--out=")) {
valid = false;
issues.append("缺少输出文件名; ");
}
break;
case THUNDER:
if (!link.startsWith("thunder://")) {
valid = false;
issues.append("不是以 'thunder://' 开头; ");
}
// 迅雷不支持 Cookie,所以不检查
break;
}
if (valid) {
System.out.println(" ✓ " + panName + "的" + type.getDisplayName() + "格式验证通过");
} else {
System.out.println(" ⚠️ " + panName + "的" + type.getDisplayName() + "格式异常: " + issues);
}
}
}
@@ -1,139 +0,0 @@
package cn.qaiu.parser.impl;
import cn.qaiu.entity.ShareLinkInfo;
import io.vertx.core.MultiMap;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import java.util.HashMap;
import java.util.Map;
/**
* UC 和夸克网盘工具类验证测试
*/
public class UcQkToolValidationTest {
public static void main(String[] args) {
System.out.println("========================================");
System.out.println(" UC/夸克网盘工具类验证测试");
System.out.println("========================================\n");
testQkToolWithAuth();
testUcToolWithAuth();
testQkToolWithoutAuth();
testUcToolWithoutAuth();
System.out.println("\n========================================");
System.out.println(" 所有验证通过! ✓");
System.out.println("========================================");
}
private static void testQkToolWithAuth() {
System.out.println("=== 测试夸克网盘工具类(带认证)===");
try {
// 创建认证配置
MultiMap auths = new HeadersMultiMap();
auths.set("cookie", "__pus=test_token; __kp=key123; __kps=secret; __puus=signature");
Map<String, Object> otherParam = new HashMap<>();
otherParam.put("auths", auths);
// 创建分享链接信息
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
.type("QK")
.panName("夸克网盘")
.shareKey("test_key")
.shareUrl("https://pan.quark.cn/s/test123")
.build();
shareLinkInfo.setOtherParam(otherParam);
// 创建工具类实例
QkTool qkTool = new QkTool(shareLinkInfo);
System.out.println("✓ 夸克网盘工具类实例创建成功");
System.out.println(" - 已配置认证信息");
System.out.println(" - Cookie 已过滤和应用\n");
} catch (Exception e) {
System.err.println("✗ 夸克网盘工具类测试失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void testUcToolWithAuth() {
System.out.println("=== 测试 UC 网盘工具类(带认证)===");
try {
// 创建认证配置
MultiMap auths = new HeadersMultiMap();
auths.set("cookie", "__pus=uc_token; __kp=uc_key; __uid=user001; __puus=uc_sig");
Map<String, Object> otherParam = new HashMap<>();
otherParam.put("auths", auths);
// 创建分享链接信息
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
.type("UC")
.panName("UC网盘")
.shareKey("uc_key_123")
.shareUrl("https://fast.uc.cn/s/abc123")
.build();
shareLinkInfo.setOtherParam(otherParam);
// 创建工具类实例
UcTool ucTool = new UcTool(shareLinkInfo);
System.out.println("✓ UC 网盘工具类实例创建成功");
System.out.println(" - 已配置认证信息");
System.out.println(" - Cookie 已过滤和应用\n");
} catch (Exception e) {
System.err.println("✗ UC 网盘工具类测试失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void testQkToolWithoutAuth() {
System.out.println("=== 测试夸克网盘工具类(无认证)===");
try {
// 创建分享链接信息(无认证)
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
.type("QK")
.panName("夸克网盘")
.shareKey("test_key_no_auth")
.shareUrl("https://pan.quark.cn/s/test456")
.build();
// 创建工具类实例
QkTool qkTool = new QkTool(shareLinkInfo);
System.out.println("✓ 夸克网盘工具类实例创建成功(无认证)");
System.out.println(" - 应该使用默认请求头\n");
} catch (Exception e) {
System.err.println("✗ 夸克网盘工具类(无认证)测试失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void testUcToolWithoutAuth() {
System.out.println("=== 测试 UC 网盘工具类(无认证)===");
try {
// 创建分享链接信息(无认证)
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
.type("UC")
.panName("UC网盘")
.shareKey("uc_no_auth")
.shareUrl("https://fast.uc.cn/s/def456")
.build();
// 创建工具类实例
UcTool ucTool = new UcTool(shareLinkInfo);
System.out.println("✓ UC 网盘工具类实例创建成功(无认证)");
System.out.println(" - 应该使用默认请求头\n");
} catch (Exception e) {
System.err.println("✗ UC 网盘工具类(无认证)测试失败: " + e.getMessage());
e.printStackTrace();
}
}
}
@@ -1,367 +0,0 @@
package cn.qaiu.parser.integration;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.IPanTool;
import cn.qaiu.parser.ParserCreate;
import io.vertx.core.MultiMap;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* 带认证的解析集成测试
*
* 使用方式:
* 1. 在 src/test/resources/auth-test.properties 中配置认证信息
* 2. 运行测试
*
* 配置文件格式:
* qk.cookie=__pus=xxx; __kp=xxx; ...
* qk.url=https://pan.quark.cn/s/xxx
* uc.cookie=__pus=xxx; __kp=xxx; ...
* uc.url=https://fast.uc.cn/s/xxx
* fj.cookie=your_cookie_here
* fj.url=https://share.feijipan.com/s/xxx
* fj.pwd=1234
*/
public class AuthParseIntegrationTest {
private static final String CONFIG_FILE = "src/test/resources/auth-test.properties";
private static Properties config;
public static void main(String[] args) {
System.out.println("========================================");
System.out.println(" 带认证的解析集成测试");
System.out.println("========================================\n");
// 加载配置
if (!loadConfig()) {
System.err.println("❌ 无法加载配置文件: " + CONFIG_FILE);
System.out.println("\n请创建配置文件并添加认证信息:");
printConfigExample();
return;
}
System.out.println("✓ 配置文件加载成功\n");
// 测试夸克网盘
if (hasConfig("qk")) {
testQuark();
} else {
System.out.println("⏭ 跳过夸克网盘测试(未配置)\n");
}
// 测试 UC 网盘
if (hasConfig("uc")) {
testUc();
} else {
System.out.println("⏭ 跳过 UC 网盘测试(未配置)\n");
}
// 测试小飞机网盘
if (hasConfig("fj")) {
testFeiji();
} else {
System.out.println("⏭ 跳过小飞机网盘测试(未配置)\n");
}
System.out.println("========================================");
System.out.println(" 集成测试完成");
System.out.println("========================================");
// 给异步操作一些时间完成
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.exit(0);
}
private static boolean loadConfig() {
config = new Properties();
try (FileReader reader = new FileReader(CONFIG_FILE)) {
config.load(reader);
return true;
} catch (IOException e) {
return false;
}
}
private static boolean hasConfig(String prefix) {
return config.containsKey(prefix + ".url");
}
private static String getConfig(String key) {
return config.getProperty(key, "");
}
private static void testQuark() {
System.out.println("=== 测试夸克网盘解析(带认证)===");
String url = getConfig("qk.url");
String cookie = getConfig("qk.cookie");
String pwd = getConfig("qk.pwd");
System.out.println("分享链接: " + url);
System.out.println("Cookie: " + maskCookie(cookie));
if (!pwd.isEmpty()) {
System.out.println("密码: " + pwd);
}
try {
// 创建认证配置
MultiMap auths = new HeadersMultiMap();
auths.set("cookie", cookie);
Map<String, Object> otherParam = new HashMap<>();
otherParam.put("auths", auths);
// 创建解析器
ParserCreate parserCreate = ParserCreate.fromShareUrl(url);
ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo();
if (!pwd.isEmpty()) {
shareLinkInfo.setSharePassword(pwd);
}
shareLinkInfo.setOtherParam(otherParam);
IPanTool tool = parserCreate.createTool();
System.out.println("\n开始解析...");
// 异步解析
CountDownLatch latch = new CountDownLatch(1);
final long startTime = System.currentTimeMillis();
tool.parse().onSuccess(result -> {
long duration = System.currentTimeMillis() - startTime;
System.out.println("\n✅ 夸克网盘解析成功!");
System.out.println("耗时: " + duration + "ms");
System.out.println("直链: " + result);
// 验证直链格式
if (result != null && result.startsWith("http")) {
System.out.println("✓ 直链格式正确");
} else {
System.out.println("⚠️ 直链格式异常");
}
latch.countDown();
}).onFailure(error -> {
long duration = System.currentTimeMillis() - startTime;
System.out.println("\n❌ 夸克网盘解析失败!");
System.out.println("耗时: " + duration + "ms");
System.out.println("错误: " + error.getMessage());
if (error.getCause() != null) {
System.out.println("原因: " + error.getCause().getMessage());
}
latch.countDown();
});
// 等待结果(最多30秒)
if (!latch.await(30, TimeUnit.SECONDS)) {
System.out.println("\n⏱️ 解析超时(30秒)");
}
} catch (Exception e) {
System.out.println("\n❌ 测试异常: " + e.getMessage());
e.printStackTrace();
}
System.out.println();
}
private static void testUc() {
System.out.println("=== 测试 UC 网盘解析(带认证)===");
String url = getConfig("uc.url");
String cookie = getConfig("uc.cookie");
String pwd = getConfig("uc.pwd");
System.out.println("分享链接: " + url);
System.out.println("Cookie: " + maskCookie(cookie));
if (!pwd.isEmpty()) {
System.out.println("密码: " + pwd);
}
try {
// 创建认证配置
MultiMap auths = new HeadersMultiMap();
auths.set("cookie", cookie);
Map<String, Object> otherParam = new HashMap<>();
otherParam.put("auths", auths);
// 创建解析器
ParserCreate parserCreate = ParserCreate.fromShareUrl(url);
ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo();
if (!pwd.isEmpty()) {
shareLinkInfo.setSharePassword(pwd);
}
shareLinkInfo.setOtherParam(otherParam);
IPanTool tool = parserCreate.createTool();
System.out.println("\n开始解析...");
// 异步解析
CountDownLatch latch = new CountDownLatch(1);
final long startTime = System.currentTimeMillis();
tool.parse().onSuccess(result -> {
long duration = System.currentTimeMillis() - startTime;
System.out.println("\n✅ UC 网盘解析成功!");
System.out.println("耗时: " + duration + "ms");
System.out.println("直链: " + result);
// 验证直链格式
if (result != null && result.startsWith("http")) {
System.out.println("✓ 直链格式正确");
} else {
System.out.println("⚠️ 直链格式异常");
}
latch.countDown();
}).onFailure(error -> {
long duration = System.currentTimeMillis() - startTime;
System.out.println("\n❌ UC 网盘解析失败!");
System.out.println("耗时: " + duration + "ms");
System.out.println("错误: " + error.getMessage());
if (error.getCause() != null) {
System.out.println("原因: " + error.getCause().getMessage());
}
latch.countDown();
});
// 等待结果(最多30秒)
if (!latch.await(30, TimeUnit.SECONDS)) {
System.out.println("\n⏱️ 解析超时(30秒)");
}
} catch (Exception e) {
System.out.println("\n❌ 测试异常: " + e.getMessage());
e.printStackTrace();
}
System.out.println();
}
private static void testFeiji() {
System.out.println("=== 测试小飞机网盘解析(带认证)===");
String url = getConfig("fj.url");
String username = getConfig("fj.username");
String password = getConfig("fj.password");
String pwd = getConfig("fj.pwd");
System.out.println("分享链接: " + url);
System.out.println("用户名: " + (username.isEmpty() ? "无" : username));
System.out.println("密码: " + (password.isEmpty() ? "无" : "******"));
if (!pwd.isEmpty()) {
System.out.println("提取码: " + pwd);
}
try {
// 创建认证配置
MultiMap auths = new HeadersMultiMap();
if (!username.isEmpty() && !password.isEmpty()) {
auths.set("username", username);
auths.set("password", password);
}
Map<String, Object> otherParam = new HashMap<>();
if (!username.isEmpty()) {
otherParam.put("auths", auths);
}
// 创建解析器
ParserCreate parserCreate = ParserCreate.fromShareUrl(url);
ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo();
if (!pwd.isEmpty()) {
shareLinkInfo.setSharePassword(pwd);
}
// 设置认证参数
if (!username.isEmpty()) {
shareLinkInfo.setOtherParam(otherParam);
}
IPanTool tool = parserCreate.createTool();
System.out.println("\n开始解析...");
// 异步解析
CountDownLatch latch = new CountDownLatch(1);
final long startTime = System.currentTimeMillis();
tool.parse().onSuccess(result -> {
long duration = System.currentTimeMillis() - startTime;
System.out.println("\n✅ 小飞机网盘解析成功!");
System.out.println("耗时: " + duration + "ms");
System.out.println("直链: " + result);
// 验证直链格式
if (result != null && result.startsWith("http")) {
System.out.println("✓ 直链格式正确");
} else {
System.out.println("⚠️ 直链格式异常");
}
latch.countDown();
}).onFailure(error -> {
long duration = System.currentTimeMillis() - startTime;
System.out.println("\n❌ 小飞机网盘解析失败!");
System.out.println("耗时: " + duration + "ms");
System.out.println("错误: " + error.getMessage());
if (error.getCause() != null) {
System.out.println("原因: " + error.getCause().getMessage());
}
latch.countDown();
});
// 等待结果(最多30秒)
if (!latch.await(30, TimeUnit.SECONDS)) {
System.out.println("\n⏱️ 解析超时(30秒)");
}
} catch (Exception e) {
System.out.println("\n❌ 测试异常: " + e.getMessage());
e.printStackTrace();
}
System.out.println();
}
private static String maskCookie(String cookie) {
if (cookie == null || cookie.isEmpty()) {
return "(未配置)";
}
if (cookie.length() <= 20) {
return cookie.substring(0, Math.min(10, cookie.length())) + "...";
}
return cookie.substring(0, 10) + "..." + cookie.substring(cookie.length() - 10);
}
private static void printConfigExample() {
System.out.println("\n配置文件示例 (" + CONFIG_FILE + "):");
System.out.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.out.println("# 夸克网盘配置");
System.out.println("qk.cookie=__pus=xxx; __kp=xxx; __kps=xxx; __ktd=xxx; __uid=xxx; __puus=xxx");
System.out.println("qk.url=https://pan.quark.cn/s/xxxxxxxxxx");
System.out.println("qk.pwd=");
System.out.println();
System.out.println("# UC 网盘配置");
System.out.println("uc.cookie=__pus=xxx; __kp=xxx; __kps=xxx; __ktd=xxx; __uid=xxx; __puus=xxx");
System.out.println("uc.url=https://fast.uc.cn/s/xxxxxxxxxx");
System.out.println("uc.pwd=");
System.out.println();
System.out.println("# 小飞机网盘配置(大文件需要认证)");
System.out.println("fj.cookie=your_session_cookie_here");
System.out.println("fj.url=https://share.feijipan.com/s/xxxxxxxxxx");
System.out.println("fj.pwd=1234");
System.out.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
}
}
@@ -1,86 +0,0 @@
package cn.qaiu.parser.integration;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.ParserCreate;
import cn.qaiu.parser.IPanTool;
/**
* 测试链接识别问题
* 验证 https://pan.quark.cn/s/30e3c602ac09 是否被正确识别为夸克网盘
*/
public class LinkIdentifyTest {
public static void main(String[] args) {
System.out.println("========================================");
System.out.println(" 链接识别测试");
System.out.println("========================================\n");
// 测试夸克链接
testQkLink();
// 测试UC链接
testUcLink();
System.out.println("\n========================================");
System.out.println(" 测试完成");
System.out.println("========================================");
}
private static void testQkLink() {
System.out.println("=== 测试夸克网盘链接识别 ===\n");
String url = "https://pan.quark.cn/s/30e3c602ac09";
System.out.println("测试URL: " + url);
try {
ParserCreate parserCreate = ParserCreate.fromShareUrl(url);
ShareLinkInfo info = parserCreate.getShareLinkInfo();
System.out.println("识别结果:");
System.out.println(" 网盘名称: " + info.getPanName());
System.out.println(" 网盘类型: " + info.getType());
System.out.println(" 分享KEY: " + info.getShareKey());
System.out.println(" 标准URL: " + info.getStandardUrl());
if ("qk".equalsIgnoreCase(info.getType())) {
System.out.println("\n✅ 链接正确识别为夸克网盘");
} else {
System.out.println("\n❌ 链接识别错误! 期望: qk, 实际: " + info.getType());
}
} catch (Exception e) {
System.out.println("\n❌ 识别失败: " + e.getMessage());
e.printStackTrace();
}
System.out.println();
}
private static void testUcLink() {
System.out.println("=== 测试UC网盘链接识别 ===\n");
String url = "https://drive.uc.cn/s/e623b6da278e4";
System.out.println("测试URL: " + url);
try {
ParserCreate parserCreate = ParserCreate.fromShareUrl(url);
ShareLinkInfo info = parserCreate.getShareLinkInfo();
System.out.println("识别结果:");
System.out.println(" 网盘名称: " + info.getPanName());
System.out.println(" 网盘类型: " + info.getType());
System.out.println(" 分享KEY: " + info.getShareKey());
System.out.println(" 标准URL: " + info.getStandardUrl());
if ("uc".equalsIgnoreCase(info.getType())) {
System.out.println("\n✅ 链接正确识别为UC网盘");
} else {
System.out.println("\n❌ 链接识别错误! 期望: uc, 实际: " + info.getType());
}
} catch (Exception e) {
System.out.println("\n❌ 识别失败: " + e.getMessage());
e.printStackTrace();
}
System.out.println();
}
}
@@ -1,194 +0,0 @@
# 带认证的网盘解析集成测试
## 📋 概述
这个测试套件用于验证 UC、夸克和小飞机网盘的完整解析流程,包括认证、Cookie 处理和直链获取。
## 🚀 快速开始
### 1. 准备配置文件
```bash
cd parser/src/test/resources
cp auth-test.properties.template auth-test.properties
```
### 2. 填写认证信息
编辑 `auth-test.properties` 文件,填入真实的 Cookie 和分享链接。
**如何获取 Cookie:**
1. 在浏览器中登录对应网盘(夸克/UC)
2. 打开开发者工具(F12)
3. 切换到 Network 标签
4. 刷新页面
5. 找到任意请求,在请求头中复制完整的 Cookie
**夸克网盘 Cookie 示例:**
```
__pus=abc123; __kp=def456; __kps=ghi789; __ktd=jkl012; __uid=mno345; __puus=pqr678
```
**UC 网盘 Cookie 示例:**
```
__pus=xyz123; __kp=uvw456; __kps=rst789; __ktd=opq012; __uid=lmn345; __puus=ijk678
```
### 3. 运行测试
```bash
cd parser
mvn exec:java -Dexec.mainClass="cn.qaiu.parser.integration.AuthParseIntegrationTest" -Dexec.classpathScope=test -q
```
或者使用编译后运行:
```bash
mvn test-compile
java -cp target/test-classes:target/classes:$(mvn dependency:build-classpath -q -Dmdep.outputFile=/dev/stdout) cn.qaiu.parser.integration.AuthParseIntegrationTest
```
## 📝 配置文件格式
```properties
# 夸克网盘(必须认证)
qk.cookie=__pus=xxx; __kp=xxx; ...
qk.url=https://pan.quark.cn/s/xxxxxxxxxx
qk.pwd=
# UC 网盘(必须认证)
uc.cookie=__pus=xxx; __kp=xxx; ...
uc.url=https://fast.uc.cn/s/xxxxxxxxxx
uc.pwd=
# 小飞机网盘(大文件需认证)
fj.cookie=session_id=xxx
fj.url=https://share.feijipan.com/s/xxxxxxxxxx
fj.pwd=1234
```
## 🧪 测试内容
### 1. 夸克网盘测试
- ✅ Cookie 过滤和应用
- ✅ __puus 自动刷新机制
- ✅ 解析带认证的分享链接
- ✅ 获取直链
- ✅ 验证直链格式
### 2. UC 网盘测试
- ✅ Cookie 过滤和应用
- ✅ __puus 自动刷新机制
- ✅ 解析带认证的分享链接
- ✅ 获取直链
- ✅ 验证直链格式
### 3. 小飞机网盘测试
- ✅ 可选认证配置
- ✅ 解析带密码的分享链接
- ✅ 大文件认证处理
- ✅ 获取直链
- ✅ 验证直链格式
## 📊 测试输出示例
```
========================================
带认证的解析集成测试
========================================
✓ 配置文件加载成功
=== 测试夸克网盘解析(带认证)===
分享链接: https://pan.quark.cn/s/abc123def
Cookie: __pus=abc1...xyz789
开始解析...
✅ 夸克网盘解析成功!
耗时: 1234ms
直链: https://download.quark.cn/file/xxx
✓ 直链格式正确
=== 测试 UC 网盘解析(带认证)===
分享链接: https://fast.uc.cn/s/def456ghi
Cookie: __pus=def4...uvw012
开始解析...
✅ UC 网盘解析成功!
耗时: 2345ms
直链: https://download.uc.cn/file/xxx
✓ 直链格式正确
========================================
集成测试完成
========================================
```
## ⚠️ 注意事项
1. **Cookie 安全性**
- 不要将包含真实 Cookie 的配置文件提交到版本控制
- `auth-test.properties` 已在 `.gitignore` 中
- Cookie 包含敏感信息,请妥善保管
2. **Cookie 有效期**
- Cookie 通常有效期为 1-7 天
- 过期后需要重新获取
- 如果解析失败,首先检查 Cookie 是否过期
3. **网盘限制**
- 夸克和 UC 网盘**必须**提供 Cookie 才能解析
- 小飞机网盘仅大文件(>100MB)需要 Cookie
- 部分分享链接可能有下载次数限制
4. **测试环境**
- 需要网络连接
- 建议使用真实的大文件分享链接测试
- 超时时间设置为 30 秒
## 🔍 故障排查
### 解析失败
1. **检查 Cookie 格式**
- 确保包含所有必需字段:`__pus`, `__kp`, `__kps`, `__ktd`, `__uid`, `__puus`
- 没有多余的空格或换行符
2. **检查分享链接**
- 链接格式正确
- 链接未过期
- 分享密码正确(如果有)
3. **查看详细日志**
- 运行时不加 `-q` 参数查看完整日志
- 检查网络请求和响应
### Cookie 过期
- 重新登录网盘
- 重新获取 Cookie
- 更新配置文件
### 网络超时
- 检查网络连接
- 可能是网盘服务器响应慢
- 可以修改代码中的超时时间(默认30秒)
## 📚 相关文档
- [Cookie 工具类文档](../java/cn/qaiu/util/CookieUtils.java)
- [夸克网盘解析器](../java/cn/qaiu/parser/impl/QkTool.java)
- [UC 网盘解析器](../java/cn/qaiu/parser/impl/UcTool.java)
- [小飞机网盘解析器](../java/cn/qaiu/parser/impl/FjTool.java)
- [认证参数指南](../../doc/auth-param/AUTH_PARAM_GUIDE.md)
## 💡 提示
- 首次运行前确保已执行 `mvn compile` 编译项目
- 如果未配置某个网盘,该网盘的测试会自动跳过
- 测试结果包含解析耗时,可用于性能评估
- Cookie 会自动过滤,只保留必需字段
@@ -1,191 +0,0 @@
# 认证解析集成测试结果
## 测试日期
2026-02-05
## 测试环境
- Java: 17+
- Maven: 3.x
- 系统: macOS
## 测试配置
### 小飞机网盘 ✅
- **用户名**: 15x
- **URL**: https://share.feijipan.com/s/ZWYoZ31c
- **文件**: 资源.rar (1.13 GB)
- **认证方式**: username/password
### UC网盘 ⏸️
- **Cookie**: 已配置(长度 2.5KB)
- **URL**: 未提供
- **状态**: 等待分享链接
### 夸克网盘 ⏸️
- **Cookie**: 未配置
- **URL**: 未提供
- **状态**: 等待认证信息和分享链接
## 测试结果
### ✅ 小飞机网盘 - 成功
```
=== 测试小飞机网盘解析(带认证)===
分享链接: https://share.feijipan.com/s/ZWYoZ31c
用户名: 15x
密码: ******
开始解析...
2026-02-05 17:06:10.188 INFO 登录成功 token: f2d2186d...
2026-02-05 17:06:10.374 INFO 验证成功 userId: 4481273
✅ 小飞机网盘解析成功!
耗时: 1690ms
直链: https://dl-app.feejii.com/storage/files/2025/11/02/0/13000720/176208936345513.gz?t=6984648a&rlimit=20&us=Em7C0Gdaaz&sign=b954cdef169f2d883e1dfe4a6c9762fa&download_name=%E8%B5%84%E6%BA%90.rar&p=4481273-4481273-24620369057
✓ 直链格式正确
```
**验证项**:
- ✅ 用户名密码认证成功
- ✅ 登录和token获取正常
- ✅ 用户ID验证通过
- ✅ 直链生成成功
- ✅ 解析耗时合理(1.69秒)
- ✅ 大文件(1GB+)解析正常
### ⏸️ UC网盘 - 等待测试
**原因**: 缺少分享链接URL
**已准备**:
- ✅ Cookie配置完整(包含所有必需字段)
- ✅ CookieUtils工具已验证(7/7测试通过)
- ✅ UcTool认证逻辑已验证
- ✅ __puus自动刷新机制已实现
**下一步**: 提供UC网盘分享链接后即可测试
### ⏸️ 夸克网盘 - 等待测试
**原因**: 缺少Cookie和分享链接URL
**已准备**:
- ✅ CookieUtils工具已验证(7/7测试通过)
- ✅ QkTool认证逻辑已验证
- ✅ __puus自动刷新机制已实现
**下一步**: 提供夸克网盘Cookie和分享链接后即可测试
## 前端增强 ✅
### 新增功能:智能网盘类型检测和提示
**实现方式**:
1. 解析前调用 `/v2/linkInfo` API 获取网盘类型
2. 根据网盘类型给出相应提示
**提示规则**:
| 网盘类型 | 代码 | 提示内容 | 持续时间 |
|---------|------|---------|---------|
| 夸克网盘 | `qk` | "无法在网页端直接下载,请点击'生成下载命令'按钮,使用命令行工具下载" | 5秒 |
| UC网盘 | `uc` | "无法在网页端直接下载,请点击'生成下载命令'按钮,使用命令行工具下载" | 5秒 |
| 小飞机 | `fj` | "的大文件解析需要配置认证信息,请在'配置认证'中添加" | 4秒 |
| 蓝奏云 | `lz` | "的大文件解析需要配置认证信息,请在'配置认证'中添加" | 4秒 |
| 蓝奏优享 | `iz` | "的大文件解析需要配置认证信息,请在'配置认证'中添加" | 4秒 |
| 联想乐云 | `le` | "的大文件解析需要配置认证信息,请在'配置认证'中添加" | 4秒 |
**修改文件**:
- [Home.vue](../../../../../../../web-front/src/views/Home.vue) - parseFile() 方法
## 工具验证状态
### ✅ CookieUtils - 全部通过
- 测试文件: [CookieUtilsManualTest.java](../utils/CookieUtilsManualTest.java)
- 测试通过: 7/7
- 验证项:
- ✅ Cookie字段过滤
- ✅ getValue提取
- ✅ updateCookie更新
- ✅ containsKey检查
- ✅ 空值处理
- ✅ 复杂场景
- ✅ UC/QK所有必需字段
### ✅ UC/QK Tool - 全部通过
- 测试文件: [UcQkToolValidationTest.java](../impl/UcQkToolValidationTest.java)
- 测试通过: 4/4
- 验证项:
- ✅ QK带认证实例化
- ✅ UC带认证实例化
- ✅ QK无认证实例化
- ✅ UC无认证实例化
## 技术细节
### Cookie字段要求
UC和夸克都需要以下6个Cookie字段:
- `__pus` - 用户会话标识
- `__kp` - 密钥标识
- `__kps` - 密钥会话
- `__ktd` - 密钥令牌数据
- `__uid` - 用户ID
- `__puus` - 持久用户会话(55分钟自动刷新)
### 自动刷新机制
- **刷新间隔**: 55分钟
- **有效期**: 1小时
- **安全边际**: 5分钟
- **实现**: Vertx定时器自动执行
### 认证参数加密
- **算法**: AES/ECB/PKCS5Padding
- **密钥**: "nfd_auth_key2026"
- **编码**: Base64 → URL编码
- **参数名**: `auth`
## 下次测试准备
### UC网盘
需要提供:
- ✅ Cookie(已有)
- ⏸️ 分享链接URL(待提供)
- ⏸️ 提取码(可选)
### 夸克网盘
需要提供:
- ⏸️ Cookie(待提供)
- ⏸️ 分享链接URL(待提供)
- ⏸️ 提取码(可选)
## 运行命令
```bash
# 方法1: 使用便捷脚本
cd parser
bash src/test/java/cn/qaiu/parser/integration/run-test.sh
# 方法2: Maven直接运行
cd parser
mvn exec:java \
-Dexec.mainClass="cn.qaiu.parser.integration.AuthParseIntegrationTest" \
-Dexec.classpathScope=test \
-q
```
## 总结
✅ **已完成**:
1. 小飞机网盘认证解析测试 - 成功
2. CookieUtils工具验证 - 全部通过
3. UC/QK Tool实例化验证 - 全部通过
4. 集成测试框架 - 就绪
5. 前端类型检测和提示 - 已实现
⏸️ **待测试**:
1. UC网盘完整解析流程(等待分享链接)
2. 夸克网盘完整解析流程(等待Cookie和链接)
📋 **建议**:
1. 获取UC网盘的真实分享链接进行测试
2. 获取夸克网盘的Cookie和分享链接进行测试
3. 测试不同文件大小的解析性能
4. 验证前端UI提示是否正确显示
@@ -1,76 +0,0 @@
#!/bin/bash
# 带认证的网盘解析集成测试运行脚本
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
CONFIG_FILE="$SCRIPT_DIR/../resources/auth-test.properties"
TEMPLATE_FILE="$SCRIPT_DIR/../resources/auth-test.properties.template"
echo "========================================="
echo " 网盘解析集成测试运行器"
echo "========================================="
echo
# 检查配置文件
if [ ! -f "$CONFIG_FILE" ]; then
echo "❌ 配置文件不存在: $CONFIG_FILE"
echo
echo "请先创建配置文件:"
echo " cp $TEMPLATE_FILE $CONFIG_FILE"
echo
echo "然后编辑配置文件,填入真实的 Cookie 和分享链接"
exit 1
fi
echo "✓ 找到配置文件: $CONFIG_FILE"
echo
# 检查配置文件是否为空或只有模板
if ! grep -q "qk.url=http" "$CONFIG_FILE" && \
! grep -q "uc.url=http" "$CONFIG_FILE" && \
! grep -q "fj.url=http" "$CONFIG_FILE"; then
echo "⚠️ 配置文件似乎未填写实际数据"
echo
read -p "是否继续?(y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "已取消"
exit 0
fi
fi
# 切换到 parser 目录
cd "$PROJECT_ROOT/parser" || exit 1
echo "开始编译..."
mvn compile -q -DskipTests
if [ $? -ne 0 ]; then
echo "❌ 编译失败"
exit 1
fi
echo "✓ 编译成功"
echo
echo "开始运行测试..."
echo "========================================="
echo
# 运行测试
mvn exec:java \
-Dexec.mainClass="cn.qaiu.parser.integration.AuthParseIntegrationTest" \
-Dexec.classpathScope=test \
-q
TEST_RESULT=$?
echo
echo "========================================="
if [ $TEST_RESULT -eq 0 ]; then
echo "✓ 测试运行完成"
else
echo "❌ 测试运行失败(退出码: $TEST_RESULT)"
fi
echo "========================================="
exit $TEST_RESULT
@@ -1,67 +0,0 @@
# ========================================
# 网盘认证信息配置文件
# ========================================
#
# 使用说明:
# 1. 将此文件重命名为 auth-test.properties
# 2. 填入真实的 Cookie 和分享链接
# 3. 运行测试: mvn exec:java -Dexec.mainClass="cn.qaiu.parser.integration.AuthParseIntegrationTest" -Dexec.classpathScope=test
#
# 如何获取 Cookie:
# 1. 在浏览器中登录对应网盘
# 2. 打开开发者工具(F12)
# 3. 切换到 Network 标签
# 4. 刷新页面
# 5. 找到任意请求,在请求头中复制完整的 Cookie
#
# ========================================
# 夸克网盘配置(必须认证)
# ========================================
# 分享链接示例: https://pan.quark.cn/s/abc123def
qk.url=
# Cookie 必需字段: __pus, __kp, __kps, __ktd, __uid, __puus
# 完整示例: __pus=abc123; __kp=def456; __kps=ghi789; __ktd=jkl012; __uid=mno345; __puus=pqr678
qk.cookie=
# 分享密码(如果有)
qk.pwd=
# ========================================
# UC 网盘配置(必须认证)
# ========================================
# 分享链接示例: https://fast.uc.cn/s/abc123def
uc.url=
# Cookie 必需字段: __pus, __kp, __kps, __ktd, __uid, __puus
# 完整示例: __pus=abc123; __kp=def456; __kps=ghi789; __ktd=jkl012; __uid=mno345; __puus=pqr678
uc.cookie=
# 分享密码(如果有)
uc.pwd=
# ========================================
# 小飞机网盘配置(大文件需认证)
# ========================================
# 分享链接示例: https://share.feijipan.com/s/abc123def
fj.url=
# Cookie(大文件 >100MB 时需要)
# 完整示例: session_id=abc123; auth_token=def456
fj.cookie=
# 分享密码
fj.pwd=
# ========================================
# 注意事项
# ========================================
# 1. Cookie 中的特殊字符无需转义
# 2. 不要添加多余的空格
# 3. 密码可以为空
# 4. 未配置的网盘会自动跳过测试
# 5. Cookie 有效期通常为 1-7 天,过期需要重新获取
+6 -20
View File
@@ -17,7 +17,7 @@
</modules> </modules>
<properties> <properties>
<revision>0.2.1</revision> <revision>0.1.8</revision>
<java.version>17</java.version> <java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
@@ -25,16 +25,14 @@
<packageDirectory>${project.basedir}/web-service/target/package</packageDirectory> <packageDirectory>${project.basedir}/web-service/target/package</packageDirectory>
<!-- Vert.x 4.5.24 已包含安全修复,无需单独指定 Netty 版本 --> <vertx.version>4.5.22</vertx.version>
<vertx.version>4.5.24</vertx.version>
<org.reflections.version>0.10.2</org.reflections.version> <org.reflections.version>0.10.2</org.reflections.version>
<lombok.version>1.18.38</lombok.version> <lombok.version>1.18.38</lombok.version>
<slf4j.version>2.0.16</slf4j.version> <slf4j.version>2.0.5</slf4j.version>
<commons-lang3.version>3.18.0</commons-lang3.version> <commons-lang3.version>3.18.0</commons-lang3.version>
<commons-beanutils2.version>2.0.0</commons-beanutils2.version> <commons-beanutils2.version>2.0.0</commons-beanutils2.version>
<jackson.version>2.18.6</jackson.version> <jackson.version>2.14.2</jackson.version>
<!-- Logback 最新稳定版 --> <logback.version>1.5.19</logback.version>
<logback.version>1.5.18</logback.version>
<junit.version>4.13.2</junit.version> <junit.version>4.13.2</junit.version>
</properties> </properties>
@@ -48,18 +46,6 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<!-- 统一管理 logback 版本 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency> <dependency>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<artifactId>core</artifactId> <artifactId>core</artifactId>
@@ -74,7 +60,7 @@
<dependency> <dependency>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<artifactId>parser</artifactId> <artifactId>parser</artifactId>
<version>10.2.5</version> <version>10.2.3</version>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

+2 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "nfd-web", "name": "nfd-web",
"version": "0.2.1", "version": "0.1.9",
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
@@ -13,10 +13,9 @@
"@element-plus/icons-vue": "^2.3.1", "@element-plus/icons-vue": "^2.3.1",
"@monaco-editor/loader": "^1.4.0", "@monaco-editor/loader": "^1.4.0",
"@vueuse/core": "^11.2.0", "@vueuse/core": "^11.2.0",
"axios": "1.13.5", "axios": "1.12.0",
"clipboard": "^2.0.11", "clipboard": "^2.0.11",
"core-js": "^3.8.3", "core-js": "^3.8.3",
"crypto-js": "^4.2.0",
"element-plus": "2.11.3", "element-plus": "2.11.3",
"monaco-editor": "^0.55.1", "monaco-editor": "^0.55.1",
"qrcode": "^1.5.4", "qrcode": "^1.5.4",
Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 482 KiB

-2
View File
@@ -11,8 +11,6 @@
content="Netdisk fast download 网盘直链解析工具"> content="Netdisk fast download 网盘直链解析工具">
<!-- Font Awesome 图标库 - 使用国内CDN --> <!-- Font Awesome 图标库 - 使用国内CDN -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- 迅雷 JS-SDK -->
<script src="//open.thunderurl.com/thunder-link.js"></script>
<style> <style>
.page-loading-wrap { .page-loading-wrap {
padding: 120px; padding: 120px;
File diff suppressed because it is too large. Load diff
-266
View File
@@ -1,266 +0,0 @@
<template>
<el-dialog
title="文件下载"
v-model="dialogVisible"
width="600px"
:close-on-click-modal="false"
@close="$emit('update:visible', false)"
>
<div v-if="info" class="download-info-content">
<div class="download-file-header">
<i class="fas fa-file" style="margin-right: 8px; color: #409eff;"></i>
<strong>{{ info.fileName || '未命名文件' }}</strong>
</div>
<el-alert
title="该文件需要特殊请求头才能下载,无法直接通过浏览器下载。请使用以下方式之一:"
type="warning"
:closable="false"
show-icon
style="margin-bottom: 16px;"
/>
<el-tabs v-model="activeTab">
<el-tab-pane label="发送到下载器" name="downloader">
<div class="downloader-section">
<template v-if="isThunder">
<p v-if="thunderNeedsCookie" style="color: #f56c6c; margin-bottom: 12px;">
<i class="fas fa-exclamation-circle"></i>
该文件需要 Cookie 认证,迅雷不支持自定义 Cookie,请切换到 Aria2/Motrix/Gopeed
</p>
<p v-else-if="thunderNeedsUa" style="color: #e6a23c; margin-bottom: 12px;">
<i class="fas fa-exclamation-triangle"></i>
该文件需要特殊 User-Agent 才能下载,迅雷客户端可能不支持自定义 UA,下载可能失败。建议切换到 Aria2/Motrix/Gopeed
</p>
<p v-else style="color: #909399; margin-bottom: 12px;">
<i class="fas fa-bolt"></i>
迅雷将通过浏览器唤起本地客户端下载
</p>
</template>
<template v-else>
<p v-if="!connected" style="color: #e6a23c; margin-bottom: 12px;">
<i class="fas fa-exclamation-triangle"></i>
未检测到下载器连接,请先在首页配置下载器(Aria2/Motrix/Gopeed/迅雷)
</p>
<p v-else style="color: #67c23a; margin-bottom: 12px;">
<i class="fas fa-check-circle"></i>
下载器已连接 ({{ downloaderVersion }})
</p>
</template>
<el-button
type="success"
@click="sendToDownloader"
:disabled="(isThunder && thunderNeedsCookie) || (!isThunder && !connected)"
:loading="sending"
>
<i class="fas fa-paper-plane"></i> 发送到下载器
</el-button>
<el-button
v-if="!isThunder"
size="small"
@click="doTestConnection"
style="margin-left: 8px;"
>
测试连接
</el-button>
</div>
</el-tab-pane>
<el-tab-pane label="Aria2 命令" name="aria2">
<el-input
type="textarea"
:model-value="info.aria2Command"
:rows="4"
readonly
resize="none"
class="download-command-textarea"
/>
<div class="download-actions">
<el-button type="primary" size="small" @click="copyText(info.aria2Command)">
<i class="fas fa-copy"></i> 复制 Aria2 命令
</el-button>
</div>
</el-tab-pane>
<el-tab-pane label="Curl 命令" name="curl">
<el-input
type="textarea"
:model-value="info.curlCommand"
:rows="4"
readonly
resize="none"
class="download-command-textarea"
/>
<div class="download-actions">
<el-button type="primary" size="small" @click="copyText(info.curlCommand)">
<i class="fas fa-copy"></i> 复制 Curl 命令
</el-button>
</div>
</el-tab-pane>
</el-tabs>
<div style="margin-top: 16px; text-align: right;">
<el-button size="small" type="warning" @click="doDirectDownload">
直接打开链接(可能失败)
</el-button>
</div>
</div>
</el-dialog>
</template>
<script>
import { testConnection, addDownload, getConfig, hasCookieHeader, hasCustomUaHeader } from '@/utils/downloaderService'
export default {
name: 'DownloadDialog',
props: {
/** v-model:visible 控制弹窗显示 */
visible: {
type: Boolean,
default: false
},
/**
* 下载信息对象
* { downloadUrl, fileName, downloadHeaders, aria2Command, curlCommand, aria2JsonRpc, needDownloader }
*/
downloadInfo: {
type: Object,
default: null
}
},
emits: ['update:visible', 'close'],
data() {
return {
activeTab: 'downloader',
connected: false,
downloaderVersion: '',
sending: false
}
},
computed: {
dialogVisible: {
get() { return this.visible },
set(val) { this.$emit('update:visible', val) }
},
info() { return this.downloadInfo },
isThunder() { return getConfig().downloaderType === 'thunder' },
thunderNeedsCookie() { return this.isThunder && this.info && hasCookieHeader(this.info.downloadHeaders) },
thunderNeedsUa() { return this.isThunder && this.info && hasCustomUaHeader(this.info.downloadHeaders) }
},
watch: {
visible(val) {
if (val) {
this.activeTab = 'downloader'
this.checkConnection()
}
}
},
methods: {
/** 检测下载器连接状态 */
async checkConnection() {
const result = await testConnection()
this.connected = result.connected
this.downloaderVersion = result.version
},
/** 手动测试连接 */
async doTestConnection() {
const result = await testConnection()
this.connected = result.connected
this.downloaderVersion = result.version
if (result.connected) {
this.$message.success(`下载器连接正常 (${result.version})`)
} else {
this.$message.error('无法连接到下载器,请检查配置')
}
},
/** 发送到 Aria2/Motrix/Gopeed */
async sendToDownloader() {
if (!this.info) return
this.sending = true
try {
const gid = await addDownload(
this.info.downloadUrl,
this.info.downloadHeaders,
this.info.fileName
)
this.$message.success('已发送到下载器,任务ID: ' + gid)
this.dialogVisible = false
} catch (error) {
console.error('发送到下载器失败:', error)
this.$message.error('发送到下载器失败: ' + (error.message || '未知错误'))
} finally {
this.sending = false
}
},
/** 直接打开下载链接(可能因缺请求头而失败) */
doDirectDownload() {
if (this.info && this.info.downloadUrl) {
const a = document.createElement('a')
a.href = this.info.downloadUrl
a.target = '_blank'
a.rel = 'noopener noreferrer'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
this.dialogVisible = false
}
},
/** 复制文本到剪贴板 */
async copyText(text) {
if (!text) return
try {
await navigator.clipboard.writeText(text)
this.$message.success('已复制到剪贴板')
} catch {
const textarea = document.createElement('textarea')
textarea.value = text
textarea.style.position = 'fixed'
textarea.style.opacity = '0'
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
document.body.removeChild(textarea)
this.$message.success('已复制到剪贴板')
}
}
}
}
</script>
<style scoped>
.download-info-content {
padding: 0 4px;
}
.download-file-header {
font-size: 16px;
margin-bottom: 12px;
padding: 10px 14px;
background: #f0f7ff;
border-radius: 8px;
display: flex;
align-items: center;
word-break: break-all;
}
:deep(.dark) .download-file-header,
.dark-theme .download-file-header {
background: #1a3350;
}
.download-command-textarea :deep(.el-textarea__inner) {
font-family: 'Courier New', Courier, monospace;
font-size: 12px;
background: #f5f5f5;
color: #333;
}
:deep(.dark) .download-command-textarea :deep(.el-textarea__inner),
.dark-theme .download-command-textarea :deep(.el-textarea__inner) {
background: #1e1e1e;
color: #d4d4d4;
}
.download-actions {
margin-top: 10px;
display: flex;
gap: 8px;
}
.downloader-section {
padding: 16px 0;
}
</style>
+1 -13
View File
@@ -274,7 +274,7 @@
name: '联想乐云' name: '联想乐云'
}, },
fangcloud: { fangcloud: {
reg: /https:\/\/v2\.fangcloud\.(com|cn)\/(s|share|sharing)\/.+/, reg: /https:\/\/v2\.fangcloud\.(com|cn)\/(s|sharing)\/.+/,
host: /fangcloud\.(com|cn)/, host: /fangcloud\.(com|cn)/,
name: '亿方云' name: '亿方云'
}, },
@@ -345,18 +345,6 @@
host: /www\.kdocs\.cn/, host: /www\.kdocs\.cn/,
name: 'WPS云文档' name: 'WPS云文档'
}, },
quark: {
reg: /https:\/\/pan\.quark\.cn\/s\/[a-zA-Z\d]+/,
host: /pan\.quark\.cn/,
name: '夸克网盘'
},
uc: {
reg: /https:\/\/(fast|drive)\.uc\.cn\/s\/[a-zA-Z\d]+(?:\?public=\d+)?(?:[#&].*)?/,
host: /(fast|drive)\.uc\.cn/,
name: 'UC网盘'
},
other: { other: {
reg: /https:\/\/([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,}\/s\/.+/, reg: /https:\/\/([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,}\/s\/.+/,
host: /.*/, host: /.*/,
-463
View File
@@ -1,463 +0,0 @@
/**
* 下载器服务 - 统一管理 Aria2/Motrix/Gopeed/迅雷 的配置读取、连接检测、RPC 调用
* 供 Home.vue、DirectoryTree.vue、DownloadDialog.vue 等共用
*/
import axios from 'axios'
const STORAGE_KEY = 'nfd-aria2-local-config'
const DEFAULT_CONFIG = {
downloaderType: 'aria2',
rpcUrl: 'http://localhost:6800/jsonrpc',
rpcSecret: '',
downloadDir: ''
}
/**
* 从 localStorage 读取下载器配置
* @returns {{ downloaderType: string, rpcUrl: string, rpcSecret: string, downloadDir: string }}
*/
export function getConfig() {
try {
const raw = localStorage.getItem(STORAGE_KEY)
if (raw) {
const parsed = JSON.parse(raw)
return {
downloaderType: parsed.downloaderType || DEFAULT_CONFIG.downloaderType,
rpcUrl: parsed.rpcUrl || DEFAULT_CONFIG.rpcUrl,
rpcSecret: parsed.rpcSecret || '',
downloadDir: parsed.downloadDir || ''
}
}
} catch (e) {
console.warn('读取下载器配置失败', e)
}
return { ...DEFAULT_CONFIG }
}
/**
* 保存下载器配置到 localStorage
* @param {{ downloaderType?: string, rpcUrl?: string, rpcSecret?: string, downloadDir?: string }} config
*/
export function saveConfig(config) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(config))
}
/**
* 构建 RPC 参数数组(自动添加 token)
* @param {string} rpcSecret
* @param {Array} extraParams
* @returns {Array}
*/
function buildRpcParams(rpcSecret, extraParams = []) {
const params = []
if (rpcSecret && rpcSecret.trim()) {
params.push(`token:${rpcSecret}`)
}
if (extraParams && extraParams.length > 0) {
params.push(...extraParams)
}
return params
}
/**
* 调用 Aria2 JSON-RPC 接口
* @param {string} rpcUrl
* @param {string} rpcSecret
* @param {string} method - 例如 'aria2.getVersion', 'aria2.addUri'
* @param {Array} [extraParams] - 除 token 外的参数
* @param {number} [timeout=5000]
* @returns {Promise<Object>} RPC 响应的 data
*/
export async function callRpc(rpcUrl, rpcSecret, method, extraParams = [], timeout = 5000) {
const requestBody = {
jsonrpc: '2.0',
id: Date.now().toString(),
method,
params: buildRpcParams(rpcSecret, extraParams)
}
const response = await axios.post(rpcUrl, requestBody, {
headers: { 'Content-Type': 'application/json' },
timeout
})
if (response.data && response.data.error) {
throw new Error(response.data.error.message || 'Aria2 RPC 错误')
}
return response.data
}
/**
* 判断 rpcUrl 是否指向 Gopeed(端口 9999 或 URL 含 /api/v1)
* @param {string} url
* @returns {boolean}
*/
function isGopeedUrl(url) {
if (!url) return false
return url.includes(':9999') || url.includes('/api/v1')
}
/**
* 从 Gopeed rpcUrl 中提取 baseUrl(去掉 /jsonrpc 或 /api/v1 后缀)
* 例如 "http://localhost:9999/jsonrpc" → "http://localhost:9999"
* @param {string} rpcUrl
* @returns {string}
*/
function gopeedBaseUrl(rpcUrl) {
return rpcUrl.replace(/\/jsonrpc$/, '').replace(/\/api\/v1.*$/, '')
}
/**
* 调用 Gopeed REST API
* @param {string} baseUrl - 例如 "http://localhost:9999"
* @param {string} rpcSecret - Bearer token
* @param {string} method - 'GET' | 'POST'
* @param {string} path - 例如 '/api/v1/version'
* @param {Object} [body] - POST body
* @param {number} [timeout=5000]
* @returns {Promise<Object>} 响应 data
*/
async function callGopeedApi(baseUrl, rpcSecret, method, path, body, timeout = 5000) {
const headers = { 'Content-Type': 'application/json' }
if (rpcSecret && rpcSecret.trim()) {
headers['X-Api-Token'] = rpcSecret
}
const url = baseUrl.replace(/\/$/, '') + path
const response = await axios({ method, url, headers, data: body, timeout })
return response.data
}
/**
* 测试下载器连接(自动识别 迅雷 / Gopeed / Aria2 / Motrix)
* @param {string} [rpcUrl] - 不传则自动读取配置
* @param {string} [rpcSecret] - 不传则自动读取配置
* @returns {Promise<{ connected: boolean, version: string }>}
*/
export async function testConnection(rpcUrl, rpcSecret) {
if (!rpcUrl) {
const config = getConfig()
// 迅雷不需要 RPC,直接检测 JS SDK
if (config.downloaderType === 'thunder') {
const available = typeof window !== 'undefined' && window.thunderLink && typeof window.thunderLink.newTask === 'function'
return { connected: available, version: available ? 'JS-SDK' : '' }
}
rpcUrl = config.rpcUrl
rpcSecret = rpcSecret ?? config.rpcSecret
}
try {
if (isGopeedUrl(rpcUrl)) {
// Gopeed 使用 REST API:GET /api/v1/info
const base = gopeedBaseUrl(rpcUrl)
const res = await callGopeedApi(base, rpcSecret || '', 'GET', '/api/v1/info', undefined, 3000)
const d = (res && res.code === 0 && res.data) ? res.data : {}
const version = [d.version, d.runtime].filter(Boolean).join(' + ') || ''
return { connected: true, version }
} else {
// Aria2 / Motrix 使用 JSON-RPC
const res = await callRpc(rpcUrl, rpcSecret || '', 'aria2.getVersion', [], 3000)
if (res && res.result && res.result.version) {
return { connected: true, version: res.result.version }
}
return { connected: false, version: '' }
}
} catch {
return { connected: false, version: '' }
}
}
/**
* 自动检测本地下载器(依次尝试 Motrix/Gopeed/Aria2)
* @param {string} [rpcSecret] - 可选密钥
* @returns {Promise<{ found: boolean, type: string, rpcUrl: string, version: string }>}
*/
export async function autoDetect(rpcSecret = '') {
const candidates = [
{ type: 'motrix', port: 16800, path: '/jsonrpc' },
{ type: 'gopeed', port: 9999, path: '/api/v1/info', gopeed: true },
{ type: 'aria2', port: 6800, path: '/jsonrpc' }
]
for (const c of candidates) {
try {
if (c.gopeed) {
// Gopeed:直接调 REST GET /api/v1/info
const base = `http://localhost:${c.port}`
const res = await callGopeedApi(base, rpcSecret || '', 'GET', '/api/v1/info', undefined, 3000)
const d = (res && res.code === 0 && res.data) ? res.data : {}
const version = [d.version, d.runtime].filter(Boolean).join(' + ') || 'unknown'
return { found: true, type: c.type, rpcUrl: `${base}/api/v1`, version }
} else {
const url = `http://localhost:${c.port}${c.path}`
const result = await testConnection(url, rpcSecret)
if (result.connected) {
return { found: true, type: c.type, rpcUrl: url, version: result.version }
}
}
} catch {
// 该端口未响应,继续下一个
}
}
return { found: false, type: '', rpcUrl: '', version: '' }
}
/**
* 发送下载任务到下载器(自动识别 迅雷 / Gopeed / Aria2 / Motrix)
* @param {string} downloadUrl - 文件下载地址
* @param {Object} [headers] - 请求头 {cookie, referer, user-agent, ...}
* @param {string} [fileName] - 输出文件名
* @param {{ rpcUrl?: string, rpcSecret?: string, downloadDir?: string, downloaderType?: string }} [configOverride] - 覆盖配置
* @returns {Promise<string>} 任务 ID / GID
*/
export async function addDownload(downloadUrl, headers, fileName, configOverride) {
const config = { ...getConfig(), ...configOverride }
if (config.downloaderType === 'thunder') {
return addThunderDownload([{ url: downloadUrl, headers, fileName }], config)
}
if (isGopeedUrl(config.rpcUrl)) {
// Gopeed REST API:POST /api/v1/tasks
const base = gopeedBaseUrl(config.rpcUrl)
const extraHeader = {}
if (headers && typeof headers === 'object') {
for (const [key, value] of Object.entries(headers)) {
if (key && value) extraHeader[key] = value
}
}
const body = {
req: { url: downloadUrl, extra: { header: extraHeader } },
opt: {}
}
if (config.downloadDir) body.opt.path = config.downloadDir
const res = await callGopeedApi(base, config.rpcSecret, 'POST', '/api/v1/tasks', body, 10000)
// Gopeed 返回 { code: 0, data: "task-id" }
if (res && res.code !== undefined && res.code !== 0) throw new Error(res.message || 'Gopeed 发送失败')
if (res && res.data) return typeof res.data === 'string' ? res.data : JSON.stringify(res.data)
return 'ok'
}
// Aria2 / Motrix JSON-RPC
const options = {}
if (headers && typeof headers === 'object') {
const headerArray = []
for (const [key, value] of Object.entries(headers)) {
if (key && value) headerArray.push(`${key}: ${value}`)
}
if (headerArray.length > 0) options.header = headerArray
}
if (fileName) options.out = fileName
if (config.downloadDir) options.dir = config.downloadDir
const res = await callRpc(config.rpcUrl, config.rpcSecret, 'aria2.addUri', [[downloadUrl], options], 10000)
if (res && res.result) return res.result // GID
throw new Error('未知错误')
}
/**
* 批量发送下载任务到下载器(aria2 用 system.multicall,gopeed 用 batch API,迅雷用 JS-SDK newTask)
* @param {{ url: string, headers?: Object, fileName?: string }[]} tasks - 下载任务列表
* @param {{ rpcUrl?: string, rpcSecret?: string, downloadDir?: string, downloaderType?: string }} [configOverride]
* @returns {Promise<{ succeeded: number, failed: number, errors: string[] }>}
*/
export async function batchAddDownload(tasks, configOverride) {
if (!tasks || tasks.length === 0) return { succeeded: 0, failed: 0, errors: [] }
if (tasks.length === 1) {
try {
await addDownload(tasks[0].url, tasks[0].headers, tasks[0].fileName, configOverride)
return { succeeded: 1, failed: 0, errors: [] }
} catch (e) {
return { succeeded: 0, failed: 1, errors: [e.message || '未知错误'] }
}
}
const config = { ...getConfig(), ...configOverride }
if (config.downloaderType === 'thunder') {
try {
await addThunderDownload(tasks, config)
return { succeeded: tasks.length, failed: 0, errors: [] }
} catch (e) {
return { succeeded: 0, failed: tasks.length, errors: [e.message || '迅雷下载失败'] }
}
}
if (isGopeedUrl(config.rpcUrl)) {
return batchAddGopeed(tasks, config)
} else {
return batchAddAria2(tasks, config)
}
}
async function batchAddAria2(tasks, config) {
const calls = tasks.map(task => {
const options = {}
if (task.headers && typeof task.headers === 'object') {
const headerArray = []
for (const [key, value] of Object.entries(task.headers)) {
if (key && value) headerArray.push(`${key}: ${value}`)
}
if (headerArray.length > 0) options.header = headerArray
}
if (task.fileName) options.out = task.fileName
if (config.downloadDir) options.dir = config.downloadDir
const params = []
if (config.rpcSecret && config.rpcSecret.trim()) {
params.push(`token:${config.rpcSecret}`)
}
params.push([task.url], options)
return { methodName: 'aria2.addUri', params }
})
try {
const requestBody = {
jsonrpc: '2.0',
id: Date.now().toString(),
method: 'system.multicall',
params: [calls]
}
const response = await axios.post(config.rpcUrl, requestBody, {
headers: { 'Content-Type': 'application/json' },
timeout: Math.max(10000, tasks.length * 500)
})
const results = response.data && response.data.result
if (!Array.isArray(results)) {
throw new Error(response.data?.error?.message || 'system.multicall 返回异常')
}
let succeeded = 0, failed = 0
const errors = []
for (let i = 0; i < results.length; i++) {
const r = results[i]
if (Array.isArray(r) && r.length > 0 && typeof r[0] === 'string') {
succeeded++
} else if (r && r.faultCode) {
failed++
errors.push(`${tasks[i].fileName || tasks[i].url}: ${r.faultString || '未知错误'}`)
} else {
succeeded++
}
}
return { succeeded, failed, errors }
} catch (e) {
return { succeeded: 0, failed: tasks.length, errors: [e.message || 'multicall 请求失败'] }
}
}
async function batchAddGopeed(tasks, config) {
const base = gopeedBaseUrl(config.rpcUrl)
const reqs = tasks.map(task => {
const extraHeader = {}
if (task.headers && typeof task.headers === 'object') {
for (const [key, value] of Object.entries(task.headers)) {
if (key && value) extraHeader[key] = value
}
}
const item = { req: { url: task.url, extra: { header: extraHeader } } }
if (task.fileName) {
item.opts = { name: task.fileName }
}
return item
})
const body = { reqs }
if (config.downloadDir) body.opts = { path: config.downloadDir }
try {
const res = await callGopeedApi(base, config.rpcSecret, 'POST', '/api/v1/tasks/batch', body,
Math.max(10000, tasks.length * 500))
if (res && res.code !== undefined && res.code !== 0) {
return { succeeded: 0, failed: tasks.length, errors: [res.message || 'Gopeed batch 失败'] }
}
const ids = Array.isArray(res?.data) ? res.data : []
return { succeeded: ids.length || tasks.length, failed: 0, errors: [] }
} catch (e) {
return { succeeded: 0, failed: tasks.length, errors: [e.message || 'Gopeed batch 请求失败'] }
}
}
/**
* 通过迅雷 JS-SDK 发送下载任务
* @param {{ url: string, headers?: Object, fileName?: string }[]} tasks
* @param {{ downloadDir?: string }} config
* @returns {Promise<string>}
*/
function addThunderDownload(tasks, config) {
if (typeof window === 'undefined' || !window.thunderLink || typeof window.thunderLink.newTask !== 'function') {
return Promise.reject(new Error('迅雷客户端未检测到,请确认已安装并启动迅雷'))
}
// 迅雷 JS-SDK 不支持自定义 Cookie,含 Cookie 的下载链接无法通过迅雷下载
const firstHeaders = (tasks[0] && tasks[0].headers) || {}
if (firstHeaders.cookie || firstHeaders.Cookie) {
return Promise.reject(new Error('该文件需要 Cookie 认证,迅雷不支持自定义 Cookie,请使用 Aria2/Motrix/Gopeed'))
}
// 遍历所有 header key 大小写不敏感地提取 referer / user-agent
let referer = ''
let userAgent = ''
for (const [key, value] of Object.entries(firstHeaders)) {
const lk = key.toLowerCase()
if (lk === 'referer' && value) referer = value
if (lk === 'user-agent' && value) userAgent = value
}
const taskParam = {
tasks: tasks.map(t => {
const item = { url: t.url }
if (t.fileName) item.name = t.fileName
return item
})
}
if (config.downloadDir) taskParam.downloadDir = config.downloadDir
if (referer) taskParam.referer = referer
if (userAgent) taskParam.userAgent = userAgent
taskParam.threadCount = '1'
console.log('[Thunder SDK] newTask params:', JSON.stringify(taskParam))
window.thunderLink.newTask(taskParam)
return Promise.resolve('thunder-ok')
}
/**
* 根据 RPC URL 猜测下载器类型
* @param {string} url
* @returns {string}
*/
export function guessDownloaderType(url) {
if (!url) return 'aria2'
if (url.includes(':16800')) return 'motrix'
if (url.includes(':9999')) return 'gopeed'
return 'aria2'
}
/**
* 检查下载头中是否含有 Cookie(迅雷不支持)
* @param {Object} [headers]
* @returns {boolean}
*/
export function hasCookieHeader(headers) {
if (!headers || typeof headers !== 'object') return false
return !!(headers.cookie || headers.Cookie)
}
/**
* 检查下载头中是否含有自定义 User-Agent(迅雷客户端可能不支持)
* @param {Object} [headers]
* @returns {boolean}
*/
export function hasCustomUaHeader(headers) {
if (!headers || typeof headers !== 'object') return false
for (const key of Object.keys(headers)) {
if (key.toLowerCase() === 'user-agent' && headers[key]) return true
}
return false
}
export default {
getConfig,
saveConfig,
callRpc,
testConnection,
autoDetect,
addDownload,
batchAddDownload,
guessDownloaderType,
hasCookieHeader
}
+1 -15
View File
@@ -5,15 +5,6 @@ const axiosInstance = axios.create({
withCredentials: true // 重要:允许跨域请求携带cookie withCredentials: true // 重要:允许跨域请求携带cookie
}); });
// 请求拦截器:将存储的Token添加到Authorization请求头
axiosInstance.interceptors.request.use(config => {
const token = localStorage.getItem('playground_token');
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
}
return config;
});
/** /**
* 演练场API服务 * 演练场API服务
*/ */
@@ -39,12 +30,7 @@ export const playgroundApi = {
async login(password) { async login(password) {
try { try {
const response = await axiosInstance.post('/v2/playground/login', { password }); const response = await axiosInstance.post('/v2/playground/login', { password });
const data = response.data; return response.data;
// 登录成功时从响应中提取并保存Token
if ((data.code === 200 || data.success) && data.data?.token) {
localStorage.setItem('playground_token', data.data.token);
}
return data;
} catch (error) { } catch (error) {
throw new Error(error.response?.data?.error || error.message || '登录失败'); throw new Error(error.response?.data?.error || error.message || '登录失败');
} }
+449 -184
View File
@@ -3,8 +3,8 @@
<el-card> <el-card>
<template #header> <template #header>
<div style="display: flex; align-items: center; gap: 12px;"> <div style="display: flex; align-items: center; gap: 12px;">
<el-icon @click="goBack" style="cursor: pointer; font-size: 20px;"><Close /></el-icon> <el-button @click="goBack" :icon="ArrowLeft" circle size="small" />
<span>客户端下载链接生成器</span> <span>客户端下载链接生成器 (实验功能)</span>
</div> </div>
</template> </template>
@@ -22,42 +22,6 @@
</el-descriptions> </el-descriptions>
</div> </div>
<!-- 认证提示 -->
<div v-if="result && result.authRequirement && result.authRequirement !== 'none'" style="margin-bottom: 20px;">
<el-alert
:title="result.authRequirement === 'required' ? '需要认证信息' : '可选认证信息'"
:type="result.authRequirement === 'required' ? 'warning' : 'info'"
show-icon
:closable="false"
>
<template #default>
<div style="font-size: 13px;">
{{ result.authHint }}
<el-link type="primary" @click="goToAuthConfig" style="margin-left: 8px;">
<el-icon><Key /></el-icon>
去配置认证信息
</el-link>
</div>
</template>
</el-alert>
</div>
<!-- 需要客户端提示 -->
<div v-if="result && result.requiresClient" style="margin-bottom: 20px;">
<el-alert
title="此网盘需要使用下载工具"
type="warning"
show-icon
:closable="false"
>
<template #default>
<div style="font-size: 13px;">
该网盘的直链需要特殊请求头,浏览器无法直接下载。请使用以下客户端工具复制命令或链接进行下载。
</div>
</template>
</el-alert>
</div>
<!-- 结果展示区域 --> <!-- 结果展示区域 -->
<div v-if="result"> <div v-if="result">
<!-- 文件基本信息 --> <!-- 文件基本信息 -->
@@ -72,14 +36,11 @@
<el-descriptions-item label="解析器" v-if="result.parserInfo"> <el-descriptions-item label="解析器" v-if="result.parserInfo">
<el-tag type="warning">{{ result.parserInfo }}</el-tag> <el-tag type="warning">{{ result.parserInfo }}</el-tag>
</el-descriptions-item> </el-descriptions-item>
<el-descriptions-item label="直链" v-if="result.directLink && !result.requiresClient"> <el-descriptions-item label="直链" v-if="result.directLink">
<el-link :href="result.directLink" target="_blank" type="primary"> <el-link :href="result.directLink" target="_blank" type="primary">
点击下载 点击下载
</el-link> </el-link>
</el-descriptions-item> </el-descriptions-item>
<el-descriptions-item label="直链" v-else-if="result.directLink && result.requiresClient">
<el-tag type="danger">需要客户端工具下载</el-tag>
</el-descriptions-item>
</el-descriptions> </el-descriptions>
</div> </div>
@@ -92,72 +53,128 @@
> >
<template #label> <template #label>
<div style="display: flex; align-items: center; gap: 8px;"> <div style="display: flex; align-items: center; gap: 8px;">
<el-icon
v-if="isElementIcon(getClientLogo(type))"
:size="20"
:color="getClientIconColor(type)"
>
<component :is="getClientLogo(type)" />
</el-icon>
<img <img
v-if="getClientLogo(type)" v-else
:src="getClientLogo(type)" :src="getClientLogo(type)"
:alt="getClientDisplayName(type)" :alt="getClientDisplayName(type)"
style="width: 20px; height: 20px;" style="width: 20px; height: 20px;"
@error="handleImageError" @error="handleImageError"
/> />
<el-icon v-else :size="20"><Download /></el-icon>
<span>{{ getClientDisplayName(type) }}</span> <span>{{ getClientDisplayName(type) }}</span>
<el-tag v-if="!getClientSupportsCookie(type)" type="danger" size="small">不支持Cookie</el-tag> <el-icon
v-if="isClientInstalled(type)"
color="#67C23A"
size="16"
title="已安装"
>
<Check />
</el-icon>
<el-icon
v-else
color="#E6A23C"
size="16"
title="未安装或无法检测"
>
<QuestionFilled />
</el-icon>
</div> </div>
</template> </template>
<el-card> <el-card>
<template #header> <template #header>
<div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px;"> <div style="display: flex; justify-content: space-between; align-items: center;">
<div> <div>
<el-tag :type="getClientTagType(type)" size="large"> <el-tag :type="getClientTagType(type)" size="large">
{{ getClientDisplayName(type) }} {{ getClientDisplayName(type) }}
</el-tag> </el-tag>
<span style="margin-left: 12px; color: #666;"> <span style="margin-left: 12px; color: #666;">
{{ getClientDescription(type) }} {{ getClientDescription(type) }}
</span> </span>
</div> </div>
<div style="display: flex; gap: 8px;"> <div style="display: flex; gap: 8px;">
<el-button
size="small"
type="primary"
@click="copyToClipboard(link)"
:icon="CopyDocument"
>
复制
</el-button>
<el-button
size="small"
type="success"
@click="downloadWithClient(type, link)"
:icon="Download"
>
下载
</el-button>
<el-button <el-button
v-if="!isClientInstalled(type) && shouldShowDownloadButton(type)"
size="small" size="small"
type="primary" type="warning"
@click="copyToClipboard(link)" @click="downloadClient(type)"
:icon="CopyDocument" :icon="Download"
> >
复制 下载客户端
</el-button> </el-button>
</div> </div>
</div> </div>
</template> </template>
<el-input <el-input
:model-value="link" :model-value="link"
type="textarea" type="textarea"
:rows="getTextareaRows(link)" :rows="getTextareaRows(link)"
readonly readonly
placeholder="客户端下载命令将显示在这里" placeholder="客户端下载命令将显示在这里"
style="font-family: 'Courier New', 'Consolas', monospace; font-size: 13px;" style="font-family: 'Courier New', 'Consolas', monospace; font-size: 13px;"
/> />
</el-card> </el-card>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
<!-- 客户端说明 --> <!-- 支持的客户端类型 -->
<div style="margin-top: 20px;"> <div v-if="result.supportedClients" style="margin-top: 20px;">
<el-divider content-position="left"> <el-divider content-position="left">
<el-icon><InfoFilled /></el-icon> <el-icon><Star /></el-icon>
使用说明 支持的客户端类型
</el-divider> </el-divider>
<el-descriptions :column="1" border> <div style="display: flex; flex-wrap: wrap; gap: 8px;">
<el-descriptions-item label="cURL 命令"> <el-tag
支持Cookie,复制命令到终端运行即可下载 v-for="(name, code) in result.supportedClients"
</el-descriptions-item> :key="code"
<el-descriptions-item label="Aria2"> size="small"
支持Cookie,多线程下载器,复制命令后在终端运行或配置到Aria2客户端 :type="getClientTagType(code.toUpperCase())"
</el-descriptions-item> >
<el-descriptions-item label="迅雷"> {{ name }}
<el-text type="danger">不支持Cookie</el-text>,仅适用于无需Cookie的直链,点击下载按钮可唤起迅雷 </el-tag>
</el-descriptions-item> </div>
</el-descriptions>
<!-- 添加说明信息 -->
<el-alert
title="客户端检测说明"
type="info"
:closable="false"
style="margin-top: 12px;"
>
<template #default>
<div style="font-size: 13px;">
<p style="margin: 0 0 8px 0;">
• 为了避免自动打开外部应用,系统不会自动检测大部分下载器是否已安装
</p>
<p style="margin: 0;">
• 您可以点击"下载"按钮尝试使用相应的下载器,或点击"下载客户端"按钮安装
</p>
</div>
</template>
</el-alert>
</div> </div>
</div> </div>
@@ -173,7 +190,7 @@
<!-- 空状态 --> <!-- 空状态 -->
<div v-if="!result && !error && !form.shareUrl" style="margin-top: 40px;"> <div v-if="!result && !error && !form.shareUrl" style="margin-top: 40px;">
<el-empty description="请从主页点击'客户端下载'按钮开始使用" /> <el-empty description="请从主页点击'客户端链接(实验)'按钮开始使用" />
</div> </div>
</el-card> </el-card>
</div> </div>
@@ -182,17 +199,19 @@
<script> <script>
import { ref, reactive, onMounted } from 'vue' import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { Download, Close, CopyDocument, Key, InfoFilled } from '@element-plus/icons-vue' import { Download, ArrowLeft, CopyDocument, Star, Check, QuestionFilled, Setting } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
export default { export default {
name: 'ClientLinks', name: 'ClientLinks',
components: { components: {
Download, Download,
Close, ArrowLeft,
CopyDocument, CopyDocument,
Key, Star,
InfoFilled Check,
QuestionFilled,
Setting
}, },
setup() { setup() {
const form = reactive({ const form = reactive({
@@ -204,6 +223,7 @@ export default {
const error = ref('') const error = ref('')
const router = useRouter() const router = useRouter()
const activeTab = ref('') const activeTab = ref('')
const installedClients = ref(new Set())
// 从 sessionStorage 获取从 Home 页面传递的数据 // 从 sessionStorage 获取从 Home 页面传递的数据
const loadDataFromHome = () => { const loadDataFromHome = () => {
@@ -229,86 +249,53 @@ export default {
} }
} }
// 客户端配置(只保留3种) // 客户端 Logo 映射
const clientConfig = {
'CURL': {
displayName: 'cURL 命令',
description: '命令行下载工具,支持Cookie',
logo: 'https://gcore.jsdelivr.net/gh/simple-icons/simple-icons@develop/icons/curl.svg',
tagType: 'success',
supportsCookie: true,
downloadUrl: 'https://curl.se/download.html'
},
'ARIA2': {
displayName: 'Aria2',
description: '多线程下载器,支持Cookie',
logo: 'https://gcore.jsdelivr.net/gh/simple-icons/simple-icons@develop/icons/gnometerminal.svg',
tagType: 'warning',
supportsCookie: true,
downloadUrl: 'https://aria2.github.io/'
},
'THUNDER': {
displayName: '迅雷',
description: '迅雷下载器,不支持Cookie',
logo: null, // 使用base64
tagType: 'primary',
supportsCookie: false,
downloadUrl: 'https://www.xunlei.com/'
}
}
// 迅雷logo(base64)
const thunderLogo = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBmaWxsPSIjMUJBMERCIiBkPSJNMTMgMkw0IDEzaDZsLTEgOWw5LTExaC02eiIvPjwvc3ZnPg=='
// 获取客户端 Logo
const getClientLogo = (type) => { const getClientLogo = (type) => {
if (type === 'THUNDER') { const logoMap = {
return thunderLogo 'ARIA2': 'https://gcore.jsdelivr.net/gh/simple-icons/simple-icons@develop/icons/gnometerminal.svg',
'MOTRIX': 'Download',
'BITCOMET': 'Download',
'THUNDER': 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB3aWR0aD0iNTYiIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgMCA1NiAyMCI+PGRlZnM+PHJlY3QgaWQ9ImEiIHdpZHRoPSI1NiIgaGVpZ2h0PSIyMCIgeD0iMCIgeT0iMCIgcng9IjMiLz48L2RlZnM+PGcgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj48bWFzayBpZD0iYiIgZmlsbD0iI2ZmZiI+PHVzZSB4bGluazpocmVmPSIjYSIvPjwvbWFzaz48dXNlIHhsaW5rOmhyZWY9IiNhIiBmaWxsPSIjMUJBMERCIi8+PHJlY3Qgd2lkdGg9IjIyIiBoZWlnaHQ9IjIwIiBmaWxsPSIjMEU2QUIyIiBtYXNrPSJ1cmwoI2IpIi8+PHRleHQgZmlsbD0iI0ZGRiIgZm9udC1mYW1pbHk9IlBpbmdGYW5nU0MtU2VtaWJvbGQsIFBpbmdGYW5nIFNDIiBmb250LXNpemU9IjEyIiBmb250LXdlaWdodD0iNTAwIiBtYXNrPSJ1cmwoI2IpIj48dHNwYW4geD0iMjYuNSIgeT0iMTQiPuq4gOq4gDwvdHNwYW4+PC90ZXh0PjxwYXRoIGZpbGw9IiNGRkYiIGQ9Ik0xNi44MTIxNDE4LDMuMTI4OCBDMTUuODU0OTIyLDMuNTMyNzU4MzMgMTQuNjEyNzA5Miw0LjE3MDM0MTY3IDEzLjU1NzI3NjYsNC44MDAwNSBDMTIuMjQ1NzMwNSw1LjU4MjE4MzMzIDEwLjk4MDM2ODgsNi40NjEzMjUgOS45MDc2MzEyMSw3LjMzNjU1ODMzIEw5LjY5NTc3MzA1LDcuNTA4ODE2NjcgOS42NTcyNDgyMyw3LjQ2MTI3NSBDOS42MzYwODUxMSw3LjQzMzU2NjY3IDkuNTY4NjgwODUsNy4zNDI0NSA5LjUwODk5MjkxLDcuMjU3MzQxNjcgQzguOTYzOTE0ODksNi40ODUwNjY2NyA4LjE5NzM5MDA3LDUuOTUwNDQxNjcgNy40NTU5NDMyNiw1LjgyMTc1ODMzIEM3LjEzNjIyNjk1LDUuNzY4MjY2NjcgNi42NzM5ODU4Miw1LjgxNTgwODMzIDYuMzM1MDM1NDYsNS45NDA1ODMzMyBDNi4xNzMyNzY2LDUuOTk3OTgzMzMgNi4xNTc4NDM5Nyw1Ljk5OTk2NjY3IDQuNjI4NjUyNDgsNi4xMTg3OTE2NyBDMy43ODEyMTk4Niw2LjE4NjEwODMzIDMuMDY2NzIzNCw2LjIzOTU0MTY3IDMuMDQxNzAyMTMsNi4yMzk1NDE2NyBDMi45NDUzNjE3LDYuMjM5NTQxNjcgMy4wMzIwNTY3NCw2LjI2MzM0MTY3IDMuNDA5NTMxOTEsNi4zNDA1MTY2NyBDNC4zNjI4OTM2Miw2LjUzMjYwODMzIDQuODk2MzQwNDMsNi42NzEyMDgzMyA1LjM3MjA4NTExLDYuODQ5NDE2NjcgQzUuNjYwOTM2MTcsNi45NTYzNDE2NyA2LjE5NjM2ODc5LDcuMTg2MDU4MzMgNi4yMDc5NDMyNiw3LjIwNzgxNjY3IEM2LjIyOTEwNjM4LDcuMjQzNDU4MzMgNi4wOTA0Mzk3Miw4LjM1MDMzMzMzIDYuMDI0OTY0NTQsOC42NzUwNzUgQzYuMDAzNzQ0NjgsOC43NzgwMzMzMyA1Ljk3Njc5NDMzLDguOTEyNzI1IDUuOTYzMzQ3NTIsOC45NzIxMDgzMyBDNS45NTE3NzMwNSw5LjAzMTQ5MTY3IDUuOTQyMTI3NjYsOS4yNDUzNDE2NyA1Ljk0MDE5ODU4LDkuNDQ3MzUgQzUuOTQwMTk4NTgsOS43NTIyNTgzMyA1Ljk0NzkxNDg5LDkuODUxMjUgNS45ODQ1MTA2NCwxMC4wMzU0MDgzIEM2LjEyMzE3NzMsMTAuNzQ2MjU4MyA2LjQ2MjEyNzY2LDExLjM2MjA4MzMgNy4wMzk5NDMyNiwxMS45NTIxODMzIEM3LjM3ODg5MzYyLDEyLjI5ODY4MzMgNy43MTIwNTY3NCwxMi41NjAwNzUgOC41NDAyNTUzMiwxMy4xMjI0MDgzIEM5LjAxMDE1NjAzLDEzLjQ0MzE4MzMgOS4zMDg2NTI0OCwxMy42MjkzMjUgOS44NjcxNzczLDEzLjk1MjA4MzMgQzEwLjA4ODY4MDksMTQuMDc4NzgzMyAxMC4zMjc0ODk0LDE0LjIxOTM2NjcgMTAuMzk2ODIyNywxNC4yNjQ5MjUgQzEwLjkxODc1MTgsMTQuNjAxNTY2NyAxMS4zMzg2MDk5LDE0Ljk4MzcwODMzIDEyLjQ5OTk3MTYsMTYuMTc5NzE2NyBDMTMuMTIwMTEzNSwxNi44MjEyNjY3IDEzLjMzNzc1ODksMTcuMDMzMTMzMyAxMy4zMzAwNDI2LDE2Ljk5MzUyNSBDMTMuMzAzMDkyMiwxNi44MjkxNDE3IDEzLjEyMDExMzUsMTUuODk4NDkxNyAxMy4wNTQ2MzgzLDE1LjU5NTU2NjcgQzEyLjc4MTE2MzEsMTQuMzIyMzI1IDEyLjQ2NzIzNCwxMy4yMTM0NjY3IDEyLjIyNDU2NzQsMTIuNjY1MDE2NyBDMTIuMTcyNTM5LDEyLjU0NDIwODMgMTIuMTE2NzA5MiwxMi40MTk0OTE3IDEyLjEwMzIwNTcsMTIuMzg3ODE2NyBMMTIuMDc4MTg0NCwxMi4zMjgzNzUgTDEyLjU5MDQ2ODEsMTIuMzM2MzA4MyBDMTMuMDIxOTAwNywxMi4zNDIyNTgzIDEzLjg1Nzc1ODksMTIuMzEwNTgzMyAxMy44ODI3ODAxLDEyLjI4ODc2NjcgQzEzLjg4NDcwOTIsMTIuMjg0OCAxMy44NjE2MTcsMTIuMTk1NzI1IDEzLjgyNjk1MDQsMTIuMDkwNzgzMyBDMTMuNzkyMjgzNywxMS45ODU4NDE3IDEzLjc2OTEzNDgsMTEuODk2NzA4MyAxMy43NzI5OTI5LDExLjg5MDc1ODMgQzEzLjc3ODc4MDEsMTEuODg2NzkxNyAxMy45MTkzNzU5LDExLjk1MDIgMTQuMDg4ODUxMSwxMi4wMzMzMjUgTDE0LjM5NTA2MzgsMTIuMTgxODQxNyBMMTQuOTUzNTg4NywxMi4wMTk1IEMxNS4yNTk4MDE0LDExLjkzMDM2NjcgMTUuNTE5ODI5OCwxMS44NTMxMzMzIDE1LjUyOTQ3NTIsMTEuODQ3MjQxNyBDMTUuNTM3MTkxNSwxMS44NDMyNzUgMTUuNDg5MDIxMywxMS43Nzk4NjY3IDE1LjQyMTYxNywxMS43MDY2NTgzIEMxNS4zNTQyMTI4LDExLjYzNTM3NSAxNS4yNjE3MzA1LDExLjUzMjM1ODMgMTUuMjEzNjE3LDExLjQ4MDkwODMgQzE1LjExMzQ3NTIsMTEuMzcwMDE2NyAxNS4wODI2NjY3LDExLjM2MjA4MzMgMTUuNTU2NDI1NSwxMS41NjYwMTY3IEwxNS44ODk1ODg3LDExLjcwODU4MzMgTDE2LjI2OTA0OTYsMTEuNTY2MDE2NyBDMTYuNDc3MDQ5NiwxMS40ODY4NTgzIDE2LjY1NjExMzUsMTEuNDE3NTU4MyAxNi42NjU3NTg5LDExLjQxMTYwODMgQzE2LjY3NTQwNDMsMTEuNDA1NjU4MzMgMTYuNTk0NDk2NSwxMS4zMTQ1NDE3IDE2LjQ4Mjc4MDEsMTEuMjA3NjE2NyBMMTYuMjgyNDk2NSwxMS4wMTM2IEwxNi42MjMzNzU5LDExLjEzMDQ0MTcgTDE2Ljk2NjE4NDQsMTEuMjQ1MjQxNyBMMTcuODY1NjQ1NCwxMC45MTQ2MDgzIEMxOC4zNjI0OTY1LDEwLjczMjQzMzMgMTguODE1MDkyMiwxMC41NjYwNjY3IDE4Ljg3MjkwNzgsMTAuNTQ2MjkxNyBDMTguOTMwNjY2NywxMC41Mjg0NDE3IDE4Ljk4NDU2NzQsMTAuNTA0NyAxOC45OTQyMTI4LDEwLjQ5ODc1IEMxOS4wMTU0MzI2LDEwLjQ3Njk5MTcgMTguMDYwMTQxOCwxMC4wNDUzMjUgMTcuMjEwNzgwMSw5LjY5Mjg3NSBDMTYuMjMyNDUzOSw5LjI4NjkzMzMzIDE0LjgxMjk5MjksOC43NjQyMDgzMyAxNC4yMzEzNzU5LDguNTk1ODU4MzMgQzE0LjExMDA3MDksOC41NjIyIDE0LjAwMjIxMjgsOC41MjY1NTgzMyAxMy45OTQ0OTY1LDguNTE4NjgzMzMgQzEzLjk4Njc4MDEsOC41MDg3NjY2NyAxNC4xOTY3MDkyLDguMTg5OTc1IDE0LjQ2MjQ2ODEsNy44MDU4NSBDMTQuNzI2MzU0Niw3LjQyMTY2NjY3IDE0Ljk0NCw3LjEwNDg1ODMzIDE0Ljk0NCw3LjEwMjg3NSBDMTQuOTQ0LDcuMDk4OTA4MzMgMTQuODg0MjU1Myw3LjA4OTA1IDE0LjgwOTEzNDgsNy4wODMxIEMxNC43MzYsNy4wNzUxNjY2NyAxNC42NzQzMjYyLDcuMDYzMjY2NjcgMTQuNjc0MzI2Miw3LjA1NzMxNjY3IEMxNC42NzQzMjYyLDcuMDUxNDI1IDE0Ljk2MTMwNSw2LjYyMzcyNSAxNS4zMTE4Mjk4LDYuMTA4ODc1IEMxNS42NjIzNTQ2LDUuNTk0MDI1IDE1Ljk1MTI2MjQsNS4xNjYzMjUgMTUuOTUzMTkxNSw1LjE1NjQ2NjY3IEMxNS45NTMxOTE1LDUuMTQ4NLine truncated
'WGET': 'https://gcore.jsdelivr.net/gh/simple-icons/simple-icons@develop/icons/gnu.svg',
'CURL': 'https://gcore.jsdelivr.net/gh/simple-icons/simple-icons@develop/icons/curl.svg',
'IDM': 'https://gcore.jsdelivr.net/gh/simple-icons/simple-icons@develop/icons/internetexplorer.svg',
'FDM': 'Download',
'POWERSHELL': 'Setting'
} }
return clientConfig[type]?.logo || null return logoMap[type] || 'Download'
} }
// 获取客户端显示名称 // 判断是否为 Element Plus 图标
const getClientDisplayName = (type) => { const isElementIcon = (logo) => {
return clientConfig[type]?.displayName || type const elementIcons = ['Download', 'Setting']
return elementIcons.includes(logo)
} }
// 获取客户端描述 // 获取客户端图标颜色
const getClientDescription = (type) => { const getClientIconColor = (type) => {
return clientConfig[type]?.description || '下载工具' const colorMap = {
'MOTRIX': '#409EFF',
'BITCOMET': '#67C23A',
'FDM': '#E6A23C',
'POWERSHELL': '#F56C6C'
}
return colorMap[type] || '#666'
} }
// 获取客户端标签类型 // 客户端下载链接映射
const getClientTagType = (type) => {
return clientConfig[type]?.tagType || 'info'
}
// 获取客户端是否支持Cookie
const getClientSupportsCookie = (type) => {
return clientConfig[type]?.supportsCookie !== false
}
// 获取客户端下载链接
const getClientDownloadUrl = (type) => { const getClientDownloadUrl = (type) => {
return clientConfig[type]?.downloadUrl || '#' const downloadUrls = {
} 'ARIA2': 'https://aria2.github.io/',
'MOTRIX': 'https://motrix.app/',
// 判断是否应该显示下载客户端按钮 'BITCOMET': 'https://www.bitcomet.com/',
const shouldShowDownloadButton = (type) => { 'THUNDER': 'https://www.xunlei.com/',
const os = getOSInfo() 'WGET': 'https://www.gnu.org/software/wget/',
switch (type) { 'CURL': 'https://curl.se/download.html',
case 'CURL': 'IDM': 'https://www.internetdownloadmanager.com/download.html',
// cURL 在 Windows 上可能需要安装 'FDM': 'https://www.freedownloadmanager.org/',
return os === 'windows' 'POWERSHELL': 'https://docs.microsoft.com/en-us/powershell/'
case 'ARIA2':
// Aria2 需要手动安装
return true
case 'THUNDER':
// 迅雷主要在 Windows 上使用
return os === 'windows'
default:
return false
} }
return downloadUrls[type] || '#'
} }
// 获取操作系统信息 // 获取操作系统信息
@@ -317,17 +304,200 @@ export default {
if (userAgent.includes('windows')) return 'windows' if (userAgent.includes('windows')) return 'windows'
if (userAgent.includes('mac')) return 'mac' if (userAgent.includes('mac')) return 'mac'
if (userAgent.includes('linux')) return 'linux' if (userAgent.includes('linux')) return 'linux'
if (userAgent.includes('android')) return 'android'
if (userAgent.includes('ios')) return 'ios'
return 'unknown' return 'unknown'
} }
// 获取设备类型
const getDeviceType = () => {
const userAgent = navigator.userAgent.toLowerCase()
if (/mobile|android|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent)) {
return 'mobile'
}
return 'desktop'
}
// 检查是否为移动端
const isMobile = () => {
return getDeviceType() === 'mobile'
}
// 检测客户端是否已安装(改进版本,避免自动打开应用)
const detectClient = async (type) => {
try {
const os = getOSInfo()
const deviceType = getDeviceType()
// 移动端不支持大部分下载器
if (deviceType === 'mobile') {
switch (type) {
case 'CURL':
// 移动端可能支持 curl(通过终端应用)
return os === 'android' || os === 'ios'
default:
return false
}
}
switch (type) {
case 'IDM':
// IDM 只在 Windows 桌面端可用
if (os !== 'windows' || deviceType !== 'desktop') return false
return await testProtocol('idm://')
case 'THUNDER':
// 迅雷主要在 Windows 桌面端使用
if (os !== 'windows' || deviceType !== 'desktop') return false
return await testProtocol('thunder://')
case 'BITCOMET':
// 比特彗星主要在 Windows 桌面端使用
if (os !== 'windows' || deviceType !== 'desktop') return false
return await testProtocol('bc://')
case 'MOTRIX':
// Motrix 跨平台桌面端 - 使用保守检测,避免自动打开
if (deviceType !== 'desktop') return false
// 对于 Motrix,我们假设用户可能已安装,但不进行实际检测
// 这样可以避免自动打开应用的问题
return false // 改为 false,让用户手动选择
case 'FDM':
// FDM 主要在 Windows 桌面端使用
if (os !== 'windows' || deviceType !== 'desktop') return false
return await testProtocol('fdm://')
case 'WGET':
// wget 在 Linux/Mac 桌面端通常已安装
return (os === 'linux' || os === 'mac') && deviceType === 'desktop'
case 'CURL':
// curl 在现代桌面系统上通常已安装
return deviceType === 'desktop'
case 'ARIA2':
// aria2 需要手动安装,无法检测
return false
case 'POWERSHELL':
// PowerShell 在 Windows 10+ 桌面端通常可用
return os === 'windows' && deviceType === 'desktop'
default:
return false
}
} catch (error) {
console.warn(`检测客户端 ${type} 失败:`, error)
return false
}
}
// 测试协议是否可用(改进版本,避免自动打开应用)
const testProtocol = (protocol) => {
return new Promise((resolve) => {
// 对于某些协议,我们使用更安全的方式检测
// 而不是直接尝试打开应用
try {
// 创建一个临时的 a 标签来测试协议
const link = document.createElement('a')
link.href = protocol + 'test'
link.style.display = 'none'
// 检查协议是否被识别
const isProtocolSupported = link.protocol === protocol.slice(0, -1) // 移除末尾的 ':'
if (isProtocolSupported) {
// 如果协议被识别,我们假设应用可能已安装
// 但不实际尝试打开它
resolve(true)
} else {
resolve(false)
}
} catch (error) {
// 如果检测失败,返回 false
resolve(false)
}
})
}
// 检测所有客户端(改进版本,避免自动打开应用)
const detectAllClients = async () => {
const clientTypes = ['ARIA2', 'MOTRIX', 'BITCOMET', 'THUNDER', 'WGET', 'CURL', 'IDM', 'FDM', 'POWERSHELL']
// 为了避免自动打开应用,我们使用更保守的检测方式
// 只检测那些不会触发外部应用启动的客户端
const safeDetectionTypes = ['WGET', 'CURL', 'POWERSHELL']
const detectionPromises = safeDetectionTypes.map(async (type) => {
const isInstalled = await detectClient(type)
if (isInstalled) {
installedClients.value.add(type)
}
})
await Promise.all(detectionPromises)
// 对于其他客户端,我们假设它们未安装,让用户手动选择
// 这样可以避免自动打开外部应用的问题
}
// 检查客户端是否已安装
const isClientInstalled = (type) => {
return installedClients.value.has(type)
}
// 判断是否应该显示下载按钮
const shouldShowDownloadButton = (type) => {
const os = getOSInfo()
const deviceType = getDeviceType()
// 移动端不显示下载按钮
if (deviceType === 'mobile') {
return false
}
switch (type) {
case 'CURL':
case 'WGET':
// 命令行工具,在 Linux/Mac 上通常已安装
return os === 'windows'
case 'POWERSHELL':
// PowerShell 在 Windows 10+ 上通常可用
return os !== 'windows'
case 'ARIA2':
// Aria2 需要手动安装
return true
case 'IDM':
case 'THUNDER':
case 'BITCOMET':
case 'FDM':
// Windows 专用工具
return os === 'windows'
case 'MOTRIX':
// 跨平台工具
return true
default:
return false
}
}
// 下载客户端
const downloadClient = (type) => {
const url = getClientDownloadUrl(type)
window.open(url, '_blank')
ElMessage.success(`正在跳转到 ${getClientDisplayName(type)} 下载页面`)
}
// 处理图片加载错误 // 处理图片加载错误
const handleImageError = (event) => { const handleImageError = (event) => {
event.target.style.display = 'none' event.target.src = 'el-icon-download'
} }
// 组件挂载时加载数据 // 组件挂载时加载数据和检测客户端
onMounted(() => { onMounted(async () => {
loadDataFromHome() loadDataFromHome()
await detectAllClients()
// 设置默认激活的 tab // 设置默认激活的 tab
if (result.value && result.value.clientLinks) { if (result.value && result.value.clientLinks) {
@@ -353,10 +523,27 @@ export default {
const downloadWithClient = (type, link) => { const downloadWithClient = (type, link) => {
try { try {
const os = getOSInfo() const os = getOSInfo()
const deviceType = getDeviceType()
// 移动端特殊处理
if (deviceType === 'mobile') {
ElMessage.warning('移动端不支持直接启动下载器,请复制命令到终端使用')
return
}
let downloadUrl = link
let successMessage = `正在使用 ${getClientDisplayName(type)} 下载`
switch (type) { switch (type) {
case 'CURL':
case 'ARIA2': case 'ARIA2':
// Aria2 需要特殊处理,尝试复制到剪贴板
copyToClipboard(link)
ElMessage.success('Aria2 命令已复制到剪贴板,请在终端中运行')
return
case 'CURL':
case 'WGET':
case 'POWERSHELL':
// 命令行工具,复制到剪贴板 // 命令行工具,复制到剪贴板
copyToClipboard(link) copyToClipboard(link)
ElMessage.success(`${getClientDisplayName(type)} 命令已复制到剪贴板,请在终端中运行`) ElMessage.success(`${getClientDisplayName(type)} 命令已复制到剪贴板,请在终端中运行`)
@@ -365,29 +552,115 @@ export default {
case 'THUNDER': case 'THUNDER':
// 迅雷协议 // 迅雷协议
if (os !== 'windows') { if (os !== 'windows') {
ElMessage.warning('迅雷主要在 Windows 系统上使用,已复制链接到剪贴板') ElMessage.warning('迅雷仅在 Windows 系统上可用')
copyToClipboard(link) return
}
window.open(link, '_blank')
break
case 'IDM':
// IDM 协议
if (os !== 'windows') {
ElMessage.warning('IDM 仅在 Windows 系统上可用')
return
}
window.open(link, '_blank')
break
case 'BITCOMET':
// 比特彗星协议
if (os !== 'windows') {
ElMessage.warning('比特彗星仅在 Windows 系统上可用')
return
}
window.open(link, '_blank')
break
case 'MOTRIX':
// Motrix JSON 格式处理
try {
const jsonData = JSON.parse(link)
if (jsonData.url) {
downloadUrl = jsonData.url
window.open(downloadUrl, '_blank')
} else {
ElMessage.warning('Motrix 链接格式错误')
return
}
} catch {
// 如果不是 JSON 格式,尝试直接打开
window.open(link, '_blank')
}
break
case 'FDM':
// FDM 协议
if (os !== 'windows') {
ElMessage.warning('FDM 仅在 Windows 系统上可用')
return return
} }
window.open(link, '_blank') window.open(link, '_blank')
ElMessage.success('正在唤起迅雷下载')
break break
default: default:
copyToClipboard(link) // 其他情况,直接下载文件
ElMessage.success('链接已复制到剪贴板') window.open(downloadUrl, '_blank')
successMessage = '正在下载文件'
} }
ElMessage.success(successMessage)
} catch (err) { } catch (err) {
console.error('下载失败:', err) console.error('下载失败:', err)
ElMessage.error('下载失败: ' + err.message) ElMessage.error('下载失败: ' + err.message)
} }
} }
// 下载客户端 // 获取客户端标签类型
const downloadClient = (type) => { const getClientTagType = (type) => {
const url = getClientDownloadUrl(type) const tagTypes = {
window.open(url, '_blank') 'ARIA2': 'warning',
ElMessage.success(`正在跳转到 ${getClientDisplayName(type)} 下载页面`) 'MOTRIX': 'info',
'BITCOMET': 'success',
'THUNDER': 'primary',
'WGET': 'info',
'CURL': 'success',
'IDM': 'danger',
'FDM': 'warning',
'POWERSHELL': 'primary'
}
return tagTypes[type] || 'info'
}
// 获取客户端显示名称
const getClientDisplayName = (type) => {
const displayNames = {
'ARIA2': 'Aria2',
'MOTRIX': 'Motrix',
'BITCOMET': '比特彗星',
'THUNDER': '迅雷',
'WGET': 'wget 命令',
'CURL': 'cURL 命令',
'IDM': 'IDM',
'FDM': 'Free Download Manager',
'POWERSHELL': 'PowerShell'
}
return displayNames[type] || type
}
// 获取客户端描述
const getClientDescription = (type) => {
const descriptions = {
'ARIA2': '多线程下载器',
'MOTRIX': '跨平台下载器',
'BITCOMET': '比特彗星下载器',
'THUNDER': '迅雷下载器',
'WGET': 'Linux 下载工具',
'CURL': '命令行下载工具',
'IDM': 'Windows 下载管理器',
'FDM': '免费下载管理器',
'POWERSHELL': 'Windows PowerShell'
}
return descriptions[type] || '下载工具'
} }
// 格式化文件大小 // 格式化文件大小
@@ -417,15 +690,6 @@ export default {
router.push('/') router.push('/')
} }
// 跳转到认证配置
const goToAuthConfig = () => {
router.push('/')
// 延迟提示用户点击认证按钮
setTimeout(() => {
ElMessage.info('请点击页面右上角的钥匙图标配置认证信息')
}, 500)
}
return { return {
form, form,
result, result,
@@ -440,16 +704,17 @@ export default {
getTextareaRows, getTextareaRows,
goBack, goBack,
getClientLogo, getClientLogo,
isClientInstalled,
downloadClient, downloadClient,
handleImageError, handleImageError,
shouldShowDownloadButton, shouldShowDownloadButton,
getClientSupportsCookie, isElementIcon,
goToAuthConfig getClientIconColor
} }
} }
} }
</script> </script>
<style scoped> <style scoped>
/* 使用 Element Plus 默认主题 */ /* 移除所有自定义样式,使用 Element Plus 默认主题 */
</style> </style>
File diff suppressed because it is too large. Load diff
+1 -2
View File
@@ -1146,11 +1146,10 @@ function parseById(shareLinkInfo, http, logger) {
const isAuthed = res.data.authed || res.data.public; const isAuthed = res.data.authed || res.data.public;
authed.value = isAuthed; authed.value = isAuthed;
// 如果后端认证已失效,清除localStorage中的认证信息 // 如果后端session已失效,清除localStorage
if (!isAuthed && savedAuth === 'true') { if (!isAuthed && savedAuth === 'true') {
localStorage.removeItem('playground_authed'); localStorage.removeItem('playground_authed');
localStorage.removeItem('playground_auth_time'); localStorage.removeItem('playground_auth_time');
localStorage.removeItem('playground_token');
} }
return isAuthed; return isAuthed;
+5 -6
View File
@@ -19,19 +19,19 @@ module.exports = {
port: 6444, port: 6444,
proxy: { proxy: {
'/parser': { '/parser': {
target: 'http://127.0.0.1:6401/', // 请求本地 target: 'http://127.0.0.1:6400/', // 请求本地
ws: false ws: false
}, },
'/v2': { '/v2': {
target: 'http://127.0.0.1:6401/', // 请求本地 target: 'http://127.0.0.1:6400/', // 请求本地
ws: false ws: false
}, },
'/json': { '/json': {
target: 'http://127.0.0.1:6401/', // 请求本地 target: 'http://127.0.0.1:6400/', // 请求本地
ws: false ws: false
}, },
'/d': { '/d': {
target: 'http://127.0.0.1:6401/', // 请求本地 target: 'http://127.0.0.1:6400/', // 请求本地
ws: false ws: false
}, },
} }
@@ -85,8 +85,7 @@ module.exports = {
{ {
source: './node_modules/monaco-editor/min/vs', source: './node_modules/monaco-editor/min/vs',
destination: './nfd-front/js/vs' destination: './nfd-front/js/vs'
}, }
{ source: './nfd-front', destination: '../webroot/nfd-front' }
], ],
archive: [ //然后我们选择dist文件夹将之打包成dist.zip并放在根目录 archive: [ //然后我们选择dist文件夹将之打包成dist.zip并放在根目录
{ {
@@ -1,225 +0,0 @@
package cn.qaiu.lz.common.util;
import cn.qaiu.lz.web.model.AuthParam;
import cn.qaiu.util.AESUtils;
import cn.qaiu.vx.core.util.SharedDataUtil;
import io.vertx.core.json.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* 认证参数编解码工具类
* <p>
* 编码流程: JSON -> AES加密 -> Base64编码 -> URL编码
* 解码流程: URL解码 -> Base64解码 -> AES解密 -> JSON解析
* </p>
*
* @author <a href="https://qaiu.top">QAIU</a>
* @date 2026/2/5
*/
@Slf4j
public class AuthParamCodec {
/**
* 默认加密密钥(16位)
*/
private static final String DEFAULT_ENCRYPT_KEY = "nfd_auth_key2026";
/**
* 配置中的密钥路径
*/
private static final String CONFIG_KEY_PATH = "authEncryptKey";
private AuthParamCodec() {
// 工具类禁止实例化
}
/**
* 获取加密密钥
* 优先从配置文件读取,如果未配置则使用默认密钥
*/
public static String getEncryptKey() {
try {
String configKey = SharedDataUtil.getJsonStringForServerConfig(CONFIG_KEY_PATH);
if (StringUtils.isNotBlank(configKey)) {
return configKey;
}
} catch (Exception e) {
log.debug("从配置读取加密密钥失败,使用默认密钥: {}", e.getMessage());
}
return DEFAULT_ENCRYPT_KEY;
}
/**
* 解码认证参数
* 解码流程: URL解码 -> Base64解码 -> AES解密 -> JSON解析
*
* @param encryptedAuth 加密后的认证参数字符串
* @return AuthParam 对象,解码失败返回 null
*/
public static AuthParam decode(String encryptedAuth) {
return decode(encryptedAuth, getEncryptKey());
}
/**
* 解码认证参数(指定密钥)
*
* @param encryptedAuth 加密后的认证参数字符串
* @param key AES密钥(16位)
* @return AuthParam 对象,解码失败返回 null
*/
public static AuthParam decode(String encryptedAuth, String key) {
if (StringUtils.isBlank(encryptedAuth)) {
return null;
}
try {
// Step 1: URL解码(兼容:有些框架已自动解码,此处避免再次把 '+' 变成空格)
String normalized = encryptedAuth;
if (normalized.contains("%")) {
normalized = URLDecoder.decode(normalized, StandardCharsets.UTF_8);
}
// 兼容 query 参数中 '+' 被还原为空格的情况
normalized = normalized.replace(' ', '+');
log.debug("认证参数规范化结果: {}", normalized);
// Step 2: Base64解码
byte[] base64Decoded = Base64.getDecoder().decode(normalized);
log.debug("Base64解码成功,长度: {}", base64Decoded.length);
// Step 3: AES解密
String jsonStr = AESUtils.decryptByAES(base64Decoded, key);
log.debug("AES解密结果: {}", jsonStr);
// Step 4: JSON解析
JsonObject json = new JsonObject(jsonStr);
AuthParam authParam = new AuthParam(json);
log.info("认证参数解码成功: authType={}", authParam.getAuthType());
return authParam;
} catch (IllegalArgumentException e) {
log.warn("认证参数Base64解码失败: {}", e.getMessage());
} catch (Exception e) {
log.warn("认证参数解码失败: {}", e.getMessage());
}
return null;
}
/**
* 编码认证参数
* 编码流程: JSON -> AES加密 -> Base64编码 -> URL编码
*
* @param authParam 认证参数对象
* @return 加密后的字符串,编码失败返回 null
*/
public static String encode(AuthParam authParam) {
return encode(authParam, getEncryptKey());
}
/**
* 编码认证参数(指定密钥)
*
* @param authParam 认证参数对象
* @param key AES密钥(16位)
* @return 加密后的字符串,编码失败返回 null
*/
public static String encode(AuthParam authParam, String key) {
if (authParam == null || !authParam.hasValidAuth()) {
return null;
}
try {
// Step 1: 转换为JSON
String jsonStr = authParam.toJsonObject().encode();
log.debug("JSON字符串: {}", jsonStr);
// Step 2: AES加密 + Base64编码
String base64Encoded = AESUtils.encryptBase64ByAES(jsonStr, key);
log.debug("AES+Base64编码结果: {}", base64Encoded);
// Step 3: URL编码
String urlEncoded = java.net.URLEncoder.encode(base64Encoded, StandardCharsets.UTF_8);
log.debug("URL编码结果: {}", urlEncoded);
return urlEncoded;
} catch (Exception e) {
log.error("认证参数编码失败: {}", e.getMessage(), e);
}
return null;
}
/**
* 编码认证参数(从JsonObject)
*
* @param json 认证参数JSON对象
* @return 加密后的字符串
*/
public static String encode(JsonObject json) {
return encode(new AuthParam(json));
}
/**
* 编码认证参数(从JSON字符串)
*
* @param jsonStr 认证参数JSON字符串
* @return 加密后的字符串
*/
public static String encodeFromJsonString(String jsonStr) {
if (StringUtils.isBlank(jsonStr)) {
return null;
}
try {
JsonObject json = new JsonObject(jsonStr);
return encode(new AuthParam(json));
} catch (Exception e) {
log.error("JSON解析失败: {}", e.getMessage());
return null;
}
}
/**
* 验证加密的认证参数是否有效
*
* @param encryptedAuth 加密后的认证参数
* @return true 如果可以成功解码
*/
public static boolean isValid(String encryptedAuth) {
return decode(encryptedAuth) != null;
}
/**
* 快速构建并编码认证参数
*
* @param authType 认证类型
* @param token token/cookie/credential
* @return 加密后的字符串
*/
public static String quickEncode(String authType, String token) {
AuthParam authParam = AuthParam.builder()
.authType(authType)
.token(token)
.build();
return encode(authParam);
}
/**
* 快速构建并编码用户名密码认证
*
* @param username 用户名
* @param password 密码
* @return 加密后的字符串
*/
public static String quickEncodePassword(String username, String password) {
AuthParam authParam = AuthParam.builder()
.authType("password")
.username(username)
.password(password)
.build();
return encode(authParam);
}
}
@@ -8,8 +8,6 @@ import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.LocalMap; import io.vertx.core.shareddata.LocalMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -20,7 +18,6 @@ import java.nio.charset.StandardCharsets;
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
* Create at 2024/9/13 * Create at 2024/9/13
*/ */
@Slf4j
public class URLParamUtil { public class URLParamUtil {
/** /**
@@ -63,14 +60,12 @@ public class URLParamUtil {
} }
} }
// 拼接被截断的URL参数,忽略pwd、auth等参数 // 拼接被截断的URL参数,忽略pwd参数
StringBuilder urlBuilder = new StringBuilder(decodedUrl); StringBuilder urlBuilder = new StringBuilder(decodedUrl);
boolean firstParam = !decodedUrl.contains("?"); boolean firstParam = !decodedUrl.contains("?");
for (String paramName : params.names()) { for (String paramName : params.names()) {
// 忽略 "url", "pwd", "dirId", "uuid", "auth" 参数(这些参数单独处理,不应拼接到分享URL中) if (!paramName.equals("url") && !paramName.equals("pwd") && !paramName.equals("dirId") && !paramName.equals("uuid")) { // 忽略 "url" 和 "pwd" 参数
if (!paramName.equals("url") && !paramName.equals("pwd") && !paramName.equals("dirId")
&& !paramName.equals("uuid") && !paramName.equals("auth")) {
if (firstParam) { if (firstParam) {
urlBuilder.append("?"); urlBuilder.append("?");
firstParam = false; firstParam = false;
@@ -121,152 +116,4 @@ public class URLParamUtil {
String linkPrefix = SharedDataUtil.getJsonConfig("server").getString("domainName"); String linkPrefix = SharedDataUtil.getJsonConfig("server").getString("domainName");
parserCreate.getShareLinkInfo().getOtherParam().put("domainName", linkPrefix); parserCreate.getShareLinkInfo().getOtherParam().put("domainName", linkPrefix);
} }
/**
* 添加临时认证参数(一次性,不保存到数据库或共享内存)
* 如果提供了临时认证参数,将覆盖后台配置的认证信息
*
* @param parserCreate ParserCreate对象
* @param authType 认证类型
* @param authToken 认证token/用户名/accesstoken/cookie
* @param authPassword 密码(仅用于username_password认证)
* @param authInfo1-5 扩展认证信息(用于custom认证)
*/
public static void addTempAuthParam(ParserCreate parserCreate, String authType,
String authToken, String authPassword,
String authInfo1, String authInfo2, String authInfo3,
String authInfo4, String authInfo5) {
if (StringUtils.isBlank(authType) && StringUtils.isBlank(authToken)) {
// 没有提供临时认证参数,使用后台配置
addParam(parserCreate);
return;
}
// 先添加代理配置和域名配置
LocalMap<Object, Object> localMap = VertxHolder.getVertxInstance().sharedData()
.getLocalMap(ConfigConstant.LOCAL);
String type = parserCreate.getShareLinkInfo().getType();
if (localMap.containsKey(ConfigConstant.PROXY)) {
JsonObject proxy = (JsonObject) localMap.get(ConfigConstant.PROXY);
if (proxy.containsKey(type)) {
parserCreate.getShareLinkInfo().getOtherParam().put(ConfigConstant.PROXY, proxy.getJsonObject(type));
}
}
String linkPrefix = SharedDataUtil.getJsonConfig("server").getString("domainName");
parserCreate.getShareLinkInfo().getOtherParam().put("domainName", linkPrefix);
// 构建临时认证信息
MultiMap tempAuth = MultiMap.caseInsensitiveMultiMap();
if (StringUtils.isNotBlank(authType)) {
tempAuth.set("authType", authType.trim());
}
String authTypeValue = authType != null ? authType : "";
switch (authTypeValue.toLowerCase()) {
case "accesstoken":
case "authorization":
if (StringUtils.isNotBlank(authToken)) {
tempAuth.set("token", authToken.trim());
}
break;
case "cookie":
// cookie 类型需要同时设置 token 和 cookie 字段
// QkTool/UcTool 等从 auths.get("cookie") 获取 cookie 值
if (StringUtils.isNotBlank(authToken)) {
tempAuth.set("token", authToken.trim());
tempAuth.set("cookie", authToken.trim());
}
break;
case "password":
case "username_password":
if (StringUtils.isNotBlank(authToken)) {
tempAuth.set("username", authToken.trim());
tempAuth.set("token", authToken.trim()); // 兼容旧的解析器
}
if (StringUtils.isNotBlank(authPassword)) {
tempAuth.set("password", authPassword.trim());
}
break;
case "custom":
// 自定义认证支持多个扩展字段
if (StringUtils.isNotBlank(authToken)) {
tempAuth.set("token", authToken.trim());
}
if (StringUtils.isNotBlank(authInfo1)) {
parseAndSetAuthInfo(tempAuth, authInfo1);
}
if (StringUtils.isNotBlank(authInfo2)) {
parseAndSetAuthInfo(tempAuth, authInfo2);
}
if (StringUtils.isNotBlank(authInfo3)) {
parseAndSetAuthInfo(tempAuth, authInfo3);
}
if (StringUtils.isNotBlank(authInfo4)) {
parseAndSetAuthInfo(tempAuth, authInfo4);
}
if (StringUtils.isNotBlank(authInfo5)) {
parseAndSetAuthInfo(tempAuth, authInfo5);
}
break;
default:
// 默认处理:将authToken作为token
if (StringUtils.isNotBlank(authToken)) {
tempAuth.set("token", authToken.trim());
}
break;
}
// 设置临时认证信息(覆盖后台配置)
if (!tempAuth.isEmpty()) {
parserCreate.getShareLinkInfo().getOtherParam().put(ConfigConstant.AUTHS, tempAuth);
// 设置标记表示已添加临时认证
parserCreate.getShareLinkInfo().getOtherParam().put("__TEMP_AUTH_ADDED", true);
log.debug("已添加临时认证参数: diskType={}, authType={}", type, authType);
} else {
// 如果没有有效的临时认证参数,回退到使用后台配置
if (localMap.containsKey(ConfigConstant.AUTHS)) {
JsonObject auths = (JsonObject) localMap.get(ConfigConstant.AUTHS);
if (auths.containsKey(type)) {
MultiMap entries = MultiMap.caseInsensitiveMultiMap();
JsonObject jsonObject = auths.getJsonObject(type);
if (jsonObject != null) {
jsonObject.forEach(entity -> {
if (entity == null || entity.getValue() == null) {
return;
}
if (StringUtils.isEmpty(entity.getKey()) || StringUtils.isEmpty(entity.getValue().toString())) {
return;
}
entries.set(StringUtils.trim(entity.getKey()), StringUtils.trim(entity.getValue().toString()));
});
}
parserCreate.getShareLinkInfo().getOtherParam().put(ConfigConstant.AUTHS, entries);
}
}
}
}
/**
* 解析并设置认证信息(格式: key:value)
*/
private static void parseAndSetAuthInfo(MultiMap authMap, String authInfo) {
if (StringUtils.isBlank(authInfo)) {
return;
}
String[] parts = authInfo.split(":", 2);
if (parts.length == 2) {
String key = parts[0].trim();
String value = parts[1].trim();
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
authMap.set(key, value);
}
}
}
} }
@@ -4,10 +4,6 @@ import io.vertx.core.json.JsonObject;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.security.SecureRandom;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* JS演练场配置 * JS演练场配置
* *
@@ -16,21 +12,11 @@ import java.util.concurrent.ConcurrentHashMap;
@Data @Data
@Slf4j @Slf4j
public class PlaygroundConfig { public class PlaygroundConfig {
/** Token有效期:24小时 */
private static final long TOKEN_EXPIRY_MS = 24 * 60 * 60 * 1000L;
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
/** /**
* 单例实例 * 单例实例
*/ */
private static PlaygroundConfig instance; private static PlaygroundConfig instance;
/**
* 已颁发的认证Token及其创建时间
*/
private final Map<String, Long> validTokens = new ConcurrentHashMap<>();
/** /**
* 是否启用演练场 * 是否启用演练场
@@ -55,39 +41,6 @@ public class PlaygroundConfig {
*/ */
private PlaygroundConfig() { private PlaygroundConfig() {
} }
/**
* 生成并存储一个新的认证Token,同时清理过期Token
*/
public String generateToken() {
// 清理过期Token,防止Map无限增长
long now = System.currentTimeMillis();
validTokens.entrySet().removeIf(e -> now - e.getValue() > TOKEN_EXPIRY_MS);
// 使用SecureRandom生成32字节的密码学安全Token
byte[] bytes = new byte[32];
SECURE_RANDOM.nextBytes(bytes);
StringBuilder sb = new StringBuilder(64);
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
String token = sb.toString();
validTokens.put(token, now);
return token;
}
/**
* 校验Token是否合法且未过期
*/
public boolean validateToken(String token) {
if (token == null || token.isEmpty()) return false;
Long createdAt = validTokens.get(token);
if (createdAt == null) return false;
if (System.currentTimeMillis() - createdAt > TOKEN_EXPIRY_MS) {
validTokens.remove(token);
return false;
}
return true;
}
/** /**
* 获取单例实例 * 获取单例实例
@@ -4,9 +4,7 @@ package cn.qaiu.lz.web.controller;
import cn.qaiu.entity.FileInfo; import cn.qaiu.entity.FileInfo;
import cn.qaiu.entity.ShareLinkInfo; import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.lz.common.cache.CacheManager; import cn.qaiu.lz.common.cache.CacheManager;
import cn.qaiu.lz.common.util.AuthParamCodec;
import cn.qaiu.lz.common.util.URLParamUtil; import cn.qaiu.lz.common.util.URLParamUtil;
import cn.qaiu.lz.web.model.AuthParam;
import cn.qaiu.lz.web.model.CacheLinkInfo; import cn.qaiu.lz.web.model.CacheLinkInfo;
import cn.qaiu.lz.web.model.ClientLinkResp; import cn.qaiu.lz.web.model.ClientLinkResp;
import cn.qaiu.lz.web.model.LinkInfoResp; import cn.qaiu.lz.web.model.LinkInfoResp;
@@ -28,7 +26,6 @@ import io.vertx.core.Promise;
import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse; import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -53,18 +50,15 @@ public class ParserApi {
private final ServerApi serverApi = new ServerApi(); private final ServerApi serverApi = new ServerApi();
@RouteMapping(value = "/linkInfo", method = RouteMethod.GET) @RouteMapping(value = "/linkInfo", method = RouteMethod.GET)
public Future<LinkInfoResp> parse(HttpServerRequest request, String pwd, String auth) { public Future<LinkInfoResp> parse(HttpServerRequest request, String pwd) {
Promise<LinkInfoResp> promise = Promise.promise(); Promise<LinkInfoResp> promise = Promise.promise();
String url = URLParamUtil.parserParams(request); String url = URLParamUtil.parserParams(request);
ParserCreate parserCreate = ParserCreate.fromShareUrl(url).setShareLinkInfoPwd(pwd); ParserCreate parserCreate = ParserCreate.fromShareUrl(url).setShareLinkInfoPwd(pwd);
ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo(); ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo();
// 构建链接信息响应,如果有 auth 参数则附加到链接中
String authSuffix = (auth != null && !auth.isEmpty()) ? "&auth=" + auth : "";
LinkInfoResp build = LinkInfoResp.builder() LinkInfoResp build = LinkInfoResp.builder()
.downLink(getDownLink(parserCreate, false) + authSuffix) .downLink(getDownLink(parserCreate, false))
.apiLink(getDownLink(parserCreate, true) + authSuffix) .apiLink(getDownLink(parserCreate, true))
.viewLink(getViewLink(parserCreate) + authSuffix) .viewLink(getViewLink(parserCreate))
.shareLinkInfo(shareLinkInfo).build(); .shareLinkInfo(shareLinkInfo).build();
// 解析次数统计 // 解析次数统计
shareLinkInfo.getOtherParam().put("UA",request.headers().get("user-agent")); shareLinkInfo.getOtherParam().put("UA",request.headers().get("user-agent"));
@@ -220,7 +214,7 @@ public class ParserApi {
} }
String previewURL = SharedDataUtil.getJsonStringForServerConfig("previewURL"); String previewURL = SharedDataUtil.getJsonStringForServerConfig("previewURL");
new ServerApi().parseJson(request, pwd, null).onSuccess(res -> { new ServerApi().parseJson(request, pwd).onSuccess(res -> {
redirect(response, previewURL, res); redirect(response, previewURL, res);
}).onFailure(e -> { }).onFailure(e -> {
ResponseUtil.fireJsonResultResponse(response, JsonResult.error(e.toString())); ResponseUtil.fireJsonResultResponse(response, JsonResult.error(e.toString()));
@@ -258,11 +252,10 @@ public class ParserApi {
* *
* @param request HTTP请求 * @param request HTTP请求
* @param pwd 提取码 * @param pwd 提取码
* @param auth 加密的认证参数
* @return 客户端下载链接响应 * @return 客户端下载链接响应
*/ */
@RouteMapping(value = "/clientLinks", method = RouteMethod.GET) @RouteMapping(value = "/clientLinks", method = RouteMethod.GET)
public Future<ClientLinkResp> getClientLinks(HttpServerRequest request, String pwd, String auth) { public Future<ClientLinkResp> getClientLinks(HttpServerRequest request, String pwd) {
Promise<ClientLinkResp> promise = Promise.promise(); Promise<ClientLinkResp> promise = Promise.promise();
try { try {
@@ -270,23 +263,6 @@ public class ParserApi {
ParserCreate parserCreate = ParserCreate.fromShareUrl(shareUrl).setShareLinkInfoPwd(pwd); ParserCreate parserCreate = ParserCreate.fromShareUrl(shareUrl).setShareLinkInfoPwd(pwd);
ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo(); ShareLinkInfo shareLinkInfo = parserCreate.getShareLinkInfo();
// 处理认证参数
if (auth != null && !auth.isEmpty()) {
AuthParam authParam = AuthParamCodec.decode(auth);
if (authParam != null && authParam.hasValidAuth()) {
URLParamUtil.addTempAuthParam(parserCreate,
authParam.getAuthType(),
authParam.getPrimaryCredential(),
authParam.getPassword(),
authParam.getExt1(),
authParam.getExt2(),
authParam.getExt3(),
authParam.getExt4(),
authParam.getExt5());
log.debug("客户端链接API: 已解码认证参数 authType={}", authParam.getAuthType());
}
}
// 使用默认方法解析并生成客户端链接 // 使用默认方法解析并生成客户端链接
parserCreate.createTool().parseWithClientLinks() parserCreate.createTool().parseWithClientLinks()
.onSuccess(clientLinks -> { .onSuccess(clientLinks -> {
@@ -369,10 +345,6 @@ public class ParserApi {
String directLink = (String) shareLinkInfo.getOtherParam().get("downloadUrl"); String directLink = (String) shareLinkInfo.getOtherParam().get("downloadUrl");
Map<String, String> supportedClients = buildSupportedClientsMap(); Map<String, String> supportedClients = buildSupportedClientsMap();
FileInfo fileInfo = extractFileInfo(shareLinkInfo); FileInfo fileInfo = extractFileInfo(shareLinkInfo);
String panType = shareLinkInfo.getType().toUpperCase();
// 判断是否需要客户端下载和认证需求
PanRequirementInfo requirementInfo = getPanRequirementInfo(panType);
return ClientLinkResp.builder() return ClientLinkResp.builder()
.success(true) .success(true)
@@ -382,66 +354,9 @@ public class ParserApi {
.clientLinks(clientLinks) .clientLinks(clientLinks)
.supportedClients(supportedClients) .supportedClients(supportedClients)
.parserInfo(shareLinkInfo.getPanName() + " - " + shareLinkInfo.getType()) .parserInfo(shareLinkInfo.getPanName() + " - " + shareLinkInfo.getType())
.panType(panType)
.requiresClient(requirementInfo.requiresClient)
.authRequirement(requirementInfo.authRequirement)
.authHint(requirementInfo.authHint)
.build(); .build();
} }
/**
* 网盘需求信息内部类
*/
private static class PanRequirementInfo {
boolean requiresClient;
String authRequirement;
String authHint;
PanRequirementInfo(boolean requiresClient, String authRequirement, String authHint) {
this.requiresClient = requiresClient;
this.authRequirement = authRequirement;
this.authHint = authHint;
}
}
/**
* 获取网盘需求信息
*
* @param panType 网盘类型代码(大写)
* @return 网盘需求信息
*/
private PanRequirementInfo getPanRequirementInfo(String panType) {
// 需要使用客户端下载的网盘类型(直链需要特殊头部,浏览器无法直接下载)
boolean requiresClient = switch (panType) {
case "UC", "QK", "PCX", "COW" -> true;
default -> false;
};
// 认证需求判断
String authRequirement;
String authHint;
switch (panType) {
case "UC", "QK":
authRequirement = "required";
authHint = "此网盘必须配置认证信息(Cookie/Token)才能正常解析和下载";
break;
case "FJ":
authRequirement = "optional";
authHint = "小飞机网盘大文件(>100MB)需要配置认证信息";
break;
case "IZ":
authRequirement = "optional";
authHint = "蓝奏优享大文件需要配置认证信息";
break;
default:
authRequirement = "none";
authHint = null;
break;
}
return new PanRequirementInfo(requiresClient, authRequirement, authHint);
}
/** /**
* 构建支持的客户端类型映射 * 构建支持的客户端类型映射
* *
@@ -478,46 +393,4 @@ public class ParserApi {
ClientLinkType type = ClientLinkType.valueOf(clientType.toUpperCase()); ClientLinkType type = ClientLinkType.valueOf(clientType.toUpperCase());
return clientLinks.get(type); return clientLinks.get(type);
} }
// ========== 捐赠账号 API ==========
/**
* 捐赠网盘账号
*/
@RouteMapping(value = "/donateAccount", method = RouteMethod.POST)
public Future<JsonObject> donateAccount(RoutingContext ctx) {
JsonObject body = ctx.body().asJsonObject();
if (body == null || StringUtils.isBlank(body.getString("panType"))
|| StringUtils.isBlank(body.getString("authType"))) {
return Future.succeededFuture(JsonResult.error("panType and authType are required").toJsonObject());
}
String ip = ctx.request().remoteAddress().host();
body.put("ip", ip);
return dbService.saveDonatedAccount(body);
}
/**
* 获取各网盘捐赠账号数量
*/
@RouteMapping(value = "/donateAccountCounts", method = RouteMethod.GET)
public Future<JsonObject> getDonateAccountCounts() {
return dbService.getDonatedAccountCounts();
}
/**
* 随机获取指定网盘类型的捐赠账号(内部使用,返回加密后的 auth 参数)
*/
@RouteMapping(value = "/randomAuth", method = RouteMethod.GET)
public Future<JsonObject> getRandomAuth(String 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;
});
}
} }
@@ -21,6 +21,7 @@ import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse; import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.Session;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -46,6 +47,7 @@ public class PlaygroundApi {
private static final int MAX_PARSER_COUNT = 100; private static final int MAX_PARSER_COUNT = 100;
private static final int MAX_CODE_LENGTH = 128 * 1024; // 128KB 代码长度限制 private static final int MAX_CODE_LENGTH = 128 * 1024; // 128KB 代码长度限制
private static final String SESSION_AUTH_KEY = "playgroundAuthed";
private final DbService dbService = AsyncServiceUtil.getAsyncServiceInstance(DbService.class); private final DbService dbService = AsyncServiceUtil.getAsyncServiceInstance(DbService.class);
/** /**
@@ -72,14 +74,14 @@ public class PlaygroundApi {
return true; return true;
} }
// 检查 Authorization: Bearer <token> 请求头 // 否则检查Session中的认证状态
String authHeader = ctx.request().getHeader("Authorization"); Session session = ctx.session();
if (authHeader != null && authHeader.startsWith("Bearer ") && authHeader.length() > 7) { if (session == null) {
String token = authHeader.substring(7).trim(); return false;
return config.validateToken(token);
} }
return false; Boolean authed = session.get(SESSION_AUTH_KEY);
return authed != null && authed;
} }
/** /**
@@ -114,8 +116,12 @@ public class PlaygroundApi {
try { try {
PlaygroundConfig config = PlaygroundConfig.getInstance(); PlaygroundConfig config = PlaygroundConfig.getInstance();
// 如果是公开模式,直接成功(不需要token) // 如果是公开模式,直接成功
if (config.isPublic()) { if (config.isPublic()) {
Session session = ctx.session();
if (session != null) {
session.put(SESSION_AUTH_KEY, true);
}
promise.complete(JsonResult.success("公开模式,无需密码").toJsonObject()); promise.complete(JsonResult.success("公开模式,无需密码").toJsonObject());
return promise.future(); return promise.future();
} }
@@ -131,9 +137,11 @@ public class PlaygroundApi {
// 验证密码 // 验证密码
if (config.getPassword().equals(password)) { if (config.getPassword().equals(password)) {
String token = config.generateToken(); Session session = ctx.session();
JsonObject tokenData = new JsonObject().put("token", token); if (session != null) {
promise.complete(JsonResult.data(tokenData).toJsonObject()); session.put(SESSION_AUTH_KEY, true);
}
promise.complete(JsonResult.success("登录成功").toJsonObject());
} else { } else {
promise.complete(JsonResult.error("密码错误").toJsonObject()); promise.complete(JsonResult.error("密码错误").toJsonObject());
} }
@@ -1,11 +1,8 @@
package cn.qaiu.lz.web.controller; package cn.qaiu.lz.web.controller;
import cn.qaiu.lz.common.util.AuthParamCodec;
import cn.qaiu.lz.common.util.URLParamUtil; import cn.qaiu.lz.common.util.URLParamUtil;
import cn.qaiu.lz.web.model.AuthParam;
import cn.qaiu.lz.web.model.CacheLinkInfo; import cn.qaiu.lz.web.model.CacheLinkInfo;
import cn.qaiu.lz.web.service.CacheService; import cn.qaiu.lz.web.service.CacheService;
import cn.qaiu.lz.web.service.DbService;
import cn.qaiu.vx.core.annotaions.RouteHandler; import cn.qaiu.vx.core.annotaions.RouteHandler;
import cn.qaiu.vx.core.annotaions.RouteMapping; import cn.qaiu.vx.core.annotaions.RouteMapping;
import cn.qaiu.vx.core.enums.RouteMethod; import cn.qaiu.vx.core.enums.RouteMethod;
@@ -30,34 +27,25 @@ import lombok.extern.slf4j.Slf4j;
public class ServerApi { public class ServerApi {
private final CacheService cacheService = AsyncServiceUtil.getAsyncServiceInstance(CacheService.class); private final CacheService cacheService = AsyncServiceUtil.getAsyncServiceInstance(CacheService.class);
private final DbService dbService = AsyncServiceUtil.getAsyncServiceInstance(DbService.class);
@RouteMapping(value = "/parser", method = RouteMethod.GET, order = 1) @RouteMapping(value = "/parser", method = RouteMethod.GET, order = 1)
public Future<Void> parse(HttpServerResponse response, HttpServerRequest request, RoutingContext rcx, String pwd, String auth) { public Future<Void> parse(HttpServerResponse response, HttpServerRequest request, RoutingContext rcx, String pwd) {
Promise<Void> promise = Promise.promise(); Promise<Void> promise = Promise.promise();
String url = URLParamUtil.parserParams(request); String url = URLParamUtil.parserParams(request);
// 构建 otherParam,包含 UA 和解码后的认证参数 cacheService.getCachedByShareUrlAndPwd(url, pwd, JsonObject.of("UA",request.headers().get("user-agent")))
JsonObject otherParam = buildOtherParam(request, auth);
cacheService.getCachedByShareUrlAndPwd(url, pwd, otherParam)
.onSuccess(res -> ResponseUtil.redirect( .onSuccess(res -> ResponseUtil.redirect(
response.putHeader("nfd-cache-hit", res.getCacheHit().toString()) response.putHeader("nfd-cache-hit", res.getCacheHit().toString())
.putHeader("nfd-cache-expires", res.getExpires()), .putHeader("nfd-cache-expires", res.getExpires()),
res.getDirectLink(), promise)) res.getDirectLink(), promise))
.onFailure(t -> { .onFailure(t -> promise.fail(t.fillInStackTrace()));
recordDonatedAccountFailureIfNeeded(otherParam, t);
promise.fail(t.fillInStackTrace());
});
return promise.future(); return promise.future();
} }
@RouteMapping(value = "/json/parser", method = RouteMethod.GET, order = 1) @RouteMapping(value = "/json/parser", method = RouteMethod.GET, order = 1)
public Future<CacheLinkInfo> parseJson(HttpServerRequest request, String pwd, String auth) { public Future<CacheLinkInfo> parseJson(HttpServerRequest request, String pwd) {
String url = URLParamUtil.parserParams(request); String url = URLParamUtil.parserParams(request);
JsonObject otherParam = buildOtherParam(request, auth); return cacheService.getCachedByShareUrlAndPwd(url, pwd, JsonObject.of("UA",request.headers().get("user-agent")));
return cacheService.getCachedByShareUrlAndPwd(url, pwd, otherParam)
.onFailure(t -> recordDonatedAccountFailureIfNeeded(otherParam, t));
} }
@RouteMapping(value = "/json/:type/:key", method = RouteMethod.GET) @RouteMapping(value = "/json/:type/:key", method = RouteMethod.GET)
@@ -89,71 +77,4 @@ public class ServerApi {
return promise.future(); return promise.future();
} }
/**
* 构建 otherParam,包含 UA 和解码后的认证参数
*
* @param request HTTP请求
* @param auth 加密的认证参数
* @return JsonObject
*/
private JsonObject buildOtherParam(HttpServerRequest request, String auth) {
JsonObject otherParam = JsonObject.of("UA", request.headers().get("user-agent"));
// 解码认证参数
if (auth != null && !auth.isEmpty()) {
AuthParam authParam = AuthParamCodec.decode(auth);
if (authParam != null && authParam.hasValidAuth()) {
// 将认证参数放入 otherParam
otherParam.put("authType", authParam.getAuthType());
otherParam.put("authToken", authParam.getPrimaryCredential());
otherParam.put("authPassword", authParam.getPassword());
otherParam.put("authInfo1", authParam.getExt1());
otherParam.put("authInfo2", authParam.getExt2());
otherParam.put("authInfo3", authParam.getExt3());
otherParam.put("authInfo4", authParam.getExt4());
otherParam.put("authInfo5", authParam.getExt5());
if (authParam.getDonatedAccountToken() != null && !authParam.getDonatedAccountToken().isBlank()) {
otherParam.put("donatedAccountToken", authParam.getDonatedAccountToken());
}
log.debug("已解码认证参数: authType={}", authParam.getAuthType());
}
}
return otherParam;
}
private void recordDonatedAccountFailureIfNeeded(JsonObject otherParam, Throwable cause) {
if (!isLikelyAuthFailure(cause)) {
return;
}
String donatedAccountToken = otherParam.getString("donatedAccountToken");
if (donatedAccountToken == null || donatedAccountToken.isBlank()) {
return;
}
dbService.recordDonatedAccountFailureByToken(donatedAccountToken)
.onFailure(e -> log.warn("记录捐赠账号失败次数失败", e));
}
private boolean isLikelyAuthFailure(Throwable cause) {
if (cause == null) {
return false;
}
String msg = cause.getMessage();
if (msg == null || msg.isBlank()) {
return false;
}
String lower = msg.toLowerCase();
return lower.contains("auth")
|| lower.contains("token")
|| lower.contains("cookie")
|| lower.contains("password")
|| lower.contains("credential")
|| lower.contains("401")
|| lower.contains("403")
|| lower.contains("unauthorized")
|| lower.contains("forbidden")
|| lower.contains("expired")
|| lower.contains("登录")
|| lower.contains("认证");
}
} }
@@ -1,173 +0,0 @@
package cn.qaiu.lz.web.model;
import cn.qaiu.lz.common.ToJson;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 认证参数模型
* 用于接口传参时携带临时认证信息
* <p>
* 传参格式: auth=URL_ENCODE(BASE64(AES_ENCRYPT(JSON)))
* JSON格式: {"username":"xxx","password":"xxx","token":"xxx","cookie":"xxx",...}
* </p>
*
* @author <a href="https://qaiu.top">QAIU</a>
* @date 2026/2/5
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DataObject
public class AuthParam implements ToJson {
/**
* 认证类型
* <ul>
* <li>accesstoken - 使用 accessToken 认证</li>
* <li>cookie - 使用 Cookie 认证</li>
* <li>authorization - 使用 Authorization 头认证</li>
* <li>password/username_password - 使用用户名密码认证</li>
* <li>custom - 自定义认证(使用 ext1-ext5 扩展字段)</li>
* </ul>
*/
private String authType;
/**
* 用户名(用于 password/username_password 认证类型)
*/
private String username;
/**
* 密码(用于 password/username_password 认证类型)
*/
private String password;
/**
* Token(accessToken/authorization/通用token)
*/
private String token;
/**
* Cookie 字符串
*/
private String cookie;
/**
* 授权信息(Authorization 头内容)
*/
private String auth;
/**
* 扩展字段1(用于 custom 认证类型)
* 格式: key:value
*/
private String ext1;
/**
* 扩展字段2(用于 custom 认证类型)
* 格式: key:value
*/
private String ext2;
/**
* 扩展字段3(用于 custom 认证类型)
* 格式: key:value
*/
private String ext3;
/**
* 扩展字段4(用于 custom 认证类型)
* 格式: key:value
*/
private String ext4;
/**
* 扩展字段5(用于 custom 认证类型)
* 格式: key:value
*/
private String ext5;
/**
* 捐赠账号失败计数令牌(服务端签发,不可伪造)
*/
private String donatedAccountToken;
/**
* 从 JsonObject 构造
*/
public AuthParam(JsonObject json) {
if (json == null) {
return;
}
this.authType = json.getString("authType");
this.username = json.getString("username");
this.password = json.getString("password");
this.token = json.getString("token");
this.cookie = json.getString("cookie");
this.auth = json.getString("auth");
this.ext1 = json.getString("ext1");
this.ext2 = json.getString("ext2");
this.ext3 = json.getString("ext3");
this.ext4 = json.getString("ext4");
this.ext5 = json.getString("ext5");
this.donatedAccountToken = json.getString("donatedAccountToken");
}
/**
* 转换为 JsonObject
*/
public JsonObject toJsonObject() {
JsonObject json = new JsonObject();
if (authType != null) json.put("authType", authType);
if (username != null) json.put("username", username);
if (password != null) json.put("password", password);
if (token != null) json.put("token", token);
if (cookie != null) json.put("cookie", cookie);
if (auth != null) json.put("auth", auth);
if (ext1 != null) json.put("ext1", ext1);
if (ext2 != null) json.put("ext2", ext2);
if (ext3 != null) json.put("ext3", ext3);
if (ext4 != null) json.put("ext4", ext4);
if (ext5 != null) json.put("ext5", ext5);
if (donatedAccountToken != null) json.put("donatedAccountToken", donatedAccountToken);
return json;
}
/**
* 检查是否有有效的认证信息
*/
public boolean hasValidAuth() {
return authType != null ||
username != null ||
password != null ||
token != null ||
cookie != null ||
auth != null;
}
/**
* 获取主要的认证凭证(token/cookie/auth)
* 优先级: token > cookie > auth > username
*/
public String getPrimaryCredential() {
if (token != null && !token.isEmpty()) {
return token;
}
if (cookie != null && !cookie.isEmpty()) {
return cookie;
}
if (auth != null && !auth.isEmpty()) {
return auth;
}
if (username != null && !username.isEmpty()) {
return username;
}
return null;
}
}
@@ -12,9 +12,6 @@ import io.vertx.core.json.jackson.DatabindCodec;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;
/** /**
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
* Create at 2024/9/11 16:06 * Create at 2024/9/11 16:06
@@ -55,16 +52,6 @@ public class CacheLinkInfo implements ToJson {
private Long expiration; private Long expiration;
private FileInfo fileInfo; private FileInfo fileInfo;
/**
* 其他参数,包含:
* - downloadHeaders: 下载请求头 Map<String, String>
* - aria2Command: aria2 命令行命令
* - aria2JsonRpc: aria2 JSON-RPC 请求体
* - curlCommand: curl 命令
*/
@TableGenIgnore
private Map<String, Object> otherParam;
// 使用 JsonObject 构造 // 使用 JsonObject 构造
@@ -87,15 +74,6 @@ public class CacheLinkInfo implements ToJson {
this.setFileInfo(mapper.convertValue(json.getJsonObject("fileInfo"), FileInfo.class)); this.setFileInfo(mapper.convertValue(json.getJsonObject("fileInfo"), FileInfo.class));
} }
this.setCacheHit(json.getBoolean("cacheHit", false)); this.setCacheHit(json.getBoolean("cacheHit", false));
// 初始化 otherParam
this.otherParam = new HashMap<>();
if (json.containsKey("otherParam")) {
JsonObject otherJson = json.getJsonObject("otherParam");
if (otherJson != null) {
this.otherParam.putAll(otherJson.getMap());
}
}
} }
@@ -59,28 +59,4 @@ public class ClientLinkResp {
* 解析信息 * 解析信息
*/ */
private String parserInfo; private String parserInfo;
/**
* 网盘类型代码
*/
private String panType;
/**
* 是否必须使用客户端下载(直链需要特殊头部,浏览器无法直接下载)
* 适用于:UC、QK、PCX、COW等
*/
private boolean requiresClient;
/**
* 认证需求级别:
* - "none": 不需要认证
* - "required": 必须认证(UC、QK)
* - "optional": 可选认证,大文件需要(FJ、IZ)
*/
private String authRequirement;
/**
* 认证提示信息
*/
private String authHint;
} }
Loaded 100 of 112 files, more files were not shown because too many files have changed in this diff. Show more