Permalink
Browse files

add proxy interface

add getGUID inteface
  • Loading branch information...
chenshijie committed Feb 22, 2012
1 parent 2e58375 commit e0d37f2b4e69e74b325fffe9f10cc6088200f6eb
Showing with 287 additions and 0 deletions.
  1. +26 −0 etc/settings.original.json
  2. +229 −0 gateway-server.js
  3. +32 −0 lib/utils.js
View
26 etc/settings.original.json 100644 → 100755
@@ -0,0 +1,26 @@
{
"service_port":8080,
"redis": {
"host": "127.0.0.1",
"port": 6379,
"db" : 3
},
"mysql": {
"host": "127.0.0.1",
"port": 3306,
"username": "root",
"password": "",
"database": "wlan"
},
"log" : {
"file" : "log/server.log"
},
"n4": {
"host": "127.0.0.1",
"port": "8088"
},
"n6": {
"host": "127.0.0.1",
"port": "8098"
}
}
View
@@ -0,0 +1,229 @@
var logger = require('./lib/logger').logger;
var utils = require('./lib/utils');
var configs = require('./etc/settings.json');
var _logger = logger(__dirname + '/' + configs.log.file);
var fs = require('fs');
var _logger = logger(__dirname + '/' + configs.log.file);
var express = require('express');
var app = express.createServer();
app.use(express.static(__dirname + '/public'));
fs.writeFileSync(__dirname + '/run/server.lock', process.pid.toString(), 'ascii');
var http = require('http');
// Redis对象
var redis = require("redis");
// Redis对象,用户保存用户首次登陆
var client1 = redis.createClient(configs.redis.port, configs.redis.host);
client1.on('ready', function() {
client1.select(configs.redis.db);
});
var nn = function(request, res, proxy_option) {
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 req = http.request(options, function(response) {
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 nx_proxy = function(request, res, data, need_emit) {
_logger.info([ 'PROXY', request.url, request.headers['x-guid'], request.headers['x-sess'] || '-' ].join("\t"));
var guid = utils.getGUIDFromXGUID(request.headers['x-guid']);
if ('-' === guid) {
_logger.debug([ 'GUIDERROR', request.headers['x-guid'], request.headers['x-ak'] || '-' ].join("\t"));
res.send('no guid');
} else {
// 设置目标地址和端口
var proxy_option = {
host : settings.n5.host,
port : settings.n5.port
};
if (!_.isNull(/\/N4\//.exec(request.url))) {
proxy_option.port = settings.n4.port;
proxy_option.host = settings.n4.host;
}
if (data) {
proxy_option.data = data;
}
// console.log(proxy_option);
if (request.headers['x-sess']) {
var session = request.headers['x-sess'];
client.get('sess:' + session, function(err, replies) {
if (null === replies) {
getMobile(guid, function(mobile) {
delete request.headers['x-sess'];
request.headers['x-up-calling-line-id'] = String(mobile);
nn(request, res, proxy_option);
});
} else {
client.expire('sess:' + session, 1800);
user.find(replies, function(result) {
// console.log("MOBILE FROM DB: " + result[0].mobile);
if (result.length == 0 || undefined == result[0].mobile || null === result[0].mobile || result[0].mobile == '') {
getMobile(guid, function(mobile) {
// console.log("MOBILE FROM REDIS: " + mobile);
request.headers['x-up-calling-line-id'] = String(mobile);
nn(request, res, proxy_option);
});
} else {
request.headers['x-up-calling-line-id'] = String(result[0].mobile);
nn(request, res, proxy_option);
}
});
}
});
} else {
getMobile(guid, function(mobile) {
request.headers['x-up-calling-line-id'] = String(mobile);
nn(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);
}
});
};
// 处理N6请求
app.get('/N6/:uri', function(req, res) {
var guid = utils.getGUIDFromXGUID(req.headers['x-guid']);
isFirstVisit(guid, function(firstVisit) {
if (firstVisit) {
req.headers['x-firstvisit'] = 'true';
} else {
req.headers['x-firstvisit'] = 'false';
}
nx_proxy(req, res, '');
});
});
app.post('/N6/:uri', function(req, res) {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
var guid = utils.getGUIDFromXGUID(req.headers['x-guid']);
isFirstVisit(guid, function(firstVisit) {
if (firstVisit) {
req.headers['x-firstvisit'] = 'true';
} else {
req.headers['x-firstvisit'] = 'false';
}
nx_proxy(req, res, body);
});
});
});
// 处理N4请求
app.post('/N4/:uri', function(req, res) {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
var guid = utils.getGUIDFromXGUID(req.headers['x-guid']);
isFirstVisit(guid, function(firstVisit) {
if (firstVisit) {
req.headers['x-firstvisit'] = 'true';
} else {
req.headers['x-firstvisit'] = 'false';
}
nx_proxy(req, res, body);
});
});
});
app.get('/N4/:uri', function(req, res) {
var guid = utils.getGUIDFromXGUID(req.headers['x-guid']);
isFirstVisit(guid, function(firstVisit) {
if (firstVisit) {
req.headers['x-firstvisit'] = 'true';
} else {
req.headers['x-firstvisit'] = 'false';
}
nx_proxy(req, res, '');
});
});
app.post('/getGUID', function(req, res) {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
var headers = req.headers;
var options = {
host : '127.0.0.1',
port : 8081,
path : req.url,
method : req.method,
headers : headers
};
var request = http.request(options, function(response) {
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');
});
});
request.end(body, 'binary');
});
});
app.listen(configs.service_port);
console.log('Service Started ' + utils.getLocaleISOString());
View
32 lib/utils.js 100644 → 100755
@@ -39,4 +39,36 @@ exports.getLocaleISOString = function() {
return i;
};
return [ date.getFullYear(), pad(date.getMonth() + 1), pad(date.getDate()) ].join('-') + ' ' + [ pad(date.getHours()), pad(date.getMinutes()), pad(date.getSeconds()) ].join(':') + '.' + pad3(date.getMilliseconds());
};
exports.getGUIDFromXGUID = function(xguid) {
var guid = xguid || '-';
if (xguid == undefined) {
return '-';
}
guid = xguid.substr(0, 34);
if (guid.length < 34) {
return '-';
} else {
var v = guid.split('');
var xor_value = 0;
var xor_sum = parseInt(v[32] + v[33], 16);
for ( var i = 0; i < 32; i += 2) {
xor_value = xor_value ^ parseInt(v[i] + v[i + 1], 16);
}
if (xor_sum == xor_value) {
return guid;
} else {
return '-';
}
}
};
exports.genCheckSum = function(short_guid) {
var xor_value = 0;
var v = short_guid.split('');
for ( var i = 0; i < 32; i += 2) {
xor_value = xor_value ^ parseInt(v[i] + v[i + 1], 16);
}
return xor_value.toString(16);
};

0 comments on commit e0d37f2

Please sign in to comment.