Skip to content

Commit

Permalink
Merge pull request #85 from zhuangya/global
Browse files Browse the repository at this point in the history
reopen #69
  • Loading branch information
codingfishman authored Sep 1, 2016
2 parents 207a20a + cc71812 commit 70d0e39
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function Recorder(option){
mime : "",
content : ""
};
GLOBAL.recorder.getSingleRecord(id,function(err,doc){
global.recorder.getSingleRecord(id,function(err,doc){
//check whether this record exists
if(!doc || !doc[0]){
cb(new Error("failed to find record for this id"));
Expand Down Expand Up @@ -230,4 +230,4 @@ function normalizeInfo(id,info){
return singleRecord;
}

module.exports = Recorder;
module.exports = Recorder;
26 changes: 13 additions & 13 deletions lib/requestHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var http = require("http"),
https = require("https"),
https = require("https"),
net = require("net"),
fs = require("fs"),
url = require("url"),
Expand Down Expand Up @@ -46,8 +46,8 @@ function userRequestHandler(req,userRes){
req : req,
startTime : new Date().getTime()
};
if(GLOBAL.recorder){
resourceInfoId = GLOBAL.recorder.appendRecord(resourceInfo);
if(global.recorder){
resourceInfoId = global.recorder.appendRecord(resourceInfo);
}

logUtil.printLog(color.green("\nreceived request to : " + host + path));
Expand All @@ -59,7 +59,7 @@ function userRequestHandler(req,userRes){
],function(){
//mark some ext info
if(req.anyproxy_map_local){
GLOBAL.recorder.updateExtInfo(resourceInfoId, {map : req.anyproxy_map_local});
global.recorder.updateExtInfo(resourceInfoId, {map : req.anyproxy_map_local});
}
});

Expand All @@ -72,7 +72,7 @@ function userRequestHandler(req,userRes){
req.on("end",function(){
reqData = Buffer.concat(postData);
resourceInfo.reqBody = reqData.toString();
GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);

callback();
});
Expand Down Expand Up @@ -103,7 +103,7 @@ function userRequestHandler(req,userRes){
resourceInfo.length = resBody ? resBody.length : 0;
resourceInfo.statusCode = statusCode;

GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);

userRes.writeHead(statusCode,resHeader);
userRes.end(resBody);
Expand Down Expand Up @@ -212,10 +212,10 @@ function userRequestHandler(req,userRes){

//send response
},function(callback){
if(GLOBAL._throttle){
if(global._throttle){
var thrStream = new Stream();

var readable = thrStream.pipe(GLOBAL._throttle.throttle());
var readable = thrStream.pipe(global._throttle.throttle());
readable.pipe(userRes);

thrStream.emit("data",serverResData);
Expand All @@ -234,7 +234,7 @@ function userRequestHandler(req,userRes){
resourceInfo.resBody = serverResData;
resourceInfo.length = serverResData ? serverResData.length : 0;

GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);

callback();

Expand Down Expand Up @@ -294,7 +294,7 @@ function connectReqHandler(req, socket, head){
req : req,
startTime : new Date().getTime()
};
resourceInfoId = GLOBAL.recorder.appendRecord(resourceInfo);
resourceInfoId = global.recorder.appendRecord(resourceInfo);

var proxyPort,
proxyHost,
Expand Down Expand Up @@ -346,8 +346,8 @@ function connectReqHandler(req, socket, head){
socket.write('HTTP/' + req.httpVersion + ' 200 OK\r\n\r\n', 'UTF-8', function(){

//throttle for direct-foward https
if(GLOBAL._throttle && !shouldIntercept ){
var readable = conn.pipe(GLOBAL._throttle.throttle());
if(global._throttle && !shouldIntercept ){
var readable = conn.pipe(global._throttle.throttle());
readable.pipe(socket);
socket.pipe(conn);
}else{
Expand All @@ -374,7 +374,7 @@ function connectReqHandler(req, socket, head){
resourceInfo.resBody = "";
resourceInfo.length = 0;

GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);

callback();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/webInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function webInterface(config){
app.get("/fetchBody",function(req,res){
var query = req.query;
if(query && query.id){
GLOBAL.recorder.getDecodedBody(query.id, function(err, result){
global.recorder.getDecodedBody(query.id, function(err, result){
if(err || !result || !result.content){
res.json({});
}else if(result.type && result.type == "image" && result.mime){
Expand Down Expand Up @@ -154,4 +154,4 @@ function webInterface(config){

inherits(webInterface, events.EventEmitter);

module.exports = webInterface;
module.exports = webInterface;
6 changes: 3 additions & 3 deletions lib/wsServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function resToMsg(msg,cb){

if(jsonData.type == "reqBody" && jsonData.id){
result.type = "body";
GLOBAL.recorder.getBody(jsonData.id, function(err, data){
global.recorder.getBody(jsonData.id, function(err, data){
if(err){
result.content = {
id : null,
Expand Down Expand Up @@ -74,7 +74,7 @@ function wsServer(config){

wss.on("close",function(){});

GLOBAL.recorder.on("update",function(data){
global.recorder.on("update",function(data){
try{
wss && wss.broadcast({
type : "update",
Expand All @@ -95,4 +95,4 @@ wsServer.prototype.closeAll = function(){
self.wss.close();
}

module.exports = wsServer;
module.exports = wsServer;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"test": "sh test/test.sh",
"testserver": "node test/server/startServer.js"
},
"optionalDependencies": {},
"repository": {
"type": "git",
"url": "https://github.com/alibaba/anyproxy"
Expand Down
8 changes: 4 additions & 4 deletions proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try{
GLOBAL.util = require('./lib/util');
global.util = require('./lib/util');
}catch(e){}

var http = require('http'),
Expand Down Expand Up @@ -100,7 +100,7 @@ function proxyServer(option){
logUtil.printLog(color.red('Invalid throttle rate value, should be positive integer\n'), logUtil.T_ERR);
process.exit(0);
}
GLOBAL._throttle = new ThrottleGroup({rate: 1024 * parseFloat(option.throttle) }); // rate - byte/sec
global._throttle = new ThrottleGroup({rate: 1024 * parseFloat(option.throttle) }); // rate - byte/sec
}

self.httpProxyServer = null;
Expand All @@ -111,9 +111,9 @@ function proxyServer(option){
function(callback){
util.clearCacheDir(function(){
if(option.dbFile){
GLOBAL.recorder = new Recorder({filename: option.dbFile});
global.recorder = new Recorder({filename: option.dbFile});
}else{
GLOBAL.recorder = new Recorder();
global.recorder = new Recorder();
}
callback();
});
Expand Down

0 comments on commit 70d0e39

Please sign in to comment.