//获取真实ip
//是否有反向代理IP(头信息:x-forwarded-for),在判断connection的远程IP,以及后端的socket的IP
let getClientIp = function(req) {
return req.headers['X-Real-IP'] ||
req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress ||
req.ip || '';
};
let ip = getClientIp(req).match(/\d+.\d+.\d+.\d+/);
ip = ip ? ip.join('.') : null;
console.log('真实ip', ip);
网友评论