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

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

问题描述

我想在服务器端的meteor应用程序中获取用户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天全站免登陆