Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit c0adb94

Browse files
committed
verbose and debug mode for proxy server; custom client IDs; session lockdown; misc
1 parent 8cfac2e commit c0adb94

14 files changed

Lines changed: 164 additions & 25 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Launch debug proxy server:
5555

5656
node ./example/server --port 9080 --php lib-phpdebug.localhost --idekey secret-key
5757

58+
Use `-v` to log major events to console and `-d` to log debug messages to console.
59+
5860
Test
5961
----
6062

example/client/js/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,7 @@ function initDefaultClient(XDEBUG)
195195
{
196196
});
197197

198-
client.connect();
198+
client.connect({
199+
id: "client-browser"
200+
});
199201
}

example/server.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ var proxyServer = null,
7272

7373
// See: https://github.com/chriso/cli/blob/master/examples/static.js
7474
CLI.parse({
75+
verbose: ["v", 'Log major events to console', 'boolean', false],
76+
debug: ["d", 'Log debug messages to console', 'boolean', false],
7577
port: [false, 'Listen on this port', 'number', PROXY_PORT],
7678
php: [false, 'Hostname for `../php/`', 'string', PHP_VHOST],
7779
test: [false, 'Test mode. Must be pinged to stay alive!'],
@@ -142,13 +144,22 @@ function startServer(options)
142144
var io = SOCKET_IO.listen(app);
143145

144146
io.set("log level", 0);
147+
if (options.verbose)
148+
io.set("log level", 2);
149+
if (options.debug)
150+
io.set("log level", 3);
145151

146152
// Initialize and hook in the debug proxy server so it can
147153
// communicate via `socket.io`.
148154

149-
proxyServer = new XDEBUG_PROXY.Server();
155+
proxyServer = new XDEBUG_PROXY.Server({
156+
verbose: options.verbose,
157+
debug: options.debug
158+
});
150159

151160
proxyServer.listen(new XDEBUG.Client({
161+
verbose: options.verbose,
162+
debug: options.debug,
152163
API: {
153164
NET: NET,
154165
XML2JS: XML2JS
@@ -167,7 +178,19 @@ function startServer(options)
167178
proxyServer.hook({
168179
socketIO: io
169180
});
181+
182+
proxyServer.on("session", function(session)
183+
{
184+
if (session.name === "session-locked")
185+
{
186+
// Lock down the session to the client so no other client can listen in.
187+
// See the `../test/session.js` *lockedSession* test.
188+
189+
session.lockToClient("client-server-session-locked");
190+
}
191+
});
170192

193+
171194
// Hook in browser test system
172195
io.of("/test").on("connection", function(socket)
173196
{

lib/dbgp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ define(function(require, exports, module)
8787
}
8888
if (!/\u0000$/.test(chunk)) {
8989
this.buffer = chunk;
90+
// TODO: Parse as much of the buffer as we can right away
9091
return;
9192
}
9293

lib/proxy.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var Server = exports.Server = function(options)
3535
var self = this;
3636

3737
this.options = options;
38+
this.debug = this.options.debug || false;
39+
this.verbose = this.options.verbose || false;
3840
this.listeners = {};
3941
this.clients = {};
4042
this.sessions = {};
@@ -67,18 +69,25 @@ Server.prototype.hook = function(options)
6769
{
6870
socket.on("connect-client", function(data, fn)
6971
{
70-
self.clients[socket.id] = socket;
72+
if (self.verbose)
73+
console.log("Got `connect-client` for socket ID '" + socket.id + "' and client ID '" + data.id + "'");
74+
data.socket = socket;
75+
self.clients[socket.id] = data;
7176
fn();
7277
});
7378

7479
socket.on("disconnect-client", function(data, fn)
7580
{
81+
if (self.verbose)
82+
console.log("Got `disconnect-client` for socket ID '" + socket.id + "' and client ID '" + self.clients[socket.id].id + "'");
7683
delete self.clients[socket.id];
7784
fn();
7885
});
7986

8087
socket.on("disconnect", function(data)
8188
{
89+
if (self.verbose)
90+
console.log("Got `disconnect` for socket ID '" + socket.id + "' and client ID '" + self.clients[socket.id].id + "'");
8291
delete self.clients[socket.id];
8392
});
8493

@@ -96,10 +105,19 @@ Server.prototype.listen = function(client)
96105
{
97106
var self = this;
98107

99-
function sendToClients(name, args)
108+
function sendToClients(name, args, session)
100109
{
101-
for (var id in self.clients) {
102-
self.clients[id].emit(name, args);
110+
for (var id in self.clients)
111+
{
112+
if (!session.lockedClientId || self.clients[id].id === session.lockedClientId)
113+
{
114+
self.clients[id].socket.emit(name, args);
115+
}
116+
else
117+
{
118+
if (self.debug)
119+
console.log("Skip sending data for session '" + session.name + "' to client '" + self.clients[id].id + "' as session is locked to client '" + session.lockedClientId + "'.");
120+
}
103121
}
104122
}
105123

@@ -117,10 +135,15 @@ Server.prototype.listen = function(client)
117135
sendToClients("event", {
118136
type: name,
119137
session: session.id,
138+
sessionName: session.name,
120139
args: args
121-
});
140+
}, session);
122141
});
142+
143+
self.emit("session", session);
123144
});
124145

125-
client.connect();
146+
client.connect({
147+
"id": "xdebug-" + client.options.xdebugPort
148+
});
126149
}

lib/xdebug.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ define(function(require, exports, module)
118118

119119
this.API = options.API;
120120
this.options = options;
121+
this.debug = this.options.debug || false;
122+
this.verbose = this.options.verbose || false;
121123
this.listeners = {};
122124
this.sessions = {};
123125
this.connected = false;
124-
125126
// NOTE: The client ID is unique to the environment only, not globally!
127+
// A custom ID may be set via `connect()`.
126128
this.id = "client-" + (++clientCounter);
127129
};
128130

@@ -148,13 +150,18 @@ define(function(require, exports, module)
148150
}
149151
}
150152

151-
Client.prototype.connect = function()
153+
Client.prototype.connect = function(options)
152154
{
155+
options = options || {};
156+
153157
var self = this;
154158

155159
if (this.connected)
156160
throw new Error("Client already connected!");
157161

162+
if (options.id)
163+
self.id = options.id;
164+
158165
if (typeof this.options.xdebugPort !== "undefined")
159166
{
160167
this.engineServer = null;
@@ -183,11 +190,17 @@ define(function(require, exports, module)
183190
self.sessions[session.id] = session;
184191

185192
self.emit("session", session);
193+
194+
if (self.verbose)
195+
console.log("Got `ready` for session '" + session.name + "' and client '" + self.id + "'.");
186196
});
187197

188198
session.on("end", function()
189199
{
190200
delete self.sessions[session.id];
201+
202+
if (self.verbose)
203+
console.log("Got `end` for session '" + session.name + "' and client '" + self.id + "'.");
191204
});
192205

193206
session.listen(socket);
@@ -214,7 +227,9 @@ define(function(require, exports, module)
214227

215228
clients[self.id] = true;
216229

217-
self.proxySocket.emit("connect-client", {}, function()
230+
self.proxySocket.emit("connect-client", {
231+
id: self.id
232+
}, function()
218233
{
219234
self.connected = true;
220235

@@ -319,12 +334,15 @@ define(function(require, exports, module)
319334
options = options || {};
320335
this.API = options.API;
321336
this.options = options;
337+
this.debug = this.options.debug || false;
338+
this.verbose = this.options.verbose || false;
322339
this.listeners = {};
323340
this.status = "init"; // init, ready, aborted, ended
324341
this.socketIO = null;
325342
this.socket = null;
326343
this.commandCounter = 0;
327344
this.commandCallbacks = {};
345+
this.lockedClientId = false;
328346

329347
this.on("ready", function()
330348
{
@@ -363,6 +381,14 @@ define(function(require, exports, module)
363381
}
364382
}
365383

384+
Session.prototype.lockToClient = function(clientId)
385+
{
386+
this.lockedClientId = clientId;
387+
388+
if (this.verbose)
389+
console.log("Locked session '" + this.name + "' to client '" + this.lockedClientId + "'.");
390+
}
391+
366392
Session.prototype.listen = function(socket)
367393
{
368394
var self = this;
@@ -384,7 +410,10 @@ define(function(require, exports, module)
384410

385411
parser.on("packet", function(packet)
386412
{
387-
if (self.status === "ready")
413+
if (self.debug)
414+
console.log("Got packet", packet);
415+
416+
if (self.status === "ready")
388417
{
389418
// 6.5 debugger engine errors
390419
// @see http://www.xdebug.org/docs-dbgp.php#id32
@@ -433,7 +462,8 @@ define(function(require, exports, module)
433462
}
434463
else
435464
{
436-
console.log(packet);
465+
console.log("ERROR: Got unknown packet type from xdebug!", packet);
466+
throw new Error("Got unknown packet type from xdebug!");
437467
}
438468

439469
if (typeof packet["@"].status !== "undefined")
@@ -465,8 +495,8 @@ console.log(packet);
465495
}
466496
else
467497
{
468-
469-
console.log('status', packet);
498+
console.log("ERROR: Got unknown status from xdebug!", packet);
499+
throw new Error("Got unknown status from xdebug!");
470500
}
471501
}
472502
}
@@ -540,6 +570,8 @@ console.log('status', packet);
540570
self.id += "-" + packet["@"].thread;
541571
if (packet["@"].idekey)
542572
self.id += "-" + packet["@"].idekey;
573+
574+
self.name = packet["@"].session;
543575

544576
self.emit("init", {raw: packet});
545577

test/breakpoints.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ var Test =
165165
next();
166166
});
167167

168-
client.connect();
168+
client.connect({
169+
id: "client-server-breakpoints"
170+
});
169171
},
170172

171173
"test browserBreakpoints": function(next)

test/browser/breakpoints.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ define(function(require, exports, module)
158158
callback(true);
159159
});
160160

161-
client.connect();
161+
client.connect({
162+
id: "client-browser-breakpoints"
163+
});
162164
}
163165

164166
});

test/browser/connection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ define(function(require, exports, module)
4444
callback(true);
4545
});
4646

47-
client.connect();
47+
client.connect({
48+
id: "client-browser-connection"
49+
});
4850
}
4951

5052
});

test/browser/session.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ define(function(require, exports, module)
6363
callback(true);
6464
});
6565

66-
client.connect();
66+
client.connect({
67+
id: "client-browser-session"
68+
});
6769
}
6870

6971
});

0 commit comments

Comments
 (0)