Ejemplo n.º 1
0
    protected override async Task <PipePoint> ConnectImplAsync(CancellationToken cancel = default)
    {
        await Task.CompletedTask;

        cancel.ThrowIfCancellationRequested();

        SshClient ssh;

        switch (Settings.AuthType)
        {
        case SecureShellClientAuthType.Password:
            ssh = new SshClient(Settings.TargetName, Settings.TargetPort, Settings.Username, Settings.Password);
            break;

        default:
            throw new ArgumentException(nameof(Settings.AuthType));
        }

        ShellStream?stream            = null;
        PipePoint?  pipePointMySide   = null;
        PipePoint?  pipePointUserSide = null;
        PipePointSshShellStreamWrapper?pipePointWrapper = null;

        try
        {
            ssh.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, Settings.ConnectTimeoutMsecs);

            ssh.Connect();

            stream = ssh.CreateShellStream("test", uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, 65536);

            //while (true)
            //{
            //    byte[] data = new byte[256];

            //    int x = stream.Read(data, 0, data.Length);
            //    x._Print();
            //}

            // Stream に標準入出力を接続する
            pipePointMySide   = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
            pipePointUserSide = pipePointMySide.CounterPart._NullCheck();
            pipePointWrapper  = new PipePointSshShellStreamWrapper(pipePointMySide, stream);

            this.PipePointMySide   = pipePointMySide;
            this.PipePointUserSide = pipePointUserSide;
            this.PipePointWrapper  = pipePointWrapper;

            this.Ssh    = ssh;
            this.Stream = stream;

            return(this.PipePointUserSide);
        }
        catch
        {
            pipePointMySide._DisposeSafe();
            pipePointUserSide._DisposeSafe();
            pipePointWrapper._DisposeSafe();
            stream._DisposeSafe();
            ssh._DisposeSafe();

            throw;
        }
    }