1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
package ssh
import (
"io"
"strconv"
"strings"
"sync"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/subprocess"
"github.com/git-lfs/git-lfs/v3/tr"
)
type PktlineConnection struct {
r io.ReadCloser
w io.WriteCloser
mu sync.Mutex
cmd *subprocess.Cmd
pl Pktline
}
func (conn *PktlineConnection) Lock() {
conn.mu.Lock()
}
func (conn *PktlineConnection) Unlock() {
conn.mu.Unlock()
}
func (conn *PktlineConnection) Start() error {
conn.Lock()
defer conn.Unlock()
return conn.negotiateVersion()
}
func (conn *PktlineConnection) End() error {
conn.Lock()
defer conn.Unlock()
err := conn.SendMessage("quit", nil)
if err != nil {
return err
}
_, err = conn.ReadStatus()
conn.r.Close()
conn.w.Close()
conn.cmd.Wait()
return err
}
func (conn *PktlineConnection) negotiateVersion() error {
pkts, err := conn.pl.ReadPacketList()
if err != nil {
return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unable to read capabilities)"), err)
}
ok := false
for _, line := range pkts {
if line == "version=1" {
ok = true
}
}
if !ok {
return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (missing version=1)"), nil)
}
err = conn.SendMessage("version 1", nil)
if err != nil {
return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unable to send version)"), err)
}
status, args, _, err := conn.ReadStatusWithLines()
if err != nil {
return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unable to read status)"), err)
}
if status != 200 {
text := tr.Tr.Get("no error provided")
if len(args) > 0 {
text = tr.Tr.Get("server said: %q", args[0])
}
return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unexpected status %d; %s)", status, text), nil)
}
return nil
}
func (conn *PktlineConnection) SendMessage(command string, args []string) error {
err := conn.pl.WritePacketText(command)
if err != nil {
return err
}
for _, arg := range args {
err = conn.pl.WritePacketText(arg)
if err != nil {
return err
}
}
return conn.pl.WriteFlush()
}
func (conn *PktlineConnection) SendMessageWithLines(command string, args []string, lines []string) error {
err := conn.pl.WritePacketText(command)
if err != nil {
return err
}
for _, arg := range args {
err = conn.pl.WritePacketText(arg)
if err != nil {
return err
}
}
err = conn.pl.WriteDelim()
if err != nil {
return err
}
for _, line := range lines {
err = conn.pl.WritePacketText(line)
if err != nil {
return err
}
}
return conn.pl.WriteFlush()
}
func (conn *PktlineConnection) SendMessageWithData(command string, args []string, data io.Reader) error {
err := conn.pl.WritePacketText(command)
if err != nil {
return err
}
for _, arg := range args {
err = conn.pl.WritePacketText(arg)
if err != nil {
return err
}
}
err = conn.pl.WriteDelim()
if err != nil {
return err
}
buf := make([]byte, 32768)
for {
n, err := data.Read(buf)
if n > 0 {
err := conn.pl.WritePacket(buf[0:n])
if err != nil {
return err
}
}
if err != nil {
break
}
}
return conn.pl.WriteFlush()
}
func (conn *PktlineConnection) ReadStatus() (int, error) {
status := 0
seenStatus := false
for {
s, pktLen, err := conn.pl.ReadPacketTextWithLength()
if err != nil {
return 0, errors.NewProtocolError(tr.Tr.Get("error reading packet"), err)
}
switch {
case pktLen == 0:
if !seenStatus {
return 0, errors.NewProtocolError(tr.Tr.Get("no status seen"), nil)
}
return status, nil
case !seenStatus:
ok := false
if strings.HasPrefix(s, "status ") {
status, err = strconv.Atoi(s[7:])
ok = err == nil
}
if !ok {
return 0, errors.NewProtocolError(tr.Tr.Get("expected status line, got %q", s), err)
}
seenStatus = true
default:
return 0, errors.NewProtocolError(tr.Tr.Get("unexpected data, got %q", s), err)
}
}
}
// ReadStatusWithData reads a status, arguments, and any binary data. Note that
// the reader must be fully exhausted before invoking any other read methods.
func (conn *PktlineConnection) ReadStatusWithData() (int, []string, io.Reader, error) {
args := make([]string, 0, 100)
status := 0
seenStatus := false
for {
s, pktLen, err := conn.pl.ReadPacketTextWithLength()
if err != nil {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("error reading packet"), err)
}
if pktLen == 0 {
if !seenStatus {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("no status seen"), nil)
}
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("unexpected flush packet"), nil)
} else if !seenStatus {
ok := false
if strings.HasPrefix(s, "status ") {
status, err = strconv.Atoi(s[7:])
ok = err == nil
}
if !ok {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("expected status line, got %q", s), err)
}
seenStatus = true
} else if pktLen == 1 {
break
} else {
args = append(args, s)
}
}
return status, args, pktlineReader(conn.pl), nil
}
// ReadStatusWithLines reads a status, arguments, and a set of text lines.
func (conn *PktlineConnection) ReadStatusWithLines() (int, []string, []string, error) {
args := make([]string, 0, 100)
lines := make([]string, 0, 100)
status := 0
seenDelim := false
seenStatus := false
for {
s, pktLen, err := conn.pl.ReadPacketTextWithLength()
if err != nil {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("error reading packet"), err)
}
switch {
case pktLen == 0:
if !seenStatus {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("no status seen"), nil)
}
return status, args, lines, nil
case seenDelim:
lines = append(lines, s)
case !seenStatus:
ok := false
if strings.HasPrefix(s, "status ") {
status, err = strconv.Atoi(s[7:])
ok = err == nil
}
if !ok {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("expected status line, got %q", s), err)
}
seenStatus = true
case pktLen == 1:
if seenDelim {
return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("unexpected delimiter packet"), nil)
}
seenDelim = true
default:
args = append(args, s)
}
}
}
|