From d546f2340d4d8fa0508f90c2cfb46cad46e8dd64 Mon Sep 17 00:00:00 2001 From: mountain Date: Wed, 5 Jun 2024 13:41:43 +0900 Subject: [PATCH] =?UTF-8?q?config=20handler=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/http_handler.go | 56 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/server/http_handler.go b/server/http_handler.go index 142a8a0..bc68394 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -1,10 +1,13 @@ package server import ( + "crypto/md5" + "encoding/hex" "encoding/json" "fmt" "io" "net/http" + "net/url" "os" "path" "reflect" @@ -56,15 +59,62 @@ func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string return err } - logger.Printf("houstonHandler registed. deployPath : %s, downloadPath : %s", h.deployPath, h.downloadPath) - if len(prefix) > 0 { prefix = "/" + prefix } serveMux.Handle(prefix, h) fsx := http.FileServer(http.Dir(h.deployPath)) - serveMux.Handle(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), http.StripPrefix(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), fsx)) + deployPrefix := fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys) + logger.Printf("houstonHandler registed. deployPath : %s -> %s", fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), h.deployPath) + serveMux.HandleFunc(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), func(w http.ResponseWriter, r *http.Request) { + p := strings.TrimPrefix(r.URL.Path, deployPrefix) + rp := strings.TrimPrefix(r.URL.RawPath, deployPrefix) + + h := md5.New() + src := strings.TrimLeft(r.URL.Path, fmt.Sprintf("/%s/", prefix)) + + h.Write([]byte(src)) + at := hex.EncodeToString(h.Sum(nil)) + + if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) && at == r.Header.Get("As-X-UrlHash") { + r2 := new(http.Request) + *r2 = *r + r2.URL = new(url.URL) + *r2.URL = *r.URL + r2.URL.Path = p + r2.URL.RawPath = rp + fsx.ServeHTTP(w, r2) + } else { + http.NotFound(w, r) + } + }) + + // config는 접근하기 편하게 단축 경로 제공 + serveMux.HandleFunc("/config/", func(w http.ResponseWriter, r *http.Request) { + logger.Println("config url.path :", r.URL.Path) + h := md5.New() + h.Write([]byte(r.URL.Path)) + at := hex.EncodeToString(h.Sum(nil)) + hash := r.Header.Get("As-X-UrlHash") + logger.Println("config at = hash :", at, hash) + if at == hash { + urlpath := strings.TrimPrefix(r.URL.Path, "/config/") + dir := path.Dir(urlpath) + file := path.Base(urlpath) + dest := fmt.Sprintf("%s/config/%s", dir, file) + logger.Println("config dest :", dest) + r2 := new(http.Request) + *r2 = *r + r2.URL = new(url.URL) + *r2.URL = *r.URL + r2.URL.Path = dest + r2.URL.RawPath = dest + fsx.ServeHTTP(w, r2) + } else { + http.NotFound(w, r) + } + }) ufsx := http.FileServer(http.Dir(h.downloadPath)) serveMux.Handle(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_downloads), http.StripPrefix(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_downloads), ufsx))