main.router.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package main
  2. /*
  3. ArOZ Online System Main Request Router
  4. This is used to check authentication before actually serving file to the target client
  5. This function also handle the special page (login.system and user.system) delivery
  6. */
  7. import (
  8. "net/http"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. fs "imuslab.com/arozos/mod/filesystem"
  13. )
  14. func mrouter(h http.Handler) http.Handler {
  15. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. /*
  17. You can also check the path for url using r.URL.Path
  18. */
  19. if r.URL.Path == "/favicon.ico" || r.URL.Path == "/manifest.webmanifest" {
  20. //Serving favicon or manifest. Allow no auth access.
  21. h.ServeHTTP(w, r)
  22. } else if r.URL.Path == "/login.system" {
  23. //Login page. Require special treatment for template.
  24. //Get the redirection address from the request URL
  25. red, _ := mv(r, "redirect", false)
  26. //Append the redirection addr into the template
  27. imgsrc := "./web/" + iconSystem
  28. if !fileExists(imgsrc) {
  29. imgsrc = "./web/img/public/auth_icon.png"
  30. }
  31. imageBase64, _ := LoadImageAsBase64(imgsrc)
  32. parsedPage, err := template_load("web/login.system", map[string]interface{}{
  33. "redirection_addr": red,
  34. "usercount": strconv.Itoa(authAgent.GetUserCounts()),
  35. "service_logo": imageBase64,
  36. })
  37. if err != nil {
  38. panic("Error. Unable to parse login page. Is web directory data exists?")
  39. }
  40. w.Write([]byte(parsedPage))
  41. } else if r.URL.Path == "/reset.system" && authAgent.GetUserCounts() > 0 {
  42. //Password restart page. Allow access only when user number > 0
  43. system_resetpw_handlePasswordReset(w, r)
  44. } else if r.URL.Path == "/user.system" && authAgent.GetUserCounts() == 0 {
  45. //Serve user management page. This only allows serving of such page when the total usercount = 0 (aka System Initiation)
  46. h.ServeHTTP(w, r)
  47. } else if (len(r.URL.Path) > 11 && r.URL.Path[:11] == "/img/public") || (len(r.URL.Path) > 7 && r.URL.Path[:7] == "/script") {
  48. //Public image directory. Allow anyone to access resources inside this directory.
  49. if filepath.Ext("web"+fs.DecodeURI(r.RequestURI)) == ".js" {
  50. //Fixed serve js meme type invalid bug on Firefox
  51. w.Header().Add("Content-Type", "application/javascript; charset=UTF-8")
  52. }
  53. h.ServeHTTP(w, r)
  54. } else if len(r.URL.Path) >= len("/webdav") && r.URL.Path[:7] == "/webdav" {
  55. WebDavHandler.HandleRequest(w, r)
  56. } else if r.URL.Path == "/" && authAgent.CheckAuth(r) {
  57. //Use logged in and request the index. Serve the user's interface module
  58. w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
  59. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  60. if err != nil {
  61. //ERROR!! Server default
  62. h.ServeHTTP(w, r)
  63. } else {
  64. interfaceModule := userinfo.GetInterfaceModules()
  65. if len(interfaceModule) == 1 && interfaceModule[0] == "Desktop" {
  66. http.Redirect(w, r, "desktop.system", 307)
  67. } else if len(interfaceModule) == 1 {
  68. //User with default interface module not desktop
  69. modileInfo := moduleHandler.GetModuleInfoByID(interfaceModule[0])
  70. http.Redirect(w, r, modileInfo.StartDir, 307)
  71. } else if len(interfaceModule) > 1 {
  72. //Redirect to module selector
  73. http.Redirect(w, r, "SystemAO/boot/interface_selector.html", 307)
  74. } else if len(interfaceModule) == 0 {
  75. //Redirect to error page
  76. http.Redirect(w, r, "SystemAO/boot/no_interfaceing.html", 307)
  77. } else {
  78. //For unknown operations, send it to desktop
  79. http.Redirect(w, r, "desktop.system", 307)
  80. }
  81. }
  82. } else if ((len(r.URL.Path) >= 5 && r.URL.Path[:5] == "/www/") || r.URL.Path == "/www") && *allow_homepage == true {
  83. //Serve the custom homepage of the user defined. Hand over to the www router
  84. userWwwHandler.RouteRequest(w, r)
  85. } else if authAgent.CheckAuth(r) {
  86. //User logged in. Continue to serve the file the client want
  87. authAgent.UpdateSessionExpireTime(w, r)
  88. if build_version == "development" {
  89. //Do something if development build
  90. }
  91. if filepath.Ext("web"+fs.DecodeURI(r.RequestURI)) == ".js" {
  92. //Fixed serve js meme type invalid bug on Firefox
  93. w.Header().Add("Content-Type", "application/javascript; charset=UTF-8")
  94. }
  95. if *disable_subservices == false {
  96. //Enable subservice access
  97. //Check if this path is reverse proxy path. If yes, serve with proxyserver
  98. isRP, proxy, rewriteURL, subserviceObject := ssRouter.CheckIfReverseProxyPath(r)
  99. if isRP {
  100. //Check user permission on that module
  101. ssRouter.HandleRoutingRequest(w, r, proxy, subserviceObject, rewriteURL)
  102. return
  103. }
  104. }
  105. //Not subservice routine. Handle file server
  106. if *enable_dir_listing == false {
  107. if strings.HasSuffix(r.URL.Path, "/") {
  108. //User trying to access a directory. Send NOT FOUND.
  109. if fileExists("web" + r.URL.Path + "index.html") {
  110. //Index exists. Allow passthrough
  111. } else {
  112. errorHandleNotFound(w, r)
  113. return
  114. }
  115. }
  116. }
  117. h.ServeHTTP(w, r)
  118. } else {
  119. //User not logged in. Check if the path end with public/. If yes, allow public access
  120. if !fs.FileExists(filepath.Join("./web", r.URL.Path)) {
  121. //Requested file not exists on the server. Return not found
  122. errorHandleNotFound(w, r)
  123. } else if r.URL.Path[len(r.URL.Path)-1:] != "/" && filepath.Base(filepath.Dir(r.URL.Path)) == "public" {
  124. //This file path end with public/. Allow public access
  125. h.ServeHTTP(w, r)
  126. } else if *allow_homepage == true && len(r.URL.Path) >= 5 && r.URL.Path[:5] == "/www/" {
  127. //Handle public home serving if homepage mode is enabled
  128. h.ServeHTTP(w, r)
  129. } else {
  130. //Other paths
  131. //Rediect to login page
  132. w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
  133. http.Redirect(w, r, "/login.system?redirect="+r.URL.Path, 307)
  134. }
  135. }
  136. })
  137. }