I'm creating a SSH bastion server, which essentially connects the client to multiple SSH servers. The client should be able to seamlessly switch between SSH servers without losing connection. When a client connects, I connect to the first SSH server and then use io.copy in goroutines to pipe responses to/from the server to the client.However, when I connect to the second server, I need to be able to pipe response from the second server to the client.
Here is my attempt at the proxy. It works for the first connection, but when I get to the second one, the client connection closes.
package proxyio
import (
"io"
"github.com/sirupsen/logrus"
)
// Reader wraps and io.reader
func Reader(reader io.Reader) ReaderProxy {
return ReaderProxy{reader: reader}
}
type ReaderProxy struct {
reader io.Reader
}
// Read from reader
func (r ReaderProxy) Read(p []byte) (n int, err error) {
if n, err = r.reader.Read(p); err != nil {
logrus.Errorf("Failed to read: %v", err)
return 0, err
}
return
}
// Switch reader
func (r ReaderProxy) Switch(reader io.Reader) {
r.reader = reader
}
// Writer wraps an io.Writer
func Writer(writer io.Writer) WriterProxy {
return WriterProxy{writer: writer}
}
type WriterProxy struct {
writer io.Writer
}
// Write
func (w WriterProxy) Write(p []byte) (n int, err error) {
if n, err = w.writer.Write(p); err != nil {
logrus.Errorf("Failed to write: %v", err)
return 0, err
}
return
}
// Switch writer
func (w WriterProxy) Switch(writer io.Writer) {
w.writer = writer
}