"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.SshClient = void 0;
const net = require("net");
const dev_tunnels_ssh_1 = require("@microsoft/dev-tunnels-ssh");
/**
 * Enables opening an SSH session over a TCP connection.
 *
 * It's possible to create an `SshClientSession` over any `Stream` instance;
 * this class is merely a convenient helper that manages creating a session
 * over a Node.js TCP `Socket`.
 */
class SshClient {
    constructor(config) {
        this.config = config;
        this.sessions = [];
        /**
         * Gets or sets a function that handles trace messages associated with the client session.
         *
         * By default, no messages are traced. To enable tracing, set this property to a function
         * that routes the message to console.log, a file, or anywhere else.
         *
         * @param level The level of message being traced: error, warning, info, or verbose.
         * @param eventId An integer that identifies the type of event. Normally this is one of
         * the values from `SshTraceEventIds`, but extensions may define additional event IDs.
         * @param msg A description of the event (non-localized).
         * @param err Optional `Error` object associated with the event, often included with
         * warning or error events. While the `Error.message` property is typically included as
         * (part of) the `msg` parameter, the error object may contain additional useful context
         * such as the stack trace.
         */
        this.trace = (level, eventId, msg, err) => { };
        if (!config)
            throw new TypeError('SshSessionConfiguration is required.');
    }
    async openSession(serverHost, serverPort, cancellation) {
        if (!serverHost)
            throw new TypeError('Server host is reqiured.');
        const stream = await this.openConnection(serverHost, serverPort, cancellation);
        const session = new dev_tunnels_ssh_1.SshClientSession(this.config);
        session.trace = this.trace;
        await session.connect(stream, cancellation);
        this.sessions.push(session);
        return session;
    }
    async openConnection(serverHost, serverPort, cancellation) {
        const socket = new net.Socket();
        await new Promise((resolve, reject) => {
            socket.on('connect', resolve);
            socket.on('error', reject);
            if (cancellation) {
                if (cancellation.isCancellationRequested) {
                    reject(new dev_tunnels_ssh_1.CancellationError());
                    return;
                }
                cancellation.onCancellationRequested(reject);
            }
            socket.connect(serverPort || SshClient.defaultServerPort, serverHost);
        });
        return new dev_tunnels_ssh_1.NodeStream(socket);
    }
    async reconnectSession(session, serverHost, serverPort, cancellation) {
        const stream = await this.openConnection(serverHost, serverPort, cancellation);
        await session.reconnect(stream, cancellation);
    }
    dispose() {
        while (this.sessions.length > 0) {
            const session = this.sessions.shift();
            session.dispose();
        }
    }
}
exports.SshClient = SshClient;
SshClient.defaultServerPort = 22;
//# sourceMappingURL=https://main.vscode-cdn.net/sourcemaps/6a44c352bd24569c417e530095901b649960f9f8/node_modules/@microsoft/dev-tunnels-ssh-tcp/sshClient.js.map