2019-07-01 23:01:09 +00:00
|
|
|
import * as net from "net";
|
|
|
|
import { VSBuffer } from "vs/base/common/buffer";
|
|
|
|
import { PersistentProtocol } from "vs/base/parts/ipc/common/ipc.net";
|
2019-08-09 23:50:05 +00:00
|
|
|
import { NodeSocket, WebSocketNodeSocket } from "vs/base/parts/ipc/node/ipc.net";
|
2019-07-01 23:01:09 +00:00
|
|
|
import { AuthRequest, ConnectionTypeRequest, HandshakeMessage } from "vs/platform/remote/common/remoteAgentConnection";
|
|
|
|
|
|
|
|
export interface SocketOptions {
|
|
|
|
readonly reconnectionToken: string;
|
|
|
|
readonly reconnection: boolean;
|
|
|
|
readonly skipWebSocketFrames: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Protocol extends PersistentProtocol {
|
2019-07-25 22:39:43 +00:00
|
|
|
public constructor(socket: net.Socket, public readonly options: SocketOptions) {
|
2019-07-01 23:01:09 +00:00
|
|
|
super(
|
|
|
|
options.skipWebSocketFrames
|
|
|
|
? new NodeSocket(socket)
|
|
|
|
: new WebSocketNodeSocket(new NodeSocket(socket)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:55:30 +00:00
|
|
|
public getUnderlyingSocket(): net.Socket {
|
|
|
|
const socket = this.getSocket();
|
|
|
|
return socket instanceof NodeSocket
|
|
|
|
? socket.socket
|
|
|
|
: (socket as WebSocketNodeSocket).socket.socket;
|
2019-07-01 23:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a handshake to get a connection request.
|
|
|
|
*/
|
|
|
|
public handshake(): Promise<ConnectionTypeRequest> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const handler = this.onControlMessage((rawMessage) => {
|
|
|
|
try {
|
|
|
|
const message = JSON.parse(rawMessage.toString());
|
|
|
|
switch (message.type) {
|
|
|
|
case "auth": return this.authenticate(message);
|
|
|
|
case "connectionType":
|
|
|
|
handler.dispose();
|
|
|
|
return resolve(message);
|
|
|
|
default: throw new Error("Unrecognized message type");
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
handler.dispose();
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: This ignores the authentication process entirely for now.
|
|
|
|
*/
|
|
|
|
private authenticate(_message: AuthRequest): void {
|
2019-07-19 22:43:54 +00:00
|
|
|
this.sendMessage({ type: "sign", data: "" });
|
2019-07-01 23:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: implement.
|
|
|
|
*/
|
|
|
|
public tunnel(): void {
|
|
|
|
throw new Error("Tunnel is not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a handshake message. In the case of the extension host, it just sends
|
|
|
|
* back a debug port.
|
|
|
|
*/
|
|
|
|
public sendMessage(message: HandshakeMessage | { debugPort?: number } ): void {
|
|
|
|
this.sendControl(VSBuffer.fromString(JSON.stringify(message)));
|
|
|
|
}
|
|
|
|
}
|