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