|
|
const express = require('express'); |
|
|
const cors = require('cors'); |
|
|
const axios = require('axios'); |
|
|
const cheerio = require('cheerio'); |
|
|
|
|
|
const app = express(); |
|
|
const PORT = process.env.PORT || 3000; |
|
|
|
|
|
|
|
|
app.use(cors()); |
|
|
app.use(express.json()); |
|
|
|
|
|
|
|
|
app.get('/proxy', async (req, res) => { |
|
|
try { |
|
|
const targetUrl = req.query.url; |
|
|
if (!targetUrl) { |
|
|
return res.status(400).send('URL parameter is required'); |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
new URL(targetUrl); |
|
|
} catch (e) { |
|
|
return res.status(400).send('Invalid URL'); |
|
|
} |
|
|
|
|
|
|
|
|
const response = await axios.get(targetUrl, { |
|
|
headers: { |
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
const $ = cheerio.load(response.data); |
|
|
|
|
|
|
|
|
$('a[href]').each((i, el) => { |
|
|
const href = $(el).attr('href'); |
|
|
try { |
|
|
const url = new URL(href, targetUrl); |
|
|
$(el).attr('href', `/proxy?url=${encodeURIComponent(url.href)}`); |
|
|
} catch (e) { |
|
|
|
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
$('form[action]').each((i, el) => { |
|
|
const action = $(el).attr('action'); |
|
|
try { |
|
|
const url = new URL(action, targetUrl); |
|
|
$(el).attr('action', `/proxy?url=${encodeURIComponent(url.href)}`); |
|
|
} catch (e) { |
|
|
|
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
res.set('Content-Type', response.headers['content-type']); |
|
|
res.send($.html()); |
|
|
|
|
|
} catch (error) { |
|
|
console.error('Proxy error:', error); |
|
|
res.status(500).send('Error fetching the requested URL'); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.listen(PORT, () => { |
|
|
console.log(`Proxy server running on port ${PORT}`); |
|
|
}); |