123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package main
- import (
- "fmt"
- "net/http"
- "net/url"
- "os"
- "path/filepath"
- "strings"
- "github.com/gorilla/csrf"
- "imuslab.com/zoraxy/mod/sshprox"
- )
- func FSHandler(handler http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
- if development && strings.HasPrefix(r.URL.Path, "/web/") {
- u, _ := url.Parse(strings.TrimPrefix(r.URL.Path, "/web"))
- r.URL = u
- }
-
- if !development && r.URL.Path == "/" {
-
- http.Redirect(w, r, "/web/", http.StatusTemporaryRedirect)
- return
- }
-
- if strings.HasPrefix(r.URL.Path, ppf("/script/")) || strings.HasPrefix(r.URL.Path, ppf("/img/public/")) || r.URL.Path == ppf("/login.html") || r.URL.Path == ppf("/reset.html") || r.URL.Path == ppf("/favicon.png") {
- if isHTMLFilePath(r.URL.Path) {
- handleInjectHTML(w, r, r.URL.Path)
- return
- }
- handler.ServeHTTP(w, r)
- return
- }
-
- if !authAgent.CheckAuth(r) && requireAuth {
- http.Redirect(w, r, ppf("/login.html"), http.StatusTemporaryRedirect)
- return
- }
-
-
- if strings.HasPrefix(r.URL.Path, "/web.ssh/") {
- requestPath := r.URL.Path
- parts := strings.Split(requestPath, "/")
- if !strings.HasSuffix(requestPath, "/") && len(parts) == 3 {
- http.Redirect(w, r, requestPath+"/", http.StatusTemporaryRedirect)
- return
- }
- if len(parts) > 2 {
-
- instanceUUID := parts[2]
- fmt.Println(instanceUUID)
-
- r.URL, _ = sshprox.RewriteURL("/web.ssh/"+instanceUUID, r.RequestURI)
- webSshManager.HandleHttpByInstanceId(instanceUUID, w, r)
- } else {
- fmt.Println(parts)
- http.Error(w, "Invalid Usage", http.StatusInternalServerError)
- }
- return
- }
-
- if isHTMLFilePath(r.URL.Path) {
- handleInjectHTML(w, r, r.URL.Path)
- return
- }
- handler.ServeHTTP(w, r)
- })
- }
- func ppf(relativeFilepath string) string {
- if !development {
- return strings.ReplaceAll(filepath.Join("/web/", relativeFilepath), "\\", "/")
- }
- return relativeFilepath
- }
- func isHTMLFilePath(requestURI string) bool {
- return strings.HasSuffix(requestURI, ".html") || strings.HasSuffix(requestURI, "/")
- }
- func handleInjectHTML(w http.ResponseWriter, r *http.Request, relativeFilepath string) {
-
- var content []byte
- var err error
- if len(relativeFilepath) > 0 && relativeFilepath[len(relativeFilepath)-1:] == "/" {
- relativeFilepath = relativeFilepath + "index.html"
- }
- if development {
-
- targetFilePath := strings.ReplaceAll(filepath.Join("web/", relativeFilepath), "\\", "/")
- content, err = os.ReadFile(targetFilePath)
- if err != nil {
- http.Error(w, "Internal Server Error", http.StatusInternalServerError)
- return
- }
- } else {
-
- relativeFilepath = strings.TrimPrefix(relativeFilepath, "/")
- content, err = webres.ReadFile(relativeFilepath)
- if err != nil {
- SystemWideLogger.Println("load embedded web file failed: ", err)
- http.Error(w, "Internal Server Error", http.StatusInternalServerError)
- return
- }
- }
-
- htmlContent := string(content)
-
- templateStrings := map[string]string{
- ".csrfToken": csrf.Token(r),
- }
-
- for key, value := range templateStrings {
- placeholder := "{{" + key + "}}"
- htmlContent = strings.ReplaceAll(htmlContent, placeholder, value)
- }
-
- w.Header().Set("Content-Type", "text/html")
- w.Write([]byte(htmlContent))
- }
|