-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmyproxy0.js
248 lines (222 loc) · 5.49 KB
/
myproxy0.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// myporxy0.js
// 例外が発生してもサービスを停止しないようにする
process.on('uncaughtException', function(err) {
console.log(err.stack);
});
var sys = require('util');
var net = require('net');
var url = require('url');
var server = net.createServer(function(c) {
// 'connection' listener
console.log('server connected');
var socket = "";
var isClientClosed = false;
c.on('data', function(data) {
c.pause();
//if (socket == "") {
// ソケットが未作成の場合、HTTP要求とみなし解析する。
var parseheader = data.toString().split(/\n/);
if (parseheader[0].match(/^GET/) || parseheader[0].match(/^CONNECT/)
|| parseheader[0].match(/^POST/)) {
var hthead = parseheader[0].split(/ /);
// console.log(hthead[1]);
var destPort = "";
var destHost = "";
if (parseheader[0].match(/^CONNECT/)) {
// httpsの場合の処理
// hostname:port
var parseheader = data.toString().split(/\n/);
var hthead = parseheader[0].split(/ /);
var tmp = hthead[1].split(/:/);
destHost = tmp[0];
destPort = tmp[1];
} else {
// GET,POSTの場合の処理
destPort = url.parse(hthead[1]).port || 80;
destHost = url.parse(hthead[1]).host;
}
console.log("destPort = " + destPort);
console.log("destHost = " + destHost);
console.log("client send : " + parseheader[0].toString());
//if(socket == "") {
socket = net.createConnection(destPort, destHost, function() {
console.log('server connected');
if (parseheader[0].match(/^CONNECT/)) {
c.pause();
//socket.write(data);
c.write("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\n");
c.resume();
} else {
socket.write(data);
}
// c.resume();
});
socket.on('data', function(data) {
// socket.pause();
if (isClientClosed) {
socket.end();
return;
}
c.pause();
socket.pause();
console.log('server send data');
c.write(data);
// socket.resume();
c.resume();
socket.resume();
});
socket.on('end', function() {
console.log('server is disconnected');
c.end();
socket = "";
});
socket.on('close', function() {
console.log('server has been disconnected');
c.end();
socket = "";
});
socket.on('error', function(err) {
console.log("socket error! " + err);
socket = "";
c.end();
});
//} else {
// 既に接続済みのソケットに対してのHTTP要求の場合
// console.log("socket has made! ");
//}
} else {
if (socket != "") {
socket.pause();
c.pause();
socket.write(data);
c.resume();
socket.resume();
}
}
// } else {
// console.log("client send ");
// if (socket != "") {
// socket.pause();
// c.pause();
// socket.write(data);
// c.resume();
// socket.resume();
// } else {
// console.log("Oops! no socket.");
// dumpResponse(data);
// c.end();
// }
// socket.write(data);
// }
});
c.on('end', function() {
console.log('client disconnected');
if (socket != "") {
socket.end();
socket = "";
}
isClientClosed = true;
});
c.on('close', function() {
console.log('client is closed');
if (socket != "") {
socket.end();
socket = "";
}
isClientClosed = true;
});
c.on('error', function(err) {
console.log('client error! ' + err);
if (socket != "") {
socket.end();
socket = "";
}
isClientClosed = true;
});
});
// https接続
function doConnect(client, data) {
var parseheader = data.toString().split(/\n/);
var hthead = parseheader[0].split(/ /);
var tmp = hthead[1].split(/:/);
var destHost = tmp[0];
var destPort = tmp[1];
var isClientClosed = false;
console.log("destPort = " + destPort);
console.log("destHost = " + destHost);
var socket = net.createConnection(destPort, destHost, function() {
console.log('server connected');
client.write("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\n");
client.resume();
});
client.on('data', function(dt) {
client.pause();
socket.pause();
console.log('client send');
socket.write(dt);
// client.resume();
socket.resume();
client.resume();
});
client.on('err', function(err) {
console.log("client error! " + err);
socket.end();
isClientClosed = true;
});
client.on('close', function() {
console.log("client has been closed.");
socket.end();
isClientClosed = true;
});
client.on('end', function() {
console.log("client is closed.");
socket.end();
isClientClosed = true;
});
socket.on('data', function(dt) {
socket.pause();
if (isClientClosed) {
console.log("client socket has been closed.");
socket.end();
}
client.pause();
// socket.pause();
console.log('sever send');
client.write(dt);
client.resume();
socket.resume();
});
socket.on('end', function() {
client.end();
console.log('sever close1');
});
socket.on('close', function() {
client.end();
console.log('sever close1');
});
socket.on('error', function(err) {
console("socket error! " + err);
client.end();
});
// socket.write(data);
// client.pipe(socket);
// socket.pipe(client);
}
var port = process.env.PORT || 8080;
server.listen(port, function() { // 'listening' listener
console.log('server bound');
});
sys.puts('Server listening on port ' + port);
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);
}