Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -2,56 +2,60 @@ import express from 'express';
|
|
| 2 |
import fetch from 'node-fetch';
|
| 3 |
|
| 4 |
const app = express();
|
|
|
|
| 5 |
|
| 6 |
-
//
|
| 7 |
-
app.
|
| 8 |
-
|
| 9 |
-
next();
|
| 10 |
});
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
app.all('*', async (req, res) => {
|
|
|
|
|
|
|
| 15 |
try {
|
| 16 |
-
//
|
| 17 |
-
const targetUrl = req.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
// Копируем
|
| 20 |
const headers = {...req.headers};
|
| 21 |
delete headers['host'];
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
const fetchOptions = {
|
| 25 |
method: req.method,
|
| 26 |
headers,
|
| 27 |
-
|
| 28 |
-
};
|
| 29 |
-
|
| 30 |
-
//
|
| 31 |
-
if (
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
// Копируем заголовки
|
| 41 |
-
for (const [key, value] of response.headers) {
|
| 42 |
res.setHeader(key, value);
|
| 43 |
}
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
} catch (error) {
|
| 49 |
-
|
| 50 |
-
res.status(500).end();
|
| 51 |
}
|
| 52 |
});
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
console.log(`Server running on port ${port}`);
|
| 57 |
});
|
|
|
|
| 2 |
import fetch from 'node-fetch';
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
+
app.use(express.raw({type: '*/*'}));
|
| 6 |
|
| 7 |
+
// Hello World на главной
|
| 8 |
+
app.get('/', (req, res) => {
|
| 9 |
+
res.send('Hello World');
|
|
|
|
| 10 |
});
|
| 11 |
|
| 12 |
+
// Прокси для всех остальных путей
|
|
|
|
| 13 |
app.all('*', async (req, res) => {
|
| 14 |
+
if (req.url === '/') return;
|
| 15 |
+
|
| 16 |
try {
|
| 17 |
+
// Извлекаем URL из пути, пропуская первый слэш
|
| 18 |
+
const targetUrl = req.url.substring(1);
|
| 19 |
+
|
| 20 |
+
// Проверяем что URL валидный
|
| 21 |
+
if (!targetUrl.startsWith('http')) {
|
| 22 |
+
return res.status(400).send('Invalid URL');
|
| 23 |
+
}
|
| 24 |
|
| 25 |
+
// Копируем и фильтруем заголовки
|
| 26 |
const headers = {...req.headers};
|
| 27 |
delete headers['host'];
|
| 28 |
+
delete headers['content-length'];
|
| 29 |
|
| 30 |
+
const response = await fetch(targetUrl, {
|
|
|
|
| 31 |
method: req.method,
|
| 32 |
headers,
|
| 33 |
+
body: req.method !== 'GET' ? req.body : undefined
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
// Streaming response
|
| 37 |
+
if (response.headers.get('content-type')?.includes('text/event-stream')) {
|
| 38 |
+
res.setHeader('Content-Type', 'text/event-stream');
|
| 39 |
+
response.body.pipe(res);
|
| 40 |
+
return;
|
| 41 |
}
|
| 42 |
|
| 43 |
+
// Regular response
|
| 44 |
+
const data = await response.buffer();
|
| 45 |
+
|
| 46 |
+
// Копируем все заголовки из ответа
|
| 47 |
+
for (const [key, value] of response.headers.entries()) {
|
|
|
|
|
|
|
| 48 |
res.setHeader(key, value);
|
| 49 |
}
|
| 50 |
+
|
| 51 |
+
res.status(response.status);
|
| 52 |
+
res.send(data);
|
| 53 |
|
| 54 |
} catch (error) {
|
| 55 |
+
res.status(500).send(error.message);
|
|
|
|
| 56 |
}
|
| 57 |
});
|
| 58 |
|
| 59 |
+
app.listen(7860, '0.0.0.0', () => {
|
| 60 |
+
console.log('Server running on port 7860');
|
|
|
|
| 61 |
});
|