diff --git a/internal/bsr/convert/convert.go b/internal/bsr/convert/convert.go index 2b2d7ec7b..bb6538a58 100644 --- a/internal/bsr/convert/convert.go +++ b/internal/bsr/convert/convert.go @@ -16,8 +16,11 @@ import ( // ToAsciicast accepts a bsr.Session and will convert the underlying BSR connection or channel file to an asciinema file. // The tempFs will be used to write the asciinema file to disk -// It returns an io.Reader to the converted asciinema file -// Supports WithChannelId() to indicate this conversion should occur on a channel on a multiplexed session +// It returns an io.Reader to the converted asciinema file. +// This supports the following options: +// - WithChannelId to indicate this conversion should occur on a channel on a multiplexed session +// - WithMinWidth to set a minimum width for the asciicast +// - WithMinHeigh to set a minimum height for the asciicast func ToAsciicast(ctx context.Context, session *bsr.Session, tmp storage.TempFile, connectionId string, options ...Option) (io.ReadCloser, error) { const op = "convert.ToAsciicast" @@ -65,7 +68,7 @@ func ToAsciicast(ctx context.Context, session *bsr.Session, tmp storage.TempFile return nil, fmt.Errorf("%s: %w", op, err) } - return sshChannelToAsciicast(ctx, reqScanner, msgScanner, tmp) + return sshChannelToAsciicast(ctx, reqScanner, msgScanner, tmp, options...) default: return nil, fmt.Errorf("%s: %w", op, ErrUnsupportedProtocol) } diff --git a/internal/bsr/convert/internal/asciicast/asciicast.go b/internal/bsr/convert/internal/asciicast/asciicast.go index e38c17a2b..174d2ebc2 100644 --- a/internal/bsr/convert/internal/asciicast/asciicast.go +++ b/internal/bsr/convert/internal/asciicast/asciicast.go @@ -23,8 +23,8 @@ const ( // This is only the initial terminal size, so it does not seem to have any // real impact on playback. const ( - MinWidth uint32 = 80 - MinHeight uint32 = 120 + DefaultWidth uint32 = 80 + DefaultHeight uint32 = 24 ) // Sane defaults for the Env section of the Header. @@ -71,8 +71,8 @@ type Header struct { func NewHeader() *Header { return &Header{ Version: Version, - Width: MinWidth, - Height: MinHeight, + Width: DefaultWidth, + Height: DefaultHeight, Env: HeaderEnv{ Shell: DefaultShell, Term: DefaultTerm, diff --git a/internal/bsr/convert/internal/asciicast/asciicast_test.go b/internal/bsr/convert/internal/asciicast/asciicast_test.go index ba0dabf27..8ef17f0ce 100644 --- a/internal/bsr/convert/internal/asciicast/asciicast_test.go +++ b/internal/bsr/convert/internal/asciicast/asciicast_test.go @@ -59,7 +59,7 @@ func TestHeaderMarshal(t *testing.T) { { "default", asciicast.NewHeader(), - []byte(`{"version":2,"width":80,"height":120,"timestamp":-62135596800,"env":{"SHELL":"/bin/bash","TERM":"xterm"}}`), + []byte(`{"version":2,"width":80,"height":24,"timestamp":-62135596800,"env":{"SHELL":"/bin/bash","TERM":"xterm"}}`), nil, }, { diff --git a/internal/bsr/convert/options.go b/internal/bsr/convert/options.go index 49bb29361..a0266e276 100644 --- a/internal/bsr/convert/options.go +++ b/internal/bsr/convert/options.go @@ -18,6 +18,8 @@ type Option func(*options) // options = how options are represented type options struct { withChannelId string + withMinWidth uint32 + withMinHeight uint32 } func getDefaultOptions() options { @@ -30,3 +32,17 @@ func WithChannelId(id string) Option { o.withChannelId = id } } + +// WithMinWidth can be used to set a minimum width for playback. +func WithMinWidth(w uint32) Option { + return func(o *options) { + o.withMinWidth = w + } +} + +// WithMinHeight can be used to set a minimum height for playback. +func WithMinHeight(h uint32) Option { + return func(o *options) { + o.withMinHeight = h + } +} diff --git a/internal/bsr/convert/options_test.go b/internal/bsr/convert/options_test.go index a30fec526..343bec763 100644 --- a/internal/bsr/convert/options_test.go +++ b/internal/bsr/convert/options_test.go @@ -20,4 +20,20 @@ func Test_GetOpts(t *testing.T) { testOpts.withChannelId = channelId assert.Equal(opts, testOpts) }) + t.Run("WithMinWidth", func(t *testing.T) { + assert := assert.New(t) + width := uint32(23) + opts := getOpts(WithMinWidth(width)) + testOpts := getDefaultOptions() + testOpts.withMinWidth = width + assert.Equal(opts, testOpts) + }) + t.Run("WithMinHeight", func(t *testing.T) { + assert := assert.New(t) + height := uint32(64) + opts := getOpts(WithMinHeight(height)) + testOpts := getDefaultOptions() + testOpts.withMinHeight = height + assert.Equal(opts, testOpts) + }) } diff --git a/internal/bsr/convert/ssh.go b/internal/bsr/convert/ssh.go index 3120a6f69..07aa3147c 100644 --- a/internal/bsr/convert/ssh.go +++ b/internal/bsr/convert/ssh.go @@ -23,7 +23,7 @@ import ( // This also expects a io.ReadWriteSeeker that will be used to write the // asciicast. This is then reset and returned as a io.ReadCloser. The caller // should call Close on the returned io.ReadCloser after reading the asciicast. -func sshChannelToAsciicast(ctx context.Context, requestScanner *bsr.ChunkScanner, messagesScanner *bsr.ChunkScanner, w io.ReadWriteSeeker) (io.ReadCloser, error) { +func sshChannelToAsciicast(ctx context.Context, requestScanner *bsr.ChunkScanner, messagesScanner *bsr.ChunkScanner, w io.ReadWriteSeeker, options ...Option) (io.ReadCloser, error) { const op = "convert.sshChannelToAsciicast" switch { @@ -35,8 +35,17 @@ func sshChannelToAsciicast(ctx context.Context, requestScanner *bsr.ChunkScanner return nil, fmt.Errorf("%s: missing read write seeker: %w", op, bsr.ErrInvalidParameter) } + opts := getOpts(options...) + header := asciicast.NewHeader() + if opts.withMinWidth > 0 { + header.Width = opts.withMinWidth + } + if opts.withMinHeight > 0 { + header.Height = opts.withMinHeight + } + // Walk the requests to extract any information for asciicast header. This // will come from either a Pty and/or Env request. There could also be // additional WindowChange requests to resize the terminal. In picking an @@ -54,10 +63,10 @@ func sshChannelToAsciicast(ctx context.Context, requestScanner *bsr.ChunkScanner case ssh.PtyReqChunkType: cc := c.(*ssh.PtyRequest) - if cc.GetTerminalWidthCharacters() > asciicast.MinWidth { + if cc.GetTerminalWidthCharacters() > opts.withMinWidth { header.Width = cc.GetTerminalWidthCharacters() } - if cc.GetTerminalHeightRows() > asciicast.MinHeight { + if cc.GetTerminalHeightRows() > opts.withMinHeight { header.Height = cc.GetTerminalHeightRows() } if cc.GetTermEnvVar() != "" { diff --git a/internal/bsr/convert/ssh_test.go b/internal/bsr/convert/ssh_test.go index 4a1e6ef10..463397c35 100644 --- a/internal/bsr/convert/ssh_test.go +++ b/internal/bsr/convert/ssh_test.go @@ -49,6 +49,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { requestScanner *bsr.ChunkScanner messageScanner *bsr.ChunkScanner w io.ReadWriteSeeker + opts []Option want []byte wantErr error }{ @@ -97,7 +98,8 @@ func Test_sshChannelToAsciicast(t *testing.T) { }, ), newW(), - []byte(`{"version":2,"width":80,"height":120,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"xterm"}} + nil, + []byte(`{"version":2,"width":80,"height":24,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"xterm"}} `), nil, }, @@ -164,7 +166,144 @@ func Test_sshChannelToAsciicast(t *testing.T) { }, ), newW(), + nil, []byte(`{"version":2,"width":160,"height":200,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"kitty"}} +`), + nil, + }, + { + "tiny-pty-no-env-no-messages", + newScanner( + &bsr.HeaderChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts), + Type: bsr.ChunkHeader, + }, + Compression: bsr.NoCompression, + Encryption: bsr.NoEncryption, + SessionId: "sess_123456789", + }, + &ssh.PtyRequest{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts.Add(time.Microsecond)), + Type: ssh.PtyReqChunkType, + }, + PtyRequest: &sshv1.PtyRequest{ + RequestType: ssh.PtyRequestType, + WantReply: false, + TermEnvVar: "kitty", + TerminalWidthCharacters: 2, + TerminalHeightRows: 2, + TerminalWidthPixels: 0, + TerminalHeightPixels: 0, + EncodedTerminalMode: []byte{}, + }, + }, + &bsr.EndChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts.Add(time.Second)), + Type: bsr.ChunkEnd, + }, + }, + ), + newScanner( + &bsr.HeaderChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts), + Type: bsr.ChunkHeader, + }, + Compression: bsr.NoCompression, + Encryption: bsr.NoEncryption, + SessionId: "sess_123456789", + }, + &bsr.EndChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts.Add(time.Second)), + Type: bsr.ChunkEnd, + }, + }, + ), + newW(), + nil, + []byte(`{"version":2,"width":2,"height":2,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"kitty"}} +`), + nil, + }, + { + "tiny-pty-no-env-no-messages-min-width-min-height", + newScanner( + &bsr.HeaderChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts), + Type: bsr.ChunkHeader, + }, + Compression: bsr.NoCompression, + Encryption: bsr.NoEncryption, + SessionId: "sess_123456789", + }, + &ssh.PtyRequest{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts.Add(time.Microsecond)), + Type: ssh.PtyReqChunkType, + }, + PtyRequest: &sshv1.PtyRequest{ + RequestType: ssh.PtyRequestType, + WantReply: false, + TermEnvVar: "kitty", + TerminalWidthCharacters: 2, + TerminalHeightRows: 2, + TerminalWidthPixels: 0, + TerminalHeightPixels: 0, + EncodedTerminalMode: []byte{}, + }, + }, + &bsr.EndChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts.Add(time.Second)), + Type: bsr.ChunkEnd, + }, + }, + ), + newScanner( + &bsr.HeaderChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts), + Type: bsr.ChunkHeader, + }, + Compression: bsr.NoCompression, + Encryption: bsr.NoEncryption, + SessionId: "sess_123456789", + }, + &bsr.EndChunk{ + BaseChunk: &bsr.BaseChunk{ + Protocol: ssh.Protocol, + Direction: bsr.Inbound, + Timestamp: bsr.NewTimestamp(ts.Add(time.Second)), + Type: bsr.ChunkEnd, + }, + }, + ), + newW(), + []Option{WithMinWidth(60), WithMinHeight(26)}, + []byte(`{"version":2,"width":60,"height":26,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"kitty"}} `), nil, }, @@ -259,6 +398,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { }, ), newW(), + nil, []byte(`{"version":2,"width":220,"height":500,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"kitty"}} `), nil, @@ -340,6 +480,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { }, ), newW(), + nil, []byte(`{"version":2,"width":160,"height":200,"timestamp":1678963623,"env":{"SHELL":"/bin/fish","TERM":"kitty"}} `), nil, @@ -398,7 +539,8 @@ func Test_sshChannelToAsciicast(t *testing.T) { }, ), newW(), - []byte(`{"version":2,"width":80,"height":120,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"xterm"}} + nil, + []byte(`{"version":2,"width":80,"height":24,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"xterm"}} [0.000001,"o","ls -lash"] `), nil, @@ -466,7 +608,8 @@ func Test_sshChannelToAsciicast(t *testing.T) { }, ), newW(), - []byte(`{"version":2,"width":80,"height":120,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"xterm"}} + nil, + []byte(`{"version":2,"width":80,"height":24,"timestamp":1678963623,"env":{"SHELL":"/bin/bash","TERM":"xterm"}} [0.000001,"o","ls -lash"] [0.000002,"o","foo\r\n"] `), @@ -498,6 +641,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { ), newW(), nil, + nil, errors.New("convert.sshChannelToAsciicast: missing request scanner: invalid parameter"), }, { @@ -526,6 +670,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { nil, newW(), nil, + nil, errors.New("convert.sshChannelToAsciicast: missing message scanner: invalid parameter"), }, { @@ -574,6 +719,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { ), nil, nil, + nil, errors.New("convert.sshChannelToAsciicast: missing read write seeker: invalid parameter"), }, { @@ -620,6 +766,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { ), newW(), nil, + nil, errors.New("convert.sshChannelToAsciicast: bsr.ChunkWalk: data chunk before header: malformed bsr data file"), }, { @@ -679,6 +826,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { ), newW(), nil, + nil, errors.New("convert.sshChannelToAsciicast: bsr.ChunkWalk: multiple header chunks: malformed bsr data file"), }, } @@ -690,6 +838,7 @@ func Test_sshChannelToAsciicast(t *testing.T) { tc.requestScanner, tc.messageScanner, tc.w, + tc.opts..., ) if tc.wantErr != nil { require.EqualError(t, err, tc.wantErr.Error())