diff --git a/main.go b/main.go index 13dcced..7932212 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,9 @@ import ( "time" ) +// -------------------------------------------------------------------- +// Configuration: HTTP client and buffer pool (memory tuned) +// -------------------------------------------------------------------- var httpClient = &http.Client{ Timeout: 15 * time.Second, Transport: &http.Transport{ @@ -36,7 +39,7 @@ var httpClient = &http.Client{ var copyBufPool = sync.Pool{ New: func() any { - b := make([]byte, 32*1024) + b := make([]byte, 32*1024) // 32KB buffer reused return &b }, } @@ -44,39 +47,63 @@ var copyBufPool = sync.Pool{ const pinterestSearchURL = "https://www.pinterest.com/resource/BaseSearchResource/get/" const cookieName = "pinata_bm" -// -- bookmark encryption key (32 bytes) -- +// -------------------------------------------------------------------- +// Bookmarks: types, key, limits +// -------------------------------------------------------------------- +type BookmarkEntry struct { + Type string `json:"type"` // "q" or "img" + Value string `json:"value"` // query or image URL +} + var bookmarkKey []byte var bookmarkingEnabled bool var disableReverse bool -func init() { - if kb := os.Getenv("PINATA_BOOKMARK_KEY"); kb != "" { - if decoded, err := base64.StdEncoding.DecodeString(kb); err == nil && len(decoded) == 32 { - bookmarkKey = decoded - bookmarkingEnabled = true - log.Println("Bookmarking enabled") - } else { - log.Println("PINATA_BOOKMARK_KEY provided but invalid; bookmarking disabled") - bookmarkingEnabled = false - } - } else { - log.Println("PINATA_BOOKMARK_KEY not set; bookmarking disabled") - bookmarkingEnabled = false - } +const maxBookmarks = 30 +const maxItemLen = 256 - // New: whether to disable reverse image search (Tineye) - disableEnv := strings.ToLower(strings.TrimSpace(os.Getenv("PINATA_DISABLE_REVERSE"))) - if disableEnv == "1" || disableEnv == "true" || disableEnv == "yes" { - disableReverse = true - log.Println("Reverse image search disabled via PINATA_DISABLE_REVERSE") - } else { - disableReverse = false - } +// -------------------------------------------------------------------- +// init: read env variables +// - PINATA_BOOKMARK_KEY: base64 32-byte key to enable bookmarks +// - PINATA_DISABLE_REVERSE: "1"/"true"/"yes" disables reverse search link +// -------------------------------------------------------------------- +func init() { + // PINATA_BOOKMARK_KEY + if kb := os.Getenv("PINATA_BOOKMARK_KEY"); kb != "" { + if decoded, err := base64.StdEncoding.DecodeString(kb); err == nil && len(decoded) == 32 { + bookmarkKey = decoded + bookmarkingEnabled = true + log.Println("Bookmarking enabled") + } else { + bookmarkingEnabled = false + log.Println("PINATA_BOOKMARK_KEY provided but invalid; bookmarking disabled") + } + } else { + bookmarkingEnabled = false + log.Println("PINATA_BOOKMARK_KEY not set; bookmarking disabled") + } + + // PINATA_DISABLE_REVERSE + switch strings.ToLower(strings.TrimSpace(os.Getenv("PINATA_DISABLE_REVERSE"))) { + case "1", "true", "yes": + disableReverse = true + log.Println("Reverse image search disabled via PINATA_DISABLE_REVERSE") + default: + disableReverse = false + } } -// encrypt a JSON list of strings -> base64 url safe -func encryptBookmarks(items []string) (string, error) { - plain, _ := json.Marshal(items) +// -------------------------------------------------------------------- +// Encryption helpers for cookie storage (AES-GCM) +// -------------------------------------------------------------------- +func encryptBookmarks(entries []BookmarkEntry) (string, error) { + if !bookmarkingEnabled { + return "", nil + } + plain, err := json.Marshal(entries) + if err != nil { + return "", err + } block, err := aes.NewCipher(bookmarkKey) if err != nil { return "", err @@ -89,12 +116,14 @@ func encryptBookmarks(items []string) (string, error) { if _, err := rand.Read(nonce); err != nil { return "", err } - ciphertext := gcm.Seal(nonce, nonce, plain, nil) - return base64.RawURLEncoding.EncodeToString(ciphertext), nil + ct := gcm.Seal(nonce, nonce, plain, nil) + return base64.RawURLEncoding.EncodeToString(ct), nil } -// decrypt base64 cookie -> list of strings -func decryptBookmarks(encoded string) ([]string, error) { +func decryptBookmarks(encoded string) ([]BookmarkEntry, error) { + if !bookmarkingEnabled { + return nil, nil + } data, err := base64.RawURLEncoding.DecodeString(encoded) if err != nil { return nil, err @@ -109,7 +138,7 @@ func decryptBookmarks(encoded string) ([]string, error) { } ns := gcm.NonceSize() if len(data) < ns { - return nil, err + return nil, io.ErrUnexpectedEOF } nonce := data[:ns] ct := data[ns:] @@ -117,15 +146,27 @@ func decryptBookmarks(encoded string) ([]string, error) { if err != nil { return nil, err } - var items []string - if err := json.Unmarshal(plain, &items); err != nil { - return nil, err + // Try new format ([]BookmarkEntry) + var entries []BookmarkEntry + if err := json.Unmarshal(plain, &entries); err == nil { + return entries, nil } - return items, nil + // Fallback to legacy []string -> convert to type "q" + var arr []string + if err := json.Unmarshal(plain, &arr); err == nil { + out := make([]BookmarkEntry, 0, len(arr)) + for _, s := range arr { + out = append(out, BookmarkEntry{Type: "q", Value: s}) + } + return out, nil + } + return nil, io.ErrUnexpectedEOF } -// helper to read bookmarks from request cookie (returns empty slice if none or invalid) -func readBookmarksFromReq(r *http.Request) []string { +// -------------------------------------------------------------------- +// Cookie helpers: read, set, clear +// -------------------------------------------------------------------- +func readBookmarksFromReq(r *http.Request) []BookmarkEntry { if !bookmarkingEnabled { return nil } @@ -133,68 +174,72 @@ func readBookmarksFromReq(r *http.Request) []string { if err != nil || c.Value == "" { return nil } - items, err := decryptBookmarks(c.Value) + entries, err := decryptBookmarks(c.Value) if err != nil { + // invalid cookie -> ignore return nil } - return items + return entries } -// helper to set bookmarks cookie -func setBookmarksCookie(w http.ResponseWriter, items []string) { +func setBookmarksCookie(w http.ResponseWriter, entries []BookmarkEntry) { if !bookmarkingEnabled { return } - // sanitize and truncate each item - trunc := make([]string, 0, len(items)) - for _, s := range items { - s = strings.TrimSpace(s) - if s == "" { + seen := map[string]bool{} + out := make([]BookmarkEntry, 0, len(entries)) + for _, e := range entries { + v := strings.TrimSpace(e.Value) + if v == "" { continue } - if len(s) > 64 { - s = s[:64] + if len(v) > maxItemLen { + v = v[:maxItemLen] } - trunc = append(trunc, s) - if len(trunc) >= 30 { + key := e.Type + "|" + v + if seen[key] { + continue + } + seen[key] = true + if e.Type != "q" && e.Type != "img" { + e.Type = "q" + } + out = append(out, BookmarkEntry{Type: e.Type, Value: v}) + if len(out) >= maxBookmarks { break } } - enc, err := encryptBookmarks(trunc) + enc, err := encryptBookmarks(out) if err != nil { - // fail silently (do not set) + // fail silently return } c := &http.Cookie{ Name: cookieName, Value: enc, Path: "/", - HttpOnly: true, // not accessible to JS (we have no JS but keeps it private) + HttpOnly: true, SameSite: http.SameSiteLaxMode, - // Secure: true, // keep commented for local HTTP dev - set true in production behind TLS - MaxAge: 60 * 60 * 24 * 365 * 10, // ~10 years + // Secure: true, // enable when serving over HTTPS + MaxAge: 60 * 60 * 24 * 365 * 10, } http.SetCookie(w, c) } -// remove bookmark and reset cookie -func removeBookmarkCookie(w http.ResponseWriter, items []string) { - // if empty, clear cookie - if len(items) == 0 { - c := &http.Cookie{ - Name: cookieName, - Value: "", - Path: "/", - HttpOnly: true, - MaxAge: -1, - } - http.SetCookie(w, c) - return +func clearBookmarksCookie(w http.ResponseWriter) { + c := &http.Cookie{ + Name: cookieName, + Value: "", + Path: "/", + HttpOnly: true, + MaxAge: -1, } - setBookmarksCookie(w, items) + http.SetCookie(w, c) } -// ----- CSS and HTML (no JS) ----- +// -------------------------------------------------------------------- +// Stylesheet and minimal HTML templates (no JS) +// -------------------------------------------------------------------- const cssContent = ` :root{--bg:#0b0f17;--muted:#94a3b8;--text:#e6e6ff;--accent:#7c3aed;--card-shadow:rgba(0,0,0,0.6)} *{box-sizing:border-box}html,body{height:100%}body{margin:0;padding:20px;background:linear-gradient(180deg,#071020 0%,#0b0f17 100%);color:var(--text);font-family:ui-monospace,Menlo,Monaco,monospace} @@ -210,29 +255,22 @@ button[type="submit"],.btn-save{background:linear-gradient(90deg,var(--accent),# .img-container { column-width: 260px; column-gap: 16px; width: 100%; max-width: 1400px; margin-top: 18px; } .card { display:inline-block; width:100%; margin:0 0 16px; border-radius:10px; overflow:hidden; background:linear-gradient(180deg,rgba(255,255,255,0.01),rgba(255,255,255,0.02)); box-shadow:0 6px 18px rgba(3,7,18,0.6); border:1px solid rgba(124,58,237,0.06); break-inside: avoid; -webkit-column-break-inside: avoid; -moz-column-break-inside: avoid; min-height:0; position:relative; } .card img { display:block; width:100%; height:auto; object-fit:cover; background:#08101a; } -.magnifier{position:absolute;top:8px;right:8px;background:rgba(0,0,0,0.45);border:1px solid rgba(255,255,255,0.06);color:var(--text);padding:6px;border-radius:999px;font-size:14px;width:34px;height:34px;display:inline-flex;align-items:center;justify-content:center;text-decoration:none;transition:transform .12s ease,background .12s ease} -.magnifier:hover{transform:translateY(-2px);background:linear-gradient(180deg,rgba(124,58,237,0.14),rgba(124,58,237,0.08));color:white} +.card-controls { position:absolute; top:8px; right:8px; display:flex; gap:8px; align-items:center; } +.btn-save-mini { background: rgba(124,58,237,0.12); color: var(--text); border: none; padding:6px; border-radius:999px; cursor:pointer; font-weight:700; } +.magnifier{background:rgba(0,0,0,0.45);border:1px solid rgba(255,255,255,0.06);color:var(--text);padding:6px;border-radius:999px;font-size:14px;width:34px;height:34px;display:inline-flex;align-items:center;justify-content:center;text-decoration:none} .bookmarks{margin-left:12px;color:var(--muted);font-size:14px} .bookmark-list{margin-top:10px;display:flex;gap:8px;flex-wrap:wrap} .bookmark-pill{background:rgba(255,255,255,0.03);padding:6px 8px;border-radius:999px;border:1px solid rgba(255,255,255,0.04);font-size:13px;display:flex;gap:6px;align-items:center} .bookmark-pill form{display:inline} .bookmark-remove-btn{background:transparent;border:none;color:#ff7b7b;font-weight:700;cursor:pointer;padding:0 6px} +.export-form{margin-top:12px;display:flex;gap:8px;align-items:center} .pagination{text-align:center;margin:26px 0} .pagination a{color:var(--accent);text-decoration:none;padding:8px 12px;border-radius:8px;border:1px solid rgba(124,58,237,0.12);background:rgba(124,58,237,0.02)} .footer-note{color:var(--muted);font-size:12px;margin-top:22px} -@media (max-width:640px){ - body{padding:12px;font-size:18px} - .brand{font-size:22px} - input[type="text"]{min-width:120px;padding:12px 14px;font-size:16px} - button[type="submit"],.btn-save{padding:10px 14px;font-size:16px;border-radius:10px} - .img-container{column-width:180px;column-gap:12px} - .search-block{gap:10px;flex-direction:column} - .search-inline{width:100%} - .search-box{margin-left:0;width:100%} - .bookmarks{order:3;width:100%;margin-top:8px} -} +@media (max-width:640px){ body{padding:12px;font-size:18px} .brand{font-size:22px} input[type="text"]{min-width:120px;padding:12px 14px;font-size:16px} button[type="submit"],.btn-save{padding:10px 14px;font-size:16px;border-radius:10px} .img-container{column-width:180px;column-gap:12px} .search-block{gap:10px;flex-direction:column} .search-inline{width:100%} .search-box{margin-left:0;width:100%} .bookmarks{order:3;width:100%;margin-top:8px} } ` +<<<<<<< HEAD // Static index page (no JS). Bookmarks rendered server-side only here. func indexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf8") @@ -258,9 +296,50 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { } io.WriteString(w, `