Skip to content

Add option to log user agent #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Options struct {
Verbose bool
EnableUpload bool
EnableTCP bool
LogUA bool
RulesFile string
TCPWithTLS bool
Version bool
Expand All @@ -53,6 +54,7 @@ func ParseOptions() *Options {
}
flag.StringVar(&options.Folder, "path", currentPath, "Folder")
flag.BoolVar(&options.EnableUpload, "upload", false, "Enable upload via PUT")
flag.BoolVar(&options.LogUA, "log-ua", false, "Log User Agent")
flag.BoolVar(&options.HTTPS, "https", false, "HTTPS")
flag.StringVar(&options.TLSCertificate, "cert", "", "HTTPS Certificate")
flag.StringVar(&options.TLSKey, "key", "", "HTTPS Certificate Key")
Expand Down
1 change: 1 addition & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func New(options *Options) (*Runner, error) {
httpServer, err := httpserver.New(&httpserver.Options{
Folder: r.options.Folder,
EnableUpload: r.options.EnableUpload,
LogUA: r.options.LogUA,
ListenAddress: r.options.ListenAddress,
TLS: r.options.HTTPS,
Certificate: r.options.TLSCertificate,
Expand Down
2 changes: 2 additions & 0 deletions pkg/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type Options struct {
Folder string
EnableUpload bool
LogUA bool // enable logging user agent
ListenAddress string
TLS bool
Certificate string
Expand Down Expand Up @@ -46,6 +47,7 @@ func New(options *Options) (*HTTPServer, error) {
var h HTTPServer
EnableUpload = options.EnableUpload
EnableVerbose = options.Verbose
EnableLogUA = options.LogUA
folder, err := filepath.Abs(options.Folder)
if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion pkg/httpserver/loglayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var (
EnableUpload bool
EnableVerbose bool
EnableLogUA bool
)

func (t *HTTPServer) shouldDumpBody(bodysize int64) bool {
Expand All @@ -34,7 +35,11 @@ func (t *HTTPServer) loglayer(handler http.Handler) http.Handler {
lrw.Header().Write(headers) //nolint
gologger.Print().Msgf("\n[%s]\nRemote Address: %s\n%s\n%s %d %s\n%s\n%s\n", time.Now().Format("2006-01-02 15:04:05"), r.RemoteAddr, string(fullRequest), r.Proto, lrw.statusCode, http.StatusText(lrw.statusCode), headers.String(), string(lrw.Data))
} else {
gologger.Print().Msgf("[%s] %s \"%s %s %s\" %d %d", time.Now().Format("2006-01-02 15:04:05"), r.RemoteAddr, r.Method, r.URL, r.Proto, lrw.statusCode, lrw.Size)
if EnableLogUA {
gologger.Print().Msgf("[%s] %s \"%s %s %s\" %d %d - %s", time.Now().Format("2006-01-02 15:04:05"), r.RemoteAddr, r.Method, r.URL, r.Proto, lrw.statusCode, lrw.Size, r.UserAgent())
} else {
gologger.Print().Msgf("[%s] %s \"%s %s %s\" %d %d", time.Now().Format("2006-01-02 15:04:05"), r.RemoteAddr, r.Method, r.URL, r.Proto, lrw.statusCode, lrw.Size)
}
}
})
}
Expand Down