mirror of
https://github.com/yusing/godoxy.git
synced 2026-02-19 17:07:42 +01:00
Add a reason parameter throughout the ACL system to track and log why each IP was allowed or denied. This provides better visibility into ACL decisions by recording specific reasons such as "allowed by allow_local rule", "blocked by deny rule: [rule]", or "deny by default". Changes include: - Add reason field to checkCache and ipLog structs - Update LogACL interface and implementations to accept reason - Generate descriptive reasons for all ACL decision paths - Include reason in console log output
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package accesslog
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
maxmind "github.com/yusing/godoxy/internal/maxmind/types"
|
|
"github.com/yusing/goutils/task"
|
|
)
|
|
|
|
type MultiAccessLogger struct {
|
|
accessLoggers []AccessLogger
|
|
}
|
|
|
|
// NewMultiAccessLogger creates a new AccessLogger that writes to multiple writers.
|
|
//
|
|
// If there is only one writer, it will return a single AccessLogger.
|
|
// Otherwise, it will return a MultiAccessLogger that writes to all the writers.
|
|
func NewMultiAccessLogger(parent task.Parent, cfg AnyConfig, writers []File) AccessLogger {
|
|
if len(writers) == 1 {
|
|
if writers[0] == stdout {
|
|
return NewConsoleLogger(cfg.ToConfig())
|
|
}
|
|
return NewFileAccessLogger(parent, writers[0], cfg)
|
|
}
|
|
|
|
accessLoggers := make([]AccessLogger, len(writers))
|
|
for i, writer := range writers {
|
|
if writer == stdout {
|
|
accessLoggers[i] = NewConsoleLogger(cfg.ToConfig())
|
|
} else {
|
|
accessLoggers[i] = NewFileAccessLogger(parent, writer, cfg)
|
|
}
|
|
}
|
|
return &MultiAccessLogger{accessLoggers}
|
|
}
|
|
|
|
func (m *MultiAccessLogger) Config() *Config {
|
|
return m.accessLoggers[0].Config()
|
|
}
|
|
|
|
func (m *MultiAccessLogger) LogRequest(req *http.Request, res *http.Response) {
|
|
for _, accessLogger := range m.accessLoggers {
|
|
accessLogger.LogRequest(req, res)
|
|
}
|
|
}
|
|
|
|
func (m *MultiAccessLogger) LogError(req *http.Request, err error) {
|
|
for _, accessLogger := range m.accessLoggers {
|
|
accessLogger.LogError(req, err)
|
|
}
|
|
}
|
|
|
|
func (m *MultiAccessLogger) LogACL(info *maxmind.IPInfo, blocked bool, reason string) {
|
|
for _, accessLogger := range m.accessLoggers {
|
|
accessLogger.LogACL(info, blocked, reason)
|
|
}
|
|
}
|
|
|
|
func (m *MultiAccessLogger) Flush() {
|
|
for _, accessLogger := range m.accessLoggers {
|
|
accessLogger.Flush()
|
|
}
|
|
}
|
|
|
|
func (m *MultiAccessLogger) Close() error {
|
|
for _, accessLogger := range m.accessLoggers {
|
|
accessLogger.Close()
|
|
}
|
|
return nil
|
|
}
|