Files
OverlordandClaude Bot 15a34e1f84 Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts
an item in the cart when it fires. The account's cookies are kept apart
from the watcher's and stored encrypted, with the key in the OS keyring.
Without a keyring, they're stored in a file only you can read.

An alert now goes out right away, without a cart link, and a second one
follows once the item is in the cart. If adding fails, the second alert
carries the cart link instead.

Amazon only accepts additions its page scripts have seen, so plugins can
now press a button in headless Chrome with ctx:click.

The TUI asks for one-time codes and shows the plugin's warning about
account locks in a modal. 'dibs login' signs in from a terminal, or
with --browser in a Chrome window, and 'dibs logout' deletes the cookies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:26:26 +02:00

203 lines
5.1 KiB
Go

// Package state keeps what dibs must remember between runs in a JSON file in
// the user config dir. Every dibs process shares it, so a `dibs check` run
// next to a running watcher still respects the rate limits.
package state
import (
"encoding/json"
"errors"
"io/fs"
"os"
"path/filepath"
"sync"
"time"
"git.security-command.org/Overlord/dibs/internal/alert"
"git.security-command.org/Overlord/dibs/internal/config"
"git.security-command.org/Overlord/dibs/internal/fsutil"
"git.security-command.org/Overlord/dibs/internal/fx"
"git.security-command.org/Overlord/dibs/internal/ratelimit"
"git.security-command.org/Overlord/dibs/internal/session"
)
// Hosts, URLs and alerts untouched for forgetAfter are dropped, such as URLs
// removed from the config. An alert is kept for as long as the longest
// cooldown lasts.
const forgetAfter = config.MaxCooldown
type file struct {
Hosts map[string]ratelimit.HostState `json:"hosts"`
URLs map[string]time.Time `json:"urls"`
Alerts map[string]alert.Sent `json:"alerts,omitempty"`
FX *fx.Rates `json:"fx,omitempty"`
// Sessions are never forgotten, unlike the rest.
Sessions map[string]session.Record `json:"sessions,omitempty"`
}
// Store is the state file. It implements ratelimit.Persister, alert.Store,
// fx.Store and session.Store.
type Store struct {
path string
lock string
mu sync.Mutex
err error
}
// Dir is the directory dibs keeps its files in.
func Dir() (string, error) {
dir, err := os.UserConfigDir()
if err != nil { return "", err }
return filepath.Join(dir, "dibs"), nil
}
// Open returns the store at dir/state.json. The file is created on the first
// write.
func Open(dir string) *Store {
return &Store{path: filepath.Join(dir, "state.json"), lock: filepath.Join(dir, "state.lock")}
}
// Err returns the last error reading or writing the file, if any. The
// limiter still works in memory without the file.
func (s *Store) Err() error {
s.mu.Lock()
defer s.mu.Unlock()
return s.err
}
// Lock takes the lock shared by every dibs process using this directory.
// When that fails, the limiter carries on with only its in-process lock.
func (s *Store) Lock() func() {
unlock, err := fsutil.Lock(s.lock)
if err != nil {
s.mu.Lock()
s.err = err
s.mu.Unlock()
return func() {}
}
return unlock
}
func (s *Store) LoadHost(host string) (ratelimit.HostState, bool) {
s.mu.Lock()
defer s.mu.Unlock()
h, ok := s.read().Hosts[host]
return h, ok
}
// StoreHost replaces the state of host. The limiter has already merged in
// what other processes stored.
func (s *Store) StoreHost(host string, h ratelimit.HostState) {
s.update(func(f *file) { f.Hosts[host] = h })
}
func (s *Store) LoadURL(url string) (time.Time, bool) {
s.mu.Lock()
defer s.mu.Unlock()
t, ok := s.read().URLs[url]
return t, ok
}
func (s *Store) StoreURL(url string, last time.Time) {
s.update(func(f *file) {
if last.After(f.URLs[url]) { f.URLs[url] = last }
})
}
func (s *Store) LoadAlert(item string) (alert.Sent, bool) {
s.mu.Lock()
defer s.mu.Unlock()
a, ok := s.read().Alerts[item]
return a, ok
}
func (s *Store) StoreAlert(item string, a alert.Sent) {
s.update(func(f *file) { f.Alerts[item] = a })
}
func (s *Store) LoadRates() (*fx.Rates, bool) {
s.mu.Lock()
defer s.mu.Unlock()
r := s.read().FX
return r, r != nil
}
func (s *Store) StoreRates(r *fx.Rates) {
s.update(func(f *file) {
if f.FX == nil || r.Fetched.After(f.FX.Fetched) { f.FX = r }
})
}
func (s *Store) LoadSession(shop string) (session.Record, bool) {
s.mu.Lock()
defer s.mu.Unlock()
r, ok := s.read().Sessions[shop]
return r, ok
}
func (s *Store) StoreSession(shop string, r session.Record) {
s.update(func(f *file) {
if r.Warned.IsZero() && len(r.Logins) == 0 { delete(f.Sessions, shop) } else { f.Sessions[shop] = r }
})
}
// read returns the file's contents, or empty state if there is none yet.
// Callers hold mu.
func (s *Store) read() *file {
f := &file{Hosts: map[string]ratelimit.HostState{}, URLs: map[string]time.Time{}, Alerts: map[string]alert.Sent{}, Sessions: map[string]session.Record{}}
data, err := os.ReadFile(s.path)
switch {
case errors.Is(err, fs.ErrNotExist):
return f
case err != nil:
s.err = err
return f
}
if err := json.Unmarshal(data, f); err != nil {
s.err = err
return f
}
if f.Hosts == nil { f.Hosts = map[string]ratelimit.HostState{} }
if f.URLs == nil { f.URLs = map[string]time.Time{} }
if f.Alerts == nil { f.Alerts = map[string]alert.Sent{} }
if f.Sessions == nil { f.Sessions = map[string]session.Record{} }
return f
}
func (s *Store) update(change func(f *file)) {
s.mu.Lock()
defer s.mu.Unlock()
f := s.read()
change(f)
cutoff := time.Now().Add(-forgetAfter)
for host, h := range f.Hosts {
if h.Next.Before(cutoff) && h.BackoffUntil.Before(cutoff) { delete(f.Hosts, host) }
}
for url, t := range f.URLs {
if t.Before(cutoff) { delete(f.URLs, url) }
}
for item, a := range f.Alerts {
if a.Time.Before(cutoff) { delete(f.Alerts, item) }
}
data, err := json.MarshalIndent(f, "", " ")
if err == nil { err = fsutil.WriteFileAtomic(s.path, data, 0o600) }
s.err = err
}