func runServer(hostport string, fs sftpd.FileSystem) error { config := &ssh.ServerConfig{ PasswordCallback: sshutil.CreatePasswordCheck(testUser, testPass), } // Add the sshutil.RSA2048 and sshutil.Save flags if needed for the server in question... hkey, e := sshutil.KeyLoader{Flags: sshutil.Create | sshutil.RSA2048}.Load() // hkey, e := sshutil.KeyLoader{Flags: sshutil.Create}.Load() if e != nil { return e } config.AddHostKey(hkey) listener, e := net.Listen("tcp", hostport) if e != nil { return e } log.Printf("Listening on %s user %s pass %s\n", hostport, testUser, testPass) for { conn, e := listener.Accept() if e != nil { return e } go HandleConn(conn, config, fs) } }
func ExampleConfig(fs FileSystem) { cfg := Config{HostPort: ":2022", FileSystem: fs, LogFunc: log.Println} cfg.PasswordCallback = sshutil.CreatePasswordCheck(testUser, testPass) // This creates a new host key for each run of the test. // Add the sshutil.RSA2048 and sshutil.Save flags if wanted. hkey, e := sshutil.KeyLoader{Flags: sshutil.Create}.Load() if e != nil { log.Println(e) return } cfg.AddHostKey(hkey) cfg.RunServer() }
// RunServerHighLevel is an example how to use the low level API func RunServerHighLevel(hostport string, fs sftpd.FileSystem) { cfg := sftpd.Config{HostPort: hostport, FileSystem: fs, LogFunc: log.Println} cfg.PasswordCallback = sshutil.CreatePasswordCheck(testUser, testPass) // Add the sshutil.RSA2048 and sshutil.Save flags if needed for the server in question... hkey, e := sshutil.KeyLoader{Flags: sshutil.Create}.Load() if e != nil { log.Println(e) return } cfg.AddHostKey(hkey) log.Printf("Listening on %s user %s pass %s\n", hostport, testUser, testPass) cfg.RunServer() }
func TestServer(t *testing.T) { tdebug = t.Log tdebugf = t.Logf tdebugf("Listening on port 2022 user %s pass %s\n", testUser, testPass) config := &ssh.ServerConfig{ PasswordCallback: sshutil.CreatePasswordCheck(testUser, testPass), } // Add the sshutil.RSA2048 and sshutil.Save flags if needed for the server in question... hkey, e := sshutil.KeyLoader{Flags: sshutil.Create}.Load() failOnErr(t, e, "Failed to parse host key") tdebugf("Public key: %s\n", sshutil.PublicKeyHash(hkey.PublicKey())) config.AddHostKey(hkey) listener, e := net.Listen("tcp", "127.0.0.1:2022") failOnErr(t, e, "Failed to listen") go ClientDo() // for { nConn, e := listener.Accept() failOnErr(t, e, "Failed to accept") handleTestConn(nConn, config, t, EmptyFS{}) // } go ClientDo() // for { nConn, e = listener.Accept() failOnErr(t, e, "Failed to accept") os.Mkdir("/tmp/test-sftpd", 0700) handleTestConn(nConn, config, t, rfs{}) // } }