webssh.go 1.6 KB

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