sftpserver.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package sftpserver
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net"
  9. "github.com/pkg/sftp"
  10. "golang.org/x/crypto/ssh"
  11. )
  12. type Instance struct {
  13. }
  14. //Create a new SFTP Server
  15. //listeningIP in the format of 0.0.0.0:2022
  16. func NewSFTPServer(listeningIp string, keyfile string, readOnly bool, passwordCheckFunc func(string, string) bool) (*Instance, error) {
  17. // An SSH server is represented by a ServerConfig, which holds
  18. // certificate details and handles authentication of ServerConns.
  19. config := &ssh.ServerConfig{
  20. PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
  21. // Should use constant-time compare (or better, salt+hash) in
  22. // a production setting.
  23. fmt.Printf("Login: %s\n", c.User())
  24. if passwordCheckFunc(c.User(), string(pass)) {
  25. return nil, nil
  26. }
  27. return nil, errors.New("password rejected for " + c.User())
  28. },
  29. }
  30. privateBytes, err := ioutil.ReadFile(keyfile)
  31. if err != nil {
  32. return nil, err
  33. }
  34. private, err := ssh.ParsePrivateKey(privateBytes)
  35. if err != nil {
  36. return nil, err
  37. }
  38. config.AddHostKey(private)
  39. // Once a ServerConfig has been configured, connections can be
  40. // accepted.
  41. listener, err := net.Listen("tcp", "0.0.0.0:2022")
  42. if err != nil {
  43. return nil, err
  44. }
  45. fmt.Printf("Listening on %v\n", listener.Addr())
  46. for {
  47. nConn, err := listener.Accept()
  48. if err != nil {
  49. return nil, err
  50. }
  51. go func(nConn net.Conn) error {
  52. // Before use, a handshake must be performed on the incoming
  53. // net.Conn.
  54. _, chans, reqs, err := ssh.NewServerConn(nConn, config)
  55. if err != nil {
  56. return err
  57. }
  58. fmt.Println("SSH server established\n")
  59. // The incoming Request channel must be serviced.
  60. go ssh.DiscardRequests(reqs)
  61. // Service the incoming Channel channel.
  62. for newChannel := range chans {
  63. // Channels have a type, depending on the application level
  64. // protocol intended. In the case of an SFTP session, this is "subsystem"
  65. // with a payload string of "<length=4>sftp"
  66. fmt.Println("Incoming channel: %s\n", newChannel.ChannelType())
  67. if newChannel.ChannelType() != "session" {
  68. newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
  69. fmt.Println("Unknown channel type: %s\n", newChannel.ChannelType())
  70. continue
  71. }
  72. channel, requests, err := newChannel.Accept()
  73. if err != nil {
  74. return err
  75. }
  76. fmt.Println("Channel accepted\n")
  77. // Sessions have out-of-band requests such as "shell",
  78. // "pty-req" and "env". Here we handle only the
  79. // "subsystem" request.
  80. go func(in <-chan *ssh.Request) {
  81. for req := range in {
  82. fmt.Println("Request: %v\n", req.Type)
  83. ok := false
  84. switch req.Type {
  85. case "subsystem":
  86. fmt.Println("Subsystem: %s\n", req.Payload[4:])
  87. if string(req.Payload[4:]) == "sftp" {
  88. ok = true
  89. }
  90. }
  91. fmt.Println(" - accepted: %v\n", ok)
  92. req.Reply(ok, nil)
  93. }
  94. }(requests)
  95. serverOptions := []sftp.ServerOption{}
  96. if readOnly {
  97. serverOptions = append(serverOptions, sftp.ReadOnly())
  98. fmt.Println("Read-only server\n")
  99. } else {
  100. fmt.Println("Read write server\n")
  101. }
  102. server, err := sftp.NewServer(
  103. channel,
  104. serverOptions...,
  105. )
  106. if err != nil {
  107. return err
  108. }
  109. go func() {
  110. if err := server.Serve(); err == io.EOF {
  111. server.Close()
  112. log.Print("sftp client exited session.")
  113. } else if err != nil {
  114. log.Fatal("sftp server completed with error:", err)
  115. }
  116. }()
  117. }
  118. log.Println("Connection cycle ended")
  119. return nil
  120. }(nConn)
  121. }
  122. return &Instance{}, nil
  123. }