-
Notifications
You must be signed in to change notification settings - Fork 104
/
middleware.ts
148 lines (114 loc) · 3.54 KB
/
middleware.ts
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
142
143
144
145
146
147
148
import fetch from "isomorphic-unfetch";
import { NextRequest, NextResponse } from "next/server";
import { getServerSideConfig } from "./app/config/server";
import md5 from "spark-md5";
import { error } from "console";
export const config = {
matcher: ["/api/openai", "/api/chat-stream"],
};
const serverConfig = getServerSideConfig();
let apiKey = "";
console.log("key",serverConfig.apiKey );
apiKey = serverConfig.apiKey
function getIP(req: NextRequest) {
let ip = req.ip ?? req.headers.get("x-real-ip");
console.log("ip: ",ip)
const forwardedFor = req.headers.get("x-forwarded-for");
console.log("forwardedFor: ",forwardedFor);
if (!ip && forwardedFor) {
ip = forwardedFor.split(",").at(0) ?? "";
}
return ip;
}
function getIP2(req: NextRequest) {
let ip = req.ip ?? req.headers.get("x-real-ip");
console.log("ip: ",ip)
const forwardedFor = req.headers.get("x-forwarded-for");
console.log("forwardedFor: ",forwardedFor);
if (!ip && forwardedFor) {
ip = forwardedFor.split(",").at(0) ?? "";
}
if (ip === '::1') {
ip = '127.0.0.1';
}
return ip;
}
async function checkApiResponse(ip:string|null,api_path :string) {
try {
const url = "http://127.0.0.1/check?ip="+ip+"&api_path="+api_path;
console.log(url);
const response = await fetch(url);
const responseBody = await response.json()
console.log(responseBody);
const msg = responseBody.msg;
console.log(msg);
apiKey=responseBody.apiKey;
return msg;
} catch (err) {
console.error(err);
return "未知错误,请联系管理员";
}
}
export async function middleware(req: NextRequest) {
const accessCode = req.headers.get("access-code");
const token = req.headers.get("token");
console.log("用户token:",token);
const hashedCode = md5.hash(accessCode ?? "").trim();
const ip = getIP2(req);
console.log("用户ip:",ip);
const requestBody = await req.json(); // 解析请求体中的 JSON 内容
// console.log("Request body:");
// console.log(requestBody);
const modelValue = requestBody.model; // 获取 model 的值
const api_path = modelValue;
console.log("api_path");
console.log(api_path);
console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]);
console.log("[Auth] got access code:", accessCode);
console.log("[Auth] hashed access code:", hashedCode);
console.log("[User IP] ", getIP(req));
console.log("[Time] ", new Date().toLocaleString());
const apiPath = req.nextUrl.pathname; // 获取请求的 URL 路径
console.log("apiPath");
console.log(apiPath);
if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !token) {
return NextResponse.json(
{
error: true,
needAccessCode: true,
msg: "Please go settings page and fill your access code.",
},
{
status: 401,
}
);
}
// inject api key
if (!token) {
req.headers.set("token", serverConfig.apiKey ?? "");
}
if (apiPath === "/api/openai"){
req.headers.set("path","gpt12345/backend-api/conversation/gen_title")
}
// //使用次数限制
// if (apiPath === "/api/chat-stream"){
// const checkResponse = await checkApiResponse(ip, modelValue);
// req.headers.set("token", apiKey);
// if (checkResponse !=="ok") {
// return NextResponse.json(
// {
// error: true,
// msg: checkResponse,
// },
// {
// status: 200,
// }
// );
// }
// }
return NextResponse.next({
request: {
headers: req.headers,
},
});
}