Documentation
¶
Overview ¶
Package sftp implements an SFTP version 3 server (draft-ietf-secsh-filexfer-02) that exports any github.com/go-filesystems/interface.Filesystem.
Every driver in the family — ext4, xfs, btrfs, zfs, ntfs, fat32, exfat, hfsplus, apfs, iso9660, squashfs, ufs, ffs, uefi, oci — becomes something a person can reach from a file manager, an editor or a shell, over one TCP port, with SSH keys and encryption, from a pure-Go binary with no cgo.
Why SFTP ¶
The client already exists, everywhere, and nobody has to install it.
- `sftp` on the command line ships with OpenSSH, which is present on macOS, on every Linux distribution, on the BSDs, and on Windows 10 and later as an optional feature that is on by default in current builds.
- Graphical clients are abundant and free or cheap: Cyberduck, FileZilla, WinSCP, Transmit.
- Editors speak it natively: VS Code, the JetBrains IDEs, Emacs by way of TRAMP.
- GNOME mounts sftp:// through gvfs as an ordinary unprivileged user, with no root and no entry in the kernel's mount table.
And the security is not bolted on. Authentication is SSH public keys and the channel is encrypted, both from golang.org/x/crypto/ssh — see github.com/go-filesystems/sftp/sshd. NFSv3, the sibling of this module, has neither: its AUTH_UNIX credentials are claims a client makes about itself and the wire cannot disagree. It is one TCP port, so a firewall can reason about it, and nothing needs privilege on either side: no mount, no kernel extension, no FSKit entitlement, no root.
The isolation this is built for ¶
One image file per client, one server process per client, each with its own keys. The isolation boundary is the image file plus the process — not a chroot, not uid mapping, not namespaces.
That is why Server takes exactly one github.com/go-filesystems/interface.Filesystem and holds no other capability. It never touches the host filesystem, never resolves a path against anything but the driver it was handed, and cannot be induced to: a client path is cleaned with ".." clamped at the root before it reaches the driver, so no sequence of components names anything outside the image, and a symbolic link inside the image resolves inside the image because the driver is the only thing doing the resolving. The tests assert this directly rather than leaving it to be inferred.
A Server is cheap: no goroutine until a session arrives, no listener of its own, no global state. Running a hundred of them on one machine, one per tenant, is the intended shape, and Server.Serve is deliberately handed a stream rather than a listener so that an embedding program — a microVM supervisor, say — can wire it into an SSH server it already runs.
Serving one ¶
fsys, err := fat32.Open("tenant.img", -1)
if err != nil {
return err
}
defer fsys.Close()
srv, err := sftp.New(fsys) // read-only by default
if err != nil {
return err
}
// Then either hand srv.Serve a stream you own, or let the sshd
// subpackage own the SSH side:
d, err := sshd.New(srv, sshd.Config{HostKeys: ..., AuthorizedKeys: ...})
Two honest gaps, both in the contract and not in this server ¶
Reads. github.com/go-filesystems/interface.Opener is exactly the shape of SSH_FXP_READ: OpenFile(path) returns a File whose ReadAt(buf, off) answers the request as asked. This module uses it when a driver has it. At the time of writing NO driver in the fleet implements it, so in practice every read goes through the fallback, which calls ReadFile — materialising the WHOLE file — once per READ request. An OpenSSH client reads in 32 KiB chunks, so fetching an n-byte file costs n/32768 full-file reads: quadratic. That is measured in the README rather than described.
The tempting fix — read the file once when the handle is opened and serve reads from that copy — is not taken, because it trades a quadratic time cost for a linear MEMORY cost and a 4 GiB image file becomes 4 GiB of resident memory in a process meant to run a hundred times over. The real fix is a driver implementing Opener, and leaving the cost visible is what keeps that pressure where it belongs.
Writes. SSH_FXP_WRITE carries an offset, and the base contract has only WriteFile, which replaces a file whole. A write at an offset therefore becomes read-modify-write of the entire file: O(filesize) per request, and O(filesize²) for a client streaming a file in chunks. This is the same wall github.com/go-filesystems/nfs hit and measured at 90 kB/s. Exports are consequently READ-ONLY BY DEFAULT — see ReadWrite — and the number for this module is in the README.
The escape is WritableFile: a File that also has WriteAt, Truncate and Sync. When a driver's File satisfies it, writes go to the offset directly and the wall disappears. The probe is here and tested; the implementations belong in the drivers.
Index ¶
Constants ¶
const DefaultMaxInFlight = 64
DefaultMaxInFlight is how many requests a session may have in progress at once unless WithMaxInFlight says otherwise: OpenSSH's sftp client keeps 64 outstanding (sftp-client.c, DEFAULT_NUM_REQUESTS).
const DefaultMaxInFlightTotal = 256
DefaultMaxInFlightTotal is the server-wide ceiling on requests in flight across every session unless WithMaxInFlightTotal says otherwise.
const DefaultPacketTimeout = 30 * time.Second
DefaultPacketTimeout is how long the rest of a packet may take to arrive once its first byte has, unless WithPacketTimeout says otherwise.
const DefaultReplyBatch = inflight.DefaultBatch
DefaultReplyBatch is how many bytes of replies a session coalesces into one write of its channel unless WithReplyBatch says otherwise: eight 32 KiB SSH packets.
const MaxInFlightLimit = 1024
MaxInFlightLimit is the ceiling WithMaxInFlight is clamped to.
Variables ¶
var ErrNilFilesystem = errors.New("sftp: nil filesystem")
ErrNilFilesystem reports New called with no filesystem.
It is caught at construction because the alternative is a nil dereference on the first client request — long after the mistake, in a per-session goroutine, with no recover, taking down every other tenant the process is serving.
var ErrNoInit = errors.New("sftp: first packet was not SSH_FXP_INIT")
ErrNoInit reports a peer whose first packet was not SSH_FXP_INIT.
The session cannot continue: version negotiation is the only thing that establishes what the following bytes mean, so guessing would be worse than stopping.
var ErrVersion = errors.New("sftp: unsupported client protocol version")
ErrVersion reports a peer demanding a protocol version this server cannot speak. See wire.Version.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Server)
Option configures a Server.
func PathOpsConcurrent ¶ added in v0.8.0
func PathOpsConcurrent() Option
PathOpsConcurrent asserts that the driver's read-only path calls (Stat, ListDir, ReadFile, ReadLink) may run concurrently with each other and with File I/O, which lets the server run them in parallel. The interface module does not promise it, so the embedder has to: it is right for a host directory (os calls), wrong for a driver with a shared cursor or cache.
func ReadWrite ¶
func ReadWrite() Option
ReadWrite makes the export writable.
Exports are READ-ONLY BY DEFAULT, for two reasons that both point the same way. Most of what this module is pointed at is a forensic or build artefact, and an accidental write to one is unrecoverable. And until a driver's File implements WritableFile, every write at a non-zero offset is a read-modify-write of the entire file — see the package documentation and the measurement in the README. Opting in should be a decision, not a default.
func WithMaxInFlight ¶ added in v0.8.0
WithMaxInFlight bounds how many requests one session processes at once.
The bound is enforced BEFORE a request is read: the session takes one of n buffers first, and when none is free it stops reading, so a client that pipelines faster than the server answers waits in TCP's window instead of in this process's memory. Memory per session is at most n packets in and n replies out. 1 restores v0.7.0's one-request-at-a-time behaviour; a non-positive value means DefaultMaxInFlight.
func WithMaxInFlightTotal ¶ added in v0.9.0
WithMaxInFlightTotal bounds the requests in flight across ALL sessions of the server, the figure a host serving many clients sizes memory by: each request in flight holds up to one packet in, one reply out and one read buffer, so memory is at most 3 x n x the packet limit (about 200 MiB at the defaults), however many sessions there are. A session that would exceed it stops reading its input until another request is answered, as OpenSSH's sftp-server stops reading while its output queue cannot take a maximum-size message (sftp-server.c, process_requests). A peer that does not read its replies keeps its tokens: the same is true of knfsd's threads and Samba's `aio max threads`, and the answer there is the same, to close idle or stalled connections. A non-positive value means DefaultMaxInFlightTotal.
func WithMaxPacket ¶
WithMaxPacket caps the size of a single SFTP packet, in bytes, counting the type byte and payload.
The default, wire.MaxPacket, accepts everything OpenSSH's client sends including its largest -B setting. Lowering it is how a host running many tenants bounds the memory any one session can make it hold. A non-positive value restores the default.
func WithPacketTimeout ¶ added in v0.10.1
WithPacketTimeout bounds how long the rest of a packet may take once its first byte has arrived. The request holds a slot and a token of the server-wide budget meanwhile, so without a bound a client that sends a length and stops holds them for ever. A packet that is not complete in time ends the session: the stream is closed (an SSH channel) or given a past read deadline (a net.Conn); a stream with neither is not interrupted. Zero means DefaultPacketTimeout; a negative value disables it. Waiting for the NEXT packet is not bounded and holds nothing.
func WithReplyBatch ¶ added in v0.11.0
WithReplyBatch bounds the bytes of replies that leave in one write. Replies that are ready together (they already leave in request order) are copied end to end into one buffer and written once, so the SSH channel splits the bytes into full packets instead of one short tail packet per reply, and a net.Conn pays one syscall for several replies, as OpenSSH's sftp-server does by writing its whole output queue at once (sftp-server.c, process_requests).
Memory is unchanged: a reply's slot and token are released only after the write, so the bytes held are what WithMaxInFlight and WithMaxInFlightTotal already count; the session adds one buffer of at most n bytes. Zero means DefaultReplyBatch; a negative value writes each reply on its own.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server exports one github.com/go-filesystems/interface.Filesystem over SFTP version 3.
One Server holds exactly one filesystem and no other capability. It has no listener, no goroutine and no global state until a session is handed to Server.Serve, which is what makes running one per tenant — the shape this module is built for — cheap rather than aspirational.
A Server is safe for concurrent use by any number of sessions.
func New ¶
func New(fsys filesystem.Filesystem, opts ...Option) (*Server, error)
New returns a Server exporting fsys, read-only unless ReadWrite is given.
func (*Server) Serve ¶
func (s *Server) Serve(rw io.ReadWriter) error
Serve runs one SFTP session over rw until the peer closes it or the stream fails, and returns nil on a clean close.
rw is one client's SFTP channel: in the ordinary case the SSH subsystem channel opened by github.com/go-filesystems/sftp/sshd, but deliberately only an io.ReadWriter, so a program that already runs an SSH server can hand this its own channel, and a test can hand it a pipe. Serve does not close rw — it did not open it.
Every handle the session opened is released before Serve returns, including when the client vanishes mid-transfer, which is what killing an `sftp` process does.
type TimeStat ¶
type TimeStat interface {
ModTime() int64 // seconds since the Unix epoch
}
TimeStat is the optional capability a driver's github.com/go-filesystems/interface.Stat may implement to report a real modification time.
It is the older probe, kept for a Stat that carries its own time. A filesystem with filesystem.TimesReader (interface v0.6.0) takes precedence over it (see attrsAt), and a driver with neither carries the server's start time in atime and mtime for every file.
type WritableFile ¶
type WritableFile = filesystem.WritableFile
WritableFile is the capability that removes this module's write wall.
It is an open file that can be written AT AN OFFSET, which is exactly what SSH_FXP_WRITE(handle, offset, data) asks for, and exactly what the base github.com/go-filesystems/interface.Filesystem contract cannot express: that contract's only write is WriteFile, which replaces a file WHOLE. So serving a 32 KiB write into the middle of a 100 MiB file without this capability means reading 100 MiB, splicing 32 KiB in, and writing 100 MiB back — per request. See [writeSplice] and the README for the measurement of both paths.
It is an alias for github.com/go-filesystems/interface.WritableFile, published in interface v0.3.0, rather than a local redeclaration. The alias exists purely so that this package's own documentation has somewhere to explain what the capability means TO AN SFTP SERVER — which is not the same question as what it means to the interface module — while the type itself stays the fleet's one canonical definition. A driver satisfies it or does not; there is no second contract here to drift out of sync.
This module never implements it. It probes for it on the File returned by github.com/go-filesystems/interface.Opener, uses it when present, and falls back to read-modify-write with a documented cost when absent.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
fat32demo
module
|
|
|
Package sshd is the SSH front-end for github.com/go-filesystems/sftp: it authenticates clients with public keys, accepts the "sftp" subsystem, and hands the resulting channel to the SFTP server.
|
Package sshd is the SSH front-end for github.com/go-filesystems/sftp: it authenticates clients with public keys, accepts the "sftp" subsystem, and hands the resulting channel to the SFTP server. |
|
Package wire is the SFTP version 3 codec: packet framing, message types, and the SSH binary encoding they are built from.
|
Package wire is the SFTP version 3 codec: packet framing, message types, and the SSH binary encoding they are built from. |
