Files
whatismyip/internal/httputils/http.go
T
dcarrillo 49ffcadb0a Fix data races, nil-record panics, scanner SSRF, log date layout, shutdown timeouts, and enable stricter linters (#54)
High (behavioral bugs):
- service.Geo: add RLock on lookups, handle Reload error, keep old DBs on failure
- models.GeoDB.Reload: open new DBs before closing old ones (no reader gap)
- router: guard nil geo records (404 instead of 500) in geo.go, generic.go, dns.go
- router/port_scanner: reject undetermined client IPs (nil->localhost dial)
- internal/httputils: fix access log date layout (02/Jan/2006, not literal Nov)
- server/quic: log.Printf instead of log.Fatal in request handler

Medium:
- server/*: 10s shutdown timeout; SIGTERM drains servers before closing geo DBs
- httputils: Clone() request headers instead of mutating the live map; strings.Builder
- setting/app: handle all os.Stat errors; fix "truster" typos; use errors.New;
  remove dead Token field
- cmd: errors.Is for sentinel checks; os.Stderr; log.Fatal instead of panic;
  pass http.Handler by value (not pointer-to-interface)
- resolver: validate config at startup (pre-parse RRs, validate IPs); check WriteMsg
  errors; Setup returns error

Low:
- .golangci.yaml: enable errcheck, govet, errorlint; fix all resulting findings
- server/tls: explicit TLSConfig{MinVersion: tls.VersionTLS12}
- router/dns: label-exact vhost suffix check, strip port; template.Must
- models: errors.Join instead of %s on []error
- service/geo_test: race-regression test (concurrent lookups during Reload)
- README: Go >= 1.25; fix latent test bug (store.Add->Set in dns_test.go)
2026-07-19 12:09:52 +02:00

76 lines
1.7 KiB
Go

package httputils
import (
"fmt"
"net/http"
"net/textproto"
"sort"
"strings"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/gin-gonic/gin"
)
// HeadersToSortedString sorts and dumps http.Header to a string separated by \n
func HeadersToSortedString(headers http.Header) string {
keys := make([]string, 0, len(headers))
for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
var output strings.Builder
for _, k := range keys {
for _, h := range headers[k] {
output.WriteString(k + ": " + h + "\n")
}
}
return output.String()
}
// GetHeadersWithoutTrustedHeaders returns a copy of the request headers with the trusted headers removed
func GetHeadersWithoutTrustedHeaders(ctx *gin.Context) http.Header {
h := ctx.Request.Header.Clone()
for _, k := range []string{setting.App.TrustedHeader, setting.App.TrustedPortHeader} {
delete(h, textproto.CanonicalMIMEHeaderKey(k))
}
return h
}
// GetLogFormatter returns our custom log format
func GetLogFormatter(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n",
param.ClientIP,
param.TimeStamp.Format("02/Jan/2006:15:04:05 -0700"),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.BodySize,
param.Latency.Nanoseconds(),
normalizeLog(param.Request.Referer()),
normalizeLog(param.Request.UserAgent()),
normalizeLog(param.Request.Header["X-Forwarded-For"]),
normalizeLog(param.ErrorMessage),
)
}
func normalizeLog(log any) any {
switch v := log.(type) {
case string:
if v == "" {
return "-"
}
case []string:
if len(v) == 0 {
return "-"
}
return strings.Join(v, ", ")
}
return log
}