system.boot.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "net/http"
  4. "encoding/json"
  5. "runtime"
  6. "strings"
  7. "log"
  8. "io/ioutil"
  9. )
  10. /*
  11. System Boot Configuration Module
  12. This module handle the boot flags and settings of the booting paramters
  13. */
  14. type bootConfigParamters struct {
  15. Hostname string `json:"Hostname"`
  16. ListenPort int `json:"ListenPort"`
  17. MaxUpload int `json:"MaxUpload"`
  18. UploadBuffer int `json:"UploadBuffer"`
  19. FileIOBuf int `json:"FileIOBuf"`
  20. EnableUPnP bool `json:"EnableUPnP,omitempty"`
  21. AllowHardwareMan bool `json:"AllowHardwareMan"`
  22. AllowPackageAutoInstall bool `json:"AllowPackageAutoInstall"`
  23. DisableIPResolve bool `json:"DisableIPResolve"`
  24. IsWindows bool `json:"IsWindows,omitempty"`
  25. }
  26. func system_boot_init(){
  27. //Register Endpoints
  28. http.HandleFunc("/system/boot/generateBootConfig", system_boot_generateBootConfig)
  29. http.HandleFunc("/system/boot/getCurrentBootConfig", system_boot_getCurrentBootConfig)
  30. //Register Settings
  31. registerSetting(settingModule{
  32. Name: "Boot Config",
  33. Desc: "Setup Boot Modes and Flags",
  34. IconPath: "SystemAO/boot/img/boot.png",
  35. Group: "Advance",
  36. StartDir: "SystemAO/boot/bootflags.html",
  37. RequireAdmin: true,
  38. })
  39. }
  40. func system_boot_generateBootConfig(w http.ResponseWriter, r *http.Request) {
  41. isAdmin := system_permission_checkUserIsAdmin(w,r);
  42. if !isAdmin{
  43. sendErrorResponse(w, "Permission Denied")
  44. return
  45. }
  46. configs, err := mv(r, "config", true)
  47. if err != nil{
  48. sendErrorResponse(w, "Internal Server Error")
  49. return
  50. }
  51. //Decode config file
  52. configs = system_fs_specialURIDecode(configs)
  53. //Generate the boot script from the given paramters
  54. parsedConfig := new(bootConfigParamters);
  55. err = json.Unmarshal([]byte(configs), &parsedConfig)
  56. if err != nil{
  57. sendErrorResponse(w, err.Error())
  58. return
  59. }
  60. log.Println("Warning! Boot configuration updated!")
  61. commandSlice := []string{
  62. "-hostname",
  63. "\"" + parsedConfig.Hostname + "\"",
  64. "-port",
  65. IntToString(parsedConfig.ListenPort),
  66. "-max_upload_size",
  67. IntToString(parsedConfig.MaxUpload),
  68. "-upload_buf",
  69. IntToString(parsedConfig.UploadBuffer),
  70. "-iobuf",
  71. IntToString(parsedConfig.FileIOBuf),
  72. }
  73. if !parsedConfig.AllowHardwareMan{
  74. commandSlice = append(commandSlice, "-enable_hwman=false")
  75. }
  76. if !parsedConfig.AllowPackageAutoInstall{
  77. commandSlice = append(commandSlice, "-allow_pkg_install=false")
  78. }
  79. if parsedConfig.DisableIPResolve{
  80. commandSlice = append(commandSlice, "-disable_ip_resolver=true")
  81. }
  82. execpath := strings.Join(commandSlice, " ")
  83. //Replace all odd things like && and || and & etc
  84. if (strings.Contains(execpath, "&&") || strings.Contains(execpath, "||") || strings.Contains(execpath, "&") || strings.Contains(execpath, ">")){
  85. sendErrorResponse(w, "Configuration contains invalid characters")
  86. return
  87. }
  88. //Generate the corrisponding scrip file
  89. binaryPath := ""
  90. scriptName := "start.sh"
  91. if runtime.GOOS == "windows" {
  92. binaryPath = "aroz_online_windows_amd64.exe"
  93. scriptName = "start.bat"
  94. } else if runtime.GOOS == "linux" {
  95. if runtime.GOARCH == "arm" {
  96. binaryPath = "aroz_online_linux_arm"
  97. }else if runtime.GOARCH == "arm64" {
  98. binaryPath = "aroz_online_linux_arm64"
  99. }else if runtime.GOARCH == "386" {
  100. binaryPath = "aroz_online_windows_386"
  101. }else if runtime.GOARCH == "amd64" {
  102. binaryPath = "aroz_online_linux_amd64"
  103. }
  104. }
  105. //Build the final start script
  106. execpath = binaryPath + " " + execpath
  107. if runtime.GOOS == "windows" {
  108. err = ioutil.WriteFile(scriptName, []byte(execpath), 0755)
  109. if err != nil{
  110. sendErrorResponse(w, err.Error())
  111. return
  112. }
  113. }else{
  114. //Append other services to the script
  115. scriptContent := "#/bin/bash\nsudo " + execpath
  116. err = ioutil.WriteFile(scriptName, []byte(scriptContent), 0755)
  117. if err != nil{
  118. sendErrorResponse(w, err.Error())
  119. return
  120. }
  121. }
  122. sendOK(w)
  123. }
  124. //Get the current booting flags (Only those for basic users)
  125. func system_boot_getCurrentBootConfig(w http.ResponseWriter, r *http.Request){
  126. isAdmin := system_permission_checkUserIsAdmin(w,r);
  127. if !isAdmin{
  128. sendErrorResponse(w, "Permission Denied")
  129. return
  130. }
  131. isWindows := false;
  132. if runtime.GOOS == "windows" {
  133. isWindows = true
  134. }
  135. //Create a struct for the current booting options
  136. jsonString, _ := json.Marshal(bootConfigParamters{
  137. ListenPort: *listen_port,
  138. Hostname: *host_name,
  139. EnableUPnP: *allow_upnp,
  140. MaxUpload: *max_upload,
  141. UploadBuffer: *upload_buf,
  142. FileIOBuf: *file_opr_buff,
  143. AllowHardwareMan: *allow_hardware_management,
  144. AllowPackageAutoInstall: *allow_package_autoInstall,
  145. DisableIPResolve: *disable_ip_resolve_services,
  146. IsWindows: isWindows,
  147. })
  148. sendJSONResponse(w, string(jsonString))
  149. }