module.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "imuslab.com/arozos/mod/common"
  7. module "imuslab.com/arozos/mod/modules"
  8. prout "imuslab.com/arozos/mod/prouter"
  9. )
  10. var (
  11. moduleHandler *module.ModuleHandler
  12. )
  13. func ModuleServiceInit() {
  14. //Create a new module handler
  15. moduleHandler = module.NewModuleHandler(userHandler, *tmp_directory)
  16. //Register FTP Endpoints
  17. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  18. AdminOnly: true,
  19. UserHandler: userHandler,
  20. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  21. errorHandlePermissionDenied(w, r)
  22. },
  23. })
  24. //Pass through the endpoint to authAgent
  25. http.HandleFunc("/system/modules/list", func(w http.ResponseWriter, r *http.Request) {
  26. authAgent.HandleCheckAuth(w, r, moduleHandler.ListLoadedModules)
  27. })
  28. http.HandleFunc("/system/modules/getDefault", func(w http.ResponseWriter, r *http.Request) {
  29. authAgent.HandleCheckAuth(w, r, moduleHandler.HandleDefaultLauncher)
  30. })
  31. http.HandleFunc("/system/modules/getLaunchPara", func(w http.ResponseWriter, r *http.Request) {
  32. authAgent.HandleCheckAuth(w, r, moduleHandler.GetLaunchParameter)
  33. })
  34. adminRouter.HandleFunc("/system/modules/reload", func(w http.ResponseWriter, r *http.Request) {
  35. moduleHandler.ReloadAllModules(AGIGateway)
  36. common.SendOK(w)
  37. })
  38. //Handle module installer. Require admin
  39. http.HandleFunc("/system/modules/installViaZip", func(w http.ResponseWriter, r *http.Request) {
  40. //Check if the user is admin
  41. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  42. if err != nil {
  43. common.SendErrorResponse(w, "User not logged in")
  44. return
  45. }
  46. //Validate the user is admin
  47. if userinfo.IsAdmin() {
  48. //Get the installation file path
  49. installerPath, err := common.Mv(r, "path", true)
  50. if err != nil {
  51. common.SendErrorResponse(w, "Invalid installer path")
  52. return
  53. }
  54. //Translate it to realpath
  55. rpath, err := userinfo.VirtualPathToRealPath(installerPath)
  56. if err != nil {
  57. log.Println("*Module Installer* Failed to install module: ", err.Error())
  58. common.SendErrorResponse(w, "Invalid installer path")
  59. return
  60. }
  61. //Install it
  62. moduleHandler.InstallViaZip(rpath, AGIGateway)
  63. } else {
  64. //Permission denied
  65. common.SendErrorResponse(w, "Permission Denied")
  66. }
  67. })
  68. //Register setting interface for module configuration
  69. registerSetting(settingModule{
  70. Name: "Module List",
  71. Desc: "A list of module currently loaded in the system",
  72. IconPath: "SystemAO/modules/img/small_icon.png",
  73. Group: "Module",
  74. StartDir: "SystemAO/modules/moduleList.html",
  75. })
  76. registerSetting(settingModule{
  77. Name: "Default Module",
  78. Desc: "Default module use to open a file",
  79. IconPath: "SystemAO/modules/img/small_icon.png",
  80. Group: "Module",
  81. StartDir: "SystemAO/modules/defaultOpener.html",
  82. })
  83. if !*disable_subservices {
  84. registerSetting(settingModule{
  85. Name: "Subservices",
  86. Desc: "Launch and kill subservices",
  87. IconPath: "SystemAO/modules/img/small_icon.png",
  88. Group: "Module",
  89. StartDir: "SystemAO/modules/subservices.html",
  90. RequireAdmin: true,
  91. })
  92. }
  93. err := sysdb.NewTable("module")
  94. if err != nil {
  95. log.Fatal(err)
  96. os.Exit(1)
  97. }
  98. }
  99. /*
  100. Handle endpoint registry for Module installer
  101. */
  102. func ModuleInstallerInit() {
  103. //Register module installation setting
  104. registerSetting(settingModule{
  105. Name: "Add & Remove Module",
  106. Desc: "Install & Remove Module to the system",
  107. IconPath: "SystemAO/modules/img/small_icon.png",
  108. Group: "Module",
  109. StartDir: "SystemAO/modules/addAndRemove.html",
  110. RequireAdmin: true,
  111. })
  112. //Create new permission router
  113. router := prout.NewModuleRouter(prout.RouterOption{
  114. ModuleName: "System Setting",
  115. UserHandler: userHandler,
  116. AdminOnly: true,
  117. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  118. errorHandlePermissionDenied(w, r)
  119. },
  120. })
  121. router.HandleFunc("/system/module/install", HandleModuleInstall)
  122. }
  123. //Handle module installation request
  124. func HandleModuleInstall(w http.ResponseWriter, r *http.Request) {
  125. opr, _ := common.Mv(r, "opr", true)
  126. if opr == "gitinstall" {
  127. //Get URL from request
  128. url, _ := common.Mv(r, "url", true)
  129. if url == "" {
  130. common.SendErrorResponse(w, "Invalid URL")
  131. return
  132. }
  133. //Install the module using git
  134. err := moduleHandler.InstallModuleViaGit(url, AGIGateway)
  135. if err != nil {
  136. common.SendErrorResponse(w, err.Error())
  137. return
  138. }
  139. //Reply ok
  140. common.SendOK(w)
  141. } else if opr == "zipinstall" {
  142. } else if opr == "remove" {
  143. //Get the module name from list
  144. module, _ := common.Mv(r, "module", true)
  145. if module == "" {
  146. common.SendErrorResponse(w, "Invalid Module Name")
  147. return
  148. }
  149. //Remove the module
  150. err := moduleHandler.UninstallModule(module)
  151. if err != nil {
  152. common.SendErrorResponse(w, err.Error())
  153. return
  154. }
  155. //Reply ok
  156. common.SendOK(w)
  157. } else {
  158. //List all the modules
  159. moduleHandler.HandleModuleInstallationListing(w, r)
  160. }
  161. }