2

I'm creating a custom sign-in/sign-up in Meteor because I need to check the email format and more importantly to add a recaptcha in the sign-up form, which requires the client's ip.

So I plan to verify the captcha in Accounts.validateNewUser, but I cant figure out how to get the client's ip here. I read an answer on how to do that, but it's not where I want to. I checked Meteor.default_server.sessions which contains all the sessions/sockets on the server. They contain information about headers, ips of all sockets. Each one has interesting stuff like:

pathname: "/sockjs/375/ibmrlwb2/xhr"
prefix: "/sockjs"
protocol: "xhr-polling"
remoteAddress: "127.0.0.1"
remotePort: 42009

But without knowing which socket is the current one, I cannot determine which ip of the current request is.

How can I know the current request url to the server? With that I can check with all the sockets on the server to infer the client's ip.

Or just simply how can I get the client's ip on the server?

Community
  • 1
  • 1
lastid
  • 469
  • 5
  • 8
  • Duplicate of [How to get the user IP address in Meteor server?](http://stackoverflow.com/questions/14843232/how-to-get-the-user-ip-address-in-meteor-server) – Dan Dascalescu Jul 06 '15 at 19:06

2 Answers2

2

I'm the maintainer of the user-status package mentioned in the other answer.

Recent/upcoming versions of Meteor have better support for managing connections as well as built-in support for grabbing IP addresses. Combined with the fact that login hooks were just merged into the devel branch today, you should be able to either handle this directly or use something that we'll add to the user-status package.

Feel free to open a pull request if there is something you need.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
0

Getting the client's ip can be done with the user-status package. You can either use package or look through the code and see how it is being done.

user728291
  • 4,138
  • 1
  • 23
  • 26
  • Thanks for the answer. I looked into the code. Actually, we can have the current user's socket information (session ....) when in the `Meteor.publish` or `Meteor.method` context, not in `Accounts.validateNewUser` . If we inspect the socket information, we will have the user's ip address. The solution I took was: the client must give the socket id, then at the server side I loop through `Meteor.default_server.stream_server.open_sockets` to pick up the right socket and extract the client's ip. It's not a solid solution but helps me to move on. – lastid Jul 28 '13 at 15:43
  • I dont really know coffeescript, but is there any way of replicating this: `@_session.socket.remoteAddress` in Javascipt? – Chet Aug 29 '13 at 09:27
  • @Chet @ just means 'this', so that's this._session.socket.remoteAddress, which probably has the same scope problems – Kevin Aug 29 '13 at 15:58