You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.3 KiB

  1. package ftpd
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "fmt"
  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: fmt.Sprintf("%s:%d", 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. }