4

I can't seem to find this anywhere in the docs and all info on google is one stackoverflow post which is rather incomplete. I have an application which does geoip and I need the visitor's ip address. Not logged in user, all visitors.

Any ideas how you do this with meteor?

UPDATE: After more searches I found this code which seems to work so far:

var Fiber = Npm.require('fibers');

__meteor_bootstrap__.app.use(function(req, res, next) {
    Fiber(function() {
        console.info(req.connection.remoteAddress);
        console.log(req);
        next();
    }).run();
});

but there's a problem. I can't link the ip address with the visitor itself since I can't send cookies or set session data from the server to the client like I would normally do in PHP/Python/Ruby.

I got the full request with it's headers but no visitor session id or something to pick this user out from the crowd.

Think of an application where you need to send a chat invite to all users from UK for example. You first need to geoip him then send the invite if everything matches up. So I need this back and forth.

4

2 回答 2

0

流星用户状态包跟踪所有连接用户的 IP 地址,以及其他一些东西。

请参阅http://user-status.meteor.com/上的演示。

请注意,使用此包和其他方法,您必须指定服务器运行的反向代理的数量,如下所述:http: //docs.meteor.com/#meteor_onconnection

免责声明:我是这个包的维护者。

于 2014-04-12T21:02:32.790 回答
-2

好的,这就是我如何让它工作的。它不漂亮,但它有效。当客户端打开您的页面时,您必须尽快调用服务器方法:

Meteor.call("get_visitor_ip");

然后在服务器上你有方法:

Meteor.methods({
    ...
    "get_visitor_ip": function() {
        this.setUserId(this.userId ? this.userId : new Meteor.Collection.ObjectID()._str);
        var user_ip = get_visitor_ip(this.userId);
                    // Do whatever you need with it
    }
    ...
});

get_visitor_ip = function(uid) {
    var k, ret, s, ss, _ref, _ref1, _ref2, _ref3;
    ret = {};
    if (uid != null) {
        _ref = Meteor.default_server.sessions;
        for (k in _ref) {
            ss = _ref[k];
            if (ss.userId === uid) {
                s = ss;
            }
        }
        if (s) {
            ret.forwardedFor = ( _ref1 = s.socket) != null ? ( _ref2 = _ref1.headers) != null ? _ref2['x-forwarded-for'] :
            void 0 :
            void 0;
            ret.remoteAddress = ( _ref3 = s.socket) != null ? _ref3.remoteAddress :
            void 0;
        }
    }
    return ret.forwardedFor ? ret.forwardedFor : ret.remoteAddress;
};
于 2013-06-30T22:07:40.743 回答