// newSSH2Server constructs a ServerTransport based on HTTP2. ConnectionError is // returned if something goes wrong. func newSSH2Server(conn net.Conn, maxStreams uint32) (_ ServerTransport, err error) { logrus.SetLevel(logrus.DebugLevel) logrus.Debugln("newSSH2Server") keyAuthCallback := func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { logrus.Debugln("newSSH2Server -- user public key: ", hex.EncodeToString(key.Marshal())[:64]+"...") return &ssh.Permissions{}, nil } config := &ssh.ServerConfig{ PublicKeyCallback: keyAuthCallback, } // get or create host key for ssh server appPath, err := osext.Executable() if err != nil { return nil, err } keyPath := filepath.Join(filepath.Dir(appPath), "hostKey.pem") hostKey, err := sshutil.KeyLoader{Path: keyPath, Flags: sshutil.Create + sshutil.Save + sshutil.RSA2048}.Load() if err != nil { return nil, err } config.AddHostKey(hostKey) t := &ssh2Server{ conn: conn, writableChan: make(chan int, 1), channelsByStreamId: make(map[uint32]*ssh.Channel), } t.sshServerConn, t.newChans, t.globalReqs, err = ssh.NewServerConn(conn, config) if err != nil { logrus.Debugln("newSSH2Server -- Failed to hanshake:", err.Error()) return nil, err } else { logrus.Debugln("newSSH2Server -- hanshake OK") } t.writableChan <- 0 return t, nil }
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{}) // } }