serve: add HTTP timeouts and bind localhost by default

The server had no read/write/idle timeouts, so slowloris connections
held goroutines and file descriptors forever, and the default address
bound all interfaces, exposing the API to the network unintentionally.
Defaults now match the tool's local-search use case; add timeouts and
cap header size.
This commit is contained in:
lakshit verma 2026-08-21 21:43:44 +05:30
parent 2be376e728
commit c8207996d1
No known key found for this signature in database
2 changed files with 11 additions and 3 deletions

View file

@ -41,7 +41,7 @@ Useful flags:
| Flag | Default | Meaning |
|---|---|---|
| `-store` | `./data` | Where the database lives |
| `-addr` | `:8080` | Listen address for `serve` |
| `-addr` | `127.0.0.1:8080` | Listen address for `serve` (binds localhost by default; use `:8080` to expose) |
| `-poll-interval` | `3s` | How often each log is checked for new entries |
| `-window` | `512` | Entries fetched per request while catching up |
| `-no-drain` | off | Skip old and rejected logs (they hold years of history, terabytes) |

12
main.go
View file

@ -96,7 +96,7 @@ func addCommon(fs *flag.FlagSet) *commonConfig {
func cmdServe(args []string) error {
fs := flag.NewFlagSet("serve", flag.ExitOnError)
c := addCommon(fs)
addr := fs.String("addr", ":8080", "listen address")
addr := fs.String("addr", "127.0.0.1:8080", "listen address")
noTail := fs.Bool("no-tail", false, "disable CT tailers")
rateLimit := fs.Int64("rate-limit", 1000, "requests per rolling 24h per IP")
maxResults := fs.Int("max-results", store.DefaultScanLimit, "max results buffered per search query")
@ -137,7 +137,15 @@ func cmdServe(args []string) error {
}
srv.Limiter.StartSweeper(ctx.Done())
httpSrv := &http.Server{Addr: *addr, Handler: srv.Handler()}
httpSrv := &http.Server{
Addr: *addr,
Handler: srv.Handler(),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 8192,
}
errCh := make(chan error, 1)
go func() { errCh <- httpSrv.ListenAndServe() }()