sshprox.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package sshprox
  2. import (
  3. "errors"
  4. "net/http"
  5. "net/url"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "imuslab.com/zoraxy/mod/reverseproxy"
  11. "imuslab.com/zoraxy/mod/utils"
  12. )
  13. /*
  14. SSH Proxy
  15. This is a tool to bind gotty into Zoraxy
  16. so that you can do something similar to
  17. online ssh terminal
  18. */
  19. type Instance struct {
  20. ExecPath string
  21. }
  22. func NewSSHProxy(binaryRoot string) (*Instance, error) {
  23. //Check if the binary exists in system/gotty/
  24. binary := "gotty_" + runtime.GOOS + "_" + runtime.GOARCH
  25. execPath := filepath.Join(binaryRoot, binary)
  26. if !utils.FileExists(execPath) {
  27. //Binary not found
  28. return nil, errors.New("binary not found at " + execPath)
  29. }
  30. //Convert the binary path to realpath
  31. realpath, err := filepath.Abs(execPath)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &Instance{
  36. ExecPath: realpath,
  37. }, nil
  38. }
  39. //Create a new Connection to target address
  40. func (i *Instance) CreateNewConnection(ipaddr string) (*reverseproxy.ReverseProxy, error) {
  41. //Create a gotty instance
  42. cmd := exec.Command(i.ExecPath, "-p", "8080", "-w", "ssh", ipaddr)
  43. cmd.Stdout = os.Stdout
  44. cmd.Stderr = os.Stderr
  45. cmd.Run()
  46. //Create a new proxy agent for this root
  47. path, err := url.Parse(ipaddr)
  48. if err != nil {
  49. return nil, err
  50. }
  51. proxy := reverseproxy.NewReverseProxy(path)
  52. return proxy, nil
  53. }
  54. func HandleWebSSHConnection(w http.ResponseWriter, r *http.Request) {
  55. }