Skip to content

Commit

Permalink
Recurse instead of passing empty buffer (microsoft/vscode-remote-rele…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Dec 1, 2022
1 parent 1289e4e commit d7068f4
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/spec-common/cliHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,26 @@ function cygwinUnixSocketCookieToBuffer(cookie: string) {

// The cygwin/git bash ssh-agent server will reply us with the cookie back (16 bytes)
// + identifiers (12 bytes), skip them while forwarding data from ssh-agent to the client
function skipHeader(headerSize: number, cb: SourceCallback<Buffer>, abort: Abort, data?: Buffer): number {
if (abort || data === undefined) {
cb(abort);
return headerSize;
function skipHeader(headerSize: number, err: Abort, data?: Buffer) {
if (err || data === undefined) {
return { headerSize, err };
}

if (headerSize === 0) {
// Fast path avoiding data buffer manipulation
// We don't need to modify the received data (handshake header
// already removed)
cb(null, data);
return { headerSize, data };
} else if (data.length > headerSize) {
// We need to remove part of the data to forward
data = data.slice(headerSize, data.length);
headerSize = 0;
cb(null, data);
return { headerSize, data };
} else {
// We need to remove all forwarded data
headerSize = headerSize - data.length;
cb(null, Buffer.of());
return { headerSize };
}

// Return the updated headerSize
return headerSize;
}

// Function to handle the Cygwin/Gpg4win socket filtering
Expand Down Expand Up @@ -156,6 +152,18 @@ function handleUnixSocketOnWindows(socket: net.Socket, socketPath: string): Dupl
pendingSinkCalls = [];
};

function doSource(abort: Abort, cb: SourceCallback<Buffer>) {
(connectionDuplex as Duplex<Buffer, Buffer>).source(abort, function (err, data) {
const res = skipHeader(headerSize, err, data);
headerSize = res.headerSize;
if (res.err || res.data) {
cb(res.err || null, res.data);
} else {
doSource(abort, cb);
}
});
}

(async () => {
const buf = await readLocalFile(socketPath);
const str = buf.toString();
Expand Down Expand Up @@ -211,9 +219,7 @@ function handleUnixSocketOnWindows(socket: net.Socket, socketPath: string): Dupl
// The received data from ssh-agent/gpg-agent server is filtered
// to skip the handshake header.
for (let callback of pendingSourceCallbacks) {
(connectionDuplex as Duplex<Buffer, Buffer>).source(callback.abort, function (abort, data) {
headerSize = skipHeader(headerSize, callback.cb, abort, data);
});
doSource(callback.abort, callback.cb);
}
pendingSourceCallbacks = [];

Expand All @@ -233,9 +239,7 @@ function handleUnixSocketOnWindows(socket: net.Socket, socketPath: string): Dupl
// pull-stream source that remove the first <headerSize> bytes
let source: Source<Buffer> = function (abort: Abort, cb: SourceCallback<Buffer>) {
if (connectionDuplex !== undefined) {
connectionDuplex.source(abort, function (abort, data) {
headerSize = skipHeader(headerSize, cb, abort, data);
});
doSource(abort, cb);
} else {
pendingSourceCallbacks.push({ abort: abort, cb: cb });
}
Expand Down

0 comments on commit d7068f4

Please sign in to comment.