|
@@ -2,6 +2,7 @@ package sshprox
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
"os"
|
|
@@ -10,6 +11,7 @@ import (
|
|
|
"runtime"
|
|
|
"strconv"
|
|
|
|
|
|
+ "github.com/google/uuid"
|
|
|
"imuslab.com/zoraxy/mod/reverseproxy"
|
|
|
"imuslab.com/zoraxy/mod/utils"
|
|
|
)
|
|
@@ -29,6 +31,7 @@ type Manager struct {
|
|
|
}
|
|
|
|
|
|
type Instance struct {
|
|
|
+ UUID string
|
|
|
ExecPath string
|
|
|
conn *reverseproxy.ReverseProxy //HTTP proxy
|
|
|
tty *exec.Cmd //SSH connection ported to web interface
|
|
@@ -59,6 +62,24 @@ func (m *Manager) GetNextPort() int {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (m *Manager) HandleHttpByInstanceId(instanceId string, w http.ResponseWriter, r *http.Request) {
|
|
|
+ targetInstance, err := m.GetInstanceById(instanceId)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ targetInstance.conn.ProxyHTTP(w, r)
|
|
|
+}
|
|
|
+
|
|
|
+func (m *Manager) GetInstanceById(instanceId string) (*Instance, error) {
|
|
|
+ for _, instance := range m.Instances {
|
|
|
+ if instance.UUID == instanceId {
|
|
|
+ return instance, nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil, fmt.Errorf("instance not found: %s", instanceId)
|
|
|
+}
|
|
|
func (m *Manager) NewSSHProxy(binaryRoot string) (*Instance, error) {
|
|
|
//Check if the binary exists in system/gotty/
|
|
|
binary := "gotty_" + runtime.GOOS + "_" + runtime.GOARCH
|
|
@@ -80,13 +101,13 @@ func (m *Manager) NewSSHProxy(binaryRoot string) (*Instance, error) {
|
|
|
}
|
|
|
|
|
|
return &Instance{
|
|
|
+ UUID: uuid.New().String(),
|
|
|
ExecPath: realpath,
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
//Create a new Connection to target address
|
|
|
func (i *Instance) CreateNewConnection(listenPort int, remoteIpAddr string, remotePort int) error {
|
|
|
-
|
|
|
//Create a gotty instance
|
|
|
cmd := exec.Command(i.ExecPath, "-w", "-p", strconv.Itoa(listenPort), "--once", "ssh", remoteIpAddr, "-p", strconv.Itoa(remotePort))
|
|
|
cmd.Stdout = os.Stdout
|
|
@@ -108,8 +129,3 @@ func (i *Instance) CreateNewConnection(listenPort int, remoteIpAddr string, remo
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
-
|
|
|
-//Bridge the remote connection to reverse proxy
|
|
|
-func (i *Instance) BridgeConnection(w http.ResponseWriter, r *http.Request) error {
|
|
|
- return i.conn.ProxyHTTP(w, r)
|
|
|
-}
|