Permalink
Browse files

update route

  • Loading branch information...
1 parent a3d08c6 commit 5b9ed79ad130f04014c4bc62d57f780a7a9972fc @rehorn committed Apr 10, 2013
View
@@ -2,7 +2,6 @@
var http = require('http'),
https = require('https'),
httpProxy = require('http-proxy'),
- proxy = new httpProxy.RoutingProxy(),
fs = require('fs'),
net = require('net'),
path = require('path'),
@@ -56,16 +55,14 @@ livepool.run = function (){
var reqInfo = request.getReqInfo(req);
var handler = config.getHandler(reqInfo);
var reqUrl = reqInfo.url;
+ // local replacement
if(handler && (responder = responder[handler.respond.type])){
logger.log('req handler [ ' + handler.respond.type.grey + ' ]: ' + reqUrl.grey);
responder(handler, req, res);
}else{
- logger.log('req proxied: ' + reqUrl.grey);
- var host = req.headers.host;
- proxy.proxyRequest(req, res, {
- host: proxyAgent[0] || host,
- port: proxyAgent[1] || 80
- });
+ // remote route
+ responder = responder['route'];
+ responder(null, req, res);
}
}).listen(httpPort);
View
@@ -33,6 +33,23 @@ config.rulesGroup = [];
config.handlers = [];
config.routers = [];
+function parseUrl(urlString){
+
+ var pathname = '/', hostname = '';
+ // 处理match,获取pathname
+ var urlpath = urlString.match(/http[s]?/) ? urlString : 'http://' + urlString;
+ var urlInfo = url.parse(urlpath.replace(/\*/g, '__ls__'));
+ hostname = urlInfo.hostname.replace(/__ls__/g, '\*');
+ pathname = urlInfo.pathname.replace(/__ls__/g, '\*');
+ pathname = pathname.indexOf('*') > -1 ? pathname.substring(0, pathname.indexOf('*')) : pathname;
+ pathname = pathname.substring(0, pathname.lastIndexOf('/') + 1);
+
+ return {
+ hostname: hostname,
+ pathname: pathname
+ };
+}
+
config.loadRules = function(){
var rulePath = 'rules/';
var dirList = fs.readdirSync(rulePath);
@@ -54,22 +71,17 @@ config.loadRules = function(){
var handler = rules.handler;
handler.forEach(function(item){
item.match = item.match || '';
- var pathname = '/';
var split = item.match.split(/\s+/);
split.forEach(function(match){
- // 处理match,获取pathname
- var urlpath = match.match(/http[s]?/) ? match : 'http://' + match;
- pathname = url.parse(urlpath.replace(/\*/g, '__ls__')).pathname.replace(/__ls__/g, '\*');
- pathname = pathname.indexOf('*') > -1 ? pathname.substring(0, pathname.indexOf('*')) : pathname;
- pathname = pathname.substring(0, pathname.lastIndexOf('/') + 1);
-
+ var urlInfo = parseUrl(match);
config.handlers.push({
base: rules.base,
match: match.replace(/\./g, '\\.').replace(/\*/g, '.*'),
matchRaw: match,
- pathname: pathname,
action: item.action,
actionRaw: item.action,
+ hostname: urlInfo.hostname,
+ pathname: urlInfo.pathname,
enabled: item.enabled
});
});
@@ -80,18 +92,23 @@ config.loadRules = function(){
router.forEach(function(item){
item.match = item.match || '';
var split = item.match.split(/\s+/);
+ var pathname = '/', hostname = '';
split.forEach(function(match){
+ var urlInfo = parseUrl(match);
config.routers.push({
match: match.replace(/\./g, '\\.').replace(/\*/g, '.*'),
matchRaw: match,
action: item.action,
actionRaw: item.action,
+ hostname: urlInfo.hostname,
+ pathname: urlInfo.pathname,
enabled: item.enabled
});
});
});
});
- console.log(config.handlers)
+ // console.log(config.handlers)
+ console.log(config.routers);
};
config.updateRules = function(rules){
@@ -130,7 +147,16 @@ config.getHandler = function(reqInfo){
};
config.getRouter = function(reqInfo){
-
+ var urlRaw = reqInfo.url;
+ var reqUrl = url.parse(urlRaw);
+ var pathname = reqUrl.pathname;
+
+ return _.find(config.routers, function(router){
+ if(new RegExp(router.match).test(urlRaw)){
+ return true;
+ }
+ return false;
+ });
};
@@ -1,5 +1,5 @@
exports.combo = require('./combo');
exports.local = require('./local');
-exports.proxy = require('./proxy');
exports.qzmin = require('./qzmin');
exports.remote = require('./remote');
+exports.proxy = require('./route');
No changes.
@@ -0,0 +1,35 @@
+
+var config = require('../config'),
+ request = request('../request');
+
+var proxy = new httpProxy.RoutingProxy();
+
+var global = config.global,
+ proxyAgent = global.proxy || '',
+ proxyAgent = proxyAgent.split(':');
+
+function routeResponder(router, req, res){
+ logger.log('req proxied: ' + reqUrl.grey);
+
+ var host = req.headers.host;
+ var reqInfo = request.getReqInfo(req);
+ router = router || config.getRouter(reqInfo);
+
+ if(router){
+ // proxy to specified server
+ if(router)
+ }else{
+ // directly proxy
+ goProxy(req, res, proxyAgent[0], proxyAgent[1]);
+ proxy.proxyRequest(req, res, {
+ host: proxyAgent[0] || host,
+ port: proxyAgent[1] || 80
+ });
+ }
+};
+
+function goProxy = function(req, res, host, port){
+
+};
+
+module.exports = routeResponder;

0 comments on commit 5b9ed79

Please sign in to comment.