Anti-scraping: 3x3 grids, decoy pools, robot check
Every pyramid level is cut into at least min_grid x min_grid tiles. Each photo's tile pool mixes real tiles with hard-linked decoys from other photos; the grid-to-pool map, byte sizes, seed and scramble seed are sealed per build and unlocked by a press-and-hold check backed by a static proof of work. Cell scrambling is on by default, and robots.txt disallows tiles and pool files. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
aaf7cafdf9
commit
f5152fb709
30 files changed
+2136
-203
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"hash"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Keyed struct {
|
||||
mac hash.Hash
|
||||
buf []byte
|
||||
sum []byte
|
||||
}
|
||||
|
||||
func (p *Processor) Keyed() *Keyed {
|
||||
return &Keyed{mac: hmac.New(sha256.New, p.key), buf: make([]byte, 0, 96), sum: make([]byte, 0, sha256.Size)}
|
||||
}
|
||||
|
||||
func (k *Keyed) digest(label, hash string, level, slot int, source string) []byte {
|
||||
b := append(k.buf[:0], label...)
|
||||
b = append(b, '|')
|
||||
b = append(b, hash...)
|
||||
b = append(b, '|')
|
||||
b = strconv.AppendInt(b, int64(level), 10)
|
||||
if slot >= 0 {
|
||||
b = append(b, '|')
|
||||
b = strconv.AppendInt(b, int64(slot), 10)
|
||||
b = append(b, '|')
|
||||
b = append(b, source...)
|
||||
}
|
||||
k.buf = b
|
||||
k.mac.Reset()
|
||||
k.mac.Write(b)
|
||||
k.sum = k.mac.Sum(k.sum[:0])
|
||||
return k.sum
|
||||
}
|
||||
|
||||
func (k *Keyed) DecoyName(hash string, level, slot int, source string) string {
|
||||
return hex.EncodeToString(k.digest("decoy", hash, level, slot, source)[:8])
|
||||
}
|
||||
|
||||
func (k *Keyed) Seed(label, hash string, level int) uint64 {
|
||||
return binary.BigEndian.Uint64(k.digest(label, hash, level, -1, ""))
|
||||
}
|
||||
|
||||
func (p *Processor) Ext() string {
|
||||
return "." + p.images.Format
|
||||
}
|
||||
@@ -21,16 +21,18 @@ import (
|
||||
"github.com/luciochen233/luphoto/internal/fsutil"
|
||||
)
|
||||
|
||||
const processorVersion = "luphoto-pyramid-1"
|
||||
const processorVersion = "luphoto-pyramid-2"
|
||||
|
||||
type Level struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Cols int `json:"cols"`
|
||||
Rows int `json:"rows"`
|
||||
Quality int `json:"quality"`
|
||||
Tiles []string `json:"tiles"`
|
||||
Bytes []int64 `json:"bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
TileSize int `json:"tile_size"`
|
||||
Overlap int `json:"overlap,omitempty"`
|
||||
Cols int `json:"cols"`
|
||||
Rows int `json:"rows"`
|
||||
Quality int `json:"quality"`
|
||||
Tiles []string `json:"tiles"`
|
||||
Bytes []int64 `json:"bytes"`
|
||||
}
|
||||
|
||||
type Pyramid struct {
|
||||
@@ -124,6 +126,7 @@ func New(ctx context.Context, cfg *config.Config) (*Processor, error) {
|
||||
"format=" + format,
|
||||
fmt.Sprintf("q=%d,%d,%d", img.Quality.Top, img.Quality.Mid, img.Quality.Low),
|
||||
"min_levels=" + strconv.Itoa(img.MinLevels),
|
||||
"min_grid=" + strconv.Itoa(img.MinGrid),
|
||||
"seed=" + strconv.Itoa(img.SeedPx),
|
||||
"names=" + img.TileNames,
|
||||
"scramble=" + strconv.FormatBool(img.ScrambleCells),
|
||||
|
||||
+30
-15
@@ -29,14 +29,27 @@ func (p *Processor) Overlap() int {
|
||||
return p.images.Overlap
|
||||
}
|
||||
|
||||
func LevelSizes(width, height, minLevels int) [][2]int {
|
||||
func (p *Processor) levelTile(size [2]int) (int, int) {
|
||||
ts := max(1, min(p.images.TileSize, min(size[0], size[1])/p.images.MinGrid))
|
||||
overlap := p.Overlap()
|
||||
if ts <= 2*overlap {
|
||||
overlap = 0
|
||||
}
|
||||
return ts, overlap
|
||||
}
|
||||
|
||||
func LevelSizes(width, height, minLevels, minGrid int) [][2]int {
|
||||
sizes := [][2]int{{width, height}}
|
||||
for {
|
||||
w, h := sizes[len(sizes)-1][0], sizes[len(sizes)-1][1]
|
||||
if (max(w, h) < minLongEdge && len(sizes) >= minLevels) || (w == 1 && h == 1) {
|
||||
break
|
||||
}
|
||||
sizes = append(sizes, [2]int{(w + 1) / 2, (h + 1) / 2})
|
||||
next := [2]int{(w + 1) / 2, (h + 1) / 2}
|
||||
if min(next[0], next[1]) < minGrid {
|
||||
break
|
||||
}
|
||||
sizes = append(sizes, next)
|
||||
}
|
||||
for i, j := 0, len(sizes)-1; i < j; i, j = i+1, j-1 {
|
||||
sizes[i], sizes[j] = sizes[j], sizes[i]
|
||||
@@ -152,7 +165,7 @@ func (p *Processor) process(ctx context.Context, job *Job) (*Record, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sizes := LevelSizes(width, height, p.images.MinLevels)
|
||||
sizes := LevelSizes(width, height, p.images.MinLevels, p.images.MinGrid)
|
||||
count := len(sizes)
|
||||
seedLevel := count - 1
|
||||
for i, size := range sizes {
|
||||
@@ -200,9 +213,9 @@ func (p *Processor) process(ctx context.Context, job *Job) (*Record, error) {
|
||||
defer wg.Done()
|
||||
defer src.release()
|
||||
input := src.path
|
||||
if p.images.ScrambleCells {
|
||||
if ts, _ := p.levelTile(sizes[i]); p.images.ScrambleCells && ts >= 2*ScrambleCell {
|
||||
scrambled := filepath.Join(work, "s"+strconv.Itoa(i)+".v")
|
||||
if err := ScrambleV(src.path, scrambled, scrambleSeed, i, p.images.TileSize, ScrambleCell); err != nil {
|
||||
if err := ScrambleV(src.path, scrambled, scrambleSeed, i, ts, ScrambleCell); err != nil {
|
||||
fail(fmt.Errorf("scramble level %d: %w", i, err))
|
||||
return
|
||||
}
|
||||
@@ -368,11 +381,12 @@ func (p *Processor) cutLevel(ctx context.Context, hash, src, stage string, level
|
||||
quality := p.levelQuality(level, count, size)
|
||||
base := filepath.Join(stage, strconv.Itoa(level))
|
||||
ext := "." + p.images.Format
|
||||
ts, overlap := p.levelTile(size)
|
||||
if err := p.vips.Run(ctx, "dzsave", src, base,
|
||||
"--layout", "dz",
|
||||
"--depth", "one",
|
||||
"--tile-size", strconv.Itoa(p.images.TileSize),
|
||||
"--overlap", strconv.Itoa(p.Overlap()),
|
||||
"--tile-size", strconv.Itoa(ts),
|
||||
"--overlap", strconv.Itoa(overlap),
|
||||
"--suffix", ext+p.vips.SaveOptions(p.images.Format, quality)); err != nil {
|
||||
return Level{}, err
|
||||
}
|
||||
@@ -384,7 +398,6 @@ func (p *Processor) cutLevel(ctx context.Context, hash, src, stage string, level
|
||||
if err != nil {
|
||||
return Level{}, err
|
||||
}
|
||||
ts := p.images.TileSize
|
||||
cols := (size[0] + ts - 1) / ts
|
||||
rows := (size[1] + ts - 1) / ts
|
||||
files := make([]tileFile, 0, len(entries))
|
||||
@@ -405,13 +418,15 @@ func (p *Processor) cutLevel(ctx context.Context, hash, src, stage string, level
|
||||
return Level{}, fmt.Errorf("expected %dx%d tiles, found %d", cols, rows, len(files))
|
||||
}
|
||||
out := Level{
|
||||
Width: size[0],
|
||||
Height: size[1],
|
||||
Cols: cols,
|
||||
Rows: rows,
|
||||
Quality: quality,
|
||||
Tiles: make([]string, cols*rows),
|
||||
Bytes: make([]int64, cols*rows),
|
||||
Width: size[0],
|
||||
Height: size[1],
|
||||
TileSize: ts,
|
||||
Overlap: overlap,
|
||||
Cols: cols,
|
||||
Rows: rows,
|
||||
Quality: quality,
|
||||
Tiles: make([]string, cols*rows),
|
||||
Bytes: make([]int64, cols*rows),
|
||||
}
|
||||
renamers := min(4, (len(files)+31)/32)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
@@ -171,10 +171,10 @@ func scrambleBand(in, out *os.File, buf, tmp []byte, header vHeader, band int, r
|
||||
for tx := 0; tx < tiles; tx++ {
|
||||
width := min(tileSize, header.width-tx*tileSize)
|
||||
cols := width / cell
|
||||
n := cols * cellRows
|
||||
if n < 2 {
|
||||
if cols < 2 || cellRows < 2 {
|
||||
continue
|
||||
}
|
||||
n := cols * cellRows
|
||||
perm := CellPermutation(n, tileSeed(seed, level, tx, band))
|
||||
origin := tx * tileSize * header.pixelBytes
|
||||
at := func(index, row int) []byte {
|
||||
|
||||
Reference in new issue
Block a user