本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

124 lines
3.8KB

  1. package ssh
  2. import (
  3. "crypto/subtle"
  4. "net"
  5. gossh "golang.org/x/crypto/ssh"
  6. )
  7. type Signal string
  8. // POSIX signals as listed in RFC 4254 Section 6.10.
  9. const (
  10. SIGABRT Signal = "ABRT"
  11. SIGALRM Signal = "ALRM"
  12. SIGFPE Signal = "FPE"
  13. SIGHUP Signal = "HUP"
  14. SIGILL Signal = "ILL"
  15. SIGINT Signal = "INT"
  16. SIGKILL Signal = "KILL"
  17. SIGPIPE Signal = "PIPE"
  18. SIGQUIT Signal = "QUIT"
  19. SIGSEGV Signal = "SEGV"
  20. SIGTERM Signal = "TERM"
  21. SIGUSR1 Signal = "USR1"
  22. SIGUSR2 Signal = "USR2"
  23. )
  24. // DefaultHandler is the default Handler used by Serve.
  25. var DefaultHandler Handler
  26. // Option is a functional option handler for Server.
  27. type Option func(*Server) error
  28. // Handler is a callback for handling established SSH sessions.
  29. type Handler func(Session)
  30. // PublicKeyHandler is a callback for performing public key authentication.
  31. type PublicKeyHandler func(ctx Context, key PublicKey) bool
  32. // PasswordHandler is a callback for performing password authentication.
  33. type PasswordHandler func(ctx Context, password string) bool
  34. // KeyboardInteractiveHandler is a callback for performing keyboard-interactive authentication.
  35. type KeyboardInteractiveHandler func(ctx Context, challenger gossh.KeyboardInteractiveChallenge) bool
  36. // PtyCallback is a hook for allowing PTY sessions.
  37. type PtyCallback func(ctx Context, pty Pty) bool
  38. // SessionRequestCallback is a callback for allowing or denying SSH sessions.
  39. type SessionRequestCallback func(sess Session, requestType string) bool
  40. // ConnCallback is a hook for new connections before handling.
  41. // It allows wrapping for timeouts and limiting by returning
  42. // the net.Conn that will be used as the underlying connection.
  43. type ConnCallback func(conn net.Conn) net.Conn
  44. // LocalPortForwardingCallback is a hook for allowing port forwarding
  45. type LocalPortForwardingCallback func(ctx Context, destinationHost string, destinationPort uint32) bool
  46. // ReversePortForwardingCallback is a hook for allowing reverse port forwarding
  47. type ReversePortForwardingCallback func(ctx Context, bindHost string, bindPort uint32) bool
  48. // ServerConfigCallback is a hook for creating custom default server configs
  49. type ServerConfigCallback func(ctx Context) *gossh.ServerConfig
  50. // Window represents the size of a PTY window.
  51. type Window struct {
  52. Width int
  53. Height int
  54. }
  55. // Pty represents a PTY request and configuration.
  56. type Pty struct {
  57. Term string
  58. Window Window
  59. // HELP WANTED: terminal modes!
  60. }
  61. // Serve accepts incoming SSH connections on the listener l, creating a new
  62. // connection goroutine for each. The connection goroutines read requests and
  63. // then calls handler to handle sessions. Handler is typically nil, in which
  64. // case the DefaultHandler is used.
  65. func Serve(l net.Listener, handler Handler, options ...Option) error {
  66. srv := &Server{Handler: handler}
  67. for _, option := range options {
  68. if err := srv.SetOption(option); err != nil {
  69. return err
  70. }
  71. }
  72. return srv.Serve(l)
  73. }
  74. // ListenAndServe listens on the TCP network address addr and then calls Serve
  75. // with handler to handle sessions on incoming connections. Handler is typically
  76. // nil, in which case the DefaultHandler is used.
  77. func ListenAndServe(addr string, handler Handler, options ...Option) error {
  78. srv := &Server{Addr: addr, Handler: handler}
  79. for _, option := range options {
  80. if err := srv.SetOption(option); err != nil {
  81. return err
  82. }
  83. }
  84. return srv.ListenAndServe()
  85. }
  86. // Handle registers the handler as the DefaultHandler.
  87. func Handle(handler Handler) {
  88. DefaultHandler = handler
  89. }
  90. // KeysEqual is constant time compare of the keys to avoid timing attacks.
  91. func KeysEqual(ak, bk PublicKey) bool {
  92. //avoid panic if one of the keys is nil, return false instead
  93. if ak == nil || bk == nil {
  94. return false
  95. }
  96. a := ak.Marshal()
  97. b := bk.Marshal()
  98. return (len(a) == len(b) && subtle.ConstantTimeCompare(a, b) == 1)
  99. }
上海开阖软件有限公司 沪ICP备12045867号-1