Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.

Commit cd803d0

Browse files
authored
Merge pull request #51 from cubos/fix/missing-ip
Use a proper function to get the IP from request
2 parents 97a4603 + 8d87cd7 commit cd803d0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/target/typescript_nodeserver.cr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ function sleep(ms: number) {
219219
return new Promise<void>(resolve => setTimeout(resolve, ms));
220220
}
221221
222+
function getIp(req: http.IncomingMessage): string | undefined {
223+
const possibleOverridenIp = req.headers["x-real-ip"] || req.headers["x-forwarded-for"];
224+
225+
if (possibleOverridenIp) {
226+
return Array.isArray(possibleOverridenIp) ? possibleOverridenIp[0] : possibleOverridenIp;
227+
} else {
228+
return req.connection.remoteAddress;
229+
}
230+
}
231+
222232
export let server: http.Server;
223233
224234
export const hook: {
@@ -262,7 +272,7 @@ export function start(port: number = 8000) {
262272
res.end();
263273
return;
264274
}
265-
const ip = req.headers["x-real-ip"] as string || (req.headers["x-fowarded-for"] as string || "");
275+
const ip = getIp(req) || "";
266276
const signature = req.method! + url.parse(req.url || "").pathname;
267277
if (httpHandlers[signature]) {
268278
console.log(`${toDateTimeString(new Date())} http ${signature}`);

0 commit comments

Comments
 (0)