-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (45 loc) · 1.61 KB
/
server.js
File metadata and controls
54 lines (45 loc) · 1.61 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
// server.js
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
// Enable CORS for all origins (customize for production)
app.use(cors());
app.use(express.json());
// Helper: fetch for Node.js v16+ with node-fetch v3+
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
// POST /api/virustotal { url: "https://example.com" }
app.post('/api/virustotal', async (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'Missing url in request body' });
}
const apiKey = process.env.VIRUSTOTAL_API_KEY;
if (!apiKey) {
return res.status(500).json({ error: 'VirusTotal API key not configured' });
}
try {
// Submit the URL for scanning (optional, but ensures fresh scan)
await fetch('https://www.virustotal.com/vtapi/v2/url/scan', {
method: 'POST',
headers: { 'x-apikey': apiKey },
body: new URLSearchParams({ url })
});
// Get the analysis results
const encodedUrl = encodeURIComponent(url);
const vtRes = await fetch(`https://www.virustotal.com/vtapi/v2/url/report?apikey=${apiKey}&resource=${encodedUrl}`);
const data = await vtRes.json();
res.json(data);
} catch (error) {
console.error('VirusTotal proxy error:', error);
res.status(500).json({ error: 'Failed to fetch from VirusTotal' });
}
});
// Health check
app.get('/', (req, res) => {
res.send('VirusTotal Proxy Server is running.');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});