startup.flags.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. prout "imuslab.com/arozos/mod/prouter"
  6. "imuslab.com/arozos/mod/utils"
  7. )
  8. /*
  9. Startup Flags Manager
  10. This script is design to provide interface for editing the boot flags
  11. during the system is running
  12. */
  13. func StartupFlagsInit() {
  14. //Create a admin permission router for handling requests
  15. //Register a boot flag modifier
  16. registerSetting(settingModule{
  17. Name: "Runtime",
  18. Desc: "Change startup paramter in runtime",
  19. IconPath: "SystemAO/info/img/small_icon.png",
  20. Group: "Info",
  21. StartDir: "SystemAO/boot/bootflags.html",
  22. RequireAdmin: true,
  23. })
  24. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  25. AdminOnly: true,
  26. UserHandler: userHandler,
  27. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  28. utils.SendErrorResponse(w, "Permission Denied")
  29. },
  30. })
  31. adminRouter.HandleFunc("/system/bootflags", handleBootFlagsFunction)
  32. }
  33. func handleBootFlagsFunction(w http.ResponseWriter, r *http.Request) {
  34. type bootFlags struct {
  35. Hostname string
  36. MaxUploadSize int
  37. MaxFileUploadBuff int
  38. FileIOBuffer int
  39. DisableIPResolver bool
  40. EnableHomePage bool
  41. EnableDirListing bool
  42. }
  43. opr, _ := utils.PostPara(r, "opr")
  44. if opr == "" {
  45. //List the current boot flag, all units in MB
  46. js, _ := json.Marshal(bootFlags{
  47. *host_name,
  48. int(max_upload_size >> 20),
  49. *upload_buf,
  50. *file_opr_buff,
  51. *disable_ip_resolve_services,
  52. *allow_homepage,
  53. *enable_dir_listing,
  54. })
  55. utils.SendJSONResponse(w, string(js))
  56. } else if opr == "set" {
  57. //Set and update the boot flags
  58. newSettings, err := utils.PostPara(r, "value")
  59. if err != nil {
  60. utils.SendErrorResponse(w, "Invalid new seting value")
  61. return
  62. }
  63. //Try parse it
  64. newConfig := bootFlags{
  65. "My ArOZ",
  66. 8192,
  67. 25,
  68. 1024,
  69. false,
  70. false,
  71. true,
  72. }
  73. err = json.Unmarshal([]byte(newSettings), &newConfig)
  74. if err != nil {
  75. utils.SendErrorResponse(w, err.Error())
  76. return
  77. }
  78. //Update the current global flags
  79. systemWideLogger.PrintAndLog("System", "Updating boot flag to:"+newSettings, nil)
  80. *host_name = newConfig.Hostname
  81. max_upload_size = int64(newConfig.MaxUploadSize << 20)
  82. *upload_buf = newConfig.MaxFileUploadBuff
  83. *file_opr_buff = newConfig.FileIOBuffer
  84. *disable_ip_resolve_services = newConfig.DisableIPResolver
  85. *allow_homepage = newConfig.EnableHomePage
  86. *enable_dir_listing = newConfig.EnableDirListing
  87. utils.SendOK(w)
  88. } else {
  89. utils.SendErrorResponse(w, "Unknown operation")
  90. }
  91. }