main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "git.arozos.com/ArSamba/aroz"
  10. )
  11. var (
  12. handler *aroz.ArozHandler
  13. )
  14. func SetupCloseHandler() {
  15. c := make(chan os.Signal, 2)
  16. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  17. go func() {
  18. <-c
  19. log.Println("\r- Shutting down ArSamba module.")
  20. os.Exit(0)
  21. }()
  22. }
  23. func main() {
  24. //If you have other flags, please add them here
  25. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  26. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  27. Name: "ArSamba",
  28. Desc: "arozos Samba Setting Subservice",
  29. Group: "System Settings",
  30. IconPath: "arsamba/img/icon.png",
  31. Version: "1.0",
  32. StartDir: "arsamba/index.html",
  33. SupportFW: true,
  34. LaunchFWDir: "arsamba/index.html",
  35. InitFWSize: []int{350, 560},
  36. })
  37. //Register the standard web services urls
  38. fs := http.FileServer(http.Dir("./web"))
  39. http.HandleFunc("/create", handleNewUser)
  40. http.HandleFunc("/remove", handleUserRemove)
  41. http.HandleFunc("/getStatus", handleGetStatus)
  42. http.Handle("/", fs)
  43. SetupCloseHandler()
  44. log.Println("ArSamba subservice started. Listening on " + handler.Port)
  45. err := http.ListenAndServe(handler.Port, nil)
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. }
  50. func handleGetStatus(w http.ResponseWriter, r *http.Request) {
  51. username, err := mv(r, "username", false)
  52. if err != nil {
  53. sendErrorResponse(w, "Invalid username given")
  54. return
  55. }
  56. //Check if the user has already in samba user
  57. log.Println("Checking User Status", username)
  58. //Send the results
  59. js, _ := json.Marshal(true)
  60. sendJSONResponse(w, string(js))
  61. }
  62. func handleNewUser(w http.ResponseWriter, r *http.Request) {
  63. //Get the required information
  64. username, err := mv(r, "username", true)
  65. if err != nil {
  66. sendErrorResponse(w, "Invalid username given")
  67. return
  68. }
  69. //Match the session username
  70. proxyUser, _ := handler.GetUserInfoFromRequest(w, r)
  71. if username != proxyUser {
  72. sendErrorResponse(w, "User not logged in")
  73. return
  74. }
  75. password, err := mv(r, "password", true)
  76. if err != nil {
  77. sendErrorResponse(w, "Invalid password given")
  78. return
  79. }
  80. //Add the user to samba
  81. log.Println("Adding User", username, password)
  82. //Return ok
  83. sendOK(w)
  84. }
  85. func handleUserRemove(w http.ResponseWriter, r *http.Request) {
  86. //Get the required information
  87. username, err := mv(r, "username", true)
  88. if err != nil {
  89. sendErrorResponse(w, "Invalid username given")
  90. return
  91. }
  92. //Match the session username
  93. proxyUser, _ := handler.GetUserInfoFromRequest(w, r)
  94. if username != proxyUser {
  95. sendErrorResponse(w, "User not logged in")
  96. return
  97. }
  98. //OK! Remove user
  99. log.Println("Remove user", username)
  100. //Return OK
  101. sendOK(w)
  102. }