webssh.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "imuslab.com/zoraxy/mod/sshprox"
  9. "imuslab.com/zoraxy/mod/utils"
  10. )
  11. /*
  12. webssh.go
  13. This script handle the establish of a new ssh proxy object
  14. */
  15. func HandleCreateProxySession(w http.ResponseWriter, r *http.Request) {
  16. //Get what ip address and port to connect to
  17. ipaddr, err := utils.PostPara(r, "ipaddr")
  18. if err != nil {
  19. http.Error(w, "Invalid Usage", http.StatusInternalServerError)
  20. return
  21. }
  22. portString, err := utils.PostPara(r, "port")
  23. if err != nil {
  24. portString = "22"
  25. }
  26. username, err := utils.PostPara(r, "username")
  27. if err != nil {
  28. username = ""
  29. }
  30. port, err := strconv.Atoi(portString)
  31. if err != nil {
  32. utils.SendErrorResponse(w, "invalid port number given")
  33. return
  34. }
  35. //Check if the target is a valid ssh endpoint
  36. if !sshprox.IsSSHConnectable(ipaddr, port) {
  37. utils.SendErrorResponse(w, ipaddr+":"+strconv.Itoa(port)+" is not a valid SSH server")
  38. return
  39. }
  40. //Create a new proxy instance
  41. instance, err := webSshManager.NewSSHProxy("./system/gotty")
  42. if err != nil {
  43. utils.SendErrorResponse(w, strings.ReplaceAll(err.Error(), "\\", "/"))
  44. return
  45. }
  46. //Create an ssh process to the target address
  47. err = instance.CreateNewConnection(webSshManager.GetNextPort(), username, ipaddr, port)
  48. if err != nil {
  49. utils.SendErrorResponse(w, err.Error())
  50. return
  51. }
  52. //Return the instance uuid
  53. js, _ := json.Marshal(instance.UUID)
  54. utils.SendJSONResponse(w, string(js))
  55. }
  56. func HandleTest(w http.ResponseWriter, r *http.Request) {
  57. fmt.Println(sshprox.IsSSHConnectable("192.168.1.120", 22))
  58. }