You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

  1. package ssh
  2. import (
  3. "io/ioutil"
  4. gossh "golang.org/x/crypto/ssh"
  5. )
  6. // PasswordAuth returns a functional option that sets PasswordHandler on the server.
  7. func PasswordAuth(fn PasswordHandler) Option {
  8. return func(srv *Server) error {
  9. srv.PasswordHandler = fn
  10. return nil
  11. }
  12. }
  13. // PublicKeyAuth returns a functional option that sets PublicKeyHandler on the server.
  14. func PublicKeyAuth(fn PublicKeyHandler) Option {
  15. return func(srv *Server) error {
  16. srv.PublicKeyHandler = fn
  17. return nil
  18. }
  19. }
  20. // HostKeyFile returns a functional option that adds HostSigners to the server
  21. // from a PEM file at filepath.
  22. func HostKeyFile(filepath string) Option {
  23. return func(srv *Server) error {
  24. pemBytes, err := ioutil.ReadFile(filepath)
  25. if err != nil {
  26. return err
  27. }
  28. signer, err := gossh.ParsePrivateKey(pemBytes)
  29. if err != nil {
  30. return err
  31. }
  32. srv.AddHostKey(signer)
  33. return nil
  34. }
  35. }
  36. // HostKeyPEM returns a functional option that adds HostSigners to the server
  37. // from a PEM file as bytes.
  38. func HostKeyPEM(bytes []byte) Option {
  39. return func(srv *Server) error {
  40. signer, err := gossh.ParsePrivateKey(bytes)
  41. if err != nil {
  42. return err
  43. }
  44. srv.AddHostKey(signer)
  45. return nil
  46. }
  47. }
  48. // NoPty returns a functional option that sets PtyCallback to return false,
  49. // denying PTY requests.
  50. func NoPty() Option {
  51. return func(srv *Server) error {
  52. srv.PtyCallback = func(ctx Context, pty Pty) bool {
  53. return false
  54. }
  55. return nil
  56. }
  57. }
  58. // WrapConn returns a functional option that sets ConnCallback on the server.
  59. func WrapConn(fn ConnCallback) Option {
  60. return func(srv *Server) error {
  61. srv.ConnCallback = fn
  62. return nil
  63. }
  64. }