From a227ee9e6cc39c11d7d4bf5fb16a639865ff32f9 Mon Sep 17 00:00:00 2001
From: starainrt
Date: Sun, 12 May 2024 19:52:35 +0800
Subject: [PATCH] add ip filter for httpreverseproxy
---
httpreverse/service.go | 7 ++++---
httpserver/cmd.go | 1 +
httpserver/server.go | 43 +++++++++++++++++++++++++++++++++++++++---
3 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/httpreverse/service.go b/httpreverse/service.go
index 1d58140..6cd395c 100644
--- a/httpreverse/service.go
+++ b/httpreverse/service.go
@@ -27,7 +27,6 @@ func (h *ReverseConfig) Run() error {
for key, proxy := range h.proxy {
h.httpmux.HandleFunc(key, func(writer http.ResponseWriter, request *http.Request) {
starlog.Infof("<%s> Req Path:%s ListenAddr:%s UA:%s\n", h.Name, request.URL.Path, request.RemoteAddr, request.Header.Get("User-Agent"))
-
if !h.BasicAuth(writer, request) {
h.SetResponseHeader(writer)
return
@@ -305,8 +304,10 @@ func (h *ReverseConfig) filter(w http.ResponseWriter, r *http.Request) bool {
}
func joinURLPath(a, b *url.URL, hpath string) (path, rawpath string) {
- b.Path = strings.TrimPrefix(b.Path, hpath)
- b.RawPath = strings.TrimPrefix(b.RawPath, hpath)
+ if hpath != "/" {
+ b.Path = strings.TrimPrefix(b.Path, hpath)
+ b.RawPath = strings.TrimPrefix(b.RawPath, hpath)
+ }
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
diff --git a/httpserver/cmd.go b/httpserver/cmd.go
index 7810fa9..53e6414 100644
--- a/httpserver/cmd.go
+++ b/httpserver/cmd.go
@@ -33,6 +33,7 @@ func init() {
Cmd.Flags().StringVar(&s.page401, "401", "", "自定义401页面地址")
Cmd.Flags().StringVar(&s.page403, "403", "", "自定义403页面地址")
Cmd.Flags().StringVar(&s.page404, "404", "", "自定义404页面地址")
+ Cmd.Flags().BoolVarP(&s.httpDebug, "debug", "D", false, "开启调试模式")
Cmd.Flags().Bool("daeapplied", false, "")
Cmd.Flags().MarkHidden("daeapplied")
}
diff --git a/httpserver/server.go b/httpserver/server.go
index d9684ca..b83c67e 100644
--- a/httpserver/server.go
+++ b/httpserver/server.go
@@ -44,6 +44,7 @@ type HttpServerCfg struct {
disableMIME bool
ctx context.Context
hooks []ServerHook
+ httpDebug bool
}
type ServerHook struct {
@@ -477,6 +478,37 @@ func (h *HttpServer) SetUpload(w http.ResponseWriter, r *http.Request, path stri
}
return false
}
+
+func (h *HttpServer) debugMode(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html")
+ w.WriteHeader(200)
+ html := `Debug Mode
%s`
+ resp := "Url
"
+ resp += "
" + r.Method + " " + r.URL.Path + "
" + resp += "query: " + r.URL.RawQuery + "
" + resp += "fragment: " + r.URL.Fragment + "
" + resp += "FullUrl: " + r.URL.String() + "
" + resp += "%s:%s
", k, v) + } + resp += "%s:%s
", key, v) + } + } + resp += "%s:%s
", c.Name, c.Value) + } + resp += "" + r.RemoteAddr + "
" + resp += "" + r.Proto + "
" + w.Write([]byte(fmt.Sprintf(html, resp))) +} + func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) { log := starlog.Std.NewFlag() log.SetShowFuncName(false) @@ -487,11 +519,16 @@ func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) { return } path := r.URL.Path + ua := r.Header.Get("User-Agent") + if h.httpDebug { + log.Infof("debug mode:%s %s From %s %s\n", r.Method, path, r.RemoteAddr, ua) + h.debugMode(w, r) + return + } if h.uploadFolder != "" && path == "/recv" && len(r.URL.Query()["upload"]) != 0 { h.uploadFile(w, r) return } - ua := r.Header.Get("User-Agent") fullpath := filepath.Clean(filepath.Join(h.envPath, path)) { @@ -530,9 +567,9 @@ func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) { log.Warningf("%s %s From %s %s %.2fs %v\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds(), err) return } - log.Infof("%s %s From %s %s %.2fs \n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds()) + log.Infof("%s %s From %s %s %.2fs\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds()) default: - log.Errorf("Invalid %s %s From %s %s %.2fs %v\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds()) + log.Errorf("Invalid %s %s From %s %s %.2fs\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds()) return } }