From 5e87fd07071965723ab74db62c0b920874dd1361 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8A=A0=E9=87=8C?= <xiaofeng.mxf@taobao.com>
Date: Sat, 9 Aug 2014 16:33:51 +0800
Subject: [PATCH] add -c

---
 index.js | 90 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 40 deletions(-)

diff --git a/index.js b/index.js
index f4bac3d..8caf61a 100644
--- a/index.js
+++ b/index.js
@@ -10,9 +10,10 @@ var http      = require('http'),
     program   = require('commander');
 
 program
-    .option('-h, --host [value]', 'hostname,use for https proxy. localhost for default')
+    .option('-h, --host [value]', 'hostname for https proxy, localhost for default')
     .option('-t, --type [value]', 'http|https,http for default')
-    .option('-p, --port [value]', 'proxy port , 8001 for default')
+    .option('-p, --port [value]', 'proxy port, 8001 for default')
+    .option('-c, --clear', 'clear all the tmp certificates')
     .parse(process.argv);
 
 var PROXY_PORT    = program.port || 8001,
@@ -21,55 +22,64 @@ var PROXY_PORT    = program.port || 8001,
     PROXY_TYPE    = /https/i.test(program.type)? T_PROXY_HTTPS : T_PROXY_HTTP;
     HOSTNAME      = program.host || "localhost";
 
-var serverMgrInstance = new serverMgr(),
-    httpProxyServer;
+if(program.clear){
+    exec("rm -rf ./cert/tmpCert",function(){
+        console.log("certificates cleared");
+        process.exit(0);
+    });
+}else{
+    var serverMgrInstance = new serverMgr(),
+        httpProxyServer;
 
+    async.series([
+        //creat server
+        function(callback){
+            if(PROXY_TYPE == T_PROXY_HTTP){
+                httpProxyServer = http.createServer(dealProxyUserHttpReq);
+                callback(null);
+            }else{
 
-async.series([
-    //creat server
-    function(callback){
-        if(PROXY_TYPE == T_PROXY_HTTP){
-            httpProxyServer = http.createServer(dealProxyUserHttpReq);
-            callback(null);
-        }else{
+                var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
+                    crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
 
-            var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
-                crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
-
-            if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
-                createCert(HOSTNAME,function(){
+                if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
+                    createCert(HOSTNAME,function(){
+                        httpProxyServer = https.createServer({
+                            key : fs.readFileSync(keyFile),
+                            cert: fs.readFileSync(crtFile)
+                        },dealProxyUserHttpReq);
+                        callback(null);
+                    });
+                }else{
                     httpProxyServer = https.createServer({
                         key : fs.readFileSync(keyFile),
                         cert: fs.readFileSync(crtFile)
                     },dealProxyUserHttpReq);
                     callback(null);
-                });
+                }
+            }        
+        },
+
+        //
+        function(callback){
+            //listen CONNECT method for https over http
+            httpProxyServer.on('connect',dealProxyConnectReq);
+            httpProxyServer.listen(PROXY_PORT);
+            callback(null);
+
+        }],function(err,result){
+            if(!err){
+                console.log( (PROXY_TYPE == T_PROXY_HTTP ? "Http" : "Https") + " proxy started at port " + PROXY_PORT);
             }else{
-                httpProxyServer = https.createServer({
-                    key : fs.readFileSync(keyFile),
-                    cert: fs.readFileSync(crtFile)
-                },dealProxyUserHttpReq);
-                callback(null);
+                console.log("err when start proxy server :(");
+                console.log(err);
             }
-        }        
-    },
-
-    //
-    function(callback){
-        //listen CONNECT method for https over http
-        httpProxyServer.on('connect',dealProxyConnectReq);
-        httpProxyServer.listen(PROXY_PORT);
-        callback(null);
-
-    }],function(err,result){
-        if(!err){
-            console.log( (PROXY_TYPE == T_PROXY_HTTP ? "Http" : "Https") + " proxy started at port " + PROXY_PORT);
-        }else{
-            console.log("err when start proxy server :(");
-            console.log(err);
         }
-    }
-);
+    );    
+}
+
+
+
 
 function dealProxyUserHttpReq(req,res){
     var urlPattern = url.parse(req.url);