| 1 | package sftp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "sync" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | type conn struct { |
| 14 | io.Reader |
| 15 | io.WriteCloser |
| 16 | |
| 17 | alloc *allocator |
| 18 | sync.Mutex |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | func (c *conn) recvPacket(orderID uint32) (fxp, []byte, error) { |
| 26 | return recvPacket(c, c.alloc, orderID) |
| 27 | } |
| 28 | |
| 29 | func (c *conn) sendPacket(m encoding.BinaryMarshaler) error { |
| 30 | c.Lock() |
| 31 | defer c.Unlock() |
| 32 | |
| 33 | return sendPacket(c, m) |
| 34 | } |
| 35 | |
| 36 | func (c *conn) Close() error { |
| 37 | c.Lock() |
| 38 | defer c.Unlock() |
| 39 | return c.WriteCloser.Close() |
| 40 | } |
| 41 | |
| 42 | type clientConn struct { |
| 43 | conn |
| 44 | wg sync.WaitGroup |
| 45 | |
| 46 | wait func() error |
| 47 | |
| 48 | sync.Mutex |
| 49 | inflight map[uint32]chan<- result |
| 50 | |
| 51 | closed chan struct{} |
| 52 | err error |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | func (c *clientConn) Wait() error { |
| 59 | <-c.closed |
| 60 | |
| 61 | if c.wait == nil { |
| 62 | |
| 63 | return c.err |
| 64 | } |
| 65 | |
| 66 | if err := c.wait(); err != nil { |
| 67 | |
| 68 | TODO: |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | if err.Error() == "ssh: session not started" { |
| 74 | return c.err |
| 75 | } |
| 76 | |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | |
| 81 | return c.err |
| 82 | } |
| 83 | |
| 84 | |
| 85 | func (c *clientConn) Close() error { |
| 86 | defer c.wg.Wait() |
| 87 | return c.conn.Close() |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | func (c *clientConn) recv() error { |
| 93 | defer c.conn.Close() |
| 94 | |
| 95 | for { |
| 96 | typ, data, err := c.recvPacket(0) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | sid, _, err := unmarshalUint32Safe(data) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | ch, ok := c.getChannel(sid) |
| 106 | if !ok { |
| 107 | |
| 108 | |
| 109 | |
| 110 | return fmt.Errorf("sid not found: %d", sid) |
| 111 | } |
| 112 | |
| 113 | ch <- result{typ: typ, data: data} |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func (c *clientConn) putChannel(ch chan<- result, sid uint32) bool { |
| 118 | c.Lock() |
| 119 | defer c.Unlock() |
| 120 | |
| 121 | select { |
| 122 | case <-c.closed: |
| 123 | |
| 124 | ch <- result{err: ErrSSHFxConnectionLost} |
| 125 | return false |
| 126 | default: |
| 127 | } |
| 128 | |
| 129 | c.inflight[sid] = ch |
| 130 | return true |
| 131 | } |
| 132 | |
| 133 | func (c *clientConn) getChannel(sid uint32) (chan<- result, bool) { |
| 134 | c.Lock() |
| 135 | defer c.Unlock() |
| 136 | |
| 137 | ch, ok := c.inflight[sid] |
| 138 | delete(c.inflight, sid) |
| 139 | |
| 140 | return ch, ok |
| 141 | } |
| 142 | |
| 143 | |
| 144 | type result struct { |
| 145 | typ fxp |
| 146 | data []byte |
| 147 | err error |
| 148 | } |
| 149 | |
| 150 | type idmarshaler interface { |
| 151 | id() uint32 |
| 152 | encoding.BinaryMarshaler |
| 153 | } |
| 154 | |
| 155 | func (c *clientConn) sendPacket(ctx context.Context, ch chan result, p idmarshaler) (fxp, []byte, error) { |
| 156 | if cap(ch) < 1 { |
| 157 | ch = make(chan result, 1) |
| 158 | } |
| 159 | |
| 160 | c.dispatchRequest(ch, p) |
| 161 | |
| 162 | select { |
| 163 | case <-ctx.Done(): |
| 164 | return 0, nil, ctx.Err() |
| 165 | case s := <-ch: |
| 166 | return s.typ, s.data, s.err |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | func (c *clientConn) dispatchRequest(ch chan<- result, p idmarshaler) { |
| 173 | sid := p.id() |
| 174 | |
| 175 | if !c.putChannel(ch, sid) { |
| 176 | |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | if err := c.conn.sendPacket(p); err != nil { |
| 181 | if ch, ok := c.getChannel(sid); ok { |
| 182 | ch <- result{err: err} |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | func (c *clientConn) broadcastErr(err error) { |
| 189 | c.Lock() |
| 190 | defer c.Unlock() |
| 191 | |
| 192 | bcastRes := result{err: ErrSSHFxConnectionLost} |
| 193 | for sid, ch := range c.inflight { |
| 194 | ch <- bcastRes |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | c.inflight[sid] = make(chan<- result, 1) |
| 200 | } |
| 201 | |
| 202 | c.err = err |
| 203 | close(c.closed) |
| 204 | } |
| 205 | |
| 206 | type serverConn struct { |
| 207 | conn |
| 208 | } |
| 209 | |
| 210 | func (s *serverConn) sendError(id uint32, err error) error { |
| 211 | return s.sendPacket(statusFromError(id, err)) |
| 212 | } |