-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathhttp.js
141 lines (130 loc) · 4.89 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import axios from 'axios';
import http from 'http';
import https from 'https';
import {ENV} from '../utils/env.js';
import {keysToLowerCase} from '../utils/utils.js';
const AgentOption = {keepAlive: true, maxSockets: 64, timeout: 30000}; // 最大连接数64,30秒定期清理空闲连接
// const AgentOption = {keepAlive: true};
const httpAgent = new http.Agent(AgentOption);
const httpsAgent = new https.Agent({rejectUnauthorized: false, ...AgentOption});
// 配置 axios 使用代理
const _axios = axios.create({
httpAgent, // 用于 HTTP 请求的代理
httpsAgent, // 用于 HTTPS 请求的代理
});
export default (fastify, options, done) => {
// 读取 views 目录下的 encoder.html 文件并返回
fastify.post('/http', async (req, reply) => {
const {url, headers: userHeaders = {}, params = {}, method = 'GET', data = {}} = req.body;
if (!url) {
return reply.status(400).send({error: 'Missing required field: url'});
}
const headers = keysToLowerCase({
...userHeaders,
});
// 添加accept属性防止获取网页源码编码不正确问题
if (!Object.keys(headers).includes('accept')) {
headers['accept'] = '*/*';
}
console.log('http headers:', headers);
try {
const response = await _axios({
url,
method,
headers,
params,
data,
});
reply.status(response.status).send({
status: response.status,
headers: response.headers,
data: response.data,
});
} catch (error) {
// console.error(error);
if (error.response) {
// 服务器返回了非 2xx 状态码
reply.status(error.response.status).send({
error: error.response.data,
status: error.response.status,
});
} else {
// 请求失败或其他错误
reply.status(500).send({error: error.message});
}
}
});
fastify.get('/ai', async (request, reply) => {
const userInput = request.query.text;
if (!userInput || userInput.trim() === '') {
return reply.status(400).send({error: '请提供文本内容'});
}
const postFields = {
messages: [
{role: 'user', content: userInput}
],
model: 'gpt-4o-mini-2024-07-18'
};
// console.log(JSON.stringify(postFields));
try {
const response = await _axios.post(
'https://api.s01s.cn/API/ai_zdy/?type=2',
postFields,
{
headers: {'Content-Type': 'application/json'},
timeout: 30000,
}
);
return reply.send(response.data);
} catch (error) {
fastify.log.error('Error:', error.message);
return reply.status(500).send({error: '请求失败,请稍后重试'});
}
});
fastify.all('/req/*', async (request, reply) => {
// 非VERCEL环境可在设置中心控制此功能是否开启
if (!process.env.VERCEL) {
if (ENV.get('allow_forward') !== '1') {
return reply.code(403).send({error: 'Forward api is not allowed by owner'});
}
}
try {
const targetUrl = request.params['*'];
if (!/^https?:\/\//.test(targetUrl)) {
return reply.code(400).send({error: 'Invalid URL. Must start with http:// or https://'});
}
console.log(`Forwarding request to: ${targetUrl}`);
const headers = keysToLowerCase({
...request.headers,
});
delete headers['host'];
// 添加accept属性防止获取网页源码编码不正确问题
if (!Object.keys(headers).includes('accept')) {
headers['accept'] = '*/*';
}
const response = await _axios({
method: request.method,
url: targetUrl,
headers: headers,
data: request.body,
params: request.query,
timeout: 10000,
});
reply
.code(response.status)
.headers(response.headers)
.send(response.data);
} catch (error) {
console.error('Error forwarding request:', error.message);
if (error.response) {
reply
.code(error.response.status)
.headers(error.response.headers)
.send(error.response.data);
} else {
reply.code(500).send({error: `Internal Server Error:${error.message}`});
}
}
});
done();
};