Files
lucioandClaude Opus 5.5 c87aad0254 Risk-based robot check with server-issued photo keys
The main page and direct photo links open without a prompt. Each
photo's pool is sealed with its own key, handed out by POST /k
(served by the daemon in serve and export mode). The hold-and-solve
card appears only for low-entropy clicks into a collection, too many
low-entropy landings from one IP, or more than photo_limit photos per
window, with escalating proof-of-work and a 429 cap. Build ids now
sort chronologically within the same second.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 22:45:50 -08:00

128 lines
2.5 KiB
Go

package keys
import (
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/luciochen233/luphoto/internal/fsutil"
"github.com/luciochen233/luphoto/internal/protect"
"github.com/luciochen233/luphoto/internal/site"
)
const (
rescanEvery = 2 * time.Second
albumCache = 4096
)
type album map[string]struct{}
type buildInfo struct {
id string
dir string
secret []byte
mu sync.Mutex
albums map[string]album
}
type builds struct {
mu sync.RWMutex
root string
byID map[string]*buildInfo
scanned time.Time
}
func validID(id string) bool {
if id == "" || len(id) > 64 {
return false
}
for i := 0; i < len(id); i++ {
c := id[i]
if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c == '-') {
return false
}
}
return true
}
func validSlug(slug string) bool {
return slug != "" && len(slug) <= 255 && !strings.HasPrefix(slug, ".") && !strings.ContainsAny(slug, "/\\\x00")
}
func (b *builds) reload(root string) {
next := map[string]*buildInfo{}
b.mu.RLock()
old := b.byID
same := b.root == root
b.mu.RUnlock()
dir := filepath.Join(root, "builds")
entries, _ := os.ReadDir(dir)
for _, entry := range entries {
id := entry.Name()
if !entry.IsDir() || !validID(id) {
continue
}
if info, ok := old[id]; ok && same {
next[id] = info
continue
}
secret, err := protect.ReadSecret(filepath.Join(dir, id))
if err != nil {
continue
}
next[id] = &buildInfo{id: id, dir: filepath.Join(dir, id), secret: secret, albums: map[string]album{}}
}
b.mu.Lock()
b.root = root
b.byID = next
b.scanned = time.Now()
b.mu.Unlock()
}
func (b *builds) get(root, id string) *buildInfo {
if !validID(id) {
return nil
}
b.mu.RLock()
info := b.byID[id]
fresh := time.Since(b.scanned) < rescanEvery && b.root == root
b.mu.RUnlock()
if info != nil || fresh {
return info
}
b.reload(root)
b.mu.RLock()
info = b.byID[id]
b.mu.RUnlock()
return info
}
func (info *buildInfo) album(slug string) album {
if !validSlug(slug) {
return nil
}
info.mu.Lock()
defer info.mu.Unlock()
if a, ok := info.albums[slug]; ok {
return a
}
var m struct {
Photos []struct {
Hash string `json:"hash"`
} `json:"photos"`
}
var a album
if err := fsutil.ReadJSON(filepath.Join(info.dir, "a", slug, site.ManifestName(info.id)), &m); err == nil {
a = make(album, len(m.Photos))
for _, p := range m.Photos {
a[p.Hash] = struct{}{}
}
}
if a != nil || len(info.albums) < albumCache {
info.albums[slug] = a
}
return a
}