2024-01-15 13:22:54 +08:00
|
|
|
|
import random
|
|
|
|
|
|
2024-01-15 11:33:52 +08:00
|
|
|
|
import requests
|
2024-01-15 13:22:54 +08:00
|
|
|
|
from fake_useragent import UserAgent
|
2024-01-15 11:33:52 +08:00
|
|
|
|
from flask import Flask, request, jsonify
|
|
|
|
|
|
2024-01-18 11:17:32 +08:00
|
|
|
|
import tools
|
|
|
|
|
|
2024-01-15 11:33:52 +08:00
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
2024-01-15 13:22:54 +08:00
|
|
|
|
def generate_random_headers():
|
|
|
|
|
"""
|
|
|
|
|
生成随机的User-Agent头部。
|
|
|
|
|
|
|
|
|
|
:return: 随机的User-Agent头部
|
|
|
|
|
:rtype: dict
|
|
|
|
|
"""
|
|
|
|
|
user_agent = UserAgent().random
|
|
|
|
|
accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
|
|
|
accept_language = random.choice(
|
|
|
|
|
["zh-CN,zh;q=0.9",
|
|
|
|
|
"en-US,en;q=0.5",
|
|
|
|
|
"en-GB,en;q=0.5",
|
|
|
|
|
"de-DE,de;q=0.5",
|
|
|
|
|
"fr-FR,fr;q=0.5"])
|
|
|
|
|
accept_encoding = "gzip, deflate, br"
|
|
|
|
|
referer = random.choice(
|
|
|
|
|
["https://www.baidu.com",
|
|
|
|
|
"https://www.google.com",
|
|
|
|
|
"https://www.bing.com"])
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
"User-Agent": user_agent,
|
|
|
|
|
"Accept": accept,
|
|
|
|
|
"Accept-Language": accept_language,
|
|
|
|
|
"Accept-Encoding": accept_encoding,
|
|
|
|
|
"Referer": referer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return headers
|
|
|
|
|
|
|
|
|
|
|
2024-01-15 11:33:52 +08:00
|
|
|
|
@app.route('/proxy', methods=['POST'])
|
|
|
|
|
def proxy():
|
|
|
|
|
"""
|
|
|
|
|
代理接口,转发请求到指定的 URL。
|
|
|
|
|
接受 JSON 格式的请求体,其中应包含 URL、HTTP 方法和其他可选参数。
|
|
|
|
|
返回格式化的响应,包含状态码、消息释义和数据。
|
|
|
|
|
"""
|
|
|
|
|
req_data = request.json
|
|
|
|
|
|
|
|
|
|
# 确保提供了 url 和 method
|
|
|
|
|
if 'url' not in req_data or 'method' not in req_data:
|
|
|
|
|
return jsonify({'status': 'error', 'msg': 'URL or Method not provided', 'data': {}}), 400
|
|
|
|
|
|
|
|
|
|
url = req_data['url']
|
|
|
|
|
method = req_data['method'].lower()
|
|
|
|
|
|
|
|
|
|
# 支持的请求方法
|
|
|
|
|
supported_methods = {'get', 'post', 'put', 'delete', 'patch'}
|
|
|
|
|
if method not in supported_methods:
|
|
|
|
|
return jsonify({'status': 'error', 'msg': 'Unsupported method', 'data': {}}), 400
|
|
|
|
|
|
|
|
|
|
# 提取请求参数
|
|
|
|
|
params = req_data.get('params', {})
|
|
|
|
|
data = req_data.get('data', {})
|
|
|
|
|
json_data = req_data.get('json', {})
|
2024-01-15 13:22:54 +08:00
|
|
|
|
headers = req_data.get('headers', generate_random_headers())
|
2024-01-15 11:33:52 +08:00
|
|
|
|
cookies = req_data.get('cookies', {})
|
|
|
|
|
timeout = req_data.get('timeout', 10)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 使用 requests 发送请求
|
|
|
|
|
response = requests.request(method, url, params=params, data=data, json=json_data,
|
2024-01-15 13:22:54 +08:00
|
|
|
|
headers=headers, cookies=cookies, timeout=timeout)
|
2024-01-15 11:33:52 +08:00
|
|
|
|
|
|
|
|
|
# 尝试解析 JSON,如果不是 JSON 则保持原样
|
|
|
|
|
try:
|
|
|
|
|
response_json = response.json()
|
|
|
|
|
except ValueError:
|
|
|
|
|
response_json = {}
|
|
|
|
|
|
|
|
|
|
# 构建响应数据
|
|
|
|
|
response_data = {
|
|
|
|
|
'url': response.url,
|
|
|
|
|
'headers': dict(response.headers),
|
|
|
|
|
'status_code': response.status_code,
|
|
|
|
|
'text': response.text,
|
|
|
|
|
'json': response_json,
|
|
|
|
|
'cookies': requests.utils.dict_from_cookiejar(response.cookies)
|
|
|
|
|
}
|
|
|
|
|
return jsonify({
|
|
|
|
|
'status': 'success',
|
|
|
|
|
'msg': 'Request successful',
|
|
|
|
|
'data': response_data
|
|
|
|
|
}), response.status_code
|
|
|
|
|
except requests.RequestException as e:
|
|
|
|
|
# 请求过程中出现异常
|
|
|
|
|
return jsonify({
|
|
|
|
|
'status': 'error',
|
|
|
|
|
'msg': str(e),
|
|
|
|
|
'data': {}
|
|
|
|
|
}), 500
|
|
|
|
|
|
|
|
|
|
|
2024-01-18 11:17:32 +08:00
|
|
|
|
@app.route('/download', methods=['GET'])
|
|
|
|
|
def proxy():
|
|
|
|
|
url = request.args.get('url')
|
|
|
|
|
if not url:
|
|
|
|
|
return jsonify({
|
|
|
|
|
'status': 'error',
|
|
|
|
|
'msg': 'No URL provided',
|
|
|
|
|
'data': {}
|
|
|
|
|
}), 400
|
|
|
|
|
|
|
|
|
|
content = tools.download_html_with_wget(url)
|
|
|
|
|
|
|
|
|
|
if content is None:
|
|
|
|
|
return jsonify({
|
|
|
|
|
'status': 'error',
|
|
|
|
|
'msg': 'Unable to download the content',
|
|
|
|
|
'data': {}
|
|
|
|
|
}), 500
|
|
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
|
'status': 'success',
|
|
|
|
|
'msg': 'Request successful',
|
|
|
|
|
'data': content
|
|
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
|
2024-01-15 11:33:52 +08:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run(host='0.0.0.0', port=5000)
|