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