Skip to content

Commit 4f34f86

Browse files
committed
ssh: export a transport interface
1 parent 9756ffd commit 4f34f86

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

ssh/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (ch *channel) writePacket(packet []byte) error {
212212
return io.EOF
213213
}
214214
ch.sentClose = (packet[0] == msgChannelClose)
215-
err := ch.mux.conn.writePacket(packet)
215+
err := ch.mux.conn.WritePacket(packet)
216216
ch.writeMu.Unlock()
217217
return err
218218
}

ssh/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ func NewClientConn(c net.Conn, addr string, config *ClientConfig) (Conn, <-chan
8888
return conn, conn.mux.incomingChannels, conn.mux.incomingRequests, nil
8989
}
9090

91+
func NewClientConnFromTransport(t Transport) (Conn, <-chan NewChannel, <-chan *Request, error) {
92+
conn := &connection{
93+
mux: newMux(t),
94+
}
95+
return conn, conn.mux.incomingChannels, conn.mux.incomingRequests, nil
96+
}
97+
9198
// clientHandshake performs the client side key exchange. See RFC 4253 Section
9299
// 7.
93100
func (c *connection) clientHandshake(dialAddress string, config *ClientConfig) error {

ssh/handshake.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ func (t *handshakeTransport) printPacket(p []byte, write bool) {
183183
}
184184
}
185185

186+
func (t *handshakeTransport) ReadPacket() ([]byte, error) {
187+
return t.readPacket()
188+
}
189+
186190
func (t *handshakeTransport) readPacket() ([]byte, error) {
187191
p, ok := <-t.incoming
188192
if !ok {
@@ -479,6 +483,10 @@ func (t *handshakeTransport) sendKexInit() error {
479483
return nil
480484
}
481485

486+
func (t *handshakeTransport) WritePacket(p []byte) error {
487+
return t.writePacket(p)
488+
}
489+
482490
func (t *handshakeTransport) writePacket(p []byte) error {
483491
switch p[0] {
484492
case msgKexInit:

ssh/mux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (c *chanList) dropAll() []*channel {
8686
// mux represents the state for the SSH connection protocol, which
8787
// multiplexes many channels onto a single packet transport.
8888
type mux struct {
89-
conn packetConn
89+
conn Transport
9090
chanList chanList
9191

9292
incomingChannels chan NewChannel
@@ -113,7 +113,7 @@ func (m *mux) Wait() error {
113113
}
114114

115115
// newMux returns a mux that runs over the given connection.
116-
func newMux(p packetConn) *mux {
116+
func newMux(p Transport) *mux {
117117
m := &mux{
118118
conn: p,
119119
incomingChannels: make(chan NewChannel, chanSize),
@@ -134,7 +134,7 @@ func (m *mux) sendMessage(msg interface{}) error {
134134
if debugMux {
135135
log.Printf("send global(%d): %#v", m.chanList.offset, msg)
136136
}
137-
return m.conn.writePacket(p)
137+
return m.conn.WritePacket(p)
138138
}
139139

140140
func (m *mux) SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error) {
@@ -212,7 +212,7 @@ func (m *mux) loop() {
212212

213213
// onePacket reads and processes one packet.
214214
func (m *mux) onePacket() error {
215-
packet, err := m.conn.readPacket()
215+
packet, err := m.conn.ReadPacket()
216216
if err != nil {
217217
return err
218218
}

ssh/transport.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ type packetConn interface {
3737
Close() error
3838
}
3939

40+
// Transport represents a connection that implements packet based operations as
41+
// specified by SSH Transport Protocol (RFC 4253).
42+
type Transport interface {
43+
// WritePacket encrypts and sends a packet of data to the remote peer.
44+
WritePacket([]byte) error
45+
46+
// ReadPacket reads and decrypts a packet of data from the remote peer. The
47+
// read is blocking. If error is nil then the returned byte slice is always
48+
// non-empty.
49+
ReadPacket() ([]byte, error)
50+
51+
// Close closes the connection with the remote peer.
52+
Close() error
53+
}
54+
4055
// transport is the keyingTransport that implements the SSH packet
4156
// protocol.
4257
type transport struct {

0 commit comments

Comments
 (0)