kahmali / meteor-restivus

REST APIs for the Best of Us! - A Meteor 0.9+ package for building REST APIs https://atmospherejs.com/nimble/restivus
MIT License
544 stars 116 forks source link

Add client IP address to endpoint context #74

Open iascchen opened 9 years ago

iascchen commented 9 years ago

I want to get the client ip address on restivus restful api, but I found when i use this.request.connection.remoteAddress only return 127.0.0.1. Is it a bug?

My sample code as follow:

Restivus.configure({
    useAuth: true,
    prettyJson: false 
});

Restivus.addRoute(':version/clientIp', {authRequired: false}, {
    get: function () {
        var deviceIp = this.request.connection.remoteAddress;
        console.log("deviceIp:", deviceIp);

        return {
            deviceIp: deviceIp
        };
    }
});

But I found var deviceIp = this.request.headers["x-forwarded-for"]; will return right ip address

kahmali commented 9 years ago

Sorry for the delayed response. Just getting back from a weekend trip. From what I've seen, using the X-Forwarded-For header is the only way to get the client IP when the server is behind a proxy. You can check out these StackOverflow answers here and here for more information on that.

What I will gladly do is add the client IP address into the endpoint context at this.clientIp, similar to Express. I'll convert this issue to a feature request, since it is the intended behavior at the moment. If someone wants to do it, I'll definitely accept a pull request on this. It's a pretty straightforward update. To make my life easier, I'm just going to add this into the 0.7.0 release (or shortly after), so I don't have to put in any extra effort to rebase it. Any PRs should be made on the 0.7.0 branch.