package main import ( "encoding/json" "log" "net/http" "os" "os/signal" "syscall" "git.arozos.com/ArSamba/aroz" ) var ( handler *aroz.ArozHandler ) func SetupCloseHandler() { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c log.Println("\r- Shutting down ArSamba module.") os.Exit(0) }() } func main() { //If you have other flags, please add them here //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information handler = aroz.HandleFlagParse(aroz.ServiceInfo{ Name: "ArSamba", Desc: "arozos Samba Setting Subservice", Group: "System Settings", IconPath: "arsamba/img/icon.png", Version: "1.0", StartDir: "arsamba/index.html", SupportFW: true, LaunchFWDir: "arsamba/index.html", InitFWSize: []int{350, 560}, }) //Register the standard web services urls fs := http.FileServer(http.Dir("./web")) http.HandleFunc("/create", handleNewUser) http.HandleFunc("/remove", handleUserRemove) http.HandleFunc("/getStatus", handleGetStatus) http.Handle("/", fs) SetupCloseHandler() log.Println("ArSamba subservice started. Listening on " + handler.Port) err := http.ListenAndServe(handler.Port, nil) if err != nil { log.Fatal(err) } } func handleGetStatus(w http.ResponseWriter, r *http.Request) { username, err := mv(r, "username", false) if err != nil { sendErrorResponse(w, "Invalid username given") return } //Check if the user has already in samba user log.Println("Checking User Status", username) //Send the results js, _ := json.Marshal(true) sendJSONResponse(w, string(js)) } func handleNewUser(w http.ResponseWriter, r *http.Request) { //Get the required information username, err := mv(r, "username", true) if err != nil { sendErrorResponse(w, "Invalid username given") return } //Match the session username proxyUser, _ := handler.GetUserInfoFromRequest(w, r) if username != proxyUser { sendErrorResponse(w, "User not logged in") return } password, err := mv(r, "password", true) if err != nil { sendErrorResponse(w, "Invalid password given") return } //Add the user to samba log.Println("Adding User", username, password) //Return ok sendOK(w) } func handleUserRemove(w http.ResponseWriter, r *http.Request) { //Get the required information username, err := mv(r, "username", true) if err != nil { sendErrorResponse(w, "Invalid username given") return } //Match the session username proxyUser, _ := handler.GetUserInfoFromRequest(w, r) if username != proxyUser { sendErrorResponse(w, "User not logged in") return } //OK! Remove user log.Println("Remove user", username) //Return OK sendOK(w) }