1
0

webssh.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if !*allowSshLoopback {
  36. //Not allow loopback connections
  37. if strings.EqualFold(strings.TrimSpace(ipaddr), "localhost") || strings.TrimSpace(ipaddr) == "127.0.0.1" {
  38. //Request target is loopback
  39. utils.SendErrorResponse(w, "loopback web ssh connection is not enabled on this host")
  40. return
  41. }
  42. }
  43. //Check if the target is a valid ssh endpoint
  44. if !sshprox.IsSSHConnectable(ipaddr, port) {
  45. utils.SendErrorResponse(w, ipaddr+":"+strconv.Itoa(port)+" is not a valid SSH server")
  46. return
  47. }
  48. //Create a new proxy instance
  49. instance, err := webSshManager.NewSSHProxy("./system/gotty")
  50. if err != nil {
  51. utils.SendErrorResponse(w, strings.ReplaceAll(err.Error(), "\\", "/"))
  52. return
  53. }
  54. //Create an ssh process to the target address
  55. err = instance.CreateNewConnection(webSshManager.GetNextPort(), username, ipaddr, port)
  56. if err != nil {
  57. utils.SendErrorResponse(w, err.Error())
  58. return
  59. }
  60. //Return the instance uuid
  61. js, _ := json.Marshal(instance.UUID)
  62. utils.SendJSONResponse(w, string(js))
  63. }
  64. func HandleTest(w http.ResponseWriter, r *http.Request) {
  65. fmt.Println(sshprox.IsSSHConnectable("192.168.1.120", 22))
  66. }