-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmyproxy4.js
176 lines (149 loc) · 4.74 KB
/
myproxy4.js
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
// myproxy4.js
// HttpProxyとして要求を受けて、WebSocketにして要求を転送する。
// WebSocketから応答を受けて、HttpProxyに転送する。
var WEB_SOCKET_PORT = 8001;
var HTTP_PROXY_PORT = 8081;
// 例外が発生してもサービスを停止しないようにする
process.on('uncaughtException', function(err) {
console.log(err.stack);
});
var app = require('http').createServer(handler), io = require('socket.io')
.listen(app), fs = require('fs'),uuid=require('node-uuid');
app.listen(WEB_SOCKET_PORT);
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var myWsClient = {};
var count = 0;
var webClients = {};
var httpProxyServer;
io.sockets.on('connection', function(wsclient) {
myWsClient = wsclient;
console.log("HTTP Proxy start");
if(httpProxyServer) {
// close
httpProxyServer.close();
}
httpProxyServer = doProxy(wsclient);
wsclient.on('end', function() {
console.log("HTTP Proxy end");
httpProxyServer.close();
});
});
function doProxy(wsclient) {
// HttpProxyを開始する。
var sys = require('util');
var net = require('net');
var server = net.createServer(function(webBrowser) {
count++;
// 接続してきたブラウザのソケットを保持する。
var wid = uuid.v4();
webClients[wid] = webBrowser;
webBrowser.wid = wid;
console.log('web client connected [' + count + "]");
// クライアントからデータを受けた場合
webBrowser.on('data', function(data) {
webBrowser.pause();
var parseheader = data.toString().split(/\n/);
if (parseheader[0].match(/^GET/)
|| parseheader[0].match(/^CONNECT/)
|| parseheader[0].match(/^POST/)) {
console.log("client send : " + parseheader[0].toString());
} else {
console.log("client send ");
}
// Base64化してWS->HttpProxyへ送る
//console.log("to ws : " + data.toString('base64'));
wsclient.emit('httptows', {
httpdata : data.toString('base64'),
wid : webBrowser.wid
});
webBrowser.resume();
});
webBrowser.on('end', function() {
// ブラウザの接続が切断された場合の処理
console.log('client disconnected[' + webBrowser.wid + ']');
wsclient.emit('httpend', {
wid : webBrowser.wid
});
webClients[webBrowser.wid] = "";
});
webBrowser.on('close',
function() {
// ブラウザの接続が切断された場合の処理
console.log('client connection is closed.['
+ webBrowser.wid + ']');
webClients[webBrowser.wid] = "";
wsclient.emit('httpend', {
wid : webBrowser.wid
});
});
webBrowser.on('error', function(err) {
// ブラウザの接続が切断された場合の処理
console.log('client err[' + webBrowser.wid + '].' + err);
webClients[webBrowser.wid] = "";
wsclient.emit('httpend', {
wid : webBrowser.wid
});
});
}); // server
server.listen(HTTP_PROXY_PORT, function() { //'listening' listener
console.log('server bound');
});
wsclient.on('wstohttp', function(data) {
// WebSocketからの応答をブラウザに返す。
if (webClients[data['wid']] == "") {
console.log('web client connection has been closed.');
wsclient.emit('httpend', {
wid : data['wid']
});
return;
}
// WebSocket経由で結果受取、Base64デコードしてブラウザに返す。
console.log('server send data : [' + data['wid'] + "]");
var a = new Buffer(data['httpdata'].toString(), 'base64');
var clientSocket = webClients[data['wid']];
clientSocket.pause();
clientSocket.write(a);
//dumpResponse(a);
clientSocket.resume();
});
wsclient.on('httpend', function(data) {
console.log("server is closed.[" + data['wid'] + "]");
// HttpProxy先でコネクションが切れたら、こちらもクライアントを切断
var clientSocket = webClients[data['wid']];
console.log("clientSocket = "+clientSocket);
if (clientSocket == "") {
return;
}
clientSocket.end();
webClients[data['wid']] = "";
});
wsclient.on('end', function() {
console.log('server disconnected');
//var clientSocket = webClients[data['wid']];
//clientSocket.end();
});
sys.puts('Server listening on port ' + HTTP_PROXY_PORT);
return server;
}
function dumpResponse(buf) {
var tmp = "";
// 表示できる文字は表示する
for ( var i = 0; i < buf.length; i++) {
var c = buf.readUInt8(i);
if ((c > 31 && c < 127) || c == 13 || c == 10) {
tmp = tmp + String.fromCharCode(c);
} else {
tmp = tmp + c + ":";
}
}
console.log(tmp);
}