| 1 | package ssh |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "sync" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/config" |
| 8 | "github.com/git-lfs/git-lfs/v3/errors" |
| 9 | "github.com/git-lfs/git-lfs/v3/subprocess" |
| 10 | "github.com/git-lfs/git-lfs/v3/tr" |
| 11 | "github.com/git-lfs/pktline" |
| 12 | "github.com/rubyist/tracerx" |
| 13 | ) |
| 14 | |
| 15 | type SSHTransfer struct { |
| 16 | lock *sync.RWMutex |
| 17 | conn []*PktlineConnection |
| 18 | osEnv config.Environment |
| 19 | gitEnv config.Environment |
| 20 | meta *SSHMetadata |
| 21 | operation string |
| 22 | multiplexing bool |
| 23 | controlPath string |
| 24 | } |
| 25 | |
| 26 | func NewSSHTransfer(osEnv config.Environment, gitEnv config.Environment, meta *SSHMetadata, operation string) (*SSHTransfer, error) { |
| 27 | conn, multiplexing, controlPath, err := startConnection(0, osEnv, gitEnv, meta, operation, "") |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | return &SSHTransfer{ |
| 32 | lock: &sync.RWMutex{}, |
| 33 | osEnv: osEnv, |
| 34 | gitEnv: gitEnv, |
| 35 | meta: meta, |
| 36 | operation: operation, |
| 37 | multiplexing: multiplexing, |
| 38 | controlPath: controlPath, |
| 39 | conn: []*PktlineConnection{conn}, |
| 40 | }, nil |
| 41 | } |
| 42 | |
| 43 | func startConnection(id int, osEnv config.Environment, gitEnv config.Environment, meta *SSHMetadata, operation string, multiplexControlPath string) (conn *PktlineConnection, multiplexing bool, controlPath string, err error) { |
| 44 | tracerx.Printf("spawning pure SSH connection (#%d)", id) |
| 45 | var errbuf bytes.Buffer |
| 46 | exe, args, multiplexing, controlPath := GetLFSExeAndArgs(osEnv, gitEnv, meta, "git-lfs-transfer", operation, true, multiplexControlPath) |
| 47 | cmd, err := subprocess.ExecCommand(exe, args...) |
| 48 | if err != nil { |
| 49 | return nil, false, "", err |
| 50 | } |
| 51 | r, err := cmd.StdoutPipe() |
| 52 | if err != nil { |
| 53 | return nil, false, "", err |
| 54 | } |
| 55 | w, err := cmd.StdinPipe() |
| 56 | if err != nil { |
| 57 | return nil, false, "", err |
| 58 | } |
| 59 | cmd.Stderr = &errbuf |
| 60 | err = cmd.Start() |
| 61 | if err != nil { |
| 62 | return nil, false, "", err |
| 63 | } |
| 64 | |
| 65 | var pl Pktline |
| 66 | if osEnv.Bool("GIT_TRACE_PACKET", false) { |
| 67 | pl = &TraceablePktline{id: id, pl: pktline.NewPktline(r, w)} |
| 68 | } else { |
| 69 | pl = pktline.NewPktline(r, w) |
| 70 | } |
| 71 | conn = &PktlineConnection{ |
| 72 | cmd: cmd, |
| 73 | pl: pl, |
| 74 | r: r, |
| 75 | w: w, |
| 76 | } |
| 77 | err = conn.Start() |
| 78 | if err != nil { |
| 79 | r.Close() |
| 80 | w.Close() |
| 81 | cmd.Wait() |
| 82 | err = errors.Join(err, errors.New(tr.Tr.Get("Failed to connect to remote SSH server: %s", cmd.Stderr))) |
| 83 | tracerx.Printf("pure SSH connection unsuccessful (#%d)", id) |
| 84 | } else { |
| 85 | tracerx.Printf("pure SSH connection successful (#%d)", id) |
| 86 | } |
| 87 | return conn, multiplexing, controlPath, err |
| 88 | } |
| 89 | |
| 90 | // Connection returns the nth connection (starting from 0) in this transfer |
| 91 | // instance or nil if there is no such item. |
| 92 | func (st *SSHTransfer) IsMultiplexingEnabled() bool { |
| 93 | return st.multiplexing |
| 94 | } |
| 95 | |
| 96 | // Connection returns the nth connection (starting from 0) in this transfer |
| 97 | // instance if it is initialized and otherwise initializes a new connection and |
| 98 | // saves it in the nth position. In all cases, nil is returned with an error |
| 99 | // if n is greater than the maximum number of connections, including when |
| 100 | // the connection array itself is nil. |
| 101 | func (st *SSHTransfer) Connection(n int) (*PktlineConnection, error) { |
| 102 | st.lock.RLock() |
| 103 | if n >= len(st.conn) { |
| 104 | st.lock.RUnlock() |
| 105 | return nil, errors.New(tr.Tr.Get("pure SSH connection unavailable (#%d)", n)) |
| 106 | } |
| 107 | if st.conn[n] != nil { |
| 108 | defer st.lock.RUnlock() |
| 109 | return st.conn[n], nil |
| 110 | } |
| 111 | st.lock.RUnlock() |
| 112 | |
| 113 | st.lock.Lock() |
| 114 | defer st.lock.Unlock() |
| 115 | if st.conn[n] != nil { |
| 116 | return st.conn[n], nil |
| 117 | } |
| 118 | conn, _, err := st.spawnConnection(n) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | st.conn[n] = conn |
| 123 | return conn, nil |
| 124 | } |
| 125 | |
| 126 | // ConnectionCount returns the number of connections this object has. |
| 127 | func (st *SSHTransfer) ConnectionCount() int { |
| 128 | st.lock.RLock() |
| 129 | defer st.lock.RUnlock() |
| 130 | return len(st.conn) |
| 131 | } |
| 132 | |
| 133 | // SetConnectionCount sets the number of connections to the specified number. |
| 134 | func (st *SSHTransfer) SetConnectionCount(n int) error { |
| 135 | st.lock.Lock() |
| 136 | defer st.lock.Unlock() |
| 137 | return st.setConnectionCount(n) |
| 138 | } |
| 139 | |
| 140 | // SetConnectionCountAtLeast sets the number of connections to be not less than |
| 141 | // the specified number. |
| 142 | func (st *SSHTransfer) SetConnectionCountAtLeast(n int) error { |
| 143 | st.lock.Lock() |
| 144 | defer st.lock.Unlock() |
| 145 | count := len(st.conn) |
| 146 | if n <= count { |
| 147 | return nil |
| 148 | } |
| 149 | return st.setConnectionCount(n) |
| 150 | } |
| 151 | |
| 152 | func (st *SSHTransfer) spawnConnection(n int) (*PktlineConnection, string, error) { |
| 153 | conn, _, controlPath, err := startConnection(n, st.osEnv, st.gitEnv, st.meta, st.operation, st.controlPath) |
| 154 | if err != nil { |
| 155 | tracerx.Printf("failed to spawn pure SSH connection (#%d): %s", n, err) |
| 156 | return nil, "", err |
| 157 | } |
| 158 | return conn, controlPath, err |
| 159 | } |
| 160 | |
| 161 | func (st *SSHTransfer) setConnectionCount(n int) error { |
| 162 | count := len(st.conn) |
| 163 | if n < count { |
| 164 | tn := n |
| 165 | if tn == 0 { |
| 166 | tn = 1 |
| 167 | } |
| 168 | for i, item := range st.conn[tn:count] { |
| 169 | if item == nil { |
| 170 | tracerx.Printf("skipping uninitialized lazy pure SSH connection (#%d) (resetting total from %d to %d)", tn+i, count, n) |
| 171 | continue |
| 172 | } |
| 173 | tracerx.Printf("terminating pure SSH connection (#%d) (resetting total from %d to %d)", tn+i, count, n) |
| 174 | if err := item.End(); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | } |
| 178 | st.conn = st.conn[0:tn] |
| 179 | } else if n > count { |
| 180 | for i := count; i < n; i++ { |
| 181 | if i == 0 { |
| 182 | conn, controlPath, err := st.spawnConnection(i) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | st.conn = append(st.conn, conn) |
| 187 | st.controlPath = controlPath |
| 188 | } else { |
| 189 | st.conn = append(st.conn, nil) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | if n == 0 && count > 0 { |
| 194 | tracerx.Printf("terminating pure SSH connection (#0) (resetting total from %d to %d)", count, n) |
| 195 | if err := st.conn[0].End(); err != nil { |
| 196 | return err |
| 197 | } |
| 198 | st.conn = nil |
| 199 | st.controlPath = "" |
| 200 | } |
| 201 | return nil |
| 202 | } |
| 203 | |
| 204 | func (st *SSHTransfer) Shutdown() error { |
| 205 | tracerx.Printf("shutting down pure SSH connections") |
| 206 | return st.SetConnectionCount(0) |
| 207 | } |