-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-server.js
More file actions
114 lines (92 loc) · 3.77 KB
/
proxy-server.js
File metadata and controls
114 lines (92 loc) · 3.77 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
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3001;
// Ensure downloads directory exists
const DOWNLOADS_DIR = path.join(__dirname, 'downloads');
if (!fs.existsSync(DOWNLOADS_DIR)) {
fs.mkdirSync(DOWNLOADS_DIR);
}
app.use(cors({
origin: 'http://localhost:3000',
credentials: true
}));
// Increase limit for JSON/URL encoded bodies, though we will use streaming for files
app.use(express.json({ limit: '50mb' }));
// Endpoint to save files directly to disk
app.post('/save-file', (req, res) => {
const fileName = req.query.filename;
if (!fileName) {
return res.status(400).send('Missing filename query parameter');
}
// Sanitize filename to prevent directory traversal
const safeFileName = path.basename(fileName);
const filePath = path.join(DOWNLOADS_DIR, safeFileName);
console.log(`Saving file to: ${filePath}`);
const writeStream = fs.createWriteStream(filePath);
req.pipe(writeStream);
writeStream.on('finish', () => {
console.log(`Successfully saved ${safeFileName}`);
res.status(200).send({ success: true, path: filePath });
});
writeStream.on('error', (err) => {
console.error('Error writing file:', err);
res.status(500).send({ error: err.message });
});
});
app.all('/api/*', async (req, res) => {
try {
const path = req.url.replace('/api', '');
const targetUrl = `https://api.limitless.ai${path}`;
console.log('\n=== Proxy Request ===');
console.log('Method:', req.method);
console.log('Target URL:', targetUrl);
console.log('Authorization:', req.headers.authorization ? req.headers.authorization.substring(0, 25) + '...' : 'MISSING');
console.log('X-API-Key:', req.headers['x-api-key'] ? req.headers['x-api-key'].substring(0, 10) + '...' : 'MISSING');
const headers = {
'Authorization': req.headers.authorization || '',
'X-API-Key': req.headers['x-api-key'] || '',
'Accept': req.headers.accept || 'application/json',
'User-Agent': 'LimitlessProxy/1.0'
};
const response = await fetch(targetUrl, {
method: req.method,
headers: headers,
body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : undefined
});
console.log('Response Status:', response.status);
response.headers.forEach((value, name) => {
res.setHeader(name, value);
});
res.status(response.status);
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
const data = await response.json();
console.log('JSON Response:', JSON.stringify(data).substring(0, 100));
res.json(data);
} else if (response.status >= 400) {
const text = await response.text();
console.log('Error Response:', text.substring(0, 200));
res.send(text);
} else {
const buffer = await response.buffer();
console.log('Binary data:', buffer.length, 'bytes');
res.send(buffer);
}
} catch (error) {
console.error('Proxy error:', error.message);
console.error(error.stack);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`\n🚀 Proxy server on http://localhost:${PORT}`);
console.log(`📁 Saving files to: ${DOWNLOADS_DIR}`);
console.log(`Forwarding /api/* to https://api.limitless.ai/*\n`);
});