Alan Yeung hace 2 años
padre
commit
92e2a79ba1
Se han modificado 3 ficheros con 76 adiciones y 0 borrados
  1. 6 0
      lambda.json
  2. 2 0
      main.router.go
  3. 68 0
      mod/agi/agi.go

+ 6 - 0
lambda.json

@@ -0,0 +1,6 @@
+{
+    "uuid1": {
+      "username": "admin",
+      "path": "user:/test.agi"
+    }
+  }

+ 2 - 0
main.router.go

@@ -69,6 +69,8 @@ func mrouter(h http.Handler) http.Handler {
 			WebDavHandler.HandleRequest(w, r)
 		} else if len(r.URL.Path) >= len("/share") && r.URL.Path[:6] == "/share" {
 			shareManager.HandleShareAccess(w, r)
+		} else if len(r.URL.Path) >= len("/rexec") && r.URL.Path[:6] == "/rexec" {
+			AGIGateway.ExtAPIHandler(w, r)
 		} else if r.URL.Path == "/" && authAgent.CheckAuth(r) {
 			//Use logged in and request the index. Serve the user's interface module
 			w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")

+ 68 - 0
mod/agi/agi.go

@@ -9,6 +9,7 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
+	"reflect"
 	"strings"
 	"time"
 
@@ -16,6 +17,7 @@ import (
 	uuid "github.com/satori/go.uuid"
 
 	apt "imuslab.com/arozos/mod/apt"
+	"imuslab.com/arozos/mod/common"
 	"imuslab.com/arozos/mod/filesystem"
 	metadata "imuslab.com/arozos/mod/filesystem/metadata"
 	"imuslab.com/arozos/mod/iot"
@@ -208,6 +210,72 @@ func (g *Gateway) APIHandler(w http.ResponseWriter, r *http.Request, thisuser *u
 	g.ExecuteAGIScript(scriptContent, "", "", w, r, thisuser)
 }
 
+//Handle request from RESTFUL API
+func (g *Gateway) ExtAPIHandler(w http.ResponseWriter, r *http.Request) {
+	// obtain all information from "DB" aka our json file
+	// this should be either db or in the constructor instead for production
+	var db map[string]map[string]string
+	jsonFile, err := os.Open("lambda.json")
+	if err != nil {
+		common.SendErrorResponse(w, "Bad config")
+		return
+	}
+	jsonStr, err := ioutil.ReadAll(jsonFile)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad config")
+		return
+	}
+	json.Unmarshal([]byte(jsonStr), &db)
+	defer jsonFile.Close()
+	// end of our DB
+
+	// get the request URI from the r.URL
+	requestURI := filepath.ToSlash(filepath.Clean(r.URL.Path))
+	subpathElements := strings.Split(requestURI[1:], "/")
+
+	// check if it contains only two part, [rexec uuid]
+	if len(subpathElements) != 2 {
+		common.SendErrorResponse(w, "Bad Request, invaild request sent")
+		return
+	}
+
+	// check if UUID exists in the database
+	if db[subpathElements[1]] == nil || reflect.ValueOf(db[subpathElements[1]]).IsNil() {
+		common.SendErrorResponse(w, "Bad Request, invaild UUID entered")
+		return
+	}
+
+	// get the info from the database
+	usernameFromDb := db[subpathElements[1]]["username"]
+	pathFromDb := db[subpathElements[1]]["path"]
+
+	// get the userinfo and the realPath
+	userInfo, err := g.Option.UserHandler.GetUserInfoFromUsername(usernameFromDb)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad username")
+		return
+	}
+	_, realPath, err := virtualPathToRealPath(pathFromDb, userInfo)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad filepath")
+		return
+	}
+
+	// read the file and store it into scriptContent
+	scriptContentByte, err := ioutil.ReadFile(realPath)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad file I/O")
+		return
+	}
+	scriptContent := string(scriptContentByte)
+
+	log.Println("[Remote AGI] IP:", r.RemoteAddr, " executed the script ", pathFromDb, "(", realPath, ")", " on behalf of", userInfo.Username)
+
+	// execute!
+	g.ExecuteAGIScript(scriptContent, "", "", w, r, userInfo)
+
+}
+
 //Handle user requests
 func (g *Gateway) InterfaceHandler(w http.ResponseWriter, r *http.Request, thisuser *user.User) {
 	//Get user object from the request