mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package ftpd
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/spf13/afero"
|
|
"net"
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type FtpServerOption struct {
|
|
Filer string
|
|
IP string
|
|
IpBind string
|
|
Port int
|
|
FilerGrpcAddress string
|
|
FtpRoot string
|
|
GrpcDialOption grpc.DialOption
|
|
PassivePortStart int
|
|
PassivePortStop int
|
|
}
|
|
|
|
type SftpServer struct {
|
|
option *FtpServerOption
|
|
ftpListener net.Listener
|
|
fs *FtpFileSystem
|
|
}
|
|
|
|
var _ = ftpserver.MainDriver(&SftpServer{})
|
|
|
|
// NewServer returns a new FTP server driver
|
|
func NewFtpServer(ftpListener net.Listener, option *FtpServerOption) (*SftpServer, error) {
|
|
fs, err := NewFtpFileSystem(option)
|
|
server := &SftpServer{
|
|
option: option,
|
|
ftpListener: ftpListener,
|
|
fs: fs,
|
|
}
|
|
return server, err
|
|
}
|
|
|
|
// GetSettings returns some general settings around the server setup
|
|
func (s *SftpServer) GetSettings() (*ftpserver.Settings, error) {
|
|
var portRange *ftpserver.PortRange
|
|
if s.option.PassivePortStart > 0 && s.option.PassivePortStop > s.option.PassivePortStart {
|
|
portRange = &ftpserver.PortRange{
|
|
Start: s.option.PassivePortStart,
|
|
End: s.option.PassivePortStop,
|
|
}
|
|
}
|
|
|
|
return &ftpserver.Settings{
|
|
Listener: s.ftpListener,
|
|
ListenAddr: fmt.Sprintf("%s:%d", s.option.IpBind, s.option.Port),
|
|
PublicHost: s.option.IP,
|
|
PassiveTransferPortRange: portRange,
|
|
ActiveTransferPortNon20: true,
|
|
IdleTimeout: -1,
|
|
ConnectionTimeout: 20,
|
|
}, nil
|
|
}
|
|
|
|
// ClientConnected is called to send the very first welcome message
|
|
func (s *SftpServer) ClientConnected(cc ftpserver.ClientContext) (string, error) {
|
|
return "Welcome to SeaweedFS FTP Server", nil
|
|
}
|
|
|
|
// ClientDisconnected is called when the user disconnects, even if he never authenticated
|
|
func (s *SftpServer) ClientDisconnected(cc ftpserver.ClientContext) {
|
|
}
|
|
|
|
// AuthUser authenticates the user and selects an handling driver
|
|
func (s *SftpServer) AuthUser(cc ftpserver.ClientContext, username, password string) (ftpserver.ClientDriver, error) {
|
|
return &ClientDriver{
|
|
Fs: s.fs,
|
|
}, nil
|
|
}
|
|
|
|
// GetTLSConfig returns a TLS Certificate to use
|
|
// The certificate could frequently change if we use something like "let's encrypt"
|
|
func (s *SftpServer) GetTLSConfig() (*tls.Config, error) {
|
|
return nil, errors.New("no TLS certificate configured")
|
|
}
|
|
|
|
type ClientDriver struct {
|
|
afero.Fs
|
|
}
|