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.

83 lines
1.3 KiB

  1. package ssh
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "encoding/binary"
  6. "golang.org/x/crypto/ssh"
  7. )
  8. func generateSigner() (ssh.Signer, error) {
  9. key, err := rsa.GenerateKey(rand.Reader, 2048)
  10. if err != nil {
  11. return nil, err
  12. }
  13. return ssh.NewSignerFromKey(key)
  14. }
  15. func parsePtyRequest(s []byte) (pty Pty, ok bool) {
  16. term, s, ok := parseString(s)
  17. if !ok {
  18. return
  19. }
  20. width32, s, ok := parseUint32(s)
  21. if !ok {
  22. return
  23. }
  24. height32, _, ok := parseUint32(s)
  25. if !ok {
  26. return
  27. }
  28. pty = Pty{
  29. Term: term,
  30. Window: Window{
  31. Width: int(width32),
  32. Height: int(height32),
  33. },
  34. }
  35. return
  36. }
  37. func parseWinchRequest(s []byte) (win Window, ok bool) {
  38. width32, s, ok := parseUint32(s)
  39. if width32 < 1 {
  40. ok = false
  41. }
  42. if !ok {
  43. return
  44. }
  45. height32, _, ok := parseUint32(s)
  46. if height32 < 1 {
  47. ok = false
  48. }
  49. if !ok {
  50. return
  51. }
  52. win = Window{
  53. Width: int(width32),
  54. Height: int(height32),
  55. }
  56. return
  57. }
  58. func parseString(in []byte) (out string, rest []byte, ok bool) {
  59. if len(in) < 4 {
  60. return
  61. }
  62. length := binary.BigEndian.Uint32(in)
  63. if uint32(len(in)) < 4+length {
  64. return
  65. }
  66. out = string(in[4 : 4+length])
  67. rest = in[4+length:]
  68. ok = true
  69. return
  70. }
  71. func parseUint32(in []byte) (uint32, []byte, bool) {
  72. if len(in) < 4 {
  73. return 0, nil, false
  74. }
  75. return binary.BigEndian.Uint32(in), in[4:], true
  76. }