summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pkg/sftp/examples
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/pkg/sftp/examples')
-rw-r--r--vendor/github.com/pkg/sftp/examples/buffered-read-benchmark/main.go78
-rw-r--r--vendor/github.com/pkg/sftp/examples/buffered-write-benchmark/main.go84
-rw-r--r--vendor/github.com/pkg/sftp/examples/request-server/main.go131
-rw-r--r--vendor/github.com/pkg/sftp/examples/sftp-server/main.go147
-rw-r--r--vendor/github.com/pkg/sftp/examples/streaming-read-benchmark/main.go85
-rw-r--r--vendor/github.com/pkg/sftp/examples/streaming-write-benchmark/main.go85
6 files changed, 610 insertions, 0 deletions
diff --git a/vendor/github.com/pkg/sftp/examples/buffered-read-benchmark/main.go b/vendor/github.com/pkg/sftp/examples/buffered-read-benchmark/main.go
new file mode 100644
index 00000000..36ac6d72
--- /dev/null
+++ b/vendor/github.com/pkg/sftp/examples/buffered-read-benchmark/main.go
@@ -0,0 +1,78 @@
+// buffered-read-benchmark benchmarks the peformance of reading
+// from /dev/zero on the server to a []byte on the client via io.Copy.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "os"
+ "time"
+
+ "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/agent"
+
+ "github.com/pkg/sftp"
+)
+
+var (
+ USER = flag.String("user", os.Getenv("USER"), "ssh username")
+ HOST = flag.String("host", "localhost", "ssh server hostname")
+ PORT = flag.Int("port", 22, "ssh server port")
+ PASS = flag.String("pass", os.Getenv("SOCKSIE_SSH_PASSWORD"), "ssh password")
+ SIZE = flag.Int("s", 1<<15, "set max packet size")
+)
+
+func init() {
+ flag.Parse()
+}
+
+func main() {
+ var auths []ssh.AuthMethod
+ if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
+ auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
+
+ }
+ if *PASS != "" {
+ auths = append(auths, ssh.Password(*PASS))
+ }
+
+ config := ssh.ClientConfig{
+ User: *USER,
+ Auth: auths,
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ }
+ addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
+ conn, err := ssh.Dial("tcp", addr, &config)
+ if err != nil {
+ log.Fatalf("unable to connect to [%s]: %v", addr, err)
+ }
+ defer conn.Close()
+
+ c, err := sftp.NewClient(conn, sftp.MaxPacket(*SIZE))
+ if err != nil {
+ log.Fatalf("unable to start sftp subsytem: %v", err)
+ }
+ defer c.Close()
+
+ r, err := c.Open("/dev/zero")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer r.Close()
+
+ const size = 1e9
+
+ log.Printf("reading %v bytes", size)
+ t1 := time.Now()
+ n, err := io.ReadFull(r, make([]byte, size))
+ if err != nil {
+ log.Fatal(err)
+ }
+ if n != size {
+ log.Fatalf("copy: expected %v bytes, got %d", size, n)
+ }
+ log.Printf("read %v bytes in %s", size, time.Since(t1))
+}
diff --git a/vendor/github.com/pkg/sftp/examples/buffered-write-benchmark/main.go b/vendor/github.com/pkg/sftp/examples/buffered-write-benchmark/main.go
new file mode 100644
index 00000000..d1babedb
--- /dev/null
+++ b/vendor/github.com/pkg/sftp/examples/buffered-write-benchmark/main.go
@@ -0,0 +1,84 @@
+// buffered-write-benchmark benchmarks the peformance of writing
+// a single large []byte on the client to /dev/null on the server via io.Copy.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "net"
+ "os"
+ "syscall"
+ "time"
+
+ "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/agent"
+
+ "github.com/pkg/sftp"
+)
+
+var (
+ USER = flag.String("user", os.Getenv("USER"), "ssh username")
+ HOST = flag.String("host", "localhost", "ssh server hostname")
+ PORT = flag.Int("port", 22, "ssh server port")
+ PASS = flag.String("pass", os.Getenv("SOCKSIE_SSH_PASSWORD"), "ssh password")
+ SIZE = flag.Int("s", 1<<15, "set max packet size")
+)
+
+func init() {
+ flag.Parse()
+}
+
+func main() {
+ var auths []ssh.AuthMethod
+ if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
+ auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
+
+ }
+ if *PASS != "" {
+ auths = append(auths, ssh.Password(*PASS))
+ }
+
+ config := ssh.ClientConfig{
+ User: *USER,
+ Auth: auths,
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ }
+ addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
+ conn, err := ssh.Dial("tcp", addr, &config)
+ if err != nil {
+ log.Fatalf("unable to connect to [%s]: %v", addr, err)
+ }
+ defer conn.Close()
+
+ c, err := sftp.NewClient(conn, sftp.MaxPacket(*SIZE))
+ if err != nil {
+ log.Fatalf("unable to start sftp subsytem: %v", err)
+ }
+ defer c.Close()
+
+ w, err := c.OpenFile("/dev/null", syscall.O_WRONLY)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer w.Close()
+
+ f, err := os.Open("/dev/zero")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer f.Close()
+
+ const size = 1e9
+
+ log.Printf("writing %v bytes", size)
+ t1 := time.Now()
+ n, err := w.Write(make([]byte, size))
+ if err != nil {
+ log.Fatal(err)
+ }
+ if n != size {
+ log.Fatalf("copy: expected %v bytes, got %d", size, n)
+ }
+ log.Printf("wrote %v bytes in %s", size, time.Since(t1))
+}
diff --git a/vendor/github.com/pkg/sftp/examples/request-server/main.go b/vendor/github.com/pkg/sftp/examples/request-server/main.go
new file mode 100644
index 00000000..fd21b43e
--- /dev/null
+++ b/vendor/github.com/pkg/sftp/examples/request-server/main.go
@@ -0,0 +1,131 @@
+// An example SFTP server implementation using the golang SSH package.
+// Serves the whole filesystem visible to the user, and has a hard-coded username and password,
+// so not for real use!
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "net"
+ "os"
+
+ "github.com/pkg/sftp"
+ "golang.org/x/crypto/ssh"
+)
+
+// Based on example server code from golang.org/x/crypto/ssh and server_standalone
+func main() {
+
+ var (
+ readOnly bool
+ debugStderr bool
+ )
+
+ flag.BoolVar(&readOnly, "R", false, "read-only server")
+ flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
+ flag.Parse()
+
+ debugStream := ioutil.Discard
+ if debugStderr {
+ debugStream = os.Stderr
+ }
+
+ // An SSH server is represented by a ServerConfig, which holds
+ // certificate details and handles authentication of ServerConns.
+ config := &ssh.ServerConfig{
+ PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
+ // Should use constant-time compare (or better, salt+hash) in
+ // a production setting.
+ fmt.Fprintf(debugStream, "Login: %s\n", c.User())
+ if c.User() == "testuser" && string(pass) == "tiger" {
+ return nil, nil
+ }
+ return nil, fmt.Errorf("password rejected for %q", c.User())
+ },
+ }
+
+ privateBytes, err := ioutil.ReadFile("id_rsa")
+ if err != nil {
+ log.Fatal("Failed to load private key", err)
+ }
+
+ private, err := ssh.ParsePrivateKey(privateBytes)
+ if err != nil {
+ log.Fatal("Failed to parse private key", err)
+ }
+
+ config.AddHostKey(private)
+
+ // Once a ServerConfig has been configured, connections can be
+ // accepted.
+ listener, err := net.Listen("tcp", "0.0.0.0:2022")
+ if err != nil {
+ log.Fatal("failed to listen for connection", err)
+ }
+ fmt.Printf("Listening on %v\n", listener.Addr())
+
+ nConn, err := listener.Accept()
+ if err != nil {
+ log.Fatal("failed to accept incoming connection", err)
+ }
+
+ // Before use, a handshake must be performed on the incoming net.Conn.
+ sconn, chans, reqs, err := ssh.NewServerConn(nConn, config)
+ if err != nil {
+ log.Fatal("failed to handshake", err)
+ }
+ log.Println("login detected:", sconn.User())
+ fmt.Fprintf(debugStream, "SSH server established\n")
+
+ // The incoming Request channel must be serviced.
+ go ssh.DiscardRequests(reqs)
+
+ // Service the incoming Channel channel.
+ for newChannel := range chans {
+ // Channels have a type, depending on the application level
+ // protocol intended. In the case of an SFTP session, this is "subsystem"
+ // with a payload string of "<length=4>sftp"
+ fmt.Fprintf(debugStream, "Incoming channel: %s\n", newChannel.ChannelType())
+ if newChannel.ChannelType() != "session" {
+ newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
+ fmt.Fprintf(debugStream, "Unknown channel type: %s\n", newChannel.ChannelType())
+ continue
+ }
+ channel, requests, err := newChannel.Accept()
+ if err != nil {
+ log.Fatal("could not accept channel.", err)
+ }
+ fmt.Fprintf(debugStream, "Channel accepted\n")
+
+ // Sessions have out-of-band requests such as "shell",
+ // "pty-req" and "env". Here we handle only the
+ // "subsystem" request.
+ go func(in <-chan *ssh.Request) {
+ for req := range in {
+ fmt.Fprintf(debugStream, "Request: %v\n", req.Type)
+ ok := false
+ switch req.Type {
+ case "subsystem":
+ fmt.Fprintf(debugStream, "Subsystem: %s\n", req.Payload[4:])
+ if string(req.Payload[4:]) == "sftp" {
+ ok = true
+ }
+ }
+ fmt.Fprintf(debugStream, " - accepted: %v\n", ok)
+ req.Reply(ok, nil)
+ }
+ }(requests)
+
+ root := sftp.InMemHandler()
+ server := sftp.NewRequestServer(channel, root)
+ if err := server.Serve(); err == io.EOF {
+ server.Close()
+ log.Print("sftp client exited session.")
+ } else if err != nil {
+ log.Fatal("sftp server completed with error:", err)
+ }
+ }
+}
diff --git a/vendor/github.com/pkg/sftp/examples/sftp-server/main.go b/vendor/github.com/pkg/sftp/examples/sftp-server/main.go
new file mode 100644
index 00000000..48e0e868
--- /dev/null
+++ b/vendor/github.com/pkg/sftp/examples/sftp-server/main.go
@@ -0,0 +1,147 @@
+// An example SFTP server implementation using the golang SSH package.
+// Serves the whole filesystem visible to the user, and has a hard-coded username and password,
+// so not for real use!
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "net"
+ "os"
+
+ "github.com/pkg/sftp"
+ "golang.org/x/crypto/ssh"
+)
+
+// Based on example server code from golang.org/x/crypto/ssh and server_standalone
+func main() {
+
+ var (
+ readOnly bool
+ debugStderr bool
+ )
+
+ flag.BoolVar(&readOnly, "R", false, "read-only server")
+ flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
+ flag.Parse()
+
+ debugStream := ioutil.Discard
+ if debugStderr {
+ debugStream = os.Stderr
+ }
+
+ // An SSH server is represented by a ServerConfig, which holds
+ // certificate details and handles authentication of ServerConns.
+ config := &ssh.ServerConfig{
+ PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
+ // Should use constant-time compare (or better, salt+hash) in
+ // a production setting.
+ fmt.Fprintf(debugStream, "Login: %s\n", c.User())
+ if c.User() == "testuser" && string(pass) == "tiger" {
+ return nil, nil
+ }
+ return nil, fmt.Errorf("password rejected for %q", c.User())
+ },
+ }
+
+ privateBytes, err := ioutil.ReadFile("id_rsa")
+ if err != nil {
+ log.Fatal("Failed to load private key", err)
+ }
+
+ private, err := ssh.ParsePrivateKey(privateBytes)
+ if err != nil {
+ log.Fatal("Failed to parse private key", err)
+ }
+
+ config.AddHostKey(private)
+
+ // Once a ServerConfig has been configured, connections can be
+ // accepted.
+ listener, err := net.Listen("tcp", "0.0.0.0:2022")
+ if err != nil {
+ log.Fatal("failed to listen for connection", err)
+ }
+ fmt.Printf("Listening on %v\n", listener.Addr())
+
+ nConn, err := listener.Accept()
+ if err != nil {
+ log.Fatal("failed to accept incoming connection", err)
+ }
+
+ // Before use, a handshake must be performed on the incoming
+ // net.Conn.
+ _, chans, reqs, err := ssh.NewServerConn(nConn, config)
+ if err != nil {
+ log.Fatal("failed to handshake", err)
+ }
+ fmt.Fprintf(debugStream, "SSH server established\n")
+
+ // The incoming Request channel must be serviced.
+ go ssh.DiscardRequests(reqs)
+
+ // Service the incoming Channel channel.
+ for newChannel := range chans {
+ // Channels have a type, depending on the application level
+ // protocol intended. In the case of an SFTP session, this is "subsystem"
+ // with a payload string of "<length=4>sftp"
+ fmt.Fprintf(debugStream, "Incoming channel: %s\n", newChannel.ChannelType())
+ if newChannel.ChannelType() != "session" {
+ newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
+ fmt.Fprintf(debugStream, "Unknown channel type: %s\n", newChannel.ChannelType())
+ continue
+ }
+ channel, requests, err := newChannel.Accept()
+ if err != nil {
+ log.Fatal("could not accept channel.", err)
+ }
+ fmt.Fprintf(debugStream, "Channel accepted\n")
+
+ // Sessions have out-of-band requests such as "shell",
+ // "pty-req" and "env". Here we handle only the
+ // "subsystem" request.
+ go func(in <-chan *ssh.Request) {
+ for req := range in {
+ fmt.Fprintf(debugStream, "Request: %v\n", req.Type)
+ ok := false
+ switch req.Type {
+ case "subsystem":
+ fmt.Fprintf(debugStream, "Subsystem: %s\n", req.Payload[4:])
+ if string(req.Payload[4:]) == "sftp" {
+ ok = true
+ }
+ }
+ fmt.Fprintf(debugStream, " - accepted: %v\n", ok)
+ req.Reply(ok, nil)
+ }
+ }(requests)
+
+ serverOptions := []sftp.ServerOption{
+ sftp.WithDebug(debugStream),
+ }
+
+ if readOnly {
+ serverOptions = append(serverOptions, sftp.ReadOnly())
+ fmt.Fprintf(debugStream, "Read-only server\n")
+ } else {
+ fmt.Fprintf(debugStream, "Read write server\n")
+ }
+
+ server, err := sftp.NewServer(
+ channel,
+ serverOptions...,
+ )
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := server.Serve(); err == io.EOF {
+ server.Close()
+ log.Print("sftp client exited session.")
+ } else if err != nil {
+ log.Fatal("sftp server completed with error:", err)
+ }
+ }
+}
diff --git a/vendor/github.com/pkg/sftp/examples/streaming-read-benchmark/main.go b/vendor/github.com/pkg/sftp/examples/streaming-read-benchmark/main.go
new file mode 100644
index 00000000..87afc5a3
--- /dev/null
+++ b/vendor/github.com/pkg/sftp/examples/streaming-read-benchmark/main.go
@@ -0,0 +1,85 @@
+// streaming-read-benchmark benchmarks the peformance of reading
+// from /dev/zero on the server to /dev/null on the client via io.Copy.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "os"
+ "syscall"
+ "time"
+
+ "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/agent"
+
+ "github.com/pkg/sftp"
+)
+
+var (
+ USER = flag.String("user", os.Getenv("USER"), "ssh username")
+ HOST = flag.String("host", "localhost", "ssh server hostname")
+ PORT = flag.Int("port", 22, "ssh server port")
+ PASS = flag.String("pass", os.Getenv("SOCKSIE_SSH_PASSWORD"), "ssh password")
+ SIZE = flag.Int("s", 1<<15, "set max packet size")
+)
+
+func init() {
+ flag.Parse()
+}
+
+func main() {
+ var auths []ssh.AuthMethod
+ if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
+ auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
+
+ }
+ if *PASS != "" {
+ auths = append(auths, ssh.Password(*PASS))
+ }
+
+ config := ssh.ClientConfig{
+ User: *USER,
+ Auth: auths,
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ }
+ addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
+ conn, err := ssh.Dial("tcp", addr, &config)
+ if err != nil {
+ log.Fatalf("unable to connect to [%s]: %v", addr, err)
+ }
+ defer conn.Close()
+
+ c, err := sftp.NewClient(conn, sftp.MaxPacket(*SIZE))
+ if err != nil {
+ log.Fatalf("unable to start sftp subsytem: %v", err)
+ }
+ defer c.Close()
+
+ r, err := c.Open("/dev/zero")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer r.Close()
+
+ w, err := os.OpenFile("/dev/null", syscall.O_WRONLY, 0600)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer w.Close()
+
+ const size int64 = 1e9
+
+ log.Printf("reading %v bytes", size)
+ t1 := time.Now()
+ n, err := io.Copy(w, io.LimitReader(r, size))
+ if err != nil {
+ log.Fatal(err)
+ }
+ if n != size {
+ log.Fatalf("copy: expected %v bytes, got %d", size, n)
+ }
+ log.Printf("read %v bytes in %s", size, time.Since(t1))
+}
diff --git a/vendor/github.com/pkg/sftp/examples/streaming-write-benchmark/main.go b/vendor/github.com/pkg/sftp/examples/streaming-write-benchmark/main.go
new file mode 100644
index 00000000..8f432d39
--- /dev/null
+++ b/vendor/github.com/pkg/sftp/examples/streaming-write-benchmark/main.go
@@ -0,0 +1,85 @@
+// streaming-write-benchmark benchmarks the peformance of writing
+// from /dev/zero on the client to /dev/null on the server via io.Copy.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "os"
+ "syscall"
+ "time"
+
+ "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/agent"
+
+ "github.com/pkg/sftp"
+)
+
+var (
+ USER = flag.String("user", os.Getenv("USER"), "ssh username")
+ HOST = flag.String("host", "localhost", "ssh server hostname")
+ PORT = flag.Int("port", 22, "ssh server port")
+ PASS = flag.String("pass", os.Getenv("SOCKSIE_SSH_PASSWORD"), "ssh password")
+ SIZE = flag.Int("s", 1<<15, "set max packet size")
+)
+
+func init() {
+ flag.Parse()
+}
+
+func main() {
+ var auths []ssh.AuthMethod
+ if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
+ auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
+
+ }
+ if *PASS != "" {
+ auths = append(auths, ssh.Password(*PASS))
+ }
+
+ config := ssh.ClientConfig{
+ User: *USER,
+ Auth: auths,
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ }
+ addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
+ conn, err := ssh.Dial("tcp", addr, &config)
+ if err != nil {
+ log.Fatalf("unable to connect to [%s]: %v", addr, err)
+ }
+ defer conn.Close()
+
+ c, err := sftp.NewClient(conn, sftp.MaxPacket(*SIZE))
+ if err != nil {
+ log.Fatalf("unable to start sftp subsytem: %v", err)
+ }
+ defer c.Close()
+
+ w, err := c.OpenFile("/dev/null", syscall.O_WRONLY)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer w.Close()
+
+ f, err := os.Open("/dev/zero")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer f.Close()
+
+ const size int64 = 1e9
+
+ log.Printf("writing %v bytes", size)
+ t1 := time.Now()
+ n, err := io.Copy(w, io.LimitReader(f, size))
+ if err != nil {
+ log.Fatal(err)
+ }
+ if n != size {
+ log.Fatalf("copy: expected %v bytes, got %d", size, n)
+ }
+ log.Printf("wrote %v bytes in %s", size, time.Since(t1))
+}