Skip to content

Commit

Permalink
feat: close ChatGPTNextWeb#813 log user ip time
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Apr 16, 2023
1 parent b751861 commit bd69c8f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export const config = {

const serverConfig = getServerSideConfig();

function getIP(req: NextRequest) {
let ip = req.ip ?? req.headers.get("x-real-ip");
const forwardedFor = req.headers.get("x-forwarded-for");

if (!ip && forwardedFor) {
ip = forwardedFor.split(",").at(0) ?? "";
}

return ip;
}

export function middleware(req: NextRequest) {
const accessCode = req.headers.get("access-code");
const token = req.headers.get("token");
Expand All @@ -16,6 +27,8 @@ export function middleware(req: NextRequest) {
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());

if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !token) {
return NextResponse.json(
Expand Down

0 comments on commit bd69c8f

Please sign in to comment.