main.router.go 6.6 KB

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