node.js - header not revealing the ip address? -


{ host: '127.0.0.1:8080',   connection: 'keep-alive',   'cache-control': 'max-age=0',   'user-agent': 'mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) ubuntu chromium/44.0.2403.89 chrome/44.0.2403.89 safari/537.36',   accept: '*/*',   referer: 'http://127.0.0.1:8080/',   'accept-encoding': 'gzip, deflate, sdch',   'accept-language': 'en-us,en;q=0.8',   cookie: 'io=bp2exrqs3skwyy4caaac11' } 

i using koa.io npm try , ip address of client connecting. when try following code above printed in console. can't seem ip address of client.

app.io.use(function* userleft(next) {     // on connect     console.log(this.headers); }); 

i on localhost. expecting client ip address 127.0.0.1?

try this.request.ip. should work...

after testing realized 2 things.

  1. when behind proxy, make sure proxy forwarding clients ip using x-forwarded-for header.
  2. in theory 1. should enough this.request.ip return client ip, isn't. after configuring proxy, this.request.header["x-forwarded-for"] returns client ip

test simple server:

var koa = require('koa'); var app = koa();  app.use(function *(){ this.body = this.request.ip; });  app.listen(3000); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -