Documentation
¶
Overview ¶
Package sftpfs implements an absfs.Filer for SFTP (SSH File Transfer Protocol). It provides secure file operations over SSH using the github.com/pkg/sftp library.
Index ¶
- Variables
- func MultiUserPasswordAuth(users map[string]string) func(ssh.ConnMetadata, []byte) (*ssh.Permissions, error)
- func NewServerHandler(fs absfs.FileSystem) sftp.Handlers
- func SimplePasswordAuth(username, password string) func(ssh.ConnMetadata, []byte) (*ssh.Permissions, error)
- type AuthError
- type Config
- type File
- func (f *File) Close() error
- func (f *File) Name() string
- func (f *File) Read(b []byte) (int, error)
- func (f *File) ReadAt(b []byte, off int64) (int, error)
- func (f *File) ReadDir(n int) ([]iofs.DirEntry, error)
- func (f *File) Readdir(n int) ([]os.FileInfo, error)
- func (f *File) Readdirnames(n int) ([]string, error)
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Sync() error
- func (f *File) Truncate(size int64) error
- func (f *File) Write(b []byte) (int, error)
- func (f *File) WriteAt(b []byte, off int64) (int, error)
- func (f *File) WriteString(s string) (int, error)
- type FileSystem
- func (fs *FileSystem) Chmod(name string, mode os.FileMode) error
- func (fs *FileSystem) Chown(name string, uid, gid int) error
- func (fs *FileSystem) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (fs *FileSystem) Close() error
- func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error
- func (fs *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)
- func (fs *FileSystem) ReadDir(name string) (entries []iofs.DirEntry, err error)
- func (fs *FileSystem) ReadFile(name string) ([]byte, error)
- func (fs *FileSystem) Remove(name string) error
- func (fs *FileSystem) Rename(oldpath, newpath string) error
- func (fs *FileSystem) Stat(name string) (os.FileInfo, error)
- func (fs *FileSystem) Sub(dir string) (iofs.FS, error)
- type Server
- type ServerConfig
- type ServerHandler
Constants ¶
This section is empty.
Variables ¶
var ErrAuthFailed = &AuthError{msg: "authentication failed"}
ErrAuthFailed is returned when authentication fails.
var ErrNotDir = os.ErrInvalid
ErrNotDir is returned when a path is expected to be a directory but is not.
Functions ¶
func MultiUserPasswordAuth ¶
func MultiUserPasswordAuth(users map[string]string) func(ssh.ConnMetadata, []byte) (*ssh.Permissions, error)
MultiUserPasswordAuth returns a PasswordCallback that validates against a user/password map.
func NewServerHandler ¶
func NewServerHandler(fs absfs.FileSystem) sftp.Handlers
NewServerHandler creates SFTP handlers that serve the given absfs.FileSystem.
func SimplePasswordAuth ¶
func SimplePasswordAuth(username, password string) func(ssh.ConnMetadata, []byte) (*ssh.Permissions, error)
SimplePasswordAuth returns a PasswordCallback that validates a single user/password. This is a convenience function for simple authentication scenarios.
Types ¶
type AuthError ¶
type AuthError struct {
// contains filtered or unexported fields
}
AuthError represents an authentication failure.
type Config ¶
type Config struct {
Host string // Host address (e.g., "example.com:22")
User string // Username for authentication
Password string // Password for authentication (if using password auth)
Key []byte // Private key for authentication (if using key auth)
Timeout time.Duration // Connection timeout
}
Config contains the configuration for connecting to an SFTP server.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File wraps an sftp.File to implement absfs.File interface.
func (*File) Readdirnames ¶
Readdirnames reads directory entry names.
type FileSystem ¶
type FileSystem struct {
// contains filtered or unexported fields
}
FileSystem implements absfs.Filer for SFTP protocol.
func Dial ¶
func Dial(host, user, password string) (*FileSystem, error)
Dial creates a new SFTP filesystem by dialing the specified host. This is a convenience function for simple password-based authentication.
func DialWithKey ¶
func DialWithKey(host, user string, privateKey []byte) (*FileSystem, error)
DialWithKey creates a new SFTP filesystem using SSH key authentication.
func New ¶
func New(config *Config) (*FileSystem, error)
New creates a new SFTP filesystem with the given configuration.
func (*FileSystem) Chmod ¶
func (fs *FileSystem) Chmod(name string, mode os.FileMode) error
Chmod changes the mode of a file on the SFTP server.
func (*FileSystem) Chown ¶
func (fs *FileSystem) Chown(name string, uid, gid int) error
Chown changes the owner and group of a file on the SFTP server.
func (*FileSystem) Chtimes ¶
Chtimes changes the access and modification times of a file on the SFTP server.
func (*FileSystem) Mkdir ¶
func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error
Mkdir creates a directory on the SFTP server.
func (*FileSystem) ReadDir ¶
func (fs *FileSystem) ReadDir(name string) (entries []iofs.DirEntry, err error)
ReadDir reads the directory named by name and returns a list of directory entries.
func (*FileSystem) ReadFile ¶
func (fs *FileSystem) ReadFile(name string) ([]byte, error)
ReadFile reads the file named by name and returns the contents.
func (*FileSystem) Remove ¶
func (fs *FileSystem) Remove(name string) error
Remove removes a file or empty directory from the SFTP server.
func (*FileSystem) Rename ¶
func (fs *FileSystem) Rename(oldpath, newpath string) error
Rename renames a file on the SFTP server.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server provides an SFTP server backed by any absfs.FileSystem. It handles SSH connections and SFTP protocol negotiation.
func NewServer ¶
func NewServer(fs absfs.FileSystem, config *ServerConfig) *Server
NewServer creates a new SFTP server for the given filesystem.
Example usage:
fs, _ := memfs.NewFS()
// Load or generate host key
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
signer, _ := ssh.NewSignerFromKey(privateKey)
server := sftpfs.NewServer(fs, &sftpfs.ServerConfig{
HostKeys: []ssh.Signer{signer},
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
if c.User() == "admin" && string(pass) == "secret" {
return nil, nil
}
return nil, fmt.Errorf("invalid credentials")
},
})
listener, _ := net.Listen("tcp", ":2222")
server.Serve(listener)
func (*Server) SSHConfig ¶
func (s *Server) SSHConfig() *ssh.ServerConfig
SSHConfig returns the underlying SSH server configuration. This can be used to add additional configuration options.
type ServerConfig ¶
type ServerConfig struct {
// HostKeys are the private keys for the SSH server.
// At least one host key is required.
HostKeys []ssh.Signer
// PasswordCallback validates password authentication.
// If nil, password authentication is disabled.
PasswordCallback func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error)
// PublicKeyCallback validates public key authentication.
// If nil, public key authentication is disabled.
PublicKeyCallback func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error)
// NoClientAuth allows any client to connect without authentication.
// WARNING: Only use this for testing or trusted networks.
NoClientAuth bool
// MaxAuthTries specifies the maximum number of authentication attempts.
// If 0, defaults to 6.
MaxAuthTries int
// ServerVersion is the SSH server version string.
// If empty, defaults to "SSH-2.0-sftpfs".
ServerVersion string
}
ServerConfig holds configuration for the SFTP server.
type ServerHandler ¶
type ServerHandler struct {
// contains filtered or unexported fields
}
ServerHandler implements all four sftp.Handlers interfaces: FileReader, FileWriter, FileCmder, and FileLister. It adapts an absfs.FileSystem to serve files via SFTP protocol.
func (*ServerHandler) Filecmd ¶
func (h *ServerHandler) Filecmd(r *sftp.Request) error
Filecmd implements sftp.FileCmder. Handles file commands like mkdir, remove, rename, etc. Called for SFTP Methods: Setstat, Rename, Rmdir, Mkdir, Link, Symlink, Remove
func (*ServerHandler) Filelist ¶
Filelist implements sftp.FileLister. Returns a ListerAt for directory listings and file stat operations. Called for SFTP Methods: List, Stat, Readlink