-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (61 loc) · 2.12 KB
/
app.js
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
// Updated app.js with fully functional proxy implementation and additional features
const express = require('express');
const fs = require('fs');
const { createProxyMiddleware } = require('http-proxy-middleware');
const geoip = require('geoip-lite');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Load proxy lists
const socks5List = fs.readFileSync('./proxies/socks5.txt', 'utf-8').split('\n');
const socks4List = fs.readFileSync('./proxies/socks4.txt', 'utf-8').split('\n');
const httpList = fs.readFileSync('./proxies/http.txt', 'utf-8').split('\n');
// Helper function to get proxy details
const getProxyDetails = (type, location) => {
const proxyList = {
SOCKS5: socks5List,
SOCKS4: socks4List,
HTTP: httpList,
}[type.toUpperCase()] || httpList;
// Filter by location if provided
const filteredList = location
? proxyList.filter(proxy => proxy.includes(location))
: proxyList;
// Return the first available proxy
return filteredList[0] || 'No proxy available';
};
// Middleware for proxying requests
app.use(
'/proxy',
(req, res, next) => {
const { url, type = 'SOCKS5', country = 'US' } = req.query;
if (!url) {
return res.status(400).send('Missing URL parameter');
}
const proxy = getProxyDetails(type, country);
if (proxy === 'No proxy available') {
return res.status(500).send('No proxies available for the specified criteria');
}
console.log(`Using proxy: ${proxy}`);
req.headers['X-Forwarded-For'] = proxy; // Set proxy details in header
// Use the proxy middleware
const proxyMiddleware = createProxyMiddleware({
target: url,
changeOrigin: true,
onProxyReq(proxyReq, req, res) {
proxyReq.setHeader('X-Forwarded-For', proxy);
},
});
proxyMiddleware(req, res, next);
}
);
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Main endpoint for home
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});