main.go 2.8 KB

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