|
@@ -9,6 +9,7 @@ import (
|
|
"net/http"
|
|
"net/http"
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
|
+ "reflect"
|
|
"strings"
|
|
"strings"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -16,6 +17,7 @@ import (
|
|
uuid "github.com/satori/go.uuid"
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
|
|
apt "imuslab.com/arozos/mod/apt"
|
|
apt "imuslab.com/arozos/mod/apt"
|
|
|
|
+ "imuslab.com/arozos/mod/common"
|
|
"imuslab.com/arozos/mod/filesystem"
|
|
"imuslab.com/arozos/mod/filesystem"
|
|
metadata "imuslab.com/arozos/mod/filesystem/metadata"
|
|
metadata "imuslab.com/arozos/mod/filesystem/metadata"
|
|
"imuslab.com/arozos/mod/iot"
|
|
"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)
|
|
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
|
|
//Handle user requests
|
|
func (g *Gateway) InterfaceHandler(w http.ResponseWriter, r *http.Request, thisuser *user.User) {
|
|
func (g *Gateway) InterfaceHandler(w http.ResponseWriter, r *http.Request, thisuser *user.User) {
|
|
//Get user object from the request
|
|
//Get user object from the request
|