forked from alibaba/anyproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServer.js
More file actions
176 lines (147 loc) · 5.1 KB
/
Copy pathwebServer.js
File metadata and controls
176 lines (147 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// var process = require("child_process")
var express = require("express"),
url = require('url'),
fs = require("fs"),
util = require("./lib/util"),
certMgr = require("./lib/certMgr"),
events = require("events"),
inherits = require("util").inherits,
ent = require("ent"),
qrCode = require('qrcode-npm'),
logUtil = require("./lib/log"),
WebSocketServer = require('ws').Server;
function proxyWebServer(port,webSocketPort,proxyConfigPort,ruleSummary,ipAddress,menuListStr){
var self = this,
myAbsAddress = "http://" + ipAddress + ":" + port +"/",
crtFilePath = certMgr.getRootCAFilePath();
if(arguments.length < 3){
throw new Error("please assign ports");
}
//web interface
var app = express();
app.use(function(req, res, next) {
res.setHeader("note", "THIS IS A REQUEST FROM ANYPROXY WEB INTERFACE");
return next();
});
// app.get("/summary",function(req,res){
// recorder.getSummaryList(function(err,docs){
// if(err){
// res.end(err.toString());
// }else{
// res.json(docs.slice(docs.length -500));
// }
// });
// });
app.get("/body",function(req,res){
var id = req.query.id;
res.setHeader("Content-Type","text/html");
res.writeHead(200);
fetchBody(id,function(body){
res.end(ent.encode(body));
});
});
app.get("/fetchCrtFile",function(req,res){
if(crtFilePath){
res.setHeader("Content-Type","application/x-x509-ca-cert");
res.setHeader("Content-Disposition",'attachment; filename="rootCA.crt"');
res.end(fs.readFileSync(crtFilePath,{encoding:null}));
}else{
res.setHeader("Content-Type","text/html");
res.end("can not file rootCA ,plase use <strong>anyproxy --root</strong> to generate one");
}
});
//make qr code
app.get("/qr",function(req,res){
var qr = qrCode.qrcode(4, 'M'),
targetUrl = myAbsAddress,
qrImageTag,
resDom;
qr.addData(targetUrl);
qr.make();
qrImageTag = qr.createImgTag(4);
resDom = '<a href="__url"> __img <br> click or scan qr code to start client </a>'.replace(/__url/,targetUrl).replace(/__img/,qrImageTag);
res.setHeader("Content-Type", "text/html");
res.end(resDom);
});
app.get("/qr_root",function(req,res){
var qr = qrCode.qrcode(4, 'M'),
targetUrl = myAbsAddress + "fetchCrtFile",
qrImageTag,
resDom;
qr.addData(targetUrl);
qr.make();
qrImageTag = qr.createImgTag(4);
resDom = '<a href="__url"> __img <br> click or scan qr code to download rootCA.crt </a>'.replace(/__url/,targetUrl).replace(/__img/,qrImageTag);
res.setHeader("Content-Type", "text/html");
res.end(resDom);
});
app.use(function(req,res,next){
var indexHTML = fs.readFileSync(__dirname + "/web/index.html",{encoding:"utf8"});
if(req.url == "/"){
res.setHeader("Content-Type", "text/html");
res.end(util.simpleRender(indexHTML, {
rule : ruleSummary || "",
webSocketPort : webSocketPort,
proxyConfigPort : proxyConfigPort,
ipAddress : ipAddress || "127.0.0.1",
menu : menuListStr
}));
}else{
next();
}
});
app.use(express.static(__dirname + '/web'));
app.listen(port);
//web socket interface
var wss = new WebSocketServer({port: webSocketPort});
wss.on("connection",function(ws){});
wss.broadcast = function(data) {
for(var i in this.clients){
try{
this.clients[i].send(data);
}catch(e){
logUtil.printLog("websocket failed to send data, " + e, logUtil.T_ERR);
}
}
};
self.on("update",function(data){
wss.broadcast( JSON.stringify(data) );
})
self.app = app;
self.wss = wss;
}
inherits(proxyWebServer, events.EventEmitter);
var param = process.argv.slice(2),
server = new proxyWebServer(param[0],param[1],param[2],param[3],param[4],param[5]),
cbMap = {}, // id body cb
lastestHeartbeat = new Date().getTime();
process.on("message",function(data){
if(data.type == "update"){
server.emit("update",data.body);
}else if( data.type == "body"){
try{
var key = data.id + "";
cbMap[key].body = data.body;
cbMap[key].cb.call(null,data.body);
}catch(e){}
}else if(data.type == "watch"){
lastestHeartbeat = new Date().getTime();
}
});
//watch dog
setInterval(function(){
if(new Date().getTime() - lastestHeartbeat > 10 * 1000){
process.exit();
}
},7000);
function fetchBody(id,cb){
var key = id + "";
if(cbMap[key]){
cb(cbMap[key].body);
}else{
cbMap[key] = {
cb : cb
};
process.send({type : "reqBody", id: id});
}
}