@@ -17,10 +17,13 @@ var http = require('http'),
1717 getPort = require ( "./lib/getPort" ) ,
1818 requestHandler = require ( "./lib/requestHandler" ) ,
1919 Recorder = require ( "./lib/Recorder" ) ,
20+ inherits = require ( "util" ) . inherits ,
2021 util = require ( "./lib/util" ) ,
2122 entities = require ( "entities" ) ,
2223 express = require ( "express" ) ,
2324 path = require ( "path" ) ,
25+ juicer = require ( 'juicer' ) ,
26+ events = require ( "events" ) ,
2427 WebSocketServer = require ( 'ws' ) . Server ;
2528
2629GLOBAL . recorder = new Recorder ( ) ;
@@ -30,6 +33,7 @@ var T_TYPE_HTTP = 0,
3033 DEFAULT_PORT = 8001 ,
3134 DEFAULT_WEB_PORT = 8002 ,
3235 DEFAULT_WEBSOCKET_PORT = 8003 ,
36+ DEFAULT_CONFIG_PORT = 8080 ,
3337 DEFAULT_HOST = "localhost" ,
3438 DEFAULT_TYPE = T_TYPE_HTTP ;
3539
@@ -57,7 +61,7 @@ function proxyServer(option){
5761 proxyType = / h t t p s / i. test ( option . type || DEFAULT_TYPE ) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
5862 proxyPort = option . port || DEFAULT_PORT ,
5963 proxyHost = option . hostname || DEFAULT_HOST ,
60- proxyRules = option . rule || default_rule ;
64+ proxyRules = option . rule || default_rule ;
6165
6266 requestHandler . setRules ( proxyRules ) ; //TODO : optimize calling for set rule
6367 self . httpProxyServer = null ;
@@ -95,7 +99,15 @@ function proxyServer(option){
9599
96100 //start web interface
97101 function ( callback ) {
98- startWebServer ( ) ;
102+ var webServer = new proxyWebServer ( ) ;
103+ var wss = webServer . wss ;
104+
105+ var configServer = new UIConfigServer ( DEFAULT_CONFIG_PORT ) ;
106+ configServer . on ( "rule_changed" , function ( ) {
107+ console . log ( arguments ) ;
108+ } )
109+ // var wss = proxyWebServer();
110+
99111 callback ( null ) ;
100112 }
101113 ] ,
@@ -119,7 +131,90 @@ function proxyServer(option){
119131 }
120132}
121133
122- function startWebServer ( port ) {
134+ // doing
135+ function UIConfigServer ( port ) {
136+ var self = this ;
137+
138+ var app = express ( ) ,
139+ customerRule = {
140+ summary : function ( ) {
141+ console . log ( "replace some response with local response" ) ;
142+ return "replace some response with local response" ;
143+ }
144+ } ,
145+ userKey ;
146+
147+
148+ customerRule . shouldUseLocalResponse = function ( req , reqBody ) {
149+ var url = req . url ;
150+ if ( userKey ) {
151+ var ifMatch = false ;
152+ userKey . map ( function ( item ) {
153+ if ( ifMatch ) return ;
154+
155+ var matchCount = 0 ;
156+ if ( ! item . urlKey && ! item . reqBodyKey ) {
157+ ifMatch = false ;
158+ return ;
159+ } else {
160+ if ( ! item . urlKey || ( item . urlKey && url . indexOf ( item . urlKey ) >= 0 ) ) {
161+ ++ matchCount ;
162+ }
163+
164+ if ( ! item . reqBodyKey || ( item . reqBodyKey && reqBody . toString ( ) . indexOf ( item . reqBodyKey ) >= 0 ) ) {
165+ ++ matchCount ;
166+ }
167+
168+ ifMatch = ( matchCount == 2 ) ;
169+ if ( ifMatch ) {
170+ req . willResponse = item . localResponse ;
171+ }
172+ }
173+ } ) ;
174+
175+ return ifMatch ;
176+ } else {
177+ return false ;
178+ }
179+ } ;
180+
181+ customerRule . dealLocalResponse = function ( req , reqBody , callback ) {
182+ callback ( 200 , { "content-type" :"text/html" } , req . willResponse )
183+
184+ return req . willResponse ;
185+ } ;
186+
187+ app . post ( "/update" , function ( req , res ) {
188+ var data = "" ;
189+ req . on ( "data" , function ( chunk ) {
190+ data += chunk ;
191+ } ) ;
192+
193+ req . on ( "end" , function ( ) {
194+ userKey = JSON . parse ( data ) ;
195+
196+ res . statusCode = 200 ;
197+ res . setHeader ( "Content-Type" , "application/json;charset=UTF-8" ) ;
198+ res . end ( JSON . stringify ( { success : true } ) ) ;
199+
200+ requestHandler . setRules ( customerRule ) ;
201+ self . emit ( "rule_changed" ) ;
202+ } ) ;
203+ } ) ;
204+
205+ app . use ( express . static ( __dirname + "/web_uiconfig" ) ) ;
206+ app . listen ( port ) ;
207+
208+ self . app = app ;
209+ }
210+
211+ inherits ( UIConfigServer , events . EventEmitter ) ;
212+
213+
214+
215+ function proxyWebServer ( port ) {
216+ var self = this ;
217+
123218 port = port || DEFAULT_WEB_PORT ;
124219
125220 //web interface
@@ -150,13 +245,22 @@ function startWebServer(port){
150245 res . end ( entities . encodeHTML ( body ) ) ;
151246 } ) ;
152247
248+ app . use ( function ( req , res , next ) {
249+ var indexHTML = fs . readFileSync ( "./web/index.html" , { encoding :"utf8" } ) ;
250+
251+ if ( req . url == "/" ) {
252+ res . setHeader ( "Content-Type" , "text/html" ) ;
253+ res . end ( indexHTML . replace ( "{{rule}}" , requestHandler . getRuleSummary ( ) ) ) ;
254+ } else {
255+ next ( ) ;
256+ }
257+ } ) ;
258+
153259 app . use ( express . static ( __dirname + '/web' ) ) ;
154260
155261 app . listen ( port ) ;
156262
157263 var tipText = "web interface started at port " + port ;
158- console . log ( color . green ( tipText ) ) ;
159-
160264
161265 //web socket interface
162266 var wss = new WebSocketServer ( { port : DEFAULT_WEBSOCKET_PORT } ) ;
@@ -169,6 +273,9 @@ function startWebServer(port){
169273 GLOBAL . recorder . on ( "update" , function ( data ) {
170274 wss . broadcast ( JSON . stringify ( data ) ) ;
171275 } ) ;
276+
277+ self . app = app ;
278+ self . wss = wss ;
172279}
173280
174281
0 commit comments