| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 | package mainimport (	"log"	"net/http"	"os"	module "imuslab.com/arozos/mod/modules"	prout "imuslab.com/arozos/mod/prouter")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)		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 {			sendErrorResponse(w, "User not logged in")			return		}		//Validate the user is admin		if userinfo.IsAdmin() {			//Get the installation file path			installerPath, err := mv(r, "path", true)			if err != nil {				sendErrorResponse(w, "Invalid installer path")				return			}			//Translate it to realpath			rpath, err := userinfo.VirtualPathToRealPath(installerPath)			if err != nil {				log.Println("*Module Installer* Failed to install module: ", err.Error())				sendErrorResponse(w, "Invalid installer path")				return			}			//Install it			moduleHandler.InstallViaZip(rpath, AGIGateway)		} else {			//Permission denied			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 requestfunc HandleModuleInstall(w http.ResponseWriter, r *http.Request) {	opr, _ := mv(r, "opr", true)	if opr == "gitinstall" {		//Get URL from request		url, _ := mv(r, "url", true)		if url == "" {			sendErrorResponse(w, "Invalid URL")			return		}		//Install the module using git		err := moduleHandler.InstallModuleViaGit(url, AGIGateway)		if err != nil {			sendErrorResponse(w, err.Error())			return		}		//Reply ok		sendOK(w)	} else if opr == "zipinstall" {	} else if opr == "remove" {		//Get the module name from list		module, _ := mv(r, "module", true)		if module == "" {			sendErrorResponse(w, "Invalid Module Name")			return		}		//Remove the module		err := moduleHandler.UninstallModule(module)		if err != nil {			sendErrorResponse(w, err.Error())			return		}		//Reply ok		sendOK(w)	} else {		//List all the modules		moduleHandler.HandleModuleInstallationListing(w, r)	}}
 |