main.router.go 6.5 KB

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