Files
ZYZ/web-server/app/middleware/getIp.ts
2026-03-13 01:38:40 +00:00

20 lines
686 B
TypeScript

import { Context } from 'egg';
module.exports = () => {
return async function parmsDecode(ctx: Context, next: () => Promise<any>) {
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();
};
};