ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m23s
ci / test (pull_request) Successful in 12m59s
ci / build (pull_request) Canceled after 7m17s
ci / image (pull_request) Canceled after 0s
The API serves Prometheus metrics on a listener of its own (UPTIME_API_METRICS_LISTEN, default :9090, "off" disables it), so the public ingress never exposes them: - requests by route and status, with durations except for long polls and agent streams - dispatched checks, stored and rejected results per region, how late results arrive, live checkers and overdue checks per platform region - notification and subscriber mail deliveries, incidents opened and resolved - runs, duration and last success of the background jobs - platform totals (orgs, users, monitors by type, status pages, subscribers, open incidents, pending notifications), read at most every 15 seconds when scraped - the Postgres and Redis connection pools, Go runtime and build info The Helm chart exposes the port on the API service and can add a ServiceMonitor for the Prometheus Operator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
177 lines
5.9 KiB
Go
177 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/base32"
|
|
"net"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
// A custom domain proves it belongs to its page in one of two ways:
|
|
//
|
|
// - a CNAME to <label>.<status page host>, where the label is unique to
|
|
// the page. The platform's status page host has a wildcard record, so
|
|
// the CNAME also routes the domain; one record does both. A CNAME to
|
|
// the shared host alone would not do: it proves the domain points at
|
|
// the platform, not which organization put it there.
|
|
// - the TXT record _uptimesebi.<domain>, for domains whose CNAME cannot be
|
|
// seen: behind a proxy such as Cloudflare's, or a domain without a
|
|
// subdomain using ALIAS or CNAME flattening.
|
|
//
|
|
// The domain watch looks for them on its own after a domain is set, and
|
|
// checks verified domains daily. One whose record has been missing for
|
|
// domainGrace is switched off, so a record left behind after a page is
|
|
// gone or moved cannot be claimed by anybody else.
|
|
|
|
const (
|
|
domainWatchEvery = time.Minute
|
|
domainGrace = 7 * 24 * time.Hour
|
|
keyDomainWatch = "domainwatch:lock"
|
|
)
|
|
|
|
// lookupCNAME is replaced in tests.
|
|
var lookupCNAME = net.DefaultResolver.LookupCNAME
|
|
|
|
var labelEncoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
|
|
|
|
// cnameLabel is the page's label below the status page host, derived from
|
|
// its domain token.
|
|
func cnameLabel(token string) string {
|
|
sum := sha256.Sum256([]byte("cname\x00" + token))
|
|
return "p" + labelEncoding.EncodeToString(sum[:10])
|
|
}
|
|
|
|
// cnameFor is the CNAME target of the page with token, or empty when the
|
|
// platform's status page host is not known.
|
|
func (s *Server) cnameFor(token string) string {
|
|
host := s.cnameTarget()
|
|
if host == "" {
|
|
return ""
|
|
}
|
|
return cnameLabel(token) + "." + host
|
|
}
|
|
|
|
// checkDomain looks for the records proving domain belongs to the page
|
|
// with token and returns "cname", "txt" or "" for neither.
|
|
func (s *Server) checkDomain(ctx context.Context, domain, token string) string {
|
|
if target := s.cnameFor(token); target != "" {
|
|
if c, err := lookupCNAME(ctx, domain); err == nil && strings.EqualFold(strings.TrimSuffix(c, "."), target) {
|
|
return "cname"
|
|
}
|
|
}
|
|
if records, err := lookupTXT(ctx, verifyPrefix+domain); err == nil && slices.Contains(records, token) {
|
|
return "txt"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// RunDomainWatch checks custom domains until ctx ends. Replicas take
|
|
// turns through a lock in Redis.
|
|
func (s *Server) RunDomainWatch(ctx context.Context) {
|
|
tick := time.NewTicker(domainWatchEvery)
|
|
defer tick.Stop()
|
|
for {
|
|
if ok, err := s.rdb.SetNX(ctx, keyDomainWatch, 1, domainWatchEvery-5*time.Second).Result(); err == nil && ok {
|
|
if err := s.metrics.Job("domain_watch", func() error { return s.WatchDomains(ctx, time.Now()) }); err != nil && ctx.Err() == nil {
|
|
s.log.WarnContext(ctx, "checking custom domains failed", "error", err)
|
|
}
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-tick.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
// WatchDomains checks the domains that are due once.
|
|
func (s *Server) WatchDomains(ctx context.Context, now time.Time) error {
|
|
return s.watchDomains(ctx, now, nil)
|
|
}
|
|
|
|
// watchDomains is WatchDomains limited to the page only, unless it is nil.
|
|
func (s *Server) watchDomains(ctx context.Context, now time.Time, only *uuid.UUID) error {
|
|
var due []store.DomainsToCheckRow
|
|
if err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
due, err = q.DomainsToCheck(ctx, store.DomainsToCheckParams{Now: now, PageID: only})
|
|
return err
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
for _, d := range due {
|
|
if err := s.watchDomain(ctx, d, now); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) watchDomain(ctx context.Context, d store.DomainsToCheckRow, now time.Time) error {
|
|
method := s.checkDomain(ctx, d.Domain, d.DomainToken)
|
|
var (
|
|
failing *time.Time
|
|
off bool
|
|
)
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
if method != "" {
|
|
return q.MarkDomainVerified(ctx, store.MarkDomainVerifiedParams{ID: d.ID, Now: now, Method: method})
|
|
}
|
|
var err error
|
|
if failing, err = q.MarkDomainUnconfirmed(ctx, store.MarkDomainUnconfirmedParams{ID: d.ID, Now: now}); err != nil || failing == nil {
|
|
return err
|
|
}
|
|
if now.Sub(*failing) >= domainGrace {
|
|
off = true
|
|
return q.UnverifyDomain(ctx, d.ID)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch {
|
|
case method != "" && d.DomainVerifiedAt == nil:
|
|
s.log.InfoContext(ctx, "custom domain verified", "page", d.ID, "domain", d.Domain, "method", method)
|
|
case off:
|
|
s.log.InfoContext(ctx, "custom domain switched off: its record is gone", "page", d.ID, "domain", d.Domain)
|
|
s.mailDomain(ctx, d, "email.domain_off", now)
|
|
case failing != nil && d.DomainFailingSince == nil:
|
|
s.log.InfoContext(ctx, "custom domain record missing", "page", d.ID, "domain", d.Domain)
|
|
s.mailDomain(ctx, d, "email.domain_missing", now.Add(domainGrace))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// mailDomain tells the organization's owners about d.
|
|
func (s *Server) mailDomain(ctx context.Context, d store.DomainsToCheckRow, kind string, at time.Time) {
|
|
if s.smtp == nil {
|
|
return
|
|
}
|
|
var to []string
|
|
if err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
to, err = q.OrgOwnerEmails(ctx, d.OrgID)
|
|
return err
|
|
}); err != nil || len(to) == 0 {
|
|
return
|
|
}
|
|
l := s.catalog.Match("")
|
|
data := map[string]any{
|
|
"Domain": d.Domain, "Page": d.Title, "Link": s.ticketLink("/statuspages/" + d.ID.String()),
|
|
"Date": at.UTC().Format("2006-01-02 15:04 MST"),
|
|
}
|
|
if err := s.smtp.Send(ctx, notify.Mail{To: to, Subject: l.T(kind+".subject", data), Body: l.T(kind+".body", data)}); err != nil {
|
|
s.log.WarnContext(ctx, "mail about a custom domain failed", "page", d.ID, "error", err)
|
|
}
|
|
}
|