Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
958882515f
3 changed files with 37 additions and 12 deletions
  1. 22 6
      mod/sshprox/sshprox.go
  2. 11 0
      router.go
  3. 4 6
      webssh.go

+ 22 - 6
mod/sshprox/sshprox.go

@@ -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)
-}

+ 11 - 0
router.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"net/http"
 	"strings"
 )
@@ -26,6 +27,16 @@ func AuthFsHandler(handler http.Handler) http.Handler {
 			return
 		}
 
+		//For WebSSH Routing
+		//Example URL Path: /web.ssh/{{instance_uuid}}/*
+		if strings.HasPrefix(r.URL.Path, "/web.ssh/") {
+			parts := strings.Split(r.URL.Path, "/")
+			if len(parts) > 2 {
+				instanceUUID := parts[2]
+				fmt.Println(instanceUUID)
+			}
+		}
+
 		//Authenticated
 		handler.ServeHTTP(w, r)
 	})

+ 4 - 6
webssh.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"encoding/json"
 	"fmt"
 	"net/http"
 	"strconv"
@@ -54,12 +55,9 @@ func HandleCreateProxySession(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	//Everything seems ok. Bridge the connection
-	err = instance.BridgeConnection(w, r)
-	if err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return
-	}
+	//Return the instance uuid
+	js, _ := json.Marshal(instance.UUID)
+	utils.SendJSONResponse(w, string(js))
 }
 
 func HandleTest(w http.ResponseWriter, r *http.Request) {