mirror of
https://github.com/eggjs/egg.git
synced 2024-12-04 07:14:30 +00:00
feat: add ip setter on request (#138)
closes https://github.com/eggjs/egg/issues/123
This commit is contained in:
parent
cd99a1f06a
commit
e646fedfa4
@ -398,4 +398,5 @@ delegate(proto, 'request')
|
||||
.getter('isAjax')
|
||||
.getter('acceptJSON')
|
||||
.getter('queries')
|
||||
.getter('accept');
|
||||
.getter('accept')
|
||||
.access('ip');
|
||||
|
||||
@ -71,12 +71,13 @@ module.exports = {
|
||||
},
|
||||
|
||||
/**
|
||||
* 返回远程 ip 地址,总是返回 ipv4
|
||||
* Request remote IPv4 address
|
||||
* @member {String} Request#ip
|
||||
* @example
|
||||
* ```js
|
||||
* this.request.ip
|
||||
* => '127.0.0.1'
|
||||
* => '111.10.2.1'
|
||||
* ```
|
||||
*/
|
||||
get ip() {
|
||||
@ -84,13 +85,20 @@ module.exports = {
|
||||
return this._ip;
|
||||
}
|
||||
const ip = this.ips[0] || this.socket.remoteAddress;
|
||||
// ::ffff:x.x.x.x/96 是用于IPv4映射地址
|
||||
// 如果是 IPV6 也不会做处理,现在还未遇到 IPV6 的场景
|
||||
// will be '::ffff:x.x.x.x', should conver to standard IPv4 format
|
||||
// https://zh.wikipedia.org/wiki/IPv6
|
||||
this._ip = ip && ip.indexOf('::ffff:') > -1 ? ip.substring(7) : ip;
|
||||
return this._ip;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the remote address
|
||||
* @param {String} ip - IPv4 address
|
||||
*/
|
||||
set ip(ip) {
|
||||
this._ip = ip;
|
||||
},
|
||||
|
||||
/**
|
||||
* 从请求头获取所有 ip
|
||||
* 1. 先从 `X-Forwarded-For` 获取,这个值是从 spanner 传递过来的,如果前置没有 spanner 返回为空
|
||||
|
||||
@ -5,8 +5,6 @@ const path = require('path');
|
||||
const mm = require('egg-mock');
|
||||
const request = require('supertest');
|
||||
const sleep = require('ko-sleep');
|
||||
|
||||
|
||||
const utils = require('../../utils');
|
||||
|
||||
describe('test/app/extend/context.test.js', () => {
|
||||
@ -389,4 +387,32 @@ describe('test/app/extend/context.test.js', () => {
|
||||
.should.match(/\[egg:background] task:mockError fail \(\d+ms\)/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ctx.ip', () => {
|
||||
let app;
|
||||
before(() => {
|
||||
app = utils.app('apps/demo');
|
||||
return app.ready();
|
||||
});
|
||||
after(() => app.close());
|
||||
afterEach(mm.restore);
|
||||
|
||||
it('should get current request ip', () => {
|
||||
return request(app.callback())
|
||||
.get('/ip')
|
||||
.expect(200)
|
||||
.expect({
|
||||
ip: '127.0.0.1',
|
||||
});
|
||||
});
|
||||
|
||||
it('should set current request ip', () => {
|
||||
return request(app.callback())
|
||||
.get('/ip?set_ip=10.2.2.2')
|
||||
.expect(200)
|
||||
.expect({
|
||||
ip: '10.2.2.2',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
10
test/fixtures/apps/demo/app/controller/ip.js
vendored
Normal file
10
test/fixtures/apps/demo/app/controller/ip.js
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function* () {
|
||||
if (this.query.set_ip) {
|
||||
this.ip = this.query.set_ip;
|
||||
}
|
||||
this.body = {
|
||||
ip: this.ip,
|
||||
};
|
||||
};
|
||||
1
test/fixtures/apps/demo/app/router.js
vendored
1
test/fixtures/apps/demo/app/router.js
vendored
@ -9,4 +9,5 @@ module.exports = app => {
|
||||
app.get('/user.json', function*() {
|
||||
this.jsonp = { name: 'fengmk2' };
|
||||
});
|
||||
app.get('/ip', app.controller.ip);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user