ssh.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package v1
  2. import (
  3. "bytes"
  4. "net/http"
  5. "os/exec"
  6. "strconv"
  7. "time"
  8. "github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
  9. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  10. sshHelper "github.com/IceWhaleTech/CasaOS-Common/utils/ssh"
  11. "github.com/gin-gonic/gin"
  12. "github.com/gorilla/websocket"
  13. "go.uber.org/zap"
  14. "golang.org/x/crypto/ssh"
  15. modelCommon "github.com/IceWhaleTech/CasaOS-Common/model"
  16. )
  17. var upgrader = websocket.Upgrader{
  18. ReadBufferSize: 1024,
  19. WriteBufferSize: 1024,
  20. CheckOrigin: func(r *http.Request) bool { return true },
  21. HandshakeTimeout: time.Duration(time.Second * 5),
  22. }
  23. func PostSshLogin(c *gin.Context) {
  24. j := make(map[string]string)
  25. c.ShouldBind(&j)
  26. userName := j["username"]
  27. password := j["password"]
  28. port := j["port"]
  29. if userName == "" || password == "" || port == "" {
  30. c.JSON(common_err.CLIENT_ERROR, modelCommon.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: "Username or password or port is empty"})
  31. return
  32. }
  33. _, err := sshHelper.NewSshClient(userName, password, port)
  34. if err != nil {
  35. c.JSON(common_err.CLIENT_ERROR, modelCommon.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: "Please check if the username and port are correct, and make sure that ssh server is installed."})
  36. logger.Error("connect ssh error", zap.Any("error", err))
  37. return
  38. }
  39. c.JSON(common_err.SUCCESS, modelCommon.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
  40. }
  41. func WsSsh(c *gin.Context) {
  42. _, e := exec.LookPath("ssh")
  43. if e != nil {
  44. c.JSON(common_err.SERVICE_ERROR, modelCommon.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: "ssh server not found"})
  45. return
  46. }
  47. userName := c.Query("username")
  48. password := c.Query("password")
  49. port := c.Query("port")
  50. wsConn, _ := upgrader.Upgrade(c.Writer, c.Request, nil)
  51. logBuff := new(bytes.Buffer)
  52. quitChan := make(chan bool, 3)
  53. // user := ""
  54. // password := ""
  55. var login int = 1
  56. cols, _ := strconv.Atoi(c.DefaultQuery("cols", "200"))
  57. rows, _ := strconv.Atoi(c.DefaultQuery("rows", "32"))
  58. var client *ssh.Client
  59. for login != 0 {
  60. var err error
  61. if userName == "" || password == "" || port == "" {
  62. wsConn.WriteMessage(websocket.TextMessage, []byte("username or password or port is empty"))
  63. }
  64. client, err = sshHelper.NewSshClient(userName, password, port)
  65. if err != nil && client == nil {
  66. wsConn.WriteMessage(websocket.TextMessage, []byte(err.Error()))
  67. wsConn.WriteMessage(websocket.TextMessage, []byte("\r\n\x1b[0m"))
  68. } else {
  69. login = 0
  70. }
  71. }
  72. if client != nil {
  73. defer client.Close()
  74. }
  75. ssConn, _ := sshHelper.NewSshConn(cols, rows, client)
  76. defer ssConn.Close()
  77. go ssConn.ReceiveWsMsg(wsConn, logBuff, quitChan)
  78. go ssConn.SendComboOutput(wsConn, quitChan)
  79. go ssConn.SessionWait(quitChan)
  80. <-quitChan
  81. }