feat(web-server): 修改IP获取方式为xff优先

This commit is contained in:
liangtongchuan
2023-05-12 22:39:06 +08:00
committed by luying
parent be4541e02d
commit 7bebb83140

View File

@@ -2,10 +2,17 @@ import { Context } from 'egg';
module.exports = () => {
return async function parmsDecode(ctx: Context, next) {
let xTrueIp = ctx.header['x-true-ip'] && (typeof ctx.header['x-true-ip'] == 'string'? ctx.header['x-true-ip']: ctx.header['x-true-ip'][0]);
let xRealIp = ctx.header['x-real-ip'] && (typeof ctx.header['x-real-ip'] == 'string'? ctx.header['x-real-ip']: ctx.header['x-real-ip'][0]);
ctx.clientIp = xTrueIp||xRealIp||ctx.request.ip;
console.log('ip ', ctx.uid, ctx.request.url, ctx.clientIp);
let clientIp = null;
if (ctx.header['x-forwarded-for'] && (typeof ctx.header['x-forwarded-for'] == 'string')) {
clientIp = ctx.header['x-forwarded-for'].split(',')[0];
console.log('***** xff', clientIp);
} else {
clientIp = ctx.header['x-real-ip'] && (typeof ctx.header['x-real-ip'] == 'string'? ctx.header['x-real-ip']: ctx.header['x-real-ip'][0]);
console.log('***** real', clientIp);
}
ctx.clientIp = clientIp||ctx.request.ip;
console.log('***** clientIp', ctx.clientIp);
await next();
};
};