简体   繁体   English

标头不显示IP地址?

[英]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, like 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 am using koa.io from npm to try and get the ip address of the client connecting. 我正在从npm使用koa.io尝试获取连接的客户端的IP地址。 when I try the following code the above is printed in the console. 当我尝试以下代码时,以上内容将打印在控制台中。 I can't seem to get the ip address of the client. 我似乎无法获得客户端的IP地址。

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

I am currently on localhost. 我目前在本地主机上。 SO I am expecting the client ip address to be 127.0.0.1? 所以我期望客户端IP地址是127.0.0.1?

Try this.request.ip . 试试this.request.ip It should work... 应该可以...

after testing i realized two things. 经过测试,我意识到了两件事。

  1. when behind a proxy, make sure the proxy is forwarding the clients ip using an x-forwarded-for header. 当位于代理后面时,请确保代理使用x-forwarded-for标头转发客户端ip。
  2. in theory 1. should be enough for this.request.ip to return the client ip, but it isn't. 理论上1.应该足以让this.request.ip返回客户端ip,但事实并非如此。 But after configuring my proxy, this.request.header["x-forwarded-for"] returns the client ip 但是在配置我的代理后, this.request.header["x-forwarded-for"]返回客户端ip

test with this simple server: 用这个简单的服务器测试:

var koa = require('koa');
var app = koa();

app.use(function *(){
this.body = this.request.ip;
});

app.listen(3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM