webssh.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  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.GetPara(r, "ipaddr")
  17. if err != nil {
  18. http.Error(w, "Invalid Usage", http.StatusInternalServerError)
  19. return
  20. }
  21. portString, err := utils.GetPara(r, "port")
  22. if err != nil {
  23. portString = "22"
  24. }
  25. port, err := strconv.Atoi(portString)
  26. if err != nil {
  27. http.Error(w, "Invalid port number given", http.StatusInternalServerError)
  28. return
  29. }
  30. //Check if the target is a valid ssh endpoint
  31. if !sshprox.IsSSHConnectable(ipaddr, port) {
  32. http.Error(w, ipaddr+":"+strconv.Itoa(port)+" is not a valid SSH server", http.StatusInternalServerError)
  33. return
  34. }
  35. //Create a new proxy instance
  36. instance, err := webSshManager.NewSSHProxy("./system/gotty")
  37. if err != nil {
  38. http.Error(w, err.Error(), http.StatusInternalServerError)
  39. return
  40. }
  41. //Create an ssh process to the target address
  42. err = instance.CreateNewConnection(webSshManager.GetNextPort(), ipaddr, port)
  43. if err != nil {
  44. http.Error(w, err.Error(), http.StatusInternalServerError)
  45. return
  46. }
  47. //Return the instance uuid
  48. js, _ := json.Marshal(instance.UUID)
  49. utils.SendJSONResponse(w, string(js))
  50. }
  51. func HandleTest(w http.ResponseWriter, r *http.Request) {
  52. fmt.Println(sshprox.IsSSHConnectable("192.168.1.120", 22))
  53. }