123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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)
- }
|