webssh.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "imuslab.com/zoraxy/mod/sshprox"
  8. "imuslab.com/zoraxy/mod/utils"
  9. )
  10. /*
  11. webssh.go
  12. This script handle the establish of a new ssh proxy object
  13. */
  14. func HandleCreateProxySession(w http.ResponseWriter, r *http.Request) {
  15. //Get what ip address and port to connect to
  16. ipaddr, err := utils.PostPara(r, "ipaddr")
  17. if err != nil {
  18. http.Error(w, "Invalid Usage", http.StatusInternalServerError)
  19. return
  20. }
  21. portString, err := utils.PostPara(r, "port")
  22. if err != nil {
  23. portString = "22"
  24. }
  25. username, err := utils.PostPara(r, "username")
  26. if err != nil {
  27. username = ""
  28. }
  29. port, err := strconv.Atoi(portString)
  30. if err != nil {
  31. utils.SendErrorResponse(w, "invalid port number given")
  32. return
  33. }
  34. if !*allowSshLoopback {
  35. //Not allow loopback connections
  36. if sshprox.IsLoopbackIPOrDomain(ipaddr) {
  37. //Request target is loopback
  38. utils.SendErrorResponse(w, "loopback web ssh connection is not enabled on this host")
  39. return
  40. }
  41. }
  42. //Check if the target is a valid ssh endpoint
  43. if !sshprox.IsSSHConnectable(ipaddr, port) {
  44. utils.SendErrorResponse(w, ipaddr+":"+strconv.Itoa(port)+" is not a valid SSH server")
  45. return
  46. }
  47. //Create a new proxy instance
  48. instance, err := webSshManager.NewSSHProxy("./tmp/gotty")
  49. if err != nil {
  50. utils.SendErrorResponse(w, strings.ReplaceAll(err.Error(), "\\", "/"))
  51. return
  52. }
  53. //Create an ssh process to the target address
  54. err = instance.CreateNewConnection(webSshManager.GetNextPort(), username, ipaddr, port)
  55. if err != nil {
  56. utils.SendErrorResponse(w, err.Error())
  57. return
  58. }
  59. //Return the instance uuid
  60. js, _ := json.Marshal(instance.UUID)
  61. utils.SendJSONResponse(w, string(js))
  62. }
  63. // Check if the host support ssh, or if the target domain (and port, optional) support ssh
  64. func HandleWebSshSupportCheck(w http.ResponseWriter, r *http.Request) {
  65. domain, err := utils.PostPara(r, "domain")
  66. if err != nil {
  67. //Check if ssh supported on this host
  68. isSupport := sshprox.IsWebSSHSupported()
  69. js, _ := json.Marshal(isSupport)
  70. utils.SendJSONResponse(w, string(js))
  71. } else {
  72. //Domain is given. Check if port is given
  73. portString, err := utils.PostPara(r, "port")
  74. if err != nil {
  75. portString = "22"
  76. }
  77. port, err := strconv.Atoi(portString)
  78. if err != nil {
  79. utils.SendErrorResponse(w, "invalid port number given")
  80. return
  81. }
  82. if port < 1 || port > 65534 {
  83. utils.SendErrorResponse(w, "invalid port number given")
  84. return
  85. }
  86. looksLikeSSHServer := sshprox.IsSSHConnectable(domain, port)
  87. js, _ := json.Marshal(looksLikeSSHServer)
  88. utils.SendJSONResponse(w, string(js))
  89. }
  90. }