|
@@ -2,6 +2,7 @@ package agi
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "fmt"
|
|
|
"io/ioutil"
|
|
|
"log"
|
|
|
"net/http"
|
|
@@ -9,9 +10,15 @@ import (
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/google/uuid"
|
|
|
"imuslab.com/arozos/mod/common"
|
|
|
)
|
|
|
|
|
|
+type endpointFormat struct {
|
|
|
+ Username string `json:"username"`
|
|
|
+ Path string `json:"path"`
|
|
|
+}
|
|
|
+
|
|
|
//Handle request from EXTERNAL RESTFUL API
|
|
|
func (g *Gateway) ExtAPIHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
// get db
|
|
@@ -33,21 +40,15 @@ func (g *Gateway) ExtAPIHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
// check if UUID exists in the database
|
|
|
- if sysdb.KeyExists("external_agi", subpathElements[1]) {
|
|
|
+ // get the info from the database
|
|
|
+ data, isExist := g.checkIfExternalEndpointExist(subpathElements[1])
|
|
|
+ if !isExist {
|
|
|
common.SendErrorResponse(w, "Bad Request, invaild UUID entered")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // get the info from the database
|
|
|
- var dat struct {
|
|
|
- Username string `json:"username"`
|
|
|
- Path string `json:"path"`
|
|
|
- }
|
|
|
- jsonData := ""
|
|
|
- sysdb.Read("external_agi", subpathElements[1], &jsonData)
|
|
|
- json.Unmarshal([]byte(jsonData), &dat)
|
|
|
- usernameFromDb := dat.Username
|
|
|
- pathFromDb := dat.Path
|
|
|
+ usernameFromDb := data.Username
|
|
|
+ pathFromDb := data.Path
|
|
|
|
|
|
// get the userinfo and the realPath
|
|
|
userInfo, err := g.Option.UserHandler.GetUserInfoFromUsername(usernameFromDb)
|
|
@@ -77,3 +78,109 @@ func (g *Gateway) ExtAPIHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
log.Println("[Remote AGI] IP:", r.RemoteAddr, " executed the script ", pathFromDb, "(", realPath, ")", " on behalf of", userInfo.Username, "with total duration: ", duration)
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+func (g *Gateway) AddExternalEndPoint(w http.ResponseWriter, r *http.Request) {
|
|
|
+ userInfo, _ := g.Option.UserHandler.GetUserInfoFromRequest(w, r)
|
|
|
+ // get db
|
|
|
+ sysdb := g.Option.UserHandler.GetDatabase()
|
|
|
+ if !sysdb.TableExists("external_agi") {
|
|
|
+ sysdb.NewTable("external_agi")
|
|
|
+ }
|
|
|
+ var dat endpointFormat
|
|
|
+
|
|
|
+ // uuid: [path, id]
|
|
|
+ path, _ := common.Mv(r, "path", false)
|
|
|
+ id := uuid.New().String()
|
|
|
+
|
|
|
+ dat.Path = path
|
|
|
+ dat.Username = userInfo.Username
|
|
|
+
|
|
|
+ jsonStr, _ := json.Marshal(dat)
|
|
|
+
|
|
|
+ sysdb.Write("external_agi", id, string(jsonStr))
|
|
|
+}
|
|
|
+
|
|
|
+func (g *Gateway) RemoveExternalEndPoint(w http.ResponseWriter, r *http.Request) {
|
|
|
+ userInfo, err := g.Option.UserHandler.GetUserInfoFromRequest(w, r)
|
|
|
+ if err != nil {
|
|
|
+ common.SendErrorResponse(w, "Bad User")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // get db
|
|
|
+ sysdb := g.Option.UserHandler.GetDatabase()
|
|
|
+ if !sysdb.TableExists("external_agi") {
|
|
|
+ sysdb.NewTable("external_agi")
|
|
|
+ }
|
|
|
+ // get path
|
|
|
+ uuid, _ := common.Mv(r, "uuid", false)
|
|
|
+ data, isExist := g.checkIfExternalEndpointExist(uuid)
|
|
|
+ if !isExist {
|
|
|
+ common.SendErrorResponse(w, "UUID does not exists in the database!")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if data.Username != userInfo.Username {
|
|
|
+ common.SendErrorResponse(w, "Bad Request, you have no permission to access this UUID entry!")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ sysdb.Delete("external_agi", uuid)
|
|
|
+
|
|
|
+ common.SendOK(w)
|
|
|
+}
|
|
|
+
|
|
|
+func (g *Gateway) ListExternalEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
+ userInfo, err := g.Option.UserHandler.GetUserInfoFromRequest(w, r)
|
|
|
+ if err != nil {
|
|
|
+ common.SendErrorResponse(w, "Bad User")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // get db
|
|
|
+ sysdb := g.Option.UserHandler.GetDatabase()
|
|
|
+ if !sysdb.TableExists("external_agi") {
|
|
|
+ sysdb.NewTable("external_agi")
|
|
|
+ }
|
|
|
+
|
|
|
+ // declare variable for return
|
|
|
+ dataFromDB := make(map[string]endpointFormat)
|
|
|
+
|
|
|
+ // O(n) method to do the lookup
|
|
|
+ entries, _ := sysdb.ListTable("external_agi")
|
|
|
+ for _, keypairs := range entries {
|
|
|
+ //Decode the string
|
|
|
+ var dataFromResult endpointFormat
|
|
|
+ result := ""
|
|
|
+ uuid := string(keypairs[0])
|
|
|
+ json.Unmarshal(keypairs[1], &result)
|
|
|
+ fmt.Println(result)
|
|
|
+ json.Unmarshal([]byte(result), &dataFromResult)
|
|
|
+ if dataFromResult.Username == userInfo.Username {
|
|
|
+ dataFromDB[uuid] = dataFromResult
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ returnJson, _ := json.Marshal(dataFromDB)
|
|
|
+
|
|
|
+ sendJSONResponse(w, string(returnJson))
|
|
|
+}
|
|
|
+
|
|
|
+func (g *Gateway) checkIfExternalEndpointExist(uuid string) (endpointFormat, bool) {
|
|
|
+ // get db
|
|
|
+ sysdb := g.Option.UserHandler.GetDatabase()
|
|
|
+ if !sysdb.TableExists("external_agi") {
|
|
|
+ sysdb.NewTable("external_agi")
|
|
|
+ }
|
|
|
+ var dat endpointFormat
|
|
|
+
|
|
|
+ if !sysdb.KeyExists("external_agi", uuid) {
|
|
|
+ return dat, false
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonData := ""
|
|
|
+ sysdb.Read("external_agi", uuid, &jsonData)
|
|
|
+ json.Unmarshal([]byte(jsonData), &dat)
|
|
|
+
|
|
|
+ return dat, true
|
|
|
+}
|