2016-06-15 09:50:02 +08:00
|
|
|
|
package sftp
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-13 16:13:33 +08:00
|
|
|
|
"context"
|
2016-06-15 09:50:02 +08:00
|
|
|
|
"encoding"
|
2021-06-05 05:18:41 +08:00
|
|
|
|
"fmt"
|
2016-06-15 09:50:02 +08:00
|
|
|
|
"io"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// conn implements a bidirectional channel on which client and server
|
2016-06-15 16:07:14 +08:00
|
|
|
|
// connections are multiplexed.
|
2016-06-15 09:50:02 +08:00
|
|
|
|
type conn struct {
|
2016-06-15 18:19:51 +08:00
|
|
|
|
io.Reader
|
|
|
|
|
io.WriteCloser
|
2020-03-18 16:36:07 +08:00
|
|
|
|
// this is the same allocator used in packet manager
|
|
|
|
|
alloc *allocator
|
2016-06-15 09:50:02 +08:00
|
|
|
|
sync.Mutex // used to serialise writes to sendPacket
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 16:36:07 +08:00
|
|
|
|
// the orderID is used in server mode if the allocator is enabled.
|
2023-07-21 16:17:22 +08:00
|
|
|
|
// For the client mode just pass 0.
|
|
|
|
|
// It returns io.EOF if the connection is closed and
|
|
|
|
|
// there are no more packets to read.
|
2025-05-30 21:29:21 +08:00
|
|
|
|
func (c *conn) recvPacket(orderID uint32) (fxp, []byte, error) {
|
2020-03-18 16:36:07 +08:00
|
|
|
|
return recvPacket(c, c.alloc, orderID)
|
2016-06-15 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *conn) sendPacket(m encoding.BinaryMarshaler) error {
|
|
|
|
|
c.Lock()
|
|
|
|
|
defer c.Unlock()
|
2021-03-17 19:03:24 +08:00
|
|
|
|
|
2016-06-15 09:50:02 +08:00
|
|
|
|
return sendPacket(c, m)
|
|
|
|
|
}
|
2016-06-15 16:07:14 +08:00
|
|
|
|
|
2020-03-08 07:05:46 +08:00
|
|
|
|
func (c *conn) Close() error {
|
|
|
|
|
c.Lock()
|
|
|
|
|
defer c.Unlock()
|
|
|
|
|
return c.WriteCloser.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 16:07:14 +08:00
|
|
|
|
type clientConn struct {
|
|
|
|
|
conn
|
2020-10-29 06:20:19 +08:00
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
2025-05-23 11:04:57 +08:00
|
|
|
|
wait func() error // if non-nil, call this during Wait() to get a possible remote status error.
|
|
|
|
|
|
2016-06-15 16:07:14 +08:00
|
|
|
|
sync.Mutex // protects inflight
|
|
|
|
|
inflight map[uint32]chan<- result // outstanding requests
|
2018-12-05 14:47:03 +08:00
|
|
|
|
|
2018-12-05 16:30:09 +08:00
|
|
|
|
closed chan struct{}
|
|
|
|
|
err error
|
2018-12-03 17:46:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait blocks until the conn has shut down, and return the error
|
2018-12-05 14:47:03 +08:00
|
|
|
|
// causing the shutdown. It can be called concurrently from multiple
|
|
|
|
|
// goroutines.
|
2018-12-03 17:46:45 +08:00
|
|
|
|
func (c *clientConn) Wait() error {
|
2018-12-05 16:30:09 +08:00
|
|
|
|
<-c.closed
|
2025-05-23 18:25:23 +08:00
|
|
|
|
|
|
|
|
|
if c.wait == nil {
|
|
|
|
|
// Only return this error if c.wait won't return something more useful.
|
|
|
|
|
return c.err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := c.wait(); err != nil {
|
|
|
|
|
|
|
|
|
|
// TODO: when https://github.com/golang/go/issues/35025 is fixed,
|
|
|
|
|
// we can remove this if block entirely.
|
|
|
|
|
// Right now, it’s always going to return this, so it is not useful.
|
|
|
|
|
// But we have this code here so that as soon as the ssh library is updated,
|
|
|
|
|
// we can return a possibly more useful error.
|
|
|
|
|
if err.Error() == "ssh: session not started" {
|
|
|
|
|
return c.err
|
2025-05-23 11:04:57 +08:00
|
|
|
|
}
|
2025-05-23 18:25:23 +08:00
|
|
|
|
|
|
|
|
|
return err
|
2025-05-23 11:04:57 +08:00
|
|
|
|
}
|
2025-05-23 18:25:23 +08:00
|
|
|
|
|
|
|
|
|
// c.wait returned no error; so, let's return something maybe more useful.
|
2018-12-05 14:47:03 +08:00
|
|
|
|
return c.err
|
2016-06-15 16:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 16:23:51 +08:00
|
|
|
|
// Close closes the SFTP session.
|
|
|
|
|
func (c *clientConn) Close() error {
|
2016-06-15 18:04:25 +08:00
|
|
|
|
defer c.wg.Wait()
|
|
|
|
|
return c.conn.Close()
|
2016-06-15 16:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 16:07:14 +08:00
|
|
|
|
// recv continuously reads from the server and forwards responses to the
|
|
|
|
|
// appropriate channel.
|
|
|
|
|
func (c *clientConn) recv() error {
|
2020-10-29 06:20:19 +08:00
|
|
|
|
defer c.conn.Close()
|
|
|
|
|
|
2016-06-15 16:07:14 +08:00
|
|
|
|
for {
|
2020-03-18 16:36:07 +08:00
|
|
|
|
typ, data, err := c.recvPacket(0)
|
2016-06-15 16:07:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-03-16 00:53:09 +08:00
|
|
|
|
sid, _, err := unmarshalUint32Safe(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-10-29 06:20:19 +08:00
|
|
|
|
|
|
|
|
|
ch, ok := c.getChannel(sid)
|
2016-06-15 16:07:14 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
// This is an unexpected occurrence. Send the error
|
|
|
|
|
// back to all listeners so that they terminate
|
|
|
|
|
// gracefully.
|
2021-06-05 05:18:41 +08:00
|
|
|
|
return fmt.Errorf("sid not found: %d", sid)
|
2016-06-15 16:07:14 +08:00
|
|
|
|
}
|
2020-10-29 06:20:19 +08:00
|
|
|
|
|
2016-06-15 16:07:14 +08:00
|
|
|
|
ch <- result{typ: typ, data: data}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 06:20:19 +08:00
|
|
|
|
func (c *clientConn) putChannel(ch chan<- result, sid uint32) bool {
|
|
|
|
|
c.Lock()
|
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-c.closed:
|
|
|
|
|
// already closed with broadcastErr, return error on chan.
|
2020-12-08 23:59:24 +08:00
|
|
|
|
ch <- result{err: ErrSSHFxConnectionLost}
|
2020-10-29 06:20:19 +08:00
|
|
|
|
return false
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.inflight[sid] = ch
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *clientConn) getChannel(sid uint32) (chan<- result, bool) {
|
|
|
|
|
c.Lock()
|
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
|
|
ch, ok := c.inflight[sid]
|
|
|
|
|
delete(c.inflight, sid)
|
|
|
|
|
|
|
|
|
|
return ch, ok
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 16:30:05 +08:00
|
|
|
|
// result captures the result of receiving the a packet from the server
|
|
|
|
|
type result struct {
|
2025-05-30 21:29:21 +08:00
|
|
|
|
typ fxp
|
2016-06-15 16:30:05 +08:00
|
|
|
|
data []byte
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type idmarshaler interface {
|
|
|
|
|
id() uint32
|
|
|
|
|
encoding.BinaryMarshaler
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-30 21:29:21 +08:00
|
|
|
|
func (c *clientConn) sendPacket(ctx context.Context, ch chan result, p idmarshaler) (fxp, []byte, error) {
|
2020-10-29 06:20:19 +08:00
|
|
|
|
if cap(ch) < 1 {
|
|
|
|
|
ch = make(chan result, 1)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 16:30:05 +08:00
|
|
|
|
c.dispatchRequest(ch, p)
|
2023-11-13 16:13:33 +08:00
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return 0, nil, ctx.Err()
|
|
|
|
|
case s := <-ch:
|
|
|
|
|
return s.typ, s.data, s.err
|
|
|
|
|
}
|
2016-06-15 16:30:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 06:20:19 +08:00
|
|
|
|
// dispatchRequest should ideally only be called by race-detection tests outside of this file,
|
|
|
|
|
// where you have to ensure two packets are in flight sequentially after each other.
|
2016-06-15 16:07:14 +08:00
|
|
|
|
func (c *clientConn) dispatchRequest(ch chan<- result, p idmarshaler) {
|
2020-10-29 06:20:19 +08:00
|
|
|
|
sid := p.id()
|
|
|
|
|
|
|
|
|
|
if !c.putChannel(ch, sid) {
|
|
|
|
|
// already closed.
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 16:57:05 +08:00
|
|
|
|
if err := c.conn.sendPacket(p); err != nil {
|
2020-10-29 06:20:19 +08:00
|
|
|
|
if ch, ok := c.getChannel(sid); ok {
|
|
|
|
|
ch <- result{err: err}
|
|
|
|
|
}
|
2016-06-15 16:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// broadcastErr sends an error to all goroutines waiting for a response.
|
|
|
|
|
func (c *clientConn) broadcastErr(err error) {
|
|
|
|
|
c.Lock()
|
2020-10-29 06:20:19 +08:00
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
2020-12-08 23:59:24 +08:00
|
|
|
|
bcastRes := result{err: ErrSSHFxConnectionLost}
|
2020-10-29 06:20:19 +08:00
|
|
|
|
for sid, ch := range c.inflight {
|
2020-11-02 21:02:47 +08:00
|
|
|
|
ch <- bcastRes
|
2020-10-29 06:20:19 +08:00
|
|
|
|
|
|
|
|
|
// Replace the chan in inflight,
|
|
|
|
|
// we have hijacked this chan,
|
|
|
|
|
// and this guarantees always-only-once sending.
|
|
|
|
|
c.inflight[sid] = make(chan<- result, 1)
|
2016-06-15 16:07:14 +08:00
|
|
|
|
}
|
2020-10-29 06:20:19 +08:00
|
|
|
|
|
2018-12-05 16:30:09 +08:00
|
|
|
|
c.err = err
|
|
|
|
|
close(c.closed)
|
2016-06-15 16:07:14 +08:00
|
|
|
|
}
|
2016-06-15 19:08:29 +08:00
|
|
|
|
|
|
|
|
|
type serverConn struct {
|
|
|
|
|
conn
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 20:00:27 +08:00
|
|
|
|
func (s *serverConn) sendError(id uint32, err error) error {
|
|
|
|
|
return s.sendPacket(statusFromError(id, err))
|
2016-06-15 19:08:29 +08:00
|
|
|
|
}
|