mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2026-07-23 15:55:45 +00:00
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)
This commit is contained in:
+23
-21
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
@@ -53,39 +54,40 @@ func Setup(cityPath string, asnPath string) (*GeoDB, error) {
|
||||
}
|
||||
|
||||
func (db *GeoDB) CloseDBs() error {
|
||||
var errs []error
|
||||
return closeReaders(db.City, db.ASN)
|
||||
}
|
||||
|
||||
if db.City != nil {
|
||||
if err := db.City.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("closing city db: %w", err))
|
||||
}
|
||||
func (db *GeoDB) Reload() error {
|
||||
city, asn, err := openDatabases(db.cityPath, db.asnPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening new databases: %w", err)
|
||||
}
|
||||
|
||||
if db.ASN != nil {
|
||||
if err := db.ASN.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("closing ASN db: %w", err))
|
||||
}
|
||||
}
|
||||
oldCity, oldASN := db.City, db.ASN
|
||||
db.City, db.ASN = city, asn
|
||||
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("errors closing databases: %s", errs)
|
||||
if err := closeReaders(oldCity, oldASN); err != nil {
|
||||
return fmt.Errorf("closing previous databases: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *GeoDB) Reload() error {
|
||||
if err := db.CloseDBs(); err != nil {
|
||||
return fmt.Errorf("closing existing connections: %w", err)
|
||||
func closeReaders(city *maxminddb.Reader, asn *maxminddb.Reader) error {
|
||||
var errs []error
|
||||
|
||||
if city != nil {
|
||||
if err := city.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("closing city db: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
city, asn, err := openDatabases(db.cityPath, db.asnPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening new connections: %w", err)
|
||||
if asn != nil {
|
||||
if err := asn.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("closing ASN db: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
db.City = city
|
||||
db.ASN = asn
|
||||
return nil
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (db *GeoDB) LookupCity(ip net.IP) (*GeoRecord, error) {
|
||||
|
||||
Reference in New Issue
Block a user