Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,60 +1,72 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
import fetch from 'node-fetch';
|
|
|
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
|
| 6 |
-
//
|
| 7 |
-
app.use((
|
| 8 |
-
res.header('Access-Control-Allow-Origin', '*');
|
| 9 |
-
res.header('Access-Control-Allow-Methods', '*');
|
| 10 |
-
res.header('Access-Control-Allow-Headers', '*');
|
| 11 |
-
|
| 12 |
-
if (req.method === 'OPTIONS') {
|
| 13 |
-
return res.status(200).end();
|
| 14 |
-
}
|
| 15 |
-
next();
|
| 16 |
-
});
|
| 17 |
-
|
| 18 |
-
app.use(express.raw({type: '*/*'}));
|
| 19 |
|
| 20 |
const MY_DOMAIN = 'https://wallegriffkinder-testinghf23.hf.space/';
|
| 21 |
|
| 22 |
-
app.
|
| 23 |
if (req.url === '/') return res.send('OK');
|
| 24 |
|
| 25 |
try {
|
|
|
|
| 26 |
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
|
| 27 |
const targetUrl = fullUrl.substring(fullUrl.indexOf(MY_DOMAIN) + MY_DOMAIN.length);
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
const headers = {...req.headers};
|
| 30 |
delete headers['host'];
|
| 31 |
delete headers['content-length'];
|
| 32 |
-
delete headers['connection'];
|
| 33 |
delete headers['transfer-encoding'];
|
| 34 |
-
delete headers['
|
| 35 |
-
delete headers['referer'];
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
method: req.method,
|
| 39 |
headers,
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
});
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if (!['connection', 'transfer-encoding'].includes(key.toLowerCase())) {
|
| 46 |
res.setHeader(key, value);
|
| 47 |
}
|
| 48 |
-
}
|
| 49 |
|
| 50 |
-
|
| 51 |
response.body.pipe(res);
|
| 52 |
|
| 53 |
} catch (error) {
|
|
|
|
| 54 |
res.status(500).send('Proxy Error');
|
| 55 |
}
|
| 56 |
});
|
| 57 |
|
| 58 |
app.listen(7860, '0.0.0.0', () => {
|
| 59 |
console.log('Server running on port 7860');
|
| 60 |
-
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
import fetch from 'node-fetch';
|
| 3 |
+
import bodyParser from 'body-parser';
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
|
| 7 |
+
// Отключаем парсинг body, чтобы получать сырые данные
|
| 8 |
+
app.use(bodyParser.raw({type: '*/*', limit: '50mb'}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
const MY_DOMAIN = 'https://wallegriffkinder-testinghf23.hf.space/';
|
| 11 |
|
| 12 |
+
app.use(async (req, res) => {
|
| 13 |
if (req.url === '/') return res.send('OK');
|
| 14 |
|
| 15 |
try {
|
| 16 |
+
// Получаем target URL, убирая наш домен из начала
|
| 17 |
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
|
| 18 |
const targetUrl = fullUrl.substring(fullUrl.indexOf(MY_DOMAIN) + MY_DOMAIN.length);
|
| 19 |
|
| 20 |
+
// Базовые CORS заголовки
|
| 21 |
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 22 |
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
| 23 |
+
res.setHeader('Access-Control-Allow-Headers', '*');
|
| 24 |
+
|
| 25 |
+
// Для OPTIONS запросов сразу возвращаем ответ
|
| 26 |
+
if (req.method === 'OPTIONS') {
|
| 27 |
+
return res.status(200).end();
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Копируем оригинальные заголовки
|
| 31 |
const headers = {...req.headers};
|
| 32 |
delete headers['host'];
|
| 33 |
delete headers['content-length'];
|
|
|
|
| 34 |
delete headers['transfer-encoding'];
|
| 35 |
+
delete headers['connection'];
|
|
|
|
| 36 |
|
| 37 |
+
// Делаем запрос
|
| 38 |
+
const fetchOptions = {
|
| 39 |
method: req.method,
|
| 40 |
headers,
|
| 41 |
+
redirect: 'follow'
|
| 42 |
+
};
|
|
|
|
| 43 |
|
| 44 |
+
// Добавляем тело только для не-GET запросов
|
| 45 |
+
if (req.method !== 'GET' && req.body) {
|
| 46 |
+
fetchOptions.body = req.body;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
const response = await fetch(targetUrl, fetchOptions);
|
| 50 |
+
|
| 51 |
+
// Копируем статус
|
| 52 |
+
res.status(response.status);
|
| 53 |
+
|
| 54 |
+
// Копируем заголовки ответа
|
| 55 |
+
for (const [key, value] of response.headers) {
|
| 56 |
if (!['connection', 'transfer-encoding'].includes(key.toLowerCase())) {
|
| 57 |
res.setHeader(key, value);
|
| 58 |
}
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
// Передаём тело ответа
|
| 62 |
response.body.pipe(res);
|
| 63 |
|
| 64 |
} catch (error) {
|
| 65 |
+
console.error(error);
|
| 66 |
res.status(500).send('Proxy Error');
|
| 67 |
}
|
| 68 |
});
|
| 69 |
|
| 70 |
app.listen(7860, '0.0.0.0', () => {
|
| 71 |
console.log('Server running on port 7860');
|
| 72 |
+
});
|