pkg / sftp

SFTP support for the go.crypto/ssh package
BSD 2-Clause "Simplified" License
1.5k stars 379 forks source link

Question: How does the library internally talk to the SFTP subsystem? #520

Closed hbgl closed 2 years ago

hbgl commented 2 years ago

Hi, let me preface this by saying that I am not a direct user of this package and I am not even a Go developer. I stumbled upon this project because I use the backup tool restic which is written in Go and which does use this package. With restic you can pass it a command as an option (e.g. -o sftp.command="foobar") that creates an SFTP client and then restic uses that client to send and receive data. I found that mechanism quite intricate so I wanted to know how it works, which is why I am here.

I read through the source code and here is what I gathered so far:

  1. An sftp client process is created (e.g. openssh-client). See example in restic.
  2. NewClientPipe is called with the stdin and stdout pipes from the process.
  3. The library then uses the stdin pipe to send raw SFTP packets to the sftp client process (see code).
  4. Raw sftp responses are read from the stdout pipe (see code).

Is this correct so far? Is there any external documentation about how to send and receive raw packages via stdin and stdout using the sftp client? I tried starting the sftp client and sending it packets via stdin but it only accepts text commands. I must be missing something here.

Thanks for reading this far and if there is a better channel to ask these sort of questions, please let me know.

puellanivis commented 2 years ago

Your step 1 is not entirely accurate. It does not start an sftp client process, it creates an ssh client process with the command-line argument -s sftp which is a raw connection to the SFTP as expected by this package, rather than the sftp client itself, which as you note only accepts text commands.

I’ve tested calling ssh mycomputer -s sftp and it does indeed open a raw SFTP pipe to the remote machine.

From point 2 onwards, you are correct.

hbgl commented 2 years ago

Many thanks!