main.router.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "imuslab.com/arozos/mod/network/gzipmiddleware"
  14. "imuslab.com/arozos/mod/utils"
  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, _ := utils.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, _ := utils.LoadImageAsBase64(imgsrc)
  34. parsedPage, err := utils.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. WebDAVManager.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 len(r.URL.Path) >= len("/api/remote") && r.URL.Path[:11] == "/api/remote" {
  64. AGIGateway.ExtAPIHandler(w, r)
  65. } else if r.URL.Path == "/" && authAgent.CheckAuth(r) {
  66. //Use logged in and request the index. Serve the user's interface module
  67. w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
  68. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  69. if err != nil {
  70. //ERROR!! Server default
  71. h.ServeHTTP(w, r)
  72. } else {
  73. interfaceModule := userinfo.GetInterfaceModules()
  74. if len(interfaceModule) == 1 && interfaceModule[0] == "Desktop" {
  75. http.Redirect(w, r, "./desktop.system", http.StatusTemporaryRedirect)
  76. } else if len(interfaceModule) == 1 {
  77. //User with default interface module not desktop
  78. modileInfo := moduleHandler.GetModuleInfoByID(interfaceModule[0])
  79. if modileInfo == nil {
  80. //The module is not found or not enabled
  81. http.Redirect(w, r, "./SystemAO/boot/interface_disabled.html", http.StatusTemporaryRedirect)
  82. return
  83. }
  84. http.Redirect(w, r, modileInfo.StartDir, http.StatusTemporaryRedirect)
  85. } else if len(interfaceModule) > 1 {
  86. //Redirect to module selector
  87. http.Redirect(w, r, "./SystemAO/boot/interface_selector.html", http.StatusTemporaryRedirect)
  88. } else if len(interfaceModule) == 0 {
  89. //Redirect to error page
  90. http.Redirect(w, r, "./SystemAO/boot/no_interfaceing.html", http.StatusTemporaryRedirect)
  91. } else {
  92. //For unknown operations, send it to desktop
  93. http.Redirect(w, r, "./desktop.system", http.StatusTemporaryRedirect)
  94. }
  95. }
  96. } else if ((len(r.URL.Path) >= 5 && r.URL.Path[:5] == "/www/") || r.URL.Path == "/www") && *allow_homepage == true {
  97. //Serve the custom homepage of the user defined. Hand over to the www router
  98. userWwwHandler.RouteRequest(w, r)
  99. } else if authAgent.CheckAuth(r) {
  100. //User logged in. Continue to serve the file the client want
  101. authAgent.UpdateSessionExpireTime(w, r)
  102. if build_version == "development" {
  103. //Do something if development build
  104. }
  105. if filepath.Ext("web"+fs.DecodeURI(r.RequestURI)) == ".js" {
  106. //Fixed serve js meme type invalid bug on Firefox
  107. w.Header().Add("Content-Type", "application/javascript; charset=UTF-8")
  108. }
  109. if !*disable_subservices {
  110. //Enable subservice access
  111. //Check if this path is reverse proxy path. If yes, serve with proxyserver
  112. isRP, proxy, rewriteURL, subserviceObject := ssRouter.CheckIfReverseProxyPath(r)
  113. if isRP {
  114. //Check user permission on that module
  115. ssRouter.HandleRoutingRequest(w, r, proxy, subserviceObject, rewriteURL)
  116. return
  117. }
  118. }
  119. //Not subservice routine. Handle file server
  120. if !*enable_dir_listing {
  121. if strings.HasSuffix(r.URL.Path, "/") {
  122. //User trying to access a directory. Send NOT FOUND.
  123. if fs.FileExists("web" + r.URL.Path + "index.html") {
  124. //Index exists. Allow passthrough
  125. } else {
  126. errorHandleNotFound(w, r)
  127. return
  128. }
  129. }
  130. }
  131. if !fs.FileExists("web" + r.URL.Path) {
  132. //File not found
  133. errorHandleNotFound(w, r)
  134. return
  135. }
  136. routerStaticContentServer(h, w, r)
  137. } else {
  138. //User not logged in. Check if the path end with public/. If yes, allow public access
  139. if !fs.FileExists(filepath.Join("./web", r.URL.Path)) {
  140. //Requested file not exists on the server. Return not found
  141. errorHandleNotFound(w, r)
  142. } else if r.URL.Path[len(r.URL.Path)-1:] != "/" && filepath.Base(filepath.Dir(r.URL.Path)) == "public" {
  143. //This file path end with public/. Allow public access
  144. routerStaticContentServer(h, w, r)
  145. } else if *allow_homepage && len(r.URL.Path) >= 5 && r.URL.Path[:5] == "/www/" {
  146. //Handle public home serving if homepage mode is enabled
  147. routerStaticContentServer(h, w, r)
  148. } else {
  149. //Other paths
  150. //Rediect to login page
  151. w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
  152. http.Redirect(w, r, utils.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect="+r.URL.String(), 307)
  153. }
  154. }
  155. })
  156. }
  157. func routerStaticContentServer(h http.Handler, w http.ResponseWriter, r *http.Request) {
  158. if *enable_gzip {
  159. gzipmiddleware.Compress(h).ServeHTTP(w, r)
  160. } else {
  161. h.ServeHTTP(w, r)
  162. }
  163. }