mirror of
https://github.com/juanfont/headscale.git
synced 2026-01-11 20:00:28 +01:00
hscontrol: log acme/autocert errors (#2933)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof" // nolint
|
_ "net/http/pprof" // nolint
|
||||||
@@ -877,6 +878,11 @@ func (h *Headscale) getTLSSettings() (*tls.Config, error) {
|
|||||||
Cache: autocert.DirCache(h.cfg.TLS.LetsEncrypt.CacheDir),
|
Cache: autocert.DirCache(h.cfg.TLS.LetsEncrypt.CacheDir),
|
||||||
Client: &acme.Client{
|
Client: &acme.Client{
|
||||||
DirectoryURL: h.cfg.ACMEURL,
|
DirectoryURL: h.cfg.ACMEURL,
|
||||||
|
HTTPClient: &http.Client{
|
||||||
|
Transport: &acmeLogger{
|
||||||
|
rt: http.DefaultTransport,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Email: h.cfg.ACMEEmail,
|
Email: h.cfg.ACMEEmail,
|
||||||
}
|
}
|
||||||
@@ -997,3 +1003,28 @@ func readOrCreatePrivateKey(path string) (*key.MachinePrivate, error) {
|
|||||||
func (h *Headscale) Change(cs ...change.ChangeSet) {
|
func (h *Headscale) Change(cs ...change.ChangeSet) {
|
||||||
h.mapBatcher.AddWork(cs...)
|
h.mapBatcher.AddWork(cs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide some middleware that can inspect the ACME/autocert https calls
|
||||||
|
// and log when things are failing.
|
||||||
|
type acmeLogger struct {
|
||||||
|
rt http.RoundTripper
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundTrip will log when ACME/autocert failures happen either when err != nil OR
|
||||||
|
// when http status codes indicate a failure has occurred.
|
||||||
|
func (l *acmeLogger) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
resp, err := l.rt.RoundTrip(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Str("url", req.URL.String()).Msg("ACME request failed")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode >= http.StatusBadRequest {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
log.Error().Int("status_code", resp.StatusCode).Str("url", req.URL.String()).Bytes("body", body).Msg("ACME request returned error")
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user