main.router.go 6.9 KB

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