更新 proxy/handler.go
This commit is contained in:
@@ -42,18 +42,15 @@ func NewHandler(
|
|||||||
|
|
||||||
func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
path := strings.TrimPrefix(r.URL.Path, "/p/")
|
path := strings.TrimPrefix(r.URL.Path, "/p/")
|
||||||
parts := strings.SplitN(path, "/", 2)
|
|
||||||
|
|
||||||
if len(parts) == 0 || parts[0] == "" {
|
slashIdx := strings.Index(path, "/")
|
||||||
http.Error(w, "Invalid token", http.StatusBadRequest)
|
if slashIdx == -1 {
|
||||||
|
http.Error(w, "Invalid proxy path", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := parts[0]
|
token := path[:slashIdx]
|
||||||
subPath := ""
|
targetPath := path[slashIdx+1:]
|
||||||
if len(parts) > 1 {
|
|
||||||
subPath = "/" + parts[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
session := h.sessionManager.Get(token)
|
session := h.sessionManager.Get(token)
|
||||||
if session == nil {
|
if session == nil {
|
||||||
@@ -61,9 +58,24 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
targetURL := session.TargetURL + subPath
|
var targetURL string
|
||||||
|
|
||||||
|
if strings.HasPrefix(targetPath, "http://") || strings.HasPrefix(targetPath, "https://") {
|
||||||
|
targetURL = targetPath
|
||||||
|
} else {
|
||||||
|
baseURL := strings.TrimSuffix(session.TargetURL, "/")
|
||||||
|
if !strings.HasPrefix(targetPath, "/") {
|
||||||
|
targetPath = "/" + targetPath
|
||||||
|
}
|
||||||
|
targetURL = baseURL + targetPath
|
||||||
|
}
|
||||||
|
|
||||||
if r.URL.RawQuery != "" {
|
if r.URL.RawQuery != "" {
|
||||||
targetURL += "?" + r.URL.RawQuery
|
if strings.Contains(targetURL, "?") {
|
||||||
|
targetURL += "&" + r.URL.RawQuery
|
||||||
|
} else {
|
||||||
|
targetURL += "?" + r.URL.RawQuery
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.validator.ValidateURL(targetURL); err != nil {
|
if err := h.validator.ValidateURL(targetURL); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user