ftp

package module
v0.0.0-...-ab4a0c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2016 License: MIT Imports: 16 Imported by: 0

README

A Go FTP package in the vein of net/http.

go get github.com/igneous-systems/ftp

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultGoodbye = "Goodbye."

DefaultGoodbye is the default goodbye message for closing sessions.

View Source
var DefaultGreeting = "Welcome."

DefaultGreeting is the default greeting for new connections.

View Source
var ErrInvalidSyntax = errors.New("invalid syntax")

ErrInvalidSyntax is returned by parsing functions if the input could not be parsed.

Functions

func EHostPort

func EHostPort(addr *net.TCPAddr) string

EHostPort converts addr into a string suitable for use with EPRT.

func HostPort

func HostPort(addr *net.TCPAddr) string

HostPort converts addr into a string suitable for use with PASV or PORT.

func ParseEPRT

func ParseEPRT(msg string) (*net.TCPAddr, error)

ParseEPRT extracts an address from an EPRT command message.

func ParseEPSV

func ParseEPSV(msg string) (int, error)

ParseEPSV extracts a port from an EPSV reply message.

func ParsePASV

func ParsePASV(msg string) (*net.TCPAddr, error)

ParsePASV extracts an address from a PASV reply message.

func ParsePORT

func ParsePORT(msg string) (*net.TCPAddr, error)

ParsePORT extracts an address from a PORT command message.

Types

type Authorizer

type Authorizer interface {
	// Authorize the user. Returning an error closes the session.
	Authorize(user, pass string) (bool, error)
}

An Authorizer can be used with a FileHandler to handle login.

type Client

type Client struct {
	Addr     string   // Addr of server to connect to.
	Dialer   Dialer   // Dialer for outgoing connections.
	Listener Listener // Listener for incoming connections.
	Debug    bool     // Debug prints control channel traffic.
	// contains filtered or unexported fields
}

Client for interacting with a server.

func DialFTP

func DialFTP(addr string) (*Client, error)

DialFTP dials a server.

func (*Client) Authorize

func (c *Client) Authorize(user, pass string) (bool, error)

Authorize with the server.

func (*Client) Chdir

func (c *Client) Chdir(dir string) error

Chdir changes to the given directory.

func (*Client) Close

func (c *Client) Close() error

Close the connection.

func (*Client) Connect

func (c *Client) Connect() error

Connect to a server.

func (*Client) Create

func (c *Client) Create(path string) (File, error)

Create a new file.

func (*Client) Mkdir

func (c *Client) Mkdir(path string) error

Mkdir makes a new directory.

func (*Client) Open

func (c *Client) Open(path string) (File, error)

Open a file or directory.

func (*Client) Remove

func (c *Client) Remove(path string) error

Remove a file or directory.

func (*Client) Rename

func (c *Client) Rename(old, new string) error

Rename a file or directory.

func (*Client) Stat

func (c *Client) Stat(p string) (os.FileInfo, error)

Stat a file or directory.

type Command

type Command struct {
	Cmd string // Cmd is the command type.
	Msg string // Msg is the full message.
}

A Command read from or written to a control channel.

func (*Command) Args

func (c *Command) Args() []string

Args returns the arguments split into tokens.

func (*Command) Decode

func (c *Command) Decode(r *textproto.Reader) error

Decode from r into c.

func (*Command) Encode

func (c *Command) Encode(w *textproto.Writer) error

Encode c into w.

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

A Conn represents a data channel. This transforms data according to the transfer type and also performs buffering.

func ActiveConn

func ActiveConn(c net.Conn) *Conn

ActiveConn creates an active connection over c.

func PassiveConn

func PassiveConn(l net.Listener) *Conn

PassiveConn creates a passive connection over l. This will close l once a connection has been established.

func (*Conn) Active

func (c *Conn) Active() bool

Active returns whether this is an active connection or a passive one that has accepted a connection.

func (*Conn) Addr

func (c *Conn) Addr() net.Addr

Addr returns the listening address if this is a passive connection. Otherwise, this returns the remote address of the active connection.

func (*Conn) Close

func (c *Conn) Close() (err error)

Close flushes and closes the connection.

func (*Conn) EHostPort

func (c *Conn) EHostPort() string

EHostPort is shorthand for EHostPort(c.Addr()).

func (*Conn) Flush

func (c *Conn) Flush() error

Flush any buffered data.

func (*Conn) HostPort

func (c *Conn) HostPort() string

HostPort is shorthand for HostPort(c.Addr()).

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr waits for a connection, then calls LocalAddr on it.

func (*Conn) Mode

func (c *Conn) Mode(m string)

Mode sets the transfer mode of the connection.

func (*Conn) Passive

func (c *Conn) Passive() bool

Passive returns whether this is a passive connection.

func (*Conn) Port

func (c *Conn) Port() int

Port returns the port associated with c.Addr().

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

Read implements io.Reader. If a connection has not been established, this waits for a connection.

func (*Conn) ReadLine

func (c *Conn) ReadLine() (line string, err error)

ReadLine reads a line. If a connection has not been established, this waits for a connection.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr waits for a connection, then calls RemoteAddr on it.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline waits for a connection, then calls SetDeadline on it.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline waits for a connection, then calls SetReadDeadline on it.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline waits for a connection, then calls SetWriteDeadline on it.

func (*Conn) Type

func (c *Conn) Type(t string)

Type sets the transfer type of the connection.

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

Write implements io.Writer. If a connection has not been established, this waits for a connection.

type Context

type Context struct {
	User     string // User name used to authorize the session.
	Password string // Password used to authorize the session.
	Dir      string // Dir is the working directory.
	Mode     string // Mode of data transfer.
	Type     string // Type of data channel.
	Data     *Conn  // Data channel connection.
}

Context shared between clients and servers.

func (*Context) CloseData

func (c *Context) CloseData() error

CloseData closes the data connection and sets it to nil. If there is no data connection, this returns an error.

func (*Context) Path

func (c *Context) Path(p string) string

Path returns the absolute path of p, using the working directory as the base.

type Dialer

type Dialer interface {
	Dial(net, addr string) (net.Conn, error)
}

A Dialer establishes an outgoing connection.

type File

type File interface {
	io.Reader
	io.Writer
	io.Seeker
	io.Closer

	// Readdir has semantics like os.Readdir.
	Readdir(n int) ([]os.FileInfo, error)
}

File is the interface returned by certain FileSystem methods.

type FileHandler

type FileHandler struct {
	Authorizer // Authorizer for login. If nil, accept all.
	FileSystem // FileSystem to serve.
}

A FileHandler serves from a FileSystem.

func (*FileHandler) Handle

func (h *FileHandler) Handle(s *Session) error

Handle implements Handler.

type FileSystem

type FileSystem interface {
	Create(path string) (File, error)      // Create a new file.
	Mkdir(path string) error               // Mkdir makes a new directory.
	Open(path string) (File, error)        // Open a file or directory.
	Remove(path string) error              // Remove a file or directory.
	Rename(old, new string) error          // Rename a file or directory.
	Stat(path string) (os.FileInfo, error) // Stat a file or directory.
}

FileSystem is the interface expected by a FileHandler. This type is intended to work with the os package. If errors returned by these methods match errors returned by os package functions, more informative reply codes may be chosen by a FileHandler in response to failed commands.

type Handler

type Handler interface {
	// Handle a session. It is optional to send a greeting or reply to a QUIT.
	// The session is closed by the Server on return. The return value is for
	// composing Handlers and is ignored by the Server.
	Handle(*Session) error
}

A Handler for a session.

type Listener

type Listener interface {
	Listen(net, addr string) (net.Listener, error)
}

A Listener listens for incoming connections.

type Lister

type Lister struct {
	File
	Cmd string
	// contains filtered or unexported fields
}

A Lister produces listing output similar to ls.

func (*Lister) Read

func (l *Lister) Read(b []byte) (n int, err error)

Read implements io.Reader.

func (*Lister) WriteTo

func (l *Lister) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo.

type LocalFileSystem

type LocalFileSystem struct {
	Root string // Root of the file system, or current directory if "".
}

LocalFileSystem is a FileSystem implementation that calls os package functions.

func (*LocalFileSystem) Create

func (f *LocalFileSystem) Create(path string) (File, error)

Create implements FileSystem.

func (*LocalFileSystem) Mkdir

func (f *LocalFileSystem) Mkdir(path string) error

Mkdir implements FileSystem.

func (*LocalFileSystem) Open

func (f *LocalFileSystem) Open(path string) (File, error)

Open implements FileSystem.

func (*LocalFileSystem) Remove

func (f *LocalFileSystem) Remove(path string) error

Remove implements FileSystem.

func (*LocalFileSystem) Rename

func (f *LocalFileSystem) Rename(old, new string) error

Rename implements FileSystem.

func (*LocalFileSystem) Stat

func (f *LocalFileSystem) Stat(path string) (os.FileInfo, error)

Stat implements FileSystem.

type Reply

type Reply struct {
	Code int
	Msg  string
}

A Reply read from or written to a control channel.

func (*Reply) Decode

func (r *Reply) Decode(tr *textproto.Reader) error

Decode from tr into r.

func (*Reply) Encode

func (r *Reply) Encode(w *textproto.Writer) error

Encode r into w.

func (*Reply) Intermediate

func (r *Reply) Intermediate() bool

Intermediate returns whether r.Code is 3xx.

func (*Reply) Lines

func (r *Reply) Lines() []string

Lines returns r.Msg split by line.

func (*Reply) Preliminary

func (r *Reply) Preliminary() bool

Preliminary returns whether r.Code is 1xx.

func (*Reply) Success

func (r *Reply) Success() bool

Success returns whether r.Code is 2xx.

type Server

type Server struct {
	Addr     string      // Addr to bind the control channel to.
	TLS      *tls.Config // TLS config enables FTPS if non-nil.
	Dialer   Dialer      // Dialer for active connections.
	Listener Listener    // Listener for passive connections.
	Handler  Handler     // Handler for commands.
	Debug    bool        // Debug prints control channel traffic.
}

A Server serves incoming connections.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(fork bool) (net.Listener, error)

ListenAndServe listens on s.Addr and serves incoming connections. If fork is true, Serve is called on a new goroutine. Otherwise, Serve is called on this goroutine.

func (*Server) Serve

func (s *Server) Serve(l net.Listener) error

Serve incoming connections over l.

func (*Server) ServeFTP

func (s *Server) ServeFTP(c net.Conn)

ServeFTP serves one client.

type Session

type Session struct {
	Addr    net.Addr // Addr of remote host.
	Server  *Server  // Server the session belongs to.
	Context          // Context shared with the client.

	TLS *tls.Config // TLS config to use for data connections.
	// contains filtered or unexported fields
}

A Session represents a single control channel session with a client.

func (*Session) Active

func (s *Session) Active(addr net.Addr) error

Active establishes an active data channel connection through the associated server's dialer. This sets s.Data and closes any existing data channel.

func (*Session) Close

func (s *Session) Close() error

Close the session. This will send a default goodbye reply if one has not been sent in response to a QUIT.

func (*Session) Command

func (s *Session) Command() (*Command, error)

Command reads the next command, or returns the current command if it has already been read and has not been replied to. If the greeting has not been sent, this will send the greeting first.

func (*Session) Passive

func (s *Session) Passive(nw string) error

Passive creates a passive connection listening through the associated server's listener. This sets s.Data and closes any existing data channel.

func (*Session) Reply

func (s *Session) Reply(code int, msg string, args ...interface{}) error

Reply sends a reply. This must be called with a non-intermediate reply code in order to allow the next command to be read. After replying to a QUIT command with a non-intermediate response code, the session is closed.

func (*Session) SetMode

func (s *Session) SetMode(m string) error

SetMode sets s.Mode as well as the mode of any existing data channel.

func (*Session) SetType

func (s *Session) SetType(t string) error

SetType sets s.Type as well as the type of any existing data channel.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL