This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
netproxy.js
136 lines (108 loc) · 2.82 KB
/
netproxy.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
var net = require("net");
var fs = require("fs");
var debug = function() {};
// var debug = console.error.bind(console);
var socketPath = process.env.HOME + "/.c9/xdebug.sock";
if (process.platform == "win32")
socketPath = "\\\\.\\pipe\\" + socketPath.replace(/\//g, "\\");
var host = "{HOST}" || "127.0.0.1";
var port = parseInt("{PORT}", 10) || 9000;
var browserBuffer = [];
var debugBuffer = [];
var browserClient;
var debugClient;
var log = console.log;
console.warn = console.log = function() {
return console.error.apply(console, arguments);
};
function send() {
log.apply(console, arguments);
}
/*** --- ***/
var browserServer = net.createServer(function(client) {
debug("browserClient::connect");
if (browserClient)
browserClient.end();
browserClient = client;
debugBuffer = [];
browserClient.on("end", function() {
debug("browserClient::end");
process.exit(0);
browserClient = null;
});
browserClient.on("data", function(data) {
debug("browserClient::data:", data.toString("utf8"));
if (debugClient) {
debugClient.write(data);
} else {
debugBuffer.push(data);
}
});
if (browserBuffer.length) {
browserBuffer.forEach(function(data) {
browserClient.write(data);
});
browserBuffer = [];
}
});
if (process.platform !== "win32") {
try {
fs.unlinkSync(socketPath);
} catch (e) {
if (e.code != "ENOENT")
console.log(e);
}
}
browserServer.listen(socketPath, function() {
debug("netproxy listening for " + socketPath);
start();
});
browserServer.on("error", function(err) {
console.log(err);
process.exit(0);
});
browserServer.on("close", function(err) {
console.log(err);
process.exit(0);
});
/*** --- ***/
var debugServer = net.createServer(function(client) {
debug("debugClient::connect");
if (debugClient)
debugClient.end();
debugClient = client;
debugBuffer = [];
browserBuffer = [];
debugClient.on("end", function() {
debug("debugClient::end");
process.exit(0);
});
debugClient.on("data", function(data) {
debug("debugClient::data:", data.toString("utf8"));
if (browserClient) {
browserClient.write(data);
} else {
browserBuffer.push(data);
}
});
if (debugBuffer.length) {
debugBuffer.forEach(function(data) {
debugClient.write(data);
});
debugBuffer = [];
}
});
debugServer.listen(port, host, function() {
debug("netproxy listening for debugger on port " + port);
start();
});
debugServer.on("error", function(err) {
console.log(err);
process.exit(0);
});
/*** --- ***/
var I = 0;
function start() {
if (++I == 2)
send("ß");
}