Documentation ¶
Index ¶
- Variables
- func DefaultCommands() map[string]Command
- type Auth
- type Command
- type Context
- type DataSocket
- type DiscardLogger
- func (logger *DiscardLogger) Print(sessionID string, message interface{})
- func (logger *DiscardLogger) PrintCommand(sessionID string, command string, params string)
- func (logger *DiscardLogger) PrintResponse(sessionID string, code int, message string)
- func (logger *DiscardLogger) Printf(sessionID string, format string, v ...interface{})
- type Driver
- type FileInfo
- type Logger
- type MultiDriver
- func (driver *MultiDriver) DeleteDir(ctx *Context, path string) error
- func (driver *MultiDriver) DeleteFile(ctx *Context, path string) error
- func (driver *MultiDriver) GetFile(ctx *Context, path string, offset int64) (int64, io.ReadCloser, error)
- func (driver *MultiDriver) ListDir(ctx *Context, path string, callback func(os.FileInfo) error) error
- func (driver *MultiDriver) MakeDir(ctx *Context, path string) error
- func (driver *MultiDriver) PutFile(ctx *Context, destPath string, data io.Reader, offset int64) (int64, error)
- func (driver *MultiDriver) Rename(ctx *Context, fromPath string, toPath string) error
- func (driver *MultiDriver) Stat(ctx *Context, path string) (os.FileInfo, error)
- type Notifier
- type NullNotifier
- func (NullNotifier) AfterCurDirChanged(ctx *Context, oldCurDir, newCurDir string, err error)
- func (NullNotifier) AfterDirCreated(ctx *Context, dstPath string, err error)
- func (NullNotifier) AfterDirDeleted(ctx *Context, dstPath string, err error)
- func (NullNotifier) AfterFileDeleted(ctx *Context, dstPath string, err error)
- func (NullNotifier) AfterFileDownloaded(ctx *Context, dstPath string, size int64, err error)
- func (NullNotifier) AfterFilePut(ctx *Context, dstPath string, size int64, err error)
- func (NullNotifier) AfterUserLogin(ctx *Context, userName, password string, passMatched bool, err error)
- func (NullNotifier) BeforeChangeCurDir(ctx *Context, oldCurDir, newCurDir string)
- func (NullNotifier) BeforeCommand(ctx *Context, command string)
- func (NullNotifier) BeforeCreateDir(ctx *Context, dstPath string)
- func (NullNotifier) BeforeDeleteDir(ctx *Context, dstPath string)
- func (NullNotifier) BeforeDeleteFile(ctx *Context, dstPath string)
- func (NullNotifier) BeforeDownloadFile(ctx *Context, dstPath string)
- func (NullNotifier) BeforeLoginUser(ctx *Context, userName string)
- func (NullNotifier) BeforePutFile(ctx *Context, dstPath string)
- type Options
- type Perm
- type RegexAuth
- type Server
- type Session
- func (sess *Session) BuildPath(filename string) string
- func (sess *Session) Close()
- func (sess *Session) DataConn() DataSocket
- func (sess *Session) IsLogin() bool
- func (sess *Session) LoginUser() string
- func (sess *Session) Options() *Options
- func (sess *Session) PassivePort() int
- func (sess *Session) PublicIP() string
- func (sess *Session) RemoteAddr() net.Addr
- func (sess *Session) Serve()
- func (sess *Session) Server() *Server
- func (sess *Session) WriteMessage(code int, message string)
- type SimpleAuth
- type SimplePerm
- func (s *SimplePerm) ChGroup(string, string) error
- func (s *SimplePerm) ChMode(string, os.FileMode) error
- func (s *SimplePerm) ChOwner(string, string) error
- func (s *SimplePerm) GetGroup(string) (string, error)
- func (s *SimplePerm) GetMode(string) (os.FileMode, error)
- func (s *SimplePerm) GetOwner(string) (string, error)
- type StdLogger
- func (logger *StdLogger) Print(sessionID string, message interface{})
- func (logger *StdLogger) PrintCommand(sessionID string, command string, params string)
- func (logger *StdLogger) PrintResponse(sessionID string, code int, message string)
- func (logger *StdLogger) Printf(sessionID string, format string, v ...interface{})
Constants ¶
This section is empty.
Variables ¶
var ( // ErrServerClosed is returned by ListenAndServe() or Serve() when a shutdown was requested. ErrServerClosed = errors.New("ftp: Server closed") )
Functions ¶
func DefaultCommands ¶
DefaultCommands returns the default commands
Types ¶
type Command ¶
type Command interface { IsExtend() bool RequireParam() bool RequireAuth() bool Execute(*Session, string) }
Command represents a Command interface to a ftp command
type Context ¶
type Context struct { Sess *Session Data map[string]interface{} // share data between middlewares Cmd string // request command on this request Param string // request param on this request }
Context represents a context the driver may want to know
type DataSocket ¶
type DataSocket interface { Host() string Port() int // Read implements the standard io.Reader interface. Read(p []byte) (n int, err error) // ReadFrom implements the standard io.ReaderFrom interface. ReadFrom(r io.Reader) (int64, error) // Write implements the standard io.Writer interface. Write(p []byte) (n int, err error) // Close implements the standard io.Closer interface. Close() error }
DataSocket describes a data socket is used to send non-control data between the client and server.
type DiscardLogger ¶
type DiscardLogger struct{}
DiscardLogger represents a silent logger, produces no output
func (*DiscardLogger) Print ¶
func (logger *DiscardLogger) Print(sessionID string, message interface{})
Print implements Logger
func (*DiscardLogger) PrintCommand ¶
func (logger *DiscardLogger) PrintCommand(sessionID string, command string, params string)
PrintCommand implements Logger
func (*DiscardLogger) PrintResponse ¶
func (logger *DiscardLogger) PrintResponse(sessionID string, code int, message string)
PrintResponse implements Logger
func (*DiscardLogger) Printf ¶
func (logger *DiscardLogger) Printf(sessionID string, format string, v ...interface{})
Printf implements Logger
type Driver ¶
type Driver interface { // params - a file path // returns - a time indicating when the requested path was last modified // - an error if the file doesn't exist or the user lacks // permissions Stat(*Context, string) (os.FileInfo, error) // params - path, function on file or subdir found // returns - error // path ListDir(*Context, string, func(os.FileInfo) error) error // params - path // returns - nil if the directory was deleted or any error encountered DeleteDir(*Context, string) error // params - path // returns - nil if the file was deleted or any error encountered DeleteFile(*Context, string) error // params - from_path, to_path // returns - nil if the file was renamed or any error encountered Rename(*Context, string, string) error // params - path // returns - nil if the new directory was created or any error encountered MakeDir(*Context, string) error // params - path, filepos // returns - a string containing the file data to send to the client GetFile(*Context, string, int64) (int64, io.ReadCloser, error) // params - destination path, an io.Reader containing the file data // returns - the number of bytes written and the first error encountered while writing, if any. PutFile(*Context, string, io.Reader, int64) (int64, error) }
Driver is an interface that you will implement to create a driver for your chosen persistence layer. The server will create a new instance of your driver for each client that connects and delegate to it as required.
Note that if the driver also implements the Auth interface then this will be called instead of calling Options.Auth. This allows the Auth mechanism to change the driver configuration.
func NewMultiDriver ¶
NewMultiDriver creates a multi driver to combind multiple driver
type Logger ¶
type Logger interface { Print(sessionID string, message interface{}) Printf(sessionID string, format string, v ...interface{}) PrintCommand(sessionID string, command string, params string) PrintResponse(sessionID string, code int, message string) }
Logger represents an interface to record all ftp information and command
type MultiDriver ¶
type MultiDriver struct {
// contains filtered or unexported fields
}
MultiDriver represents a composite driver
func (*MultiDriver) DeleteDir ¶
func (driver *MultiDriver) DeleteDir(ctx *Context, path string) error
DeleteDir implements Driver
func (*MultiDriver) DeleteFile ¶
func (driver *MultiDriver) DeleteFile(ctx *Context, path string) error
DeleteFile implements Driver
func (*MultiDriver) GetFile ¶
func (driver *MultiDriver) GetFile(ctx *Context, path string, offset int64) (int64, io.ReadCloser, error)
GetFile implements Driver
func (*MultiDriver) ListDir ¶
func (driver *MultiDriver) ListDir(ctx *Context, path string, callback func(os.FileInfo) error) error
ListDir implements Driver
func (*MultiDriver) MakeDir ¶
func (driver *MultiDriver) MakeDir(ctx *Context, path string) error
MakeDir implements Driver
func (*MultiDriver) PutFile ¶
func (driver *MultiDriver) PutFile(ctx *Context, destPath string, data io.Reader, offset int64) (int64, error)
PutFile implements Driver
type Notifier ¶
type Notifier interface { BeforeCommand(ctx *Context, command string) BeforeLoginUser(ctx *Context, userName string) BeforePutFile(ctx *Context, dstPath string) BeforeDeleteFile(ctx *Context, dstPath string) BeforeChangeCurDir(ctx *Context, oldCurDir, newCurDir string) BeforeCreateDir(ctx *Context, dstPath string) BeforeDeleteDir(ctx *Context, dstPath string) BeforeDownloadFile(ctx *Context, dstPath string) AfterUserLogin(ctx *Context, userName, password string, passMatched bool, err error) AfterFilePut(ctx *Context, dstPath string, size int64, err error) AfterFileDeleted(ctx *Context, dstPath string, err error) AfterFileDownloaded(ctx *Context, dstPath string, size int64, err error) AfterCurDirChanged(ctx *Context, oldCurDir, newCurDir string, err error) AfterDirCreated(ctx *Context, dstPath string, err error) AfterDirDeleted(ctx *Context, dstPath string, err error) }
Notifier represents a notification operator interface
type NullNotifier ¶
type NullNotifier struct{}
NullNotifier implements Notifier
func (NullNotifier) AfterCurDirChanged ¶
func (NullNotifier) AfterCurDirChanged(ctx *Context, oldCurDir, newCurDir string, err error)
AfterCurDirChanged implements Notifier
func (NullNotifier) AfterDirCreated ¶
func (NullNotifier) AfterDirCreated(ctx *Context, dstPath string, err error)
AfterDirCreated implements Notifier
func (NullNotifier) AfterDirDeleted ¶
func (NullNotifier) AfterDirDeleted(ctx *Context, dstPath string, err error)
AfterDirDeleted implements Notifier
func (NullNotifier) AfterFileDeleted ¶
func (NullNotifier) AfterFileDeleted(ctx *Context, dstPath string, err error)
AfterFileDeleted implements Notifier
func (NullNotifier) AfterFileDownloaded ¶
func (NullNotifier) AfterFileDownloaded(ctx *Context, dstPath string, size int64, err error)
AfterFileDownloaded implements Notifier
func (NullNotifier) AfterFilePut ¶
func (NullNotifier) AfterFilePut(ctx *Context, dstPath string, size int64, err error)
AfterFilePut implements Notifier
func (NullNotifier) AfterUserLogin ¶
func (NullNotifier) AfterUserLogin(ctx *Context, userName, password string, passMatched bool, err error)
AfterUserLogin implements Notifier
func (NullNotifier) BeforeChangeCurDir ¶
func (NullNotifier) BeforeChangeCurDir(ctx *Context, oldCurDir, newCurDir string)
BeforeChangeCurDir implements Notifier
func (NullNotifier) BeforeCommand ¶
func (NullNotifier) BeforeCommand(ctx *Context, command string)
BeforeCommand implements Notifier
func (NullNotifier) BeforeCreateDir ¶
func (NullNotifier) BeforeCreateDir(ctx *Context, dstPath string)
BeforeCreateDir implements Notifier
func (NullNotifier) BeforeDeleteDir ¶
func (NullNotifier) BeforeDeleteDir(ctx *Context, dstPath string)
BeforeDeleteDir implements Notifier
func (NullNotifier) BeforeDeleteFile ¶
func (NullNotifier) BeforeDeleteFile(ctx *Context, dstPath string)
BeforeDeleteFile implements Notifier
func (NullNotifier) BeforeDownloadFile ¶
func (NullNotifier) BeforeDownloadFile(ctx *Context, dstPath string)
BeforeDownloadFile implements Notifier
func (NullNotifier) BeforeLoginUser ¶
func (NullNotifier) BeforeLoginUser(ctx *Context, userName string)
BeforeLoginUser implements Notifier
func (NullNotifier) BeforePutFile ¶
func (NullNotifier) BeforePutFile(ctx *Context, dstPath string)
BeforePutFile implements Notifier
type Options ¶
type Options struct { // The driver that will be used to handle files persistent Driver Driver // How to handle the authenticate requests Auth Auth // How to handle the perm controls Perm Perm // A logger implementation, if nil the StdLogger is used Logger Logger // This server supported commands, if blank, it will be defaultCommands // So that users could override the Commands Commands map[string]Command // Server Name, Default is Go Ftp Server Name string // The hostname that the FTP server should listen on. Optional, defaults to // "::", which means all hostnames on ipv4 and ipv6. Hostname string // Public IP of the server PublicIP string // Disable use of passive ports DisablePassive bool // Passive ports PassivePorts string // if tls used, cert file is required CertFile string // if tls used, key file is required KeyFile string WelcomeMessage string // The port that the FTP should listen on. Optional, defaults to 3000. In // a production environment you will probably want to change this to 21. Port int // Rate Limit per connection bytes per second, 0 means no limit RateLimit int64 // Timeout is used to restrict the total length of a session Timeout time.Duration // CommandsMu controls access to the Commands map CommandsMu sync.RWMutex // use tls, default is false TLS bool // If ture TLS is used in RFC4217 mode ExplicitFTPS bool // If true, client must upgrade to TLS before sending any other command ForceTLS bool }
Options contains parameters for server.NewServer()
type Perm ¶
type Perm interface { GetOwner(string) (string, error) GetGroup(string) (string, error) GetMode(string) (os.FileMode, error) ChOwner(string, string) error ChGroup(string, string) error ChMode(string, os.FileMode) error }
Perm represents a perm interface
type RegexAuth ¶
type RegexAuth struct {
// contains filtered or unexported fields
}
RegexAuth implements Auth interface to provide a memory user login auth
func NewRegexAuth ¶
type Server ¶
type Server struct { *Options ConnCallback func(ctx context.Context, conn net.Conn) net.Conn // optional callback for wrapping net.Conn before handling // contains filtered or unexported fields }
Server is the root of your FTP application. You should instantiate one of these and call ListenAndServe() to start accepting client connections.
Always use the NewServer() method to create a new Server.
func NewServer ¶
NewServer initialises a new FTP server. Configuration options are provided via an instance of Options. Calling this function in your code will probably look something like this:
driver := &MyDriver{} opts := &server.Options{ Driver: driver, Auth: auth, Port: 2000, Perm: perm, Hostname: "127.0.0.1", } server, err := server.NewServer(opts)
func (*Server) ListenAndServe ¶
ListenAndServe asks a new Server to begin accepting client connections. It accepts no arguments - all configuration is provided via the NewServer function.
If the server fails to start for any reason, an error will be returned. Common errors are trying to bind to a privileged port or something else is already listening on the same port.
func (*Server) RegisterNotifier ¶
RegisterNotifier registers a notifier
type Session ¶
type Session struct { Conn net.Conn Ctx context.Context Data map[string]interface{} // shared data between different commands // contains filtered or unexported fields }
Session represents a session between ftp client and the server
func (*Session) Close ¶
func (sess *Session) Close()
Close will manually close this connection, even if the client isn't ready.
func (*Session) DataConn ¶
func (sess *Session) DataConn() DataSocket
DataConn returns the data connection
func (*Session) PassivePort ¶
PassivePort returns the port which could be used by passive mode.
func (*Session) RemoteAddr ¶
RemoteAddr returns the remote ftp client's address
func (*Session) Serve ¶
func (sess *Session) Serve()
Serve starts an endless loop that reads FTP commands from the client and responds appropriately. terminated is a channel that will receive a true message when the connection closes. This loop will be running inside a goroutine, so use this channel to be notified when the connection can be cleaned up.
func (*Session) WriteMessage ¶
type SimpleAuth ¶
SimpleAuth implements Auth interface to provide a memory user login auth
func (*SimpleAuth) CheckPasswd ¶
func (a *SimpleAuth) CheckPasswd(ctx *Context, name, pass string) (bool, error)
CheckPasswd will check user's password
type SimplePerm ¶
type SimplePerm struct {
// contains filtered or unexported fields
}
SimplePerm implements Perm interface that all files are owned by special owner and group
func NewSimplePerm ¶
func NewSimplePerm(owner, group string) *SimplePerm
NewSimplePerm creates a SimplePerm
func (*SimplePerm) ChGroup ¶
func (s *SimplePerm) ChGroup(string, string) error
ChGroup changed the file's group
func (*SimplePerm) ChMode ¶
func (s *SimplePerm) ChMode(string, os.FileMode) error
ChMode changed the file's mode
func (*SimplePerm) ChOwner ¶
func (s *SimplePerm) ChOwner(string, string) error
ChOwner changed the file's owner
func (*SimplePerm) GetGroup ¶
func (s *SimplePerm) GetGroup(string) (string, error)
GetGroup returns the group of the file
type StdLogger ¶
type StdLogger struct{}
StdLogger use an instance of this to log in a standard format
func (*StdLogger) PrintCommand ¶
PrintCommand implements Logger
func (*StdLogger) PrintResponse ¶
PrintResponse implements Logger