add ip filter for httpreverseproxy

master
兔子 6 months ago
parent 9fc353211f
commit a227ee9e6c

@ -27,7 +27,6 @@ func (h *ReverseConfig) Run() error {
for key, proxy := range h.proxy { for key, proxy := range h.proxy {
h.httpmux.HandleFunc(key, func(writer http.ResponseWriter, request *http.Request) { 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")) 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) { if !h.BasicAuth(writer, request) {
h.SetResponseHeader(writer) h.SetResponseHeader(writer)
return 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) { func joinURLPath(a, b *url.URL, hpath string) (path, rawpath string) {
if hpath != "/" {
b.Path = strings.TrimPrefix(b.Path, hpath) b.Path = strings.TrimPrefix(b.Path, hpath)
b.RawPath = strings.TrimPrefix(b.RawPath, hpath) b.RawPath = strings.TrimPrefix(b.RawPath, hpath)
}
if a.RawPath == "" && b.RawPath == "" { if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), "" return singleJoiningSlash(a.Path, b.Path), ""
} }

@ -33,6 +33,7 @@ func init() {
Cmd.Flags().StringVar(&s.page401, "401", "", "自定义401页面地址") Cmd.Flags().StringVar(&s.page401, "401", "", "自定义401页面地址")
Cmd.Flags().StringVar(&s.page403, "403", "", "自定义403页面地址") Cmd.Flags().StringVar(&s.page403, "403", "", "自定义403页面地址")
Cmd.Flags().StringVar(&s.page404, "404", "", "自定义404页面地址") Cmd.Flags().StringVar(&s.page404, "404", "", "自定义404页面地址")
Cmd.Flags().BoolVarP(&s.httpDebug, "debug", "D", false, "开启调试模式")
Cmd.Flags().Bool("daeapplied", false, "") Cmd.Flags().Bool("daeapplied", false, "")
Cmd.Flags().MarkHidden("daeapplied") Cmd.Flags().MarkHidden("daeapplied")
} }

@ -44,6 +44,7 @@ type HttpServerCfg struct {
disableMIME bool disableMIME bool
ctx context.Context ctx context.Context
hooks []ServerHook hooks []ServerHook
httpDebug bool
} }
type ServerHook struct { type ServerHook struct {
@ -477,6 +478,37 @@ func (h *HttpServer) SetUpload(w http.ResponseWriter, r *http.Request, path stri
} }
return false return false
} }
func (h *HttpServer) debugMode(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
html := `<html><head><meta charset="utf-8"><title>B612 Http Server</title></head><body><h1 "style="text-align: center;">Debug Mode</h1><hr >%s</body></html>`
resp := "<h2>Url</h2>"
resp += "<p>" + r.Method + " " + r.URL.Path + "</p>"
resp += "<p> query " + r.URL.RawQuery + "</p>"
resp += "<p> fragment " + r.URL.Fragment + "</p>"
resp += "<p> FullUrl " + r.URL.String() + "</p>"
resp += "<h2>Query</h2>"
for k, v := range r.URL.Query() {
resp += fmt.Sprintf("<p>%s:%s</p>", k, v)
}
resp += "<h2>Header</h2>"
for key, val := range r.Header {
for _, v := range val {
resp += fmt.Sprintf("<p>%s:%s</p>", key, v)
}
}
resp += "<h2>Cookie</h2>"
for _, c := range r.Cookies() {
resp += fmt.Sprintf("<p>%s:%s</p>", c.Name, c.Value)
}
resp += "<h2>RemoteAddr</h2>"
resp += "<p>" + r.RemoteAddr + "</p>"
resp += "<h2>Proto</h2>"
resp += "<p>" + r.Proto + "</p>"
w.Write([]byte(fmt.Sprintf(html, resp)))
}
func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) { func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
log := starlog.Std.NewFlag() log := starlog.Std.NewFlag()
log.SetShowFuncName(false) log.SetShowFuncName(false)
@ -487,11 +519,16 @@ func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
return return
} }
path := r.URL.Path 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 { if h.uploadFolder != "" && path == "/recv" && len(r.URL.Query()["upload"]) != 0 {
h.uploadFile(w, r) h.uploadFile(w, r)
return return
} }
ua := r.Header.Get("User-Agent")
fullpath := filepath.Clean(filepath.Join(h.envPath, path)) fullpath := filepath.Clean(filepath.Join(h.envPath, path))
{ {
@ -532,7 +569,7 @@ func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
} }
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: 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 return
} }
} }

Loading…
Cancel
Save