Skip to content

Commit

Permalink
Support for Google Cloud App Engine (x-appengine-user-ip)
Browse files Browse the repository at this point in the history
  • Loading branch information
floris-hatsa committed Nov 6, 2019
1 parent 915d984 commit 1e8be3a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getClientIp(req) {

// Server is probably behind a proxy.
if (req.headers) {

// Standard headers used by Amazon EC2, Heroku, and others.
if (is.ip(req.headers['x-client-ip'])) {
return req.headers['x-client-ip'];
Expand Down Expand Up @@ -101,6 +101,14 @@ function getClientIp(req) {
if (is.ip(req.headers.forwarded)) {
return req.headers.forwarded;
}

// Google Cloud App Engine
// https://cloud.google.com/appengine/docs/standard/go/reference/request-response-headers

if (is.ip(req.headers['x-appengine-user-ip'])) {
return req.headers['x-appengine-user-ip'];
}

}

// Remote address checks.
Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,28 @@ test('android request to AWS EBS app (x-forwarded-for)', (t) => {
});
});
});

test('request to Google Cloud App Engine (x-appengine-user-ip)', (t) => {
t.plan(1);
const wanted = '107.77.213.113';
const options = {
url: '',
headers: {
host: '[redacted]',
'x-appengine-user-ip': '107.77.213.113',
},
};

const server = new ServerFactory();
server.listen(0, serverInfo.host);
server.on('listening', () => {
options.url = `http://${serverInfo.host}:${server.address().port}`;
request(options, (error, response, found) => {
if (!error && response.statusCode === 200) {
// ip address should be equal to the first "x-appengine-user-ip" value
t.equal(found, wanted);
server.close();
}
});
});
});

0 comments on commit 1e8be3a

Please sign in to comment.