diff --git a/.pkgignore b/.pkgignore new file mode 100644 index 0000000..be5353c --- /dev/null +++ b/.pkgignore @@ -0,0 +1,77 @@ +# 开发和测试文件 +test.js +quick-test.js +build.js +start.js +test-video.html + +# 文档文件 +README.md +LICENSE +docs/ + +# Git 相关 +.gitignore +.git/ + +# Node.js 相关 +node_modules/.cache/ +node_modules/*/test/ +node_modules/*/tests/ +node_modules/*/.nyc_output/ +node_modules/*/coverage/ +node_modules/*/docs/ +node_modules/*/doc/ +node_modules/*/examples/ +node_modules/*/example/ +node_modules/*/benchmark/ +node_modules/*/benchmarks/ + +# Puppeteer 相关 - 排除大部分 Chromium 文件 +node_modules/puppeteer/.local-chromium/ +node_modules/puppeteer/lib/esm/ +node_modules/puppeteer/src/ +node_modules/puppeteer/test/ +node_modules/puppeteer/docs/ + +# 开发环境专用模块 +node_modules/pino-pretty/ + +# TypeScript 定义文件 +node_modules/@types/ +*.d.ts + +# 日志文件 +*.log +logs/ + +# 临时文件 +*.tmp +*.temp +.DS_Store +Thumbs.db + +# 开发工具配置 +.vscode/ +.idea/ +*.swp +*.swo + +# 构建输出 +dist/ +build/ + +# 包管理器文件 +package-lock.json +yarn.lock +pnpm-lock.yaml + +# 其他大型依赖的非必要部分 +node_modules/*/CHANGELOG.md +node_modules/*/HISTORY.md +node_modules/*/AUTHORS +node_modules/*/CONTRIBUTORS +node_modules/*/*.md +node_modules/*/*.txt +node_modules/*/LICENSE* +node_modules/*/LICENCE* \ No newline at end of file diff --git a/README.md b/README.md index 0131057..550a285 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,30 @@ # Pup Sniffer +[](https://nodejs.org/) +[](https://opensource.org/licenses/MIT) +[](https://github.com) + 基于 Puppeteer 和 Fastify 的视频资源嗅探器 Node.js 版本。 +> 🎯 **高性能视频资源嗅探工具**,支持多平台部署,提供完整的 API 接口和现代化 Web 界面。 + +## 📋 目录 + +- [功能特性](#功能特性) +- [系统要求](#系统要求) +- [快速开始](#快速开始) +- [构建二进制文件](#构建二进制文件) +- [API 接口](#api-接口) +- [使用示例](#使用示例) +- [测试](#测试) +- [命令行参数](#命令行参数) +- [环境变量](#环境变量) +- [注意事项](#注意事项) +- [故障排除](#故障排除) +- [文档](#文档) +- [开发](#开发) +- [许可证](#许可证) + ## 功能特性 - 🎯 视频资源嗅探(支持 m3u8、mp4 等格式) @@ -12,26 +35,42 @@ - 🎨 现代化 Web 演示界面 - 📦 支持打包成二进制文件,无需安装 Node.js +## 系统要求 + +- **Chrome 浏览器**: 系统需要安装 Chrome 浏览器(Puppeteer 依赖) +- **Node.js**: 从源码运行需要 Node.js >= 18.0.0 +- **操作系统**: 支持 Windows、Linux、macOS + ## 快速开始 ### 方式一:使用二进制文件(推荐) 1. 下载对应平台的二进制文件: - - Windows: `pup-sniffer-win.exe` - - Linux: `pup-sniffer-linux` - - macOS (Intel): `pup-sniffer-macos` - - macOS (Apple Silicon): `pup-sniffer-macos-arm64` + - Windows: `pup-sniffer-win.exe` + - Linux: `pup-sniffer-linux` + - macOS (Intel): `pup-sniffer-macos` + - macOS (Apple Silicon): `pup-sniffer-macos-arm64` 2. 将 `demo.html` 文件放在二进制文件同一目录下 3. 直接运行二进制文件: ```bash - # Windows + # Windows - 默认端口启动 ./pup-sniffer-win.exe - # Linux/macOS + # Windows - 指定端口启动 + ./pup-sniffer-win.exe -port 8080 + + # Linux/macOS - 默认端口启动 ./pup-sniffer-linux ./pup-sniffer-macos + + # Linux/macOS - 指定端口启动 + ./pup-sniffer-linux -port 8080 + ./pup-sniffer-macos -port 8080 + + # 查看帮助信息 + ./pup-sniffer-win.exe --help ``` ### 方式二:从源码运行 @@ -43,27 +82,56 @@ 2. 启动服务: ```bash + # 默认端口启动(自动查找可用端口,从57573开始) npm start + + # 或直接使用 node 命令 + node server.cjs + + # 指定端口启动 + node server.cjs -port 8080 + + # 查看帮助信息 + node server.cjs --help ``` -服务将在 http://localhost:57573 启动 +服务默认在 http://localhost:57573 启动(如果端口被占用会自动查找下一个可用端口) ## 构建二进制文件 -### 构建所有平台 +项目提供了多种构建选项,支持不同的压缩算法和优化级别,以满足不同的使用需求。 + +### 📦 构建选项概览 + +| 构建类型 | 压缩算法 | 体积优化 | 适用场景 | +|---------|--------|------|--------| +| 标准版 | GZip | 中等 | 通用部署 | +| Brotli版 | Brotli | 较高 | 网络传输优化 | +| 轻量版 | Brotli | 高 | 资源受限环境 | +| 迷你版 | Brotli | 最高 | 极致体积要求 | + +### 🚀 快速构建 + ```bash +# 构建所有平台(标准版,GZip压缩) npm run build + +# 构建优化版本(推荐,包含多种压缩选项) +npm run build:optimized + +# 构建迷你版本(最小体积,所有平台) +npm run build:mini ``` -### 构建特定平台 +### 🎯 平台特定构建 -[打包工具pkg安装参考](https://www.jb51.net/javascript/329845uie.htm) +#### 标准版本(GZip 压缩) ```bash # Windows x64 npm run build:win -# Linux x64 +# Linux x64 npm run build:linux # macOS x64 @@ -73,6 +141,131 @@ npm run build:macos npm run build:macos-arm ``` +#### Brotli 压缩版本(更小体积) + +```bash +# Windows x64 +npm run build:win:brotli + +# Linux x64 +npm run build:linux:brotli + +# macOS x64 +npm run build:macos:brotli + +# macOS ARM64 +npm run build:macos-arm:brotli +``` + +#### 轻量版本(优化选项) + +```bash +# Windows x64 +npm run build:win:lite + +# Linux x64 +npm run build:linux:lite + +# macOS x64 +npm run build:macos:lite + +# macOS ARM64 +npm run build:macos-arm:lite +``` + +#### 迷你版本(最小体积) + +```bash +# Windows x64 +npm run build:win:mini + +# Linux x64 +npm run build:linux:mini + +# macOS x64 +npm run build:macos:mini + +# macOS ARM64 +npm run build:macos-arm:mini +``` + +### ⚙️ 构建优化技术 + +项目采用了多种优化技术来减小打包体积: + +1. **压缩算法**: + - **GZip**:标准压缩,兼容性好 + - **Brotli**:更高压缩率,体积减少 10-15% + +2. **PKG 优化选项**: + - `--no-bytecode`:禁用字节码缓存 + - `--public-packages=*`:减少包装开销 + - `--no-warnings`:减少输出体积 + +3. **资源排除**: + - 通过 `.pkgignore` 排除开发、测试、文档文件 + - 排除大型依赖的非必要部分 + - 智能排除开发环境专用模块 + +4. **智能配置**: + - 生产环境自动禁用 `pino-pretty` 日志美化 + - 动态检测运行环境,优化资源加载 + +### 📊 体积对比 + +以 Windows x64 版本为例: + +| 版本类型 | 文件大小 | 压缩率 | 说明 | +|---------|--------|------|---------------| +| 标准版 | ~46 MB | - | GZip 压缩 | +| Brotli版 | ~44 MB | -4% | Brotli 压缩 | +| 轻量版 | ~42 MB | -9% | Brotli + 优化选项 | +| 迷你版 | ~41 MB | -11% | 最大优化 | + +### 🛠️ 高级构建 + +如需更多自定义选项,可以使用优化构建脚本: + +```bash +# 运行优化构建脚本(包含所有选项) +node build-optimized.js + +# 查看详细构建指南 +cat docs/BUILD_GUIDE.md +``` + +### 📋 构建要求 + +[打包工具pkg安装参考](https://www.jb51.net/javascript/329845uie.htm) + +[各版本二进制下载地址](https://github.com/vercel/pkg-fetch/releases) + +linux命令安装方法 + +``` +cd /root/.pkg-cache +wget -O fetched-v18.5.0-linux-x64 https://github.catvod.com/https://github.com/vercel/pkg-fetch/releases/download/v3.4/node-v18.5.0-linux-x64 +npm run build:linux:mini +cd dist +./pup-sniffer-linux-mini +``` + +**系统要求**: + +- Node.js >= 18.0.0 +- npm 或 yarn +- 足够的磁盘空间(构建过程需要约 200MB 临时空间) + +**依赖安装**: + +```bash +# 安装构建依赖 +npm install + +# 全局安装 pkg(可选,项目已包含) +npm install -g pkg +``` + 构建完成后,二进制文件将输出到 `dist/` 目录。 ## API 接口 @@ -83,18 +276,18 @@ npm run build:macos-arm #### 请求参数 -| 参数 | 类型 | 必需 | 说明 | -|------|------|------|------| -| url | string | ✅ | 要嗅探的页面 URL | -| mode | string | ❌ | 嗅探模式:0=单个链接,1=多个链接(默认:0) | -| is_pc | string | ❌ | 是否模拟 PC:0=移动设备,1=PC(默认:0) | -| timeout | string | ❌ | 超时时间(毫秒,默认:10000) | -| css | string | ❌ | 等待的 CSS 选择器 | -| script | string | ❌ | 页面执行脚本(Base64 编码) | -| init_script | string | ❌ | 页面初始化脚本(Base64 编码) | -| headers | string | ❌ | 自定义请求头(换行分隔) | -| custom_regex | string | ❌ | 自定义匹配正则表达式 | -| sniffer_exclude | string | ❌ | 排除匹配的正则表达式 | +| 参数 | 类型 | 必需 | 说明 | +|-----------------|--------|----|---------------------------| +| url | string | ✅ | 要嗅探的页面 URL | +| mode | string | ❌ | 嗅探模式:0=单个链接,1=多个链接(默认:0) | +| is_pc | string | ❌ | 是否模拟 PC:0=移动设备,1=PC(默认:0) | +| timeout | string | ❌ | 超时时间(毫秒,默认:10000) | +| css | string | ❌ | 等待的 CSS 选择器 | +| script | string | ❌ | 页面执行脚本(Base64 编码) | +| init_script | string | ❌ | 页面初始化脚本(Base64 编码) | +| headers | string | ❌ | 自定义请求头(换行分隔) | +| custom_regex | string | ❌ | 自定义匹配正则表达式 | +| sniffer_exclude | string | ❌ | 排除匹配的正则表达式 | #### 响应示例 @@ -123,15 +316,15 @@ npm run build:macos-arm #### 请求参数 -| 参数 | 类型 | 必需 | 说明 | -|------|------|------|------| -| url | string | ✅ | 要获取源码的页面 URL | -| is_pc | string | ❌ | 是否模拟 PC:0=移动设备,1=PC(默认:0) | -| timeout | string | ❌ | 超时时间(毫秒,默认:10000) | -| css | string | ❌ | 等待的 CSS 选择器 | -| script | string | ❌ | 页面执行脚本(Base64 编码) | -| init_script | string | ❌ | 页面初始化脚本(Base64 编码) | -| headers | string | ❌ | 自定义请求头(换行分隔) | +| 参数 | 类型 | 必需 | 说明 | +|-------------|--------|----|---------------------------| +| url | string | ✅ | 要获取源码的页面 URL | +| is_pc | string | ❌ | 是否模拟 PC:0=移动设备,1=PC(默认:0) | +| timeout | string | ❌ | 超时时间(毫秒,默认:10000) | +| css | string | ❌ | 等待的 CSS 选择器 | +| script | string | ❌ | 页面执行脚本(Base64 编码) | +| init_script | string | ❌ | 页面初始化脚本(Base64 编码) | +| headers | string | ❌ | 自定义请求头(换行分隔) | #### 响应示例 @@ -185,10 +378,16 @@ npm run build:macos-arm ## 使用示例 +**注意**:以下示例使用默认端口 57573,如果您使用了 `-port` 参数指定了其他端口,请相应调整 URL 中的端口号。 + ### 基本嗅探 ```bash +# 默认端口 curl "http://localhost:57573/sniffer?url=https://example.com/play" + +# 自定义端口(如果使用 -port 8080 启动) +curl "http://localhost:8080/sniffer?url=https://example.com/play" ``` ### 多链接嗅探 @@ -220,19 +419,32 @@ curl "http://localhost:57573/fetCodeByWebView?url=https://example.com" ## 测试 ```bash -# 测试 Sniffer 类 +# 运行测试脚本 npm test - -# 测试服务器接口(需要先启动服务) -npm run test:server ``` +**注意**: 测试前请确保服务器已启动 (`npm start`)。 + +## 命令行参数 + +| 参数 | 说明 | 示例 | +|---------------|--------------------|--------------| +| `-port <端口号>` | 指定服务器端口号 (1-65535) | `-port 8080` | +| `-h, --help` | 显示帮助信息 | `--help` | + +**端口说明**: + +- 如果不指定端口号,程序将从 57573 开始自动查找可用端口 +- 如果指定的端口被占用,程序会报错并退出 +- 端口号必须在 1-65535 范围内 + ## 环境变量 -| 变量 | 说明 | 默认值 | -|------|------|--------| -| PORT | 服务端口 | 57573 | -| HOST | 服务主机 | 0.0.0.0 | +| 变量 | 说明 | 默认值 | +|------|--------|---------| +| HOST | 服务主机地址 | 0.0.0.0 | + +**注意**: 端口配置请使用 `-port` 命令行参数,不支持通过环境变量设置端口。 ## 注意事项 @@ -260,15 +472,59 @@ npm run test:server 3. 尝试使用自定义正则表达式 4. 启用调试模式查看详细日志 -## 开发 +### linux使用前置条件 + +```shell +# 安装谷歌浏览器 +apt update +wget -O google-chrome-104.deb -c https://www.slimjet.com/chrome/download-chrome.php?file=files%2F104.0.5112.102%2Fgoogle-chrome-stable_current_amd64.deb +dpkg -i google-chrome-104.deb +apt install -f +dpkg -i google-chrome-104.deb +google-chrome --version +``` + +## 📚 文档 + +- [API 文档](docs/API_DOCS.md) - 详细的 API 接口说明 +- [构建指南](docs/BUILD_GUIDE.md) - 完整的构建配置和优化说明 -项目采用 ESM 模块化开发,主要文件: +## 🔧 开发 -- `sniffer.js`: 核心嗅探类 -- `server.js`: Fastify 服务器 +项目采用 CommonJS 模块化开发,主要文件: + +- `sniffer.cjs`: 核心嗅探类 +- `server.cjs`: Fastify 服务器 - `test.js`: 测试脚本 - `package.json`: 项目配置 +- `build-optimized.js`: 优化构建脚本 +- `.pkgignore`: 打包排除文件配置 + +### 开发环境设置 + +```bash +# 克隆项目 +git clone https://github.com/hjdhnx/pup-sniffer.git +cd pup-sniffer + +# 安装依赖 +npm install + +# 启动开发服务器 +npm start + +# 运行测试 +npm test +``` + +### 代码贡献 + +1. Fork 项目 +2. 创建特性分支 (`git checkout -b feature/AmazingFeature`) +3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) +4. 推送到分支 (`git push origin feature/AmazingFeature`) +5. 打开 Pull Request -## 许可证 +## 📄 许可证 MIT License \ No newline at end of file diff --git a/build-optimized.js b/build-optimized.js new file mode 100644 index 0000000..1a7ad69 --- /dev/null +++ b/build-optimized.js @@ -0,0 +1,91 @@ +#!/usr/bin/env node + +const { execSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +console.log('🚀 开始优化构建...'); + +// 清理输出目录 +if (fs.existsSync('dist')) { + console.log('🧹 清理旧的构建文件...'); + try { + fs.rmSync('dist', { recursive: true, force: true }); + } catch (error) { + console.log('⚠️ 清理失败,继续构建...'); + } +} + +// 创建输出目录 +fs.mkdirSync('dist', { recursive: true }); + +// 构建配置 +const builds = [ + { + name: 'Windows (GZip)', + command: 'pkg . --targets node18-win-x64 --compress GZip --output dist/pup-sniffer-win.exe' + }, + { + name: 'Windows (Brotli - 最小体积)', + command: 'pkg . --targets node18-win-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-win-mini.exe' + }, + { + name: 'Linux (GZip)', + command: 'pkg . --targets node18-linux-x64 --compress GZip --output dist/pup-sniffer-linux' + }, + { + name: 'Linux (Brotli - 最小体积)', + command: 'pkg . --targets node18-linux-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-linux-mini' + }, + { + name: 'macOS Intel (GZip)', + command: 'pkg . --targets node18-macos-x64 --compress GZip --output dist/pup-sniffer-macos' + }, + { + name: 'macOS Intel (Brotli - 最小体积)', + command: 'pkg . --targets node18-macos-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-macos-mini' + }, + { + name: 'macOS ARM (GZip)', + command: 'pkg . --targets node18-macos-arm64 --compress GZip --output dist/pup-sniffer-macos-arm64' + }, + { + name: 'macOS ARM (Brotli - 最小体积)', + command: 'pkg . --targets node18-macos-arm64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-macos-arm64-mini' + } +]; + +// 执行构建 +for (const build of builds) { + console.log(`\n📦 构建: ${build.name}`); + try { + const startTime = Date.now(); + execSync(build.command, { stdio: 'inherit' }); + const endTime = Date.now(); + console.log(`✅ ${build.name} 构建完成 (${((endTime - startTime) / 1000).toFixed(1)}s)`); + } catch (error) { + console.error(`❌ ${build.name} 构建失败:`, error.message); + } +} + +// 显示文件大小统计 +console.log('\n📊 构建结果统计:'); +console.log('=' .repeat(60)); + +const distFiles = fs.readdirSync('dist').filter(file => + !file.includes('.') || file.endsWith('.exe') +); + +distFiles.forEach(file => { + const filePath = path.join('dist', file); + const stats = fs.statSync(filePath); + const sizeInMB = (stats.size / (1024 * 1024)).toFixed(2); + console.log(`${file.padEnd(35)} ${sizeInMB.padStart(8)} MB`); +}); + +console.log('=' .repeat(60)); +console.log('🎉 所有构建完成!'); +console.log('\n💡 提示:'); +console.log('- 使用 GZip 版本获得更好的兼容性'); +console.log('- 使用 Brotli mini 版本获得最小体积'); +console.log('- mini 版本可能需要更多运行时依赖'); \ No newline at end of file diff --git a/demo.html b/demo.html index 549ade3..0e82d13 100644 --- a/demo.html +++ b/demo.html @@ -452,7 +452,8 @@