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