Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
132 lines (108 loc) · 3.72 KB
/
Copy pathserver.js
File metadata and controls
132 lines (108 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'public')));
// --- Shared constants ---
const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
// --- Image Search API (DuckDuckGo scraping) ---
const searchCache = new Map();
const CACHE_TTL = 5 * 60 * 1000;
// Prevent unbounded cache growth
setInterval(() => {
const now = Date.now();
for (const [key, entry] of searchCache) {
if (now - entry.timestamp > CACHE_TTL) searchCache.delete(key);
}
}, CACHE_TTL);
// DuckDuckGo requires a per-session anti-CSRF token for image search
async function fetchSearchToken(query) {
const res = await fetch(`https://duckduckgo.com/?q=${encodeURIComponent(query)}&iax=images&ia=images`, {
headers: { 'User-Agent': USER_AGENT }
});
const text = await res.text();
const match = text.match(/vqd=["']([^"']+)["']/) || text.match(/vqd=([\d-]+)/);
if (!match) throw new Error('Could not extract search token');
return match[1];
}
async function searchImages(query) {
const cached = searchCache.get(query);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.results;
}
const token = await fetchSearchToken(query);
const params = new URLSearchParams({
l: 'us-en',
o: 'json',
q: query,
vqd: token,
f: ',,,,license:Share,',
p: '1'
});
const res = await fetch(`https://duckduckgo.com/i.js?${params}`, {
headers: {
'User-Agent': USER_AGENT,
'Referer': 'https://duckduckgo.com/'
}
});
if (!res.ok) throw new Error(`DuckDuckGo returned ${res.status}`);
const data = await res.json();
const results = (data.results || [])
.filter(r => {
if (r.width && r.height) {
return r.width >= 150 && r.height >= 150;
}
return true;
})
.map(r => ({
url: r.image,
thumbnail: r.thumbnail,
title: r.title,
source: r.source
}));
searchCache.set(query, { results, timestamp: Date.now() });
return results;
}
app.get('/api/search-images', async (req, res) => {
const query = req.query.q;
if (!query) return res.status(400).json({ error: 'Missing query parameter "q"' });
try {
const results = await searchImages(query);
res.json({ results });
} catch (err) {
console.error('Image search error:', err.message);
res.status(500).json({ error: 'Image search failed', details: err.message });
}
});
// --- Image Proxy ---
app.get('/api/proxy-image', async (req, res) => {
const imageUrl = req.query.url;
if (!imageUrl) return res.status(400).json({ error: 'Missing query parameter "url"' });
// Only proxy HTTP(S) URLs to prevent SSRF against local services
if (!imageUrl.startsWith('http://') && !imageUrl.startsWith('https://')) {
return res.status(400).json({ error: 'Only HTTP(S) URLs are allowed' });
}
try {
const response = await fetch(imageUrl, {
headers: {
'User-Agent': USER_AGENT,
'Accept': 'image/*'
},
redirect: 'follow'
});
if (!response.ok) {
return res.status(response.status).json({ error: 'Failed to fetch image' });
}
const contentType = response.headers.get('content-type') || 'image/jpeg';
res.set('Content-Type', contentType);
res.set('Cache-Control', 'public, max-age=3600');
const buffer = Buffer.from(await response.arrayBuffer());
res.send(buffer);
} catch (err) {
console.error('Image proxy error:', err.message);
res.status(500).json({ error: 'Image proxy failed' });
}
});
app.listen(PORT, () => {
console.log(`assisted_self-portrait running at http://localhost:${PORT}`);
});