dailiip/README.md
2025-10-30 23:05:24 +08:00

305 lines
5.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 代理IP抓取和验证服务
一个基于Node.js的代理IP自动抓取、验证和管理系统提供RESTful API接口和定时任务调度。
## 功能特性
- 🕷️ **自动抓取**: 从快代理网站抓取免费代理IP
-**自动验证**: 通过访问百度验证代理可用性
- 🗄️ **数据存储**: 使用SQLite存储代理数据
- 🔄 **定时任务**: 自动抓取和验证维护代理池
- 🚀 **RESTful API**: 完整的API接口服务
- 📊 **统计监控**: 实时代理池状态统计
- 🛡️ **错误处理**: 完善的错误处理和重试机制
## 项目结构
```
dailiip/
├── src/
│ ├── database/
│ │ ├── db.js # 数据库连接管理
│ │ └── models/
│ │ └── proxy.js # 代理数据模型
│ ├── services/
│ │ ├── scraper.js # 代理抓取服务
│ │ ├── validator.js # 代理验证服务
│ │ └── scheduler.js # 定时任务调度器
│ ├── routes/
│ │ └── proxies.js # API路由
│ └── app.js # 应用入口
├── config/
│ └── database.js # 数据库配置
├── logs/ # 日志目录
├── proxies.db # SQLite数据库
├── package.json
└── README.md
```
## 快速开始
### 1. 安装依赖
```bash
npm install
```
### 2. 启动服务
```bash
npm start
```
服务将在 `http://localhost:3000` 启动
### 3. 验证服务
访问 `http://localhost:3000/health` 检查服务状态
## API接口
### 基础信息
- **基础URL**: `http://localhost:3000`
- **Content-Type**: `application/json`
### 接口列表
#### 1. 获取服务信息
```
GET /
```
#### 2. 健康检查
```
GET /health
```
#### 3. 获取代理列表
```
GET /api/proxies?limit=100&offset=0&sortBy=response_time&order=ASC
```
- `limit`: 限制数量 (默认100)
- `offset`: 偏移量 (默认0)
- `sortBy`: 排序字段 (response_time, created_at, updated_at, ip, port)
- `order`: 排序方式 (ASC, DESC)
#### 4. 获取随机代理
```
GET /api/proxies/random?count=1
```
- `count`: 返回数量 (1-10)
#### 5. 获取统计信息
```
GET /api/proxies/stats
```
#### 6. 验证单个代理
```
POST /api/proxies/verify
{
"ip": "192.168.1.1",
"port": 8080
}
```
#### 7. 批量验证代理
```
POST /api/proxies/verify-batch
{
"proxies": [
{"ip": "192.168.1.1", "port": 8080},
{"ip": "192.168.1.2", "port": 8080}
]
}
```
#### 8. 手动触发抓取
```
POST /api/proxies/scrape
{
"pages": 5
}
```
#### 9. 手动触发验证
```
POST /api/proxies/validate-all
{
"limit": 50
}
```
#### 10. 搜索代理
```
GET /api/proxies/search?ip=192.168.1.1&port=8080
```
#### 11. 清理无效代理
```
DELETE /api/proxies/cleanup
```
## 定时任务
系统自动执行以下定时任务:
### 抓取任务
- **频率**: 每小时整点执行
- **功能**: 抓取快代理网站前5页的代理IP
- **网址**: https://www.kuaidaili.com/free/inha/
### 验证任务
- **频率**: 每10分钟执行一次
- **功能**: 验证数据库中的代理IP可用性
- **验证方式**: 通过访问 https://www.baidu.com
- **超时时间**: 8秒
### 健康检查
- **频率**: 每小时30分执行
- **功能**: 检查代理池健康状态
- **应急处理**: 可用代理少于10个时触发紧急抓取
## 数据库结构
### proxies 表
| 字段名 | 类型 | 说明 |
|--------|------|------|
| id | INTEGER | 主键,自增 |
| ip | TEXT | IP地址 |
| port | INTEGER | 端口号 |
| location | TEXT | 地理位置 |
| speed | INTEGER | 响应速度(毫秒) |
| last_check_time | TEXT | 最后验证时间 |
| is_valid | INTEGER | 是否可用 (1:可用, 0:不可用) |
| response_time | INTEGER | 验证响应时间 |
| created_at | DATETIME | 创建时间 |
| updated_at | DATETIME | 更新时间 |
**唯一约束**: (ip, port)
## 使用示例
### 获取随机代理
```bash
curl http://localhost:3000/api/proxies/random
```
响应:
```json
{
"success": true,
"data": {
"id": 1,
"ip": "101.132.134.160",
"port": 8888,
"location": "上海市",
"is_valid": 1,
"response_time": 150,
"created_at": "2025-10-30 12:00:00",
"updated_at": "2025-10-30 12:30:00"
}
}
```
### 验证代理
```bash
curl -X POST http://localhost:3000/api/proxies/verify \
-H "Content-Type: application/json" \
-d '{"ip":"101.132.134.160","port":8888}'
```
响应:
```json
{
"success": true,
"data": {
"ip": "101.132.134.160",
"port": 8888,
"isValid": true,
"responseTime": 150,
"error": null
}
}
```
### 获取统计信息
```bash
curl http://localhost:3000/api/proxies/stats
```
响应:
```json
{
"success": true,
"data": {
"total": 50,
"valid": 30,
"invalid": 20,
"validRate": "60.00%"
}
}
```
## 配置说明
### 环境变量
- `PORT`: 服务端口 (默认: 3000)
### 数据库配置
数据库配置文件: `config/database.js`
```javascript
const dbConfig = {
development: {
dialect: 'sqlite',
storage: path.join(__dirname, '../proxies.db')
}
};
```
## 日志和监控
- 服务启动后会输出详细的执行日志
- 通过 `/health` 接口可查看系统状态
- 支持优雅关闭,保证数据完整性
## 注意事项
1. **合法使用**: 请遵守网站服务条款合理使用代理IP
2. **频率控制**: 内置请求延迟和重试机制,避免对目标网站造成压力
3. **数据质量**: 免费代理IP质量参差不齐建议仅用于测试和学习
4. **资源占用**: 定时任务会占用一定的系统资源
## 开发和部署
### 开发模式
```bash
npm run dev
```
### 生产部署
建议使用 PM2 进行进程管理:
```bash
npm install -g pm2
pm2 start src/app.js --name "proxy-service"
pm2 logs proxy-service
pm2 status proxy-service
```
## 许可证
MIT License
## 贡献
欢迎提交Issue和Pull Request