Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user IP address in Meteor server?

Tags:

meteor

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).

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).

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

like image 682
sarfata Avatar asked Feb 12 '13 22:02

sarfata


People also ask

Where do I find the IP address of my server?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.


2 Answers

Similar to the TimDog answer but works with newer versions of Meteor:

var Fiber = Npm.require('fibers');  __meteor_bootstrap__.app   .use(function(req, res, next) {     Fiber(function () {       console.info(req.connection.remoteAddress);       next();     }).run();   }); 

This needs to be in your top-level server code (not in Meteor.startup)

like image 32
prideout Avatar answered Oct 15 '22 19:10

prideout


1 - Without a http request, in the functions you should be able to get the clientIP with:

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 - With a http request and using iron-router and its Router.map function:

In the action function of the targeted route use:

clientIp = this.request.connection.remoteAddress; 

3 - using Meteor.onConnection function:

Meteor.onConnection(function(conn) {     console.log(conn.clientAddress); }); 
like image 129
Florin Dobre Avatar answered Oct 15 '22 20:10

Florin Dobre