Skip to content

Commit

Permalink
Merge pull request #182 from jm33-m0/master
Browse files Browse the repository at this point in the history
add sftp-server example
  • Loading branch information
gustavosbarreto authored Oct 25, 2022
2 parents 0f80af4 + 3cef403 commit db09465
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions _examples/ssh-sftpserver/sftp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"log"

"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
)

// SftpHandler handler for SFTP subsystem
func SftpHandler(sess ssh.Session) {
debugStream := ioutil.Discard
serverOptions := []sftp.ServerOption{
sftp.WithDebug(debugStream),
}
server, err := sftp.NewServer(
sess,
serverOptions...,
)
if err != nil {
log.Printf("sftp server init error: %s\n", err)
return
}
if err := server.Serve(); err == io.EOF {
server.Close()
fmt.Println("sftp client exited session.")
} else if err != nil {
fmt.Println("sftp server completed with error:", err)
}
}

func main() {
ssh_server := ssh.Server{
Addr: "127.0.0.1:2222",
SubsystemHandlers: map[string]ssh.SubsystemHandler{
"sftp": SftpHandler,
},
}
log.Fatal(ssh_server.ListenAndServe())
}

0 comments on commit db09465

Please sign in to comment.