agi.go 8.4 KB

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