|
|
/**
|
|
|
* 设置 YOHO 数据
|
|
|
* @author: 赵彪<bill.zhao@yoho.cn>
|
|
|
* @date: 2016/6/16
|
|
|
*/
|
|
|
|
|
|
'use strict';
|
|
|
const _ = require('lodash');
|
|
|
const net = require('net');
|
|
|
|
|
|
/**
|
|
|
* 获取 IP
|
|
|
* @param {*} req
|
|
|
*/
|
|
|
const _getClientIp = req => {
|
|
|
let remoteIp = req.get('X-Yoho-Real-IP') || req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip;
|
|
|
|
|
|
if (remoteIp.indexOf(',') > 0) {
|
|
|
let arr = remoteIp.split(',');
|
|
|
|
|
|
remoteIp = _.trim(arr[0]);
|
|
|
}
|
|
|
|
|
|
if (_.startsWith(remoteIp, '10.66.')) {
|
|
|
remoteIp = req.get('X-Real-IP');
|
|
|
}
|
|
|
|
|
|
remoteIp = _.trim(remoteIp);
|
|
|
|
|
|
if (!net.isIPv4(remoteIp)) {
|
|
|
let ipv6String = remoteIp.split(':');
|
|
|
|
|
|
remoteIp = ipv6String[ipv6String.length - 1];
|
|
|
}
|
|
|
|
|
|
return remoteIp;
|
|
|
};
|
|
|
|
|
|
module.exports = () => {
|
|
|
return (req, res, next) => {
|
|
|
let yoho = Object.assign({
|
|
|
pageChannel: {}
|
|
|
}, req.yoho || {});
|
|
|
|
|
|
// IP 地址
|
|
|
yoho.clientIp = _getClientIp(req);
|
|
|
|
|
|
|
|
|
Object.assign(res.locals, yoho);
|
|
|
Object.assign(req.yoho, yoho);
|
|
|
|
|
|
next();
|
|
|
};
|
|
|
}; |
...
|
...
|
|