Skip to content

Commit 24451ad

Browse files
committed
fix: correctly handle IPs forwarded by Magic API
1 parent 501b3d3 commit 24451ad

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/services/ip.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ limit 1;
5151

5252
export function getClientIp(c: Context) {
5353
// See: https://docs.rapidapi.com/docs/additional-request-headers.
54-
const forwardedFor = c.req.header("x-forwarded-for")?.split(",")[0].trim();
55-
return forwardedFor || getConnInfo(c).remote.address || "127.0.0.1";
54+
const ipForwardedByRapidAPI = c.req
55+
.header("x-forwarded-for")
56+
?.split(",")[0]
57+
.trim();
58+
const ipForwardedByMagicAPI = c.req.header("x-original-forwarded-for");
59+
return (
60+
ipForwardedByMagicAPI ||
61+
ipForwardedByRapidAPI ||
62+
getConnInfo(c).remote.address ||
63+
"127.0.0.1"
64+
);
5665
}

0 commit comments

Comments
 (0)