如何获取流星服务器中的用户IP地址? [英] How to get the user IP address in Meteor server?

查看:51
本文介绍了如何获取流星服务器中的用户IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器端的流星应用程序中获取用户IP地址,以便我可以用很多东西记录IP地址(例如:非注册用户订阅邮件列表,或者只是做任何重要的).

I would like to get the user IP address in my meteor application, on the server side, so that I can log the IP address with a bunch of things (for example: non-registered users subscribing to a mailing list, or just doing anything important).

我知道,当涉及反向代理时,服务器看到"的IP地址可能与真实源地址不同.在这种情况下,应解析X-Forwarded-For标头以获取用户的真实公共IP地址.请注意,解析X-Forwarded-For不应是自动的(请参见 http://www.openinfo.co.uk/apache/index.html 讨论潜在的安全问题.

I know that the IP address 'seen' by the server can be different than the real source address when there are reverse proxies involved. In such situations, X-Forwarded-For header should be parsed to get the real public IP address of the user. Note that parsing X-Forwarded-For should not be automatic (see http://www.openinfo.co.uk/apache/index.html for a discussion of potential security issues).

外部参考:这个问题出现在 meteor-talk邮件列表中2012年8月(未提供解决方案).

External reference: This question came up on the meteor-talk mailing list in august 2012 (no solution offered).

推荐答案

1-在没有http请求的情况下,您应该能够通过以下功能获得clientIP:

clientIP = this.connection.clientAddress;
//EX: you declare a submitForm function with Meteor.methods and 
//you call it from the client with Meteor.call().
//In submitForm function you will have access to the client address as above

2-使用http请求并使用iron-router及其Router.map功能:

在目标路线的动作功能中使用:

In the action function of the targeted route use:

clientIp = this.request.connection.remoteAddress;

3-使用Meteor.onConnection函数:

Meteor.onConnection(function(conn) {
    console.log(conn.clientAddress);
});

这篇关于如何获取流星服务器中的用户IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆