53 lines
858 B
Go
53 lines
858 B
Go
package ssh
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
type DupeChannel struct {
|
|
ssh.Channel
|
|
|
|
// Reads writes read actions.
|
|
Reads io.WriteCloser
|
|
|
|
// Writer writes write actions.
|
|
Writes io.WriteCloser
|
|
}
|
|
|
|
func (c DupeChannel) Close() error {
|
|
var errs []error
|
|
for _, closer := range []io.Closer{
|
|
c.Channel,
|
|
c.Reads,
|
|
c.Writes,
|
|
} {
|
|
if closer == nil {
|
|
continue
|
|
}
|
|
if cerr := closer.Close(); cerr != nil {
|
|
errs = append(errs, cerr)
|
|
}
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
func (c DupeChannel) Read(p []byte) (n int, err error) {
|
|
if c.Reads == nil {
|
|
return c.Channel.Read(p)
|
|
}
|
|
return io.TeeReader(c.Channel, c.Reads).Read(p)
|
|
}
|
|
|
|
func (c DupeChannel) Write(p []byte) (n int, err error) {
|
|
if c.Writes == nil {
|
|
return c.Channel.Write(p)
|
|
}
|
|
if n, err = c.Channel.Write(p); n > 0 {
|
|
_, _ = c.Writes.Write(p[:])
|
|
}
|
|
return
|
|
}
|