ftp

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 18 Imported by: 0

README

ftp

a basic ftp server written in go

to run the basic app with docker-compose ../README.md

Documentation

Index

Constants

View Source
const PublicIpUrl = "https://api.ipify.org"

PublicIpUrl is the url to get the public ip of the server

Variables

This section is empty.

Functions

func Abs

func Abs(root string, workingDir string, arg string) string

func GetServerPublicIP

func GetServerPublicIP() (string, error)

Types

type FTPServerTransferType

type FTPServerTransferType string

type Server

type Server struct {

	// Addr is the server address
	Addr string
	// supportsTLS is a flag to indicate if the server supports TLS
	FsHandler filesystem.FS
	// Root is the server root directory
	Root string

	// WelcomeMessage is the server welcome message
	WelcomeMessage string
	// PublicServerIPv4 is the server public IPv4 address for passive mode
	PublicServerIPv4 [4]byte
	// Type is the server transfer type
	Type FTPServerTransferType
	// PasvMaxPort is the server passive mode max port
	PasvMaxPort int
	// PasvMinPort is the server passive mode min port
	PasvMinPort int
	//  TLS is the server TLS configuration
	TLS *tls.Config
	// TLSe is the server TLS configuration for upgrade existing FTP connection
	TLSe *tls.Config
	// Closer is the server closer channel on close the channel will return the error
	Closer chan error
	// contains filtered or unexported fields
}

func NewServer

func NewServer(addr string, fsHandler filesystem.FS, users Users) (*Server, error)

NewServer creates a new FTP server

func (*Server) Close

func (s *Server) Close(err error)

Close stops the FTP server

func (*Server) Listen

func (s *Server) Listen() (err error)

Listen starts the FTP Listen

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() (err error)

ListenAndServe starts the FTP server

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS(certFile, keyFile string) (err error)

ListenAndServeTLS starts the FTP server

func (*Server) ListenAndServeTLSe

func (s *Server) ListenAndServeTLSe(certFile, keyFile string) (err error)

ListenAndServeTLSe and allow upgrade to TLS

func (*Server) Logger

func (s *Server) Logger() *slog.Logger

func (*Server) Serve

func (s *Server) Serve()

Serve starts the FTP server

func (*Server) ServeTLS

func (s *Server) ServeTLS(certFile, keyFile string) (err error)

ServeTLS starts the FTP server with TLS

func (*Server) ServeTLSe

func (s *Server) ServeTLSe(certFile, keyFile string) (err error)

ServeTLSe starts the FTP server and allow upgrade to TLS

func (*Server) SetLogger

func (s *Server) SetLogger(l *slog.Logger)

func (*Server) SetPublicServerIPv4

func (s *Server) SetPublicServerIPv4(publicServerIP string) error

SetPublicServerIPv4 sets the server public IPv4 address

func (*Server) TryListenAndServe

func (s *Server) TryListenAndServe(d time.Duration) (err error)

TryListenAndServe strives to starts the FTP server if there isn't an error after a certain time it returns nil

func (*Server) TryListenAndServeTLS

func (s *Server) TryListenAndServeTLS(certFile, keyFile string, d time.Duration) (err error)

TryListenAndServeTLS tries to start the FTP server if there isn't an error after a certain time it returns nil

func (*Server) TryListenAndServeTLSe

func (s *Server) TryListenAndServeTLSe(certFile, keyFile string, d time.Duration) (err error)

TryListenAndServeTLSe strives to starts the FTP server if there isn't an error after a certain time it returns nil

func (*Server) Wait

func (s *Server) Wait() error

func (*Server) WithContext

func (s *Server) WithContext(ctx context.Context) *Server

WithContext sets the server context

type Session

type Session struct {
	HelpCommands string
	CTX          context.Context
	// contains filtered or unexported fields
}

Session represents an individual client FTP session.

func (*Session) AbortCommand

func (s *Session) AbortCommand(cmd, arg string) error

AbortCommand handles the ABOR command from the client.

func (*Session) ActiveModeCommand

func (s *Session) ActiveModeCommand(cmd, args string) error

ActiveModeCommand handles the PORT command from the client.

func (*Session) AuthCommand

func (s *Session) AuthCommand(cmd, arg string) error

AuthCommand handles the AUTH command from the client.

func (*Session) ChangeDirectoryCommand

func (s *Session) ChangeDirectoryCommand(cmd, arg string) error

ChangeDirectoryCommand handles the CWD command from the client. The CWD command is used to change the working directory on the server.

func (*Session) ChangeDirectoryToParentCommand

func (s *Session) ChangeDirectoryToParentCommand(cmd, arg string) error

ChangeDirectoryToParentCommand handles the CDUP command from the client. The CDUP command is used to change the working directory to the parent directory.

func (*Session) CloseCommand

func (s *Session) CloseCommand(cmd, arg string) error

func (*Session) CloseDataCaller

func (s *Session) CloseDataCaller()

CloseDataCaller closes the data connection.

func (*Session) CloseDataConnection

func (s *Session) CloseDataConnection()

CloseDataConnection closes the data connection.

func (*Session) ExtendedActiveModeCommand

func (s *Session) ExtendedActiveModeCommand(cmd, arg string) error

ExtendedActiveModeCommand handles the EPRT command from the client.

func (*Session) ExtendedPassiveModeCommand

func (s *Session) ExtendedPassiveModeCommand(cmd, arg string) error

ExtendedPassiveModeCommand handles the EPSV command from the client. The EPSV command is used to enter extended passive mode.

func (*Session) FeaturesCommand

func (s *Session) FeaturesCommand(cmd, arg string) error

func (*Session) GetDirInfoCommand

func (s *Session) GetDirInfoCommand(cmd, arg string) error

GetDirInfoCommand handles the MLSD command from the client. The MLSD command is used to list the contents of a directory in a machine-readable format.

func (*Session) GetFileInfoCommand

func (s *Session) GetFileInfoCommand(cmd, arg string) error

GetFileInfoCommand handles the MLST command from the client.

func (*Session) HelpCommand

func (s *Session) HelpCommand(cmd, arg string) error

HelpCommand handles the HELP command from the client.

func (*Session) MakeDirectoryCommand

func (s *Session) MakeDirectoryCommand(cmd, arg string) error

func (*Session) ModeCommand

func (s *Session) ModeCommand(cmd, args string) error

ModeCommand handles the MODE command from the client.

func (*Session) ModifyTimeCommand

func (s *Session) ModifyTimeCommand(cmd, arg string) error

ModifyTimeCommand handles the MDTM command from the client. The MDTM command is used to modify the modification time of a file on the server.

func (*Session) NoopCommand

func (s *Session) NoopCommand(cmd, arg string) error

NoopCommand handles the NOOP command from the client. The NOOP command is used to keep the connection alive.

func (*Session) OptsCommand

func (s *Session) OptsCommand(cmd, arg string) error

OptsCommand handles the OPTS command from the client. The OPTS command is used to specify options for the server.

func (*Session) PROTCommand

func (s *Session) PROTCommand(cmd, args string) error

PROTCommand handles the PROT command from the client.

func (*Session) ParseCommand

func (s *Session) ParseCommand() (cmd, arg string, err error)

ParseCommand parses the command from the client and returns the command and argument.

func (*Session) PassCommand

func (s *Session) PassCommand(cmd, arg string) (err error)

PassCommand handles the PASS command from the client.

func (*Session) PassiveModeCommand

func (s *Session) PassiveModeCommand(cmd, arg string) error

PassiveModeCommand handles the PASV command from the client. The PASV command is used to enter passive mode.

func (*Session) PassiveOrActiveModeConn

func (s *Session) PassiveOrActiveModeConn() (net.Conn, error)

PassiveOrActiveModeConn returns the data connection. if passive mode is enabled, it returns the listener. if active mode is enabled, it returns the caller.

func (*Session) PasvEpsvCommand

func (s *Session) PasvEpsvCommand(arg string) (port int, err error)

PasvEpsvCommand handles the PASV command from the client. The PASV command is used to enter passive mode.

func (*Session) PbszCommand

func (s *Session) PbszCommand(cmd string, arg string) error

func (*Session) PortErptCommand

func (s *Session) PortErptCommand(addr string) (err error)

func (*Session) PrintWorkingDirectoryCommand

func (s *Session) PrintWorkingDirectoryCommand(cmd, arg string) error

PrintWorkingDirectoryCommand handles the PWD command from the client. The PWD command is used to print the current working directory on the server.

func (*Session) RemoveCommand

func (s *Session) RemoveCommand(cmd, arg string) error

func (*Session) RenameFromCommand

func (s *Session) RenameFromCommand(cmd, arg string) error

func (*Session) RenameToCommand

func (s *Session) RenameToCommand(cmd, arg string) error

func (*Session) RessetCommand

func (s *Session) RessetCommand(cmd, arg string) error

func (*Session) RetrieveCommand

func (s *Session) RetrieveCommand(cmd, arg string) error

RetrieveCommand handles the RETR command from the client.

func (*Session) SaveCommand

func (s *Session) SaveCommand(cmd, arg string) error

SaveCommand handles the STOR command from the client. The STOR command is used to store a file on the server.

func (*Session) SiteCommand

func (s *Session) SiteCommand(cmd, arg string) error

func (*Session) SizeCommand

func (s *Session) SizeCommand(cmd, arg string) error

SizeCommand handles the SIZE command from the client.

func (*Session) StatusCommand

func (s *Session) StatusCommand(cmd, arg string) error

StatusCommand handles the MLST command from the client.

func (*Session) StruCommand

func (s *Session) StruCommand(cmd, args string) error

StruCommand handles the STRU command from the client.

func (*Session) SystemCommand

func (s *Session) SystemCommand(cmd, arg string) error

SystemCommand returns the system type.

func (*Session) TypeCommand

func (s *Session) TypeCommand(cmd, arg string) error

TypeCommand handles the TYPE command from the client. The TYPE command is used to specify the type of file being transferred. The two types are ASCII (A) and binary (I).

func (*Session) UnAuthenticatedCommand

func (s *Session) UnAuthenticatedCommand(cmd, arg string) error

UnAuthenticatedCommand handles the commands that are not allowed when the user is not authenticated.

func (*Session) UnknownCommand

func (s *Session) UnknownCommand(cmd, arg string) error

func (*Session) UserCommand

func (s *Session) UserCommand(cmd, arg string) (err error)

UserCommand handles the USER command from the client.

type SessionManager

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

SessionManager manages all active sessions.

func NewSessionManager

func NewSessionManager() *SessionManager

func (*SessionManager) Add

func (manager *SessionManager) Add(id string, session *Session)

Add adds a new session for the client.

func (*SessionManager) Get

func (manager *SessionManager) Get(id string) (*Session, bool)

Get retrieves a session by its ID.

func (*SessionManager) Remove

func (manager *SessionManager) Remove(id string)

Remove removes a session by its ID.

type Users

type Users interface {
	// FindUser returns a user by username and password, if the user is not found it returns an error
	FindUser(ctx context.Context, username, password, ipaddr string) (any, error)
}

Users is the interface to find a user by username and password and return it

Jump to

Keyboard shortcuts

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