ftp_server.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package ftpd
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "net"
  7. ftpserver "github.com/fclairamb/ftpserverlib"
  8. "google.golang.org/grpc"
  9. )
  10. type FtpServerOption struct {
  11. Filer string
  12. IP string
  13. IpBind string
  14. Port int
  15. FilerGrpcAddress string
  16. FtpRoot string
  17. GrpcDialOption grpc.DialOption
  18. PassivePortStart int
  19. PassivePortStop int
  20. }
  21. type SftpServer struct {
  22. option *FtpServerOption
  23. ftpListener net.Listener
  24. }
  25. var _ = ftpserver.MainDriver(&SftpServer{})
  26. // NewServer returns a new FTP server driver
  27. func NewFtpServer(ftpListener net.Listener, option *FtpServerOption) (*SftpServer, error) {
  28. var err error
  29. server := &SftpServer{
  30. option: option,
  31. ftpListener: ftpListener,
  32. }
  33. return server, err
  34. }
  35. // GetSettings returns some general settings around the server setup
  36. func (s *SftpServer) GetSettings() (*ftpserver.Settings, error) {
  37. var portRange *ftpserver.PortRange
  38. if s.option.PassivePortStart > 0 && s.option.PassivePortStop > s.option.PassivePortStart {
  39. portRange = &ftpserver.PortRange{
  40. Start: s.option.PassivePortStart,
  41. End: s.option.PassivePortStop,
  42. }
  43. }
  44. return &ftpserver.Settings{
  45. Listener: s.ftpListener,
  46. ListenAddr: util.JoinHostPort(s.option.IpBind, s.option.Port),
  47. PublicHost: s.option.IP,
  48. PassiveTransferPortRange: portRange,
  49. ActiveTransferPortNon20: true,
  50. IdleTimeout: -1,
  51. ConnectionTimeout: 20,
  52. }, nil
  53. }
  54. // ClientConnected is called to send the very first welcome message
  55. func (s *SftpServer) ClientConnected(cc ftpserver.ClientContext) (string, error) {
  56. return "Welcome to SeaweedFS FTP Server", nil
  57. }
  58. // ClientDisconnected is called when the user disconnects, even if he never authenticated
  59. func (s *SftpServer) ClientDisconnected(cc ftpserver.ClientContext) {
  60. }
  61. // AuthUser authenticates the user and selects an handling driver
  62. func (s *SftpServer) AuthUser(cc ftpserver.ClientContext, username, password string) (ftpserver.ClientDriver, error) {
  63. return nil, nil
  64. }
  65. // GetTLSConfig returns a TLS Certificate to use
  66. // The certificate could frequently change if we use something like "let's encrypt"
  67. func (s *SftpServer) GetTLSConfig() (*tls.Config, error) {
  68. return nil, errors.New("no TLS certificate configured")
  69. }