From 4144beecc14c2f3eb7354447fac9ea9774937e79 Mon Sep 17 00:00:00 2001
From: Ya Zhuang <zhuangya@bytedance.com>
Date: Tue, 31 May 2016 14:13:24 +0800
Subject: [PATCH 1/3] change: discard decrpecated GLOBAL

---
 lib/recorder.js       |  4 ++--
 lib/requestHandler.js | 32 +++++++++++++++++++++-----------
 lib/webInterface.js   |  4 ++--
 lib/wsServer.js       |  6 +++---
 proxy.js              |  8 ++++----
 5 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/lib/recorder.js b/lib/recorder.js
index 06100b3..9917163 100644
--- a/lib/recorder.js
+++ b/lib/recorder.js
@@ -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"));
@@ -230,4 +230,4 @@ function normalizeInfo(id,info){
     return singleRecord;
 }
 
-module.exports = Recorder;
\ No newline at end of file
+module.exports = Recorder;
diff --git a/lib/requestHandler.js b/lib/requestHandler.js
index 2f79d83..fa12692 100644
--- a/lib/requestHandler.js
+++ b/lib/requestHandler.js
@@ -1,5 +1,5 @@
 var http           = require("http"),
-    https          = require("https"),
+  https          = require("https"),
     net            = require("net"),
     fs             = require("fs"),
     url            = require("url"),
@@ -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));
@@ -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});
         }
     });
 
@@ -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();
         });
@@ -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);
@@ -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);
@@ -233,8 +233,13 @@ function userRequestHandler(req,userRes){
                         resourceInfo.resHeader  = resHeader;
                         resourceInfo.resBody    = serverResData;
                         resourceInfo.length     = serverResData ? serverResData.length : 0;
+<<<<<<< HEAD
 
                         GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
+=======
+                        
+                        global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);
+>>>>>>> 6521604... change: discard decrpecated GLOBAL
 
                         callback();
 
@@ -294,7 +299,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,
@@ -346,8 +351,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{
@@ -373,8 +378,13 @@ function connectReqHandler(req, socket, head){
             resourceInfo.resHeader  = {};
             resourceInfo.resBody    = "";
             resourceInfo.length     = 0;
+<<<<<<< HEAD
 
             GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
+=======
+            
+            global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);
+>>>>>>> 6521604... change: discard decrpecated GLOBAL
 
             callback();
         }
diff --git a/lib/webInterface.js b/lib/webInterface.js
index 3e74597..cc616ea 100644
--- a/lib/webInterface.js
+++ b/lib/webInterface.js
@@ -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){
@@ -154,4 +154,4 @@ function webInterface(config){
 
 inherits(webInterface, events.EventEmitter);
 
-module.exports = webInterface;
\ No newline at end of file
+module.exports = webInterface;
diff --git a/lib/wsServer.js b/lib/wsServer.js
index d56fb4a..81da63a 100644
--- a/lib/wsServer.js
+++ b/lib/wsServer.js
@@ -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,
@@ -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",
@@ -95,4 +95,4 @@ wsServer.prototype.closeAll = function(){
     self.wss.close();
 }
 
-module.exports = wsServer;
\ No newline at end of file
+module.exports = wsServer;
diff --git a/proxy.js b/proxy.js
index 85b9411..8f566d7 100644
--- a/proxy.js
+++ b/proxy.js
@@ -1,5 +1,5 @@
 try{
-    GLOBAL.util = require('./lib/util');
+    global.util = require('./lib/util');
 }catch(e){}
 
 var http = require('http'),
@@ -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;
@@ -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();
                 });

From cc7181244564486ba55f7a42a8a4cff84e190bba Mon Sep 17 00:00:00 2001
From: Ya Zhuang <zhuangya@bytedance.com>
Date: Tue, 31 May 2016 14:17:57 +0800
Subject: [PATCH 2/3] chore: npm testing script

---
 lib/requestHandler.js | 10 ----------
 package.json          |  1 -
 2 files changed, 11 deletions(-)

diff --git a/lib/requestHandler.js b/lib/requestHandler.js
index fa12692..f0f9a7e 100644
--- a/lib/requestHandler.js
+++ b/lib/requestHandler.js
@@ -233,13 +233,8 @@ function userRequestHandler(req,userRes){
                         resourceInfo.resHeader  = resHeader;
                         resourceInfo.resBody    = serverResData;
                         resourceInfo.length     = serverResData ? serverResData.length : 0;
-<<<<<<< HEAD
 
-                        GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
-=======
-                        
                         global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);
->>>>>>> 6521604... change: discard decrpecated GLOBAL
 
                         callback();
 
@@ -378,13 +373,8 @@ function connectReqHandler(req, socket, head){
             resourceInfo.resHeader  = {};
             resourceInfo.resBody    = "";
             resourceInfo.length     = 0;
-<<<<<<< HEAD
 
-            GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
-=======
-            
             global.recorder && global.recorder.updateRecord(resourceInfoId,resourceInfo);
->>>>>>> 6521604... change: discard decrpecated GLOBAL
 
             callback();
         }
diff --git a/package.json b/package.json
index d58f736..20e0181 100644
--- a/package.json
+++ b/package.json
@@ -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"

From 4461628e1b959642e2aa051b28dab9f7cfceb6bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A0=9A=E7=84=B6?= <yanran.wwj@alipay.com>
Date: Tue, 6 Sep 2016 10:23:09 +0800
Subject: [PATCH 3/3] fix the issue with TLS connection cache, when server
 doesn't recognize the cache, it will hang up the following request after the
 cached one.

refer to https://github.com/nodejs/node/issues/8368
---
 lib/requestHandler.js | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/requestHandler.js b/lib/requestHandler.js
index f0f9a7e..04b0cda 100644
--- a/lib/requestHandler.js
+++ b/lib/requestHandler.js
@@ -16,6 +16,9 @@ var http           = require("http"),
 
 var userRule = util.freshRequire('./rule_default');
 
+// to fix issue with TLS cache, refer to: https://github.com/nodejs/node/issues/8368
+https.globalAgent.maxCachedSessions = 0;
+
 function userRequestHandler(req,userRes){
     /*
     note