Permalink
Browse files

增加订购,退订,订购关系查询接口

  • Loading branch information...
chenshijie committed Mar 27, 2012
1 parent d655f65 commit 26ccac2016fd4eb40ddf1dce50199be6244b2bce
Showing with 310 additions and 88 deletions.
  1. +138 −88 gateway-server.js
  2. +136 −0 lib/auth.js
  3. +36 −0 lib/guidservice.js
View
@@ -9,10 +9,17 @@ var redis = require("redis");
var _logger = logger(__dirname + '/' + configs.log.file);
var express = require('express');
var app = express.createServer();
var _ = require('underscore');
//var _.str = require('underscore.string');
_.mixin(require('underscore.string'));
var guidServer = require('./lib/guidservice');
var guidService = guidServer.getGUIDService('127.0.0.1', 8081);
var authority = require('./lib/auth');
var auth = authority.getAuthority('211.136.109.36', 8081);
var wapIPList = [];
app.use(express.static(__dirname + '/public'));
fs.writeFileSync(__dirname + '/run/server.lock', process.pid.toString(), 'ascii');
@@ -24,118 +31,118 @@ client1.on('ready', function() {
});
var proxy2Nx = function(request, res, proxy_option) {
var body = '';
var headers = request.headers;
var body = '';
var headers = request.headers;
var options = {
host: proxy_option.host,
port: proxy_option.port,
path: request.url,
method: request.method,
headers: headers
};
var options = {
host: proxy_option.host,
port: proxy_option.port,
path: request.url,
method: request.method,
headers: headers
};
var req = http.request(options, function(response) {
//TODO 处理来源IP,参考http-proxy
response.setEncoding('binary');
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (response.headers['transfer-encoding'] != undefined) {
delete response.headers['transfer-encoding'];
}
if (response.headers['content-length'] != undefined) {
delete response.headers['content-length'];
}
response.headers['Content-Length'] = body.length;
res.writeHead(response.statusCode, response.headers);
res.end(body, 'binary');
var req = http.request(options, function(response) {
//TODO 处理来源IP,参考http-proxy
response.setEncoding('binary');
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (response.headers['transfer-encoding'] != undefined) {
delete response.headers['transfer-encoding'];
}
if (response.headers['content-length'] != undefined) {
delete response.headers['content-length'];
}
response.headers['Content-Length'] = body.length;
res.writeHead(response.statusCode, response.headers);
res.end(body, 'binary');
});
});
});
if (proxy_option.data != undefined) {
req.end(proxy_option.data, 'binary');
} else {
req.end('', 'binary');
}
};
var n4_proxy = function(request, res, data) {
_logger.info(['PROXY', request.url, request.headers['x-guid'], request.headers['x-sess'] || '-'].join("\t"));
// 设置目标地址和端口
var proxy_option = {
host: configs.n4.host,
port: configs.n4.port
if (proxy_option.data != undefined) {
req.end(proxy_option.data, 'binary');
} else {
req.end('', 'binary');
}
};
if (data) {
proxy_option.data = data;
}
proxy2Nx(request, res, proxy_option);
};
var n6_proxy = function(request, res, data) {
_logger.info(['PROXY', request.url, request.headers['x-guid'], request.headers['x-sess'] || '-'].join("\t"));
var guid = utils.getGUIDFromXGUID(request.headers['x-guid']);
if (false && '-' == guid) {
res.end(utils.getErrorMessage( - 1, 'NO_GUID'));
} else {
var n4_proxy = function(request, res, data) {
_logger.info(['PROXY', request.url, request.headers['x-guid'], request.headers['x-sess'] || '-'].join("\t"));
// 设置目标地址和端口
var proxy_option = {
host: configs.n6.host,
port: configs.n6.port
host: configs.n4.host,
port: configs.n4.port
};
if (data) {
proxy_option.data = data;
}
guidService.getIDCodeByGUID(guid, function(result) {
console.log(guid);
if (!result.error) {
request.headers['x-up-calling-line-id'] = String(result.idcode);
} else {
request.headers['x-up-calling-line-id'] = '';
}
console.log(result);
console.log(request.headers['x-up-calling-line-id']);
proxy2Nx(request, res, proxy_option);
})
}
};
proxy2Nx(request, res, proxy_option);
};
var isFirstVisit = function(guid, callback) {
var daytime = utils.getDateString();
var key = daytime + ':' + guid;
client1.select(1);
client1.get(key, function(err, replies) {
if (null === replies) {
var date = new Date();
var timeleft = (24 - date.getHours()) * 3600 - date.getMinutes() * 60;
client1.setex(key, timeleft, 1);
callback(true);
var n6_proxy = function(request, res, data) {
_logger.info(['PROXY', request.url, request.headers['x-guid'], request.headers['x-sess'] || '-'].join("\t"));
var guid = utils.getGUIDFromXGUID(request.headers['x-guid']);
if (false && '-' == guid) {
res.end(utils.getErrorMessage(-1, 'NO_GUID'));
} else {
callback(false);
// 设置目标地址和端口
var proxy_option = {
host: configs.n6.host,
port: configs.n6.port
};
if (data) {
proxy_option.data = data;
}
guidService.getIDCodeByGUID(guid, function(result) {
console.log(guid);
if (!result.error) {
request.headers['x-up-calling-line-id'] = String(result.idcode);
} else {
request.headers['x-up-calling-line-id'] = '';
}
console.log(result);
console.log(request.headers['x-up-calling-line-id']);
proxy2Nx(request, res, proxy_option);
})
}
});
};
};
var isFirstVisit = function(guid, callback) {
var daytime = utils.getDateString();
var key = daytime + ':' + guid;
client1.select(1);
client1.get(key, function(err, replies) {
if (null === replies) {
var date = new Date();
var timeleft = (24 - date.getHours()) * 3600 - date.getMinutes() * 60;
client1.setex(key, timeleft, 1);
callback(true);
} else {
callback(false);
}
});
};
/**
* 获取请求来源IP
*/
var getRealIP = function(request) {
if (undefined !== request.META && undefined !== request.META['HTTP_X_FORWARDED_FOR']) {
return request.META['HTTP_X_FORWARDED_FOR'];
} else {
return request.connection.remoteAddress;
}
};
if (undefined !== request.META && undefined !== request.META['HTTP_X_FORWARDED_FOR']) {
return request.META['HTTP_X_FORWARDED_FOR'];
} else {
return request.connection.remoteAddress;
}
};
/**
* 判断来源IP是否来自WAP网关
* @param String ip 来源IP
* @return Boolean
*/
var isComeFromWAPNet = function(ip) {
return _.include(wapIPList, ip);
};
return _.include(wapIPList, ip);
};
/**
* 处理客户端到N6的GET方式请求
@@ -206,7 +213,7 @@ app.get('/N4/:uri', function(req, res) {
console.log(req.url);
var msisdn = '';
if (undefined != req.headers['x-up-calling-line-id']) {
msisdn = req.headers['x-up-calling-line-id'].substr( - 11);
msisdn = req.headers['x-up-calling-line-id'].substr(-11);
}
console.log(msisdn);
var options = {
@@ -279,7 +286,50 @@ app.post('/getGUID', function(req, res) {
request.end(body, 'binary');
});
});
var isEmpty = function(value) {
var temp = _.trim(value);
return (temp == undefined || temp == '');
};
app.get('/Auth/:idcode/Order', function(req, res) {
var action = req.query.action;
var idcode = req.params.idcode;
var ac = req.query.ac;
var mc = req.query.mc;
var mt = req.query.mt;
var ua = req.query.ua;
var ak = req.query.ak;
var ip = req.query.ip;
if (!_.include(['list', 'order', 'cancel'], action)) {
res.end('action error!')
} else if (isEmpty(ac) || isEmpty(mc) || isEmpty(mt) || isEmpty(ua) || isEmpty(ak) || isEmpty(ip)) {
res.end('参数错误');
} else {
guidService.getMSISDNByIDCode(idcode, function(result) {
if (!result.error && !isEmpty(result.msisdn)) {
if (action == 'list') {
auth.getOrders(result.msisdn, ac, mc, mt, ua, ak, ip, function(authResult){
res.end(JSON.stringify(authResult));
});
}
if (action == 'order') {
auth.doOrder(result.msisdn, ac, mc, mt, ua, ak, ip, function(authResult){
res.end(JSON.stringify(authResult));
});
}
if (action == 'cancel') {
auth.doCancelOrder(result.msisdn, ac, mc, mt, ua, ak, ip, function(authResult){
res.end(JSON.stringify(authResult));
});
}
} else {
res.end('用户未绑定手机号');
}
});
}
});
console.log(configs.service_port);
app.listen(configs.service_port);
console.log('Service Started ' + utils.getLocaleISOString());
Oops, something went wrong.

0 comments on commit 26ccac2

Please sign in to comment.