in src/commands/remoteDebugJava/DebugProxy.ts [32:123]
public async startProxy(context: IActionContext): Promise<void> {
if (!this._server) {
this.emit('error', new Error('Proxy server is not started.'));
} else {
// wake up the Function App before connecting to it.
await this.keepAlive(context);
this._server.on('connection', (socket: Socket) => {
if (this._wsclient) {
ext.outputChannel.appendLog(`[Proxy Server] The server is already connected. Rejected connection to "${socket.remoteAddress}:${socket.remotePort}"`);
this.emit('error', new Error(`[Proxy Server] The server is already connected. Rejected connection to "${socket.remoteAddress}:${socket.remotePort}"`));
socket.destroy();
} else {
ext.outputChannel.appendLog(`[Proxy Server] client connected ${socket.remoteAddress}:${socket.remotePort}`);
socket.pause();
this._wsclient = new websocket.client();
this._wsclient.on('connect', (connection: websocket.connection) => {
ext.outputChannel.appendLog('[WebSocket] client connected');
this._wsconnection = connection;
connection.on('close', () => {
ext.outputChannel.appendLog('[WebSocket] client closed');
this.dispose();
socket.destroy();
this.emit('end');
});
connection.on('error', (err: Error) => {
ext.outputChannel.appendLog(`[WebSocket] ${err}`);
this.dispose();
socket.destroy();
this.emit('error', err);
});
connection.on('message', (data: websocket.Message) => {
if ('binaryData' in data && data.binaryData) {
socket.write(data.binaryData);
}
});
socket.resume();
});
this._wsclient.on('connectFailed', (err: Error) => {
ext.outputChannel.appendLog(`[WebSocket] ${err}`);
this.dispose();
socket.destroy();
this.emit('error', err);
});
this._wsclient.connect(
`wss://${this._site.kuduHostName}/DebugSiteExtension/JavaDebugSiteExtension.ashx`,
undefined,
undefined,
{ 'Cache-Control': 'no-cache', Pragma: 'no-cache' },
{ auth: `${this._publishCredential.publishingUserName}:${this._publishCredential.publishingPassword}` }
);
socket.on('data', (data: Buffer) => {
if (this._wsconnection) {
this._wsconnection.send(data);
}
});
socket.on('end', () => {
ext.outputChannel.appendLog(`[Proxy Server] client disconnected ${socket.remoteAddress}:${socket.remotePort}`);
this.dispose();
this.emit('end');
});
socket.on('error', (err: Error) => {
ext.outputChannel.appendLog(`[Proxy Server] ${err}`);
this.dispose();
socket.destroy();
this.emit('error', err);
});
}
});
this._server.on('listening', () => {
ext.outputChannel.appendLog('[Proxy Server] start listening');
this.emit('start');
});
this._server.listen({
host: 'localhost',
port: this._port,
backlog: 1
});
}
}