agi.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package agi
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "path/filepath"
  9. "strings"
  10. "github.com/robertkrimen/otto"
  11. apt "imuslab.com/arozos/mod/apt"
  12. metadata "imuslab.com/arozos/mod/filesystem/metadata"
  13. "imuslab.com/arozos/mod/iot"
  14. "imuslab.com/arozos/mod/share"
  15. "imuslab.com/arozos/mod/time/nightly"
  16. user "imuslab.com/arozos/mod/user"
  17. )
  18. /*
  19. ArOZ Online Javascript Gateway Interface (AGI)
  20. author: tobychui
  21. This script load plugins written in Javascript and run them in VM inside golang
  22. DO NOT CONFUSE PLUGIN WITH SUBSERVICE :))
  23. */
  24. var (
  25. AgiVersion string = "1.6" //Defination of the agi runtime version. Update this when new function is added
  26. )
  27. type AgiLibIntergface func(*otto.Otto, *user.User) //Define the lib loader interface for AGI Libraries
  28. type AgiPackage struct {
  29. InitRoot string //The initialization of the root for the module that request this package
  30. }
  31. type AgiSysInfo struct {
  32. //System information
  33. BuildVersion string
  34. InternalVersion string
  35. LoadedModule []string
  36. //System Handlers
  37. UserHandler *user.UserHandler
  38. ReservedTables []string
  39. PackageManager *apt.AptPackageManager
  40. ModuleRegisterParser func(string) error
  41. FileSystemRender *metadata.RenderHandler
  42. IotManager *iot.Manager
  43. ShareManager *share.Manager
  44. NightlyManager *nightly.TaskManager
  45. //Scanning Roots
  46. StartupRoot string
  47. ActivateScope []string
  48. }
  49. type Gateway struct {
  50. ReservedTables []string
  51. NightlyScripts []string
  52. AllowAccessPkgs map[string][]AgiPackage
  53. LoadedAGILibrary map[string]AgiLibIntergface
  54. Option *AgiSysInfo
  55. }
  56. func NewGateway(option AgiSysInfo) (*Gateway, error) {
  57. //Handle startup registration of ajgi modules
  58. gatewayObject := Gateway{
  59. ReservedTables: option.ReservedTables,
  60. NightlyScripts: []string{},
  61. AllowAccessPkgs: map[string][]AgiPackage{},
  62. LoadedAGILibrary: map[string]AgiLibIntergface{},
  63. Option: &option,
  64. }
  65. //Start all WebApps Registration
  66. gatewayObject.InitiateAllWebAppModules()
  67. gatewayObject.RegisterNightlyOperations()
  68. //Load all the other libs entry points into the memoary
  69. gatewayObject.ImageLibRegister()
  70. gatewayObject.FileLibRegister()
  71. gatewayObject.HTTPLibRegister()
  72. gatewayObject.ShareLibRegister()
  73. gatewayObject.IoTLibRegister()
  74. gatewayObject.AppdataLibRegister()
  75. return &gatewayObject, nil
  76. }
  77. func (g *Gateway) RegisterNightlyOperations() {
  78. g.Option.NightlyManager.RegisterNightlyTask(func() {
  79. //This function will execute nightly
  80. for _, scriptFile := range g.NightlyScripts {
  81. if isValidAGIScript(scriptFile) {
  82. //Valid script file. Execute it with system
  83. for _, username := range g.Option.UserHandler.GetAuthAgent().ListUsers() {
  84. userinfo, err := g.Option.UserHandler.GetUserInfoFromUsername(username)
  85. if err != nil {
  86. continue
  87. }
  88. if checkUserAccessToScript(userinfo, scriptFile, "") {
  89. //This user can access the module that provide this script.
  90. //Execute this script on his account.
  91. log.Println("[AGI_Nightly] WIP (" + scriptFile + ")")
  92. }
  93. }
  94. } else {
  95. //Invalid script. Skipping
  96. log.Println("[AGI_Nightly] Invalid script file: " + scriptFile)
  97. }
  98. }
  99. })
  100. }
  101. func (g *Gateway) InitiateAllWebAppModules() {
  102. startupScripts, _ := filepath.Glob(filepath.ToSlash(filepath.Clean(g.Option.StartupRoot)) + "/*/init.agi")
  103. for _, script := range startupScripts {
  104. scriptContentByte, _ := ioutil.ReadFile(script)
  105. scriptContent := string(scriptContentByte)
  106. log.Println("[AGI] Gateway script loaded (" + script + ")")
  107. //Create a new vm for this request
  108. vm := otto.New()
  109. //Only allow non user based operations
  110. g.injectStandardLibs(vm, script, "./web/")
  111. _, err := vm.Run(scriptContent)
  112. if err != nil {
  113. log.Println("[AGI] Load Failed: " + script + ". Skipping.")
  114. log.Println(err)
  115. continue
  116. }
  117. }
  118. }
  119. func (g *Gateway) RunScript(script string) error {
  120. //Create a new vm for this request
  121. vm := otto.New()
  122. //Only allow non user based operations
  123. g.injectStandardLibs(vm, "", "./web/")
  124. _, err := vm.Run(script)
  125. if err != nil {
  126. log.Println("[AGI] Script Execution Failed: ", err.Error())
  127. return err
  128. }
  129. return nil
  130. }
  131. func (g *Gateway) RegisterLib(libname string, entryPoint AgiLibIntergface) error {
  132. _, ok := g.LoadedAGILibrary[libname]
  133. if ok {
  134. //This lib already registered. Return error
  135. return errors.New("This library name already registered")
  136. } else {
  137. g.LoadedAGILibrary[libname] = entryPoint
  138. }
  139. return nil
  140. }
  141. func (g *Gateway) raiseError(err error) {
  142. log.Println("[AGI] Runtime Error " + err.Error())
  143. //To be implemented
  144. }
  145. //Check if this table is restricted table. Return true if the access is valid
  146. func (g *Gateway) filterDBTable(tablename string, existsCheck bool) bool {
  147. //Check if table is restricted
  148. if stringInSlice(tablename, g.ReservedTables) {
  149. return false
  150. }
  151. //Check if table exists
  152. if existsCheck {
  153. if !g.Option.UserHandler.GetDatabase().TableExists(tablename) {
  154. return false
  155. }
  156. }
  157. return true
  158. }
  159. //Handle request from RESTFUL API
  160. func (g *Gateway) APIHandler(w http.ResponseWriter, r *http.Request, thisuser *user.User) {
  161. scriptContent, err := mv(r, "script", true)
  162. if err != nil {
  163. w.WriteHeader(http.StatusBadRequest)
  164. w.Write([]byte("400 - Bad Request (Missing script content)"))
  165. return
  166. }
  167. g.ExecuteAGIScript(scriptContent, "", "", w, r, thisuser)
  168. }
  169. //Handle user requests
  170. func (g *Gateway) InterfaceHandler(w http.ResponseWriter, r *http.Request, thisuser *user.User) {
  171. //Get user object from the request
  172. startupRoot := g.Option.StartupRoot
  173. startupRoot = filepath.ToSlash(filepath.Clean(startupRoot))
  174. //Get the script files for the plugin
  175. scriptFile, err := mv(r, "script", false)
  176. if err != nil {
  177. sendErrorResponse(w, "Invalid script path")
  178. return
  179. }
  180. scriptFile = specialURIDecode(scriptFile)
  181. //Check if the script path exists
  182. scriptExists := false
  183. scriptScope := "./web/"
  184. for _, thisScope := range g.Option.ActivateScope {
  185. thisScope = filepath.ToSlash(filepath.Clean(thisScope))
  186. if fileExists(thisScope + "/" + scriptFile) {
  187. scriptExists = true
  188. scriptFile = thisScope + "/" + scriptFile
  189. scriptScope = thisScope
  190. }
  191. }
  192. if !scriptExists {
  193. sendErrorResponse(w, "Script not found")
  194. return
  195. }
  196. //Check for user permission on this module
  197. moduleName := getScriptRoot(scriptFile, scriptScope)
  198. if !thisuser.GetModuleAccessPermission(moduleName) {
  199. w.WriteHeader(http.StatusForbidden)
  200. if g.Option.BuildVersion == "development" {
  201. w.Write([]byte("Permission denied: User do not have permission to access " + moduleName))
  202. } else {
  203. w.Write([]byte("403 Forbidden"))
  204. }
  205. return
  206. }
  207. //Check the given file is actually agi script
  208. if !(filepath.Ext(scriptFile) == ".agi" || filepath.Ext(scriptFile) == ".js") {
  209. w.WriteHeader(http.StatusForbidden)
  210. if g.Option.BuildVersion == "development" {
  211. w.Write([]byte("AGI script must have file extension of .agi or .js"))
  212. } else {
  213. w.Write([]byte("403 Forbidden"))
  214. }
  215. return
  216. }
  217. //Get the content of the script
  218. scriptContentByte, _ := ioutil.ReadFile(scriptFile)
  219. scriptContent := string(scriptContentByte)
  220. g.ExecuteAGIScript(scriptContent, scriptFile, scriptScope, w, r, thisuser)
  221. }
  222. /*
  223. Executing the given AGI Script contents. Requires:
  224. scriptContent: The AGI command sequence
  225. scriptFile: The filepath of the script file
  226. scriptScope: The scope of the script file, aka the module base path
  227. w / r : Web request and response writer
  228. thisuser: userObject
  229. */
  230. func (g *Gateway) ExecuteAGIScript(scriptContent string, scriptFile string, scriptScope string, w http.ResponseWriter, r *http.Request, thisuser *user.User) {
  231. //Create a new vm for this request
  232. vm := otto.New()
  233. //Inject standard libs into the vm
  234. g.injectStandardLibs(vm, scriptFile, scriptScope)
  235. g.injectUserFunctions(vm, scriptFile, scriptScope, thisuser, w, r)
  236. //Detect cotent type
  237. contentType := r.Header.Get("Content-type")
  238. if strings.Contains(contentType, "application/json") {
  239. //For shitty people who use Angular
  240. body, _ := ioutil.ReadAll(r.Body)
  241. fields := map[string]interface{}{}
  242. json.Unmarshal(body, &fields)
  243. for k, v := range fields {
  244. vm.Set(k, v)
  245. }
  246. vm.Set("POST_data", string(body))
  247. } else {
  248. r.ParseForm()
  249. //Insert all paramters into the vm
  250. for k, v := range r.PostForm {
  251. if len(v) == 1 {
  252. vm.Set(k, v[0])
  253. } else {
  254. vm.Set(k, v)
  255. }
  256. }
  257. }
  258. _, err := vm.Run(scriptContent)
  259. if err != nil {
  260. scriptpath, _ := filepath.Abs(scriptFile)
  261. g.RenderErrorTemplate(w, err.Error(), scriptpath)
  262. return
  263. }
  264. //Get the return valu from the script
  265. value, err := vm.Get("HTTP_RESP")
  266. if err != nil {
  267. sendTextResponse(w, "")
  268. return
  269. }
  270. valueString, err := value.ToString()
  271. //Get respond header type from the vm
  272. header, _ := vm.Get("HTTP_HEADER")
  273. headerString, _ := header.ToString()
  274. if headerString != "" {
  275. w.Header().Set("Content-Type", headerString)
  276. }
  277. w.Write([]byte(valueString))
  278. }
  279. /*
  280. Execute AGI script with given user information
  281. */
  282. func (g *Gateway) ExecuteAGIScriptAsUser(scriptFile string, targetUser *user.User) (string, error) {
  283. //Create a new vm for this request
  284. vm := otto.New()
  285. //Inject standard libs into the vm
  286. g.injectStandardLibs(vm, scriptFile, "")
  287. g.injectUserFunctions(vm, scriptFile, "", targetUser, nil, nil)
  288. //Try to read the script content
  289. scriptContent, err := ioutil.ReadFile(scriptFile)
  290. if err != nil {
  291. return "", err
  292. }
  293. _, err = vm.Run(scriptContent)
  294. if err != nil {
  295. return "", err
  296. }
  297. //Get the return value from the script
  298. value, err := vm.Get("HTTP_RESP")
  299. if err != nil {
  300. return "", err
  301. }
  302. valueString, err := value.ToString()
  303. return valueString, nil
  304. }