Compare commits

...

2 Commits

Author SHA1 Message Date
274bef13d8 feat: match proxy's from on whole targeted url
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
2024-03-29 09:21:01 +01:00
f548c8c8e7 feat: add host to access log fields
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
2024-03-28 19:47:32 +01:00
3 changed files with 32 additions and 2 deletions

View File

@ -16,6 +16,7 @@ type LogFormatter struct{}
func (*LogFormatter) NewLogEntry(r *http.Request) middleware.LogEntry { func (*LogFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
return &LogEntry{ return &LogEntry{
method: r.Method, method: r.Method,
host: r.Host,
path: r.URL.Path, path: r.URL.Path,
ctx: r.Context(), ctx: r.Context(),
} }
@ -29,6 +30,7 @@ var _ middleware.LogFormatter = &LogFormatter{}
type LogEntry struct { type LogEntry struct {
method string method string
host string
path string path string
ctx context.Context ctx context.Context
} }
@ -41,6 +43,7 @@ func (e *LogEntry) Panic(v interface{}, stack []byte) {
// Write implements middleware.LogEntry // Write implements middleware.LogEntry
func (e *LogEntry) Write(status int, bytes int, header http.Header, elapsed time.Duration, extra interface{}) { func (e *LogEntry) Write(status int, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
logger.Info(e.ctx, fmt.Sprintf("%s %s - %d", e.method, e.path, status), logger.Info(e.ctx, fmt.Sprintf("%s %s - %d", e.method, e.path, status),
logger.F("host", e.host),
logger.F("status", status), logger.F("status", status),
logger.F("bytes", bytes), logger.F("bytes", bytes),
logger.F("elapsed", elapsed), logger.F("elapsed", elapsed),

View File

@ -3,7 +3,6 @@ package director
import ( import (
"context" "context"
"net/http" "net/http"
"net/url"
"sort" "sort"
"forge.cadoles.com/Cadoles/go-proxy" "forge.cadoles.com/Cadoles/go-proxy"
@ -28,12 +27,23 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
return r, errors.WithStack(err) return r, errors.WithStack(err)
} }
url := getRequestURL(r)
ctx = logger.With(r.Context(), logger.F("url", url.String()))
var match *store.Proxy var match *store.Proxy
MAIN: MAIN:
for _, p := range proxies { for _, p := range proxies {
for _, from := range p.From { for _, from := range p.From {
if matches := wildcard.Match(r.Host, from); !matches { logger.Debug(
ctx, "matching request with proxy's from",
logger.F("from", from),
)
if matches := wildcard.Match(url.String(), from); !matches {
logger.Debug(
ctx, "proxy's from matched",
logger.F("from", from),
)
continue continue
} }

View File

@ -2,6 +2,7 @@ package director
import ( import (
"net/http" "net/http"
"net/url"
"forge.cadoles.com/Cadoles/go-proxy" "forge.cadoles.com/Cadoles/go-proxy"
"forge.cadoles.com/Cadoles/go-proxy/util" "forge.cadoles.com/Cadoles/go-proxy/util"
@ -16,3 +17,19 @@ func createMiddlewareChain(handler http.Handler, middlewares []proxy.Middleware)
return handler return handler
} }
func getRequestURL(r *http.Request) *url.URL {
scheme := "http"
if r.URL.Scheme != "" {
scheme = r.URL.Scheme
}
url := url.URL{
Host: r.Host,
Scheme: scheme,
Path: r.URL.Path,
RawQuery: r.URL.RawQuery,
}
return &url
}