From 36069cbe6d2d9adcc2ff96a02a4911f8c7e6e29b Mon Sep 17 00:00:00 2001 From: yusing Date: Wed, 18 Dec 2024 11:44:38 +0800 Subject: [PATCH] add host filter --- internal/net/http/accesslog/config.go | 1 + internal/net/http/accesslog/filter.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/internal/net/http/accesslog/config.go b/internal/net/http/accesslog/config.go index 2760803..69115c7 100644 --- a/internal/net/http/accesslog/config.go +++ b/internal/net/http/accesslog/config.go @@ -7,6 +7,7 @@ type ( Filters struct { StatusCodes LogFilter[*StatusCodeRange] `json:"status_codes"` Method LogFilter[HTTPMethod] `json:"method"` + Host LogFilter[Host] `json:"host"` Headers LogFilter[*HTTPHeader] `json:"headers"` // header exists or header == value CIDR LogFilter[*CIDR] `json:"cidr"` } diff --git a/internal/net/http/accesslog/filter.go b/internal/net/http/accesslog/filter.go index 00fc1b7..cc1f146 100644 --- a/internal/net/http/accesslog/filter.go +++ b/internal/net/http/accesslog/filter.go @@ -22,6 +22,7 @@ type ( HTTPHeader struct { Key, Value string } + Host string CIDR struct{ types.CIDR } ) @@ -79,6 +80,10 @@ func (k *HTTPHeader) Fulfill(req *http.Request, res *http.Response) bool { return false } +func (h Host) Fulfill(req *http.Request, res *http.Response) bool { + return req.Host == string(h) +} + func (cidr CIDR) Fulfill(req *http.Request, res *http.Response) bool { ip, _, err := net.SplitHostPort(req.RemoteAddr) if err != nil {