Authored by 陈峰

private ips

... ... @@ -33,6 +33,12 @@ app.locals.startTime = moment().format('YYYYMMDDHH');
ynLib.global(config);
logger = global.yoho.logger;
// zookeeper
if (config.zookeeperServer) {
require('yoho-zookeeper')(config.zookeeperServer, 'yap', app.locals.yap = {}, false, {});
}
global.Promise = bluebird;
global.yoho.utils = {
mysqlCli: new SqlHelper(config.mysql.database),
... ... @@ -80,9 +86,11 @@ try {
origin: config.corsAllowOrigin
}));
const setYohoData = require('./doraemon/middleware/set-yoho-data');
const errorHandler = require('./doraemon/middleware/error-handler');
const devtools = require('./doraemon/middleware/devtools');
app.use(setYohoData());
if (app.locals.devEnv) {
app.use(devtools());
}
... ...
... ... @@ -35,6 +35,7 @@ module.exports = {
business_line: 'yohobuy'
}
},
zookeeperServer: '127.0.0.1:2181',
corsAllowOrigin: [
'http://localhost:8081',
'http://localhost:8888',
... ... @@ -150,6 +151,7 @@ if (isProduction) {
'https://huodong.yoho.cn',
'http://ad.yhurl.com'
],
zookeeperServer: 'web.zookeeper.yohoops.org:2181',
redis: {
connect: {
host: 'redis.web.yohoops.org',
... ...
... ... @@ -10,18 +10,14 @@ const user = require('./apps/user');
const passport = require('./apps/passport');
const activity = require('./apps/activity');
const authAdmin = require('./doraemon/middleware/authAdmin');
const privateEnv = process.env.PRIVATE_ENV;
const privateRoute = require('./doraemon/middleware/private-route');
module.exports = app => {
// api
app.use('/api', api);
if (privateEnv) {
// 管理
app.use('/admin', authAdmin, admin);
}
// 管理
app.use('/admin', privateRoute, authAdmin, admin);
// 验证
app.use('/passport', passport);
... ...
const _ = require('lodash');
const logger = global.yoho.logger;
module.exports = (req, res, next) => {
try {
const privateIps = _.get(req, 'app.locals.yap.json.privateips', []);
if (_.includes(privateIps, req.yoho.clientIp)) {
return next();
}
} catch (error) {
logger.error('[privateips error], ', error);
}
return res.status(401).end();
};
... ...
/**
* 设置 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();
};
};
... ...
This diff could not be displayed because it is too large.
... ... @@ -82,7 +82,7 @@
"xlsx": "^0.11.16",
"yoho-md5": "^2.0.0",
"yoho-node-lib": "^0.6.23",
"yoho-zookeeper": "^1.0.8"
"yoho-zookeeper": "^1.0.11"
},
"devDependencies": {
"autoprefixer": "^7.0.1",
... ...
... ... @@ -11077,8 +11077,8 @@ yoho-swiper@^3.3.1:
version "3.3.2"
resolved "http://npm.yohops.com/yoho-swiper/-/yoho-swiper-3.3.2.tgz#a7b0f6e13de38d8ac8bb90badbbc316889a0c214"
yoho-zookeeper@^1.0.8:
version "1.0.8"
resolved "http://npm.yohops.com/yoho-zookeeper/-/yoho-zookeeper-1.0.8.tgz#a6def330c27e46791f9a61726370671bec7358c9"
yoho-zookeeper@^1.0.11:
version "1.0.11"
resolved "http://npm.yohops.com/yoho-zookeeper/-/yoho-zookeeper-1.0.11.tgz#a4fe2a7176de91971be4f03c9552720b93a9ffe3"
dependencies:
node-zookeeper-client "^0.2.2"
... ...